void jumpwireIo::loop()
{ 
  if(ws_status == 4){  //Wifi error
    ws_connect();
  }else if(ws_status == 3){ //Websocket error
    httpConnect();
  }else if(ws_status == 2){ //Waiting for an incomming message
    parseWebsocket();
  }
  if(millis() - pingtimer > 55000){ //ping every 55 sec.
    pingtimer = millis();
    Ping();
  }
}
Beispiel #2
0
int main(int argc, char ** argv)
{
   char buf[8000];
   websocket_t ws;
   int fd, plen, len;

   fd = ws_connect();

   ws_init(&ws, fd, 1000, 1);

   for (int eof = 0; !eof;)
   {
      for (len = 0; !eof;)
      {
         if ((plen = read(0, buf + len, sizeof(buf) - len)) == -1)
            exit(1);

         // check for eof
         eof = plen == 0;

         len += plen;
         // check for "\n.\n" (single period on line)
         if (len > 2 && buf[len - 1] == '\n' &&
               buf[len - 3] == '\n' && buf[len - 2] == '.')
         {
            // remove it
            len -= 2;
            break;
         }
      }

      if ((ws_write(&ws, buf, len)) == -1)
         exit(1);

      if ((plen = ws_read(&ws, buf, sizeof(buf))) == -1)
         exit(1);

      if (!plen)
         break;

      write(1, buf, plen);
   }

   ws_free(&ws);

   close(fd);
   return 0;
}
void jumpwireIo::setup()
{
  if(debug){
    //ESP.restart();
    Serial.begin(115200);
    delay(10);
    Serial.setDebugOutput(true);
    Serial.println(ESP.getFreeHeap());
    Serial.println();
    Serial.println("START jumpwireIo::setup()");

  }
  ws_connect();
  pingtimer = millis();
  if(debug){
    Serial.println("END   jumpwireIo::setup()");
    Serial.println();
  }
}
Beispiel #4
0
int main()
{
    ws_server_t server;
    
    ws_listen(&server, uv_default_loop(), 8090, malloc_connect, NULL);
    
    //uv_run(uv_default_loop(), UV_RUN_DEFAULT);
    
    ws_connect_t connect;
    
    ws_init(&connect, uv_default_loop());
    
    ws_connect(&connect, "127.0.0.1", 8090, connect_cb, cb_error, NULL);
    
    printf("adfd");
    
    uv_run(uv_default_loop(), UV_RUN_DEFAULT);
    
    //ws_quit(&connect);
    
    return 0;
}
Beispiel #5
0
/*! \brief Finds a tcpconn & sends on it */
static int proto_ws_send(struct socket_info* send_sock,
											char* buf, unsigned int len,
											union sockaddr_union* to, int id)
{
	struct tcp_connection *c;
	struct timeval get;
	struct ip_addr ip;
	int port = 0;
	int fd, n;

	reset_tcp_vars(tcpthreshold);
	start_expire_timer(get,tcpthreshold);

	if (to){
		su2ip_addr(&ip, to);
		port=su_getport(to);
		n = tcp_conn_get(id, &ip, port, &c, &fd);
	}else if (id){
		n = tcp_conn_get(id, 0, 0, &c, &fd);
	}else{
		LM_CRIT("prot_tls_send called with null id & to\n");
		get_time_difference(get,tcpthreshold,tcp_timeout_con_get);
		return -1;
	}

	if (n<0) {
		/* error during conn get, return with error too */
		LM_ERR("failed to aquire connection\n");
		get_time_difference(get,tcpthreshold,tcp_timeout_con_get);
		return -1;
	}

	/* was connection found ?? */
	if (c==0) {
		if (tcp_no_new_conn) {
			return -1;
		}
		LM_DBG("no open tcp connection found, opening new one\n");
		/* create tcp connection */
		if ((c=ws_connect(send_sock, to, &fd))==0) {
			LM_ERR("connect failed\n");
			return -1;
		}
		goto send_it;
	}
	get_time_difference(get, tcpthreshold, tcp_timeout_con_get);

