geomyidae

A small C-based gopherd. (gopher://bitreich.org/1/scm/geomyidae)
git clone git://r-36.net/geomyidae
Log | Files | Refs | README | LICENSE

commit cd5884a78c51da927a0cbf302989b21a055b770a
parent f470bb28b01e20414e34e074a17447c556884095
Author: Christoph Lohmann <20h@r-36.net>
Date:   Tue,  1 Mar 2011 06:54:34 +0100

Refurbishing the getaddrinfo stuff and signals.

Diffstat:
main.c | 32++++++++++++++++++--------------
1 file changed, 18 insertions(+), 14 deletions(-)

diff --git a/main.c b/main.c @@ -16,6 +16,8 @@ #include <sys/wait.h> #include <sys/types.h> #include <signal.h> +#include <string.h> +#include <strings.h> #include <time.h> #include <pwd.h> #include <grp.h> @@ -212,7 +214,7 @@ sighandler(int sig) case SIGTERM: if(logfile != nil) stoplogging(glfd); - running = 0; + exit(EXIT_SUCCESS); break; default: break; @@ -245,7 +247,7 @@ usage(void) int main(int argc, char *argv[]) { - struct addrinfo hints, *ai; + struct addrinfo hints, *ai, *rp; struct sockaddr_storage clt; socklen_t cltlen; int sock, list, opt, dofork; @@ -325,8 +327,8 @@ main(int argc, char *argv[]) } } - memset(&hints, 0, sizeof(hints)); - hints.ai_flags |= AI_PASSIVE; + bzero(&hints, sizeof(hints)); + hints.ai_flags = AI_PASSIVE; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; hints.ai_family = AF_INET; @@ -339,11 +341,20 @@ main(int argc, char *argv[]) return 1; } - list = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); - if(list < 0) { - perror("socket"); + for(rp = ai; rp != nil; rp = rp->ai_next) { + list = socket(rp->ai_family, rp->ai_socktype, + rp->ai_protocol); + if(list < 0) + continue; + if(bind(list, rp->ai_addr, rp->ai_addrlen) == 0) + break; + close(list); + } + if(rp == nil) { + perror("Could not find any suitable bindable address."); return 1; } + freeaddrinfo(ai); opt = 1; if(setsockopt(list, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) { @@ -351,18 +362,11 @@ main(int argc, char *argv[]) return 1; } - if(bind(list, ai->ai_addr, ai->ai_addrlen)) { - perror("bind"); - return 1; - } - if(listen(list, 255)) { perror("listen"); return 1; } - freeaddrinfo(ai); - if(dropprivileges(gr, us) < 0) { perror("cannot drop privileges"); return 1;