| 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; |
|---|
| 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"); |
|---|
| 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 | } |
|---|
| | 154 | void 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 | |
|---|