	/* now we have a connection, let's what we can do with it */
	/* BE CAREFUL now as we need to release the conn before exiting !!! */
	if (fd==-1) {
		/* connection is not writable because of its state */
		/* return error, nothing to do about it */
		tcp_conn_release(c, 0);
		return -1;
	}

send_it:
	LM_DBG("sending via fd %d...\n",fd);

	n = ws_req_write(c, fd, buf, len);
	stop_expire_timer(get, tcpthreshold, "WS ops",buf,(int)len,1);
	tcp_conn_set_lifetime( c, tcp_con_lifetime);

	LM_DBG("after write: c= %p n=%d fd=%d\n",c, n, fd);
	if (n<0){
		LM_ERR("failed to send\n");
		c->state=S_CONN_BAD;
		if (c->proc_id != process_no)
			close(fd);
		tcp_conn_release(c, 0);
		return -1;
	}

	/* only close the FD if not already in the context of our process
	either we just connected, or main sent us the FD */
	if (c->proc_id != process_no)
		close(fd);

	tcp_conn_release(c, 0);
	return n;
}
void WSClientParser::Connected() {
	ws_connect();
	mState = WSClientState_UnKnow;
}
Beispiel #7
0
int main(int argc, char **argv)
{
	int ret = 0;
	int i;
	ws_base_t base = NULL;
	ws_t ws = NULL;
	int echo_count = 5;
	int ssl = 0;
	char *server = "localhost";

	for (i = 1; i < argc; i++)
	{
		if (!strcmp(argv[i], "--ssl"))
		{
			ssl = 1;
		}
		else
		{
			server = argv[i];
		}
	}

	ws_set_log_cb(ws_default_log_cb);
	ws_set_log_level(-1);

	printf("Echo client\n\n");

	if (ws_global_init(&base))
	{
		fprintf(stderr, "Failed to init global state.\n");
		return -1;
	}

	if (ws_init(&ws, base))
	{
		fprintf(stderr, "Failed to init websocket state.\n");
		ret = -1;
		goto fail;
	}

	ws_set_onmsg_cb(ws, onmsg, &echo_count);
	ws_set_onconnect_cb(ws, onconnect, NULL);
	ws_set_onclose_cb(ws, onclose, NULL);

	if (ssl)
	{
		ws_set_ssl_state(ws, LIBWS_SSL_SELFSIGNED);
	}

	printf("Connect to server %s\n", server);

	if (ws_connect(ws, server, 9500, ""))
	{
		ret = -1;
		goto fail;
	}

	ws_base_service_blocking(base);

fail:
	ws_destroy(&ws);
	ws_global_destroy(&base);
	printf("Bye bye!\n");
	return ret;
}
Beispiel #8
0
int main(int argc, const char **argv) {
	/* Check uid */
	if (getuid() != 0) {
		fprintf(stderr,"Swifer must be run as root.\n");
		return 1;
	}
	/* Check config file for interface and dhcp */
	FILE *cfg;
	if ( (cfg=fopen(config,"r")) ) {
		char *line = calloc(MAX_LINE+1,sizeof(char));
		char *val = calloc(MAX_LINE+1,sizeof(char));
		while (fgets(line,MAX_LINE,cfg) != NULL) {
			if (line[0] == '#') continue;
			if (sscanf(line,"INTERFACE = %s",val))
				strncpy(ifname,val,IFNAMSIZ);
			else if (sscanf(line,"DHCP = %s",val))
				strncpy(dhcp,val,DHCPLEN);
			else if (sscanf(line,"PRE_UP = %s",val))
				hook_preup = strdup(val);
			else if (sscanf(line,"POST_UP = %s",val))
				hook_postup = strdup(val);
			else if (strncmp(line,"[NETWORKS]",10)==0)
				break;
		}
		free(line); free(val); fclose(cfg);
	}
	/* Get basic wifi info */
	we_ver = iw_get_kernel_we_version();
	skfd = iw_sockets_open();
	iw_get_basic_config(skfd,ifname,&cur);
	/* Bring up interface (eg "ip link set IFACE up") */
	struct ifreq req;
	int err;
	strncpy(req.ifr_name,ifname,IFNAMSIZ);
	if ( (err=ioctl(skfd,SIOCGIFFLAGS,&req)) ){
		int loop = 0;
		while ( (err=ioctl(skfd,SIOCGIFFLAGS,&req)) ) {
			usleep(100000);
			if (loop++ > 50) break;
		}
		if (err) {
			close(skfd);
			return 2;
		}
	}
	req.ifr_flags |= IFF_UP;
	if (ioctl(skfd,SIOCSIFFLAGS,&req)) {
		close(skfd); return 3;
	}
	/* Processes command line arguments */
	int i;
	for (i = 1; i < argc; i++) {
		if (strncmp(argv[i],"ad",2)==0) mode |= MODE_ADD;
		else if (strncmp(argv[i],"au",2)==0) mode |= MODE_AUTO;
		else if (strncmp(argv[i],"hi",2)==0) mode |= MODE_HIDDEN;
		else if (strncmp(argv[i],"an",2)==0) mode |= (MODE_ANY | MODE_AUTO);
		else if (strncmp(argv[i],"re",2)==0) mode |= (MODE_RECONNECT | MODE_AUTO);
		else if (strncmp(argv[i],"ve",2)==0) mode |= MODE_VERBOSE;
		else if (strncmp(argv[i],"wa",2)==0) mode |= MODE_WAIT;
		else if (strncmp(argv[i],"de",2)==0) {
			if (argc > i+1) remove_network(argv[i+1]);
		}
		else fprintf(stderr,"[%s] Ignoring unknown parameter: %s\n",
			argv[0],argv[i]);
	}
	if ( (mode & MODE_VERBOSE) && (mode & MODE_AUTO) ) mode &= ~MODE_VERBOSE;
	/* Scan and select network */
	iw_scan(skfd,ifname,we_ver,&context);
	wireless_scan *ws;
	if (mode & MODE_AUTO) {
		if (mode && MODE_WAIT) {
			int loops;
			/* keep trying for up to 5 minutes */
			for (loops = 0; loops < 30 && !(ws=get_best()); ++loops)
				sleep(TIMEOUT);
		}
		else ws = get_best();
	}
	else ws = show_menu();
	const char *arg[4];
	if (ws) { /* Stop any current processes then connect to "ws" */
		arg[0] = killall; arg[1] = dhcp; arg[2] = NULL;
		if (fork()==0) {
			fclose(stdout); fclose(stderr);
			execvp(arg[0],(char * const *) arg);
		}
		arg[1] = wpa_sup;
		if (fork()==0) {
			fclose(stdout); fclose(stderr);
			execvp(arg[0],(char * const *) arg);
		}
		sleep(1);
		if ( (mode & MODE_ADD) && is_known(ws) ) mode &= ~MODE_ADD;
		if (ws->b.key_flags == 2048) mode |= MODE_SECURE;
		mode_t pre = umask(S_IWGRP|S_IWOTH|S_IRGRP|S_IROTH);
		ws_connect(ws);
		umask(pre);
	}
	else if ( !(mode & MODE_RECONNECT) ) {
		fprintf(stderr,"[swifer] no suitable networks found.\n");
		return 5;
	}
	/* Keep alive to reconnect? */
	iw_sockets_close(skfd);
	if (mode & MODE_RECONNECT) {
		if (fork() == 0) {
			setsid();
			int level = THRESHOLD + 1, ret;
			char scanline[256];
			snprintf(scanline,255,"%%*[^\n]\n%%*[^\n]\n%s: %%*d %%d.",ifname);
			FILE *procw;
			while (level > THRESHOLD) {
				sleep(TIMEOUT);
				procw = fopen(PROC_NET_WIRELESS,"r");
				ret = fscanf(procw,scanline,&level);
				fclose(procw);
				if (ret != 1) level = 0;
			}
			arg[0] = argv[0]; arg[1] = re; arg[2] = an; arg[3] = NULL;
			if ( !(mode & MODE_ANY)) arg[2] = NULL;
			execvp(arg[0],(char * const *) arg);
		}
	}
	if (hook_preup) free(hook_preup);
	if (hook_postup) free(hook_postup);
	return 0;
}
Beispiel #9
0
/*! \brief Finds a tcpconn & sends on it */
static int proto_wss_send(struct socket_info* send_sock,
											char* buf, unsigned int len,
											union sockaddr_union* to, int id)
{
	struct tcp_connection *c;
	struct timeval get;
	struct ip_addr ip;
	int port = 0;
	int fd, n;
	struct ws_data* d;

