00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../../stdafx.h"
00013
00014 #ifdef ENABLE_NETWORK
00015
00016 #include "address.h"
00017 #include "../../debug.h"
00018
00024 const char *NetworkAddress::GetHostname()
00025 {
00026 if (StrEmpty(this->hostname) && this->address.ss_family != AF_UNSPEC) {
00027 assert(this->address_length != 0);
00028 getnameinfo((struct sockaddr *)&this->address, this->address_length, this->hostname, sizeof(this->hostname), NULL, 0, NI_NUMERICHOST);
00029 }
00030 return this->hostname;
00031 }
00032
00037 uint16 NetworkAddress::GetPort() const
00038 {
00039 switch (this->address.ss_family) {
00040 case AF_UNSPEC:
00041 case AF_INET:
00042 return ntohs(((struct sockaddr_in *)&this->address)->sin_port);
00043
00044 case AF_INET6:
00045 return ntohs(((struct sockaddr_in6 *)&this->address)->sin6_port);
00046
00047 default:
00048 NOT_REACHED();
00049 }
00050 }
00051
00056 void NetworkAddress::SetPort(uint16 port)
00057 {
00058 switch (this->address.ss_family) {
00059 case AF_UNSPEC:
00060 case AF_INET:
00061 ((struct sockaddr_in*)&this->address)->sin_port = htons(port);
00062 break;
00063
00064 case AF_INET6:
00065 ((struct sockaddr_in6*)&this->address)->sin6_port = htons(port);
00066 break;
00067
00068 default:
00069 NOT_REACHED();
00070 }
00071 }
00072
00079 void NetworkAddress::GetAddressAsString(char *buffer, const char *last, bool with_family)
00080 {
00081 if (this->GetAddress()->ss_family == AF_INET6) buffer = strecpy(buffer, "[", last);
00082 buffer = strecpy(buffer, this->GetHostname(), last);
00083 if (this->GetAddress()->ss_family == AF_INET6) buffer = strecpy(buffer, "]", last);
00084 buffer += seprintf(buffer, last, ":%d", this->GetPort());
00085
00086 if (with_family) {
00087 char family;
00088 switch (this->address.ss_family) {
00089 case AF_INET: family = '4'; break;
00090 case AF_INET6: family = '6'; break;
00091 default: family = '?'; break;
00092 }
00093 seprintf(buffer, last, " (IPv%c)", family);
00094 }
00095 }
00096
00103 const char *NetworkAddress::GetAddressAsString(bool with_family)
00104 {
00105
00106 static char buf[NETWORK_HOSTNAME_LENGTH + 6 + 7];
00107 this->GetAddressAsString(buf, lastof(buf), with_family);
00108 return buf;
00109 }
00110
00116 static SOCKET ResolveLoopProc(addrinfo *runp)
00117 {
00118
00119 return !INVALID_SOCKET;
00120 }
00121
00126 const sockaddr_storage *NetworkAddress::GetAddress()
00127 {
00128 if (!this->IsResolved()) {
00129
00130
00131
00132
00133
00134 this->Resolve(this->address.ss_family, SOCK_STREAM, AI_ADDRCONFIG, NULL, ResolveLoopProc);
00135 this->resolved = true;
00136 }
00137 return &this->address;
00138 }
00139
00145 bool NetworkAddress::IsFamily(int family)
00146 {
00147 if (!this->IsResolved()) {
00148 this->Resolve(family, SOCK_STREAM, AI_ADDRCONFIG, NULL, ResolveLoopProc);
00149 }
00150 return this->address.ss_family == family;
00151 }
00152
00159 bool NetworkAddress::IsInNetmask(char *netmask)
00160 {
00161
00162 if (!this->IsResolved()) this->GetAddress();
00163
00164 int cidr = this->address.ss_family == AF_INET ? 32 : 128;
00165
00166 NetworkAddress mask_address;
00167
00168
00169 char *chr_cidr = strchr(netmask, '/');
00170 if (chr_cidr != NULL) {
00171 int tmp_cidr = atoi(chr_cidr + 1);
00172
00173
00174 if (tmp_cidr > 0 || tmp_cidr < cidr) cidr = tmp_cidr;
00175
00176
00177 *chr_cidr = '\0';
00178 mask_address = NetworkAddress(netmask, 0, this->address.ss_family);
00179 *chr_cidr = '/';
00180 } else {
00181 mask_address = NetworkAddress(netmask, 0, this->address.ss_family);
00182 }
00183
00184 if (mask_address.GetAddressLength() == 0) return false;
00185
00186 uint32 *ip;
00187 uint32 *mask;
00188 switch (this->address.ss_family) {
00189 case AF_INET:
00190 ip = (uint32*)&((struct sockaddr_in*)&this->address)->sin_addr.s_addr;
00191 mask = (uint32*)&((struct sockaddr_in*)&mask_address.address)->sin_addr.s_addr;
00192 break;
00193
00194 case AF_INET6:
00195 ip = (uint32*)&((struct sockaddr_in6*)&this->address)->sin6_addr;
00196 mask = (uint32*)&((struct sockaddr_in6*)&mask_address.address)->sin6_addr;
00197 break;
00198
00199 default:
00200 NOT_REACHED();
00201 }
00202
00203 while (cidr > 0) {
00204 uint32 msk = cidr >= 32 ? (uint32)-1 : htonl(-(1 << (32 - cidr)));
00205 if ((*mask++ & msk) != (*ip++ & msk)) return false;
00206
00207 cidr -= 32;
00208 }
00209
00210 return true;
00211 }
00212
00222 SOCKET NetworkAddress::Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func)
00223 {
00224 struct addrinfo *ai;
00225 struct addrinfo hints;
00226 memset(&hints, 0, sizeof (hints));
00227 hints.ai_family = family;
00228 hints.ai_flags = flags;
00229 hints.ai_socktype = socktype;
00230
00231
00232 char port_name[6];
00233 seprintf(port_name, lastof(port_name), "%u", this->GetPort());
00234
00235 bool reset_hostname = false;
00236
00237
00238
00239 if (StrEmpty(this->hostname) && this->address_length == 0 && this->GetPort() == 0) {
00240 reset_hostname = true;
00241 int fam = this->address.ss_family;
00242 if (fam == AF_UNSPEC) fam = family;
00243 strecpy(this->hostname, fam == AF_INET ? "0.0.0.0" : "::", lastof(this->hostname));
00244 }
00245
00246 int e = getaddrinfo(StrEmpty(this->hostname) ? NULL : this->hostname, port_name, &hints, &ai);
00247
00248 if (reset_hostname) strecpy(this->hostname, "", lastof(this->hostname));
00249
00250 if (e != 0) {
00251 if (func != ResolveLoopProc) {
00252 DEBUG(net, 0, "getaddrinfo for hostname \"%s\", port %s, address family %s and socket type %s failed: %s",
00253 this->hostname, port_name, AddressFamilyAsString(family), SocketTypeAsString(socktype), FS2OTTD(gai_strerror(e)));
00254 }
00255 return INVALID_SOCKET;
00256 }
00257
00258 SOCKET sock = INVALID_SOCKET;
00259 for (struct addrinfo *runp = ai; runp != NULL; runp = runp->ai_next) {
00260
00261
00262
00263 if (sockets != NULL) {
00264 NetworkAddress address(runp->ai_addr, (int)runp->ai_addrlen);
00265 if (sockets->Contains(address)) continue;
00266 }
00267 sock = func(runp);
00268 if (sock == INVALID_SOCKET) continue;
00269
00270 if (sockets == NULL) {
00271 this->address_length = (int)runp->ai_addrlen;
00272 assert(sizeof(this->address) >= runp->ai_addrlen);
00273 memcpy(&this->address, runp->ai_addr, runp->ai_addrlen);
00274 break;
00275 }
00276
00277 NetworkAddress addr(runp->ai_addr, (int)runp->ai_addrlen);
00278 (*sockets)[addr] = sock;
00279 sock = INVALID_SOCKET;
00280 }
00281 freeaddrinfo (ai);
00282
00283 return sock;
00284 }
00285
00291 static SOCKET ConnectLoopProc(addrinfo *runp)
00292 {
00293 const char *type = NetworkAddress::SocketTypeAsString(runp->ai_socktype);
00294 const char *family = NetworkAddress::AddressFamilyAsString(runp->ai_family);
00295 const char *address = NetworkAddress(runp->ai_addr, (int)runp->ai_addrlen).GetAddressAsString();
00296
00297 SOCKET sock = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol);
00298 if (sock == INVALID_SOCKET) {
00299 DEBUG(net, 1, "[%s] could not create %s socket: %s", type, family, strerror(errno));
00300 return INVALID_SOCKET;
00301 }
00302
00303 if (!SetNoDelay(sock)) DEBUG(net, 1, "[%s] setting TCP_NODELAY failed", type);
00304
00305 if (connect(sock, runp->ai_addr, (int)runp->ai_addrlen) != 0) {
00306 DEBUG(net, 1, "[%s] could not connect %s socket: %s", type, family, strerror(errno));
00307 closesocket(sock);
00308 return INVALID_SOCKET;
00309 }
00310
00311
00312 if (!SetNonBlocking(sock)) DEBUG(net, 0, "[%s] setting non-blocking mode failed", type);
00313
00314 DEBUG(net, 1, "[%s] connected to %s", type, address);
00315
00316 return sock;
00317 }
00318
00323 SOCKET NetworkAddress::Connect()
00324 {
00325 DEBUG(net, 1, "Connecting to %s", this->GetAddressAsString());
00326
00327 return this->Resolve(AF_UNSPEC, SOCK_STREAM, AI_ADDRCONFIG, NULL, ConnectLoopProc);
00328 }
00329
00335 static SOCKET ListenLoopProc(addrinfo *runp)
00336 {
00337 const char *type = NetworkAddress::SocketTypeAsString(runp->ai_socktype);
00338 const char *family = NetworkAddress::AddressFamilyAsString(runp->ai_family);
00339 const char *address = NetworkAddress(runp->ai_addr, (int)runp->ai_addrlen).GetAddressAsString();
00340
00341 SOCKET sock = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol);
00342 if (sock == INVALID_SOCKET) {
00343 DEBUG(net, 0, "[%s] could not create %s socket on port %s: %s", type, family, address, strerror(errno));
00344 return INVALID_SOCKET;
00345 }
00346
00347 if (runp->ai_socktype == SOCK_STREAM && !SetNoDelay(sock)) {
00348 DEBUG(net, 3, "[%s] setting TCP_NODELAY failed for port %s", type, address);
00349 }
00350
00351 int on = 1;
00352
00353 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)) == -1) {
00354 DEBUG(net, 3, "[%s] could not set reusable %s sockets for port %s: %s", type, family, address, strerror(errno));
00355 }
00356
00357 if (runp->ai_family == AF_INET6 &&
00358 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&on, sizeof(on)) == -1) {
00359 DEBUG(net, 3, "[%s] could not disable IPv4 over IPv6 on port %s: %s", type, address, strerror(errno));
00360 }
00361
00362 if (bind(sock, runp->ai_addr, (int)runp->ai_addrlen) != 0) {
00363 DEBUG(net, 1, "[%s] could not bind on %s port %s: %s", type, family, address, strerror(errno));
00364 closesocket(sock);
00365 return INVALID_SOCKET;
00366 }
00367
00368 if (runp->ai_socktype != SOCK_DGRAM && listen(sock, 1) != 0) {
00369 DEBUG(net, 1, "[%s] could not listen at %s port %s: %s", type, family, address, strerror(errno));
00370 closesocket(sock);
00371 return INVALID_SOCKET;
00372 }
00373
00374
00375 if (!SetNonBlocking(sock)) DEBUG(net, 0, "[%s] setting non-blocking mode failed for %s port %s", type, family, address);
00376
00377 DEBUG(net, 1, "[%s] listening on %s port %s", type, family, address);
00378 return sock;
00379 }
00380
00386 void NetworkAddress::Listen(int socktype, SocketList *sockets)
00387 {
00388 assert(sockets != NULL);
00389
00390
00391
00392
00393 if (this->address_length == 0 && this->address.ss_family == AF_UNSPEC &&
00394 StrEmpty(this->hostname) && this->GetPort() == 0) {
00395 this->Resolve(AF_INET, socktype, AI_ADDRCONFIG | AI_PASSIVE, sockets, ListenLoopProc);
00396 this->Resolve(AF_INET6, socktype, AI_ADDRCONFIG | AI_PASSIVE, sockets, ListenLoopProc);
00397 } else {
00398 this->Resolve(AF_UNSPEC, socktype, AI_ADDRCONFIG | AI_PASSIVE, sockets, ListenLoopProc);
00399 }
00400 }
00401
00408 const char *NetworkAddress::SocketTypeAsString(int socktype)
00409 {
00410 switch (socktype) {
00411 case SOCK_STREAM: return "tcp";
00412 case SOCK_DGRAM: return "udp";
00413 default: return "unsupported";
00414 }
00415 }
00416
00423 const char *NetworkAddress::AddressFamilyAsString(int family)
00424 {
00425 switch (family) {
00426 case AF_UNSPEC: return "either IPv4 or IPv6";
00427 case AF_INET: return "IPv4";
00428 case AF_INET6: return "IPv6";
00429 default: return "unsupported";
00430 }
00431 }
00432
00433 #endif