Changes in protocols/jabber/s5bytestream.c [9216eff:5ebff60]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/jabber/s5bytestream.c
r9216eff r5ebff60 23 23 24 24 #include "jabber.h" 25 #include "sha1.h" 25 26 #include "lib/ftutil.h" 26 27 #include <poll.h> … … 41 42 42 43 /* SHA1( SID + Initiator JID + Target JID) */ 43 char *pseudoad dr;44 char *pseudoadr; 44 45 45 46 gint connect_timeout; … … 129 130 } 130 131 131 g_free(bt->pseudoad dr);132 g_free(bt->pseudoadr); 132 133 133 134 while (bt->streamhosts) { … … 253 254 } 254 255 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 280 256 /* Bad luck */ 281 257 void jabber_bs_canceled(file_transfer_t *ft, char *reason) … … 284 260 285 261 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 value300 * 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;317 262 } 318 263 … … 325 270 struct jabber_data *jd = ic->proto_data; 326 271 struct jabber_transfer *tf = NULL; 272 GSList *tflist; 327 273 struct bs_transfer *bt; 328 274 GSList *shlist = NULL; 329 275 struct xt_node *shnode; 276 277 sha1_state_t sha; 278 char hash_hex[41]; 279 unsigned char hash[20]; 280 int i; 330 281 331 282 if (!(iq_id = xt_find_attr(node, "id")) || … … 352 303 (port_s = xt_find_attr(shnode, "port")) && 353 304 (sscanf(port_s, "%d", &port) == 1)) { 354 shlist = g_slist_append(shlist, jabber_streamhost_new(jid, host, port)); 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); 355 310 } 356 311 shnode = shnode->next; … … 362 317 } 363 318 364 if (!(tf = get_ft_by_sid(jd->filetransfers, sid))) { 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) { 365 332 imcb_log(ic, "WARNING: Received bytestream request from %s that doesn't match an SI request", ini_jid); 366 333 return XT_HANDLED; … … 372 339 373 340 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 } 374 352 375 353 bt = g_new0(struct bs_transfer, 1); … … 378 356 bt->sh = shlist->data; 379 357 bt->phase = BS_PHASE_CONNECT; 380 bt->pseudoad dr = generate_pseudoaddr(sid, ini_jid, tgt_jid);358 bt->pseudoadr = g_strdup(hash_hex); 381 359 tf->streamhandle = bt; 382 360 tf->ft->free = jabber_bs_free_transfer; … … 473 451 .rsv = 0, 474 452 .atyp = 0x03, 475 .addrlen = strlen(bt->pseudoad dr),453 .addrlen = strlen(bt->pseudoadr), 476 454 .port = 0 477 455 }; … … 490 468 491 469 /* copy hash into connect message */ 492 memcpy(socks5_connect.address, bt->pseudoad dr, socks5_connect.addrlen);470 memcpy(socks5_connect.address, bt->pseudoadr, socks5_connect.addrlen); 493 471 494 472 ASSERTSOCKOP(send(fd, &socks5_connect, sizeof(struct socks5_message), 0), "Sending SOCKS5 Connect"); … … 580 558 if (shlist && shlist->next) { 581 559 bt->sh = shlist->next->data; 582 jabber_bs_remove_events(bt);583 560 return jabber_bs_recv_handshake(bt, -1, 0); 584 561 } … … 754 731 struct jabber_data *jd = ic->proto_data; 755 732 struct bs_transfer *bt; 733 GSList *tflist; 756 734 struct xt_node *c; 757 735 char *sid, *jid; … … 772 750 /* Let's see if we can find out what this bytestream should be for... */ 773 751 774 if (!(tf = get_ft_by_sid(jd->filetransfers, sid))) { 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) { 775 761 imcb_log(ic, "WARNING: Received SOCKS5 bytestream reply to unknown request"); 776 762 return XT_HANDLED; … … 790 776 /* using a proxy, abort listen */ 791 777 792 jabber_bs_remove_events(bt); 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 } 793 792 794 793 GSList *shlist; … … 839 838 { 840 839 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 if (!(tf = get_ft_by_sid(jd->filetransfers, sid))) { 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) { 849 857 imcb_log(ic, "WARNING: Received SOCKS5 bytestream activation for unknown stream"); 850 858 return XT_HANDLED; … … 875 883 *port++ = '\0'; 876 884 877 sh = jabber_streamhost_new(jid, host, 0); 878 strncpy(sh->port, port, sizeof(sh->port)); 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); 879 889 880 890 return sh; … … 900 910 if (strcmp(proxy, "<local>") == 0) { 901 911 if ((tf->fd = ft_listen(&tf->saddr, host, port, jd->fd, FALSE, &errmsg)) != -1) { 902 sh = jabber_streamhost_new(tf->ini_jid, host, 0); 903 strncpy(sh->port, port, sizeof(sh->port)); 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); 904 916 bt->streamhosts = g_slist_append(bt->streamhosts, sh); 905 917 … … 936 948 { 937 949 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 } 938 965 939 966 bt = g_new0(struct bs_transfer, 1); 940 967 bt->tf = tf; 941 968 bt->phase = BS_PHASE_CONNECT; 942 bt->pseudoad dr = generate_pseudoaddr(tf->sid, tf->ini_jid, tf->tgt_jid);969 bt->pseudoadr = g_strdup(hash_hex); 943 970 tf->streamhandle = bt; 944 971 tf->ft->free = jabber_bs_free_transfer; … … 947 974 jabber_si_set_proxies(bt); 948 975 949 return jabber_bs_send_request(tf, bt->streamhosts); 976 ret = jabber_bs_send_request(tf, bt->streamhosts); 977 978 return ret; 950 979 } 951 980 … … 1110 1139 socks5_connect.atyp); 1111 1140 } 1112 if (!(memcmp(socks5_connect.address, bt->pseudoad dr, 40) == 0)) {1141 if (!(memcmp(socks5_connect.address, bt->pseudoadr, 40) == 0)) { 1113 1142 return jabber_bs_abort(bt, "SOCKS5 Connect message contained wrong digest"); 1114 1143 }
Note: See TracChangeset
for help on using the changeset viewer.