Factor sockaddr_ntop() out of player_accept()
Oops on inet_ntop() failure instead of merely logging it.
This commit is contained in:
parent
6a0aed830c
commit
ffec91da99
1 changed files with 27 additions and 13 deletions
|
@ -55,6 +55,8 @@ static struct emp_qelem Players;
|
||||||
static int player_socket;
|
static int player_socket;
|
||||||
static size_t player_addrlen;
|
static size_t player_addrlen;
|
||||||
|
|
||||||
|
static const char *sockaddr_ntop(struct sockaddr *, char *, size_t);
|
||||||
|
|
||||||
void
|
void
|
||||||
player_init(void)
|
player_init(void)
|
||||||
{
|
{
|
||||||
|
@ -167,7 +169,6 @@ player_accept(void *unused)
|
||||||
{
|
{
|
||||||
static int conn_cnt;
|
static int conn_cnt;
|
||||||
struct sockaddr *sap;
|
struct sockaddr *sap;
|
||||||
void *inaddr;
|
|
||||||
int s = player_socket;
|
int s = player_socket;
|
||||||
struct player *np;
|
struct player *np;
|
||||||
socklen_t len;
|
socklen_t len;
|
||||||
|
@ -199,21 +200,11 @@ player_accept(void *unused)
|
||||||
close(ns);
|
close(ns);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
#ifdef HAVE_GETADDRINFO
|
if (!sockaddr_ntop(sap, np->hostaddr, sizeof(np->hostaddr))) {
|
||||||
inaddr = sap->sa_family == AF_INET
|
CANT_REACH();
|
||||||
? (void *)&((struct sockaddr_in *)sap)->sin_addr
|
|
||||||
: (void *)&((struct sockaddr_in6 *)sap)->sin6_addr;
|
|
||||||
/* Assumes that if you got getaddrinfo(), you got inet_ntop() too */
|
|
||||||
if (!inet_ntop(sap->sa_family, inaddr,
|
|
||||||
np->hostaddr, sizeof(np->hostaddr))) {
|
|
||||||
logerror("inet_ntop() failed: %s", strerror(errno));
|
|
||||||
player_delete(np);
|
player_delete(np);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
inaddr = &((struct sockaddr_in *)sap)->sin_addr;
|
|
||||||
strcpy(np->hostaddr, inet_ntoa(*(struct in_addr *)inaddr));
|
|
||||||
#endif
|
|
||||||
#ifdef RESOLVE_IPADDRESS
|
#ifdef RESOLVE_IPADDRESS
|
||||||
hostp = gethostbyaddr(inaddr, player_addrlen, sap->sa_family);
|
hostp = gethostbyaddr(inaddr, player_addrlen, sap->sa_family);
|
||||||
if (NULL != hostp)
|
if (NULL != hostp)
|
||||||
|
@ -227,3 +218,26 @@ player_accept(void *unused)
|
||||||
empth_create(player_login, stacksize, 0, buf, np);
|
empth_create(player_login, stacksize, 0, buf, np);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char *
|
||||||
|
sockaddr_ntop(struct sockaddr *sap, char *buf, size_t bufsz)
|
||||||
|
{
|
||||||
|
#ifdef HAVE_GETADDRINFO
|
||||||
|
/* Assumes that if you got getaddrinfo(), you got inet_ntop() too */
|
||||||
|
void *inaddr;
|
||||||
|
|
||||||
|
inaddr = sap->sa_family == AF_INET
|
||||||
|
? (void *)&((struct sockaddr_in *)sap)->sin_addr
|
||||||
|
: (void *)&((struct sockaddr_in6 *)sap)->sin6_addr;
|
||||||
|
return inet_ntop(sap->sa_family, inaddr, buf, bufsz);
|
||||||
|
#else
|
||||||
|
const char *p;
|
||||||
|
|
||||||
|
p = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
|
||||||
|
if (strlen(p) >= bufsz) {
|
||||||
|
errno = ENOSPC;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return strcpy(buf, p);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue