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;
}
Example #2
0
LWS_VISIBLE LWS_EXTERN struct lws *
libwebsocket_client_connect(struct lws_context *clients,
                  const char *address,
                  int port,
                  int ssl_connection,
                  const char *path,
                  const char *host,
                  const char *origin,
                  const char *protocol,
                  int ietf_version_or_minus_one)
{
    return lws_client_connect(clients, address, port, ssl_connection,
            path, host, origin, protocol, ietf_version_or_minus_one);
}
Example #3
0
LWS_VISIBLE struct lws *
lws_client_connect_extended(struct lws_context *context, const char *address,
			    int port, int ssl_connection, const char *path,
			    const char *host, const char *origin,
			    const char *protocol, int ietf_version_or_minus_one,
			    void *userdata)
{
	struct lws *wsi;

	wsi = lws_client_connect(context, address, port, ssl_connection, path,
				 host, origin, protocol,
				 ietf_version_or_minus_one);

	if (wsi && !wsi->user_space && userdata) {
		wsi->user_space_externally_allocated = 1;
		wsi->user_space = userdata ;
	}

	return wsi;
}
Example #4
0
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;
}
Example #5
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;
}