Advertisement
Guest User

Untitled

a guest
Jan 14th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.24 KB | None | 0 0
  1. /*
  2. Copyright (c) 2012 Joji Antony
  3. All right reserved
  4. Licensce Affero GPL v3
  5. */
  6. #include "tcp.h"
  7.  
  8. int send_data(int conn,void *buff,size_t len)
  9. {
  10.   char *scan;
  11.   int sent = 0;
  12.   for(scan=(char *)buff;len!=0;scan=scan+sent,len-=sent) {
  13.     if( (sent = send(conn,scan,len,0)) < 0 ) {
  14.       fprintf(stderr,"Cannot send data over network\n");
  15.       return ERR_SEND;
  16.     }
  17.   }
  18.   return 0;
  19. }
  20. static int recv_line(int sock,void *buff,int max)
  21. {
  22.   int recv_count = 0,ret_stat;
  23.     char *scan=buff;
  24.     while(1) {
  25.         if( (ret_stat = recv(sock,scan,1,0)) > 0 ) {
  26.             recv_count++;
  27.               if(recv_count == max)
  28.                 return recv_count;
  29.               if( scan[0] == '\r' ) {
  30.                 /*
  31.                    Possible end of string. Get the next character and check if its '\n'
  32.                 */
  33.                 scan++;
  34.                 if( (ret_stat = recv(sock,scan,1,0)) > 0 ) {
  35.                   recv_count++;
  36.                   if(recv_count == max)
  37.                 return recv_count;
  38.                   if( scan[0] == '\n' )
  39.                 return recv_count;
  40.                 }
  41.                 else
  42.                   return ret_stat;           
  43.               }
  44.               scan++;
  45.         }
  46.         else
  47.           return ret_stat;
  48.     }
  49. }
  50. char *recv_data(int sock)
  51. {
  52.   int count_recv,buff_len=INIT_BUFF_LEN;
  53.   char temp_recv[INCR_BUFF];
  54.   char *buff,*temp,*end=NULL;
  55.   if( sock <= 0 ) {
  56.     fprintf(stderr,"Incorrect socket passed\n");
  57.     return NULL;
  58.   }
  59.   if( (buff = malloc( buff_len * sizeof(char) )) == NULL ) {
  60.     fprintf(stderr,"Out of memmory\n");
  61.     return NULL;
  62.   }
  63.   if( (count_recv = (recv_line(sock,buff,(INIT_BUFF_LEN-1)))) <= 0 ) {
  64.     if(count_recv = 0) {
  65.       fprintf(stderr,"Connection to server lost\n");
  66.       free(buff);
  67.       return NULL;
  68.     }
  69.     if(count_recv < 0) {
  70.       fprintf(stderr,"Error in connection\n");
  71.       free(buff);
  72.       return NULL;
  73.     }
  74.   }
  75.   if( (end = strstr(buff,"\r\n")) != NULL ) {
  76.     end[2]='\0';
  77.     return buff;
  78.   }
  79.   while(end == NULL) {
  80.     if( (temp = malloc( buff_len + INCR_BUFF )) == NULL ) {
  81.       fprintf(stderr,"Out of memmory\n");
  82.       free(buff);
  83.       return NULL;
  84.     }
  85.     memset(temp,0,buff_len+INCR_BUFF);
  86.     memcpy(temp,buff,((buff_len-1) * sizeof(char)));
  87.     free(buff);
  88.     buff=temp;
  89.     if( (count_recv = (recv_line(sock,temp_recv,INCR_BUFF))) <= 0 ) {
  90.       if(count_recv = 0) {
  91.     fprintf(stderr,"Connection to server lost\n");
  92.     free(buff);
  93.     return NULL;
  94.       }
  95.       if(count_recv < 0) {
  96.     fprintf(stderr,"Error in connection\n");
  97.     free(buff);
  98.     return NULL;
  99.       }
  100.     }
  101.     memcpy(buff + buff_len - 1,temp_recv,count_recv * sizeof(char));
  102.     buff_len += INCR_BUFF;
  103.     end=strstr(buff,"\r\n");
  104.   }
  105.   end[2]='\0';
  106.   return buff;
  107. }
  108.  
  109. int connect_to_server(char *host,char *port)
  110. {
  111.   int conn;
  112.   struct addrinfo hints, *res,*scan;
  113.   memset(&hints, 0, sizeof(hints));
  114.   hints.ai_family = AF_INET;
  115.   hints.ai_socktype = SOCK_STREAM;
  116.  
  117.   if( getaddrinfo(host, port, &hints, &res) != 0 ) {
  118.     fprintf(stderr,"Failed to lookup name : %s\n",host);
  119.     return ERR_LOOKUP;
  120.   }
  121.  
  122.   for(scan=res;scan!=NULL;scan=scan->ai_next) {
  123.     if( (conn = socket(scan->ai_family, scan->ai_socktype, scan->ai_protocol)) > 0 ) {
  124.       if( (connect(conn, scan->ai_addr, scan->ai_addrlen)) == 0 ) {
  125.           freeaddrinfo(res);
  126.           return conn;
  127.       }
  128.     }
  129.   }
  130.   freeaddrinfo(res);
  131.   fprintf(stderr,"Failed to connect to server at %s:%s\n",host,port);
  132.   return ERR_CONN;
  133. }
  134. int setup_server(char *port,int backlog) {
  135.   int lis_sock;
  136.   struct addrinfo hints,*scan,*res;
  137.   memset(&hints, 0, sizeof(hints));
  138.   hints.ai_family = AF_INET;
  139.   hints.ai_socktype = SOCK_STREAM;
  140.   hints.ai_flags = AI_PASSIVE;
  141.   if( getaddrinfo(NULL, port, &hints, &res) != 0 ) {
  142.     fprintf(stderr,"Failed to lookup the local address");
  143.     return ERR_LOOKUP;
  144.   }
  145.  
  146.   for(scan=res;scan!=NULL;scan=scan->ai_next) {
  147.     if( (lis_sock = socket(scan->ai_family, scan->ai_socktype, scan->ai_protocol)) < 0  ) {
  148.       fprintf(stderr,"Failed to setup server\n");
  149.       return ERR_SERV;
  150.     }
  151.     if( bind(lis_sock,scan->ai_addr,scan->ai_addrlen) != 0 ) {
  152.       fprintf(stderr,"Failed to setup server\n");
  153.       return ERR_SERV;
  154.     }
  155.     if( listen(lis_sock,backlog) != 0 ) {
  156.       fprintf(stderr,"Failed to setup server\n");
  157.       return ERR_SERV;
  158.     }
  159.     return lis_sock;
  160.   }
  161.   fprintf(stderr,"No suitable address\n");
  162.   return ERR_SERV;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement