void ConnectedSocket::setup() { _fd = -1; _id = 0; _parent = NULL; _pending_delete = false; connlist_add(this); }
errorcode helper_fsm_start(connlist_t *list, connlist_item_t *item) { /* declare variables */ errorcode ret; /* error check arguments */ CHECK_NOT_NULL(list,ERROR_NULL_ARG_1); CHECK_NOT_NULL(item,ERROR_NULL_ARG_2); /* do function */ /* set initial values */ item->info.port_alloc.method = COMM_PORT_ALLOC_UNKNOWN; item->info.port_alloc.method_set = FLAG_UNSET; item->info.port_alloc.ext_port = PORT_UNKNOWN; item->info.port_alloc.ext_port_set = FLAG_UNSET; item->info.peer.ip = IP_UNKNOWN; item->info.peer.port = PORT_UNKNOWN; item->info.peer.set = FLAG_UNSET; item->info.buddy.ext_ip = IP_UNKNOWN; item->info.buddy.ext_port = PORT_UNKNOWN; item->info.buddy.int_ip = IP_UNKNOWN; item->info.buddy.int_port = PORT_UNKNOWN; item->info.buddy.identifier = FLAG_UNSET; item->info.buddy.ext_port_set = FLAG_UNSET; item->info.buddy_syn.seq_num = SEQ_NUM_UNKNOWN; item->info.buddy_syn.seq_num_set = FLAG_UNSET; item->info.bday.seq_num = SEQ_NUM_UNKNOWN; item->info.bday.seq_num_set = FLAG_UNSET; item->info.bday.port = PORT_UNKNOWN; item->info.bday.port_set = FLAG_UNSET; item->info.bday.status = FLAG_UNSET; /* add info to the list */ if(FAILED(connlist_add(list,item))) { /* close the socket */ close(item->info.socks.peer); return ERROR_LIST_ADD; } DEBUG(DBG_LIST,"LIST:item Watchers: %d\n",(int)item->watchers); /* call next state */ ret = helper_fsm_hello(list,item); if (FAILED(ret)) { if (ret==ERROR_NETWORK_READ){ DEBUG(DBG_PROTOCOL,"PROTOCOL:no hello message\n"); } /* close the socket */ close(item->info.socks.peer); /* if the state fails, remove the list item */ CHECK_FAILED(connlist_forget(list,connlist_item_match,item), ERROR_LIST_REMOVE_1); return ERROR_CALLED_FUNCTION; } /* close the socket */ close(item->info.socks.peer); CHECK_FAILED(connlist_forget(list,connlist_item_match,item), ERROR_LIST_REMOVE_2); return SUCCESS; }
static int do_discover_conn(struct conn * conn,struct discovery_info * di) { rand_sleep(conf_max_discovery_interval); struct wtpinfo * wtpinfo; wtpinfo = get_wtpinfo(); // wtpinfo_print(wtpinfo); // struct timespec tstart,tcur; struct radioinfo ri; memset(&ri,0,sizeof(ri)); ri.rmac[0]=6; ri.rmac[2]=14; ri.rmac[3]=14; ri.rmac[4]=14; ri.rmac[5]=14; ri.rmac[6]=14; ri.rmac[7]=14; #ifdef WITH_CW_LOG_DEBUG char str[100]; sock_addrtostr((struct sockaddr*)&conn->addr,str,100); cw_log_debug0("Sending discovery request to %s",str); #endif int rc; do { rc = cwsend_discovery_request(conn,&ri,wtpinfo); if (rc<0){ if (errno == EINTR) continue; if (errno == EMSGSIZE){ conn->mtu-=4; cw_log_debug2("Setting mtu to %i",conn->mtu); continue; } } break; }while (rc<0); if (rc < 0 ) { char str[100]; sock_addrtostr((struct sockaddr*)&conn->addr,str,100); cw_log(LOG_ERR,"Error sending discovery request to %s: %s",str,strerror(errno)); return 0; } struct connlist * connlist; connlist = connlist_create(30); // clock_gettime(CLOCK_REALTIME,&tstart); // int tstart = time(0); int treset = 0; do { char buf[2048]; int buflen=2048; struct sockaddr_storage sa; socklen_t fromlen=sizeof(struct sockaddr_storage); rc = recvfrom(conn->sock,buf,buflen,0,(struct sockaddr*)&sa,&fromlen); if (rc<0){ if (errno==EINTR) rc=0; if (errno==EAGAIN) rc=0; if (errno==EWOULDBLOCK) rc=0; } if (rc>0) { #ifdef WITH_CW_LOG_DEBUG char str[100]; sock_addrtostr((struct sockaddr*)&sa,str,100); cw_log_debug0("Received packet from %s",str); #endif struct conn * rconn; rconn = connlist_get(connlist,(struct sockaddr*)&sa); if (!rconn){ rconn = conn_create_noq(conn->sock,(struct sockaddr*)&sa); //msg_cb,NULL,0); // rconn->pmsgarg=conn->pmsgarg; rconn->mtu = conn->mtu; rconn->seqnum=conn->seqnum; connlist_add(connlist,rconn); } conn_process_packet(rconn,(uint8_t*)buf,rc,msg_cb,di); } /* reset discovery timer after we have received the first response */ if ( di->response_count == 1 && !treset ){ tstart=time(0); treset=1; } //clock_gettime(CLOCK_REALTIME,&tcur); // printf("TTSub: %i %i\n",time(0)-tstart, conf_discovery_interval); }while(time(0)-tstart < conf_discovery_interval && rc>=0 ); if (rc <0){ char str[100]; sock_addrtostr((struct sockaddr*)&conn->addr,str,100); cw_log(LOG_ERR,"Error sendings discovery request to %s: %s",str,strerror(errno)); } connlist_destroy(connlist); return 1; }
struct wtpman * wtplist_add(struct wtpman * wtpman) { wtpman->conn->data=wtpman; return (struct wtpman*)connlist_add(connlist,wtpman->conn); }