From 729bd9e0b0d5977e9c2c2a4eff7e2f0ca2ad4b9e Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Mon, 2 Apr 2007 12:37:28 +0000 Subject: [PATCH] test: comment out unused code udpsvd: fake it compile tcpsvd: more optimal memorizing of IP's for -C --- coreutils/test.c | 4 ++++ include/libbb.h | 7 ++++--- ipsvd/ipsvd_perhost.c | 19 ++++++++++--------- ipsvd/ipsvd_perhost.h | 14 ++++++++++++-- ipsvd/tcpsvd.c | 6 +++--- ipsvd/udpsvd.c | 9 +++++++-- libbb/xconnect.c | 28 ++++++++++++++++++++++------ 7 files changed, 62 insertions(+), 25 deletions(-) diff --git a/coreutils/test.c b/coreutils/test.c index 813cde47c..d5babefce 100644 --- a/coreutils/test.c +++ b/coreutils/test.c @@ -168,9 +168,11 @@ static int binop(void); static arith_t primary(enum token n); static int filstat(char *nm, enum token mode); static arith_t getn(const char *s); +/* UNUSED static int newerf(const char *f1, const char *f2); static int olderf(const char *f1, const char *f2); static int equalf(const char *f1, const char *f2); +*/ static int test_eaccess(char *path, int mode); static int is_a_group_member(gid_t gid); static void initialize_group_array(void); @@ -490,6 +492,7 @@ static arith_t getn(const char *s) return r; } +/* UNUSED static int newerf(const char *f1, const char *f2) { struct stat b1, b2; @@ -514,6 +517,7 @@ static int equalf(const char *f1, const char *f2) stat(f2, &b2) == 0 && b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino); } +*/ /* Do the same thing access(2) does, but use the effective uid and gid, and don't make the mistake of telling root that any file is diff --git a/include/libbb.h b/include/libbb.h index 794049df9..b802e01d3 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -324,11 +324,12 @@ typedef struct len_and_sockaddr { /* Create stream socket, and allocated suitable lsa * (lsa of correct size and lsa->sa.sa_family (AF_INET/AF_INET6)) */ int xsocket_stream(len_and_sockaddr **lsap); -/* Create server TCP socket bound to bindaddr:port. bindaddr can be NULL, +/* Create server socket bound to bindaddr:port. bindaddr can be NULL, * numeric IP ("N.N.N.N") or numeric IPv6 address, * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT"). * If there is no suffix, port argument is used */ int create_and_bind_stream_or_die(const char *bindaddr, int port); +int create_and_bind_dgram_or_die(const char *bindaddr, int port); /* Create client TCP socket connected to peer:port. Peer cannot be NULL. * Peer can be numeric IP ("N.N.N.N"), numeric IPv6 address or hostname, * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT"). @@ -370,9 +371,9 @@ char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa, socklen_t sale char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen); char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa, socklen_t salen); // "old" (ipv4 only) API -// users: traceroute.c hostname.c +// users: traceroute.c hostname.c - use _list_ of all IPs struct hostent *xgethostbyname(const char *name); -// Also inetd.c and inetd.c are using gethostbyname(), +// Also mount.c and inetd.c are using gethostbyname(), // + inet_common.c has additional IPv4-only stuff diff --git a/ipsvd/ipsvd_perhost.c b/ipsvd/ipsvd_perhost.c index 1c5c12af5..281f708b0 100644 --- a/ipsvd/ipsvd_perhost.c +++ b/ipsvd/ipsvd_perhost.c @@ -22,26 +22,26 @@ void ipsvd_perhost_init(unsigned c) cclen = c; } -unsigned ipsvd_perhost_add(const char *ip, unsigned maxconn, struct hcc **hccpp) +unsigned ipsvd_perhost_add(char *ip, unsigned maxconn, struct hcc **hccpp) { unsigned i; unsigned conn = 1; - int p = -1; + int freepos = -1; for (i = 0; i < cclen; ++i) { - if (cc[i].ip[0] == 0) { - if (p == -1) p = i; + if (!cc[i].ip) { + freepos = i; continue; } - if (strncmp(cc[i].ip, ip, sizeof(cc[i].ip)) == 0) { + if (strcmp(cc[i].ip, ip) == 0) { conn++; continue; } } - if (p == -1) return 0; + if (freepos == -1) return 0; if (conn <= maxconn) { - strcpy(cc[p].ip, ip); - *hccpp = &cc[p]; + cc[freepos].ip = ip; + *hccpp = &cc[freepos]; } return conn; } @@ -51,7 +51,8 @@ void ipsvd_perhost_remove(int pid) unsigned i; for (i = 0; i < cclen; ++i) { if (cc[i].pid == pid) { - cc[i].ip[0] = 0; + free(cc[i].ip); + cc[i].ip = NULL; cc[i].pid = 0; return; } diff --git a/ipsvd/ipsvd_perhost.h b/ipsvd/ipsvd_perhost.h index 26b4063ea..cf08000e0 100644 --- a/ipsvd/ipsvd_perhost.h +++ b/ipsvd/ipsvd_perhost.h @@ -8,12 +8,22 @@ */ struct hcc { - char ip[32 - sizeof(int)]; + char *ip; int pid; }; void ipsvd_perhost_init(unsigned); -unsigned ipsvd_perhost_add(const char *ip, unsigned maxconn, struct hcc **hccpp); + +/* Returns number of already opened connects to this ips, including this one. + * ip should be a malloc'ed ptr. + * If return value is <= maxconn, ip is inserted into the table + * and pointer to table entry if stored in *hccpp + * (useful for storing pid later). + * Else ip is NOT inserted (you must take care of it - free() etc) */ +unsigned ipsvd_perhost_add(char *ip, unsigned maxconn, struct hcc **hccpp); + +/* Finds and frees element with pid */ void ipsvd_perhost_remove(int pid); + //unsigned ipsvd_perhost_setpid(int pid); //void ipsvd_perhost_free(void); diff --git a/ipsvd/tcpsvd.c b/ipsvd/tcpsvd.c index 197edca4e..98234a78b 100644 --- a/ipsvd/tcpsvd.c +++ b/ipsvd/tcpsvd.c @@ -128,8 +128,8 @@ int tcpsvd_main(int argc, char **argv) uint16_t remote_port; char *local_hostname = NULL; char *remote_hostname = (char*)""; /* "" used if no -h */ - char *local_ip = local_ip; - char *remote_ip = NULL; + char *local_ip = local_ip; /* gcc */ + char *remote_ip = remote_ip; /* gcc */ //unsigned iscdb = 0; /* = option_mask32 & OPT_x (TODO) */ //unsigned long timeout = 0; #ifndef SSLSVD @@ -271,10 +271,10 @@ int tcpsvd_main(int argc, char **argv) if (max_per_host) { /* we drop connection immediately if cur_per_host > max_per_host * (minimizing load under SYN flood) */ - free(remote_ip); remote_ip = xmalloc_sockaddr2dotted_noport(&sock_adr.sa, sockadr_size); cur_per_host = ipsvd_perhost_add(remote_ip, max_per_host, &hccp); if (cur_per_host > max_per_host) { + free(remote_ip); /* ipsvd_perhost_add detected that max is exceeded * (and did not store us in connection table) */ if (msg_per_host) { diff --git a/ipsvd/udpsvd.c b/ipsvd/udpsvd.c index b3f60823f..06c4f2e88 100644 --- a/ipsvd/udpsvd.c +++ b/ipsvd/udpsvd.c @@ -42,9 +42,9 @@ int udpsvd_main(int argc, char **argv) // unsigned long timeout = 0; char *remote_hostname; - char *local_hostname; + char *local_hostname = local_hostname; /* gcc */ char *remote_ip; - char *local_ip; + char *local_ip = local_ip; /* gcc */ uint16_t local_port, remote_port; union { struct sockaddr sa; @@ -145,6 +145,11 @@ int udpsvd_main(int argc, char **argv) /* if (recvfrom(sock, 0, 0, MSG_PEEK, (struct sockaddr *)&sock_adr, &sockadr_size) == -1) drop("unable to read from socket"); */ + if (verbose) { + local_ip = argv[0]; // TODO: recv_from_to! + local_hostname = (char*)"localhost"; + } + remote_ip = xmalloc_sockaddr2dotted_noport(&sock_adr.sa, sockadr_size); remote_port = get_nport(&sock_adr.sa); remote_port = ntohs(remote_port); diff --git a/libbb/xconnect.c b/libbb/xconnect.c index a331e6bc4..3c1eec943 100644 --- a/libbb/xconnect.c +++ b/libbb/xconnect.c @@ -208,7 +208,7 @@ len_and_sockaddr* xdotted2sockaddr(const char *host, int port) return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR); } -int xsocket_stream(len_and_sockaddr **lsap) +static int xsocket_type(len_and_sockaddr **lsap, int sock_type) { len_and_sockaddr *lsa; int fd; @@ -216,14 +216,14 @@ int xsocket_stream(len_and_sockaddr **lsap) int family = AF_INET; #if ENABLE_FEATURE_IPV6 - fd = socket(AF_INET6, SOCK_STREAM, 0); + fd = socket(AF_INET6, sock_type, 0); if (fd >= 0) { len = sizeof(struct sockaddr_in6); family = AF_INET6; } else #endif { - fd = xsocket(AF_INET, SOCK_STREAM, 0); + fd = xsocket(AF_INET, sock_type, 0); } lsa = xzalloc(offsetof(len_and_sockaddr, sa) + len); lsa->len = len; @@ -232,7 +232,12 @@ int xsocket_stream(len_and_sockaddr **lsap) return fd; } -int create_and_bind_stream_or_die(const char *bindaddr, int port) +int xsocket_stream(len_and_sockaddr **lsap) +{ + return xsocket_type(lsap, SOCK_STREAM); +} + +static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type) { int fd; len_and_sockaddr *lsa; @@ -240,9 +245,9 @@ int create_and_bind_stream_or_die(const char *bindaddr, int port) if (bindaddr && bindaddr[0]) { lsa = xdotted2sockaddr(bindaddr, port); /* user specified bind addr dictates family */ - fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0); + fd = xsocket(lsa->sa.sa_family, sock_type, 0); } else { - fd = xsocket_stream(&lsa); + fd = xsocket_type(&lsa, sock_type); set_nport(lsa, htons(port)); } setsockopt_reuseaddr(fd); @@ -251,6 +256,17 @@ int create_and_bind_stream_or_die(const char *bindaddr, int port) return fd; } +int create_and_bind_stream_or_die(const char *bindaddr, int port) +{ + return create_and_bind_or_die(bindaddr, port, SOCK_STREAM); +} + +int create_and_bind_dgram_or_die(const char *bindaddr, int port) +{ + return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM); +} + + int create_and_connect_stream_or_die(const char *peer, int port) { int fd;