home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ABUSESRC.ZIP / AbuseSrc / macabuse / src / net / unix / tcpip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-20  |  4.7 KB  |  199 lines

  1. #include "tcpip.hpp"
  2. #include <ctype.h>
  3.  
  4. tcpip_protocol tcpip;
  5.  
  6. /*
  7. FILE *log_file=NULL;
  8. extern int net_start();
  9. void net_log(char *st, void *buf, long size)
  10. {
  11.     
  12.   if (!log_file) 
  13.   {
  14.     if (net_start())
  15.       log_file=fopen("client.log","wb");
  16.     else
  17.       log_file=fopen("server.log","wb");
  18.   }
  19.  
  20.  
  21.     fprintf(log_file,"%s%d - ",st,size);
  22.     int i;
  23.     for (i=0;i<size;i++) 
  24.       if (isprint(*((unsigned char *)buf+i)))
  25.         fprintf(log_file,"%c",*((unsigned char *)buf+i));
  26.       else fprintf(log_file,"~");
  27.  
  28.     fprintf(log_file," : ");
  29.  
  30.     for (i=0;i<size;i++) 
  31.     fprintf(log_file,"%02x, ",*((unsigned char *)buf+i),*((unsigned char *)buf+i));
  32.     fprintf(log_file,"\n");
  33.     fflush(log_file);
  34.  
  35. } */
  36.  
  37.  
  38. int unix_fd::read(void *buf, int size, net_address **addr) 
  39. {
  40.  
  41.   SLOW();
  42.   int tr=::read(fd,buf,size);
  43.   if (addr) *addr=NULL;
  44.   return tr;
  45. }
  46.  
  47. int unix_fd::write(void *buf, int size, net_address *addr)
  48.   SLOW();
  49.   if (addr) fprintf(stderr,"Cannot change address for this socket type\n");
  50.   return ::write(fd,buf,size); 
  51. }
  52.  
  53.  
  54. net_address *tcpip_protocol::get_local_address()
  55. {
  56.   char my_name[100];                              // now check to see if this address is 'hostname'
  57.   gethostname(my_name,100);
  58.   struct hostent *l_hn=gethostbyname(my_name);  
  59.   ip_address *a=new ip_address();
  60.   memset(&a->addr,0,sizeof(a->addr));
  61.   memcpy(&a->addr.sin_addr,*l_hn->h_addr_list,4);  
  62. }
  63.  
  64. net_address *tcpip_protocol::get_node_address(char *&server_name, int def_port, int force_port)
  65. {
  66.   char name[256],*np;
  67.   np=name;
  68.   while (*server_name && *server_name!=':' && *server_name!='/')
  69.     *(np++)=*(server_name)++;
  70.   *np=0;
  71.   if (*server_name==':')
  72.   {
  73.     server_name++;
  74.     char port[256],*p;
  75.     p=port;
  76.     while (*server_name && *server_name!='/')
  77.       *(p++)=*(server_name++);
  78.     *p=0;
  79.     int x;
  80.     if (!force_port)
  81.     {
  82.       if (sscanf(port,"%d",&x)==1) def_port=x;
  83.       else return 0;
  84.     }
  85.   }
  86.  
  87.   if (*server_name=='/') server_name++;
  88.  
  89.   hostent *hp=gethostbyname(name);
  90.   if (!hp)
  91.   { 
  92.     fprintf(stderr,"unable to locate server named '%s'\n",name);
  93.     return 0;
  94.   }
  95.   
  96.  
  97.   sockaddr_in host;
  98.   memset( (char*) &host,0, sizeof(host));
  99.   host.sin_family = AF_INET;
  100.   host.sin_port = htons(def_port);
  101.   host.sin_addr.s_addr = htonl(INADDR_ANY);
  102.   memcpy(&host.sin_addr,hp->h_addr,hp->h_length);
  103.   return new ip_address(&host);
  104. }
  105.  
  106. net_socket *tcpip_protocol::connect_to_server(net_address *addr, net_socket::socket_type sock_type)
  107. {
  108.   if (addr->protocol_type()!=net_address::IP)
  109.   {
  110.     fprintf(stderr,"Procotol type not supported in the executable\n");
  111.     return NULL;
  112.   }
  113.  
  114.   int socket_fd=socket(AF_INET,sock_type==net_socket::SOCKET_SECURE ? SOCK_STREAM : SOCK_DGRAM,0);
  115.   if (socket_fd<0) 
  116.   {
  117.     fprintf(stderr,"unable to create socket (too many open files?)\n");
  118.     return 0;
  119.   }
  120.  
  121.   int zz;
  122.   if (setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&zz,sizeof(zz))<0)
  123.   {
  124.     fprintf(stderr,"could not set socket option reuseaddr");
  125.     return 0;
  126.   }
  127.  
  128.     
  129.   if (connect(socket_fd, (struct sockaddr *) &((ip_address *)addr)->addr, sizeof( ((ip_address *)addr)->addr ))==-1)
  130.   { 
  131.     fprintf(stderr,"unable to connect\n");
  132.     close(socket_fd);
  133.     return 0;
  134.   }
  135.  
  136.   if (sock_type==net_socket::SOCKET_SECURE)
  137.     return new tcp_socket(socket_fd);
  138.   else
  139.     return new udp_socket(socket_fd);
  140. }
  141.  
  142.  
  143. net_socket *tcpip_protocol::create_listen_socket(int &port, net_socket::socket_type sock_type)
  144. {
  145.   int socket_fd=socket(AF_INET,sock_type==net_socket::SOCKET_SECURE ? SOCK_STREAM : SOCK_DGRAM,0);
  146.   if (socket_fd<0) 
  147.   {
  148.     fprintf(stderr,"unable to create socket (too many open files?)\n");
  149.     return 0;
  150.   }
  151. /*  int zz;
  152.   if (setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&zz,sizeof(zz))<0)
  153.   {
  154.     fprintf(stderr,"could not set socket option reuseaddr");
  155.     return 0;
  156.   } */
  157.  
  158.  
  159.   net_socket *s;
  160.   if (sock_type==net_socket::SOCKET_SECURE)
  161.     s=new tcp_socket(socket_fd);
  162.   else s=new udp_socket(socket_fd);
  163.   if (s->listen(port)==0)
  164.   {   
  165.     delete s;
  166.     return 0;
  167.   }
  168.  
  169.   return s;
  170. }
  171.  
  172.  
  173. tcpip_protocol::tcpip_protocol()
  174. {
  175.   FD_ZERO(&master_set);  
  176.   FD_ZERO(&master_write_set);  
  177.   FD_ZERO(&read_set);
  178.   FD_ZERO(&exception_set);
  179.   FD_ZERO(&write_set); 
  180. }
  181.  
  182.  
  183. int tcpip_protocol::select(int block)
  184. {
  185.   memcpy(&read_set,&master_set,sizeof(master_set));
  186.   memcpy(&exception_set,&master_set,sizeof(master_set));
  187.   memcpy(&write_set,&master_write_set,sizeof(master_set));
  188.   if (block)
  189.     return ::select(FD_SETSIZE,&read_set,&write_set,&exception_set,NULL);
  190.   else
  191.   {
  192.     timeval tv={0,0};
  193.     return ::select(FD_SETSIZE,&read_set,&write_set,&exception_set,&tv);
  194.   }
  195. }
  196.  
  197.  
  198.