test: comment out unused code

udpsvd: fake it compile
tcpsvd: more optimal memorizing of IP's for -C
This commit is contained in:
Denis Vlasenko 2007-04-02 12:37:28 +00:00
parent b05a939bcc
commit 729bd9e0b0
7 changed files with 62 additions and 25 deletions

View File

@ -168,9 +168,11 @@ static int binop(void);
static arith_t primary(enum token n); static arith_t primary(enum token n);
static int filstat(char *nm, enum token mode); static int filstat(char *nm, enum token mode);
static arith_t getn(const char *s); static arith_t getn(const char *s);
/* UNUSED
static int newerf(const char *f1, const char *f2); static int newerf(const char *f1, const char *f2);
static int olderf(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 equalf(const char *f1, const char *f2);
*/
static int test_eaccess(char *path, int mode); static int test_eaccess(char *path, int mode);
static int is_a_group_member(gid_t gid); static int is_a_group_member(gid_t gid);
static void initialize_group_array(void); static void initialize_group_array(void);
@ -490,6 +492,7 @@ static arith_t getn(const char *s)
return r; return r;
} }
/* UNUSED
static int newerf(const char *f1, const char *f2) static int newerf(const char *f1, const char *f2)
{ {
struct stat b1, b2; struct stat b1, b2;
@ -514,6 +517,7 @@ static int equalf(const char *f1, const char *f2)
stat(f2, &b2) == 0 && stat(f2, &b2) == 0 &&
b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino); 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, /* 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 and don't make the mistake of telling root that any file is

View File

@ -324,11 +324,12 @@ typedef struct len_and_sockaddr {
/* Create stream socket, and allocated suitable lsa /* Create stream socket, and allocated suitable lsa
* (lsa of correct size and lsa->sa.sa_family (AF_INET/AF_INET6)) */ * (lsa of correct size and lsa->sa.sa_family (AF_INET/AF_INET6)) */
int xsocket_stream(len_and_sockaddr **lsap); 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, * numeric IP ("N.N.N.N") or numeric IPv6 address,
* and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT"). * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT").
* If there is no suffix, port argument is used */ * 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_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. /* 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, * 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"). * 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(const struct sockaddr *sa, socklen_t salen);
char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa, socklen_t salen); char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa, socklen_t salen);
// "old" (ipv4 only) API // "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); 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 // + inet_common.c has additional IPv4-only stuff

View File

@ -22,26 +22,26 @@ void ipsvd_perhost_init(unsigned c)
cclen = 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 i;
unsigned conn = 1; unsigned conn = 1;
int p = -1; int freepos = -1;
for (i = 0; i < cclen; ++i) { for (i = 0; i < cclen; ++i) {
if (cc[i].ip[0] == 0) { if (!cc[i].ip) {
if (p == -1) p = i; freepos = i;
continue; continue;
} }
if (strncmp(cc[i].ip, ip, sizeof(cc[i].ip)) == 0) { if (strcmp(cc[i].ip, ip) == 0) {
conn++; conn++;
continue; continue;
} }
} }
if (p == -1) return 0; if (freepos == -1) return 0;
if (conn <= maxconn) { if (conn <= maxconn) {
strcpy(cc[p].ip, ip); cc[freepos].ip = ip;
*hccpp = &cc[p]; *hccpp = &cc[freepos];
} }
return conn; return conn;
} }
@ -51,7 +51,8 @@ void ipsvd_perhost_remove(int pid)
unsigned i; unsigned i;
for (i = 0; i < cclen; ++i) { for (i = 0; i < cclen; ++i) {
if (cc[i].pid == pid) { if (cc[i].pid == pid) {
cc[i].ip[0] = 0; free(cc[i].ip);
cc[i].ip = NULL;
cc[i].pid = 0; cc[i].pid = 0;
return; return;
} }

View File

@ -8,12 +8,22 @@
*/ */
struct hcc { struct hcc {
char ip[32 - sizeof(int)]; char *ip;
int pid; int pid;
}; };
void ipsvd_perhost_init(unsigned); 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); void ipsvd_perhost_remove(int pid);
//unsigned ipsvd_perhost_setpid(int pid); //unsigned ipsvd_perhost_setpid(int pid);
//void ipsvd_perhost_free(void); //void ipsvd_perhost_free(void);

View File

