http: add --http-listen=<ip4>, and listen by default on 127.0.0.1 (#81)

Co-authored-by: AndrolGenhald <AndrolGenhald@gmail.com>
This commit is contained in:
Kamil Trzciński
2023-07-01 16:38:36 +02:00
committed by GitHub
parent f1966ab2b9
commit 8d0c04ccd5
12 changed files with 37 additions and 6 deletions

View File

@ -19,19 +19,23 @@
#define HEADER_USER_AGENT "User-Agent:"
#define HEADER_HOST "Host:"
static int http_listen(int port, int maxcons)
static int http_listen(char *addr4, int port, int maxcons)
{
struct sockaddr_in server = {0};
int listenfd = -1;
char listen_addr[INET_ADDRSTRLEN];
// getaddrinfo for host
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
if (inet_pton(server.sin_family, addr4, &server.sin_addr) != 1) {
perror("inet_pton");
return -1;
}
server.sin_port = htons(port);
listenfd = socket(AF_INET, SOCK_STREAM, 0);
listenfd = socket(server.sin_family, SOCK_STREAM, 0);
if (listenfd < 0) {
perror("socket");
LOG_INFO(NULL, "Invalid HTTP listen address: %s", addr4);
return -1;
}
@ -48,6 +52,13 @@ static int http_listen(int port, int maxcons)
goto error;
}
if (inet_ntop(server.sin_family, &server.sin_addr, listen_addr, sizeof(listen_addr)) == NULL) {
perror("inet_ntop");
goto error;
}
LOG_INFO(NULL, "HTTP listening on %s:%d.", listen_addr, port);
return listenfd;
error:
if (listenfd >= 0)
@ -240,7 +251,7 @@ error:
int http_server(http_server_options_t *options, http_method_t *methods)
{
int listen_fd = http_listen(options->port, options->maxcons);
int listen_fd = http_listen(options->listen, options->port, options->maxcons);
if (listen_fd < 0) {
return -1;
}

View File

@ -26,6 +26,7 @@ typedef struct http_method_s {
} http_method_t;
typedef struct http_server_options_s {
char listen[512];
unsigned port;
unsigned maxcons;
} http_server_options_t;