MCPcopy Create free account
hub / github.com/BohemiaInteractive/CWR / getLocalAddress

Function getLocalAddress

engine/Poseidon/Network/Legacy/NetPeer.cpp:73–126  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

71
72#endif
73
74bool getLocalAddress(struct sockaddr_in& me, unsigned16 port)
75{
76 me.sin_family = AF_INET;
77 char meName[128];
78 if (gethostname(meName, 128) == SOCKET_ERROR)
79 {
80#ifdef NET_LOG_GET_LOCAL_ADDRESS
81#ifdef _WIN32
82 NetLog("getLocalAddress: gethostname() failed with error: %d", WSAGetLastError());
83#else
84 NetLog("getLocalAddress: gethostname() failed!");
85#endif
86#endif
87 return false;
88 }
89#ifdef NET_LOG_GET_LOCAL_ADDRESS
90 NetLog("getLocalAddress: local hostname: %s", meName);
91#endif
92 if (isalpha(meName[0]))
93 {
94 struct hostent* h = gethostbyname(meName);
95 if (!h)
96 {
97#ifdef NET_LOG_GET_LOCAL_ADDRESS
98#ifdef _WIN32
99 NetLog("getLocalAddress: gethostbyname(%s) failed with error: %d", meName, WSAGetLastError());
100#else
101 NetLog("getLocalAddress: gethostbyname(%s) failed!", meName);
102#endif
103#endif
104 return false;
105 }
106 if (h->h_length < 4)
107 {
108#ifdef NET_LOG_GET_LOCAL_ADDRESS
109 NetLog("getLocalAddress: invalid network address type!");
110#endif
111 return false;
112 }
113 memcpy(&(me.sin_addr.s_addr), h->h_addr, 4);
114#ifdef NET_LOG_GET_LOCAL_ADDRESS
115 NetLog("getLocalAddress: resolved local hostname: %s (%s)", h->h_name, inet_ntoa(me.sin_addr));
116#endif
117 }
118 else
119 {
120 me.sin_addr.s_addr = inet_addr(meName);
121 }
122 me.sin_port = htons(port);
123#ifdef NET_LOG_GET_LOCAL_ADDRESS
124 NetLog("getLocalAddress: local port: %u", (unsigned)port);
125#endif
126 return true;
127}
128
129bool getLocalName(char* name, unsigned len)

Callers 5

StartEnumHostsMethod · 0.85
GetURLMethod · 0.85
getLocalAddressMethod · 0.85
getPeerInfoMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected