int main(int argc,char **argv) { user = argv[1]; if (!user) usage(); loguser = argv[2]; if (!loguser) usage(); dir = argv[3]; if (!dir) usage(); /* if (dir[0] != '/') usage(); */ tinydns = argv[4]; if (!tinydns) usage(); /* if (tinydns[0] != '/') usage(); */ myip = argv[5]; if (!myip) usage(); pw = getpwnam(loguser); if (!pw) strerr_die3x(111,FATAL,"unknown account ",loguser); init(dir,FATAL); makelog(loguser,pw->pw_uid,pw->pw_gid); makedir("env"); perm(02755); start("env/ROOT"); outs(tinydns); outs("/root\n"); finish(); perm(0644); start("env/IP"); outs(myip); outs("\n"); finish(); perm(0644); start("run"); outs("#!/bin/sh\nexec 2>&1\nexec envdir ./env sh -c '\n exec envuidgid "); outs(user); outs(" softlimit -d300000 tcpserver -vDRHl0 -x tcp.cdb -- \"$IP\" 53 "); outs(auto_home); outs("/bin/axfrdns\n'\n"); finish(); perm(0755); start("Makefile"); outs("tcp.cdb: tcp\n"); outs("\ttcprules tcp.cdb tcp.tmp < tcp\n"); finish(); perm(0644); start("tcp"); outs("# sample line: 1.2.3.4:allow,AXFR=\"heaven.af.mil/3.2.1.in-addr.arpa\"\n"); outs(":deny\n"); finish(); perm(0644); return 0; }
int main(int argc, char *argv[]){ int client, server, n; struct sockaddr_in c_addr, s_addr; socklen_t len; char recv_query[30]; if(argc < 2) { puts("Usage: ./server -p port"); exit(1); } server = socket(PF_INET, SOCK_STREAM, 0); memset(&s_addr, 0, sizeof(s_addr)); s_addr.sin_addr.s_addr = htonl(INADDR_ANY); s_addr.sin_family = AF_INET; s_addr.sin_port = htons(atoi(argv[2])); if(bind(server, (struct sockaddr*)&s_addr, sizeof(s_addr)) == -1) { puts("bind() error"); exit(1); } if(listen(server, 5) == -1) { puts("listen() error"); exit(1); } len = sizeof(c_addr); client = accept(server, (struct sockaddr*)&c_addr, &len); if(client == -1) { puts("Client connection failed"); exit(1); } while(1) { read(client, recv_query, 30); n = strlen(recv_query); makelog(recv_query); write(client, recv_query, n+1); puts(recv_query); } close(client); return 0; }
int main(int argc,char **argv) { user = argv[1]; if (!user) usage(); loguser = argv[2]; if (!loguser) usage(); dir = argv[3]; if (!dir) usage(); if (dir[0] != '/') usage(); myip = argv[4]; if (!myip) usage(); pw = getpwnam(loguser); if (!pw) strerr_die3x(111,FATAL,"unknown account ",loguser); init(dir,FATAL); makelog(loguser,pw->pw_uid,pw->pw_gid); makedir("env"); perm(02755); start("env/ROOT"); outs(dir); outs("/root\n"); finish(); perm(0644); start("env/IP"); outs(myip); outs("\n"); finish(); perm(0644); start("run"); outs("#!/bin/sh\nexec 2>&1\nexec chpst -U "); outs(user); outs(" -e ./env -d250000 "); outs(auto_home); outs("/bin/pickdns\n"); finish(); perm(0755); makedir("root"); perm(02755); start("root/data"); finish(); perm(0644); start("root/Makefile"); outs("data.cdb: data\n"); outs("\t"); outs(auto_home); outs("/bin/pickdns-data\n"); finish(); perm(0644); _exit(0); }
int main(int argc,char **argv) { user = argv[1]; if (!user) usage(); loguser = argv[2]; if (!loguser) usage(); dir = argv[3]; if (!dir) usage(); if (dir[0] != '/') usage(); myip = argv[4]; if (!myip) usage(); pw = getpwnam(loguser); if (!pw) strerr_die3x(111,FATAL,"unknown account ",loguser); init(dir,FATAL); makelog(loguser,pw->pw_uid,pw->pw_gid); makedir("env"); perm(02755); start("env/ROOT"); outs(dir); outs("/root\n"); finish(); perm(0644); start("env/IP"); outs(myip); outs("\n"); finish(); perm(0644); start("run"); outs("#!/bin/sh\nexec 2>&1\nexec envuidgid "); outs(user); outs(" envdir ./env softlimit -d250000 "); outs(auto_home); outs("/bin/walldns\n"); finish(); perm(0755); makedir("root"); perm(02755); _exit(0); }
int main(int argc, char **argv) { user = argv[1]; if (!user) usage(); loguser = argv[2]; if (!loguser) usage(); dir = argv[3]; if (!dir) usage(); if (dir[0] != '/') usage(); myip = argv[4]; if (!myip) usage(); pw = getpwnam(loguser); if (!pw) strerr_die3x(111,FATAL,"unknown account ",loguser); init(dir,FATAL); makelog(loguser,pw->pw_uid,pw->pw_gid); if (mkdir("root",0700) == -1) strerr_die2sys(111,FATAL,"unable to create directory 'root': "); start("run"); outs("#!/bin/sh\nexec 2>&1\n"); outs("IP="); outs(myip); outs("; export IP\n"); outs("ROOT="); outs(dir); outs("/root; export ROOT\n"); outs("exec envuidgid "); outs(user); outs(" \\\nsoftlimit -d250000"); outs(" \\\ntcpserver -RPHv $IP ident"); outs(" "); outs(auto_home); outs("/bin/didentd-static"); outs("\n"); finish(); perm(0755); return 0; }
//thread routine function void* proxyroutine(void* arg) { int fd; int nfds; int sockfd = (int)arg; int n=0; struct sockaddr_in cli_addr; socklen_t clilen;// return addressstructure size fd_set rfds; fd_set afds; struct timeval tv; FD_ZERO(&afds); FD_SET(sockfd,&afds); nfds = getdtablesize(); char buffer[CLIBUFF_SIZE]; //receive buffer a while(1){ tv.tv_sec = 10; tv.tv_usec = 0; monitorCache(head); memcpy(&rfds,&afds,sizeof(rfds)); if(select(nfds,&rfds,(fd_set*)0,(fd_set*)0,&tv) <= 0) die("selecot()",errno); if(FD_ISSET(sockfd,&rfds)){ int newsockfd; clilen = sizeof(cli_addr); pthread_mutex_lock(&acceptlock); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);//accept a new connection pthread_mutex_unlock(&acceptlock); if(newsockfd < 0)//if accept is blocked, then return error message die("accept()",errno); FD_SET(newsockfd,&afds); } for(fd=0; fd<nfds; ++fd){ if(fd != sockfd && FD_ISSET(fd,&rfds)){ // printf("server: got connection from %s\n",iota(cli_addr.sin_addr)); //printf("Thread '%ld' responding request..\n",pthread_self()); bzero(buffer,CLIBUFF_SIZE);// initialize buffer n = read(fd,buffer,CLIBUFF_SIZE-1);//from nesockfd read 255byte and store the buffer if(n<=0) error("can't recv");// if can't read error message //printf("Here is the message : %s\n", buffer); char* cp_buffer; char temp_init[CLIBUFF_SIZE]; bzero(temp_init,CLIBUFF_SIZE); strcpy(temp_init,buffer); //initial method url version char * init_request = strtok_r(temp_init,"\r\n",&cp_buffer); char* commandp = NULL; char* serverp = NULL; char* pathp = NULL; char * protocolp = NULL; char * portp = NULL; parse(init_request, &commandp,&serverp,&pathp,&protocolp, &portp); //initial parsed storage; int s_portno = 80; char* server_host=NULL; if(portp != NULL){ server_host = (char*)malloc(strlen(serverp)+1+strlen(portp)+strlen(pathp)+1); sprintf(server_host,"%s:%s%s",serverp,portp,pathp); s_portno = atoi(portp); }else{ server_host = (char*)malloc(strlen(serverp)+strlen(pathp)+1); sprintf(server_host,"%s%s",serverp,pathp); } char* out1 = replaceAll(cp_buffer,"keep-alive","close"); char* http_host= malloc(strlen(commandp)+1+strlen(pathp)+strlen(" HTTP/1.0")+strlen(out1)+1); sprintf(http_host,"%s %s%s%s",commandp,pathp," HTTP/1.0",out1); //printf("changed:%s\n",http_host); //printf("hostnport:%s\n",server_host); char *ip = (char*)inet_ntoa((cli_addr.sin_addr)); // send the object int transsize = 0; DATA* send_object = getByKey(head,server_host); if(send_object){ //printf("sedn:%s\n",send_object->key); char* metadata = NULL;//parse_type(pathp); char remess[512]; bzero(remess,512); sprintf(remess,"%s%s%d%s%s%s","HTTP/1.1 200 OK\r\nServer: \r\nAccept-Ranges: bytes\r\n","Content-Length: ",send_object->size,"\r\nContent-Type: ",metadata,"; charset=utf-8\r\nConnection: close\r\n\r\n"); write(fd,remess,strlen(remess)); // printf("remes%d\n",strlen(remess)); transsize += write(fd,send_object->object,send_object->size); // printf("====================cache key:%s send===================\n",send_object->key); DATA tempD = delBykey(head,server_host); add(head,tempD.key,tempD.object,tempD.size); }else{ //printf("server_host:%s\n",server_host); transsize = makeCli(s_portno, serverp,pathp,fd,server_host,http_host); } close(fd); printf("tt:%d\n",transsize); if(transsize != 0){ /***** ciritical section start ********/ pthread_mutex_lock(&mylock); makelog(getCtime(),ip,server_host,transsize); pthread_mutex_unlock(&mylock); /**** critical section stop ********/ } free(server_host); free(out1); free(http_host); FD_CLR(fd,&afds); } } } }
void #else void* #endif //WIN32 transmitdata(LPVOID data) { SOCKET fd1, fd2; struct transocket *sock; struct timeval timeset; fd_set readfd, writefd; int result, i = 0; char read_in1[MAXSIZE], send_out1[MAXSIZE]; char read_in2[MAXSIZE], send_out2[MAXSIZE]; int read1 = 0, send1 = 0; int read2 = 0, send2 = 0; size_t totalread1 = 0, totalread2 = 0; int sendcount1, sendcount2; int maxfd; struct sockaddr_in client1, client2; socklen_t structsize1, structsize2; char host1[20], host2[20]; int port1 = 0, port2 = 0; char tmpbuf[100]; sock = (struct transocket *)data; fd1 = sock->fd1; fd2 = sock->fd2; memset(host1, 0, 20); memset(host2, 0, 20); memset(tmpbuf, 0, 100); structsize1 = sizeof(struct sockaddr); structsize2 = sizeof(struct sockaddr); if (getpeername(fd1, (struct sockaddr *)&client1, &structsize1) < 0) { strcpy(host1, "fd1"); } else { // printf("[+]got, ip:%s, port:%d\r\n",inet_ntoa(client1.sin_addr),ntohs(client1.sin_port)); strcpy(host1, inet_ntoa(client1.sin_addr)); port1 = ntohs(client1.sin_port); } if (getpeername(fd2, (struct sockaddr *)&client2, &structsize2) < 0) { strcpy(host2, "fd2"); } else { // printf("[+]got, ip:%s, port:%d\r\n",inet_ntoa(client2.sin_addr),ntohs(client2.sin_port)); strcpy(host2, inet_ntoa(client2.sin_addr)); port2 = ntohs(client2.sin_port); } printf("[+] Start Transmit (%s:%d <-> %s:%d) ......\r\n", host1, port1, host2, port2); maxfd = max(fd1, fd2) + 1; memset(read_in1, 0, MAXSIZE); memset(read_in2, 0, MAXSIZE); memset(send_out1, 0, MAXSIZE); memset(send_out2, 0, MAXSIZE); timeset.tv_sec = TIMEOUT; timeset.tv_usec = 0; while (1) { FD_ZERO(&readfd); FD_ZERO(&writefd); FD_SET((UINT)fd1, &readfd); FD_SET((UINT)fd1, &writefd); FD_SET((UINT)fd2, &writefd); FD_SET((UINT)fd2, &readfd); result = select(maxfd, &readfd, &writefd, NULL, ×et); if ((result < 0) && (errno != EINTR)) { printf("[-] Select error.\r\n"); break; } else if (result == 0) { printf("[-] Socket time out.\r\n"); break; } if (FD_ISSET(fd1, &readfd)) { /* must < MAXSIZE-totalread1, otherwise send_out1 will flow */ if (totalread1 < MAXSIZE) { read1 = recv(fd1, read_in1, MAXSIZE - totalread1, 0); if ((read1 == SOCKET_ERROR) || (read1 == 0)) { printf("[-] Read fd1 data error,maybe close?\r\n"); break; } memcpy(send_out1 + totalread1, read_in1, read1); sprintf(tmpbuf, "\r\nRecv %5d bytes from %s:%d\r\n", read1, host1, port1); printf(" Recv %5d bytes %16s:%d\r\n", read1, host1, port1); makelog(tmpbuf, strlen(tmpbuf)); makelog(read_in1, read1); totalread1 += read1; memset(read_in1, 0, MAXSIZE); } } if (FD_ISSET(fd2, &writefd)) { int err = 0; sendcount1 = 0; while (totalread1 > 0) { send1 = send(fd2, send_out1 + sendcount1, totalread1, 0); if (send1 == 0)break; if ((send1 < 0) && (errno != EINTR)) { printf("[-] Send to fd2 unknow error.\r\n"); err = 1; break; } if ((send1 < 0) && (errno == ENOSPC)) break; sendcount1 += send1; totalread1 -= send1; printf(" Send %5d bytes %16s:%d\r\n", send1, host2, port2); } if (err == 1) break; if ((totalread1 > 0) && (sendcount1>0)) { /* move not sended data to start addr */ memcpy(send_out1, send_out1 + sendcount1, totalread1); memset(send_out1 + totalread1, 0, MAXSIZE - totalread1); } else memset(send_out1, 0, MAXSIZE); } if (FD_ISSET(fd2, &readfd)) { if (totalread2 < MAXSIZE) { read2 = recv(fd2, read_in2, MAXSIZE - totalread2, 0); if (read2 == 0)break; if ((read2 < 0) && (errno != EINTR)) { printf("[-] Read fd2 data error,maybe close?\r\n\r\n"); break; } memcpy(send_out2 + totalread2, read_in2, read2); sprintf(tmpbuf, "\r\nRecv %5d bytes from %s:%d\r\n", read2, host2, port2); printf(" Recv %5d bytes %16s:%d\r\n", read2, host2, port2); makelog(tmpbuf, strlen(tmpbuf)); makelog(read_in2, read2); totalread2 += read2; memset(read_in2, 0, MAXSIZE); } } if (FD_ISSET(fd1, &writefd)) { int err2 = 0; sendcount2 = 0; while (totalread2 > 0) { send2 = send(fd1, send_out2 + sendcount2, totalread2, 0); if (send2 == 0)break; if ((send2 < 0) && (errno != EINTR)) { printf("[-] Send to fd1 unknow error.\r\n"); err2 = 1; break; } if ((send2 < 0) && (errno == ENOSPC)) break; sendcount2 += send2; totalread2 -= send2; printf(" Send %5d bytes %16s:%d\r\n", send2, host1, port1); } if (err2 == 1) break; if ((totalread2 > 0) && (sendcount2 > 0)) { /* move not sended data to start addr */ memcpy(send_out2, send_out2 + sendcount2, totalread2); memset(send_out2 + totalread2, 0, MAXSIZE - totalread2); } else memset(send_out2, 0, MAXSIZE); } SLEEP(5); } CLOSESOCKET(fd1); CLOSESOCKET(fd2); if(method == 3) --connectnum; printf("\r\n[+] OK! I Closed The Two Socket.\r\n"); #ifdef WIN32 return; #else return (void*)0; #endif //WIN32 }
/************************************************************************************* * * * function main * * * *************************************************************************************/ int main(int argc, char* argv[]) { char **p; char sConnectHost[HOSTLEN], sTransmitHost[HOSTLEN]; int iConnectPort = 0, iTransmitPort = 0; char *logfile = NULL; ver(); memset(sConnectHost, 0, HOSTLEN); memset(sTransmitHost, 0, HOSTLEN); p = argv; while (*p) { if (stricmp(*p, "-log") == 0) { if (testifisvalue(*(p + 1))) { logfile = *(++p); } else { printf("[-] ERROR: Must supply logfile name.\n\n"); return 0; } p++; continue; } p++; } if (logfile != NULL) { if (NULL == (fp = fopen(logfile, "a"))) { printf("[-] ERROR: open logfile"); return 0; } char s[] = "====== Start ======\r\n"; makelog(s, sizeof(s) / sizeof(char)); } #ifdef WIN32 // Win Start Winsock. WSADATA wsadata; WSAStartup(MAKEWORD(1, 1), &wsadata); #endif //WIN32 signal(SIGINT, &getctrlc); if (argc > 2) { if (stricmp(argv[1], "-listen") == 0 && argc >= 4) { iConnectPort = atoi(argv[2]); iTransmitPort = atoi(argv[3]); method = 1; } else if (stricmp(argv[1], "-tran") == 0 && argc >= 5) { iConnectPort = atoi(argv[2]); strncpy(sTransmitHost, argv[3], HOSTLEN); iTransmitPort = atoi(argv[4]); method = 2; } else if (stricmp(argv[1], "-slave") == 0 && argc >= 6) { strncpy(sConnectHost, argv[2], HOSTLEN); iConnectPort = atoi(argv[3]); strncpy(sTransmitHost, argv[4], HOSTLEN); iTransmitPort = atoi(argv[5]); method = 3; } } switch (method) { case 1: bind2bind(iConnectPort, iTransmitPort); break; case 2: bind2conn(iConnectPort, sTransmitHost, iTransmitPort); break; case 3: conn2conn(sConnectHost, iConnectPort, sTransmitHost, iTransmitPort); break; default: usage(argv[0]); break; } if (method) { closeallfd(); } #ifdef WIN32 WSACleanup(); #endif //WIN32 return 0; }
//************************************************************************************ // // function main // //************************************************************************************ int main(int argc, char* argv[]) { char **p; char sConnectHost[HOSTLEN], sTransmitHost[HOSTLEN]; int iConnectPort=0, iTransmitPort=0; char *logfile=NULL; // ver(); memset(sConnectHost, 0, HOSTLEN); memset(sTransmitHost, 0, HOSTLEN); p=argv; while(*p) { if(strstr(*p, "-log") !=NULL) { if(testifisvalue(*(p+1))) { logfile = *(++p); } else { printf("[-] ERROR: Must supply logfile name.\r\n"); return; } p++; continue; } p++; } if(logfile !=NULL) { fp=fopen(logfile,"a"); if(fp == NULL ) { printf("[-] ERROR: open logfile"); return; } makelog("====== Start ======\r\n", 22); } if(argc > 2) { if(!strcmp(argv[1], "-listen") && argc >=4) { iConnectPort = atoi(argv[2]); iTransmitPort = atoi(argv[3]); method = 1; } else if(!strcmp(argv[1], "-tran") && argc >= 5) { iConnectPort = atoi(argv[2]); strncpy(sTransmitHost, argv[3], HOSTLEN); iTransmitPort = atoi(argv[4]); method = 2; } else if(!strcmp(argv[1], "-slave") && argc >= 6) { strncpy(sConnectHost, argv[2], HOSTLEN); iConnectPort = atoi(argv[3]); strncpy(sTransmitHost, argv[4], HOSTLEN); iTransmitPort = atoi(argv[5]); method = 3; } } switch(method) { case 1: bind2bind(iConnectPort, iTransmitPort); break; case 2: bind2conn(iConnectPort, sTransmitHost, iTransmitPort); break; case 3: conn2conn(sConnectHost, iConnectPort, sTransmitHost, iTransmitPort); break; default: usage(argv[0]); break; } if(method) { closeallfd(); } return 0; }
//************************************************************************************ // // Socket Transmit to Socket // //************************************************************************************ void *thread(void *m) { int fd1, fd2; struct transocket *sock=m; fd_set readfd,writefd; int result,i=0; char read_in1[MAXSIZE],send_out1[MAXSIZE]; char read_in2[MAXSIZE],send_out2[MAXSIZE]; int read1=0,totalread1=0,send1=0; int read2=0,totalread2=0,send2=0; int sendcount1,sendcount2; int maxfd; struct sockaddr_in client1,client2; int structsize1,structsize2; char host1[20],host2[20]; int port1=0,port2=0; char tmpbuf[100]; fd1 = sock->fd1; fd2 = sock->fd2; memset(host1,0,20); memset(host2,0,20); memset(tmpbuf,0,100); structsize1=sizeof(struct sockaddr); structsize2=sizeof(struct sockaddr); if(getpeername(fd1,(struct sockaddr *)&client1,&structsize1)<0) { strcpy(host1, "fd1"); } else { // printf("[+]got, ip:%s, port:%d\r\n",inet_ntoa(client1.sin_addr),ntohs(client1.sin_port)); strcpy(host1, (char *)inet_ntoa(client1.sin_addr)); port1=ntohs(client1.sin_port); } if(getpeername(fd2,(struct sockaddr *)&client2,&structsize2)<0) { strcpy(host2,"fd2"); } else { // printf("[+]got, ip:%s, port:%d\r\n",inet_ntoa(client2.sin_addr),ntohs(client2.sin_port)); strcpy(host2, (char *)inet_ntoa(client2.sin_addr)); port2=ntohs(client2.sin_port); } // printf("[+] Start Transmit (%s:%d <-> %s:%d) ......\r\n\n", host1, port1, host2, port2); maxfd=max(fd1,fd2)+1; memset(read_in1,0,MAXSIZE); memset(read_in2,0,MAXSIZE); memset(send_out1,0,MAXSIZE); memset(send_out2,0,MAXSIZE); while(1) { FD_ZERO(&readfd); FD_ZERO(&writefd); FD_SET(fd1, &readfd); FD_SET(fd1, &writefd); FD_SET(fd2, &writefd); FD_SET(fd2, &readfd); result=select(maxfd,&readfd,&writefd,NULL,NULL); if(result<0) { // printf("[-] Select error.\r\n"); break; } else if(result==0) { // printf("[-] Socket time out.\r\n"); break; } if(FD_ISSET(fd1, &readfd)) { /* must < MAXSIZE-totalread1, otherwise send_out1 will flow */ if(totalread1<MAXSIZE) { read1=recv(fd1, read_in1, MAXSIZE-totalread1, 0); if(read1==0) { // printf("[-] Read fd1 data error,maybe close?\r\n"); break; } memcpy(send_out1+totalread1,read_in1,read1); // sprintf(tmpbuf,"\r\nRecv %5d bytes from %s:%d\r\n", read1, host1, port1); // printf(" Recv %5d bytes %16s:%d\r\n", read1, host1, port1); makelog(tmpbuf,strlen(tmpbuf)); makelog(read_in1,read1); totalread1+=read1; memset(read_in1,0,MAXSIZE); } } if(FD_ISSET(fd2, &writefd)) { int err=0; sendcount1=0; while(totalread1>0) { send1=send(fd2, send_out1+sendcount1, totalread1, 0); if(send1==0)break; if(send1<0) { // printf("[-] Send to fd2 unknow error.\r\n"); err=1; break; } sendcount1+=send1; totalread1-=send1; // printf(" Send %5d bytes %16s:%d\r\n", send1, host2, port2); } if(err==1) break; if((totalread1>0) && (sendcount1>0)) { /* move not sended data to start addr */ memcpy(send_out1,send_out1+sendcount1,totalread1); memset(send_out1+totalread1,0,MAXSIZE-totalread1); } else memset(send_out1,0,MAXSIZE); } if(FD_ISSET(fd2, &readfd)) { if(totalread2<MAXSIZE) { read2=recv(fd2,read_in2,MAXSIZE-totalread2, 0); if(read2==0)break; if(read2<0) { // printf("[-] Read fd2 data error,maybe close?\r\n\r\n"); break; } memcpy(send_out2+totalread2,read_in2,read2); // sprintf(tmpbuf, "\r\nRecv %5d bytes from %s:%d\r\n", read2, host2, port2); // printf(" Recv %5d bytes %16s:%d\r\n", read2, host2, port2); makelog(tmpbuf,strlen(tmpbuf)); makelog(read_in2,read2); totalread2+=read2; memset(read_in2,0,MAXSIZE); } } if(FD_ISSET(fd1, &writefd)) { int err2=0; sendcount2=0; while(totalread2>0) { send2=send(fd1, send_out2+sendcount2, totalread2, 0); if(send2==0)break; if(send2<0) { // printf("[-] Send to fd1 unknow error.\r\n"); err2=1; break; } sendcount2+=send2; totalread2-=send2; // printf(" Send %5d bytes %16s:%d\r\n", send2, host1, port1); } if(err2==1) break; if((totalread2>0) && (sendcount2 > 0)) { /* move not sended data to start addr */ memcpy(send_out2, send_out2+sendcount2, totalread2); memset(send_out2+totalread2, 0, MAXSIZE-totalread2); } else memset(send_out2,0,MAXSIZE); } // Sleep(5); } close(fd1); close(fd2); // if(method == 3) // connectnum --; // printf("\r\n[+] OK! I Closed The Two Socket.\r\n"); }
int main(int argc,char **argv) { char **p; char host1[HOSTLEN],host2[HOSTLEN]; int port1=0,port2=0,method=0; int length; char *logfile=NULL; p=argv; memset(host1,0,HOSTLEN); memset(host2,0,HOSTLEN); while(*p) { if(strcmp(*p,"-v")==0) { printf("Socket data transport tool.\r\nVersion:%s\r\n",VERSION); p++; continue; } if(strcmp(*p,"-h1")==0) { if(testifisvalue(*(p+1))==1) { length=(strlen(*(p+1))>HOSTLEN-1)?HOSTLEN-1:strlen(*(p+1)); strncpy(host1,*(++p),length); } p++; continue; } if(strcmp(*p,"-h2")==0) { if(testifisvalue(*(p+1))==1) { length=(strlen(*(p+1))>HOSTLEN-1)?HOSTLEN-1:strlen(*(p+1)); strncpy(host2,*(++p),length); } p++; continue; } if(strcmp(*p,"-p1")==0) { if(testifisvalue(*(p+1))==1) port1=atoi(*(++p)); p++; continue; } if(strcmp(*p,"-p2")==0) { if(testifisvalue(*(p+1))==1) port2=atoi(*(++p)); p++; continue; } if(strcmp(*p,"-m")==0) { if(testifisvalue(*(p+1))==1) method=atoi(*(++p)); p++; continue; } if(strcmp(*p,"-log")==0) { if(testifisvalue(*(p+1))==1) logfile=*(++p); else { printf("[ERROR]:must supply logfile name\r\n"); exit(0); } p++; continue; } p++; } signal(SIGCLD,SIG_IGN); signal(SIGINT,&closeallfd); if(logfile !=NULL) { fp=fopen(logfile,"a"); if(fp == NULL ) { perror("open logfile"); exit(0); } } makelog("######################## start ################\r\n",49); switch(method) { case 0: usage(argv[0]); break; case 1: if((port1==0) || (port2==0)) { printf("[ERROR]:must supply PORT1 and PORT2.\r\n"); break; } if(strlen(host2)==0) { printf("[ERROR]:must supply HOST2.\r\n"); break; } bind2conn(port1,host2,port2); break; case 2: if((port1==0) || (port2==0)) { printf("[ERROR]:must supply PORT1 and PORT2.\r\n"); break; } bind2bind(port1,port2); break; case 3: if((port1==0) || (port2==0)) { printf("[ERROR]:must supply PORT1 and PORT2.\r\n"); break; } if(strlen(host1)==0) { printf("[ERROR]:must supply HOST1.\r\n"); break; } if(strlen(host2)==0) { printf("[ERROR]:must supply HOST2.\r\n"); break; } conn2conn(host1,port1,host2,port2); break; default: usage(argv[0]); } closeallfd(0); return 0; }
void transdata(int fd1,int fd2) { struct timeval timeset; fd_set readfd,writefd; int result,i=0; char read_in1[MAXSIZE],send_out1[MAXSIZE]; char read_in2[MAXSIZE],send_out2[MAXSIZE]; int read1=0,totalread1=0,send1=0; int read2=0,totalread2=0,send2=0; int sendcount1,sendcount2; int maxfd; struct sockaddr_in client1,client2; int structsize1,structsize2; char host1[20],host2[20]; int port1=0,port2=0; char tmpbuf1[100],tmpbuf2[100]; memset(host1,0,20); memset(host2,0,20); memset(tmpbuf1,0,100); memset(tmpbuf2,0,100); if(fp!=NULL) { structsize1=sizeof(struct sockaddr); structsize2=sizeof(struct sockaddr); if(getpeername(fd1,(struct sockaddr *)&client1,(socklen_t *)&structsize1)<0) { strcpy(host1,"fd1"); } else { printf("got,ip:%s,port:%d\r\n",inet_ntoa(client1.sin_addr),ntohs(client1.sin_port)); strcpy(host1,inet_ntoa(client1.sin_addr)); port1=ntohs(client1.sin_port); } if(getpeername(fd2,(struct sockaddr *)&client2,(socklen_t *)&structsize2)<0) { strcpy(host2,"fd2"); } else { printf("got,ip:%s,port:%d\r\n",inet_ntoa(client2.sin_addr),ntohs(client2.sin_port)); strcpy(host2,inet_ntoa(client2.sin_addr)); port2=ntohs(client2.sin_port); } sprintf(tmpbuf1,"\r\n########### read from %s:%d ####################\r\n",host1,port1); sprintf(tmpbuf2,"\r\n########### reply from %s:%d ####################\r\n",host2,port2); } maxfd=max(fd1,fd2)+1; memset(read_in1,0,MAXSIZE); memset(read_in2,0,MAXSIZE); memset(send_out1,0,MAXSIZE); memset(send_out2,0,MAXSIZE); timeset.tv_sec=TIMEOUT; timeset.tv_usec=0; FD_ZERO(&readfd); FD_ZERO(&writefd); while(1) { FD_SET(fd1,&readfd); FD_SET(fd2,&readfd); result=select(maxfd,&readfd,&writefd,NULL,×et); if((result<0) && (errno!=EINTR)) { perror("select error"); break; } else if(result==0) { printf("time out\n"); break; } if(FD_ISSET(fd1,&writefd)) { int err2=0; sendcount2=0; while(totalread2>0) { send2=write(fd1,send_out2+sendcount2,totalread2); if(send2==0)break; if((send2<0) && (errno!=EINTR)) { perror("unknow error"); err2=1; break; } if((send2<0) && (errno==ENOSPC)) break; sendcount2+=send2; totalread2-=send2; } if(err2==1) break; if((totalread2>0) && (sendcount2 > 0)) { /* 移动未发送完的数据到开始 */ memcpy(send_out2,send_out2+sendcount2,totalread2); memset(send_out2+totalread2,0,MAXSIZE-totalread2); } else { memset(send_out2,0,MAXSIZE); FD_CLR(fd1,&writefd); } } if(FD_ISSET(fd2,&writefd)) { int err=0; sendcount1=0; while(totalread1>0) { send1=write(fd2,send_out1+sendcount1,totalread1); if(send1==0)break; if((send1<0) && (errno!=EINTR)) { perror("unknow error"); err=1; break; } if((send1<0) && (errno==ENOSPC)) break; sendcount1+=send1; totalread1-=send1; } if(err==1) break; if((totalread1>0) && (sendcount1>0)) { /* 移动未发送完的数据到开始 */ memcpy(send_out1,send_out1+sendcount1,totalread1); memset(send_out1+totalread1,0,MAXSIZE-totalread1); } else { memset(send_out1,0,MAXSIZE); FD_CLR(fd2,&writefd); } } if(FD_ISSET(fd1,&readfd)) { /* 不能超过MAXSIZE-totalread1,不然send_out1会溢出 */ if(totalread1<MAXSIZE) { read1=read(fd1,read_in1,MAXSIZE-totalread1); if(read1==0) break; if((read1<0) && (errno!=EINTR)) { perror("read data error"); break; } memcpy(send_out1+totalread1,read_in1,read1); makelog(tmpbuf1,strlen(tmpbuf1)); makelog(read_in1,read1); totalread1+=read1; memset(read_in1,0,MAXSIZE); } FD_SET(fd2,&writefd); } if(FD_ISSET(fd2,&readfd)) { if(totalread2<MAXSIZE) { read2=read(fd2,read_in2,MAXSIZE-totalread2); if(read2==0)break; if((read2<0) && (errno!=EINTR)) { perror("read data error"); break; } memcpy(send_out2+totalread2,read_in2,read2); makelog(tmpbuf2,strlen(tmpbuf2)); makelog(read_in2,read2); totalread2+=read2; memset(read_in2,0,MAXSIZE); } FD_SET(fd1,&writefd); } } close(fd1); close(fd2); printf("ok,I closed the two fd\r\n"); exit(0); }