Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add support for IPv6 to the althttpd.c standalone server. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
74563a081eb311b24963a3ae008e735b |
User & Date: | drh 2012-07-05 19:35:05.357 |
Context
2012-07-05
| ||
20:01 | Add support for IPv6 to the CGI handler. (check-in: 46c838972f user: drh tags: trunk) | |
19:35 | Add support for IPv6 to the althttpd.c standalone server. (check-in: 74563a081e user: drh tags: trunk) | |
2012-06-27
| ||
19:57 | Clarification of the input time string formats for date/time functions. (check-in: 738f9af9ca user: drh tags: trunk) | |
Changes
Changes to misc/althttpd.c.
︙ | ︙ | |||
75 76 77 78 79 80 81 82 83 84 85 86 87 88 | #include <sys/socket.h> #include <sys/wait.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdarg.h> #include <time.h> #include <sys/times.h> #ifdef linux #include <sys/sendfile.h> #endif #include <assert.h> /* ** Configure the server by setting the following macros and recompiling. | > | 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | #include <sys/socket.h> #include <sys/wait.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdarg.h> #include <time.h> #include <sys/times.h> #include <netdb.h> #ifdef linux #include <sys/sendfile.h> #endif #include <assert.h> /* ** Configure the server by setting the following macros and recompiling. |
︙ | ︙ | |||
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 | */ omitLog = 1; if( useTimeout ) alarm(30); } #define MAX_PARALLEL 50 /* Number of simultaneous children */ /* ** Implement an HTTP server daemon listening on port iPort. ** ** As new connections arrive, fork a child and let child return ** out of this procedure call. The child will handle the request. ** The parent never returns from this procedure. ** ** Return 0 to each child as it runs. If unable to establish a ** listening socket, return non-zero. */ | > > > > > > > | | > < > > > > > | | | > > > > | | > | < < > > | | > > | > > > > > | > > | | > | < < | | < | | < < > | | > > | > | | | | | | | | | | | | | | | | | | | | | | | | | | | > | | 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 | */ omitLog = 1; if( useTimeout ) alarm(30); } #define MAX_PARALLEL 50 /* Number of simultaneous children */ typedef union { struct sockaddr sa; struct sockaddr_in sa4; struct sockaddr_in6 sa6; struct sockaddr_storage sas; } address; /* ** Implement an HTTP server daemon listening on port iPort. ** ** As new connections arrive, fork a child and let child return ** out of this procedure call. The child will handle the request. ** The parent never returns from this procedure. ** ** Return 0 to each child as it runs. If unable to establish a ** listening socket, return non-zero. */ int http_server(const char *zPort, int localOnly){ int listener[20]; /* The server sockets */ int connection; /* A socket for each individual connection */ fd_set readfds; /* Set of file descriptors for select() */ address inaddr; /* Remote address */ socklen_t lenaddr; /* Length of the inaddr structure */ int child; /* PID of the child process */ int nchildren = 0; /* Number of child processes */ struct timeval delay; /* How long to wait inside select() */ int opt = 1; /* setsockopt flag */ struct addrinfo sHints; /* Address hints */ struct addrinfo *pAddrs, *p; /* */ int rc; /* Result code */ int i, n; int maxFd = -1; memset(&sHints, 0, sizeof(sHints)); sHints.ai_family = AF_UNSPEC; sHints.ai_socktype = SOCK_STREAM; sHints.ai_flags = AI_PASSIVE; sHints.ai_protocol = 0; rc = getaddrinfo(localOnly ? "localhost": 0, zPort, &sHints, &pAddrs); if( rc ){ fprintf(stderr, "could not get addr info: %s", rc!=EAI_SYSTEM ? gai_strerror(rc) : strerror(errno)); return 1; } for(n=0, p=pAddrs; n<sizeof(listener)/sizeof(listener[0]) && p!=0; p=p->ai_next){ listener[n] = socket(p->ai_family, p->ai_socktype, p->ai_protocol); if( listener[n]>=0 ){ /* if we can't terminate nicely, at least allow the socket to be reused */ setsockopt(listener[n], SOL_SOCKET, SO_REUSEADDR,&opt, sizeof(opt)); if( bind(listener[n], p->ai_addr, p->ai_addrlen)<0 ){ printf("bind failed: %s\n", strerror(errno)); close(listener[n]); continue; } if( listen(listener[n], 20)<0 ){ printf("listen() failed: %s\n", strerror(errno)); close(listener[n]); continue; } n++; } } if( n==0 ){ fprintf(stderr, "cannot open any sockets\n"); return 1; } while( 1 ){ if( nchildren>MAX_PARALLEL ){ /* Slow down if connections are arriving too fast */ sleep( nchildren-MAX_PARALLEL ); } delay.tv_sec = 60; delay.tv_usec = 0; FD_ZERO(&readfds); for(i=0; i<n; i++){ assert( listener[i]>=0 ); FD_SET( listener[i], &readfds); if( listener[i]>maxFd ) maxFd = listener[i]; } select( maxFd+1, &readfds, 0, 0, &delay); for(i=0; i<n; i++){ if( FD_ISSET(listener[i], &readfds) ){ lenaddr = sizeof(inaddr); connection = accept(listener[i], &inaddr.sa, &lenaddr); if( connection>=0 ){ child = fork(); if( child!=0 ){ if( child>0 ) nchildren++; close(connection); }else{ int nErr = 0, fd; close(0); fd = dup(connection); if( fd!=0 ) nErr++; close(1); fd = dup(connection); if( fd!=1 ) nErr++; close(2); fd = dup(connection); if( fd!=2 ) nErr++; close(connection); return nErr; } } } /* Bury dead children */ while( waitpid(0, 0, WNOHANG)>0 ){ nchildren--; } } } /* NOT REACHED */ exit(1); } int main(int argc, char **argv){ int i; /* Loop counter */ char *zPermUser = 0; /* Run daemon with this user's permissions */ const char *zPort = 0; /* Implement an HTTP server process */ int useChrootJail = 1; /* True to use a change-root jail */ struct passwd *pwd = 0; /* Information about the user */ /* Record the time when processing begins. */ time(&beginTime); |
︙ | ︙ | |||
1577 1578 1579 1580 1581 1582 1583 | zRoot = zArg; }else if( strcmp(z,"-logfile")==0 ){ zLogFile = zArg; }else if( strcmp(z,"-https")==0 ){ useHttps = atoi(zArg); zHttp = useHttps ? "http" : "https"; }else if( strcmp(z, "-port")==0 ){ | | | 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 | zRoot = zArg; }else if( strcmp(z,"-logfile")==0 ){ zLogFile = zArg; }else if( strcmp(z,"-https")==0 ){ useHttps = atoi(zArg); zHttp = useHttps ? "http" : "https"; }else if( strcmp(z, "-port")==0 ){ zPort = zArg; }else if( strcmp(z, "-jail")==0 ){ if( atoi(zArg)==0 ){ useChrootJail = 0; } }else if( strcmp(z, "-debug")==0 ){ if( atoi(zArg) ){ useTimeout = 0; |
︙ | ︙ | |||
1616 1617 1618 1619 1620 1621 1622 | Malfunction(__LINE__, "unable to create chroot jail"); }else{ zRoot = ""; } } /* Activate the server, if requested */ | | | 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 | Malfunction(__LINE__, "unable to create chroot jail"); }else{ zRoot = ""; } } /* Activate the server, if requested */ if( zPort && http_server(zPort, 0) ){ Malfunction(__LINE__, "failed to start server"); } /* Drop root privileges. */ if( zPermUser ){ if( pwd ){ |
︙ | ︙ |