Support IPv6

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
main
Jianhui Zhao 2020-12-20 00:13:29 +08:00
parent 7d94ddaedf
commit bdd7727d48
9 changed files with 164 additions and 35 deletions

View File

@ -112,9 +112,11 @@ static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents)
static void usage(const char *prog)
{
fprintf(stderr, "Usage: %s [option]\n"
" -a addr # Default addr is localhost\n"
" -p port # Default port is 8080\n"
" -s # SSl on\n"
" -f # Serve file\n"
" -P # plugin path\n"
" -v # verbose\n", prog);
exit(1);
}
@ -124,14 +126,18 @@ int main(int argc, char **argv)
struct ev_loop *loop = EV_DEFAULT;
struct ev_signal signal_watcher;
struct uh_server *srv = NULL;
char plugin_path[128];
const char *plugin_path = NULL;
bool verbose = false;
bool ssl = false;
const char *addr = "localhost";
int port = 8080;
int opt;
while ((opt = getopt(argc, argv, "p:sfv")) != -1) {
while ((opt = getopt(argc, argv, "a:p:sfP:v")) != -1) {
switch (opt) {
case 'a':
addr = optarg;
break;
case 'p':
port = atoi(optarg);
break;
@ -141,6 +147,9 @@ int main(int argc, char **argv)
case 'f':
serve_file = true;
break;
case 'P':
plugin_path = optarg;
break;
case 'v':
verbose = true;
break;
@ -156,7 +165,7 @@ int main(int argc, char **argv)
signal(SIGPIPE, SIG_IGN);
srv = uh_server_new(loop, "0.0.0.0", port);
srv = uh_server_new(loop, addr, port);
if (!srv)
return -1;
@ -169,11 +178,8 @@ int main(int argc, char **argv)
srv->add_path_handler(srv, "/upload", upload_handler);
getcwd(plugin_path, sizeof(plugin_path));
strcat(plugin_path, "/example/test_plugin.so");
srv->load_plugin(srv, plugin_path);
uh_log_info("Listen on: *:%d\n", port);
if (plugin_path)
srv->load_plugin(srv, plugin_path);
ev_signal_init(&signal_watcher, signal_cb, SIGINT);
ev_signal_start(loop, &signal_watcher);

View File

@ -26,7 +26,7 @@ if(HAVE_DLOPEN)
add_definitions(-DHAVE_DLOPEN)
endif()
set(SOURCE_FILES uhttpd.c log.c connection.c buffer/buffer.c http-parser/http_parser.c ssl.c file.c mimetypes.c)
set(SOURCE_FILES uhttpd.c log.c connection.c buffer/buffer.c http-parser/http_parser.c ssl.c file.c mimetypes.c utils.c)
option(BUILD_SHARED_LIBS "Build shared library" ON)
option(BUILD_STATIC_LIBS "Build static library" ON)

View File

@ -179,9 +179,9 @@ static void conn_redirect(struct uh_connection *conn, int code, const char *loca
conn_done(conn);
}
static uint32_t conn_get_addr(struct uh_connection *conn)
static const struct sockaddr *conn_get_addr(struct uh_connection *conn)
{
return ntohl(conn->addr.sin_addr.s_addr);
return &conn->addr.sa;
}
static enum http_method conn_get_method(struct uh_connection *conn)
@ -426,7 +426,8 @@ static struct http_parser_settings settings = {
void conn_free(struct uh_connection *conn)
{
struct ev_loop *loop = conn->srv->loop;
struct sockaddr_in *addr = &conn->addr;
char addr_str[INET6_ADDRSTRLEN];
int port;
ev_timer_stop(loop, &conn->timer);
ev_io_stop(loop, &conn->ior);
@ -453,7 +454,10 @@ void conn_free(struct uh_connection *conn)
if (conn->sock > 0)
close(conn->sock);
uh_log_debug("Connection(%s:%d) closed\n", inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
if (uh_log_get_threshold() == LOG_DEBUG) {
saddr2str(&conn->addr.sa, addr_str, sizeof(addr_str), &port);
uh_log_debug("Connection(%s %d) closed\n", addr_str, port);
}
free(conn);
}
@ -606,7 +610,7 @@ static void keepalive_cb(struct ev_loop *loop, struct ev_timer *w, int revents)
conn_error(conn, HTTP_STATUS_REQUEST_TIMEOUT, NULL);
}
struct uh_connection *uh_new_connection(struct uh_server *srv, int sock, struct sockaddr_in *addr)
struct uh_connection *uh_new_connection(struct uh_server *srv, int sock, struct sockaddr *addr)
{
struct uh_connection *conn;
@ -620,7 +624,10 @@ struct uh_connection *uh_new_connection(struct uh_server *srv, int sock, struct
conn->sock = sock;
conn->activity = ev_now(srv->loop);
memcpy(&conn->addr, addr, sizeof(struct sockaddr_in));
if (addr->sa_family == AF_INET)
memcpy(&conn->addr, addr, sizeof(struct sockaddr_in));
else
memcpy(&conn->addr, addr, sizeof(struct sockaddr_in6));
ev_io_init(&conn->iow, conn_write_cb, sock, EV_WRITE);

View File

@ -89,7 +89,11 @@ struct uh_connection {
struct ev_timer timer;
struct uh_request req;
struct uh_server *srv;
struct sockaddr_in addr;
union {
struct sockaddr sa;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
} addr; /* peer address */
struct http_parser parser;
struct http_parser_url url_parser;
struct uh_connection *prev;
@ -109,7 +113,7 @@ struct uh_connection {
void (*chunk_printf)(struct uh_connection *conn, const char *format, ...);
void (*chunk_vprintf)(struct uh_connection *conn, const char *format, va_list arg);
void (*chunk_end)(struct uh_connection *conn);
uint32_t (*get_addr)(struct uh_connection *conn);
const struct sockaddr *(*get_addr)(struct uh_connection *conn); /* peer address */
enum http_method (*get_method)(struct uh_connection *conn);
const char *(*get_method_str)(struct uh_connection *conn);
struct uh_str (*get_path)(struct uh_connection *conn);
@ -120,6 +124,6 @@ struct uh_connection {
struct uh_str (*extract_body)(struct uh_connection *conn);
};
struct uh_connection *uh_new_connection(struct uh_server *srv, int sock, struct sockaddr_in *addr);
struct uh_connection *uh_new_connection(struct uh_server *srv, int sock, struct sockaddr *addr);
#endif

View File

@ -102,6 +102,11 @@ void uh_log_threshold(int threshold)
log_threshold = threshold;
}
int uh_log_get_threshold()
{
return log_threshold;
}
void uh_log_close()
{
if (!log_initialized)

View File

@ -29,6 +29,7 @@
#include <string.h>
void uh_log_threshold(int threshold);
int uh_log_get_threshold();
void uh_log_close();
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)

View File

@ -26,6 +26,7 @@
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#ifdef HAVE_DLOPEN
@ -82,19 +83,28 @@ static void uh_accept_cb(struct ev_loop *loop, struct ev_io *w, int revents)
{
struct uh_server *srv = container_of(w, struct uh_server, ior);
struct uh_connection *conn;
struct sockaddr_in addr;
union {
struct sockaddr sa;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
} addr;
socklen_t addr_len = sizeof(addr);
char addr_str[INET6_ADDRSTRLEN];
int port;
int sock;
sock = accept4(srv->sock, (struct sockaddr *)&addr, &addr_len, SOCK_NONBLOCK);
sock = accept4(srv->sock, (struct sockaddr *)&addr, &addr_len, SOCK_NONBLOCK | SOCK_CLOEXEC);
if (sock < 0) {
uh_log_err("accept: %s\n", strerror(errno));
return;
}
uh_log_debug("New connection: %s:%d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
if (uh_log_get_threshold() == LOG_DEBUG) {
saddr2str(&addr.sa, addr_str, sizeof(addr_str), &port);
uh_log_debug("New Connection from: %s %d\n", addr_str, port);
}
conn = uh_new_connection(srv, sock, &addr);
conn = uh_new_connection(srv, sock, &addr.sa);
if (!conn)
return;
@ -210,35 +220,80 @@ static int uh_add_path_handler(struct uh_server *srv, const char *path, uh_path_
int uh_server_init(struct uh_server *srv, struct ev_loop *loop, const char *host, int port)
{
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_addr.s_addr = htonl(INADDR_ANY),
.sin_port = htons(port)
};
union {
struct sockaddr sa;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
} addr;
char addr_str[INET6_ADDRSTRLEN];
socklen_t addrlen;
int sock = -1;
int opt = 1;
memset(srv, 0, sizeof(struct uh_server));
if (!host || *host == '\0') {
addr.sin.sin_family = AF_INET;
addr.sin.sin_addr.s_addr = htonl(INADDR_ANY);
}
if (host)
addr.sin_addr.s_addr = inet_addr(host);
if (inet_pton(AF_INET, host, &addr.sin.sin_addr) == 1) {
addr.sa.sa_family = AF_INET;
} else if (inet_pton(AF_INET6, host, &addr.sin6.sin6_addr) == 1) {
addr.sa.sa_family = AF_INET6;
} else {
static struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
.ai_flags = AI_PASSIVE
};
struct addrinfo *ais;
int status;
sock = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
status = getaddrinfo(host, NULL, &hints, &ais);
if (status != 0) {
uh_log_err("getaddrinfo(): %s\n", gai_strerror(status));
return -1;
}
memcpy(&addr, ais->ai_addr, ais->ai_addrlen);
freeaddrinfo(ais);
}
if (addr.sa.sa_family == AF_INET) {
addr.sin.sin_port = ntohs(port);
addrlen = sizeof(addr.sin);
inet_ntop(AF_INET, &addr.sin.sin_addr, addr_str, sizeof(addr_str));
} else {
addr.sin6.sin6_port = ntohs(port);
addrlen = sizeof(addr.sin6);
inet_ntop(AF_INET6, &addr.sin6.sin6_addr, addr_str, sizeof(addr_str));
}
sock = socket(addr.sa.sa_family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
if (sock < 0) {
uh_log_err("socket: %s\n", strerror(errno));
return -1;
}
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)) < 0) {
uh_log_err("setsockopt: %s\n", strerror(errno));
goto err;
}
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
if (bind(sock, &addr.sa, addrlen) < 0) {
close(sock);
uh_log_err("bind: %s\n", strerror(errno));
return -1;
goto err;
}
listen(sock, SOMAXCONN);
if (uh_log_get_threshold() == LOG_DEBUG) {
saddr2str(&addr.sa, addr_str, sizeof(addr_str), &port);
uh_log_debug("Listen on: %s %d\n", addr_str, port);
}
memset(srv, 0, sizeof(struct uh_server));
srv->loop = loop ? loop : EV_DEFAULT;
srv->sock = sock;
srv->free = uh_server_free;
@ -255,5 +310,8 @@ int uh_server_init(struct uh_server *srv, struct ev_loop *loop, const char *host
ev_io_start(srv->loop, &srv->ior);
return 0;
}
err:
close(sock);
return -1;
}

46
src/utils.c 100644
View File

@ -0,0 +1,46 @@
/*
* MIT License
*
* Copyright (c) 2019 Jianhui Zhao <zhaojh329@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stddef.h>
#include <stdbool.h>
#include <inttypes.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "utils.h"
const char *saddr2str(struct sockaddr *addr, char buf[], int len, int *port)
{
if (addr->sa_family == AF_INET) {
struct sockaddr_in *sin = (struct sockaddr_in *)addr;
*port = ntohs(sin->sin_port);
inet_ntop(AF_INET, &sin->sin_addr, buf, len);
} else {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
*port = ntohs(sin6->sin6_port);
inet_ntop(AF_INET6, &sin6->sin6_addr, buf, len);
}
return buf;
}

View File

@ -39,4 +39,6 @@
})
#endif
const char *saddr2str(struct sockaddr *addr, char buf[], int len, int *port);
#endif