memset(ban, 0, sizeof(ban_t));
memcpy(ban->hash, client->hash, 20);
- if (client->remote_tcp.ss_family == AF_INET) {
- memcpy(&ban->address, &(((struct sockaddr_in*)&client->remote_tcp)->sin_addr), sizeof(in_addr_t));
- ban->mask = sizeof(in_addr_t);
- } else {
- memcpy(&ban->address, &(((struct sockaddr_in6*)&client->remote_tcp)->sin6_addr), 4 * sizeof(in_addr_t));
- ban->mask = 4 * sizeof(in_addr_t);
- }
+ if (client->remote_tcp.ss_family == AF_INET) {
+ memcpy(&ban->address, &(((struct sockaddr_in*)&client->remote_tcp)->sin_addr), sizeof(in_addr_t));
+ ban->mask = sizeof(in_addr_t);
+ } else {
+ memcpy(&ban->address, &(((struct sockaddr_in6*)&client->remote_tcp)->sin6_addr), 4 * sizeof(in_addr_t));
+ ban->mask = 4 * sizeof(in_addr_t);
+ }
ban->reason = strdup(reason);
ban->name = strdup(client->username);
ban->time = time(NULL);
SSLi_hash2hex(ban->hash, hexhash);
- char addressPresentation[INET6_ADDRSTRLEN];
-
- if(client->remote_tcp.ss_family == AF_INET)
- inet_ntop(AF_INET, &((struct sockaddr_in*)&client->remote_tcp)->sin_addr, addressPresentation, INET6_ADDRSTRLEN);
- else
- inet_ntop(AF_INET6, &((struct sockaddr_in6*)&client->remote_tcp)->sin6_addr, addressPresentation, INET6_ADDRSTRLEN);
-
Log_info_client(client, "User kickbanned. Reason: '%s' Hash: %s IP: %s Banned for: %d seconds",
- ban->reason, hexhash, addressPresentation, ban->duration);
+ ban->reason, hexhash, client->addressString, ban->duration);
}
#ifdef DEBUG
SSLi_hash2hex(ban->hash, hexhash);
Log_debug("BL: User %s Reason: '%s' Hash: %s IP: %s Time left: %d",
- ban->name, ban->reason, hexhash, inet_ntoa(*((struct in_addr *)&ban->address)),
- bantime_long / 1000000LL - Timer_elapsed(&ban->startTime) / 1000000LL);
+ ban->name, ban->reason, hexhash, inet_ntoa(*((struct in_addr *)&ban->address)),
+ bantime_long / 1000000LL - Timer_elapsed(&ban->startTime) / 1000000LL);
#endif
/* Duration of 0 = forever */
if (ban->duration != 0 && Timer_isElapsed(&ban->startTime, bantime_long)) {
memcpy(&address[12], &ban->address, 4);
memset(&address[10], 0xff, 2); /* IPv4 */
Msg_banList_addEntry(msg, i++, address, ban->mask, ban->name,
- hexhash, ban->reason, timestr, ban->duration);
+ hexhash, ban->reason, timestr, ban->duration);
}
return msg;
}
ban = list_get_entry(itr, ban_t, node);
SSLi_hash2hex(ban->hash, hexhash);
fprintf(file, "%s,%s,%d,%ld,%d,%s,%s\n", hexhash, inet_ntoa(*((struct in_addr *)&ban->address)),
- ban->mask, (long int)ban->time, ban->duration, ban->name, ban->reason);
+ ban->mask, (long int)ban->time, ban->duration, ban->name, ban->reason);
}
fclose(file);
banlist_changed = false;
CryptState_encrypt(&client->cryptState, data, buf, len);
+ // Maybe an OS X loopback quirk
if (client->remote_udp.ss_family == AF_INET)
sendto(udpsock, buf, len + 4, 0, (struct sockaddr *)&client->remote_udp, sizeof(struct sockaddr_in));
else
struct sockaddr_storage remote_tcp;
struct sockaddr_storage remote_udp;
- char addressString[INET6_ADDRSTRLEN];
+ char addressString[INET6_ADDRSTRLEN];
uint8_t rxbuf[BUFSIZE], txbuf[BUFSIZE];
uint32_t rxcount, msgsize, drainleft, txcount, txsize;
int sessionId;
conffile = defaultconfig;
if (config_read_file(&configuration, conffile) != CONFIG_TRUE) {
Log_fatal("Error in config file %s line %d: %s", conffile,
- config_error_line(&configuration), config_error_text(&configuration));
+ config_error_line(&configuration), config_error_text(&configuration));
}
}
conffile = defaultconfig;
if (config_read_file(&configuration, conffile) != CONFIG_TRUE) {
fprintf(stderr, "Error in config file %s line %d: %s\n", conffile,
- config_error_line(&configuration), config_error_text(&configuration));
+ config_error_line(&configuration), config_error_text(&configuration));
rc = false;
}
config_destroy(&configuration);
const char *strsetting = NULL;
switch (param) {
- case CERTIFICATE:
- setting = config_lookup(&configuration, "certificate");
- if (!setting)
- return "/etc/umurmur/certificate.crt";
- else {
- if ((strsetting = config_setting_get_string(setting)) != NULL)
- return strsetting;
- else
+ case CERTIFICATE:
+ setting = config_lookup(&configuration, "certificate");
+ if (!setting)
return "/etc/umurmur/certificate.crt";
- }
- break;
- case KEY:
- setting = config_lookup(&configuration, "private_key");
- if (!setting)
- return "/etc/umurmur/private_key.key";
- else {
- if ((strsetting = config_setting_get_string(setting)) != NULL)
- return strsetting;
- else
+ else {
+ if ((strsetting = config_setting_get_string(setting)) != NULL)
+ return strsetting;
+ else
+ return "/etc/umurmur/certificate.crt";
+ }
+ break;
+ case KEY:
+ setting = config_lookup(&configuration, "private_key");
+ if (!setting)
return "/etc/umurmur/private_key.key";
- }
- break;
- case CAPATH:
- setting = config_lookup(&configuration, "ca_path");
- if (!setting)
- return NULL;
- else {
- if ((strsetting = config_setting_get_string(setting)) != NULL)
- return strsetting;
- else
+ else {
+ if ((strsetting = config_setting_get_string(setting)) != NULL)
+ return strsetting;
+ else
+ return "/etc/umurmur/private_key.key";
+ }
+ break;
+ case CAPATH:
+ setting = config_lookup(&configuration, "ca_path");
+ if (!setting)
return NULL;
- }
- break;
- case PASSPHRASE:
- setting = config_lookup(&configuration, "password");
- if (!setting)
- return "";
- else {
- if ((strsetting = config_setting_get_string(setting)) != NULL)
- return strsetting;
- else
+ else {
+ if ((strsetting = config_setting_get_string(setting)) != NULL)
+ return strsetting;
+ else
+ return NULL;
+ }
+ break;
+ case PASSPHRASE:
+ setting = config_lookup(&configuration, "password");
+ if (!setting)
return "";
- }
- break;
- case ADMIN_PASSPHRASE:
- setting = config_lookup(&configuration, "admin_password");
- if (!setting)
- return "";
- else {
- if ((strsetting = config_setting_get_string(setting)) != NULL)
- return strsetting;
- else
+ else {
+ if ((strsetting = config_setting_get_string(setting)) != NULL)
+ return strsetting;
+ else
+ return "";
+ }
+ break;
+ case ADMIN_PASSPHRASE:
+ setting = config_lookup(&configuration, "admin_password");
+ if (!setting)
return "";
- }
- break;
- case BINDADDR:
- setting = config_lookup(&configuration, "bindaddr");
- if (!setting)
- return NULL;
- else {
- if ((strsetting = config_setting_get_string(setting)) != NULL)
- return strsetting;
- else
+ else {
+ if ((strsetting = config_setting_get_string(setting)) != NULL)
+ return strsetting;
+ else
+ return "";
+ }
+ break;
+ case BINDADDR:
+ setting = config_lookup(&configuration, "bindaddr");
+ if (!setting)
return NULL;
- }
- break;
- case BINDADDR6:
- setting = config_lookup(&configuration, "bindaddr6");
- if (!setting)
- return NULL;
- else {
- if ((strsetting = config_setting_get_string(setting)) != NULL)
- return strsetting;
- else
+ else {
+ if ((strsetting = config_setting_get_string(setting)) != NULL)
+ return strsetting;
+ else
+ return NULL;
+ }
+ break;
+ case BINDADDR6:
+ setting = config_lookup(&configuration, "bindaddr6");
+ if (!setting)
return NULL;
- }
- break;
- case WELCOMETEXT:
- setting = config_lookup(&configuration, "welcometext");
- if (!setting)
- return DEFAULT_WELCOME;
- else {
- if ((strsetting = config_setting_get_string(setting)) != NULL)
- return strsetting;
- else
- return DEFAULT_WELCOME;
- }
- break;
- case DEFAULT_CHANNEL:
- setting = config_lookup(&configuration, "default_channel");
- if (!setting)
- return "";
- else {
- if ((strsetting = config_setting_get_string(setting)) != NULL)
- return strsetting;
- else
- return "";
- }
- break;
- case USERNAME:
- setting = config_lookup(&configuration, "username");
- if (!setting)
- return "";
- else {
- if ((strsetting = config_setting_get_string(setting)) != NULL)
- return strsetting;
- else
- return "";
- }
- break;
- case GROUPNAME:
- setting = config_lookup(&configuration, "groupname");
- if (!setting)
- return "";
- else {
- if ((strsetting = config_setting_get_string(setting)) != NULL)
- return strsetting;
- else
- return "";
- }
- break;
- case LOGFILE:
- setting = config_lookup(&configuration, "logfile");
- if (!setting)
- return NULL;
- else {
- if ((strsetting = config_setting_get_string(setting)) != NULL)
- return strsetting;
- else
- return NULL;
- }
- break;
- case BANFILE:
- setting = config_lookup(&configuration, "banfile");
- if (!setting)
- return NULL;
- else {
- if ((strsetting = config_setting_get_string(setting)) != NULL)
- return strsetting;
- else
- return NULL;
- }
- break;
- default:
- doAssert(false);
- break;
+ else {
+ if ((strsetting = config_setting_get_string(setting)) != NULL)
+ return strsetting;
+ else
+ return NULL;
+ }
+ break;
+ case WELCOMETEXT:
+ setting = config_lookup(&configuration, "welcometext");
+ if (!setting)
+ return DEFAULT_WELCOME;
+ else {
+ if ((strsetting = config_setting_get_string(setting)) != NULL)
+ return strsetting;
+ else
+ return DEFAULT_WELCOME;
+ }
+ break;
+ case DEFAULT_CHANNEL:
+ setting = config_lookup(&configuration, "default_channel");
+ if (!setting)
+ return "";
+ else {
+ if ((strsetting = config_setting_get_string(setting)) != NULL)
+ return strsetting;
+ else
+ return "";
+ }
+ break;
+ case USERNAME:
+ setting = config_lookup(&configuration, "username");
+ if (!setting)
+ return "";
+ else {
+ if ((strsetting = config_setting_get_string(setting)) != NULL)
+ return strsetting;
+ else
+ return "";
+ }
+ break;
+ case GROUPNAME:
+ setting = config_lookup(&configuration, "groupname");
+ if (!setting)
+ return "";
+ else {
+ if ((strsetting = config_setting_get_string(setting)) != NULL)
+ return strsetting;
+ else
+ return "";
+ }
+ break;
+ case LOGFILE:
+ setting = config_lookup(&configuration, "logfile");
+ if (!setting)
+ return NULL;
+ else {
+ if ((strsetting = config_setting_get_string(setting)) != NULL)
+ return strsetting;
+ else
+ return NULL;
+ }
+ break;
+ case BANFILE:
+ setting = config_lookup(&configuration, "banfile");
+ if (!setting)
+ return NULL;
+ else {
+ if ((strsetting = config_setting_get_string(setting)) != NULL)
+ return strsetting;
+ else
+ return NULL;
+ }
+ break;
+ default:
+ doAssert(false);
+ break;
}
return NULL;
}
config_setting_t *setting = NULL;
switch (param) {
- case BINDPORT:
- setting = config_lookup(&configuration, "bindport");
- if (!setting)
- return DEFAULT_BINDPORT;
- else {
- return config_setting_get_int(setting);
- }
- break;
- case BINDPORT6:
- setting = config_lookup(&configuration, "bindport6");
- if (!setting)
- return DEFAULT_BINDPORT;
- else {
- return config_setting_get_int(setting);
- }
- break;
- case BAN_LENGTH:
- setting = config_lookup(&configuration, "ban_length");
- if (!setting)
- return DEFAULT_BAN_LENGTH;
- else {
- return config_setting_get_int(setting);
- }
- break;
- case MAX_BANDWIDTH:
- setting = config_lookup(&configuration, "max_bandwidth");
- if (!setting)
- return DEFAULT_MAX_BANDWIDTH;
- else {
- return config_setting_get_int(setting);
- }
- break;
- case MAX_CLIENTS:
- setting = config_lookup(&configuration, "max_users");
- if (!setting)
- return DEFAULT_MAX_CLIENTS;
- else {
- return config_setting_get_int(setting);
- }
- break;
- case OPUS_THRESHOLD:
- setting = config_lookup(&configuration, "opus_threshold");
- if (!setting)
- return DEFAULT_OPUS_THRESHOLD;
- else {
- return config_setting_get_int(setting);
- }
- break;
- default:
- doAssert(false);
+ case BINDPORT:
+ setting = config_lookup(&configuration, "bindport");
+ if (!setting)
+ return DEFAULT_BINDPORT;
+ else {
+ return config_setting_get_int(setting);
+ }
+ break;
+ case BINDPORT6:
+ setting = config_lookup(&configuration, "bindport6");
+ if (!setting)
+ return DEFAULT_BINDPORT;
+ else {
+ return config_setting_get_int(setting);
+ }
+ break;
+ case BAN_LENGTH:
+ setting = config_lookup(&configuration, "ban_length");
+ if (!setting)
+ return DEFAULT_BAN_LENGTH;
+ else {
+ return config_setting_get_int(setting);
+ }
+ break;
+ case MAX_BANDWIDTH:
+ setting = config_lookup(&configuration, "max_bandwidth");
+ if (!setting)
+ return DEFAULT_MAX_BANDWIDTH;
+ else {
+ return config_setting_get_int(setting);
+ }
+ break;
+ case MAX_CLIENTS:
+ setting = config_lookup(&configuration, "max_users");
+ if (!setting)
+ return DEFAULT_MAX_CLIENTS;
+ else {
+ return config_setting_get_int(setting);
+ }
+ break;
+ case OPUS_THRESHOLD:
+ setting = config_lookup(&configuration, "opus_threshold");
+ if (!setting)
+ return DEFAULT_OPUS_THRESHOLD;
+ else {
+ return config_setting_get_int(setting);
+ }
+ break;
+ default:
+ doAssert(false);
}
}
config_setting_t *setting = NULL;
switch (param) {
- case ALLOW_TEXTMESSAGE:
- setting = config_lookup(&configuration, "allow_textmessage");
- if (!setting)
- return true;
- else
- return config_setting_get_bool(setting);
- break;
- case ENABLE_BAN:
- setting = config_lookup(&configuration, "enable_ban");
- if (!setting)
- return false;
- else
- return config_setting_get_bool(setting);
- break;
- case SYNC_BANFILE:
- setting = config_lookup(&configuration, "sync_banfile");
- if (!setting)
- return false;
- else
- return config_setting_get_bool(setting);
- break;
- default:
- doAssert(false);
+ case ALLOW_TEXTMESSAGE:
+ setting = config_lookup(&configuration, "allow_textmessage");
+ if (!setting)
+ return true;
+ else
+ return config_setting_get_bool(setting);
+ break;
+ case ENABLE_BAN:
+ setting = config_lookup(&configuration, "enable_ban");
+ if (!setting)
+ return false;
+ else
+ return config_setting_get_bool(setting);
+ break;
+ case SYNC_BANFILE:
+ setting = config_lookup(&configuration, "sync_banfile");
+ if (!setting)
+ return false;
+ else
+ return config_setting_get_bool(setting);
+ break;
+ default:
+ doAssert(false);
}
}
PASSPHRASE,
CAPATH,
BINDPORT,
- BINDPORT6,
+ BINDPORT6,
BINDADDR,
- BINDADDR6,
+ BINDADDR6,
WELCOMETEXT,
MAX_BANDWIDTH,
MAX_CLIENTS,
va_list argp;
char buf[STRSIZE + 1];
int offset = 0;
- uint16_t port;
+ uint16_t port;
if (termprint || logfile)
offset = sprintf(buf, "INFO: ");
offset += vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
va_end(argp);
- if(client->remote_tcp.ss_family == AF_INET)
- port = ntohs(((struct sockaddr_in*)&client->remote_tcp)->sin_port);
- else
- port = ntohs(((struct sockaddr_in6*)&client->remote_tcp)->sin6_port);
+ if(client->remote_tcp.ss_family == AF_INET)
+ port = ntohs(((struct sockaddr_in*)&client->remote_tcp)->sin_port);
+ else
+ port = ntohs(((struct sockaddr_in6*)&client->remote_tcp)->sin6_port);
offset += snprintf(&buf[offset], STRSIZE - offset, " - [%d] %s@%s:%d",
- client->sessionId,
- client->username == NULL ? "" : client->username,
- client->addressString,
- port);
+ client->sessionId,
+ client->username == NULL ? "" : client->username,
+ client->addressString,
+ port);
if (termprint)
fprintf(stderr, "%s\n", buf);
void signal_handler(int sig)
{
switch(sig) {
- case SIGHUP:
- Log_info("HUP signal received.");
- Log_reset();
- break;
- case SIGTERM:
- Log_info("TERM signal. Shutting down.");
- Server_shutdown();
- break;
+ case SIGHUP:
+ Log_info("HUP signal received.");
+ Log_reset();
+ break;
+ case SIGTERM:
+ Log_info("TERM signal. Shutting down.");
+ Server_shutdown();
+ break;
}
}
void printhelp()
{
printf("uMurmur version %s ('%s'). Mumble protocol %d.%d.%d\n", UMURMUR_VERSION,
- UMURMUR_CODENAME, PROTVER_MAJOR, PROTVER_MINOR, PROTVER_PATCH);
+ UMURMUR_CODENAME, PROTVER_MAJOR, PROTVER_MINOR, PROTVER_PATCH);
printf("Usage: umurmurd [-d] [-r] [-h] [-p <pidfile>] [-t] [-c <conf file>] [-a <addr>] [-b <port>]\n");
printf(" -d - Do not daemonize - run in foreground.\n");
#ifdef POSIX_PRIORITY_SCHEDULING
printf(" -c <conf file> - Specify configuration file (default %s)\n", DEFAULT_CONFIG);
printf(" -t - Test config. Error message to stderr + non-zero exit code on error\n");
printf(" -a <address> - Bind to IP address\n");
+ printf(" -A <address> - Bind to IPv6 address\n");
printf(" -b <port> - Bind to port\n");
+ printf(" -B <port> - Bind to port (IPv6)\n");
printf(" -h - Print this help\n");
exit(0);
}
#ifdef POSIX_PRIORITY_SCHEDULING
while ((c = getopt(argc, argv, "drp:c:a:A:b:B:ht")) != EOF) {
#else
- while ((c = getopt(argc, argv, "dp:c:a:A:b:B:ht")) != EOF) {
+ while ((c = getopt(argc, argv, "dp:c:a:A:b:B:ht")) != EOF) {
#endif
- switch(c) {
- case 'c':
- conffile = optarg;
- break;
- case 'p':
- pidfile = optarg;
- break;
- case 'a':
- bindaddr = optarg;
- break;
- case 'A':
- bindaddr6 = optarg;
- break;
- case 'b':
- bindport = atoi(optarg);
- break;
- case 'B':
- bindport6 = atoi(optarg);
- break;
- case 'd':
- nodaemon = true;
- break;
- case 'h':
- printhelp();
- break;
- case 't':
- testconfig = true;
- break;
+ switch(c) {
+ case 'c':
+ conffile = optarg;
+ break;
+ case 'p':
+ pidfile = optarg;
+ break;
+ case 'a':
+ bindaddr = optarg;
+ break;
+ case 'A':
+ bindaddr6 = optarg;
+ break;
+ case 'b':
+ bindport = atoi(optarg);
+ break;
+ case 'B':
+ bindport6 = atoi(optarg);
+ break;
+ case 'd':
+ nodaemon = true;
+ break;
+ case 'h':
+ printhelp();
+ break;
+ case 't':
+ testconfig = true;
+ break;
#ifdef POSIX_PRIORITY_SCHEDULING
- case 'r':
- realtime = true;
- break;
+ case 'r':
+ realtime = true;
+ break;
#endif
- default:
- fprintf(stderr, "Unrecognized option\n");
- printhelp();
- break;
+ default:
+ fprintf(stderr, "Unrecognized option\n");
+ printhelp();
+ break;
+ }
}
- }
-
- if (testconfig) {
- if (!Conf_ok(conffile))
- exit(1);
- else
- exit(0);
- }
-
- /* Initialize the config subsystem early;
- * switch_user() will need to read some config variables as well as logging.
- */
- Conf_init(conffile);
-
- /* Logging to terminal if not daemonizing, otherwise to syslog or log file.
- */
- if (!nodaemon) {
- daemonize();
- Log_init(false);
- if (pidfile != NULL)
- lockfile(pidfile);
- switch_user();
+ if (testconfig) {
+ if (!Conf_ok(conffile))
+ exit(1);
+ else
+ exit(0);
+ }
- /* Reopen log file. If user switch results in access denied, we catch
- * it early.
+ /* Initialize the config subsystem early;
+ * switch_user() will need to read some config variables as well as logging.
*/
- Log_reset();
- }
- else Log_init(true);
-
- signal(SIGCHLD, SIG_IGN); /* ignore child */
- signal(SIGTSTP, SIG_IGN); /* ignore tty signals */
- signal(SIGTTOU, SIG_IGN);
- signal(SIGTTIN, SIG_IGN);
- signal(SIGPIPE, SIG_IGN);
- signal(SIGHUP, signal_handler); /* catch hangup signal */
- signal(SIGTERM, signal_handler); /* catch kill signal */
-
- /* Build system string */
- if (uname(&utsbuf) == 0) {
- snprintf(system_string, 64, "%s %s", utsbuf.sysname, utsbuf.machine);
- snprintf(version_string, 64, "%s", utsbuf.release);
- }
- else {
- snprintf(system_string, 64, "unknown unknown");
- snprintf(version_string, 64, "unknown");
- }
+ Conf_init(conffile);
+
+ /* Logging to terminal if not daemonizing, otherwise to syslog or log file.
+ */
+ if (!nodaemon) {
+ daemonize();
+ Log_init(false);
+ if (pidfile != NULL)
+ lockfile(pidfile);
+
+ switch_user();
+
+ /* Reopen log file. If user switch results in access denied, we catch
+ * it early.
+ */
+ Log_reset();
+ }
+ else Log_init(true);
+
+ signal(SIGCHLD, SIG_IGN); /* ignore child */
+ signal(SIGTSTP, SIG_IGN); /* ignore tty signals */
+ signal(SIGTTOU, SIG_IGN);
+ signal(SIGTTIN, SIG_IGN);
+ signal(SIGPIPE, SIG_IGN);
+ signal(SIGHUP, signal_handler); /* catch hangup signal */
+ signal(SIGTERM, signal_handler); /* catch kill signal */
+
+ /* Build system string */
+ if (uname(&utsbuf) == 0) {
+ snprintf(system_string, 64, "%s %s", utsbuf.sysname, utsbuf.machine);
+ snprintf(version_string, 64, "%s", utsbuf.release);
+ }
+ else {
+ snprintf(system_string, 64, "unknown unknown");
+ snprintf(version_string, 64, "unknown");
+ }
- /* Initializing */
- SSLi_init();
- Chan_init();
- Client_init();
- Ban_init();
+ /* Initializing */
+ SSLi_init();
+ Chan_init();
+ Client_init();
+ Ban_init();
#ifdef POSIX_PRIORITY_SCHEDULING
- if (realtime)
- setscheduler();
+ if (realtime)
+ setscheduler();
#endif
- Server_run();
+ Server_run();
- Ban_deinit();
- SSLi_deinit();
- Chan_free();
- Log_free();
- Conf_deinit();
+ Ban_deinit();
+ SSLi_deinit();
+ Chan_free();
+ Log_free();
+ Conf_deinit();
- if (pidfile != NULL)
- unlink(pidfile);
+ if (pidfile != NULL)
+ unlink(pidfile);
- return 0;
-}
+ return 0;
+ }
/* Initialize the address structures for IPv4 and IPv6 */
struct sockaddr_storage** Server_setupAddressesAndPorts()
{
- struct sockaddr_storage** addresses = malloc(2 * sizeof(void*));
+ struct sockaddr_storage** addresses = malloc(2 * sizeof(void*));
- struct sockaddr_storage* v4address = calloc(1, sizeof(struct sockaddr_storage));
- v4address->ss_family = AF_INET;
- v4address->ss_len = sizeof(struct sockaddr_storage);
- struct sockaddr_storage* v6address = calloc(1, sizeof(struct sockaddr_storage));
- v6address->ss_family = AF_INET6;
- v6address->ss_len = sizeof(struct sockaddr_storage);
+ struct sockaddr_storage* v4address = calloc(1, sizeof(struct sockaddr_storage));
+ v4address->ss_family = AF_INET;
+ v4address->ss_len = sizeof(struct sockaddr_storage);
+ struct sockaddr_storage* v6address = calloc(1, sizeof(struct sockaddr_storage));
+ v6address->ss_family = AF_INET6;
+ v6address->ss_len = sizeof(struct sockaddr_storage);
- int error = 0;
+ int error = 0;
- const char* confadd = getStrConf(BINDADDR);
- error = inet_pton(AF_INET, (!bindaddr) ? ((getStrConf(BINDADDR)) ? getStrConf(BINDADDR) : "0.0.0.0")
- : bindaddr, &(((struct sockaddr_in*)v4address)->sin_addr));
- if (error == 0) Log_fatal("Invalid IPv4 address supplied!");
+ const char* confadd = getStrConf(BINDADDR);
+ error = inet_pton(AF_INET, (!bindaddr) ? ((getStrConf(BINDADDR)) ? getStrConf(BINDADDR) : "0.0.0.0")
+ : bindaddr, &(((struct sockaddr_in*)v4address)->sin_addr));
+ if (error == 0) Log_fatal("Invalid IPv4 address supplied!");
- error = inet_pton(AF_INET6, (!bindaddr6) ? ((getStrConf(BINDADDR6)) ? getStrConf(BINDADDR6) : "::")
- : bindaddr6, &(((struct sockaddr_in6*)v6address)->sin6_addr));
- if (error == 0) Log_fatal("Invalid IPv6 address supplied!");
+ error = inet_pton(AF_INET6, (!bindaddr6) ? ((getStrConf(BINDADDR6)) ? getStrConf(BINDADDR6) : "::")
+ : bindaddr6, &(((struct sockaddr_in6*)v6address)->sin6_addr));
+ if (error == 0) Log_fatal("Invalid IPv6 address supplied!");
- ((struct sockaddr_in*)v4address)->sin_port = htons((bindport) ? bindport : getIntConf(BINDPORT));
- ((struct sockaddr_in6*)v6address)->sin6_port = htons((bindport6) ? bindport6 : getIntConf(BINDPORT6));
+ ((struct sockaddr_in*)v4address)->sin_port = htons((bindport) ? bindport : getIntConf(BINDPORT));
+ ((struct sockaddr_in6*)v6address)->sin6_port = htons((bindport6) ? bindport6 : getIntConf(BINDPORT6));
- addresses[0] = v4address;
- addresses[1] = v6address;
+ addresses[0] = v4address;
+ addresses[1] = v6address;
- return addresses;
+ return addresses;
}
void Server_runLoop(struct pollfd* pollfds)
- {
- int timeout = 1000, rc, clientcount;
+{
+ int timeout = 1000, rc, clientcount;
etimer_t janitorTimer;
Timer_init(&janitorTimer);
tcpfd = accept(pollfds[TCP_SOCK].fd, (struct sockaddr*)&remote, &addrlen);
fcntl(tcpfd, F_SETFL, O_NONBLOCK);
setsockopt(tcpfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
- Log_debug("Connection from %s port %d\n", inet_ntoa(((struct sockaddr_in*)&remote)->sin_addr),
- ntohs(((struct sockaddr_in*)&remote)->sin_port));
+ Log_debug("Connection from %s port %d\n", inet_ntoa(((struct sockaddr_in*)&remote)->sin_addr),
+ ntohs(((struct sockaddr_in*)&remote)->sin_port));
if (Client_add(tcpfd, &remote) < 0)
close(tcpfd);
}
}
}
}
- free(udpsocks);
- }
+}
void Server_setupTCPSockets(struct sockaddr_storage* addresses[2], struct pollfd* pollfds)
- {
- uint8_t yes = 1;
- int error = 0;
- int sockets[2];
+{
+ uint8_t yes = 1;
+ int error = 0;
+ int sockets[2];
- /* IPv4 socket setup */
+ /* IPv4 socket setup */
sockets[0] = socket(PF_INET, SOCK_STREAM, 0);
if (sockets[0] < 0)
Log_fatal("socket IPv4");
if (setsockopt(sockets[0], SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) != 0)
Log_fatal("setsockopt IPv4: %s", strerror(errno));
if (bind(sockets[0], (struct sockaddr *)addresses[0], sizeof (struct sockaddr_in)) < 0)
- Log_fatal("bind IPv4: %s", strerror(errno));
+ Log_fatal("bind IPv4: %s", strerror(errno));
if (listen(sockets[0], 3) < 0)
- Log_fatal("listen IPv4");
+ Log_fatal("listen IPv4");
fcntl(sockets[0], F_SETFL, O_NONBLOCK);
pollfds[TCP_SOCK].fd = sockets[0];
pollfds[TCP_SOCK].events = POLLIN;
- /* IPv6 socket setup */
+ /* IPv6 socket setup */
sockets[1] = socket(PF_INET6, SOCK_STREAM, 0);
if (sockets[1] < 0)
Log_fatal("socket IPv6");
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)
- Log_fatal("bind IPv6: %s", strerror(errno));
+ Log_fatal("bind IPv6: %s", strerror(errno));
if (listen(sockets[1], 3) < 0)
- Log_fatal("listen IPv6");
+ Log_fatal("listen IPv6");
fcntl(sockets[1], F_SETFL, O_NONBLOCK);
pollfds[TCP_SOCK6].fd = sockets[1];
pollfds[TCP_SOCK6].events = POLLIN;
- }
+}
void Server_setupUDPSockets(struct sockaddr_storage* addresses[2], struct pollfd* pollfds)
- {
- uint8_t yes = 1;
- int val = 0;
- int error = 0;
- int sockets[2];
- char ipv6Representation[INET6_ADDRSTRLEN];
+{
+ uint8_t yes = 1;
+ int val = 0;
+ int error = 0;
+ int sockets[2];
+ char ipv6Representation[INET6_ADDRSTRLEN];
- if((udpsocks = malloc(2 * sizeof(int))) == NULL)
- Log_fatal("Out of memory (%s:%s)", __FILE__, __LINE__);
+ if((udpsocks = malloc(2 * sizeof(int))) == NULL)
+ Log_fatal("Out of memory (%s:%s)", __FILE__, __LINE__);
- inet_ntop(AF_INET6, &((struct sockaddr_in6*)addresses[1])->sin6_addr, ipv6Representation, sizeof(INET6_ADDRSTRLEN));
+ inet_ntop(AF_INET6, &((struct sockaddr_in6*)addresses[1])->sin6_addr, ipv6Representation, sizeof(INET6_ADDRSTRLEN));
- sockets[0] = socket(PF_INET, SOCK_DGRAM, 0);
+ sockets[0] = socket(PF_INET, SOCK_DGRAM, 0);
if (bind(sockets[0], (struct sockaddr *) addresses[0], sizeof (struct sockaddr_in)) < 0)
Log_fatal("bind %d %s: %s", ((struct sockaddr_in*)addresses[0])->sin_port,
- inet_ntoa(((struct sockaddr_in*)addresses[0])->sin_addr), strerror(errno));
+ inet_ntoa(((struct sockaddr_in*)addresses[0])->sin_addr), strerror(errno));
val = 0xe0;
if (setsockopt(sockets[0], IPPROTO_IP, IP_TOS, &val, sizeof(val)) < 0)
Log_warn("Server: Failed to set TOS for UDP Socket");
pollfds[UDP_SOCK].fd = sockets[0];
pollfds[UDP_SOCK].events = POLLIN | POLLHUP | POLLERR;
- sockets[1] = socket(PF_INET6, SOCK_DGRAM, 0);
+ sockets[1] = socket(PF_INET6, SOCK_DGRAM, 0);
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)
fcntl(sockets[1], F_SETFL, O_NONBLOCK);
pollfds[UDP_SOCK6].fd = sockets[1];
pollfds[UDP_SOCK6].events = POLLIN | POLLHUP | POLLERR;
- udpsocks[0] = sockets[0];
- udpsocks[1] = sockets[1];
- }
+ udpsocks[0] = sockets[0];
+ udpsocks[1] = sockets[1];
+}
void Server_run()
{
Log_fatal("out of memory");
/* Figure out bind address and port */
- struct sockaddr_storage** addresses = Server_setupAddressesAndPorts();
+ struct sockaddr_storage** addresses = Server_setupAddressesAndPorts();
/* Prepare TCP sockets */
- Server_setupTCPSockets(addresses, pollfds);
+ Server_setupTCPSockets(addresses, pollfds);
/* Prepare UDP sockets */
- Server_setupUDPSockets(addresses, pollfds);
+ 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);
+ 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);
+ Server_runLoop(pollfds);
/* Disconnect clients and cleanup memory */
Client_disconnect_all();
free(pollfds);
- free(addresses[0]);
- free(addresses[1]);
- free(addresses);
+ free(addresses[0]);
+ free(addresses[1]);
+ free(addresses);
+ free(udpsocks);
}
void Server_shutdown()