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