Changeset 20

Show
Ignore:
Timestamp:
03/13/04 17:09:26 (5 years ago)
Author:
minaguib
Message:

Introduced libhttpd to the mix
Skeletal only - accepts connections but does not serve anything yet (only 404's)

next step: chuck most of http.c and re-implement what is there as libhttpd callbacks, and setup callbacks

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/incorporate_libhttpd/wifidog/configure.in

    r9 r20  
    8484BB_ENABLE_DOXYGEN 
    8585 
     86# check for libhttpd 
     87AC_CHECK_HEADER(httpd.h, , AC_MSG_ERROR(You do not seem to have the libhttpd headers - please obtain it from http://www.hughes.com.au/products/libhttpd/) ) 
     88AC_CHECK_LIB(httpd, httpdCreate, , AC_MSG_ERROR(You do not seem to have the libhttpd library - please obtain it from http://www.hughes.com.au/products/libhttpd/) ) 
    8689                         
    8790AC_OUTPUT(              Makefile  
  • branches/incorporate_libhttpd/wifidog/src/common.h

    r18 r20  
    5252#include "http.h" 
    5353 
     54#include "httpd.h" 
     55 
    5456#define MAX_BUF 4096 
    5557 
  • branches/incorporate_libhttpd/wifidog/src/gateway.c

    r18 r20  
    3030extern s_config config; 
    3131 
    32 fd_set master, read_fds; 
    33  
    3432void main_loop(void) 
    3533{ 
    36     int sockfd, new_fd, sin_size, yes = 1, childPid, flags; 
    37     struct sockaddr_in my_addr; 
    38     struct sockaddr_in their_addr; 
    39     struct sigaction sa; 
    4034    struct timeval tv; 
    4135    time_t last_checked; 
    42     int fdmax, i, cnt_last_check; 
     36         httpd * webserver; 
     37         int result; 
    4338 
    4439    /* Initialize the linked list */ 
    4540    node_init(); 
    4641 
    47     FD_ZERO(&master); 
    48     FD_ZERO(&read_fds); 
     42         // Initialize the web server 
     43    debug(D_LOG_DEBUG, "Creating web server on %s:%d", config.gw_address, config.gw_port); 
     44         webserver = httpdCreate(config.gw_address, config.gw_port); 
     45         if (webserver == NULL) { 
     46                debug(D_LOG_ERR, "Could not create web server"); 
     47                exit(1); 
     48         } 
    4949 
    50     sa.sa_handler = sigchld_handler; 
    51     sigemptyset(&sa.sa_mask); 
    52     sa.sa_flags = SA_RESTART; 
    53     if (sigaction(SIGCHLD, &sa, NULL) == -1) { 
    54         debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
    55         exit(1); 
    56     } 
     50         // Init the signals to catch chld/quit/etc 
     51         init_signals(); 
    5752 
    58     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 
    59         debug(D_LOG_ERR, "socket(): %s", strerror(errno)); 
    60         exit(1); 
    61     } 
     53         // Reset the firewall 
     54         // fixme 
     55    // fw_init(); 
    6256 
    63     my_addr.sin_family = AF_INET; 
    64     my_addr.sin_port = htons(config.gw_port); 
    65     if (!inet_aton(config.gw_address, &my_addr.sin_addr)) { 
    66         debug(D_LOG_ERR, "inet_aton(): %s", strerror(errno)); 
    67         exit(1); 
    68     } 
    69     memset(&(my_addr.sin_zero), '\0', 8); 
     57    last_checked = time(NULL); 
     58         tv.tv_sec = config.checkinterval; 
     59         tv.tv_usec = 0; 
    7060 
    71     if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { 
    72         debug(D_LOG_ERR, "setsockopt(): %s", strerror(errno)); 
    73         exit(1); 
    74     }  
    75  
    76     debug(D_LOG_DEBUG, "Binding to socket"); 
    77     if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { 
    78         debug(D_LOG_ERR, "bind(): %s", strerror(errno)); 
    79         exit(1); 
    80     } 
    81  
    82     debug(D_LOG_DEBUG, "Listening on TCP port %d", config.gw_port); 
    83     if (listen(sockfd, config.httpdmaxconn) == -1) { 
    84         debug(D_LOG_ERR, "listen(): %s", strerror(errno)); 
    85         exit(1); 
    86     } 
    87  
    88     // Add socket to master set 
    89     FD_SET(sockfd, &master); 
    90     fdmax = sockfd; 
    91  
    92     sa.sa_handler = termination_handler; 
    93     sigemptyset(&sa.sa_mask); 
    94     sa.sa_flags = SA_RESTART; 
    95  
    96     /* Trap SIGTERM */ 
    97     if (sigaction(SIGTERM, &sa, NULL) == -1) { 
    98         debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
    99         exit(1); 
    100     } 
    101  
    102     /* Trap SIGQUIT */ 
    103     if (sigaction(SIGQUIT, &sa, NULL) == -1) { 
    104         debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
    105         exit(1); 
    106     } 
    107  
    108     /* Trap SIGINT */ 
    109     if (sigaction(SIGINT, &sa, NULL) == -1) { 
    110         debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
    111         exit(1); 
    112     } 
    113  
    114     fw_init(); 
    115     last_checked = time(NULL); 
    116  
     61    debug(D_LOG_DEBUG, "Waiting for connections"); 
    11762    while(1) { 
    118         tv.tv_sec = config.checkinterval; 
    119         tv.tv_usec = 0; 
    120         read_fds = master; 
    121         if (select(fdmax + 1, &read_fds, NULL, NULL, &tv) == -1) { 
    122             debug(D_LOG_ERR, "select(): %s", strerror(errno)); 
    123         } 
    124  
    125         for(i = 0; i <= fdmax; i++) { 
    126             if (FD_ISSET(i, &read_fds)) { 
    127                 if (i == sockfd) { 
    128                     // Handle new connections 
    129                     sin_size = sizeof(struct sockaddr_in); 
    130                     if (-1 == (new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size))) { 
    131                         debug(D_LOG_ERR, "accept(): %s", strerror(errno)); 
    132                     } else { 
    133                         // Add to master set so we can monitor 
    134                         FD_SET(new_fd, &master); 
    135                         if (new_fd > fdmax) { 
    136                             fdmax = new_fd; 
    137                             debug(D_LOG_DEBUG, "New fdmax %d", fdmax); 
    138                         } 
    139                         debug(D_LOG_INFO, "New connection from %s on socket %d", inet_ntoa(their_addr.sin_addr), new_fd); 
    140                     } 
    141                 } else { 
    142                     // Data from client 
    143                     http_request(i, their_addr); 
    144                 } 
    145             } 
    146         } 
     63                  result = httpdGetConnection(webserver, &tv); 
     64                  if (result < 0) { 
     65                          /* 
     66                                * fixme 
     67                                * An error occurred - should we abort? reboot the device ? 
     68                                */ 
     69                                debug(D_LOG_ERR, "httpdGetConnection returned %d", result); 
     70                                exit(1); 
     71                  } 
     72                  else if (result > 0) { 
     73                          /* 
     74                                * We got a connection 
     75                                */ 
     76                                 debug(D_LOG_DEBUG, "Received connection"); 
     77                          if (httpdReadRequest(webserver) >=0) { 
     78                                  /* 
     79                                        * We read the request fine 
     80                                        */ 
     81                                  httpdProcessRequest(webserver); 
     82                          } 
     83                          httpdEndRequest(webserver); 
     84                  } 
    14785 
    14886        if (time(NULL) - last_checked > config.checkinterval) { 
     
    214152} 
    215153 
     154void init_signals() 
     155{ 
     156    struct sigaction sa; 
     157 
     158    sa.sa_handler = sigchld_handler; 
     159    sigemptyset(&sa.sa_mask); 
     160    sa.sa_flags = SA_RESTART; 
     161    if (sigaction(SIGCHLD, &sa, NULL) == -1) { 
     162        debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
     163        exit(1); 
     164    } 
     165 
     166    sa.sa_handler = termination_handler; 
     167    sigemptyset(&sa.sa_mask); 
     168    sa.sa_flags = SA_RESTART; 
     169 
     170    /* Trap SIGTERM */ 
     171    if (sigaction(SIGTERM, &sa, NULL) == -1) { 
     172        debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
     173        exit(1); 
     174    } 
     175 
     176    /* Trap SIGQUIT */ 
     177    if (sigaction(SIGQUIT, &sa, NULL) == -1) { 
     178        debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
     179        exit(1); 
     180    } 
     181 
     182    /* Trap SIGINT */ 
     183    if (sigaction(SIGINT, &sa, NULL) == -1) { 
     184        debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
     185        exit(1); 
     186    } 
     187} 
     188 
  • branches/incorporate_libhttpd/wifidog/src/gateway.h

    r9 r20  
    3030void termination_handler(int s); 
    3131void sigchld_handler(int s); 
     32void init_signals(); 
    3233void check_counters(void); 
    3334void fork_counter_checker(void); 
  • branches/incorporate_libhttpd/wifidog/src/http.c

    r18 r20  
    129129    debug(D_LOG_INFO, "Closing connection to %s", inet_ntoa(their_addr.sin_addr)); 
    130130    close(sockfd); 
    131     FD_CLR(sockfd, &master); 
     131         //fixme 
     132    //FD_CLR(sockfd, &master); 
    132133} 
    133134