summaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorDenis Vlasenko2006-11-22 16:10:39 +0000
committerDenis Vlasenko2006-11-22 16:10:39 +0000
commitc8717cd8571cd35d71696aab8aa847169756bd9f (patch)
tree254a736d3e761bddc4ba4b00a8f6af1d430ad18f /libbb
parent0801ef28ca655a28543b28754fd804ba0616c21c (diff)
downloadbusybox-c8717cd8571cd35d71696aab8aa847169756bd9f.zip
busybox-c8717cd8571cd35d71696aab8aa847169756bd9f.tar.gz
telnetd: move generic stuff into libbb. It will make it easy
to make other server applets IPv6-aware too.
Diffstat (limited to 'libbb')
-rw-r--r--libbb/xconnect.c152
1 files changed, 152 insertions, 0 deletions
diff --git a/libbb/xconnect.c b/libbb/xconnect.c
index 21ec276..cf1f9d5 100644
--- a/libbb/xconnect.c
+++ b/libbb/xconnect.c
@@ -66,3 +66,155 @@ int xconnect_tcp_v4(struct sockaddr_in *s_addr)
xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
return s;
}
+
+int dotted2sockaddr(const char *dotted, struct sockaddr* sp, int socklen)
+{
+ union {
+ struct in_addr a4;
+#if ENABLE_FEATURE_IPV6
+ struct in6_addr a6;
+#endif
+ } a;
+
+ /* TODO maybe: port spec? like n.n.n.n:nn */
+
+#if ENABLE_FEATURE_IPV6
+ if (socklen >= sizeof(struct sockaddr_in6)
+ && inet_pton(AF_INET6, dotted, &a.a6) > 0
+ ) {
+ ((struct sockaddr_in6*)sp)->sin6_family = AF_INET6;
+ ((struct sockaddr_in6*)sp)->sin6_addr = a.a6;
+ /* ((struct sockaddr_in6*)sp)->sin6_port = */
+ return 0; /* success */
+ }
+#endif
+ if (socklen >= sizeof(struct sockaddr_in)
+ && inet_pton(AF_INET, dotted, &a.a4) > 0
+ ) {
+ ((struct sockaddr_in*)sp)->sin_family = AF_INET;
+ ((struct sockaddr_in*)sp)->sin_addr = a.a4;
+ /* ((struct sockaddr_in*)sp)->sin_port = */
+ return 0; /* success */
+ }
+ return 1;
+}
+
+int xsocket_stream_ip4or6(sa_family_t *fp)
+{
+ int fd;
+#if ENABLE_FEATURE_IPV6
+ fd = socket(AF_INET6, SOCK_STREAM, 0);
+ if (fp) *fp = AF_INET6;
+ if (fd < 0)
+#endif
+ {
+ fd = xsocket(AF_INET, SOCK_STREAM, 0);
+ if (fp) *fp = AF_INET;
+ }
+ return fd;
+}
+
+int create_and_bind_socket_ip4or6(const char *hostaddr, int port)
+{
+ static const int on = 1;
+ int fd;
+ sockaddr_inet sa;
+
+ memset(&sa, 0, sizeof(sa));
+ if (hostaddr && dotted2sockaddr(hostaddr, &sa.sa, sizeof(sa)))
+ bb_show_usage();
+
+ if (!sa.sa.sa_family)
+ fd = xsocket_stream_ip4or6(&sa.sa.sa_family);
+ else /* user specified bind addr dictates family */
+ fd = xsocket(sa.sa.sa_family, SOCK_STREAM, 0);
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+
+ /* if (port >= 0) { */
+#if ENABLE_FEATURE_IPV6
+ if (sa.sa.sa_family == AF_INET6 /* && !sa.sin6.sin6_port */)
+ sa.sin6.sin6_port = htons(port);
+#endif
+ if (sa.sa.sa_family == AF_INET /* && !sa.sin.sin_port */)
+ sa.sin.sin_port = htons(port);
+ /* } */
+
+ xbind(fd, &sa.sa, sizeof(sa));
+ return fd;
+}
+
+int dotted2sockaddr(const char *dotted, struct sockaddr* sp, int socklen)
+{
+ union {
+ struct in_addr a4;
+#if ENABLE_FEATURE_IPV6
+ struct in6_addr a6;
+#endif
+ } a;
+
+ /* TODO maybe: port spec? like n.n.n.n:nn */
+
+#if ENABLE_FEATURE_IPV6
+ if (socklen >= sizeof(struct sockaddr_in6)
+ && inet_pton(AF_INET6, dotted, &a.a6) > 0
+ ) {
+ ((struct sockaddr_in6*)sp)->sin6_family = AF_INET6;
+ ((struct sockaddr_in6*)sp)->sin6_addr = a.a6;
+ /* ((struct sockaddr_in6*)sp)->sin6_port = */
+ return 0; /* success */
+ }
+#endif
+ if (socklen >= sizeof(struct sockaddr_in)
+ && inet_pton(AF_INET, dotted, &a.a4) > 0
+ ) {
+ ((struct sockaddr_in*)sp)->sin_family = AF_INET;
+ ((struct sockaddr_in*)sp)->sin_addr = a.a4;
+ /* ((struct sockaddr_in*)sp)->sin_port = */
+ return 0; /* success */
+ }
+ return 1;
+}
+
+int xsocket_stream_ip4or6(sa_family_t *fp)
+{
+ int fd;
+#if ENABLE_FEATURE_IPV6
+ fd = socket(AF_INET6, SOCK_STREAM, 0);
+ if (fp) *fp = AF_INET6;
+ if (fd < 0)
+#endif
+ {
+ fd = xsocket(AF_INET, SOCK_STREAM, 0);
+ if (fp) *fp = AF_INET;
+ }
+ return fd;
+}
+
+int create_and_bind_socket_ip4or6(const char *hostaddr, int port)
+{
+ static const int on = 1;
+ int fd;
+ sockaddr_inet sa;
+
+ memset(&sa, 0, sizeof(sa));
+ if (hostaddr) {
+ if (dotted2sockaddr(hostaddr, &sa.sa, sizeof(sa)))
+ bb_error_msg_and_die("bad address '%s'", hostaddr);
+ /* user specified bind addr dictates family */
+ fd = xsocket(sa.sa.sa_family, SOCK_STREAM, 0);
+ } else
+ fd = xsocket_stream_ip4or6(&sa.sa.sa_family);
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+
+ /* if (port >= 0) { */
+#if ENABLE_FEATURE_IPV6
+ if (sa.sa.sa_family == AF_INET6 /* && !sa.sin6.sin6_port */)
+ sa.sin6.sin6_port = htons(port);
+#endif
+ if (sa.sa.sa_family == AF_INET /* && !sa.sin.sin_port */)
+ sa.sin.sin_port = htons(port);
+ /* } */
+
+ xbind(fd, &sa.sa, sizeof(sa));
+ return fd;
+}