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