[b7d3cc34] | 1 | /* |
---|
| 2 | * gaim |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> |
---|
| 5 | * Copyright (C) 2002-2004, Wilmer van der Gaast, Jelmer Vernooij |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or modify |
---|
| 8 | * it under the terms of the GNU General Public License as published by |
---|
| 9 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | * (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | #define BITLBEE_CORE |
---|
| 24 | #include <stdio.h> |
---|
| 25 | #include <stdlib.h> |
---|
| 26 | #include <string.h> |
---|
| 27 | #include <sys/types.h> |
---|
| 28 | #ifndef _WIN32 |
---|
| 29 | #include <sys/socket.h> |
---|
| 30 | #include <netdb.h> |
---|
| 31 | #include <netinet/in.h> |
---|
| 32 | #include <arpa/inet.h> |
---|
| 33 | #include <unistd.h> |
---|
| 34 | #else |
---|
| 35 | #include "sock.h" |
---|
| 36 | #define ETIMEDOUT WSAETIMEDOUT |
---|
| 37 | #define EINPROGRESS WSAEINPROGRESS |
---|
| 38 | #endif |
---|
| 39 | #include <fcntl.h> |
---|
| 40 | #include <errno.h> |
---|
| 41 | #include "nogaim.h" |
---|
| 42 | #include "proxy.h" |
---|
[7ed3199] | 43 | #include "base64.h" |
---|
[b7d3cc34] | 44 | |
---|
| 45 | char proxyhost[128] = ""; |
---|
| 46 | int proxyport = 0; |
---|
| 47 | int proxytype = PROXY_NONE; |
---|
| 48 | char proxyuser[128] = ""; |
---|
| 49 | char proxypass[128] = ""; |
---|
| 50 | |
---|
[762d96f] | 51 | /* Some systems don't know this one. It's not essential, so set it to 0 then. */ |
---|
| 52 | #ifndef AI_NUMERICSERV |
---|
| 53 | #define AI_NUMERICSERV 0 |
---|
| 54 | #endif |
---|
| 55 | |
---|
[b7d3cc34] | 56 | struct PHB { |
---|
[ba9edaa] | 57 | b_event_handler func, proxy_func; |
---|
[b7d3cc34] | 58 | gpointer data, proxy_data; |
---|
| 59 | char *host; |
---|
| 60 | int port; |
---|
| 61 | int fd; |
---|
| 62 | gint inpa; |
---|
[762d96f] | 63 | struct addrinfo *gai, *gai_cur; |
---|
[b7d3cc34] | 64 | }; |
---|
| 65 | |
---|
[762d96f] | 66 | static int proxy_connect_none(const char *host, unsigned short port_, struct PHB *phb); |
---|
| 67 | |
---|
[ba9edaa] | 68 | static gboolean gaim_io_connected(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 69 | { |
---|
| 70 | struct PHB *phb = data; |
---|
| 71 | unsigned int len; |
---|
| 72 | int error = ETIMEDOUT; |
---|
| 73 | len = sizeof(error); |
---|
| 74 | |
---|
| 75 | #ifndef _WIN32 |
---|
[762d96f] | 76 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) { |
---|
| 77 | if ((phb->gai_cur = phb->gai_cur->ai_next)) { |
---|
| 78 | int new_fd; |
---|
| 79 | b_event_remove(phb->inpa); |
---|
| 80 | if ((new_fd = proxy_connect_none(NULL, 0, phb))) { |
---|
| 81 | b_event_remove(phb->inpa); |
---|
| 82 | closesocket(source); |
---|
| 83 | dup2(new_fd, source); |
---|
| 84 | phb->inpa = b_input_add(source, B_EV_IO_WRITE, gaim_io_connected, phb); |
---|
| 85 | return FALSE; |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | freeaddrinfo(phb->gai); |
---|
[b7d3cc34] | 89 | closesocket(source); |
---|
[ba9edaa] | 90 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 91 | if( phb->proxy_func ) |
---|
[e046390] | 92 | phb->proxy_func(phb->proxy_data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 93 | else { |
---|
[e046390] | 94 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 95 | g_free(phb); |
---|
| 96 | } |
---|
[ba9edaa] | 97 | return FALSE; |
---|
[b7d3cc34] | 98 | } |
---|
| 99 | #endif |
---|
[762d96f] | 100 | freeaddrinfo(phb->gai); |
---|
[701acdd4] | 101 | sock_make_blocking(source); |
---|
[ba9edaa] | 102 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 103 | if( phb->proxy_func ) |
---|
[e046390] | 104 | phb->proxy_func(phb->proxy_data, source, B_EV_IO_READ); |
---|
[b7d3cc34] | 105 | else { |
---|
[e046390] | 106 | phb->func(phb->data, source, B_EV_IO_READ); |
---|
[b7d3cc34] | 107 | g_free(phb); |
---|
| 108 | } |
---|
[ba9edaa] | 109 | |
---|
| 110 | return FALSE; |
---|
[b7d3cc34] | 111 | } |
---|
| 112 | |
---|
[289bd2d] | 113 | static int proxy_connect_none(const char *host, unsigned short port_, struct PHB *phb) |
---|
[b7d3cc34] | 114 | { |
---|
[aefaac3a] | 115 | struct sockaddr_in me; |
---|
[b7d3cc34] | 116 | int fd = -1; |
---|
[762d96f] | 117 | |
---|
| 118 | if (phb->gai_cur == NULL) |
---|
[289bd2d] | 119 | { |
---|
[762d96f] | 120 | int ret; |
---|
| 121 | char port[6]; |
---|
| 122 | struct addrinfo hints; |
---|
| 123 | |
---|
| 124 | g_snprintf(port, sizeof(port), "%d", port_); |
---|
| 125 | |
---|
| 126 | memset(&hints, 0, sizeof(struct addrinfo)); |
---|
| 127 | hints.ai_family = AF_UNSPEC; |
---|
| 128 | hints.ai_socktype = SOCK_STREAM; |
---|
| 129 | hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV; |
---|
| 130 | |
---|
| 131 | if (!(ret = getaddrinfo(host, port, &hints, &phb->gai))) |
---|
| 132 | phb->gai_cur = phb->gai; |
---|
| 133 | else |
---|
| 134 | event_debug("gai(): %s\n", gai_strerror(ret)); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | for (; phb->gai_cur; phb->gai_cur = phb->gai_cur->ai_next) |
---|
| 138 | { |
---|
| 139 | if ((fd = socket(phb->gai_cur->ai_family, phb->gai_cur->ai_socktype, phb->gai_cur->ai_protocol)) < 0) { |
---|
| 140 | event_debug( "socket failed: %d\n", errno); |
---|
| 141 | continue; |
---|
| 142 | } |
---|
[289bd2d] | 143 | |
---|
[762d96f] | 144 | sock_make_nonblocking(fd); |
---|
[289bd2d] | 145 | |
---|
[762d96f] | 146 | if (global.conf->iface_out) |
---|
| 147 | { |
---|
| 148 | me.sin_family = AF_INET; |
---|
| 149 | me.sin_port = 0; |
---|
| 150 | me.sin_addr.s_addr = inet_addr( global.conf->iface_out ); |
---|
[289bd2d] | 151 | |
---|
[762d96f] | 152 | if (bind(fd, (struct sockaddr *) &me, sizeof(me)) != 0) |
---|
| 153 | event_debug("bind( %d, \"%s\" ) failure\n", fd, global.conf->iface_out); |
---|
| 154 | } |
---|
[289bd2d] | 155 | |
---|
[762d96f] | 156 | event_debug("proxy_connect_none( \"%s\", %d ) = %d\n", host, port, fd); |
---|
[19ac9c5] | 157 | |
---|
[762d96f] | 158 | if (connect(fd, phb->gai_cur->ai_addr, phb->gai_cur->ai_addrlen) < 0 && !sockerr_again()) { |
---|
| 159 | event_debug( "connect failed: %s\n", strerror(errno)); |
---|
| 160 | closesocket(fd); |
---|
| 161 | fd = -1; |
---|
| 162 | continue; |
---|
| 163 | } else { |
---|
| 164 | phb->inpa = b_input_add(fd, B_EV_IO_WRITE, gaim_io_connected, phb); |
---|
| 165 | phb->fd = fd; |
---|
| 166 | |
---|
| 167 | break; |
---|
[289bd2d] | 168 | } |
---|
[aefaac3a] | 169 | } |
---|
| 170 | |
---|
[762d96f] | 171 | if(fd < 0 && host) |
---|
[6f7ac17] | 172 | g_free(phb); |
---|
[289bd2d] | 173 | |
---|
| 174 | return fd; |
---|
[b7d3cc34] | 175 | } |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | /* Connecting to HTTP proxies */ |
---|
| 179 | |
---|
| 180 | #define HTTP_GOODSTRING "HTTP/1.0 200 Connection established" |
---|
| 181 | #define HTTP_GOODSTRING2 "HTTP/1.1 200 Connection established" |
---|
| 182 | |
---|
[ba9edaa] | 183 | static gboolean http_canread(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 184 | { |
---|
| 185 | int nlc = 0; |
---|
| 186 | int pos = 0; |
---|
| 187 | struct PHB *phb = data; |
---|
| 188 | char inputline[8192]; |
---|
| 189 | |
---|
[ba9edaa] | 190 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 191 | |
---|
| 192 | while ((pos < sizeof(inputline)-1) && (nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) { |
---|
| 193 | if (inputline[pos - 1] == '\n') |
---|
| 194 | nlc++; |
---|
| 195 | else if (inputline[pos - 1] != '\r') |
---|
| 196 | nlc = 0; |
---|
| 197 | } |
---|
| 198 | inputline[pos] = '\0'; |
---|
| 199 | |
---|
| 200 | if ((memcmp(HTTP_GOODSTRING, inputline, strlen(HTTP_GOODSTRING)) == 0) || |
---|
| 201 | (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2)) == 0)) { |
---|
[e046390] | 202 | phb->func(phb->data, source, B_EV_IO_READ); |
---|
[b7d3cc34] | 203 | g_free(phb->host); |
---|
| 204 | g_free(phb); |
---|
[ba9edaa] | 205 | return FALSE; |
---|
[b7d3cc34] | 206 | } |
---|
| 207 | |
---|
| 208 | close(source); |
---|
[e046390] | 209 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 210 | g_free(phb->host); |
---|
| 211 | g_free(phb); |
---|
[ba9edaa] | 212 | |
---|
| 213 | return FALSE; |
---|
[b7d3cc34] | 214 | } |
---|
| 215 | |
---|
[ba9edaa] | 216 | static gboolean http_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 217 | { |
---|
| 218 | char cmd[384]; |
---|
| 219 | struct PHB *phb = data; |
---|
| 220 | unsigned int len; |
---|
| 221 | int error = ETIMEDOUT; |
---|
| 222 | if (phb->inpa > 0) |
---|
[ba9edaa] | 223 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 224 | len = sizeof(error); |
---|
| 225 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
| 226 | close(source); |
---|
[e046390] | 227 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 228 | g_free(phb->host); |
---|
| 229 | g_free(phb); |
---|
[ba9edaa] | 230 | return FALSE; |
---|
[b7d3cc34] | 231 | } |
---|
[701acdd4] | 232 | sock_make_blocking(source); |
---|
[b7d3cc34] | 233 | |
---|
| 234 | g_snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n", phb->host, phb->port, |
---|
| 235 | phb->host, phb->port); |
---|
| 236 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
| 237 | close(source); |
---|
[e046390] | 238 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 239 | g_free(phb->host); |
---|
| 240 | g_free(phb); |
---|
[ba9edaa] | 241 | return FALSE; |
---|
[b7d3cc34] | 242 | } |
---|
| 243 | |
---|
[f618a4a] | 244 | if (strlen(proxyuser) > 0) { |
---|
[b7d3cc34] | 245 | char *t1, *t2; |
---|
| 246 | t1 = g_strdup_printf("%s:%s", proxyuser, proxypass); |
---|
| 247 | t2 = tobase64(t1); |
---|
| 248 | g_free(t1); |
---|
| 249 | g_snprintf(cmd, sizeof(cmd), "Proxy-Authorization: Basic %s\r\n", t2); |
---|
| 250 | g_free(t2); |
---|
| 251 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
| 252 | close(source); |
---|
[e046390] | 253 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 254 | g_free(phb->host); |
---|
| 255 | g_free(phb); |
---|
[ba9edaa] | 256 | return FALSE; |
---|
[b7d3cc34] | 257 | } |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | g_snprintf(cmd, sizeof(cmd), "\r\n"); |
---|
| 261 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
| 262 | close(source); |
---|
[e046390] | 263 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 264 | g_free(phb->host); |
---|
| 265 | g_free(phb); |
---|
[ba9edaa] | 266 | return FALSE; |
---|
[b7d3cc34] | 267 | } |
---|
| 268 | |
---|
[e046390] | 269 | phb->inpa = b_input_add(source, B_EV_IO_READ, http_canread, phb); |
---|
[ba9edaa] | 270 | |
---|
| 271 | return FALSE; |
---|
[b7d3cc34] | 272 | } |
---|
| 273 | |
---|
[c3ffa45] | 274 | static int proxy_connect_http(const char *host, unsigned short port, struct PHB *phb) |
---|
[b7d3cc34] | 275 | { |
---|
| 276 | phb->host = g_strdup(host); |
---|
| 277 | phb->port = port; |
---|
| 278 | phb->proxy_func = http_canwrite; |
---|
| 279 | phb->proxy_data = phb; |
---|
| 280 | |
---|
| 281 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | |
---|
| 285 | /* Connecting to SOCKS4 proxies */ |
---|
| 286 | |
---|
[ba9edaa] | 287 | static gboolean s4_canread(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 288 | { |
---|
| 289 | unsigned char packet[12]; |
---|
| 290 | struct PHB *phb = data; |
---|
| 291 | |
---|
[ba9edaa] | 292 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 293 | |
---|
| 294 | memset(packet, 0, sizeof(packet)); |
---|
| 295 | if (read(source, packet, 9) >= 4 && packet[1] == 90) { |
---|
[e046390] | 296 | phb->func(phb->data, source, B_EV_IO_READ); |
---|
[b7d3cc34] | 297 | g_free(phb->host); |
---|
| 298 | g_free(phb); |
---|
[ba9edaa] | 299 | return FALSE; |
---|
[b7d3cc34] | 300 | } |
---|
| 301 | |
---|
| 302 | close(source); |
---|
[e046390] | 303 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 304 | g_free(phb->host); |
---|
| 305 | g_free(phb); |
---|
[ba9edaa] | 306 | |
---|
| 307 | return FALSE; |
---|
[b7d3cc34] | 308 | } |
---|
| 309 | |
---|
[ba9edaa] | 310 | static gboolean s4_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 311 | { |
---|
| 312 | unsigned char packet[12]; |
---|
| 313 | struct hostent *hp; |
---|
| 314 | struct PHB *phb = data; |
---|
| 315 | unsigned int len; |
---|
| 316 | int error = ETIMEDOUT; |
---|
| 317 | if (phb->inpa > 0) |
---|
[ba9edaa] | 318 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 319 | len = sizeof(error); |
---|
| 320 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
| 321 | close(source); |
---|
[e046390] | 322 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 323 | g_free(phb->host); |
---|
| 324 | g_free(phb); |
---|
[ba9edaa] | 325 | return FALSE; |
---|
[b7d3cc34] | 326 | } |
---|
[701acdd4] | 327 | sock_make_blocking(source); |
---|
[b7d3cc34] | 328 | |
---|
| 329 | /* XXX does socks4 not support host name lookups by the proxy? */ |
---|
| 330 | if (!(hp = gethostbyname(phb->host))) { |
---|
| 331 | close(source); |
---|
[e046390] | 332 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 333 | g_free(phb->host); |
---|
| 334 | g_free(phb); |
---|
[ba9edaa] | 335 | return FALSE; |
---|
[b7d3cc34] | 336 | } |
---|
| 337 | |
---|
| 338 | packet[0] = 4; |
---|
| 339 | packet[1] = 1; |
---|
| 340 | packet[2] = phb->port >> 8; |
---|
| 341 | packet[3] = phb->port & 0xff; |
---|
| 342 | packet[4] = (unsigned char)(hp->h_addr_list[0])[0]; |
---|
| 343 | packet[5] = (unsigned char)(hp->h_addr_list[0])[1]; |
---|
| 344 | packet[6] = (unsigned char)(hp->h_addr_list[0])[2]; |
---|
| 345 | packet[7] = (unsigned char)(hp->h_addr_list[0])[3]; |
---|
| 346 | packet[8] = 0; |
---|
| 347 | if (write(source, packet, 9) != 9) { |
---|
| 348 | close(source); |
---|
[e046390] | 349 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 350 | g_free(phb->host); |
---|
| 351 | g_free(phb); |
---|
[ba9edaa] | 352 | return FALSE; |
---|
[b7d3cc34] | 353 | } |
---|
| 354 | |
---|
[e046390] | 355 | phb->inpa = b_input_add(source, B_EV_IO_READ, s4_canread, phb); |
---|
[ba9edaa] | 356 | |
---|
| 357 | return FALSE; |
---|
[b7d3cc34] | 358 | } |
---|
| 359 | |
---|
[c3ffa45] | 360 | static int proxy_connect_socks4(const char *host, unsigned short port, struct PHB *phb) |
---|
[b7d3cc34] | 361 | { |
---|
| 362 | phb->host = g_strdup(host); |
---|
| 363 | phb->port = port; |
---|
| 364 | phb->proxy_func = s4_canwrite; |
---|
| 365 | phb->proxy_data = phb; |
---|
| 366 | |
---|
| 367 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | |
---|
| 371 | /* Connecting to SOCKS5 proxies */ |
---|
| 372 | |
---|
[ba9edaa] | 373 | static gboolean s5_canread_again(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 374 | { |
---|
| 375 | unsigned char buf[512]; |
---|
| 376 | struct PHB *phb = data; |
---|
| 377 | |
---|
[ba9edaa] | 378 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 379 | |
---|
| 380 | if (read(source, buf, 10) < 10) { |
---|
| 381 | close(source); |
---|
[e046390] | 382 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 383 | g_free(phb->host); |
---|
| 384 | g_free(phb); |
---|
[ba9edaa] | 385 | return FALSE; |
---|
[b7d3cc34] | 386 | } |
---|
| 387 | if ((buf[0] != 0x05) || (buf[1] != 0x00)) { |
---|
| 388 | close(source); |
---|
[e046390] | 389 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 390 | g_free(phb->host); |
---|
| 391 | g_free(phb); |
---|
[ba9edaa] | 392 | return FALSE; |
---|
[b7d3cc34] | 393 | } |
---|
| 394 | |
---|
[e046390] | 395 | phb->func(phb->data, source, B_EV_IO_READ); |
---|
[b7d3cc34] | 396 | g_free(phb->host); |
---|
| 397 | g_free(phb); |
---|
[ba9edaa] | 398 | |
---|
| 399 | return FALSE; |
---|
[b7d3cc34] | 400 | } |
---|
| 401 | |
---|
| 402 | static void s5_sendconnect(gpointer data, gint source) |
---|
| 403 | { |
---|
| 404 | unsigned char buf[512]; |
---|
| 405 | struct PHB *phb = data; |
---|
| 406 | int hlen = strlen(phb->host); |
---|
[ba9edaa] | 407 | |
---|
[b7d3cc34] | 408 | buf[0] = 0x05; |
---|
| 409 | buf[1] = 0x01; /* CONNECT */ |
---|
| 410 | buf[2] = 0x00; /* reserved */ |
---|
| 411 | buf[3] = 0x03; /* address type -- host name */ |
---|
| 412 | buf[4] = hlen; |
---|
| 413 | memcpy(buf + 5, phb->host, hlen); |
---|
| 414 | buf[5 + strlen(phb->host)] = phb->port >> 8; |
---|
| 415 | buf[5 + strlen(phb->host) + 1] = phb->port & 0xff; |
---|
| 416 | |
---|
| 417 | if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) { |
---|
| 418 | close(source); |
---|
[e046390] | 419 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 420 | g_free(phb->host); |
---|
| 421 | g_free(phb); |
---|
| 422 | return; |
---|
| 423 | } |
---|
| 424 | |
---|
[e046390] | 425 | phb->inpa = b_input_add(source, B_EV_IO_READ, s5_canread_again, phb); |
---|
[b7d3cc34] | 426 | } |
---|
| 427 | |
---|
[ba9edaa] | 428 | static gboolean s5_readauth(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 429 | { |
---|
| 430 | unsigned char buf[512]; |
---|
| 431 | struct PHB *phb = data; |
---|
| 432 | |
---|
[ba9edaa] | 433 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 434 | |
---|
| 435 | if (read(source, buf, 2) < 2) { |
---|
| 436 | close(source); |
---|
[e046390] | 437 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 438 | g_free(phb->host); |
---|
| 439 | g_free(phb); |
---|
[ba9edaa] | 440 | return FALSE; |
---|
[b7d3cc34] | 441 | } |
---|
| 442 | |
---|
| 443 | if ((buf[0] != 0x01) || (buf[1] != 0x00)) { |
---|
| 444 | close(source); |
---|
[e046390] | 445 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 446 | g_free(phb->host); |
---|
| 447 | g_free(phb); |
---|
[ba9edaa] | 448 | return FALSE; |
---|
[b7d3cc34] | 449 | } |
---|
| 450 | |
---|
| 451 | s5_sendconnect(phb, source); |
---|
[ba9edaa] | 452 | |
---|
| 453 | return FALSE; |
---|
[b7d3cc34] | 454 | } |
---|
| 455 | |
---|
[ba9edaa] | 456 | static gboolean s5_canread(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 457 | { |
---|
| 458 | unsigned char buf[512]; |
---|
| 459 | struct PHB *phb = data; |
---|
| 460 | |
---|
[ba9edaa] | 461 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 462 | |
---|
| 463 | if (read(source, buf, 2) < 2) { |
---|
| 464 | close(source); |
---|
[e046390] | 465 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 466 | g_free(phb->host); |
---|
| 467 | g_free(phb); |
---|
[ba9edaa] | 468 | return FALSE; |
---|
[b7d3cc34] | 469 | } |
---|
| 470 | |
---|
| 471 | if ((buf[0] != 0x05) || (buf[1] == 0xff)) { |
---|
| 472 | close(source); |
---|
[e046390] | 473 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 474 | g_free(phb->host); |
---|
| 475 | g_free(phb); |
---|
[ba9edaa] | 476 | return FALSE; |
---|
[b7d3cc34] | 477 | } |
---|
| 478 | |
---|
| 479 | if (buf[1] == 0x02) { |
---|
| 480 | unsigned int i = strlen(proxyuser), j = strlen(proxypass); |
---|
| 481 | buf[0] = 0x01; /* version 1 */ |
---|
| 482 | buf[1] = i; |
---|
| 483 | memcpy(buf + 2, proxyuser, i); |
---|
| 484 | buf[2 + i] = j; |
---|
| 485 | memcpy(buf + 2 + i + 1, proxypass, j); |
---|
| 486 | if (write(source, buf, 3 + i + j) < 3 + i + j) { |
---|
| 487 | close(source); |
---|
[e046390] | 488 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 489 | g_free(phb->host); |
---|
| 490 | g_free(phb); |
---|
[ba9edaa] | 491 | return FALSE; |
---|
[b7d3cc34] | 492 | } |
---|
| 493 | |
---|
[e046390] | 494 | phb->inpa = b_input_add(source, B_EV_IO_READ, s5_readauth, phb); |
---|
[b7d3cc34] | 495 | } else { |
---|
| 496 | s5_sendconnect(phb, source); |
---|
| 497 | } |
---|
[ba9edaa] | 498 | |
---|
| 499 | return FALSE; |
---|
[b7d3cc34] | 500 | } |
---|
| 501 | |
---|
[ba9edaa] | 502 | static gboolean s5_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 503 | { |
---|
| 504 | unsigned char buf[512]; |
---|
| 505 | int i; |
---|
| 506 | struct PHB *phb = data; |
---|
| 507 | unsigned int len; |
---|
| 508 | int error = ETIMEDOUT; |
---|
| 509 | if (phb->inpa > 0) |
---|
[ba9edaa] | 510 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 511 | len = sizeof(error); |
---|
| 512 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
| 513 | close(source); |
---|
[e046390] | 514 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 515 | g_free(phb->host); |
---|
| 516 | g_free(phb); |
---|
[ba9edaa] | 517 | return FALSE; |
---|
[b7d3cc34] | 518 | } |
---|
[701acdd4] | 519 | sock_make_blocking(source); |
---|
[b7d3cc34] | 520 | |
---|
| 521 | i = 0; |
---|
| 522 | buf[0] = 0x05; /* SOCKS version 5 */ |
---|
| 523 | if (proxyuser[0]) { |
---|
| 524 | buf[1] = 0x02; /* two methods */ |
---|
| 525 | buf[2] = 0x00; /* no authentication */ |
---|
| 526 | buf[3] = 0x02; /* username/password authentication */ |
---|
| 527 | i = 4; |
---|
| 528 | } else { |
---|
| 529 | buf[1] = 0x01; |
---|
| 530 | buf[2] = 0x00; |
---|
| 531 | i = 3; |
---|
| 532 | } |
---|
| 533 | |
---|
| 534 | if (write(source, buf, i) < i) { |
---|
| 535 | close(source); |
---|
[e046390] | 536 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
[b7d3cc34] | 537 | g_free(phb->host); |
---|
| 538 | g_free(phb); |
---|
[ba9edaa] | 539 | return FALSE; |
---|
[b7d3cc34] | 540 | } |
---|
| 541 | |
---|
[e046390] | 542 | phb->inpa = b_input_add(source, B_EV_IO_READ, s5_canread, phb); |
---|
[ba9edaa] | 543 | |
---|
| 544 | return FALSE; |
---|
[b7d3cc34] | 545 | } |
---|
| 546 | |
---|
[c3ffa45] | 547 | static int proxy_connect_socks5(const char *host, unsigned short port, struct PHB *phb) |
---|
[b7d3cc34] | 548 | { |
---|
| 549 | phb->host = g_strdup(host); |
---|
| 550 | phb->port = port; |
---|
| 551 | phb->proxy_func = s5_canwrite; |
---|
| 552 | phb->proxy_data = phb; |
---|
| 553 | |
---|
| 554 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
| 555 | } |
---|
| 556 | |
---|
| 557 | |
---|
| 558 | /* Export functions */ |
---|
| 559 | |
---|
[ba9edaa] | 560 | int proxy_connect(const char *host, int port, b_event_handler func, gpointer data) |
---|
[b7d3cc34] | 561 | { |
---|
| 562 | struct PHB *phb; |
---|
| 563 | |
---|
[58a1449] | 564 | if (!host || port <= 0 || !func || strlen(host) > 128) { |
---|
[b7d3cc34] | 565 | return -1; |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | phb = g_new0(struct PHB, 1); |
---|
| 569 | phb->func = func; |
---|
| 570 | phb->data = data; |
---|
| 571 | |
---|
[58a1449] | 572 | if (proxytype == PROXY_NONE || !proxyhost[0] || proxyport <= 0) |
---|
[b7d3cc34] | 573 | return proxy_connect_none(host, port, phb); |
---|
| 574 | else if (proxytype == PROXY_HTTP) |
---|
| 575 | return proxy_connect_http(host, port, phb); |
---|
| 576 | else if (proxytype == PROXY_SOCKS4) |
---|
| 577 | return proxy_connect_socks4(host, port, phb); |
---|
| 578 | else if (proxytype == PROXY_SOCKS5) |
---|
| 579 | return proxy_connect_socks5(host, port, phb); |
---|
| 580 | |
---|
| 581 | g_free(phb); |
---|
| 582 | return -1; |
---|
| 583 | } |
---|