diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index 2674aee4..044a1580 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -561,6 +561,24 @@ libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable) return 1; } +/** + * libwebsocket_canonical_hostname() - returns this host's hostname + * + * This is typically used by client code to fill in the host parameter + * when making a client connection. You can only call it after the context + * has been created. + * + * @this: Websocket context + */ + + +extern const char * +libwebsocket_canonical_hostname(struct libwebsocket_context *this) +{ + return (const char *)this->canonical_hostname; +} + + static void sigpipe_handler(int x) { } @@ -624,6 +642,8 @@ libwebsocket_create_context(int port, struct libwebsocket_context *this = NULL; unsigned int slen; char *p; + char hostname[1024]; + struct hostent* he; #ifdef LWS_OPENSSL_SUPPORT SSL_METHOD *method; @@ -640,6 +660,15 @@ libwebsocket_create_context(int port, this->http_proxy_port = 0; this->http_proxy_address[0] = '\0'; + /* find canonical hostname */ + + hostname[(sizeof hostname) - 1] = '\0'; + gethostname(hostname, (sizeof hostname) - 1); + he = gethostbyname(hostname); + strncpy(this->canonical_hostname, he->h_name, + sizeof this->canonical_hostname - 1); + this->canonical_hostname[sizeof this->canonical_hostname - 1] = '\0'; + /* split the proxy ads:port if given */ p = getenv("http_proxy"); diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h index ac28abf8..f68a4a3f 100644 --- a/lib/libwebsockets.h +++ b/lib/libwebsockets.h @@ -249,7 +249,10 @@ libwebsocket_client_connect(struct libwebsocket_context *clients, const char *origin, const char *protocol); -void +extern const char * +libwebsocket_canonical_hostname(struct libwebsocket_context *this); + +extern void libwebsocket_client_close(struct libwebsocket *wsi); #endif diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index 2cb3f2b6..6a5df59c 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -169,6 +170,7 @@ struct libwebsocket_context { int fds_count; int listen_port; char http_proxy_address[256]; + char canonical_hostname[1024]; unsigned int http_proxy_port; #ifdef LWS_OPENSSL_SUPPORT int use_ssl; diff --git a/libwebsockets-api-doc.html b/libwebsockets-api-doc.html index d0703436..f3ee10e1 100644 --- a/libwebsockets-api-doc.html +++ b/libwebsockets-api-doc.html @@ -112,6 +112,23 @@ If the output side of a server process becomes choked, this allows flow control for the input side.
+

libwebsocket_canonical_hostname - returns this host's hostname

+const char * +libwebsocket_canonical_hostname +(struct libwebsocket_context * this) +

Arguments

+
+
this +
Websocket context +
+

Description

+
+

+This is typically used by client code to fill in the host parameter +when making a client connection. You can only call it after the context +has been created. +

+

libwebsocket_create_context - Create the websocket handler

struct libwebsocket_context * libwebsocket_create_context diff --git a/test-server/test-client.c b/test-server/test-client.c index 734aab20..289374aa 100644 --- a/test-server/test-client.c +++ b/test-server/test-client.c @@ -25,7 +25,6 @@ #include #include - #include "../lib/libwebsockets.h" #include @@ -170,7 +169,6 @@ int main(int argc, char **argv) struct libwebsocket *wsi_dumb; struct libwebsocket *wsi_mirror; - fprintf(stderr, "libwebsockets test client\n" "(C) Copyright 2010 Andy Green " "licensed under LGPL2.1\n"); @@ -215,7 +213,7 @@ int main(int argc, char **argv) /* create a client websocket using dumb increment protocol */ wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl, - "/", "http://host", "origin", + "/", libwebsocket_canonical_hostname(context), "origin", protocols[PROTOCOL_DUMB_INCREMENT].name); if (wsi_dumb == NULL) { @@ -226,7 +224,7 @@ int main(int argc, char **argv) /* create a client websocket using mirror protocol */ wsi_mirror = libwebsocket_client_connect(context, address, port, - use_ssl, "/", "http://host", "origin", + use_ssl, "/", libwebsocket_canonical_hostname(context), "origin", protocols[PROTOCOL_LWS_MIRROR].name); if (wsi_mirror == NULL) { diff --git a/test-server/test-ping.c b/test-server/test-ping.c index 69119238..434e9d07 100644 --- a/test-server/test-ping.c +++ b/test-server/test-ping.c @@ -370,9 +370,9 @@ int main(int argc, char **argv) /* create a client websocket using dumb increment protocol */ - wsi_mirror = libwebsocket_client_connect(context, address, port, use_ssl, - "/", "http://host", "origin", - protocols[PROTOCOL_LWS_MIRROR].name); + wsi_mirror = libwebsocket_client_connect(context, address, port, + use_ssl, "/", libwebsocket_canonical_hostname(context), + "origin", protocols[PROTOCOL_LWS_MIRROR].name); if (wsi_mirror == NULL) { fprintf(stderr, "libwebsocket connect failed\n");