X-Git-Url: https://git.kernel.dk/?p=fio.git;a=blobdiff_plain;f=server.c;h=524c8146670849c58735fc4faa041d0a6dc08095;hp=ba9558ce162f5065e7acbc248ab1544eab37bde1;hb=bebe639808147d587bbe776566d390b9ff98773f;hpb=87aa8f1901e26bc377f3035a485d35c417d0255a diff --git a/server.c b/server.c index ba9558ce..524c8146 100644 --- a/server.c +++ b/server.c @@ -654,20 +654,22 @@ static int fio_init_server_ip(void) opt = 1; if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { log_err("fio: setsockopt: %s\n", strerror(errno)); + close(sk); return -1; } #ifdef SO_REUSEPORT if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { log_err("fio: setsockopt: %s\n", strerror(errno)); + close(sk); return -1; } #endif saddr_in.sin_family = AF_INET; - saddr_in.sin_port = htons(fio_net_port); if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) { log_err("fio: bind: %s\n", strerror(errno)); + close(sk); return -1; } @@ -698,6 +700,7 @@ static int fio_init_server_sock(void) if (bind(sk, (struct sockaddr *) &addr, len) < 0) { log_err("fio: bind: %s\n", strerror(errno)); + close(sk); return -1; } @@ -707,6 +710,7 @@ static int fio_init_server_sock(void) static int fio_init_server_connection(void) { + char bind_str[128]; int sk; dprint(FD_NET, "starting server\n"); @@ -719,6 +723,13 @@ static int fio_init_server_connection(void) if (sk < 0) return sk; + if (!bind_sock) + sprintf(bind_str, "%s:%u", inet_ntoa(saddr_in.sin_addr), fio_net_port); + else + strcpy(bind_str, bind_sock); + + log_info("fio: server listening on %s\n", bind_str); + if (listen(sk, 1) < 0) { log_err("fio: listen: %s\n", strerror(errno)); return -1; @@ -727,6 +738,81 @@ static int fio_init_server_connection(void) return sk; } +int fio_server_parse_string(const char *str, char **ptr, int *is_sock, + int *port, struct in_addr *inp) +{ + *ptr = NULL; + *is_sock = 0; + *port = 0; + + if (!strncmp(str, "sock:", 5)) { + *ptr = strdup(str + 5); + *is_sock = 1; + } else { + const char *host = str; + char *portp; + int lport = 0; + + /* + * Is it ip::port + */ + if (!strncmp(host, "ip:", 3)) + host += 3; + else if (host[0] == ':') { + /* String is :port */ + host++; + lport = atoi(host); + if (!lport || lport > 65535) { + log_err("fio: bad server port %u\n", port); + return 1; + } + /* no hostname given, we are done */ + *port = lport; + return 0; + } + + /* + * If no port seen yet, check if there's a last ':' at the end + */ + if (!lport) { + portp = strchr(host, ':'); + if (portp) { + *portp = '\0'; + portp++; + lport = atoi(portp); + if (!lport || lport > 65535) { + log_err("fio: bad server port %u\n", port); + return 1; + } + } + } + + if (lport) + *port = lport; + + *ptr = strdup(host); + + if (inet_aton(host, inp) != 1) { + struct hostent *hent; + + hent = gethostbyname(host); + if (!hent) { + printf("FAIL\n"); + free(*ptr); + *ptr = NULL; + return 1; + } + + memcpy(inp, hent->h_addr, 4); + } + } + + if (*port == 0) + *port = fio_net_port; + + return 0; +} + /* * Server arg should be one of: * @@ -741,28 +827,16 @@ static int fio_init_server_connection(void) */ static int fio_handle_server_arg(void) { + int unused; + saddr_in.sin_addr.s_addr = htonl(INADDR_ANY); + saddr_in.sin_port = htons(fio_net_port); if (!fio_server_arg) return 0; - if (!strncmp(fio_server_arg, "sock:", 5)) { - bind_sock = fio_server_arg += 5; - return 0; - } else { - char *host = fio_server_arg; - - if (!strncmp(host, "ip:", 3)) - host += 3; - if (inet_aton(host, &saddr_in.sin_addr) != 1) { - struct hostent *hent; - - hent = gethostbyname(host); - if (hent) - memcpy(&saddr_in.sin_addr, hent->h_addr, 4); - } - return 0; - } + return fio_server_parse_string(fio_server_arg, &bind_sock, &unused, + &fio_net_port, &saddr_in.sin_addr); } static int fio_server(void) @@ -786,6 +860,8 @@ static int fio_server(void) free(fio_server_arg); fio_server_arg = NULL; } + if (bind_sock) + free(bind_sock); return ret; } @@ -838,7 +914,7 @@ int fio_start_server(int daemonize) return fio_server(); } -void fio_server_add_arg(const char *arg) +void fio_server_set_arg(const char *arg) { fio_server_arg = strdup(arg); }