userplex: Corrected opening of /dev/null.
[ashd.git] / src / plaintcp.c
index 864efe1..b898e37 100644 (file)
@@ -22,6 +22,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <fcntl.h>
 #include <errno.h>
 #include <string.h>
 
@@ -44,9 +45,10 @@ struct tcpport {
 struct tcpconn {
     struct sockaddr_storage name;
     struct tcpport *port;
+    int fd;
 };
 
-static int listensock4(int port)
+int listensock4(int port)
 {
     struct sockaddr_in name;
     int fd;
@@ -63,14 +65,15 @@ static int listensock4(int port)
        close(fd);
        return(-1);
     }
-    if(listen(fd, 16) < 0) {
+    if(listen(fd, 128) < 0) {
        close(fd);
        return(-1);
     }
+    fcntl(fd, F_SETFD, FD_CLOEXEC);
     return(fd);
 }
 
-static int listensock6(int port)
+int listensock6(int port)
 {
     struct sockaddr_in6 name;
     int fd;
@@ -87,25 +90,56 @@ static int listensock6(int port)
        close(fd);
        return(-1);
     }
-    if(listen(fd, 16) < 0) {
+    if(listen(fd, 128) < 0) {
        close(fd);
        return(-1);
     }
+    fcntl(fd, F_SETFD, FD_CLOEXEC);
     return(fd);
 }
 
+char *formathaddress(struct sockaddr *name, socklen_t namelen)
+{
+    static char buf[128];
+    struct sockaddr_in *v4;
+    struct sockaddr_in6 *v6;
+
+    switch(name->sa_family) {
+    case AF_INET:
+       v4 = (struct sockaddr_in *)name;
+       if(!inet_ntop(AF_INET, &v4->sin_addr, buf, sizeof(buf)))
+           return(NULL);
+       return(buf);
+    case AF_INET6:
+       v6 = (struct sockaddr_in6 *)name;
+       if(IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr)) {
+           if(!inet_ntop(AF_INET, ((char *)&v6->sin6_addr) + 12, buf, sizeof(buf)))
+               return(NULL);
+       } else {
+           if(!inet_ntop(AF_INET6, &v6->sin6_addr, buf, sizeof(buf)))
+               return(NULL);
+       }
+       return(buf);
+    default:
+       errno = EPFNOSUPPORT;
+       return(NULL);
+    }
+}
+
 static int initreq(struct conn *conn, struct hthead *req)
 {
     struct tcpconn *tcp = conn->pdata;
-    char nmbuf[256];
+    struct sockaddr_storage sa;
+    socklen_t salen;
     
-    if(tcp->name.ss_family == AF_INET) {
-       headappheader(req, "X-Ash-Address", inet_ntop(AF_INET, &((struct sockaddr_in *)&tcp->name)->sin_addr, nmbuf, sizeof(nmbuf)));
+    headappheader(req, "X-Ash-Address", formathaddress((struct sockaddr *)&tcp->name, sizeof(sa)));
+    if(tcp->name.ss_family == AF_INET)
        headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in *)&tcp->name)->sin_port)));
-    } else if(tcp->name.ss_family == AF_INET6) {
-       headappheader(req, "X-Ash-Address", inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&tcp->name)->sin6_addr, nmbuf, sizeof(nmbuf)));
+    else if(tcp->name.ss_family == AF_INET6)
        headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in6 *)&tcp->name)->sin6_port)));
-    }
+    salen = sizeof(sa);
+    if(!getsockname(tcp->fd, (struct sockaddr *)&sa, &salen))
+       headappheader(req, "X-Ash-Server-Address", formathaddress((struct sockaddr *)&sa, sizeof(sa)));
     headappheader(req, "X-Ash-Server-Port", sprintf3("%i", tcp->port->sport));
     headappheader(req, "X-Ash-Protocol", "http");
     return(0);
@@ -125,6 +159,7 @@ void servetcp(struct muth *muth, va_list args)
     in = mtstdopen(fd, 1, 60, "r+");
     conn.pdata = &tcp;
     conn.initreq = initreq;
+    tcp.fd = fd;
     tcp.name = name;
     tcp.port = stcp;
     serve(in, &conn);
@@ -163,7 +198,8 @@ void handleplain(int argc, char **argp, char **argv)
     for(i = 0; i < argc; i++) {
        if(!strcmp(argp[i], "help")) {
            printf("plain handler parameters:\n");
-           printf("\tport=TCP-PORT (default is 80)\n");
+           printf("\tport=TCP-PORT   [80]\n");
+           printf("\t\tThe TCP port to listen on.\n");
            exit(0);
        } else if(!strcmp(argp[i], "port")) {
            port = atoi(argv[i]);