Changeset 829:9ed47cfb4d44 in roaraudio for libroar/socket.c


Ignore:
Timestamp:
09/25/08 04:56:33 (16 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

unifyed SOCKS4 api, have now support for SOCKS4/4a/4d

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libroar/socket.c

    r759 r829  
    594594 
    595595int roar_socket_open_proxy (int mode, int type, char * host, int port, char * proxy_type) { 
    596  int    proxy_port; 
     596 int    proxy_port = -1; 
    597597 char   proxy_host[ROAR_SOCKET_MAX_HOSTNAMELEN]; 
    598  char * proxy_addr; 
     598 char * proxy_addr = NULL; 
    599599 int    i; 
    600  int    fh; 
     600 int    fh = -1; 
    601601 
    602602 // TODO: change this so we support listen() proxys (ssh -R) 
     
    604604  return -1; 
    605605 
    606  if ( !strcmp(proxy_type, "socks4a") ) { // for TOR, the only supported type at the moment 
     606 if ( !strncmp(proxy_type, "socks", 5) ) { 
    607607  proxy_addr = getenv("socks_proxy"); 
    608608 
     
    625625   return -1; 
    626626  } 
    627  
     627 } 
     628 
     629 if ( !strcmp(proxy_type, "socks4a") ) { // for TOR, the only supported type at the moment 
    628630  if ( roar_socket_open_socks4a(mode, fh, host, port) == -1 ) { 
     631   close(fh); 
     632   return -1; 
     633  } 
     634 
     635  return fh; 
     636 } else if ( !strcmp(proxy_type, "socks4d") ) { // DECnet 
     637  if ( roar_socket_open_socks4d(mode, fh, host, port) == -1 ) { 
     638   close(fh); 
     639   return -1; 
     640  } 
     641 
     642  return fh; 
     643 } else if ( !strcmp(proxy_type, "socks4") ) { // good old SOCKS4 
     644  if ( roar_socket_open_socks4(mode, fh, host, port) == -1 ) { 
    629645   close(fh); 
    630646   return -1; 
     
    639655// protocoll dependet proxy code: 
    640656 
     657int roar_socket_open_socks4 (int mode, int fh, char * host, int port) { 
     658 struct hostent     * he; 
     659 
     660 if ( (he = gethostbyname(host)) == NULL ) { 
     661  ROAR_ERR("roar_socket_open_socks4(*): Can\'t resolve host name '%s'", host); 
     662  return -1; 
     663 } 
     664 
     665 return roar_socket_open_socks4x(mode, fh, he->h_addr, port, NULL, 0); 
     666} 
     667 
    641668int roar_socket_open_socks4a(int mode, int fh, char * host, int port) { 
     669 return roar_socket_open_socks4x(mode, fh, "\0\0\0\1", port, host, strlen(host)+1); 
     670} 
     671 
     672int roar_socket_open_socks4d(int mode, int fh, char * host, int port) { 
     673 size_t len = strlen(host)+1; 
     674 char * dp; 
     675 
     676 if ( port == 0 ) { 
     677  if ( (dp = strstr(host, "::")) == NULL ) 
     678   return -1; 
     679 
     680  len--; 
     681  *dp = 0; 
     682  memmove(dp+1, dp+2, len - (dp-host) - 1); 
     683 } 
     684 
     685 return roar_socket_open_socks4x(mode, fh, "\0\2\0\0", port, host, len); 
     686} 
     687 
     688int roar_socket_open_socks4x(int mode, int fh, char host[4], int port, char * app, size_t app_len) { 
    642689 char buf[9]; 
    643  int  len; 
    644690 
    645691 buf[0] = 0x04; 
    646692 buf[1] = mode == MODE_CONNECT ? 0x01 : 0x02; 
    647693 *((uint16_t*)&buf[2]) = htons(port); 
    648  buf[4] = 0x00; 
    649  buf[5] = 0x00; 
    650  buf[6] = 0x00; 
    651  buf[7] = 0x01; 
     694 memcpy(buf+4, host, 4); 
    652695 buf[8] = 0x00; 
    653696 
     
    655698  return -1; 
    656699 
    657  len = strlen(host); 
    658  
    659  if ( write(fh, host, len) != len ) 
    660   return -1; 
    661  
    662  if ( write(fh, "\0", 1) != 1 ) 
    663   return -1; 
     700 if ( app_len > 0 ) 
     701  if ( write(fh, app, app_len) != app_len ) 
     702   return -1; 
    664703 
    665704 if ( read(fh, buf, 8) != 8 ) 
Note: See TracChangeset for help on using the changeset viewer.