	reset_tcp_vars(tcpthreshold);
	start_expire_timer(get,tcpthreshold);

	if (to){
		su2ip_addr(&ip, to);
		port=su_getport(to);
		n = tcp_conn_get(id, &ip, port, PROTO_WSS, &c, &fd);
	}else if (id){
		n = tcp_conn_get(id, 0, 0, PROTO_NONE, &c, &fd);
	}else{
		LM_CRIT("prot_tls_send called with null id & to\n");
		get_time_difference(get,tcpthreshold,tcp_timeout_con_get);
		return -1;
	}

	if (n<0) {
		/* error during conn get, return with error too */
		LM_ERR("failed to acquire connection\n");
		get_time_difference(get,tcpthreshold,tcp_timeout_con_get);
		return -1;
	}

	/* was connection found ?? */
	if (c==0) {
		if (tcp_no_new_conn) {
			return -1;
		}
		if (!to) {
			LM_ERR("Unknown destination - cannot open new tcp connection\n");
			return -1;
		}
		LM_DBG("no open tcp connection found, opening new one\n");
		/* create tcp connection */
		if ((c=ws_connect(send_sock, to, &fd))==0) {
			LM_ERR("connect failed\n");
			return -1;
		}
		goto send_it;
	}
	get_time_difference(get, tcpthreshold, tcp_timeout_con_get);

