From 718fd13481012b9c890affae5e276d26426c7344 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 26 Sep 2014 16:46:10 +0000 Subject: [PATCH] added unix socket support into httpd urs --- qse/cmd/http/ursd.c | 6 ++++ qse/include/qse/cmn/nwad.h | 3 ++ qse/lib/http/httpd-std-urs.h | 54 +++++++++++++++++++++++++++++------- qse/lib/http/httpd-std.c | 4 +-- qse/lib/http/httpd.c | 2 +- 5 files changed, 56 insertions(+), 13 deletions(-) diff --git a/qse/cmd/http/ursd.c b/qse/cmd/http/ursd.c index e53ca634..cd92b25b 100644 --- a/qse/cmd/http/ursd.c +++ b/qse/cmd/http/ursd.c @@ -189,6 +189,12 @@ static qse_sck_hnd_t open_server_socket (int proto, const qse_nwad_t* bindnwad) type = SOCK_DGRAM; #endif + if (bindnwad->type == QSE_NWAD_LOCAL) + { + proto = 0; + /* TODO: delete sun_path */ + } + s = socket (family, type, proto); if (!qse_isvalidsckhnd(s)) { diff --git a/qse/include/qse/cmn/nwad.h b/qse/include/qse/cmn/nwad.h index 5c99bf86..6966ce70 100644 --- a/qse/include/qse/cmn/nwad.h +++ b/qse/include/qse/cmn/nwad.h @@ -107,6 +107,9 @@ struct qse_skad_t #endif /* TODO: is this large enough?? */ qse_uint8_t data[QSE_SKAD_DATA_SIZE]; + + /* dummy member to secure extra space and force structure alignment */ + qse_uintptr_t dummy; }; #ifdef __cplusplus diff --git a/qse/lib/http/httpd-std-urs.h b/qse/lib/http/httpd-std-urs.h index 479b2d15..1c1915b8 100644 --- a/qse/lib/http/httpd-std-urs.h +++ b/qse/lib/http/httpd-std-urs.h @@ -133,23 +133,47 @@ static int urs_open (qse_httpd_t* httpd, qse_httpd_urs_t* urs) #if defined(AF_INET6) urs->handle[1] = open_udp_socket (httpd, AF_INET6, type, proto); #endif - /*urs->handle[2] = open_unix_socket (httpd, AF_UNIX, SOCK_DGRAM);*/ + urs->handle[2] = open_udp_socket (httpd, AF_UNIX, type, 0); - if (!qse_isvalidsckhnd(urs->handle[0]) && !qse_isvalidsckhnd(urs->handle[1])) + if (!qse_isvalidsckhnd(urs->handle[0]) && + !qse_isvalidsckhnd(urs->handle[1]) && + !qse_isvalidsckhnd(urs->handle[2])) { goto oops; } + if (qse_isvalidsckhnd(urs->handle[2])) + { + struct sockaddr_un addr; + QSE_MEMSET (&addr, 0, QSE_SIZEOF(addr)); + addr.sun_family = AF_UNIX; + qse_mbsxfmt (addr.sun_path, QSE_COUNTOF(addr.sun_path), QSE_MT("/tmp/qsehttpd-%d.urs.sock"), (int)QSE_GETPID()); + QSE_UNLINK (addr.sun_path); + bind (urs->handle[2], (struct sockaddr*)&addr, QSE_SIZEOF(addr)); +/* TOOD: unlink this socket in urs_close() also... */ + } + /* carry on regardless of success or failure */ dc->skadlen = qse_nwadtoskad (&nwad, &dc->skad); /* determine which socket to use when sending a request to the server */ if (dc->skadlen >= 0) { - if (nwad.type == QSE_NWAD_IN4) - dc->urs_socket = urs->handle[0]; - else - dc->urs_socket = urs->handle[1]; + switch (nwad.type) + { + case QSE_NWAD_IN4: + dc->urs_socket = urs->handle[0]; + break; + case QSE_NWAD_IN6: + dc->urs_socket = urs->handle[1]; + break; + case QSE_NWAD_LOCAL: + dc->urs_socket = urs->handle[2]; + break; + default: + dc->urs_socket = QSE_INVALID_SCKHND; + break; + } } else { @@ -414,10 +438,20 @@ printf ("... URS_SEND.....................\n"); req->urs_skadlen = qse_nwadtoskad (&urs_server->nwad, &req->urs_skad); if (req->urs_skadlen <= -1) goto default_urs_server; - if (urs_server->nwad.type == QSE_NWAD_IN4) - req->urs_socket = urs->handle[0]; - else - req->urs_socket = urs->handle[1]; + switch (urs_server->nwad.type) + { + case QSE_NWAD_IN4: + req->urs_socket = urs->handle[0]; + break; + case QSE_NWAD_IN6: + req->urs_socket = urs->handle[1]; + break; + case QSE_NWAD_LOCAL: + req->urs_socket = urs->handle[2]; + break; + default: + goto default_urs_server; + } } else { diff --git a/qse/lib/http/httpd-std.c b/qse/lib/http/httpd-std.c index 9c577ebd..ed0f598f 100644 --- a/qse/lib/http/httpd-std.c +++ b/qse/lib/http/httpd-std.c @@ -74,6 +74,7 @@ # include "../cmn/syscall.h" # include # include +# include # if defined(HAVE_SYS_SENDFILE_H) # include # endif @@ -846,10 +847,9 @@ static int server_open (qse_httpd_t* httpd, qse_httpd_server_t* server) { /* TODO: logging. warning only */ /* this is not a hard failure */ -qse_printf (QSE_STDERR, QSE_T("Failed to enable SO_REUSEPORT\n")); +qse_fprintf (QSE_STDERR, QSE_T("Failed to enable SO_REUSEPORT\n")); } #endif - /* TODO: linux. use capset() to set required capabilities just in case */ diff --git a/qse/lib/http/httpd.c b/qse/lib/http/httpd.c index 8aff80d6..8a823129 100644 --- a/qse/lib/http/httpd.c +++ b/qse/lib/http/httpd.c @@ -957,7 +957,7 @@ static int activate_urs (qse_httpd_t* httpd) httpd->urs.type = QSE_HTTPD_URS; - for (i = 0; i < httpd->dns.handle_count; i++) + for (i = 0; i < httpd->urs.handle_count; i++) { if (httpd->urs.handle_mask & (1 << i)) {