X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=src%2Fserver.c;h=7e0c4c6f8d82eb1cef756d194c21f393489f1d88;hb=4c431fe65269e9b1d452855b9df8cfe80683b691;hp=e63684498cbff1a9c9423dd1c476090cf9a211b7;hpb=14a67bc74c81651015bbbad075f7eb1c41218cf7;p=umurmur.git diff --git a/src/server.c b/src/server.c index e636844..7e0c4c6 100644 --- a/src/server.c +++ b/src/server.c @@ -1,5 +1,5 @@ -/* Copyright (C) 2009-2010, Martin Johansson - Copyright (C) 2005-2010, Thorvald Natvig +/* Copyright (C) 2009-2012, Martin Johansson + Copyright (C) 2005-2012, Thorvald Natvig All rights reserved. @@ -54,6 +54,8 @@ /* globals */ int udpsock; bool_t shutdown_server; +extern char *bindaddr; +extern int bindport; void Server_run() { @@ -63,11 +65,27 @@ void Server_run() struct sockaddr_in sin; int val, clientcount; etimer_t janitorTimer; - + unsigned short port; + in_addr_t inet_address; + /* max clients + listen sock + udp sock + client connecting that will be disconnected */ pollfds = malloc((getIntConf(MAX_CLIENTS) + 3) * sizeof(struct pollfd)); if (pollfds == NULL) Log_fatal("out of memory"); + + /* Figure out bind address and port */ + if (bindport != 0) + port = htons(bindport); + else + port = htons(getIntConf(BINDPORT)); + + if (bindaddr != NULL && inet_addr(bindaddr) != -1) + inet_address = inet_addr(bindaddr); + else if (inet_addr(getStrConf(BINDADDR)) != -1) + inet_address = inet_addr(getStrConf(BINDADDR)); + else + inet_address = inet_addr("0.0.0.0"); + Log_info("Bind to %s:%hu", inet_address == 0 ? "*" : inet_ntoa(*((struct in_addr *)&inet_address)), ntohs(port)); /* Prepare TCP socket */ memset(&sin, 0, sizeof(sin)); @@ -77,8 +95,9 @@ void Server_run() if (setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(int)) != 0) Log_fatal("setsockopt: %s", strerror(errno)); sin.sin_family = AF_INET; - sin.sin_port = htons(getIntConf(BINDPORT)); - sin.sin_addr.s_addr = inet_addr(getStrConf(BINDADDR)) == -1 ? inet_addr("0.0.0.0") : inet_addr(getStrConf(BINDADDR)); + sin.sin_port = port; + sin.sin_addr.s_addr = inet_address; + rc = bind(tcpsock, (struct sockaddr *) &sin, sizeof (struct sockaddr_in)); if (rc < 0) Log_fatal("bind: %s", strerror(errno)); rc = listen(tcpsock, 3); @@ -92,19 +111,20 @@ void Server_run() memset(&sin, 0, sizeof(sin)); udpsock = socket(PF_INET, SOCK_DGRAM, 0); sin.sin_family = AF_INET; - sin.sin_port = htons(getIntConf(BINDPORT)); - sin.sin_addr.s_addr = inet_addr(getStrConf(BINDADDR)) == -1 ? inet_addr("0.0.0.0") : inet_addr(getStrConf(BINDADDR)); + sin.sin_port = port; + sin.sin_addr.s_addr = inet_address; + rc = bind(udpsock, (struct sockaddr *) &sin, sizeof (struct sockaddr_in)); if (rc < 0) Log_fatal("bind %d %s: %s", getIntConf(BINDPORT), getStrConf(BINDADDR), strerror(errno)); val = 0xe0; rc = setsockopt(udpsock, IPPROTO_IP, IP_TOS, &val, sizeof(val)); if (rc < 0) - Log_fatal("Server: Failed to set TOS for UDP Socket"); + Log_warn("Server: Failed to set TOS for UDP Socket"); val = 0x80; rc = setsockopt(udpsock, IPPROTO_IP, IP_TOS, &val, sizeof(val)); if (rc < 0) - Log_fatal("Server: Failed to set TOS for UDP Socket"); + Log_warn("Server: Failed to set TOS for UDP Socket"); fcntl(udpsock, F_SETFL, O_NONBLOCK); pollfds[UDP_SOCK].fd = udpsock; @@ -153,7 +173,8 @@ void Server_run() setsockopt(tcpfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)); Log_debug("Connection from %s port %d\n", inet_ntoa(remote.sin_addr), ntohs(remote.sin_port)); - Client_add(tcpfd, &remote); + if (Client_add(tcpfd, &remote) < 0) + close(tcpfd); } if (pollfds[UDP_SOCK].revents) {