|
From: <bul...@us...> - 2013-06-02 20:16:10
|
Revision: 22792
http://sourceforge.net/p/bzflag/code/22792
Author: bullet_catcher
Date: 2013-06-02 20:16:08 +0000 (Sun, 02 Jun 2013)
Log Message:
-----------
Encapsulate two different calls to gethostname() into a new bz_gethostbyname() function.
Modified Paths:
--------------
trunk/bzflag/src/net/Address.cxx
Modified: trunk/bzflag/src/net/Address.cxx
===================================================================
--- trunk/bzflag/src/net/Address.cxx 2013-06-02 17:45:29 UTC (rev 22791)
+++ trunk/bzflag/src/net/Address.cxx 2013-06-02 20:16:08 UTC (rev 22792)
@@ -129,27 +129,34 @@
return 4;
}
+static const struct hostent* bz_gethostbyname(const std::string &name)
+{
+ const struct hostent* hent = NULL;
+
+ if (name.length() > 0)
+ hent = gethostbyname(name.c_str());
+ else {
+ char hostname[MAXHOSTNAMELEN+1];
+ if (gethostname(hostname, sizeof(hostname)) >= 0)
+ // use our own host name if none is specified
+ hent = gethostbyname(hostname);
+ }
+ return hent;
+}
+
Address Address::getHostAddress(const std::string &hname)
{
Address a;
InAddr tempAddr;
int j;
- struct hostent* hent;
- if (hname == "") { // local address
- char hostname[MAXHOSTNAMELEN+1];
- if (gethostname(hostname, sizeof(hostname)) >= 0)
- hent = gethostbyname(hostname);
- else
- return a;
- } else if (inet_aton(hname.c_str(), &tempAddr) != 0) {
+ if (hname.length() > 0 && inet_aton(hname.c_str(), &tempAddr) != 0) {
a.addr.clear();
a.addr.push_back(tempAddr);
return a;
- } else { // non-local address
- hent = gethostbyname(hname.c_str());
}
+ const struct hostent* hent = bz_gethostbyname(hname);
if (!hent) {
herror("Looking up host name");
return a;
@@ -177,16 +184,7 @@
const std::string Address::getHostName(const std::string &hostname) // const
{
- char myname[MAXHOSTNAMELEN+1];
- std::string name = hostname;
- if (name.length() <= 0) {
- if (gethostname(myname, sizeof(myname)) >= 0)
- name = std::string(myname);
- }
- if (name.length() <= 0) {
- return std::string();
- }
- struct hostent* hent = gethostbyname(name.c_str());
+ const struct hostent* hent = bz_gethostbyname(hostname);
if (!hent) {
return std::string();
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|