@ -128,8 +128,8 @@ int tcpsvd_main(int argc, char **argv)
uint16_t remote_port; uint16_t remote_port;
char *local_hostname = NULL; char *local_hostname = NULL;
char *remote_hostname = (char*)""; /* "" used if no -h */ char *remote_hostname = (char*)""; /* "" used if no -h */
char *local_ip = local_ip; char *local_ip = local_ip; /* gcc */
char *remote_ip = NULL; char *remote_ip = remote_ip; /* gcc */
//unsigned iscdb = 0; /* = option_mask32 & OPT_x (TODO) */ //unsigned iscdb = 0; /* = option_mask32 & OPT_x (TODO) */
//unsigned long timeout = 0; //unsigned long timeout = 0;
#ifndef SSLSVD #ifndef SSLSVD
@ -271,10 +271,10 @@ int tcpsvd_main(int argc, char **argv)
if (max_per_host) { if (max_per_host) {
/* we drop connection immediately if cur_per_host > max_per_host /* we drop connection immediately if cur_per_host > max_per_host
* (minimizing load under SYN flood) */ * (minimizing load under SYN flood) */
free(remote_ip);
remote_ip = xmalloc_sockaddr2dotted_noport(&sock_adr.sa, sockadr_size); remote_ip = xmalloc_sockaddr2dotted_noport(&sock_adr.sa, sockadr_size);
cur_per_host = ipsvd_perhost_add(remote_ip, max_per_host, &hccp); cur_per_host = ipsvd_perhost_add(remote_ip, max_per_host, &hccp);
if (cur_per_host > max_per_host) { if (cur_per_host > max_per_host) {
free(remote_ip);
/* ipsvd_perhost_add detected that max is exceeded /* ipsvd_perhost_add detected that max is exceeded
* (and did not store us in connection table) */ * (and did not store us in connection table) */
if (msg_per_host) { if (msg_per_host) {

View File

@ -42,9 +42,9 @@ int udpsvd_main(int argc, char **argv)
// unsigned long timeout = 0; // unsigned long timeout = 0;
char *remote_hostname; char *remote_hostname;
char *local_hostname; char *local_hostname = local_hostname; /* gcc */
char *remote_ip; char *remote_ip;
char *local_ip; char *local_ip = local_ip; /* gcc */
uint16_t local_port, remote_port; uint16_t local_port, remote_port;
union { union {
struct sockaddr sa; 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) /* if (recvfrom(sock, 0, 0, MSG_PEEK, (struct sockaddr *)&sock_adr, &sockadr_size) == -1)
drop("unable to read from socket"); 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_ip = xmalloc_sockaddr2dotted_noport(&sock_adr.sa, sockadr_size);
remote_port = get_nport(&sock_adr.sa); remote_port = get_nport(&sock_adr.sa);
remote_port = ntohs(remote_port); remote_port = ntohs(remote_port);

View File

@ -208,7 +208,7 @@ len_and_sockaddr* xdotted2sockaddr(const char *host, int port)
return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR); 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; len_and_sockaddr *lsa;
int fd; int fd;
@ -216,14 +216,14 @@ int xsocket_stream(len_and_sockaddr **lsap)
int family = AF_INET; int family = AF_INET;
#if ENABLE_FEATURE_IPV6 #if ENABLE_FEATURE_IPV6
fd = socket(AF_INET6, SOCK_STREAM, 0); fd = socket(AF_INET6, sock_type, 0);
if (fd >= 0) { if (fd >= 0) {
len = sizeof(struct sockaddr_in6); len = sizeof(struct sockaddr_in6);
family = AF_INET6; family = AF_INET6;
} else } else
#endif #endif
{ {
fd = xsocket(AF_INET, SOCK_STREAM, 0); fd = xsocket(AF_INET, sock_type, 0);
} }
lsa = xzalloc(offsetof(len_and_sockaddr, sa) + len); lsa = xzalloc(offsetof(len_and_sockaddr, sa) + len);
lsa->len = len; lsa->len = len;
@ -232,7 +232,12 @@ int xsocket_stream(len_and_sockaddr **lsap)
return fd; 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; int fd;
len_and_sockaddr *lsa; len_and_sockaddr *lsa;
@ -240,9 +245,9 @@ int create_and_bind_stream_or_die(const char *bindaddr, int port)
if (bindaddr && bindaddr[0]) { if (bindaddr && bindaddr[0]) {
lsa = xdotted2sockaddr(bindaddr, port); lsa = xdotted2sockaddr(bindaddr, port);
/* user specified bind addr dictates family */ /* 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 { } else {
fd = xsocket_stream(&lsa); fd = xsocket_type(&lsa, sock_type);
set_nport(lsa, htons(port)); set_nport(lsa, htons(port));
} }
setsockopt_reuseaddr(fd); setsockopt_reuseaddr(fd);
@ -251,6 +256,17 @@ int create_and_bind_stream_or_die(const char *bindaddr, int port)
return fd; 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 create_and_connect_stream_or_die(const char *peer, int port)
{ {
int fd; int fd;