/* Functions. */ void inetConnect() { pid_t cpid; char *argv[] = { "/usr/bin/wvdial", NULL }; while( 1 ) { LOG( "Forking a new process to execute wvdial." ); cpid = fork(); if( cpid < 0 ) { LOG( strerror( errno ) ); LOG( "Sleeping ", FORKDELAY, " seconds and retrying." ); sleep( FORKDELAY ); } else if( !cpid ) { int nullfd; LOG( "Child: Redirecting STDOUT and STDERR to /dev/null." ); if( ( nullfd = open( "/dev/null", O_WRONLY ) ) < 0 ) { LOG( strerror( errno ) ); LOG( "Child: Could not open /dev/null for writing. Skipping redirction."); } else { LOG( "Child: Redirecting STDERR." ); if( dup2( nullfd, STDERR_FILENO ) < 0 ) { LOG( strerror( errno ) ); } LOG( "Child: Redirecting STDOUT." ); if( dup2( nullfd, STDOUT_FILENO ) < 0 ) { LOG( strerror( errno ) ); } } LOG( "Child: Executing wvdial." ); execv( argv[ 0 ], argv ); LOG( strerror( errno ) ); LOG( "Child: Looping" ); } else { LOG( "Parent: Monitoring internet connection." ); sleep( CHECKDELAY ); while( isconnected() ) sleep( CHECKDELAY ); LOG( "Parent: No internet connection is found. Sending wvdial a SIGTERM." ); if( kill( cpid, SIGTERM ) < 0 ) LOG( strerror( errno ) ); waitpid( cpid, NULL, 0 ); LOG( "Parent: wvdial process died, looping." ); } sleep( CONNECTIONDELAY ); } LOG( "Program flow is not support to reach here. Exiting." ); return; }
void endsocket(void) { if(isconnected()) shutdown(sock->socket,2); closesocket(sock->socket); if(sock->buf != NULL) bufferevent_free(sock->buf); event_base_loopbreak(sock->base); _sleep(500); //wait for libevent loop to break before unloading base. event_base_free(sock->base); libevent_global_shutdown(); WSACleanup(); free(sock); }
int main() { int i,j,cnt=0; char t; scanf("%d%d",&n,&m); getchar(); for (i=1;i<=n;i++) { for (j=1;j<=m;j++) { scanf("%c",&t); if (t=='#') { mp[i][j]=true; cnt++; } } getchar(); } if (cnt<=2) { printf("-1"); return 0; } for (i=1;i<=n;i++) for (j=1;j<=m;j++) if (mp[i][j]) { mp[i][j]=false; if (!isconnected()) { printf("1"); return 0; } mp[i][j]=true; } printf("2"); return 0; }
static void genextend(graph *g, int n2, int *deg, int ne, boolean rigid, int xlb, int xub) /* extend from n2 to n2+1 */ { int x,y,d; int *xorb,xc; int nx,i,j,imin,imax,dmax; int xlbx,xubx,n; graph gx[MAXN]; int degx[MAXN]; boolean rigidx; int dneed,need,nfeet,hideg,deg1,ft[MAXN],nfrag,frag[MAXN]; #ifdef INSTRUMENT boolean haschild; haschild = FALSE; ++nodes[n2]; if (rigid) ++rigidnodes[n2]; #endif n = n1 + n2; nx = n2 + 1; dmax = deg[n-1]; d = 0; dneed = mindeg1 - maxn2 + n2; need = 0; hideg = 0; deg1 = 0; for (i = 0; i < n1; ++i) { if (deg[i] == maxdeg1) d |= xbit[i]; if (deg[i] <= dneed) need |= xbit[i]; if (deg[i] >= 2) hideg |= xbit[i]; if (deg[i] == 1) deg1 |= xbit[i]; } if (xlb < XPOPCOUNT(need)) xlb = XPOPCOUNT(need); if (xlb > xub) return; imin = xstart[xlb]; imax = xstart[xub+1]; xorb = data[n2].xorb; if (nx == maxn2) { if (footfree) { nfeet = 0; for (j = 0; j < n2; ++j) { x = xval[j] & hideg; IFLE1BITS(x) ft[nfeet++] = xval[j] & deg1; } } if (cutfree) fragments(xval,n2,frag,&nfrag); for (i = imin; i < imax; ++i) { if (!rigid && xorb[i] != i) continue; x = xset[i]; xc = xcard[i]; if ((x & d) != 0) continue; if ((need & ~x) != 0) continue; if (simple) { for (j = n2; --j >= 0;) if (x == xval[j]) break; if (j >= 0) continue; } if (maxcommon >= 0) { for (j = n2; --j >= 0;) { y = x & xval[j]; if (XPOPCOUNT(y) > maxcommon) break; } if (j >= 0) continue; } if (antichain) { for (j = 0; j < n2; ++j) if ((xval[j] & ~x) == 0) break; if (j < n2) continue; } if (footfree) { y = x & (hideg | deg1); IFLE1BITS(y) continue; for (j = 0; j < nfeet; ++j) if ((x & ft[j]) == 0) break; if (j < nfeet) continue; } if (cutfree) { y = x & (hideg | deg1); IFLE1BITS(y) continue; for (j = 0; j < nfrag; ++j) if ((x & frag[j]) == 0) break; if (j < nfrag) continue; } xval[n2] = x; if (nx == nprune) { if (curres == 0) curres = mod; #ifdef SPLITTEST --curres; ++splitcases; continue; #else if (--curres != 0) continue; #endif } if (accept2(g,n2,x,gx,deg,xc > dmax)) if (!connec || isconnected(gx,n+1)) { ADDBIG(ecount[ne+xc],1); #ifdef INSTRUMENT haschild = TRUE; #endif (*outproc)(outfile,canonise ? gcan : gx,n1,n2+1); } } } else { for (i = imin; i < imax; ++i)
int sock_connect( const char *addr) { struct sockaddr peer; struct sockaddr *ppeer; int s; #if HAVE_SELECT fd_set rdevents, wrevents, exevents; struct timeval tv; int flags; int rc; #endif bzero( &peer, sizeof (peer)); if ( (ppeer = parse_addr( addr )) == NULL ) { return -1; } memcpy( &peer, ppeer, sizeof(peer) ); s = socket( AF_INET, SOCK_STREAM, 0); if ( s < 0 ) { Perror("socket"); return -1; } #ifdef HAVE_SELECT if ( (flags = fcntl(s, F_GETFL, 0)) < 0) { Perror("fcntl(s, F_GETFL, 0)"); return -1; } if ( (flags = fcntl(s, F_SETFL, O_NONBLOCK)) < 0) { Perror("fcntl(s, F_SETFL, O_NONBLOCK)"); return -1; } if ( (rc = connect(s, &peer, sizeof (peer))) && errno != EINPROGRESS ) { #else if ( connect(s, &peer, sizeof (peer)) ) { #endif Perror("connect"); return -1; } #ifdef HAVE_SELECT if ( rc == 0 ) { if ( fcntl(s, F_SETFL, flags) < 0 ) { Perror("fcntl"); return -1; } debug("connected to %s", addr ); return s; } FD_ZERO ( &rdevents ); FD_SET ( s, &rdevents ); wrevents = exevents = rdevents; tv.tv_sec = 5; tv.tv_usec = 0; rc = select( s + 1, &rdevents, &wrevents, &exevents, &tv); if ( rc < 0 ) { Perror("select"); return -1; } else if ( rc == 0 ) { error("timeout connecting to %s", addr ); return -1; } else if ( isconnected(s, &rdevents, &wrevents, &exevents ) ) { if ( fcntl(s, F_SETFL, flags) < 0 ) { Perror("fcntl"); return -1; } debug("connected to %s", addr ); return s; } else { error("connection to %s failed", addr ); return -1; } #else debug("connected to %s", addr ); #endif return s; } int sock_listen( const char *addr ) { int s; const int on = 1; struct sockaddr_in local; struct sockaddr *plocal; bzero( &local, sizeof (local)); if ( (plocal = parse_addr(addr)) == NULL ) { return -1; } memcpy( &local, plocal, sizeof(local) ); s = socket( AF_INET, SOCK_STREAM, 0 ); if ( s < 0 ) { Perror("socket"); return -1; } if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) { Perror("setsockopt"); return -1; } if ( bind( s, (struct sockaddr *)&local, sizeof(local) )) { Perror("bind"); return -1; } if ( listen( s, 1 ) ) { Perror("listen"); return -1; } notice("listening on %s", addr ); return s; }
VARF(throttle_accel, 0, 2, 32, throttle()); VARF(throttle_decel, 0, 2, 32, throttle()); void throttle() { if(!curpeer) return; ASSERT(ENET_PEER_PACKET_THROTTLE_SCALE==32); enet_peer_throttle_configure(curpeer, throttle_interval*1000, throttle_accel, throttle_decel); } bool isconnected(bool attempt) { return curpeer || (attempt && connpeer); } ICOMMAND(isconnected, "i", (int *attempt), intret(isconnected(*attempt > 0) ? 1 : 0)); const ENetAddress *connectedpeer() { return curpeer ? &curpeer->address : NULL; } ICOMMAND(connectedip, "", (), { const ENetAddress *address = connectedpeer(); string hostname; result(address && enet_address_get_host_ip(address, hostname, sizeof(hostname)) >= 0 ? hostname : ""); }); ICOMMAND(connectedport, "", (), {
char * makegraph(char *iname) { FILE *infile; Graph *G = NULL; char inbuf[LINE],outfilename[LINE]; register char *cp; char *method; int count, lineno=0, numlevels, nerrors; int i, m=0, prefixlen; int edgeConnMeth, haux, stubsPerTrans, tsEdges, ssEdges; long seed; geo_parms parmsbuf[MAXLEVEL]; /* make sure MAXLEVEL >= 3 */ if ((infile = fopen(iname, "r")) == NULL) { sprintf(errstr, "can't open input file %s", iname); die(errstr); } /* set up output file name */ sprintf(outfilename,"%s-",iname); prefixlen = strlen(outfilename); do { fgets(inbuf, LINE - 1, infile); lineno++; method = strtok(inbuf, delim); } while (((method == NULL) || (*method == '#')) && !feof(infile)); /* skip over comments and blank lines */ if ((cp = strtok(NULL, delim))==NULL) return "missing <number of graphs>"; count = atoi(cp); if ((cp = strtok(NULL, delim))==NULL) seed = 0; else seed = atol(cp); if (strcmp(method,GEO_KW)==0) { if (cp = get_geoparms(infile,parmsbuf)) return cp; m = GEO; } else if (strcmp(method,HIER_KW)==0) { if (cp = get_hierparms(infile, &numlevels, &edgeConnMeth, &haux, parmsbuf)) return cp; m = HIER; } else if (strcmp(method,TS_KW)==0) { if (cp = get_tsparms(infile, &stubsPerTrans, &tsEdges, &ssEdges, parmsbuf)) return cp; m = TS; } else { sprintf(errstr,"Unknown generation method %s",method); return errstr; } if (seed) gb_init_rand(seed); else gb_init_rand(DEFAULT_SEED); for (i=0; i<count; i++) { sprintf(outfilename+prefixlen,"%d.gb",i); switch(m) { case GEO: do { gb_recycle(G); G = geo(0,parmsbuf); } while (G != NULL && !isconnected(G)); break; case HIER: G = geo_hier(0,numlevels,edgeConnMeth,haux,parmsbuf); break; case TS: G = transtub(0,stubsPerTrans,tsEdges,ssEdges,&parmsbuf[0], &parmsbuf[1],&parmsbuf[2]); break; default: return "This can't happen!"; } /* switch */ if (G==NULL) { sprintf(errstr,"Error creating graph %d, trouble code=%d",i, gb_trouble_code); return errstr; } nerrors = save_graph(G, outfilename); if (nerrors > 0) fprintf(stderr, "%s had %d anomalies\n", outfilename,nerrors); gb_recycle(G); } /* for */ return NULL; }
void send_data(buffer_t *data) { if(isconnected()) socketsend(data); }
Graph * geo_hier(long seed, int nlevels, /* number of levels (=size of following array) */ int edgemeth, /* method of attaching edges */ int aux, /* auxiliary parameter for edge method (threshold) */ geo_parms *pp) /* array of parameter structures, one per level */ { Graph *newG, *tG, *GG, *srcG, *dstG; long *numv; /* array of sizes of lower-level graphs */ geo_parms *curparms, workparms[MAXLEVEL]; register i,k,indx; long dst; int temp,total,lowsize,otherend,blen,level; long maxP[MAXLEVEL], maxDiam[MAXLEVEL], wt[MAXLEVEL]; Vertex *np,*vp,*up,*base; Arc *ap; char vnamestr[MAXNAMELEN]; if (seed) /* convention: zero seed means don't use */ gb_init_rand(seed); if (nlevels < 1 || nlevels > MAXLEVEL) { gb_trouble_code = bad_specs+HIER_TRBL; return NULL; } /* 1 <= nlevels <= MAXLEVEL */ /* copy the parameters so we can modify them, and caller doesn't * see the changes. */ for (level=0; level<nlevels; level++) bcopy((char *)&pp[level],&workparms[level],sizeof(geo_parms)); level = 0; gb_trouble_code = 0; tG = NULL; do { gb_recycle(tG); tG = geo(0L,workparms); } while (tG != NULL && !isconnected(tG)); if (tG==NULL) return tG; maxDiam[0] = fdiam(tG); maxP[0] = maxDiam[0]; wt[0] = 1; for (i=1; i<nlevels; i++) maxDiam[i] = -1; curparms = workparms; while (++level < nlevels) { long tdiam; curparms++; /* parameters for graphs @ next level */ /* spread out the numbers of nodes per graph at this level */ numv = (long *) calloc(tG->n,sizeof(long)); lowsize = curparms->n; randomize(numv,tG->n,curparms->n,3*tG->n); /* create a subordinate graph for each vertex in the "top" graph, * and add it into the new graph as a whole. * We construct the subgraphs all at once to ensure that each * has a unique address. */ for (i=0,vp=tG->vertices; i<tG->n; i++,vp++) { curparms->n = numv[i]; do { newG = geo(0L,curparms); if (newG==NULL) return NULL; } while (!isconnected(newG)); vp->sub = newG; tdiam = fdiam(newG); if (tdiam>maxDiam[level]) maxDiam[level] = tdiam; } /* do some calculations before "flattening" the top Graph */ total = 0; for (i=0; i<tG->n; i++) { /* translate node numbers */ temp = numv[i]; numv[i]= total; total += temp; } if (total != tG->n*lowsize) { fprintf(stderr,"bad size of new graph!\n"); fprintf(stderr,"total %d tG->n %ld lowsize %d\n",total,tG->n,lowsize); gb_trouble_code = impossible+HIER_TRBL; return NULL; } /* now create what will become the "new" top-level graph */ newG = gb_new_graph(total); if (newG==NULL) { gb_trouble_code += HIER_TRBL; return NULL; } /* resolution of the new graph */ newG->Gscale = tG->Gscale * curparms->scale; /* compute edge weights for this level */ wt[level] = maxP[level-1] + 1; maxP[level] = (maxDiam[level]*wt[level]) + (maxDiam[level-1]*maxP[level-1]); for (i=0,vp=tG->vertices; i<tG->n; i++,vp++) { strcpy(vnamestr,vp->name); /* base name for all "offspring" */ blen = strlen(vnamestr); vnamestr[blen] = '.'; GG = tG->vertices[i].sub; base = newG->vertices + numv[i]; /* start of this node's */ for (k=0,np=base,up=GG->vertices; k<GG->n; k++,np++,up++) { /* add the node's edges */ for (ap=up->arcs; ap; ap=ap->next) { otherend = ap->tip - GG->vertices; if (k < otherend) gb_new_edge(np,base+otherend,ap->len); } /* now set the new node's position */ np->xpos = tG->vertices[i].xpos * curparms->scale + up->xpos; np->ypos = tG->vertices[i].ypos * curparms->scale + up->ypos; /* give the "new" node a name by catenating top & bot names */ strcpy(vnamestr+blen+1,up->name); np->name = gb_save_string(vnamestr); } /* loop over GG's vertices */ } /* loop over top-level vertices */ /* * Now we have to transfer the top-level edges to new graph. * This is done by one of three methods: * 0: choose a random node in each subgraph * 1: attach to the smallest-degree non-leaf node in each * 2: attach to smallest-degree node * 3: attach to first node with degree less than aux */ for (i=0; i<tG->n; i++) { Vertex *srcp, *dstp; Graph *srcG, *dstG; srcG = tG->vertices[i].sub; if (srcG == NULL) { /* paranoia */ gb_trouble_code = impossible+HIER_TRBL+1; return NULL; } for (ap=tG->vertices[i].arcs; ap; ap=ap->next) { dst = ap->tip - tG->vertices; if (i > dst) /* consider each edge only ONCE */ continue; dstG = ap->tip->sub; if (dstG == NULL) { /* paranoia */ gb_trouble_code = impossible+HIER_TRBL+1; return NULL; } /* choose endpoints of the top-level edge */ switch (edgemeth) { case 0: /* choose random node in each */ srcp = srcG->vertices + gb_next_rand()%srcG->n; dstp = dstG->vertices + gb_next_rand()%dstG->n; break; case 1: /* find nonleaf node of least degree in each */ /* This causes problems with graph size < 3 */ if (srcG->n > 2) srcp = find_small_deg(srcG,NOLEAF); else srcp = find_small_deg(srcG,LEAFOK); if (dstG->n > 2) dstp = find_small_deg(dstG,NOLEAF); else dstp = find_small_deg(dstG,LEAFOK); break; case 2: /* find node of smallest degree */ srcp = find_small_deg(srcG,LEAFOK); dstp = find_small_deg(dstG,LEAFOK); break; case 3: /* first node w/degree < aux */ srcp = find_thresh_deg(srcG,aux); dstp = find_thresh_deg(dstG,aux); default: gb_trouble_code = bad_specs+HIER_TRBL; return NULL; } /* switch on edgemeth */ /* pointer arithmetic: isn't it fun? printf("Copying edge from %d to %d\n", numv[i]+(srcp - srcG->vertices), numv[dst] + (dstp - dstG->vertices)); */ if (srcp==NULL || dstp==NULL) { gb_trouble_code = impossible + HIER_TRBL+2; return NULL; } srcp = newG->vertices + numv[i] + (srcp - srcG->vertices); dstp = newG->vertices + numv[dst] + (dstp - dstG->vertices); gb_new_edge(srcp,dstp,idist(srcp,dstp)); } /* for each arc */ } /* for each vertex of top graph */ /* now make the "new" graph the "top" graph and recycle others */ for (i=0,vp=tG->vertices; i<tG->n; i++,vp++) gb_recycle(vp->sub); gb_recycle(tG); tG = newG; free(numv); } /* while more levels */ /* Finally, go back and add the policy weights, * based upon the computed max diameters * and Max Path lengths. */ for (i=0; i<tG->n; i++) for (ap=tG->vertices[i].arcs; ap; ap=ap->next) { dst = ap->tip - tG->vertices; if (i > dst) /* consider each edge only ONCE */ continue; assert(i != dst); /* no self loops */ /* i < dst: it is safe to refer to ap's mate by ap+1. */ level = edge_level(&tG->vertices[i],&tG->vertices[dst],nlevels); ap->policywt = (ap+1)->policywt = wt[level]; } /* construct the utility and id strings for the new graph. * Space constraints will restrict us to keeping about 4 levels' * worth of info. */ { char buf[ID_FIELD_SIZE+1]; register char *cp; int len, nextlen, left; strcpy(tG->util_types,GEO_UTIL); /* same for all geo graphs, */ /* defined in geo.h */ cp = tG->id; sprintf(cp,"geo_hier(%ld,%d,%d,%d,[",seed,nlevels,edgemeth,aux); len = strlen(cp); left = ID_FIELD_SIZE - len; cp += len; for (i=0; (i < nlevels) && (left > 0); i++) { nextlen = printparms(buf,&pp[i]); strncpy(cp,buf,left); left -= nextlen; cp += nextlen; } if (left > 0) { sprintf(buf,"])"); nextlen = strlen(buf); strncpy(cp,buf,left); } } return tG; } /* geo_hier() */
/**************************** SUBROUTINES ******************************/ int main() { int sfd, device_type, channel,num_of_3g; struct sockaddr_in saddr; socklen_t len; pthread_t wifi, threeg; char name[16]; init_vrms_connection(); init_logging_network_changes(); #if 0 while(1){ send_vrms_data("wlan0", 5); printf("wlan0 sent\n"); sleep(30); send_vrms_data("ppp1", 4); printf("ppp1 sent\n"); sleep(22); #if 0 send_vrms_data("ppp1", 4); printf("wlan0 sent\n"); sleep(30); send_vrms_data("ppp2", 4); printf("ppp2 sent\n"); sleep(30); #endif send_vrms_data("ppp2", 4); printf("ppp2 sent\n"); sleep(20); } #endif openlog("SeaMo:VHO", LOG_PID, LOG_DAEMON); /* Obtain a D-Bus connection handler for all D-Bus activities */ conn = (DBusConnection *) get_connection(); init_3g_connections(); /* socket creation for communication with vrms. (earlier this was done in get_current_network every time it was called which is not necessary)*/ /* Connect to the available modems */ connect_all_modems(conn); /* Check if the system is currently connected to any network */ while (!isconnected(conn)) { syslog(LOG_WARNING, "Please connect to any available network\n"); sleep(1); } /* char *active_conn[30]; get_active_conn(conn, active_conn); while (1) { syslog(LOG_INFO, "Inside while\n"); int conn_state = get_active_conn_state(conn, *active_conn); if (conn_state == 0 || conn_state == -1) //Unknown state syslog(LOG_ERR, "Not Connected to any network\n"); else if (conn_state == CONNECTED) break; else if (conn_state == ACTIVATING) continue; } */ system ("sh /usr/local/seamo/sbin/ip_rules_all.sh"); /* Create threads to receive data from pre-handoff */ if (pthread_create(&threeg, NULL, threeg_data, NULL)) { syslog(LOG_ERR, "error in server threads\n"); } if (pthread_create(&wifi, NULL, wifi_data, NULL)) { syslog(LOG_ERR, "error in server threads\n"); } read_config(); /* Get the signal when the parent process dies */ prctl(PR_SET_PDEATHSIG, SIGHUP); while (1) { wifi_avail = wifi_param(); num_of_3g = get_num_of_modems(conn); if ( num_of_3g >=1){ threeg_avail = threeg_param(); } else { threeg_avail == -1 ; } syslog(LOG_INFO, "wifi_avail: %d, threeg_avail: %d\n", wifi_avail, threeg_avail); if (wifi_avail == 1 || threeg_avail == 1) { get_current_network(&device_type, name, &channel); #ifdef DEBUG_L1 syslog(LOG_INFO, "Current network: %s %d\n", name, channel); #endif /* If currently connected to Wi-Fi then keep the network * under Observation. If the network conditions goes down * then trigger the algorithm. But in case of 3G we dont * keep the network under observation, because 3G is assumed * to be every where, the conditions of 3G will remain almost * the same, hence we look for better Wi-Fi instead of waiting * for 3G network to degrade */ sleep(1); if (device_type == WIFI) { observe_wifi(name, channel); vho_trigger(); } else if (device_type == GSM){ observe_3g(name); vho_trigger(); } } /* Check if parent process is alive */ if (getppid() == 1) kill(getpid(), SIGHUP); } closelog(); return 0; }
VARF(throttle_accel, 0, 2, 32, throttle()); VARF(throttle_decel, 0, 2, 32, throttle()); void throttle() { if(!curpeer) return; ASSERT(ENET_PEER_PACKET_THROTTLE_SCALE==32); enet_peer_throttle_configure(curpeer, throttle_interval*1000, throttle_accel, throttle_decel); } bool isconnected(bool attempt, bool local) { return curpeer || (attempt && connpeer) || (local && haslocalclients()); } ICOMMAND(isconnected, "bb", (int *attempt, int *local), intret(isconnected(*attempt > 0, *local != 0) ? 1 : 0)); const ENetAddress *connectedpeer() { return curpeer ? &curpeer->address : NULL; } ICOMMAND(connectedip, "", (), { const ENetAddress *address = connectedpeer(); string hostname; result(address && enet_address_get_host_ip(address, hostname, sizeof(hostname)) >= 0 ? hostname : ""); }); ICOMMAND(connectedport, "", (), {