+ if (listen(sockets[0], 3) < 0)
+ Log_fatal("listen IPv4");
+ fcntl(sockets[0], F_SETFL, O_NONBLOCK);
+
+ pollfds[0].fd = sockets[0];
+ pollfds[0].events = POLLIN;
+ }
+
+ if (hasv6) {
+ /* IPv6 socket setup */
+ sockets[1] = socket(PF_INET6, SOCK_STREAM, 0);
+ if (sockets[1] < 0)
+ Log_fatal("socket IPv6: %s", strerror(errno));
+ if (setsockopt(sockets[1], SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) != 0)
+ Log_fatal("setsockopt IPv6: %s", strerror(errno));
+ if (setsockopt(sockets[1], IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(int)) != 0)
+ Log_fatal("setsockopt IPv6: %s", strerror(errno));
+ if (bind(sockets[1], (struct sockaddr *)addresses[1], sizeof (struct sockaddr_in6)) < 0) {
+ char *addressString = Util_addressToString(addresses[1]);
+ Log_fatal("bind %s %d: %s", addressString, Util_addressToPort(addresses[1]), strerror(errno));
+ free(addressString);
+ }
+ if (listen(sockets[1], 3) < 0)
+ Log_fatal("listen IPv6");
+ fcntl(sockets[1], F_SETFL, O_NONBLOCK);
+
+
+ /* If there is an IPv4 address, then IPv6 will use the second socket, otherwise it uses the first */
+ pollfds[(hasv4) ? 1 : 0].fd = sockets[1];
+ pollfds[(hasv4) ? 1 : 0].events = POLLIN;
+ }
+}
+
+void Server_setupUDPSockets(struct sockaddr_storage* addresses[2], struct pollfd* pollfds)
+{
+ int val = 0;
+ int sockets[2] = {-1, -1};
+
+ udpsocks = Memory_safeCalloc(nofServerSocks / 2, sizeof(int));
+
+ if (hasv4) {
+ sockets[0] = socket(PF_INET, SOCK_DGRAM, 0);
+ if (bind(sockets[0], (struct sockaddr *) addresses[0], sizeof (struct sockaddr_in)) < 0) {
+ char *addressString = Util_addressToString(addresses[0]);
+ Log_fatal("bind %s %d: %s", addressString, Util_addressToPort(addresses[0]), strerror(errno));
+ free(addressString);
+ }
+ val = 0xe0;
+ if (setsockopt(sockets[0], IPPROTO_IP, IP_TOS, &val, sizeof(val)) < 0)
+ Log_warn("Server: Failed to set TOS for UDP Socket");
+ val = 0x80;
+ if (setsockopt(sockets[0], IPPROTO_IP, IP_TOS, &val, sizeof(val)) < 0)
+ Log_warn("Server: Failed to set TOS for UDP Socket");
+
+ fcntl(sockets[0], F_SETFL, O_NONBLOCK);
+ pollfds[(hasv6) ? 2 : 1].fd = sockets[0];
+ pollfds[(hasv6) ? 2 : 1].events = POLLIN | POLLHUP | POLLERR;
+ udpsocks[0] = sockets[0];
+ }
+
+ if (hasv6) {
+ sockets[1] = socket(PF_INET6, SOCK_DGRAM, 0);
+ if (setsockopt(sockets[1], IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(int)) != 0)
+ Log_fatal("setsockopt IPv6: %s", strerror(errno));
+ if (bind(sockets[1], (struct sockaddr *) addresses[1], sizeof (struct sockaddr_in6)) < 0) {
+ char *addressString = Util_addressToString(addresses[1]);
+ Log_fatal("bind %s %d: %s", addressString, Util_addressToPort(addresses[1]), strerror(errno));
+ free(addressString);
+ }
+ val = 0xe0;
+ if (setsockopt(sockets[1], IPPROTO_IPV6, IPV6_TCLASS, &val, sizeof(val)) < 0)
+ Log_warn("Server: Failed to set TOS for UDP Socket");
+ val = 0x80;
+ if (setsockopt(sockets[1], IPPROTO_IPV6, IPV6_TCLASS, &val, sizeof(val)) < 0)
+ Log_warn("Server: Failed to set TOS for UDP Socket");
+
+ fcntl(sockets[1], F_SETFL, O_NONBLOCK);
+ pollfds[(hasv4) ? 3 : 1].fd = sockets[1];
+ pollfds[(hasv4) ? 3 : 1].events = POLLIN | POLLHUP | POLLERR;
+ udpsocks[(hasv4) ? 1 : 0] = sockets[1];
+ }
+
+}
+
+void Server_run()
+{
+ struct pollfd *pollfds;
+
+ checkIPversions();
+
+ /* max clients + server sokets + client connecting that will be disconnected */
+ pollfds = Memory_safeCalloc((getIntConf(MAX_CLIENTS) + nofServerSocks + 1) , sizeof(struct pollfd));
+
+ /* Figure out bind address and port */
+ struct sockaddr_storage** addresses = Server_setupAddressesAndPorts();
+
+ /* Prepare TCP sockets */
+ Server_setupTCPSockets(addresses, pollfds);
+
+ /* Prepare UDP sockets */
+ Server_setupUDPSockets(addresses, pollfds);
+
+ Log_info("uMurmur version %s ('%s') protocol version %d.%d.%d",
+ UMURMUR_VERSION, UMURMUR_CODENAME, PROTVER_MAJOR, PROTVER_MINOR, PROTVER_PATCH);
+ Log_info("Visit http://code.google.com/p/umurmur/");
+
+ /* Main server loop */
+ Server_runLoop(pollfds);