Update copyright year.
[umurmur.git] / src / server.c
1 /* Copyright (C) 2009-2013, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2013, 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 #include <stdlib.h>
43
44 #include "client.h"
45 #include "conf.h"
46 #include "log.h"
47 #include "timer.h"
48 #include "version.h"
49
50 #define LISTEN_SOCK 0
51 #define TCP_SOCK 0
52 #define UDP_SOCK 1
53
54 /* globals */
55 int udpsock; 
56 bool_t shutdown_server;
57 extern char *bindaddr;
58 extern int bindport;
59
60 void Server_run()
61 {
62         int timeout = 1000, rc;
63         struct pollfd *pollfds;
64         int tcpsock, sockopt = 1;
65         struct sockaddr_in sin;
66         int val, clientcount;
67         etimer_t janitorTimer;
68         unsigned short port;
69         in_addr_t inet_address;
70         
71         /* max clients + listen sock + udp sock + client connecting that will be disconnected */
72         pollfds = malloc((getIntConf(MAX_CLIENTS) + 3) * sizeof(struct pollfd));
73         if (pollfds == NULL)
74                 Log_fatal("out of memory");
75
76         /* Figure out bind address and port */
77         if (bindport != 0)
78                 port = htons(bindport);
79         else
80                 port = htons(getIntConf(BINDPORT));
81         
82         if (bindaddr != NULL && inet_addr(bindaddr) != -1)
83                 inet_address = inet_addr(bindaddr);
84         else if (inet_addr(getStrConf(BINDADDR)) !=  -1)
85                 inet_address = inet_addr(getStrConf(BINDADDR));
86         else
87                 inet_address = inet_addr("0.0.0.0");
88         Log_info("Bind to %s:%hu", inet_address == 0 ? "*" : inet_ntoa(*((struct in_addr *)&inet_address)), ntohs(port));
89         
90         /* Prepare TCP socket */
91         memset(&sin, 0, sizeof(sin));
92         tcpsock = socket(PF_INET, SOCK_STREAM, 0);
93         if (tcpsock < 0)
94                 Log_fatal("socket");
95         if (setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(int)) != 0)
96                 Log_fatal("setsockopt: %s", strerror(errno));
97         sin.sin_family = AF_INET;
98         sin.sin_port = port;    
99         sin.sin_addr.s_addr = inet_address;
100         
101         rc = bind(tcpsock, (struct sockaddr *) &sin, sizeof (struct sockaddr_in));
102         if (rc < 0) Log_fatal("bind: %s", strerror(errno));
103         rc = listen(tcpsock, 3);
104         if (rc < 0) Log_fatal("listen");
105         fcntl(tcpsock, F_SETFL, O_NONBLOCK);
106         
107         pollfds[LISTEN_SOCK].fd = tcpsock;
108         pollfds[LISTEN_SOCK].events = POLLIN;
109
110         /* Prepare UDP socket */
111         memset(&sin, 0, sizeof(sin));
112         udpsock = socket(PF_INET, SOCK_DGRAM, 0);
113         sin.sin_family = AF_INET;
114         sin.sin_port = port;
115         sin.sin_addr.s_addr = inet_address;
116         
117         rc = bind(udpsock, (struct sockaddr *) &sin, sizeof (struct sockaddr_in));
118         if (rc < 0)
119                 Log_fatal("bind %d %s: %s", getIntConf(BINDPORT), getStrConf(BINDADDR), strerror(errno));
120         val = 0xe0;
121         rc = setsockopt(udpsock, IPPROTO_IP, IP_TOS, &val, sizeof(val));
122         if (rc < 0)
123                 Log_warn("Server: Failed to set TOS for UDP Socket");
124         val = 0x80;
125         rc = setsockopt(udpsock, IPPROTO_IP, IP_TOS, &val, sizeof(val));
126         if (rc < 0)
127                 Log_warn("Server: Failed to set TOS for UDP Socket");
128         
129         fcntl(udpsock, F_SETFL, O_NONBLOCK);
130         pollfds[UDP_SOCK].fd = udpsock;
131         pollfds[UDP_SOCK].events = POLLIN | POLLHUP | POLLERR;
132         
133         Timer_init(&janitorTimer);
134         
135         Log_info("uMurmur version %s ('%s') protocol version %d.%d.%d",
136                  UMURMUR_VERSION, UMURMUR_CODENAME, PROTVER_MAJOR, PROTVER_MINOR, PROTVER_PATCH);
137         Log_info("Visit http://code.google.com/p/umurmur/");
138         
139         /* Main server loop */
140         while (!shutdown_server) {
141                 struct sockaddr_in remote;
142                 int i;
143                 
144                 pollfds[UDP_SOCK].revents = 0;
145                 pollfds[TCP_SOCK].revents = 0;
146                 clientcount = Client_getfds(&pollfds[2]);
147                 
148                 timeout = (int)(1000000LL - (int64_t)Timer_elapsed(&janitorTimer)) / 1000LL;
149                 if (timeout <= 0) {
150                         Client_janitor();
151                         Timer_restart(&janitorTimer);
152                         timeout = (int)(1000000LL - (int64_t)Timer_elapsed(&janitorTimer)) / 1000LL;
153                 }
154                 rc = poll(pollfds, clientcount + 2, timeout);
155                 if (rc == 0) { /* Timeout */
156                         /* Do maintenance */
157                         Timer_restart(&janitorTimer);
158                         Client_janitor();
159                         continue;
160                 }
161                 if (rc < 0) {
162                         if (errno == EINTR) /* signal */
163                                 continue;
164                         else
165                                 Log_fatal("poll: error %d", errno);
166                 }
167                 if (pollfds[LISTEN_SOCK].revents) { /* New tcp connection */
168                         int tcpfd, flag = 1;
169                         uint32_t addrlen;
170                         addrlen = sizeof(struct sockaddr_in);
171                         tcpfd = accept(pollfds[LISTEN_SOCK].fd, (struct sockaddr*)&remote, &addrlen);
172                         fcntl(tcpfd, F_SETFL, O_NONBLOCK);
173                         setsockopt(tcpfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
174                         Log_debug("Connection from %s port %d\n", inet_ntoa(remote.sin_addr),
175                                           ntohs(remote.sin_port));
176                         if (Client_add(tcpfd, &remote) < 0)
177                                 close(tcpfd);
178                 }
179
180                 if (pollfds[UDP_SOCK].revents) {
181                         Client_read_udp();
182                 }
183                 for (i = 0; i < clientcount; i++) {
184                         if (pollfds[i + 2].revents & POLLIN) {
185                                 Client_read_fd(pollfds[i + 2].fd);
186                         }
187                         if (pollfds[i + 2].revents & POLLOUT) {
188                                 Client_write_fd(pollfds[i + 2].fd);
189                         }
190                 }
191         }       
192
193         /* Disconnect clients */
194         Client_disconnect_all();
195         free(pollfds);
196 }
197
198 void Server_shutdown()
199 {
200         shutdown_server = true;
201 }