/***********************************************
 * released under (E) licensing ...            *
 *        (E) RULES AND REGULATIONS            *
 * permission to use/rewrite/add     : granted *
 * permission to trojan/steal        : denied  *
 * permission to use illegally       : denied  *
 * permission to use on /dev/urandom : denied  *
 ***********************************************/
/* contact el8@press.co.jp for full license    */
/* code copyrighted by ~el8 -- don't infringe! */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <signal.h>
#include <netinet/in.h>

#define ERRSTR "\033[1;33m!\033[m"
#define SYNSTR "\033[1;32m*\033[m"

char msg[7][3]={"how",	//0
                " no",	//1
                "w, ",	//2
                "bro",	//3
                "wn ",	//4
                "cow",	//5
                "?  "};	//6
                
#define UDP_PORT 31337
#define TCP_PORT 31338
int udpsock,tcpsock,tcpsock2;
void sig(int s)
{
   close(udpsock);
   close(tcpsock);
   close(tcpsock2);
   printf("\n%i\n",s);
   exit(1);
}
void 
main()
{
  int udpsock,tcpsock,tcpsock2;
  struct sockaddr_in udpme,udpyou;
  struct sockaddr_in tcpme,tcpyou;
  char ubuf[255],tbuf[255];
  int n=0,tcpbytes=0,udpbytes=0,utotal=0,ttotal=0;
  int verbose=0,good=0;
  int adr;
  signal(SIGINT,sig);
  signal(SIGKILL,sig);
  signal(SIGSEGV,sig);
  signal(SIGTERM,sig);
  
  if ((udpsock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
            perror("socket(SOCK_DGRAM)");
            exit(-1);
  }
  if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
            perror("socket(SOCK_STREAM)");
            exit(-1);
  }
   
  udpme.sin_family = AF_INET;
  udpme.sin_port = htons(UDP_PORT);
  udpme.sin_addr.s_addr = INADDR_ANY;
  
  tcpme.sin_family = AF_INET;
  tcpme.sin_port = htons(TCP_PORT);
  tcpme.sin_addr.s_addr = INADDR_ANY;
    
  if (bind(tcpsock, (struct sockaddr *)&tcpme, sizeof(struct sockaddr))==-1){
            perror("tcp bind");
            exit(-1);
        }
  if (bind(udpsock, (struct sockaddr *)&udpme, sizeof(struct sockaddr))==-1){
              perror("udp bind");
              exit(-1);
  }
  if (listen(tcpsock, 2) == -1) {
            perror("tcp listen");
            exit(-1);
  }

   adr=sizeof(tcpyou);
        for(;;) {  
            
            if ((tcpsock2 = accept(tcpsock, (struct sockaddr*)&tcpyou,
				   &adr)) == -1) {
                perror("tcp accept");
                continue;
            }
            bzero(&tbuf,sizeof(tbuf));
            recv(tcpsock2,tbuf,sizeof(tbuf),0);
            if (!strcmp(tbuf,"START"))
            {
            bzero(&tbuf,sizeof(tbuf));
            sprintf(tbuf,"UDP %i",UDP_PORT);
            if (send(tcpsock2,tbuf,sizeof(tbuf),0)==-1) break;
            adr=sizeof(struct sockaddr);
            fprintf(stderr,"%s [recieving]\n",inet_ntoa(tcpyou.sin_addr));
            for(;;){
               
	       udpbytes=recvfrom(udpsock, ubuf, sizeof(ubuf), 0,
		(struct sockaddr*)&udpyou, &adr);
               
	       tcpbytes=recv(tcpsock, tbuf, sizeof(tbuf), 0);
               if (udpbytes<0||tcpbytes<0) continue;
	       if((!strcmp(tbuf,"DONE"))||(!strcmp(ubuf,"DONE")))
               {
                   close(tcpsock2);
                   tcpbytes-=4;
                   break;
               }
               if((*tbuf='\0')||(*ubuf='\0'))
               {
                   close(tcpsock2);
                   break;
               }
	       n++;
               if (strcmp(tbuf,ubuf))
                   fputs(ERRSTR,stderr);
	       else {
	           fputs(SYNSTR,stderr);
	           good++;
	       }
               if (tcpbytes>0)
                   ttotal+=tcpbytes;
               if (udpbytes>0)
                   utotal+=udpbytes;
               
//	       if (tcpbytes<0 || udpbytes<0) break;
               
	       bzero(&tbuf,sizeof(tbuf));
	       bzero(&ubuf,sizeof(ubuf));
            }
   

           fprintf(stderr,"----------------------------[%s]--\n",
		   inet_ntoa(tcpyou.sin_addr));
           fprintf(stderr,"UDP:\t[%i bytes sent]\n",utotal);
           fprintf(stderr,"TCP:\t[%i bytes sent]\n",ttotal);
           fprintf(stderr,"(%i synchronized packets sent [%i bad])\n",
		   good,(n-good));
           good=0; n=0;
	   break;
	   //close(tcpsock2);
           }
           
        }
       
  close(udpsock);
  close(tcpsock);
  exit(0);
}


syntax highlighted by Code2HTML, v. 0.9.1