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