Code cleanup

This commit is contained in:
angt
2016-03-07 14:53:32 +00:00
parent 97a5b79f82
commit 994f0e61c9

34
mud.c
View File

@@ -450,21 +450,15 @@ int mud_set_key (struct mud *mud, unsigned char *key, size_t size)
return 0; return 0;
} }
struct mud *mud_create (const char *port) static
int mud_create_socket (const char *port)
{ {
struct mud *mud = calloc(1, sizeof(struct mud));
if (!mud)
return NULL;
mud->fd = -1;
struct addrinfo *p, *ai = mud_addrinfo(NULL, port, AI_PASSIVE|AI_NUMERICSERV); struct addrinfo *p, *ai = mud_addrinfo(NULL, port, AI_PASSIVE|AI_NUMERICSERV);
if (!ai) { if (!ai)
mud_delete(mud); return -1;
return NULL;
} int fd = -1;
for (p = ai; p; p = p->ai_next) { for (p = ai; p; p = p->ai_next) {
if (p->ai_family != AF_INET6) if (p->ai_family != AF_INET6)
@@ -473,7 +467,7 @@ struct mud *mud_create (const char *port)
if (!p->ai_addr) if (!p->ai_addr)
continue; continue;
int fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol); fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (fd == -1) if (fd == -1)
continue; continue;
@@ -485,15 +479,27 @@ struct mud *mud_create (const char *port)
mud_set_nonblock(fd) || mud_set_nonblock(fd) ||
bind(fd, p->ai_addr, p->ai_addrlen)) { bind(fd, p->ai_addr, p->ai_addrlen)) {
close(fd); close(fd);
fd = -1;
continue; continue;
} }
mud->fd = fd;
break; break;
} }
freeaddrinfo(ai); freeaddrinfo(ai);
return fd;
}
struct mud *mud_create (const char *port)
{
struct mud *mud = calloc(1, sizeof(struct mud));
if (!mud)
return NULL;
mud->fd = mud_create_socket(port);
if (mud->fd == -1) { if (mud->fd == -1) {
mud_delete(mud); mud_delete(mud);
return NULL; return NULL;