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);
return r;
}
+/* UNUSED
static int newerf(const char *f1, const char *f2)
{
struct stat b1, b2;
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
/* 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").
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
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;
}
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;
}
*/
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);
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
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) {
// 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;
/* 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);
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;
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;
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;
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);
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;