static void active_connect(char *ip) { int port = g_config.data_port; int fd = createsocket(ip, port); if (fd < 0) { LOG(pfs_data_log, LOG_ERROR, "connect %s:%d err %m\n", ip, port); char val[256] = {0x0}; snprintf(val, sizeof(val), "connect %s:%d err %m\n", ip, port); SetStr(PFS_STR_CONNECT_E, val); return; } if (svc_initconn(fd)) { LOG(pfs_data_log, LOG_ERROR, "svc_initconn err %m\n"); close(fd); return; } add_fd_2_efd(fd); LOG(pfs_data_log, LOG_NORMAL, "connect %s:%d\n", ip, port); struct conn *curcon = &acon[fd]; pfs_cs_peer *peer = (pfs_cs_peer *) curcon->user; peer->sock_stat = IDLE; peer->mode = CON_ACTIVE; }
void server ( void ) { struct sockaddr_storage peer_addr; socklen_t peer_addrlen; ssize_t bytesread; int skt; int errno; int i; char buff[BUF_SIZE]; char host[NI_MAXHOST]; char service[NI_MAXSERV]; skt = createsocket ( NULL, PORT ); for (;/*ever*/;) { peer_addrlen = (socklen_t) sizeof (struct sockaddr_storage); memset (buff, 0, BUF_SIZE); bytesread = recvfrom (skt, buff, BUF_SIZE, 0, (struct sockaddr *) &peer_addr, &peer_addrlen); if (bytesread <= 0) continue; // Ignore bad requests /* * Get details of client sending message... */ errno = getnameinfo ((struct sockaddr *) &peer_addr, peer_addrlen, host, NI_MAXHOST, service, NI_MAXSERV, NI_NUMERICSERV); if (errno) fprintf (stderr, "getnameinfo(): %s\n", gai_strerror(errno)); else { printf ("recvd %ld bytes from %s:%s [", (long) bytesread, host, service); for (i = 0; i < 10 && i < bytesread; ++i) putchar (buff[i]); printf ("]\n"); } /* * For the sake of something to do, we just echo the message back * * - notice that, as we have all the peer's details from the incoming * message, we can simply use sendto( ) * * - the client could have used this instead of using connect( ) * followed by write( ), sendto( ) essentially does both in one call */ if (sendto(skt, buff, bytesread, 0, (struct sockaddr *) &peer_addr, peer_addrlen) != bytesread) fprintf (stderr, "Couldn't echo message\n"); } }
int addtranslate(int usern, char *totranslate, char *from, char *dest, int direction, char *lang, char *command) { struct translatet *lkm; int lastuid; int cnt=0; char buf[200]; pcontext; if(translate==NULL) { translate=(struct translatet *)pmalloc(sizeof(struct translatet)); lkm=translate; lastuid=0; } else { lkm=translate; lastuid=translate->uid; while(lkm->next!=NULL) { lastuid=lkm->next->uid; lkm=lkm->next; cnt++; } lkm->next=(struct translatet *)pmalloc(sizeof(struct translatet)); lkm=lkm->next; } if(cnt>MAXSYNTRANS) return -1; lastuid++; lkm->uid=lastuid; lkm->delayed=30; /* before it times out, doubled, altavista got slow */ lkm->translatetext=(char *)pmalloc(strlen(totranslate)+1); strcpy(lkm->translatetext,totranslate); if(direction==TR_TO) { ap_snprintf(buf,sizeof(buf),lngtxt(861),command,dest); } else { if(strchr("&!#+",*dest)!=NULL) ap_snprintf(buf,sizeof(buf),lngtxt(862),from,command,dest); else ap_snprintf(buf,sizeof(buf),lngtxt(863),from,command,user(usern)->nick); } lkm->translatedtext=(char *)pmalloc(strlen(buf)+1); strcpy(lkm->translatedtext,buf); lkm->dest=(char *)pmalloc(strlen(dest)+1); strcpy(lkm->dest,dest); lkm->source=(char *)pmalloc(strlen(from)+1); strcpy(lkm->source,from); lkm->lang=(char *)pmalloc(strlen(lang)+1); strcpy(lkm->lang,lang); lkm->direction=direction; lkm->usern=usern; lkm->sock=createsocket(0,ST_CONNECT,lastuid,SGR_NONE,NULL,translateconnected,translatederror,translatedpart1,translatedone,NULL,AF_INET,SSL_OFF); lkm->sock=connectto(lkm->sock,lngtxt(864),80,NULL); return 0x0; }
void init() { createsocket(); std::vector<unsigned int> keycodes; for(unsigned int i = FIRST_KEY; i <= LAST_KEY; ++i) { keycodes.push_back(i); } out = new UinputDevice("/dev/uinput", BUS_USB, "TournamentHubKB", 1, 1, 1, { { EV_KEY, keycodes } }); }
/*sends udp datagrams, prints replies*/ void client (char * servername) { ssize_t bytes; int skt, retval; char buff[BUF_SIZE]; unsigned long sqnum, nsqnum; struct timeval tv; fd_set rfds; //read file descriptor set. skt = createsocket ( servername, PORT ); for(sqnum=0;/*ever*/;sqnum++){ if(sqnum==50) sqnum=0; //clear and prepare file descriptor sets FD_CLR(skt ,&rfds); FD_SET(skt, &rfds); //set up timer tv.tv_sec=1; tv.tv_usec=0; //(re)initialize retaval retval=0; //conv from host network sequence num. nsqnum = htonl(sqnum); bytes = (ssize_t) sizeof(nsqnum); //printf("%lu\n",sqnum); retval = select(skt+1, &rfds, NULL, NULL, &tv); //printf("retval: %d\n",retval); if(retval==-1){ perror("select\n"); exit(EXIT_FAILURE); }else if(retval==0){ if(write (skt, &nsqnum, bytes) < bytes) fprintf (stderr, "WARNING: write didn't accept complete message\n"); }else{ if(FD_ISSET(skt, &rfds)){ memset (buff, 0, BUF_SIZE); bytes = read (skt, buff, BUF_SIZE); if (bytes < 0) { perror ("read()"); exit (1); } } printf ("Server echoed the following: %s\n", buff); select(0, NULL, NULL, NULL, &tv); } } }
int connectlink(int nlink) { int tmpsock; int proto=AF_INET; int issl=SSL_OFF; char vsl[10]; char *ho; pcontext; vsl[0]=0; if (datalink(nlink)->type!=LI_LINK) return 0x0; if (datalink(nlink)->outstate==STD_CONN) return 0x0; if (datalink(nlink)->delayed>0) { datalink(nlink)->delayed-=1; return 0x0; } #ifdef HAVE_SSL if(strstr(datalink(nlink)->host,"S=")==datalink(nlink)->host) { ho=datalink(nlink)->host+2; issl=SSL_ON; strcpy(vsl,"SSL-"); } else #endif ho=datalink(nlink)->host; datalink(nlink)->delayed=0; p_log(LOG_INFO,-1,lngtxt(561),vsl,nlink,ho,datalink(nlink)->port); /* we got a server and a port */ tmpsock=createsocket(0,ST_CONNECT,nlink,SGR_NONE,NULL,connectedlink,errorlink,checklinkdata,killedlink,linkremap,proto,issl); if (tmpsock!=0) { datalink(nlink)->outstate=STD_CONN; datalink(nlink)->outsock=tmpsock; } tmpsock=connectto(tmpsock,ho,datalink(nlink)->port,NULL); if(tmpsock!=0) return 0x1; p_log(LOG_ERROR,-1,lngtxt(562),nlink,datalink(nlink)->host,datalink(nlink)->port); datalink(nlink)->outstate=STD_NOCON; return -1; }
static int active_connect() { char *ip = myconfig_get_value("iplist_serverip"); int port = g_config.sig_port; int fd = createsocket(ip, port); if (fd < 0) { LOG(vfs_sig_log, LOG_ERROR, "connect %s:%d err %m\n", ip, port); return -1; } if (svc_initconn(fd)) { LOG(vfs_sig_log, LOG_ERROR, "svc_initconn err %m\n"); close(fd); return -1; } add_fd_2_efd(fd); LOG(vfs_sig_log, LOG_NORMAL, "fd [%d] connect %s:%d\n", fd, ip, port); return fd; }
void main(){ int sockfd; struct sockaddr_in destination; sockfd = createsocket(); create(&destination,DEST_PORT,IP); conn(sockfd,&destination); struct reply r; // struct hold replies from server printf("Please enter your email id: "); scanf("%s",u.username); printf("Please enter your password: "******"%s",u.password); start(sockfd,AUTH); //notify auth sendUser(sockfd,u); //send user credentials to server r = recieveReply(sockfd); if(r.status == SUCCESS){ //auth success printf("\n Authenticated Successfully. \n"); menu(sockfd); }else{ //auth error printf("\n Invalid Username or Password. \n"); } close(sockfd); }
main() { int sfd = createsocket(); int n,prs; char buf[50]; if(fork()>0) { while(1) { printf("group : \n"); scanf("%d",&n); sprintf(buf,"%d",n); send(sfd,buf,sizeof(buf),0); memset(buf,0,sizeof(buf)); printf("enter process \n"); scanf("%d",&prs); sprintf(buf,"%d",prs); send(sfd,buf,sizeof(buf),0); memset(buf,0,sizeof(buf)); gets(buf); memset(buf,0,sizeof(buf)); gets(buf); send(sfd,buf,sizeof(buf),0); memset(buf,0,sizeof(buf)); } } else { recv(sfd,buf,sizeof(buf),0); printf("%s\n",buf); memset(buf,0,sizeof(buf)); } }
int linkrelay(int npeer, int rootlink) { int tmpsock; struct socketnodes *lkm; char *ho; char vsl[10]; int issl=SSL_OFF; int nlink; int proto=AF_INET; pcontext; vsl[0]=0; #ifdef HAVE_SSL if(strstr(newpeer(npeer)->server,"S=")==newpeer(npeer)->server) { issl=SSL_ON; ho=newpeer(npeer)->server+2; strcpy(vsl,"SSL-"); } else #endif ho=newpeer(npeer)->server; p_log(LOG_INFO,-1,lngtxt(558), vsl, newpeer(npeer)->login, datalink(rootlink)->name, newpeer(npeer)->host, newpeer(npeer)->lnkport, ho, newpeer(npeer)->port, newpeer(npeer)->vhost); lkm=getpsocketbysock(newpeer(npeer)->insock); if(lkm!=NULL) { lkm->sock->flag=SOC_CONN; lkm->sock->param=npeer; lkm->sock->constructor=NULL; lkm->sock->constructed=NULL; lkm->sock->handler=checklinkdata; lkm->sock->errorhandler=errorrelaylink; lkm->sock->destructor=killrelaylink; } tmpsock=createsocket(0,ST_CONNECT,0,SGR_NONE,NULL,linkrelayconnected,errorrelaylink,checklinkdata,killrelaylink,linkrelayremap,proto,issl); tmpsock=connectto(tmpsock,ho,newpeer(npeer)->port,newpeer(npeer)->vhost); if (tmpsock!=0) { nlink=getnewlink(); if (nlink==0) { killsocket(tmpsock); p_log(LOG_ERROR,-1,lngtxt(559)); return -1; } datalink(nlink)->type=LI_RELAY; datalink(nlink)->instate=STD_CONN; datalink(nlink)->insock=newpeer(npeer)->insock; strmncpy(datalink(nlink)->iam,newpeer(npeer)->nick,sizeof(datalink(nlink)->iam)); strmncpy(datalink(nlink)->host,newpeer(npeer)->host,sizeof(datalink(nlink)->host)); strmncpy(datalink(nlink)->pass,newpeer(npeer)->pass,sizeof(datalink(nlink)->pass)); strmncpy(datalink(nlink)->name,newpeer(npeer)->name,sizeof(datalink(nlink)->name)); datalink(nlink)->port=datalink(nlink)->port; datalink(nlink)->outstate=STD_CONN; datalink(nlink)->outsock=tmpsock; if(lkm) lkm->sock->param=nlink; lkm=getpsocketbysock(tmpsock); if(lkm!=NULL) { lkm->sock->flag=SOC_SYN; lkm->sock->param=nlink; } clearpeer(npeer); return 0x0; } p_log(LOG_ERROR,-1,lngtxt(560),newpeer(npeer)->server,newpeer(npeer)->port); killsocket(newpeer(npeer)->insock); return -1; }
int forkinet(int port) { FILE *fp; char logbuf[256]; int status; int sock; int pipeid[2]; pid_t pid; pid_t cpid; /* process id of the child */ /* create the pipe */ if (pipe(pipeid) <0) return -1; /* make the pipe unblocking * * as we are dealing with a pipe a message is * either sent or discarded. So there is no possibility * that the task will hang - however if the inet processing * fork does not empty the pipe quickly enough, it is possible * for a record to be lost. */ if ((status=fcntl(pipeid[1],F_GETFL))==-1) return -1; status|=O_NONBLOCK; if ((status=fcntl(pipeid[1],F_SETFL,status))==-1) return -1; if ((cpid=fork()) !=0) { close(pipeid[0]); return pipeid[1]; } close(pipeid[1]); loginfo(logfname,"Child Server Process Starting"); fp=fopen(pname,"w"); fprintf(fp,"%d\n",port); fclose(fp); sprintf(logbuf,"Listening on port %d.",port); loginfo(logfname,logbuf); sprintf(logbuf,"Port number recorded in file %s.",pname); loginfo(logfname,logbuf); sprintf(logbuf,"Time of last packet recorded in file %s.",timefname); loginfo(logfname,logbuf); fp=fopen(pidname,"w"); pid=getppid(); sprintf(logbuf,"Process ID recorded in file %s.",pidname); loginfo(logfname,logbuf); sprintf(logbuf,"Parent PID %d.",(int) pid); loginfo(logfname,logbuf); fprintf(fp,"%d\n",(int) pid); pid=getpid(); sprintf(logbuf,"Child PID %d.",(int) pid); loginfo(logfname,logbuf); fprintf(fp,"%d\n",(int) pid); fclose(fp); sock=createsocket(&port); if (sock !=-1) processsocket(sock,pipeid[0]); else loginfo(logfname,"Failed to create socket"); close(sock); close(pipeid[0]); loginfo(logfname,"Child Server Process Terminating"); exit(0); return -1; }
void main(){ int sockfd,newfd; struct sockaddr_in myaddress; struct sockaddr_in clientaddress; int sin_size; sockfd = createsocket(PF_INET,SOCK_DGRAM,0); create(&myaddress,MYPORT,IP); bindsocket(sockfd,&myaddress); //create tld data struct resource records[20]; int recordslen = 0; records[recordslen++] = createresource("dns.auth.com","127.0.0.20",Q_A,3000); records[recordslen++] = createresource("athena.com","dns.auth.com",Q_NS,3000); records[recordslen++] = createresource("www.athena.com","dns.auth.com",Q_NS,3000); records[recordslen++] = createresource("mail.athena.com","dns.auth.com",Q_NS,3000); records[recordslen++] = createresource("facebook.com","dns.auth.com",Q_NS,3000); records[recordslen++] = createresource("www.facebook.com","dns.auth.com",Q_NS,3000); records[recordslen++] = createresource("mail.facebook.com","dns.auth.com",Q_NS,3000); records[recordslen++] = createresource("google.com","dns.auth.com",Q_NS,3600); records[recordslen++] = createresource("www.google.com","dns.auth.com",Q_NS,3600); records[recordslen++] = createresource("mail.google.com","dns.auth.com",Q_NS,3600); records[recordslen++] = createresource("amazon.com","dns.auth.com",Q_NS,3000); records[recordslen++] = createresource("www.amazon.com","dns.auth.com",Q_NS,3000); records[recordslen++] = createresource("mail.amazon.com","dns.auth.com",Q_NS,3000); while(1){ printf("\n\nWaiting for query.\n\n"); struct msg m; recievequery(sockfd,&m,&clientaddress); if(m.msg_type==MSG_QUERY){ printf("Query for %s recieved.\n", m.queries[0].name); struct resource found[4]; int k = search(records,recordslen,found,m.queries[0].name); if(k>0){ if(found[0].type == Q_NS){ //find dns printf("NS Record found.\n"); struct resource dns[4]; int l = search(records,recordslen,dns,found[0].value); if(k>0){ m.answers[0] = found[0]; m.answers[1] = dns[0]; sendquery(sockfd,m,MSG_REPLY,0,2,clientaddress); }else{ //no A records found for dns. Shouldn't happen normally. printf("But No A Records found.\n"); sendquery(sockfd,m,MSG_ERROR,0,0,clientaddress); } }else{ //possibly the A records printf("A Records found.\n"); int i; for (i = 0; i < k; i++) { m.answers[i] = found[i]; } sendquery(sockfd,m,MSG_REPLY,0,0,clientaddress); } }else{ //no results printf("No Records found.\n"); sendquery(sockfd,m,MSG_ERROR,0,0,clientaddress); } }else{ //error. printf("Not a query.\n"); sendquery(sockfd,m,MSG_ERROR,0,0,clientaddress); } } }
int forkinet(int port) { FILE *fp; char logbuf[256]; int sock; int pipeid[2]; pid_t cpid; /* process id of the child */ pid_t pid; /* create the pipe */ if (pipe(pipeid) <0) return -1; /* The pipe was non-blocking, but this causes a problem if the client cannot cope with the incoming data rate from multiplexed streams. if ((status=fcntl(pipeid[1],F_GETFL))==-1) return -1; status|=O_NONBLOCK; if ((status=fcntl(pipeid[1],F_SETFL,status))==-1) return -1; */ if ((cpid=fork()) !=0) { close(pipeid[0]); return pipeid[1]; } close(pipeid[1]); loginfo(logfname,"Child Server Process Starting"); sock=createsocket(&port); if (sock==-1) { loginfo(logfname,"Failed to create socket"); close(pipeid[0]); exit(0); } fp=fopen(pname,"w"); fprintf(fp,"%d\n",port); fclose(fp); sprintf(logbuf,"Listening on port %d.",port); loginfo(logfname,logbuf); sprintf(logbuf,"Port number recorded in file %s.",pname); loginfo(logfname,logbuf); sprintf(logbuf,"Time of last packet recorded in file %s.",timefname); loginfo(logfname,logbuf); fp=fopen(pidname,"w"); pid=getppid(); sprintf(logbuf,"Process ID recorded in file %s.",pidname); loginfo(logfname,logbuf); sprintf(logbuf,"Parent PID %d.",(int) pid); loginfo(logfname,logbuf); fprintf(fp,"%d\n",(int) pid); pid=getpid(); sprintf(logbuf,"Child PID %d.",(int) pid); loginfo(logfname,logbuf); fprintf(fp,"%d\n",(int) pid); fclose(fp); processsocket(sock,pipeid[0]); close(sock); close(pipeid[0]); loginfo(logfname,"Child Server Process Terminating"); exit(0); return -1; }