@matheusbsbs / matheus-bagliano / commits / 6573faff34
Fork 0

NO_IPV6 support for git daemon

Peter Anvin committed Sep 28, 2005 at 17:26 UTC 6573faff34eb002535843bbb59365be4ad58a120
3 files changed +77 -18
Makefile
+1 -1
@@ -239,7 +239,7 @@ ifdef NO_STRCASESTR
239 LIB_OBJS += compat/strcasestr.o
240 endif
241 ifdef NO_IPV6
242 - DEFINES += -DNO_IPV6
242 + DEFINES += -DNO_IPV6 -Dsockaddr_storage=sockaddr_in
243 endif
244
245 ifdef PPC_SHA1
connect.c
+1 -1
@@ -397,7 +397,7 @@ static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
397
398 memset(&sa, 0, sizeof sa);
399 sa.sin_family = he->h_addrtype;
400 - sa.sin_port = nport;
400 + sa.sin_port = htons(nport);
401 memcpy(&sa.sin_addr, ap, he->h_length);
402
403 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
daemon.c
+75 -16
@@ -1,9 +1,11 @@
1 #include "cache.h"
2 #include "pkt-line.h"
3 +#include <alloca.h>
4 #include <signal.h>
5 #include <sys/wait.h>
6 #include <sys/socket.h>
7 #include <sys/time.h>
8 +#include <sys/poll.h>
9 #include <netdb.h>
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
@@ -328,6 +330,7 @@ static void handle(int incoming, struct sockaddr *addr, int addrlen)
330 inet_ntop(AF_INET, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf));
331 port = sin_addr->sin_port;
332
333 +#ifndef NO_IPV6
334 } else if (addr->sa_family == AF_INET6) {
335 struct sockaddr_in6 *sin6_addr = (void *) addr;
336
@@ -337,6 +340,7 @@ static void handle(int incoming, struct sockaddr *addr, int addrlen)
340 strcat(buf, "]");
341
342 port = sin6_addr->sin6_port;
343 +#endif
344 }
345 loginfo("Connection from %s:%d", addrbuf, port);
346
@@ -369,16 +373,17 @@ static void child_handler(int signo)
373 }
374 }
375
372 -static int serve(int port)
376 +#ifndef NO_IPV6
377 +
378 +static int socksetup(int port, int **socklist_p)
379 {
374 - struct addrinfo hints, *ai0, *ai;
375 - int gai;
380 int socknum = 0, *socklist = NULL;
381 int maxfd = -1;
382 fd_set fds_init, fds;
383 char pbuf[NI_MAXSERV];
384
381 - signal(SIGCHLD, child_handler);
385 + struct addrinfo hints, *ai0, *ai;
386 + int gai;
387
388 sprintf(pbuf, "%d", port);
389 memset(&hints, 0, sizeof(hints));
@@ -438,16 +443,59 @@ static int serve(int port)
443
444 freeaddrinfo(ai0);
445
441 - if (socknum == 0)
442 - die("unable to allocate any listen sockets on port %u", port);
446 + *socklist_p = socklist;
447 + return socknum;
448 +}
449 +
450 +#else /* NO_IPV6 */
451 +
452 +static int socksetup(int port, int **socklist_p)
453 +{
454 + struct sockaddr_in sin;
455 + int sockfd;
456 +
457 + sockfd = socket(AF_INET, SOCK_STREAM, 0);
458 + if (sockfd < 0)
459 + return 0;
460 +
461 + memset(&sin, 0, sizeof sin);
462 + sin.sin_family = AF_INET;
463 + sin.sin_addr.s_addr = htonl(INADDR_ANY);
464 + sin.sin_port = htons(port);
465 +
466 + if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) {
467 + close(sockfd);
468 + return 0;
469 + }
470
471 + *socklist_p = malloc(sizeof(int));
472 + if ( !*socklist_p )
473 + die("memory allocation failed: %s", strerror(errno));
474 + **socklist_p = sockfd;
475 +}
476 +
477 +#endif
478 +
479 +static int service_loop(int socknum, int *socklist)
480 +{
481 + struct pollfd *pfd;
482 + int i;
483 +
484 + pfd = calloc(socknum, sizeof(struct pollfd));
485 + if (!pfd)
486 + die("memory allocation failed: %s", strerror(errno));
487 +
488 + for (i = 0; i < socknum; i++) {
489 + pfd[i].fd = socklist[i];
490 + pfd[i].events = POLLIN;
491 + }
492 +
493 for (;;) {
494 int i;
446 - fds = fds_init;
447 -
448 - if (select(maxfd + 1, &fds, NULL, NULL, NULL) < 0) {
495 +
496 + if (poll(pfd, socknum, 0) < 0) {
497 if (errno != EINTR) {
450 - error("select failed, resuming: %s",
498 + error("poll failed, resuming: %s",
499 strerror(errno));
500 sleep(1);
501 }
@@ -455,12 +503,10 @@ static int serve(int port)
503 }
504
505 for (i = 0; i < socknum; i++) {
458 - int sockfd = socklist[i];
459 -
460 - if (FD_ISSET(sockfd, &fds)) {
506 + if (pfd[i].revents & POLLIN) {
507 struct sockaddr_storage ss;
508 int sslen = sizeof(ss);
463 - int incoming = accept(sockfd, (struct sockaddr *)&ss, &sslen);
509 + int incoming = accept(pfd[i].fd, (struct sockaddr *)&ss, &sslen);
510 if (incoming < 0) {
511 switch (errno) {
512 case EAGAIN:
@@ -477,6 +523,19 @@ static int serve(int port)
523 }
524 }
525
526 +static int serve(int port)
527 +{
528 + int socknum, *socklist;
529 +
530 + signal(SIGCHLD, child_handler);
531 +
532 + socknum = socksetup(port, &socklist);
533 + if (socknum == 0)
534 + die("unable to allocate any listen sockets on port %u", port);
535 +
536 + return service_loop(socknum, socklist);
537 +}
538 +
539 int main(int argc, char **argv)
540 {
541 int port = DEFAULT_GIT_PORT;
@@ -526,7 +585,7 @@ int main(int argc, char **argv)
585 if (inetd_mode) {
586 fclose(stderr); //FIXME: workaround
587 return execute();
588 + } else {
589 + return serve(port);
590 }
530 -
531 - return serve(port);
591 }