diff --git a/Makefile b/Makefile index 521d4114..b0257f45 100644 --- a/Makefile +++ b/Makefile @@ -51,6 +51,7 @@ LIB_SOURCES = src/libhttp.c \ src/httplib_close_socket_gracefully.c \ src/httplib_compare_dir_entries.c \ src/httplib_connect_client.c \ + src/httplib_connect_socket.c \ src/httplib_connect_websocket_client.c \ src/httplib_construct_etag.c \ src/httplib_consume_socket.c \ diff --git a/src/httplib_connect_socket.c b/src/httplib_connect_socket.c new file mode 100644 index 00000000..86bb8695 --- /dev/null +++ b/src/httplib_connect_socket.c @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2016 Lammert Bies + * Copyright (c) 2013-2016 the Civetweb developers + * Copyright (c) 2004-2013 Sergey Lyubka + * + * 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 "libhttp-private.h" + + + +/* + * int XX_httplib_connect_socket(); + * + * The function XX_httplib_connect_socket() starts a connection over a socket. + * The context structure may be NULL. The output socket and the socket address + * may not be null for this function to succeed. + */ + +int XX_httplib_connect_socket( struct mg_context *ctx, const char *host, int port, int use_ssl, char *ebuf, size_t ebuf_len, SOCKET *sock, union usa *sa ) { + + int ip_ver = 0; + + *sock = INVALID_SOCKET; + memset(sa, 0, sizeof(*sa)); + + if (ebuf_len > 0) *ebuf = 0; + + if (host == NULL) { + XX_httplib_snprintf(NULL, NULL, ebuf, ebuf_len, "%s", "NULL host"); + return 0; + } + + if (port < 0 || !XX_httplib_is_valid_port((unsigned)port)) { + XX_httplib_snprintf(NULL, NULL, ebuf, ebuf_len, "%s", "invalid port"); + return 0; + } + +#if !defined(NO_SSL) + if (use_ssl && (SSLv23_client_method == NULL)) { + XX_httplib_snprintf(NULL, NULL, ebuf, ebuf_len, "%s", "SSL is not initialized"); + return 0; + } +#else + (void)use_ssl; +#endif + + if (XX_httplib_inet_pton(AF_INET, host, &sa->sin, sizeof(sa->sin))) { + sa->sin.sin_port = htons((uint16_t)port); + ip_ver = 4; +#ifdef USE_IPV6 + } else if (XX_httplib_inet_pton(AF_INET6, host, &sa->sin6, sizeof(sa->sin6))) { + sa->sin6.sin6_port = htons((uint16_t)port); + ip_ver = 6; + } else if (host[0] == '[') { + /* While getaddrinfo on Windows will work with [::1], + * getaddrinfo on Linux only works with ::1 (without []). */ + size_t l = strlen(host + 1); + char *h = (l > 1) ? XX_httplib_strdup(host + 1) : NULL; + if (h) { + h[l - 1] = 0; + if (XX_httplib_inet_pton(AF_INET6, h, &sa->sin6, sizeof(sa->sin6))) { + sa->sin6.sin6_port = htons((uint16_t)port); + ip_ver = 6; + } + XX_httplib_free(h); + } +#endif + } + + if (ip_ver == 0) { + XX_httplib_snprintf(NULL, NULL, ebuf, ebuf_len, "%s", "host not found"); + return 0; + } + + if (ip_ver == 4) { *sock = socket(PF_INET, SOCK_STREAM, 0); } +#ifdef USE_IPV6 + else if (ip_ver == 6) { *sock = socket(PF_INET6, SOCK_STREAM, 0); } +#endif + + if (*sock == INVALID_SOCKET) { + XX_httplib_snprintf(NULL, NULL, ebuf, ebuf_len, "socket(): %s", strerror(ERRNO)); + return 0; + } + + XX_httplib_set_close_on_exec(*sock, XX_httplib_fc(ctx)); + + if ((ip_ver == 4) && (connect(*sock, (struct sockaddr *)&sa->sin, sizeof(sa->sin)) == 0)) { + /* connected with IPv4 */ + return 1; + } + +#ifdef USE_IPV6 + if ((ip_ver == 6) && (connect(*sock, (struct sockaddr *)&sa->sin6, sizeof(sa->sin6)) == 0)) { + /* connected with IPv6 */ + return 1; + } +#endif + + /* Not connected */ + XX_httplib_snprintf(NULL, NULL, ebuf, ebuf_len, "connect(%s:%d): %s", host, port, strerror(ERRNO)); + closesocket(*sock); + *sock = INVALID_SOCKET; + return 0; + +} /* XX_httplib_connect_socket */ diff --git a/src/libhttp-private.h b/src/libhttp-private.h index 4005cb77..ca49f91a 100644 --- a/src/libhttp-private.h +++ b/src/libhttp-private.h @@ -932,6 +932,7 @@ void XX_httplib_handle_ssi_file_request( struct mg_connection *conn, const cha void XX_httplib_handle_static_file_request( struct mg_connection *conn, const char *path, struct file *filep, const char *mime_type, const char *additional_headers ); void XX_httplib_handle_websocket_request( struct mg_connection *conn, const char *path, int is_callback_resource, mg_websocket_connect_handler ws_connect_handler, mg_websocket_ready_handler ws_ready_handler, mg_websocket_data_handler ws_data_handler, mg_websocket_close_handler ws_close_handler, void *cbData ); int XX_httplib_header_has_option( const char *header, const char *option ); +int XX_httplib_inet_pton( int af, const char *src, void *dst, size_t dstlen ); int XX_httplib_initialize_ssl( struct mg_context *ctx ); void XX_httplib_interpret_uri( struct mg_connection *conn, char *filename, size_t filename_buf_len, struct file *filep, int *is_found, int *is_script_resource, int *is_websocket_request, int *is_put_or_delete_request ); int XX_httplib_is_authorized_for_put( struct mg_connection *conn ); diff --git a/src/libhttp.c b/src/libhttp.c index 7fa420f6..e123289c 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -4323,7 +4323,7 @@ int XX_httplib_is_valid_port(unsigned long port) { } /* XX_httplib_is_valid_port */ -static int mg_inet_pton(int af, const char *src, void *dst, size_t dstlen) { +int XX_httplib_inet_pton( int af, const char *src, void *dst, size_t dstlen ) { struct addrinfo hints, *res, *ressave; int func_ret = 0; @@ -4356,102 +4356,5 @@ static int mg_inet_pton(int af, const char *src, void *dst, size_t dstlen) { freeaddrinfo(ressave); return func_ret; -} - - -/* - * int XX_httplib_connect_socket(); - * - * The function XX_httplib_connect_socket() starts a connection over a socket. - * The context structure may be NULL. The output socket and the socket address - * may not be null for this function to succeed. - */ - -int XX_httplib_connect_socket( struct mg_context *ctx, const char *host, int port, int use_ssl, char *ebuf, size_t ebuf_len, SOCKET *sock, union usa *sa ) { - - int ip_ver = 0; - - *sock = INVALID_SOCKET; - memset(sa, 0, sizeof(*sa)); - - if (ebuf_len > 0) *ebuf = 0; - - if (host == NULL) { - XX_httplib_snprintf(NULL, NULL, ebuf, ebuf_len, "%s", "NULL host"); - return 0; - } - - if (port < 0 || !XX_httplib_is_valid_port((unsigned)port)) { - XX_httplib_snprintf(NULL, NULL, ebuf, ebuf_len, "%s", "invalid port"); - return 0; - } - -#if !defined(NO_SSL) - if (use_ssl && (SSLv23_client_method == NULL)) { - XX_httplib_snprintf(NULL, NULL, ebuf, ebuf_len, "%s", "SSL is not initialized"); - return 0; - } -#else - (void)use_ssl; -#endif - - if (mg_inet_pton(AF_INET, host, &sa->sin, sizeof(sa->sin))) { - sa->sin.sin_port = htons((uint16_t)port); - ip_ver = 4; -#ifdef USE_IPV6 - } else if (mg_inet_pton(AF_INET6, host, &sa->sin6, sizeof(sa->sin6))) { - sa->sin6.sin6_port = htons((uint16_t)port); - ip_ver = 6; - } else if (host[0] == '[') { - /* While getaddrinfo on Windows will work with [::1], - * getaddrinfo on Linux only works with ::1 (without []). */ - size_t l = strlen(host + 1); - char *h = (l > 1) ? XX_httplib_strdup(host + 1) : NULL; - if (h) { - h[l - 1] = 0; - if (mg_inet_pton(AF_INET6, h, &sa->sin6, sizeof(sa->sin6))) { - sa->sin6.sin6_port = htons((uint16_t)port); - ip_ver = 6; - } - XX_httplib_free(h); - } -#endif - } - - if (ip_ver == 0) { - XX_httplib_snprintf(NULL, NULL, ebuf, ebuf_len, "%s", "host not found"); - return 0; - } - - if (ip_ver == 4) { *sock = socket(PF_INET, SOCK_STREAM, 0); } -#ifdef USE_IPV6 - else if (ip_ver == 6) { *sock = socket(PF_INET6, SOCK_STREAM, 0); } -#endif - - if (*sock == INVALID_SOCKET) { - XX_httplib_snprintf(NULL, NULL, ebuf, ebuf_len, "socket(): %s", strerror(ERRNO)); - return 0; - } - - XX_httplib_set_close_on_exec(*sock, XX_httplib_fc(ctx)); - - if ((ip_ver == 4) && (connect(*sock, (struct sockaddr *)&sa->sin, sizeof(sa->sin)) == 0)) { - /* connected with IPv4 */ - return 1; - } - -#ifdef USE_IPV6 - if ((ip_ver == 6) && (connect(*sock, (struct sockaddr *)&sa->sin6, sizeof(sa->sin6)) == 0)) { - /* connected with IPv6 */ - return 1; - } -#endif - - /* Not connected */ - XX_httplib_snprintf(NULL, NULL, ebuf, ebuf_len, "connect(%s:%d): %s", host, port, strerror(ERRNO)); - closesocket(*sock); - *sock = INVALID_SOCKET; - return 0; - -} /* XX_httplib_connect_socket */ +} /* XX_httplib_inet_pton */