aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-01-10 20:19:56 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-01-10 20:19:56 +0000
commitbbe514683a43e81cab1d5ccc0436b9aaf984294b (patch)
tree5faad36440333e8f1c46c367167e03be65ce8a93
parent9de420c27cbbbd99bedc95782a6937039ff071bb (diff)
downloadbusybox-w32-bbe514683a43e81cab1d5ccc0436b9aaf984294b.tar.gz
busybox-w32-bbe514683a43e81cab1d5ccc0436b9aaf984294b.tar.bz2
busybox-w32-bbe514683a43e81cab1d5ccc0436b9aaf984294b.zip
a bit more IPv6-ization work
syslogd: converted to use it (in -R host:port)
-rw-r--r--include/libbb.h23
-rw-r--r--libbb/xconnect.c31
-rw-r--r--sysklogd/syslogd.c25
3 files changed, 38 insertions, 41 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 16f092f60..62d5fcee4 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -298,12 +298,29 @@ extern int setsockopt_reuseaddr(int fd);
298extern int setsockopt_broadcast(int fd); 298extern int setsockopt_broadcast(int fd);
299/* Create server TCP socket bound to bindaddr:port. bindaddr can be NULL, 299/* Create server TCP socket bound to bindaddr:port. bindaddr can be NULL,
300 * numeric IP ("N.N.N.N") or numeric IPv6 address, 300 * numeric IP ("N.N.N.N") or numeric IPv6 address,
301 * and can have ":PORT" suffix. If no suffix trere, second argument is used */ 301 * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT").
302 * If there is no suffix, port argument is used */
302extern int create_and_bind_stream_or_die(const char *bindaddr, int port); 303extern int create_and_bind_stream_or_die(const char *bindaddr, int port);
303/* Create client TCP socket connected to peer:port. Peer cannot be NULL. 304/* Create client TCP socket connected to peer:port. Peer cannot be NULL.
304 * Peer can be numeric IP ("N.N.N.N"), numeric IPv6 address or hostname, 305 * Peer can be numeric IP ("N.N.N.N"), numeric IPv6 address or hostname,
305 * and can have ":PORT" suffix. If no suffix trere, second argument is used */ 306 * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT").
306extern int create_and_connect_stream_or_die(const char *peer, int def_port); 307 * If there is no suffix, port argument is used */
308extern int create_and_connect_stream_or_die(const char *peer, int port);
309typedef struct len_and_sockaddr {
310 int len;
311 union {
312 struct sockaddr sa;
313 struct sockaddr_in sin;
314#if ENABLE_FEATURE_IPV6
315 struct sockaddr_in6 sin6;
316#endif
317 };
318} len_and_sockaddr;
319/* Return malloc'ed len_and_sockaddr with socket address of host:port
320 * Currently will return IPv4 or IPv6 sockaddrs only
321 * (depending on host), but in theory nothing prevents e.g.
322 * UNIX socket address being returned, IPX sockaddr etc... */
323extern len_and_sockaddr* host2sockaddr(const char *host, int port);
307 324
308 325
309extern char *xstrdup(const char *s); 326extern char *xstrdup(const char *s);
diff --git a/libbb/xconnect.c b/libbb/xconnect.c
index 93c3cd5c6..6e85322cf 100644
--- a/libbb/xconnect.c
+++ b/libbb/xconnect.c
@@ -83,25 +83,13 @@ int xconnect_tcp_v4(struct sockaddr_in *s_addr)
83/* "New" networking API */ 83/* "New" networking API */
84 84
85 85
86/* So far we do not expose struct and helpers to libbb */
87typedef struct len_and_sockaddr {
88 int len;
89 union {
90 struct sockaddr sa;
91 struct sockaddr_in sin;
92#if ENABLE_FEATURE_IPV6
93 struct sockaddr_in6 sin6;
94#endif
95 };
96} len_and_sockaddr;
97//extern int xsocket_stream_ip4or6(sa_family_t *fp); 86//extern int xsocket_stream_ip4or6(sa_family_t *fp);
98//extern len_and_sockaddr* host2sockaddr(const char *host, int def_port);
99//extern len_and_sockaddr* dotted2sockaddr(const char *dotted, int def_port); 87//extern len_and_sockaddr* dotted2sockaddr(const char *dotted, int def_port);
100 88
101/* peer: "1.2.3.4[:port]", "www.google.com[:port]" 89/* peer: "1.2.3.4[:port]", "www.google.com[:port]"
102 * def_port: if neither of above specifies port # 90 * port: if neither of above specifies port #
103 */ 91 */
104static len_and_sockaddr* str2sockaddr(const char *host, int def_port, int ai_flags) 92static len_and_sockaddr* str2sockaddr(const char *host, int port, int ai_flags)
105{ 93{
106 int rc; 94 int rc;
107 len_and_sockaddr *r; // = NULL; 95 len_and_sockaddr *r; // = NULL;
@@ -126,12 +114,13 @@ static len_and_sockaddr* str2sockaddr(const char *host, int def_port, int ai_fla
126 } 114 }
127 } 115 }
128 if (cp) { 116 if (cp) {
129 host = safe_strncpy(alloca(cp - host + 1), host, cp - host); 117 int sz = cp - host + 1;
118 host = safe_strncpy(alloca(sz), host, sz);
130 if (ENABLE_FEATURE_IPV6 && *cp != ':') 119 if (ENABLE_FEATURE_IPV6 && *cp != ':')
131 cp++; /* skip ']' */ 120 cp++; /* skip ']' */
132 cp++; /* skip ':' */ 121 cp++; /* skip ':' */
133 } else { 122 } else {
134 utoa_to_buf(def_port, service, sizeof(service)); 123 utoa_to_buf(port, service, sizeof(service));
135 cp = service; 124 cp = service;
136 } 125 }
137 126
@@ -154,14 +143,14 @@ static len_and_sockaddr* str2sockaddr(const char *host, int def_port, int ai_fla
154 return r; 143 return r;
155} 144}
156 145
157static len_and_sockaddr* host2sockaddr(const char *host, int def_port) 146len_and_sockaddr* host2sockaddr(const char *host, int port)
158{ 147{
159 return str2sockaddr(host, def_port, 0); 148 return str2sockaddr(host, port, 0);
160} 149}
161 150
162static len_and_sockaddr* dotted2sockaddr(const char *host, int def_port) 151static len_and_sockaddr* dotted2sockaddr(const char *host, int port)
163{ 152{
164 return str2sockaddr(host, def_port, NI_NUMERICHOST); 153 return str2sockaddr(host, port, NI_NUMERICHOST);
165} 154}
166 155
167static int xsocket_stream_ip4or6(len_and_sockaddr *lsa) 156static int xsocket_stream_ip4or6(len_and_sockaddr *lsa)
@@ -185,7 +174,7 @@ int create_and_bind_stream_or_die(const char *bindaddr, int port)
185 int fd; 174 int fd;
186 len_and_sockaddr *lsa; 175 len_and_sockaddr *lsa;
187 176
188 if (bindaddr) { 177 if (bindaddr && bindaddr[0]) {
189 lsa = dotted2sockaddr(bindaddr, port); 178 lsa = dotted2sockaddr(bindaddr, port);
190 /* currently NULL check is in str2sockaddr */ 179 /* currently NULL check is in str2sockaddr */
191 //if (!lsa) 180 //if (!lsa)
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index aa7d516d6..405282796 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -53,7 +53,7 @@ static smallint isRegular;
53#include <netinet/in.h> 53#include <netinet/in.h>
54/* udp socket for logging to remote host */ 54/* udp socket for logging to remote host */
55static int remoteFD = -1; 55static int remoteFD = -1;
56static struct sockaddr_in remoteAddr; 56static len_and_sockaddr* remoteAddr;
57#endif 57#endif
58 58
59/* We are using bb_common_bufsiz1 for buffering: */ 59/* We are using bb_common_bufsiz1 for buffering: */
@@ -464,7 +464,7 @@ static void do_syslogd(void) ATTRIBUTE_NORETURN;
464static void do_syslogd(void) 464static void do_syslogd(void)
465{ 465{
466 struct sockaddr_un sunx; 466 struct sockaddr_un sunx;
467 socklen_t addrLength; 467 socklen_t addr_len;
468 int sock_fd; 468 int sock_fd;
469 fd_set fds; 469 fd_set fds;
470 470
@@ -491,8 +491,8 @@ static void do_syslogd(void)
491 sunx.sun_family = AF_UNIX; 491 sunx.sun_family = AF_UNIX;
492 strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path)); 492 strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
493 sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0); 493 sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
494 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path); 494 addr_len = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
495 xbind(sock_fd, (struct sockaddr *) &sunx, addrLength); 495 xbind(sock_fd, (struct sockaddr *) &sunx, addr_len);
496 496
497 if (chmod(dev_log_name, 0666) < 0) { 497 if (chmod(dev_log_name, 0666) < 0) {
498 bb_perror_msg_and_die("cannot set permission on %s", dev_log_name); 498 bb_perror_msg_and_die("cannot set permission on %s", dev_log_name);
@@ -524,15 +524,14 @@ static void do_syslogd(void)
524#if ENABLE_FEATURE_REMOTE_LOG 524#if ENABLE_FEATURE_REMOTE_LOG
525 /* We are not modifying log messages in any way before send */ 525 /* We are not modifying log messages in any way before send */
526 /* Remote site cannot trust _us_ anyway and need to do validation again */ 526 /* Remote site cannot trust _us_ anyway and need to do validation again */
527 if (option_mask32 & OPT_remotelog) { 527 if (remoteAddr) {
528 if (-1 == remoteFD) { 528 if (-1 == remoteFD) {
529 remoteFD = socket(AF_INET, SOCK_DGRAM, 0); 529 remoteFD = socket(remoteAddr->sa.sa_family, SOCK_DGRAM, 0);
530 } 530 }
531 if (-1 != remoteFD) { 531 if (-1 != remoteFD) {
532 /* send message to remote logger, ignore possible error */ 532 /* send message to remote logger, ignore possible error */
533 sendto(remoteFD, RECVBUF, i, MSG_DONTWAIT, 533 sendto(remoteFD, RECVBUF, i, MSG_DONTWAIT,
534 (struct sockaddr *) &remoteAddr, 534 &remoteAddr->sa, remoteAddr->len);
535 sizeof(remoteAddr));
536 } 535 }
537 } 536 }
538#endif 537#endif
@@ -565,15 +564,7 @@ int syslogd_main(int argc, char **argv)
565#endif 564#endif
566#if ENABLE_FEATURE_REMOTE_LOG 565#if ENABLE_FEATURE_REMOTE_LOG
567 if (option_mask32 & OPT_remotelog) { // -R 566 if (option_mask32 & OPT_remotelog) { // -R
568 int port = 514; 567 remoteAddr = host2sockaddr(opt_R, 514);
569 p = strchr(opt_R, ':');
570 if (p) {
571 *p++ = '\0';
572 port = xatou16(p);
573 }
574 /* FIXME: looks ip4-specific. need to do better */
575 bb_lookup_host(&remoteAddr, opt_R);
576 remoteAddr.sin_port = bb_lookup_port(port, "udp", port);
577 } 568 }
578 //if (option_mask32 & OPT_locallog) // -L 569 //if (option_mask32 & OPT_locallog) // -L
579#endif 570#endif