int discord_ws_init(struct im_connection *ic, discord_data *dd) { struct lws_context_creation_info info; memset(&info, 0, sizeof(info)); info.port = CONTEXT_PORT_NO_LISTEN; info.protocols = protocols; #ifndef LWS_NO_EXTENSIONS info.extensions = lws_get_internal_extensions(); #else info.extensions = NULL; #endif info.gid = -1; info.uid = -1; info.user = ic; lws_set_log_level(0, NULL); dd->lwsctx = lws_create_context(&info); if (dd->lwsctx == NULL) { return -1; } dd->lws = lws_client_connect(dd->lwsctx, dd->gateway->addr, dd->gateway->wss ? 443 : 80, dd->gateway->wss, dd->gateway->path, dd->gateway->addr, "discordapp.com", protocols[0].name, -1); return 0; }
int main(void) { /*server url will usd port 5000*/ int port = 5000; const char *interface = NULL; struct lws_context_creation_info info; struct lws_protocols protocol; struct lws_context *context; /*Not using ssl*/ const char *cert_path = NULL; const char *key_path = NULL; /*no special options*/ int opts = 0; /* register the signal SIGINT handler */ struct sigaction act; act.sa_handler = INT_HANDLER; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction( SIGINT, &act, 0); /* setup websocket protocol */ protocol.name = "my-echo-protocol"; protocol.callback = ws_service_callback; protocol.per_session_data_size=sizeof(struct per_session_data); protocol.rx_buffer_size = 0; /* setup websocket context info*/ memset(&info, 0, sizeof info); info.port = port; info.iface = interface; info.protocols = &protocol; info.extensions = lws_get_internal_extensions(); info.ssl_cert_filepath = cert_path; info.ssl_private_key_filepath = key_path; info.gid = -1; info.uid = -1; info.options = opts; /* create libwebsocket context. */ context = lws_create_context(&info); if (context == NULL) { printf(KRED"[Main] Websocket context create error.\n"RESET); return -1; } printf(KGRN"[Main] Websocket context create success.\n"RESET); /* websocket service */ while ( !destroy_flag ) { lws_service(context, 50); } usleep(10); lws_context_destroy(context); return 0; }
bool websocket_server::start() { struct lws_context_creation_info info; memset(&info, 0, sizeof info); info.port = world()->websocket_port(); info.iface = NULL; info.protocols = protocols; info.extensions = lws_get_internal_extensions(); info.ssl_cert_filepath = NULL; info.ssl_private_key_filepath = NULL; info.gid = -1; info.uid = -1; info.options = 0; info.user = this; context_ = lws_create_context(&info); return context_ != NULL; }
int main(int argc, char **argv) { int n = 0, ret = 0, port = 7681, use_ssl = 0; unsigned int rl_dumb = 0, rl_mirror = 0; struct lws_context_creation_info info; struct lws_context *context; int ietf_version = -1; /* latest */ const char *address; memset(&info, 0, sizeof info); fprintf(stderr, "libwebsockets test client\n" "(C) Copyright 2010-2015 Andy Green <*****@*****.**> " "licensed under LGPL2.1\n"); if (argc < 2) goto usage; while (n >= 0) { n = getopt_long(argc, argv, "nuv:hsp:d:l", options, NULL); if (n < 0) continue; switch (n) { case 'd': lws_set_log_level(atoi(optarg), NULL); break; case 's': use_ssl = 2; /* 2 = allow selfsigned */ break; case 'p': port = atoi(optarg); break; case 'l': longlived = 1; break; case 'v': ietf_version = atoi(optarg); break; case 'u': deny_deflate = 1; break; case 'n': deny_mux = 1; break; case 'h': goto usage; } } if (optind >= argc) goto usage; signal(SIGINT, sighandler); address = argv[optind]; /* * create the websockets context. This tracks open connections and * knows how to route any traffic and which protocol version to use, * and if each connection is client or server side. * * For this client-only demo, we tell it to not listen on any port. */ info.port = CONTEXT_PORT_NO_LISTEN; info.protocols = protocols; #ifndef LWS_NO_EXTENSIONS info.extensions = lws_get_internal_extensions(); #endif info.gid = -1; info.uid = -1; context = lws_create_context(&info); if (context == NULL) { fprintf(stderr, "Creating libwebsocket context failed\n"); return 1; } /* * sit there servicing the websocket context to handle incoming * packets, and drawing random circles on the mirror protocol websocket * * nothing happens until the client websocket connection is * asynchronously established... calling lws_client_connect() only * instantiates the connection logically, lws_service() progresses it * asynchronously. */ while (!force_exit) { if (!wsi_dumb && ratelimit_connects(&rl_dumb, 2u)) { lwsl_notice("dumb: connecting\n"); wsi_dumb = lws_client_connect(context, address, port, use_ssl, "/", argv[optind], argv[optind], protocols[PROTOCOL_DUMB_INCREMENT].name, ietf_version); } if (!wsi_mirror && ratelimit_connects(&rl_mirror, 2u)) { lwsl_notice("mirror: connecting\n"); wsi_mirror = lws_client_connect(context, address, port, use_ssl, "/", argv[optind], argv[optind], protocols[PROTOCOL_LWS_MIRROR].name, ietf_version); } lws_service(context, 500); } lwsl_err("Exiting\n"); lws_context_destroy(context); return ret; usage: fprintf(stderr, "Usage: libwebsockets-test-client " "<server address> [--port=<p>] " "[--ssl] [-k] [-v <ver>] " "[-d <log bitfield>] [-l]\n"); return 1; }
LWS_VISIBLE LWS_EXTERN struct lws_extension * libwebsocket_get_internal_extensions() { return lws_get_internal_extensions(); }
int main(int argc, char **argv) { struct lws_context_creation_info info; char interface_name[128] = ""; unsigned int ms, oldms = 0; const char *iface = NULL; char cert_path[1024]; char key_path[1024]; int debug_level = 7; int use_ssl = 0; int opts = 0; int n = 0; #ifndef _WIN32 int syslog_options = LOG_PID | LOG_PERROR; #endif #ifndef LWS_NO_DAEMONIZE int daemonize = 0; #endif /* * take care to zero down the info struct, he contains random garbaage * from the stack otherwise */ memset(&info, 0, sizeof info); info.port = 7681; while (n >= 0) { n = getopt_long(argc, argv, "eci:hsap:d:Dr:", options, NULL); if (n < 0) continue; switch (n) { case 'e': opts |= LWS_SERVER_OPTION_LIBEV; break; #ifndef LWS_NO_DAEMONIZE case 'D': daemonize = 1; #ifndef _WIN32 syslog_options &= ~LOG_PERROR; #endif break; #endif case 'd': debug_level = atoi(optarg); break; case 's': use_ssl = 1; break; case 'a': opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT; break; case 'p': info.port = atoi(optarg); break; case 'i': strncpy(interface_name, optarg, sizeof interface_name); interface_name[(sizeof interface_name) - 1] = '\0'; iface = interface_name; break; case 'c': close_testing = 1; fprintf(stderr, " Close testing mode -- closes on " "client after 50 dumb increments" "and suppresses lws_mirror spam\n"); break; case 'r': resource_path = optarg; printf("Setting resource path to \"%s\"\n", resource_path); break; case 'h': fprintf(stderr, "Usage: test-server " "[--port=<p>] [--ssl] " "[-d <log bitfield>] " "[--resource_path <path>]\n"); exit(1); } } #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32) /* * normally lock path would be /var/lock/lwsts or similar, to * simplify getting started without having to take care about * permissions or running as root, set to /tmp/.lwsts-lock */ if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) { fprintf(stderr, "Failed to daemonize\n"); return 1; } #endif signal(SIGINT, sighandler); #ifndef _WIN32 /* we will only try to log things according to our debug_level */ setlogmask(LOG_UPTO (LOG_DEBUG)); openlog("lwsts", syslog_options, LOG_DAEMON); #endif /* tell the library what debug level to emit and to send it to syslog */ lws_set_log_level(debug_level, lwsl_emit_syslog); lwsl_notice("libwebsockets test server - " "(C) Copyright 2010-2015 Andy Green <*****@*****.**> - " "licensed under LGPL2.1\n"); printf("Using resource path \"%s\"\n", resource_path); #ifdef EXTERNAL_POLL max_poll_elements = getdtablesize(); pollfds = malloc(max_poll_elements * sizeof (struct lws_pollfd)); fd_lookup = malloc(max_poll_elements * sizeof (int)); if (pollfds == NULL || fd_lookup == NULL) { lwsl_err("Out of memory pollfds=%d\n", max_poll_elements); return -1; } #endif info.iface = iface; info.protocols = protocols; #ifndef LWS_NO_EXTENSIONS info.extensions = lws_get_internal_extensions(); #endif info.ssl_cert_filepath = NULL; info.ssl_private_key_filepath = NULL; if (use_ssl) { if (strlen(resource_path) > sizeof(cert_path) - 32) { lwsl_err("resource path too long\n"); return -1; } sprintf(cert_path, "%s/libwebsockets-test-server.pem", resource_path); if (strlen(resource_path) > sizeof(key_path) - 32) { lwsl_err("resource path too long\n"); return -1; } sprintf(key_path, "%s/libwebsockets-test-server.key.pem", resource_path); info.ssl_cert_filepath = cert_path; info.ssl_private_key_filepath = key_path; } info.gid = -1; info.uid = -1; info.max_http_header_pool = 1; info.options = opts; context = lws_create_context(&info); if (context == NULL) { lwsl_err("libwebsocket init failed\n"); return -1; } /* this shows how to override the lws file operations. You don't need * to do any of this unless you have a reason (eg, want to serve * compressed files without decompressing the whole archive) */ /* stash original platform fops */ fops_plat = *(lws_get_fops(context)); /* override the active fops */ lws_get_fops(context)->open = test_server_fops_open; n = 0; while (n >= 0 && !force_exit) { struct timeval tv; gettimeofday(&tv, NULL); /* * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every * live websocket connection using the DUMB_INCREMENT protocol, * as soon as it can take more packets (usually immediately) */ ms = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); if ((ms - oldms) > 50) { lws_callback_on_writable_all_protocol(context, &protocols[PROTOCOL_DUMB_INCREMENT]); oldms = ms; } #ifdef EXTERNAL_POLL /* * this represents an existing server's single poll action * which also includes libwebsocket sockets */ n = poll(pollfds, count_pollfds, 50); if (n < 0) continue; if (n) for (n = 0; n < count_pollfds; n++) if (pollfds[n].revents) /* * returns immediately if the fd does not * match anything under libwebsockets * control */ if (lws_service_fd(context, &pollfds[n]) < 0) goto done; #else /* * If libwebsockets sockets are all we care about, * you can use this api which takes care of the poll() * and looping through finding who needed service. * * If no socket needs service, it'll return anyway after * the number of ms in the second argument. */ n = lws_service(context, 50); #endif } #ifdef EXTERNAL_POLL done: #endif lws_context_destroy(context); lwsl_notice("libwebsockets-test-server exited cleanly\n"); #ifndef _WIN32 closelog(); #endif return 0; }
int main(int argc, char **argv) { int sigs[] = { SIGINT, SIGKILL, SIGTERM, SIGSEGV, SIGFPE }; struct ev_signal signals[ARRAY_SIZE(sigs)]; struct ev_loop *loop = ev_default_loop(0); struct lws_context_creation_info info; char interface_name[128] = ""; const char *iface = NULL; ev_timer timeout_watcher; char cert_path[1024]; char key_path[1024]; int debug_level = 7; int use_ssl = 0; int opts = 0; int n = 0; #ifndef _WIN32 int syslog_options = LOG_PID | LOG_PERROR; #endif #ifndef LWS_NO_DAEMONIZE int daemonize = 0; #endif /* * take care to zero down the info struct, he contains random garbaage * from the stack otherwise */ memset(&info, 0, sizeof info); info.port = 7681; while (n >= 0) { n = getopt_long(argc, argv, "eci:hsap:d:Dr:", options, NULL); if (n < 0) continue; switch (n) { case 'e': opts |= LWS_SERVER_OPTION_LIBEV; break; #ifndef LWS_NO_DAEMONIZE case 'D': daemonize = 1; #ifndef _WIN32 syslog_options &= ~LOG_PERROR; #endif break; #endif case 'd': debug_level = atoi(optarg); break; case 's': use_ssl = 1; break; case 'a': opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT; break; case 'p': info.port = atoi(optarg); break; case 'i': strncpy(interface_name, optarg, sizeof interface_name); interface_name[(sizeof interface_name) - 1] = '\0'; iface = interface_name; break; case 'c': close_testing = 1; fprintf(stderr, " Close testing mode -- closes on " "client after 50 dumb increments" "and suppresses lws_mirror spam\n"); break; case 'r': resource_path = optarg; printf("Setting resource path to \"%s\"\n", resource_path); break; case 'h': fprintf(stderr, "Usage: test-server " "[--port=<p>] [--ssl] " "[-d <log bitfield>] " "[--resource_path <path>]\n"); exit(1); } } #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32) /* * normally lock path would be /var/lock/lwsts or similar, to * simplify getting started without having to take care about * permissions or running as root, set to /tmp/.lwsts-lock */ if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) { fprintf(stderr, "Failed to daemonize\n"); return 1; } #endif for (n = 0; n < ARRAY_SIZE(sigs); n++) { _ev_init(&signals[n], signal_cb); ev_signal_set(&signals[n], sigs[n]); ev_signal_start(loop, &signals[n]); } #ifndef _WIN32 /* we will only try to log things according to our debug_level */ setlogmask(LOG_UPTO (LOG_DEBUG)); openlog("lwsts", syslog_options, LOG_DAEMON); #endif /* tell the library what debug level to emit and to send it to syslog */ lws_set_log_level(debug_level, lwsl_emit_syslog); lwsl_notice("libwebsockets test server - " "(C) Copyright 2010-2015 Andy Green <*****@*****.**> - " "licensed under LGPL2.1\n"); printf("Using resource path \"%s\"\n", resource_path); info.iface = iface; info.protocols = protocols; #ifndef LWS_NO_EXTENSIONS info.extensions = lws_get_internal_extensions(); #endif info.ssl_cert_filepath = NULL; info.ssl_private_key_filepath = NULL; if (use_ssl) { if (strlen(resource_path) > sizeof(cert_path) - 32) { lwsl_err("resource path too long\n"); return -1; } sprintf(cert_path, "%s/libwebsockets-test-server.pem", resource_path); if (strlen(resource_path) > sizeof(key_path) - 32) { lwsl_err("resource path too long\n"); return -1; } sprintf(key_path, "%s/libwebsockets-test-server.key.pem", resource_path); info.ssl_cert_filepath = cert_path; info.ssl_private_key_filepath = key_path; } info.gid = -1; info.uid = -1; info.max_http_header_pool = 1; info.options = opts | LWS_SERVER_OPTION_LIBEV; context = lws_create_context(&info); if (context == NULL) { lwsl_err("libwebsocket init failed\n"); return -1; } /* * this shows how to override the lws file operations. You don't need * to do any of this unless you have a reason (eg, want to serve * compressed files without decompressing the whole archive) */ /* stash original platform fops */ fops_plat = *(lws_get_fops(context)); /* override the active fops */ lws_get_fops(context)->open = test_server_fops_open; lws_initloop(context, loop); _ev_timer_init(&timeout_watcher, ev_timeout_cb, 0.05, 0.05); ev_timer_start(loop, &timeout_watcher); while (!force_exit) ev_run(loop, 0); lws_context_destroy(context); ev_loop_destroy(loop); lwsl_notice("libwebsockets-test-server exited cleanly\n"); #ifndef _WIN32 closelog(); #endif return 0; }
int main(int argc, char **argv) { int n = 0; int ret = 0; int port = 80; int use_ssl = 0; struct lws_context *context; const char *address; struct lws *wsi_jt; int ietf_version = -1; /* latest */ struct lws_context_creation_info info; memset(&info, 0, sizeof info); fprintf(stderr, "jittertrap test client\n"); if (argc < 2) goto usage; while (n >= 0) { n = getopt_long(argc, argv, "nuv:hsp:d:l", options, NULL); if (n < 0) continue; switch (n) { case 'd': lws_set_log_level(atoi(optarg), NULL); break; case 's': use_ssl = 2; /* 2 = allow selfsigned */ break; case 'p': port = atoi(optarg); break; case 'l': longlived = 1; break; case 'v': ietf_version = atoi(optarg); break; case 'u': deny_deflate = 1; break; case 'n': deny_mux = 1; break; case 'h': goto usage; } } if (optind >= argc) goto usage; signal(SIGINT, sighandler); address = argv[optind]; /* * create the websockets context. This tracks open connections and * knows how to route any traffic and which protocol version to use, * and if each connection is client or server side. * * For this client-only demo, we tell it to not listen on any port. */ info.port = CONTEXT_PORT_NO_LISTEN; info.protocols = protocols; #ifndef LWS_NO_EXTENSIONS info.extensions = lws_get_internal_extensions(); #endif info.gid = -1; info.uid = -1; context = lws_create_context(&info); if (context == NULL) { fprintf(stderr, "Creating libwebsocket context failed\n"); return 1; } /* create a client websocket */ wsi_jt = lws_client_connect( context, address, port, use_ssl, "/", argv[optind], argv[optind], protocols[PROTOCOL_JITTERTRAP].name, ietf_version); if (wsi_jt == NULL) { fprintf(stderr, "libwebsocket connect failed\n"); ret = 1; goto bail; } fprintf(stderr, "Waiting for connect...\n"); do { const struct timespec rqtp = {.tv_sec = 0, .tv_nsec = 1E5 }; nanosleep(&rqtp, NULL); } while (!force_exit && (0 == lws_service(context, 0))); bail: fprintf(stderr, "Exiting\n"); lws_context_destroy(context); return ret; usage: fprintf(stderr, "Usage: jittertrap-cli " "<server address> [--port=<p>] " "[--ssl] [-k] [-v <ver>] " "[-d <log bitfield>] [-l]\n"); return 1; }
int main(int argc, char **argv) { char cert_path[1024]; char key_path[1024]; int n = 0; int use_ssl = 0; int opts = 0; char interface_name[128] = ""; const char *iface = NULL; int syslog_options = LOG_PID | LOG_PERROR; struct lws_context_creation_info info; int debug_level = 7; #ifndef LWS_NO_DAEMONIZE int daemonize = 0; #endif memset(&info, 0, sizeof info); info.port = 7681; while (n >= 0) { n = getopt_long(argc, argv, "eci:hsap:d:Dr:", options, NULL); if (n < 0) continue; switch (n) { #ifndef LWS_NO_DAEMONIZE case 'D': daemonize = 1; syslog_options &= ~LOG_PERROR; break; #endif case 'd': debug_level = atoi(optarg); break; case 's': use_ssl = 1; break; case 'p': info.port = atoi(optarg); break; case 'i': strncpy(interface_name, optarg, sizeof interface_name); interface_name[(sizeof interface_name) - 1] = '\0'; iface = interface_name; break; case 'c': close_testing = 1; fprintf(stderr, " Close testing mode -- closes on " "client after 50 messages.\n"); break; case 'r': resource_path = optarg; printf("Setting resource path to \"%s\"\n", resource_path); break; case 'h': fprintf(stderr, "Usage: " PROGNAME "[--port=<p>] [--ssl] " "[-d <log bitfield>] " "[--resource_path <path>]\n"); exit(1); } } #if !defined(LWS_NO_DAEMONIZE) /* * normally lock path would be /var/lock/lwsts or similar, to * simplify getting started without having to take care about * permissions or running as root, set to /tmp/.lwsts-lock */ if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) { fprintf(stderr, "Failed to daemonize\n"); return 1; } #endif signal(SIGINT, sighandler); /* we will only try to log things according to our debug_level */ setlogmask(LOG_UPTO(LOG_DEBUG)); openlog("lwsts", syslog_options, LOG_DAEMON); /* tell the library what debug level to emit and to send it to syslog */ lws_set_log_level(debug_level, lwsl_emit_syslog); lwsl_notice("jittertrap server\n"); printf("Using resource path \"%s\"\n", resource_path); info.iface = iface; info.protocols = protocols; #ifndef LWS_NO_EXTENSIONS info.extensions = lws_get_internal_extensions(); #endif if (!use_ssl) { info.ssl_cert_filepath = NULL; info.ssl_private_key_filepath = NULL; } else { if (strlen(resource_path) > sizeof(cert_path) - 32) { lwsl_err("resource path too long\n"); return -1; } sprintf(cert_path, "%s/libwebsockets-test-server.pem", resource_path); if (strlen(resource_path) > sizeof(key_path) - 32) { lwsl_err("resource path too long\n"); return -1; } sprintf(key_path, "%s/libwebsockets-test-server.key.pem", resource_path); info.ssl_cert_filepath = cert_path; info.ssl_private_key_filepath = key_path; } info.gid = -1; info.uid = -1; info.options = opts; context = lws_create_context(&info); if (context == NULL) { lwsl_err("libwebsocket init failed\n"); return -1; } n = 0; while (n >= 0 && !force_exit) { lws_callback_on_writable_all_protocol( context, &protocols[PROTOCOL_JITTERTRAP]); /* FIXME: something is causing us to spin. This helps to * slow things down, but it's not a proper solution. */ usleep(1); /* * takes care of the poll() and looping through finding who * needs service. * * If no socket needs service, it'll return anyway after * the number of ms in the second argument. */ n = lws_service(context, 1); } lws_context_destroy(context); lwsl_notice("jittertrap server exited cleanly\n"); closelog(); return 0; }