Adjust log output
[umurmur.git] / src / server.c
1 /* Copyright (C) 2009-2010, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2010, Thorvald Natvig <thorvald@natvig.com>
3
4    All rights reserved.
5
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9
10    - Redistributions of source code must retain the above copyright notice,
11      this list of conditions and the following disclaimer.
12    - Redistributions in binary form must reproduce the above copyright notice,
13      this list of conditions and the following disclaimer in the documentation
14      and/or other materials provided with the distribution.
15    - Neither the name of the Developers nor the names of its contributors may
16      be used to endorse or promote products derived from this software without
17      specific prior written permission.
18
19    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <stdio.h>
32 #include <sys/time.h>
33 #include <sys/poll.h>
34 #include <netinet/tcp.h>
35 #include <sys/socket.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <limits.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <errno.h>
42
43 #include "client.h"
44 #include "conf.h"
45 #include "log.h"
46 #include "timer.h"
47 #include "version.h"
48
49 #define LISTEN_SOCK 0
50 #define TCP_SOCK 0
51 #define UDP_SOCK 1
52
53 /* globals */
54 int udpsock; 
55 bool_t shutdown_server;
56
57 void Server_run()
58 {
59         int timeout = 1000, rc;
60         struct pollfd *pollfds;
61         int tcpsock, sockopt = 1;
62         struct sockaddr_in sin;
63         int val, clientcount;
64         etimer_t janitorTimer;
65
66         /* max clients + listen sock + udp sock + client connecting that will be disconnected */
67         pollfds = malloc((getIntConf(MAX_CLIENTS) + 3) * sizeof(struct pollfd));
68         if (pollfds == NULL)
69                 Log_fatal("out of memory");
70         
71         /* Prepare TCP socket */
72         memset(&sin, 0, sizeof(sin));
73         tcpsock = socket(PF_INET, SOCK_STREAM, 0);
74         if (tcpsock < 0)
75                 Log_fatal("socket");
76         if (setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(int)) != 0)
77                 Log_fatal("setsockopt: %s", strerror(errno));
78         sin.sin_family = AF_INET;
79         sin.sin_port = htons(getIntConf(BINDPORT));
80         sin.sin_addr.s_addr = inet_addr(getStrConf(BINDADDR)) ==  -1 ? inet_addr("0.0.0.0") : inet_addr(getStrConf(BINDADDR));
81         rc = bind(tcpsock, (struct sockaddr *) &sin, sizeof (struct sockaddr_in));
82         if (rc < 0) Log_fatal("bind: %s", strerror(errno));
83         rc = listen(tcpsock, 3);
84         if (rc < 0) Log_fatal("listen");
85         fcntl(tcpsock, F_SETFL, O_NONBLOCK);
86         
87         pollfds[LISTEN_SOCK].fd = tcpsock;
88         pollfds[LISTEN_SOCK].events = POLLIN;
89
90         /* Prepare UDP socket */
91         memset(&sin, 0, sizeof(sin));
92         udpsock = socket(PF_INET, SOCK_DGRAM, 0);
93         sin.sin_family = AF_INET;
94         sin.sin_port = htons(getIntConf(BINDPORT));
95         sin.sin_addr.s_addr = inet_addr(getStrConf(BINDADDR)) ==  -1 ? inet_addr("0.0.0.0") : inet_addr(getStrConf(BINDADDR));
96         rc = bind(udpsock, (struct sockaddr *) &sin, sizeof (struct sockaddr_in));
97         if (rc < 0)
98                 Log_fatal("bind %d %s: %s", getIntConf(BINDPORT), getStrConf(BINDADDR), strerror(errno));
99         val = 0xe0;
100         rc = setsockopt(udpsock, IPPROTO_IP, IP_TOS, &val, sizeof(val));
101         if (rc < 0)
102                 Log_fatal("Server: Failed to set TOS for UDP Socket");
103         val = 0x80;
104         rc = setsockopt(udpsock, IPPROTO_IP, IP_TOS, &val, sizeof(val));
105         if (rc < 0)
106                 Log_fatal("Server: Failed to set TOS for UDP Socket");
107         
108         fcntl(udpsock, F_SETFL, O_NONBLOCK);
109         pollfds[UDP_SOCK].fd = udpsock;
110         pollfds[UDP_SOCK].events = POLLIN | POLLHUP | POLLERR;
111         
112         Timer_init(&janitorTimer);
113         
114         Log_info("uMurmur version %s protocol version %d.%d.%d",
115                          UMURMUR_VERSION, PROTVER_MAJOR, PROTVER_MINOR, PROTVER_PATCH);
116         Log_info("Visit http://code.google.com/p/umurmur/");
117         
118         /* Main server loop */
119         while (!shutdown_server) {
120                 struct sockaddr_in remote;
121                 int i;
122                 
123                 pollfds[UDP_SOCK].revents = 0;
124                 pollfds[TCP_SOCK].revents = 0;
125                 clientcount = Client_getfds(&pollfds[2]);
126                 
127                 timeout = (int)(1000000LL - (int64_t)Timer_elapsed(&janitorTimer)) / 1000LL;
128                 if (timeout <= 0) {
129                         Client_janitor();
130                         Timer_restart(&janitorTimer);
131                         timeout = (int)(1000000LL - (int64_t)Timer_elapsed(&janitorTimer)) / 1000LL;
132                 }
133                 rc = poll(pollfds, clientcount + 2, timeout);
134                 if (rc == 0) { /* Timeout */
135                         /* Do maintenance */
136                         Timer_restart(&janitorTimer);
137                         Client_janitor();
138                         continue;
139                 }
140                 if (rc < 0) {
141                         if (errno == EINTR) /* signal */
142                                 continue;
143                         else
144                                 Log_fatal("poll: error %d", errno);
145                 }
146                 if (pollfds[LISTEN_SOCK].revents) { /* New tcp connection */
147                         int tcpfd, flag = 1;
148                         uint32_t addrlen;
149                         addrlen = sizeof(struct sockaddr_in);
150                         tcpfd = accept(pollfds[LISTEN_SOCK].fd, (struct sockaddr*)&remote, &addrlen);
151                         fcntl(tcpfd, F_SETFL, O_NONBLOCK);
152                         setsockopt(tcpfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
153                         Log_debug("Connection from %s port %d\n", inet_ntoa(remote.sin_addr),
154                                           ntohs(remote.sin_port));
155                         Client_add(tcpfd, &remote);
156                 }
157
158                 if (pollfds[UDP_SOCK].revents) {
159                         Client_read_udp();
160                 }
161                 for (i = 0; i < clientcount; i++) {
162                         if (pollfds[i + 2].revents & POLLIN) {
163                                 Client_read_fd(pollfds[i + 2].fd);
164                         }
165                         if (pollfds[i + 2].revents & POLLOUT) {
166                                 Client_write_fd(pollfds[i + 2].fd);
167                         }
168                 }
169         }       
170
171         /* Disconnect clients */
172         Client_disconnect_all();
173         free(pollfds);
174 }
175
176 void Server_shutdown()
177 {
178         shutdown_server = true;
179 }