Changes in protocols/jabber/s5bytestream.c [5ebff60:9216eff]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/jabber/s5bytestream.c
r5ebff60 r9216eff 23 23 24 24 #include "jabber.h" 25 #include "sha1.h"26 25 #include "lib/ftutil.h" 27 26 #include <poll.h> … … 42 41 43 42 /* SHA1( SID + Initiator JID + Target JID) */ 44 char *pseudoad r;43 char *pseudoaddr; 45 44 46 45 gint connect_timeout; … … 130 129 } 131 130 132 g_free(bt->pseudoad r);131 g_free(bt->pseudoaddr); 133 132 134 133 while (bt->streamhosts) { … … 254 253 } 255 254 255 void jabber_bs_remove_events(struct bs_transfer *bt) 256 { 257 struct jabber_transfer *tf = bt->tf; 258 259 if (tf->watch_out) { 260 b_event_remove(tf->watch_out); 261 tf->watch_out = 0; 262 } 263 264 if (tf->watch_in) { 265 b_event_remove(tf->watch_in); 266 tf->watch_in = 0; 267 } 268 269 if (tf->fd != -1) { 270 closesocket(tf->fd); 271 tf->fd = -1; 272 } 273 274 if (bt->connect_timeout) { 275 b_event_remove(bt->connect_timeout); 276 bt->connect_timeout = 0; 277 } 278 } 279 256 280 /* Bad luck */ 257 281 void jabber_bs_canceled(file_transfer_t *ft, char *reason) … … 260 284 261 285 imcb_log(tf->ic, "File transfer aborted: %s", reason); 286 } 287 288 static struct jabber_transfer *get_ft_by_sid(GSList *tflist, char *sid) { 289 GSList *l; 290 for (l = tflist; l; l = g_slist_next(l)) { 291 struct jabber_transfer *tft = l->data; 292 if ((strcmp(tft->sid, sid) == 0)) { 293 return tft; 294 } 295 } 296 return NULL; 297 } 298 299 /* SHA1( SID + Initiator JID + Target JID ) is given to the streamhost which it will match against the initiator's value 300 * Returns a newly allocated string */ 301 static char *generate_pseudoaddr(char *sid, char *ini_jid, char *tgt_jid) { 302 char *contents = g_strconcat(sid, ini_jid, tgt_jid, NULL); 303 char *hash_hex = g_compute_checksum_for_string(G_CHECKSUM_SHA1, contents, -1); 304 g_free(contents); 305 return hash_hex; 306 } 307 308 static jabber_streamhost_t *jabber_streamhost_new(char *jid, char *host, int port) 309 { 310 jabber_streamhost_t *sh = g_new0(jabber_streamhost_t, 1); 311 sh->jid = g_strdup(jid); 312 sh->host = g_strdup(host); 313 if (port) { 314 g_snprintf(sh->port, sizeof(sh->port), "%u", port); 315 } 316 return sh; 262 317 } 263 318 … … 270 325 struct jabber_data *jd = ic->proto_data; 271 326 struct jabber_transfer *tf = NULL; 272 GSList *tflist;273 327 struct bs_transfer *bt; 274 328 GSList *shlist = NULL; 275 329 struct xt_node *shnode; 276 277 sha1_state_t sha;278 char hash_hex[41];279 unsigned char hash[20];280 int i;281 330 282 331 if (!(iq_id = xt_find_attr(node, "id")) || … … 303 352 (port_s = xt_find_attr(shnode, "port")) && 304 353 (sscanf(port_s, "%d", &port) == 1)) { 305 jabber_streamhost_t *sh = g_new0(jabber_streamhost_t, 1); 306 sh->jid = g_strdup(jid); 307 sh->host = g_strdup(host); 308 sprintf(sh->port, "%u", port); 309 shlist = g_slist_append(shlist, sh); 354 shlist = g_slist_append(shlist, jabber_streamhost_new(jid, host, port)); 310 355 } 311 356 shnode = shnode->next; … … 317 362 } 318 363 319 /* Let's see if we can find out what this bytestream should be for... */ 320 321 for (tflist = jd->filetransfers; tflist; tflist = g_slist_next(tflist)) { 322 struct jabber_transfer *tft = tflist->data; 323 if ((strcmp(tft->sid, sid) == 0) && 324 (strcmp(tft->ini_jid, ini_jid) == 0) && 325 (strcmp(tft->tgt_jid, tgt_jid) == 0)) { 326 tf = tft; 327 break; 328 } 329 } 330 331 if (!tf) { 364 if (!(tf = get_ft_by_sid(jd->filetransfers, sid))) { 332 365 imcb_log(ic, "WARNING: Received bytestream request from %s that doesn't match an SI request", ini_jid); 333 366 return XT_HANDLED; … … 339 372 340 373 tf->ft->canceled = jabber_bs_canceled; 341 342 /* SHA1( SID + Initiator JID + Target JID ) is given to the streamhost which it will match against the initiator's value */343 sha1_init(&sha);344 sha1_append(&sha, (unsigned char *) sid, strlen(sid));345 sha1_append(&sha, (unsigned char *) ini_jid, strlen(ini_jid));346 sha1_append(&sha, (unsigned char *) tgt_jid, strlen(tgt_jid));347 sha1_finish(&sha, hash);348 349 for (i = 0; i < 20; i++) {350 sprintf(hash_hex + i * 2, "%02x", hash[i]);351 }352 374 353 375 bt = g_new0(struct bs_transfer, 1); … … 356 378 bt->sh = shlist->data; 357 379 bt->phase = BS_PHASE_CONNECT; 358 bt->pseudoad r = g_strdup(hash_hex);380 bt->pseudoaddr = generate_pseudoaddr(sid, ini_jid, tgt_jid); 359 381 tf->streamhandle = bt; 360 382 tf->ft->free = jabber_bs_free_transfer; … … 451 473 .rsv = 0, 452 474 .atyp = 0x03, 453 .addrlen = strlen(bt->pseudoad r),475 .addrlen = strlen(bt->pseudoaddr), 454 476 .port = 0 455 477 }; … … 468 490 469 491 /* copy hash into connect message */ 470 memcpy(socks5_connect.address, bt->pseudoad r, socks5_connect.addrlen);492 memcpy(socks5_connect.address, bt->pseudoaddr, socks5_connect.addrlen); 471 493 472 494 ASSERTSOCKOP(send(fd, &socks5_connect, sizeof(struct socks5_message), 0), "Sending SOCKS5 Connect"); … … 558 580 if (shlist && shlist->next) { 559 581 bt->sh = shlist->next->data; 582 jabber_bs_remove_events(bt); 560 583 return jabber_bs_recv_handshake(bt, -1, 0); 561 584 } … … 731 754 struct jabber_data *jd = ic->proto_data; 732 755 struct bs_transfer *bt; 733 GSList *tflist;734 756 struct xt_node *c; 735 757 char *sid, *jid; … … 750 772 /* Let's see if we can find out what this bytestream should be for... */ 751 773 752 for (tflist = jd->filetransfers; tflist; tflist = g_slist_next(tflist)) { 753 struct jabber_transfer *tft = tflist->data; 754 if ((strcmp(tft->sid, sid) == 0)) { 755 tf = tft; 756 break; 757 } 758 } 759 760 if (!tf) { 774 if (!(tf = get_ft_by_sid(jd->filetransfers, sid))) { 761 775 imcb_log(ic, "WARNING: Received SOCKS5 bytestream reply to unknown request"); 762 776 return XT_HANDLED; … … 776 790 /* using a proxy, abort listen */ 777 791 778 if (tf->watch_in) { 779 b_event_remove(tf->watch_in); 780 tf->watch_in = 0; 781 } 782 783 if (tf->fd != -1) { 784 closesocket(tf->fd); 785 tf->fd = -1; 786 } 787 788 if (bt->connect_timeout) { 789 b_event_remove(bt->connect_timeout); 790 bt->connect_timeout = 0; 791 } 792 jabber_bs_remove_events(bt); 792 793 793 794 GSList *shlist; … … 838 839 { 839 840 char *sid; 840 GSList *tflist;841 841 struct jabber_transfer *tf = NULL; 842 842 struct xt_node *query; … … 846 846 sid = xt_find_attr(query, "sid"); 847 847 848 for (tflist = jd->filetransfers; tflist; tflist = g_slist_next(tflist)) { 849 struct jabber_transfer *tft = tflist->data; 850 if ((strcmp(tft->sid, sid) == 0)) { 851 tf = tft; 852 break; 853 } 854 } 855 856 if (!tf) { 848 if (!(tf = get_ft_by_sid(jd->filetransfers, sid))) { 857 849 imcb_log(ic, "WARNING: Received SOCKS5 bytestream activation for unknown stream"); 858 850 return XT_HANDLED; … … 883 875 *port++ = '\0'; 884 876 885 sh = g_new0(jabber_streamhost_t, 1); 886 sh->jid = g_strdup(jid); 887 sh->host = g_strdup(host); 888 g_snprintf(sh->port, sizeof(sh->port), "%s", port); 877 sh = jabber_streamhost_new(jid, host, 0); 878 strncpy(sh->port, port, sizeof(sh->port)); 889 879 890 880 return sh; … … 910 900 if (strcmp(proxy, "<local>") == 0) { 911 901 if ((tf->fd = ft_listen(&tf->saddr, host, port, jd->fd, FALSE, &errmsg)) != -1) { 912 sh = g_new0(jabber_streamhost_t, 1); 913 sh->jid = g_strdup(tf->ini_jid); 914 sh->host = g_strdup(host); 915 g_snprintf(sh->port, sizeof(sh->port), "%s", port); 902 sh = jabber_streamhost_new(tf->ini_jid, host, 0); 903 strncpy(sh->port, port, sizeof(sh->port)); 916 904 bt->streamhosts = g_slist_append(bt->streamhosts, sh); 917 905 … … 948 936 { 949 937 struct bs_transfer *bt; 950 sha1_state_t sha;951 char hash_hex[41];952 unsigned char hash[20];953 int i, ret;954 955 /* SHA1( SID + Initiator JID + Target JID ) is given to the streamhost which it will match against the initiator's value */956 sha1_init(&sha);957 sha1_append(&sha, (unsigned char *) tf->sid, strlen(tf->sid));958 sha1_append(&sha, (unsigned char *) tf->ini_jid, strlen(tf->ini_jid));959 sha1_append(&sha, (unsigned char *) tf->tgt_jid, strlen(tf->tgt_jid));960 sha1_finish(&sha, hash);961 962 for (i = 0; i < 20; i++) {963 sprintf(hash_hex + i * 2, "%02x", hash[i]);964 }965 938 966 939 bt = g_new0(struct bs_transfer, 1); 967 940 bt->tf = tf; 968 941 bt->phase = BS_PHASE_CONNECT; 969 bt->pseudoad r = g_strdup(hash_hex);942 bt->pseudoaddr = generate_pseudoaddr(tf->sid, tf->ini_jid, tf->tgt_jid); 970 943 tf->streamhandle = bt; 971 944 tf->ft->free = jabber_bs_free_transfer; … … 974 947 jabber_si_set_proxies(bt); 975 948 976 ret = jabber_bs_send_request(tf, bt->streamhosts); 977 978 return ret; 949 return jabber_bs_send_request(tf, bt->streamhosts); 979 950 } 980 951 … … 1139 1110 socks5_connect.atyp); 1140 1111 } 1141 if (!(memcmp(socks5_connect.address, bt->pseudoad r, 40) == 0)) {1112 if (!(memcmp(socks5_connect.address, bt->pseudoaddr, 40) == 0)) { 1142 1113 return jabber_bs_abort(bt, "SOCKS5 Connect message contained wrong digest"); 1143 1114 }
Note: See TracChangeset
for help on using the changeset viewer.