	/* now we have a connection, let's what we can do with it */
	/* BE CAREFUL now as we need to release the conn before exiting !!! */
	if (fd==-1) {
		/* connection is not writable because of its state */
		/* return error, nothing to do about it */
		tcp_conn_release(c, 0);
		return -1;
	}

send_it:
	LM_DBG("sending via fd %d...\n",fd);

	n = ws_req_write(c, fd, buf, len);
	stop_expire_timer(get, tcpthreshold, "WSS ops",buf,(int)len,1);
	tcp_conn_set_lifetime( c, tcp_con_lifetime);

	/* only here we will have all tracing data TLS + WS */
	d = c->proto_data;

	if ( (c->flags&F_CONN_ACCEPTED)==0 && d && d->dest && d->tprot ) {
		if ( d->message ) {
			send_trace_message( d->message, t_dst);
			d->message = NULL;
		}

		/* don't allow future traces for this cnection */
		d->tprot = 0;
		d->dest  = 0;
	}


	LM_DBG("after write: c= %p n=%d fd=%d\n",c, n, fd);
	if (n<0){
		LM_ERR("failed to send\n");
		c->state=S_CONN_BAD;
		if (c->proc_id != process_no)
			close(fd);
		tcp_conn_release(c, 0);
		return -1;
	}

	/* only close the FD if not already in the context of our process
	either we just connected, or main sent us the FD */
	if (c->proc_id != process_no)
		close(fd);

	/* mark the ID of the used connection (tracing purposes) */
	last_outgoing_tcp_id = c->id;

	tcp_conn_release(c, 0);
	return n;
}