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