static inline void newcore(subroutine foo, const int flags) { int fd[2]; pid_t pid; int core = nlaunched++; assert( socketpair(AF_LOCAL, SOCK_STREAM, 0, fd) != -1 && (pid = fork()) != -1 ); if( pid ) { MASTER("forked core %d pid %d pipe %d(master) -> %d(worker) seq %llx", core, pid, fd[0], fd[1], mem_seq); close(fd[1]); child[core].fd = fd[0]; child[core].seq = mem_seq; FD_SET(fd[0], &children); fdmax = IMax(fd[0], fdmax); return; } close(fd[0]); for( ; ; ) { RealType res[NCOMP]; seq_t seq = mem_seq; res[0] = res[1] = 0; foo(res, &flags); WORKER(core, "writing result(%ld)", sizeof res); writesock(fd[1], res, sizeof res); WORKER(core, "reading mem_hel(%p#%ld)", mem.h, mem.he - mem.h); if( !readsock(fd[1], mem.h, mem.he - mem.h) ) exit(0); WORKER(core, "seq %llx new %llx", seq, mem_seq); seq ^= mem_seq; if( SEQ_ANGLE(seq) ) { WORKER(core, "reading mem_angle(%p#%ld+%p#%ld)", mem.v, mem.ve - mem.v, mem.a, mem.ae - mem.a); readsock(fd[1], mem.v, mem.ve - mem.v); readsock(fd[1], mem.a, mem.ae - mem.a); restorecache_(); } if( SEQ_S(seq) ) { WORKER(core, "reading mem_s(%p#%ld)", mem.s, mem.se - mem.s); readsock(fd[1], mem.s, mem.se - mem.s); clearcache_(); } seq = mem_seq; } }
static inline void Child(cint fd, cint core) { dispatch d; while( readsock(fd, &d, sizeof d) == sizeof d ) { if( d.thissize ) { MemAlloc(d.thisptr, d.thissize); WORKER("reading This (%lu)", d.thissize); readsock(fd, d.thisptr, d.thissize); } WORKER("running %p on fd %d", d.thisptr, fd); d.worker(d.thisptr, d.thissize, core, fd); if( d.thissize ) free(d.thisptr); } }
int mid_tcp( char * ipaddr,int port,char * mgid,char *inbuf,int inlen,char *outbuf,int * outlen) { int clientfd; /*建立连接*/ clientfd = tcp_client_init(ipaddr ,(int ) port); if (clientfd < 0) { printf("tcp_client_init error!\n"); memcpy(mgid,"0001",4); return(-1); } if (writesock(clientfd,inbuf,inlen) != inlen) { printf("writesock error!\n"); memcpy(mgid,"0002",4); tcp_close(clientfd); return(-2) ; } *outlen = readsock(clientfd,outbuf); if (*outlen < 0) { printf("readsock error! return[%d]\n",*outlen); memcpy(mgid,"0003",4); tcp_close(clientfd); return(-3); } /*close tcp connect*/ memcpy(mgid,"0000",4); tcp_close(clientfd); return(0); }
static inline int readycore(RealType *result) { static int nwaiting, core; static fd_set ready; RealType res[NCOMP]; int c; if( nwaiting == 0 ) { memcpy(&ready, &children, sizeof ready); nwaiting = select(fdmax + 1, &ready, NULL, NULL, NULL); core = 0; } --nwaiting; for( ; core < ncores; ++core ) if( FD_ISSET(child[core].fd, &ready) ) break; MASTER("reading result(%ld) from core %d", sizeof res, core); readsock(child[core].fd, res, sizeof res); for( c = 0; c < NCOMP; ++c ) result[c] += res[c]; --nrunning; return core++; }
static int do_read( int conn_id ) { /* There is 'data' pending, read it. */ int len, totlen, size, count; char *p; count = get_bytes_to_read(conn_id); if(!count) { /* dna_report_error(conn_id, -1, "Connection closed by remote peer", DIM_ERROR, DIMTCPRDERR); printf("conn_id %d\n", conn_id); */ Net_conns[conn_id].read_rout( conn_id, -1, 0 ); return 0; } size = Net_conns[conn_id].size; p = Net_conns[conn_id].buffer; totlen = 0; /* count = 1; */ while( size > 0 && count > 0 ) { /* would this be better? not sure afterwards... nbytes = (size < count) ? size : count; if( (len = readsock(Net_conns[conn_id].channel, p, (size_t)nbytes, 0)) <= 0 ) */ if( (len = (int)readsock(Net_conns[conn_id].channel, p, (size_t)size, 0)) <= 0 ) { /* Connection closed by other side. */ Net_conns[conn_id].read_rout( conn_id, -1, 0 ); return 0; } else { /* printf("tcpip: read %d bytes:\n",len); printf( "buffer[0]=%d\n", vtohl((int *)p[0])); printf( "buffer[1]=%d\n", vtohl((int *)p[1])); printf( "buffer[2]=%x\n", vtohl((int *)p[2])); */ totlen += len; size -= len; p += len; } if(size) count = get_bytes_to_read(conn_id); } Net_conns[conn_id].last_used = time(NULL); Net_conns[conn_id].read_rout( conn_id, 1, totlen ); return 1; }
int ircdboost(char *host, int port, char *nick) { int sock; char buf[2048]; char *pt; printf("Step 2: Connecting to the IRC Server.\n"); sock=tcpconnect(lookup(host),port,10); if (sock==-1) { printf("Error: cant connect\n"); exit(0x0); } printf("Step 3: Connected.. sending user / join\n"); /* the star is very very important */ writesock(sock,"USER o a a :a\r\n"); snprintf(buf,sizeof(buf),"NICK %s\r\n",nick); writesock(sock,buf); snprintf(buf,sizeof(buf),"WHOIS kbnn%d\r\n",lookup(host)); writesock(sock,buf); /* this joins are needed to broadcast the user to the connected servers */ writesock(sock,"JOIN #sex\r\n"); /* yeah, right */ writesock(sock,"JOIN #showdown\r\n"); /* yeah, right */ writesock(sock,"JOIN #funfactory\r\n"); /* yeah, right */ writesock(sock,"JOIN #usa\r\n"); /* yeah, right */ writesock(sock,"JOIN #flirt.de\r\n"); /* yeah, right */ writesock(sock,"JOIN 0\r\n"); /* yeah, right */ printf("Step 4: Please press control+break to release the split.\n"); while (readsock(sock,buf,sizeof(buf)) >=0) { pt=strstr(buf,"PING"); if (pt==buf) { writesock(sock,"PONG :PPP\r\n"); } pt=strstr(buf,"ERROR"); if (pt==buf) break; printf(buf); } close(sock); }
int command(int desc, const struct cl_node *root, const struct cl_limits *limits, int options, const struct cfgstruct *copt, int timeout) { char buff[1025]; int bread, opt, retval; struct cfgstruct *cpt; retval = poll_fd(desc, timeout); switch (retval) { case 0: /* timeout */ return -2; case -1: mdprintf(desc, "ERROR\n"); logg("!Command: poll_fd failed.\n"); return -1; } while((bread = readsock(desc, buff, 1024)) == -1 && errno == EINTR); if(bread == 0) { /* Connection closed */ return -1; } if(bread < 0) { logg("!Command parser: read() failed.\n"); /* at least try to display this error message */ /* mdprintf(desc, "ERROR: Command parser: read() failed.\n"); */ return -1; } buff[bread] = 0; cli_chomp(buff); if(!strncmp(buff, CMD1, strlen(CMD1))) { /* SCAN */ if(scan(buff + strlen(CMD1) + 1, NULL, root, limits, options, copt, desc, 0) == -2) if(cfgopt(copt, "ExitOnOOM")) return COMMAND_SHUTDOWN; } else if(!strncmp(buff, CMD2, strlen(CMD2))) { /* RAWSCAN */ opt = options & ~CL_SCAN_ARCHIVE; if(scan(buff + strlen(CMD2) + 1, NULL, root, NULL, opt, copt, desc, 0) == -2) if(cfgopt(copt, "ExitOnOOM")) return COMMAND_SHUTDOWN; } else if(!strncmp(buff, CMD3, strlen(CMD3))) { /* QUIT */ return COMMAND_SHUTDOWN; } else if(!strncmp(buff, CMD4, strlen(CMD4))) { /* RELOAD */ mdprintf(desc, "RELOADING\n"); return COMMAND_RELOAD; } else if(!strncmp(buff, CMD5, strlen(CMD5))) { /* PING */ mdprintf(desc, "PONG\n"); } else if(!strncmp(buff, CMD6, strlen(CMD6))) { /* CONTSCAN */ if(scan(buff + strlen(CMD6) + 1, NULL, root, limits, options, copt, desc, 1) == -2) if(cfgopt(copt, "ExitOnOOM")) return COMMAND_SHUTDOWN; } else if(!strncmp(buff, CMD7, strlen(CMD7))) { /* VERSION */ const char *dbdir; char *path; struct cl_cvd *daily; if((cpt = cfgopt(copt, "DatabaseDirectory")) || (cpt = cfgopt(copt, "DataDirectory"))) dbdir = cpt->strarg; else dbdir = cl_retdbdir(); if(!(path = mmalloc(strlen(dbdir) + 11))) { mdprintf(desc, "Memory allocation error - SHUTDOWN forced\n"); return COMMAND_SHUTDOWN; } sprintf(path, "%s/daily.cvd", dbdir); if((daily = cl_cvdhead(path))) { time_t t = (time_t) daily->stime; pthread_mutex_lock(&ctime_mutex); mdprintf(desc, "ClamAV "VERSION"/%d/%s", daily->version, ctime(&t)); pthread_mutex_unlock(&ctime_mutex); cl_cvdfree(daily); } else { mdprintf(desc, "ClamAV "VERSION"\n"); } free(path); } else if(!strncmp(buff, CMD8, strlen(CMD8))) { /* STREAM */ if(scanstream(desc, NULL, root, limits, options, copt) == CL_EMEM) if(cfgopt(copt, "ExitOnOOM")) return COMMAND_SHUTDOWN; } else if(!strncmp(buff, CMD9, strlen(CMD9))) { /* SESSION */ return COMMAND_SESSION; } else if(!strncmp(buff, CMD10, strlen(CMD10))) { /* END */ return COMMAND_END; } else if(!strncmp(buff, CMD11, strlen(CMD11))) { /* SHUTDOWN */ return COMMAND_SHUTDOWN; } else if(!strncmp(buff, CMD12, strlen(CMD12))) { /* FD */ int fd = atoi(buff + strlen(CMD12) + 1); scanfd(fd, NULL, root, limits, options, copt, desc, 0); close(fd); /* FIXME: should we close it here? */ } else { mdprintf(desc, "UNKNOWN COMMAND\n"); } return 0; /* no error and no 'special' command executed */ }
int processsocket(int sock,int inpipe) { fd_set fdset; /* selected file descriptors */ int poll,i; struct timeval tv; sigset_t set; struct sigaction act; sigemptyset(&set); sigaddset(&set,SIGUSR1); act.sa_flags=0; act.sa_mask=set; act.sa_handler=trapreset; sigaction(SIGUSR1,&act,NULL); signal(SIGPIPE,SIG_IGN); listen(sock,5); tv.tv_sec=0; tv.tv_usec=0; poll=0; runloop=1; do { FD_ZERO(&fdset); FD_SET(inpipe,&fdset); if (poll==0) { if (pollsock(sock,NULL,&fdset) !=0) continue; } else pollsock(sock,&tv,&fdset); /* open any new connections if possible */ opensock(sock,&fdset); poll=0; /* check to see if the root server has sent any data */ if (FD_ISSET(inpipe,&fdset)) { int size; size=read(inpipe,mbuf,BUF_SIZE); if (size==0) break; /* log the time */ logtime(timefname,size); writeraw(mbuf,size); } /* send the data to the clients */ if (writesock() !=0) poll=1; /* read back any data from the clients */ readsock(&fdset,tmpbuf,BUF_SIZE); /* decode the buffers here */ } while(runloop); /* close all the clients down */ for (i=0; i<msgmax; i++) { if (client[i].sock !=0) close(client[i].sock); } close(sock); return -1; }