htparser: Pass the server's local IP address.
[ashd.git] / src / plaintcp.c
index 03da055..9bfb9bc 100644 (file)
@@ -44,9 +44,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;
@@ -70,7 +71,7 @@ static int listensock4(int port)
     return(fd);
 }
 
-static int listensock6(int port)
+int listensock6(int port)
 {
     struct sockaddr_in6 name;
     int fd;
@@ -97,6 +98,8 @@ static int listensock6(int port)
 static int initreq(struct conn *conn, struct hthead *req)
 {
     struct tcpconn *tcp = conn->pdata;
+    struct sockaddr_storage sa;
+    socklen_t salen;
     char nmbuf[256];
     
     if(tcp->name.ss_family == AF_INET) {
@@ -106,7 +109,15 @@ static int initreq(struct conn *conn, struct hthead *req)
        headappheader(req, "X-Ash-Address", inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&tcp->name)->sin6_addr, nmbuf, sizeof(nmbuf)));
        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)) {
+       if(sa.ss_family == AF_INET)
+           headappheader(req, "X-Ash-Server-Address", inet_ntop(AF_INET, &((struct sockaddr_in *)&sa)->sin_addr, nmbuf, sizeof(nmbuf)));
+       else if(sa.ss_family == AF_INET6)
+           headappheader(req, "X-Ash-Server-Address", inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&sa)->sin6_addr, nmbuf, sizeof(nmbuf)));
+    }
     headappheader(req, "X-Ash-Server-Port", sprintf3("%i", tcp->port->sport));
+    headappheader(req, "X-Ash-Protocol", "http");
     return(0);
 }
 
@@ -124,6 +135,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);
@@ -162,7 +174,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]);
@@ -185,6 +198,9 @@ void handleplain(int argc, char **argp, char **argv)
            exit(1);
        }
     } else {
-       mustart(listenloop, fd, port);
+       omalloc(tcp);
+       tcp->fd = fd;
+       tcp->sport = port;
+       mustart(listenloop, tcp);
     }
 }