Changeset 2:2d5e843739a3 in roaraudio
- Timestamp:
- 06/11/08 02:41:56 (16 years ago)
- Branch:
- default
- Phase:
- public
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
include/libroar/socket.h
r0 r2 16 16 #define ROAR_SOCKET_NONBLOCK 2 17 17 18 #define ROAR_SOCKET_MAX_HOSTNAMELEN 64 19 18 20 int roar_socket_listen (int type, char * host, int port); 19 21 int roar_socket_connect (char * host, int port); … … 22 24 int roar_socket_new_unix (void); 23 25 int roar_socket_open (int mode, int type, char * host, int port); 26 int roar_socket_open_proxy (int mode, int type, char * host, int port, char * proxy_type); 24 27 25 28 int roar_socket_nonblock(int fh, int state); 29 30 int roar_socket_open_socks4a(int mode, int fh, char * host, int port); 26 31 27 32 #endif -
libroar/socket.c
r0 r2 52 52 53 53 int roar_socket_connect (char * host, int port) { 54 return roar_socket_open(MODE_CONNECT, ROAR_SOCKET_TYPE_UNKNOWN, host, port); 54 char * proxy_type = getenv("ROAR_PROXY"); 55 56 if ( proxy_type == NULL || strcmp(proxy_type, "") == 0 ) { 57 return roar_socket_open(MODE_CONNECT, ROAR_SOCKET_TYPE_UNKNOWN, host, port); 58 } else { 59 return roar_socket_open_proxy(MODE_CONNECT, ROAR_SOCKET_TYPE_UNKNOWN, host, port, proxy_type); 60 } 55 61 } 56 62 … … 117 123 } 118 124 125 // --- [ PROXY CODE ] --- 126 127 // generic proxy code: 128 129 int roar_socket_open_proxy (int mode, int type, char * host, int port, char * proxy_type) { 130 int proxy_port; 131 char proxy_host[ROAR_SOCKET_MAX_HOSTNAMELEN]; 132 char * proxy_addr; 133 int i; 134 int fh; 135 136 // TODO: change this so we support listen() proxys (ssh -R) 137 if ( mode != MODE_CONNECT ) 138 return -1; 139 140 if ( !strcmp(proxy_type, "socks4a") ) { // for TOR, the only supported type at the moment 141 proxy_addr = getenv("socks_proxy"); 142 143 proxy_port = 9050; // TOR's default port 144 145 if ( proxy_addr == NULL ) 146 return -1; 147 148 for (i = 0; proxy_addr[i] != 0 && proxy_addr[i] != ':' && i < ROAR_SOCKET_MAX_HOSTNAMELEN; i++) 149 proxy_host[i] = proxy_addr[i]; 150 proxy_host[i] = 0; 151 152 if ( i == 0 ) // no hostname found 153 return -1; 154 155 if ( proxy_addr[i] == ':' ) 156 proxy_port = atoi(&proxy_addr[i+1]); 157 158 if ( (fh = roar_socket_open(mode, type, proxy_host, proxy_port)) == -1) { 159 return -1; 160 } 161 162 if ( roar_socket_open_socks4a(mode, fh, host, port) == -1 ) { 163 close(fh); 164 return -1; 165 } 166 167 return fh; 168 } else { 169 return -1; // unknown type 170 } 171 } 172 173 // protocoll dependet proxy code: 174 175 int roar_socket_open_socks4a(int mode, int fh, char * host, int port) { 176 char buf[9]; 177 int len; 178 179 buf[0] = 0x04; 180 buf[1] = mode == MODE_CONNECT ? 0x01 : 0x02; 181 *((uint16_t*)&buf[2]) = htons(port); 182 buf[4] = 0x00; 183 buf[5] = 0x00; 184 buf[6] = 0x00; 185 buf[7] = 0x01; 186 buf[8] = 0x00; 187 188 if ( write(fh, buf, 9) != 9 ) 189 return -1; 190 191 len = strlen(host); 192 193 if ( write(fh, host, len) != len ) 194 return -1; 195 196 if ( write(fh, "\0", 1) != 1 ) 197 return -1; 198 199 if ( read(fh, buf, 8) != 8 ) 200 return -1; 201 202 if ( buf[1] != 0x5a ) 203 return -1; 204 205 return 0; 206 } 207 119 208 //ll
Note: See TracChangeset
for help on using the changeset viewer.