Changeset da6f167 for protocols/jabber
- Timestamp:
- 2015-02-22T18:53:21Z (10 years ago)
- Branches:
- master
- Children:
- 9216eff
- Parents:
- 90adf84
- git-author:
- dequis <dx@…> (01-02-15 16:57:22)
- git-committer:
- dequis <dx@…> (22-02-15 18:53:21)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/jabber/s5bytestream.c
r90adf84 rda6f167 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) { … … 262 261 } 263 262 263 static struct jabber_transfer *get_ft_by_sid(GSList *tflist, char *sid) { 264 GSList *l; 265 for (l = tflist; l; l = g_slist_next(l)) { 266 struct jabber_transfer *tft = l->data; 267 if ((strcmp(tft->sid, sid) == 0)) { 268 return tft; 269 } 270 } 271 return NULL; 272 } 273 274 /* SHA1( SID + Initiator JID + Target JID ) is given to the streamhost which it will match against the initiator's value 275 * Returns a newly allocated string */ 276 static char *generate_pseudoaddr(char *sid, char *ini_jid, char *tgt_jid) { 277 char *contents = g_strconcat(sid, ini_jid, tgt_jid, NULL); 278 char *hash_hex = g_compute_checksum_for_string(G_CHECKSUM_SHA1, contents, -1); 279 g_free(contents); 280 return hash_hex; 281 } 282 283 static jabber_streamhost_t *jabber_streamhost_new(char *jid, char *host, int port) 284 { 285 jabber_streamhost_t *sh = g_new0(jabber_streamhost_t, 1); 286 sh->jid = g_strdup(jid); 287 sh->host = g_strdup(host); 288 if (port) { 289 g_snprintf(sh->port, sizeof(sh->port), "%u", port); 290 } 291 return sh; 292 } 293 264 294 /* 265 295 * Parses an incoming bytestream request and calls jabber_bs_handshake on success. … … 270 300 struct jabber_data *jd = ic->proto_data; 271 301 struct jabber_transfer *tf = NULL; 272 GSList *tflist;273 302 struct bs_transfer *bt; 274 303 GSList *shlist = NULL; 275 304 struct xt_node *shnode; 276 277 sha1_state_t sha;278 char hash_hex[41];279 unsigned char hash[20];280 int i;281 305 282 306 if (!(iq_id = xt_find_attr(node, "id")) || … … 303 327 (port_s = xt_find_attr(shnode, "port")) && 304 328 (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); 329 shlist = g_slist_append(shlist, jabber_streamhost_new(jid, host, port)); 310 330 } 311 331 shnode = shnode->next; … … 317 337 } 318 338 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) { 339 if (!(tf = get_ft_by_sid(jd->filetransfers, sid))) { 332 340 imcb_log(ic, "WARNING: Received bytestream request from %s that doesn't match an SI request", ini_jid); 333 341 return XT_HANDLED; … … 339 347 340 348 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 349 353 350 bt = g_new0(struct bs_transfer, 1); … … 356 353 bt->sh = shlist->data; 357 354 bt->phase = BS_PHASE_CONNECT; 358 bt->pseudoad r = g_strdup(hash_hex);355 bt->pseudoaddr = generate_pseudoaddr(sid, ini_jid, tgt_jid); 359 356 tf->streamhandle = bt; 360 357 tf->ft->free = jabber_bs_free_transfer; … … 451 448 .rsv = 0, 452 449 .atyp = 0x03, 453 .addrlen = strlen(bt->pseudoad r),450 .addrlen = strlen(bt->pseudoaddr), 454 451 .port = 0 455 452 }; … … 468 465 469 466 /* copy hash into connect message */ 470 memcpy(socks5_connect.address, bt->pseudoad r, socks5_connect.addrlen);467 memcpy(socks5_connect.address, bt->pseudoaddr, socks5_connect.addrlen); 471 468 472 469 ASSERTSOCKOP(send(fd, &socks5_connect, sizeof(struct socks5_message), 0), "Sending SOCKS5 Connect"); … … 731 728 struct jabber_data *jd = ic->proto_data; 732 729 struct bs_transfer *bt; 733 GSList *tflist;734 730 struct xt_node *c; 735 731 char *sid, *jid; … … 750 746 /* Let's see if we can find out what this bytestream should be for... */ 751 747 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) { 748 if (!(tf = get_ft_by_sid(jd->filetransfers, sid))) { 761 749 imcb_log(ic, "WARNING: Received SOCKS5 bytestream reply to unknown request"); 762 750 return XT_HANDLED; … … 838 826 { 839 827 char *sid; 840 GSList *tflist;841 828 struct jabber_transfer *tf = NULL; 842 829 struct xt_node *query; … … 846 833 sid = xt_find_attr(query, "sid"); 847 834 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) { 835 if (!(tf = get_ft_by_sid(jd->filetransfers, sid))) { 857 836 imcb_log(ic, "WARNING: Received SOCKS5 bytestream activation for unknown stream"); 858 837 return XT_HANDLED; … … 883 862 *port++ = '\0'; 884 863 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); 864 sh = jabber_streamhost_new(jid, host, 0); 865 strncpy(sh->port, port, sizeof(sh->port)); 889 866 890 867 return sh; … … 910 887 if (strcmp(proxy, "<local>") == 0) { 911 888 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); 889 sh = jabber_streamhost_new(tf->ini_jid, host, 0); 890 strncpy(sh->port, port, sizeof(sh->port)); 916 891 bt->streamhosts = g_slist_append(bt->streamhosts, sh); 917 892 … … 948 923 { 949 924 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 925 966 926 bt = g_new0(struct bs_transfer, 1); 967 927 bt->tf = tf; 968 928 bt->phase = BS_PHASE_CONNECT; 969 bt->pseudoad r = g_strdup(hash_hex);929 bt->pseudoaddr = generate_pseudoaddr(tf->sid, tf->ini_jid, tf->tgt_jid); 970 930 tf->streamhandle = bt; 971 931 tf->ft->free = jabber_bs_free_transfer; … … 974 934 jabber_si_set_proxies(bt); 975 935 976 ret = jabber_bs_send_request(tf, bt->streamhosts); 977 978 return ret; 936 return jabber_bs_send_request(tf, bt->streamhosts); 979 937 } 980 938 … … 1139 1097 socks5_connect.atyp); 1140 1098 } 1141 if (!(memcmp(socks5_connect.address, bt->pseudoad r, 40) == 0)) {1099 if (!(memcmp(socks5_connect.address, bt->pseudoaddr, 40) == 0)) { 1142 1100 return jabber_bs_abort(bt, "SOCKS5 Connect message contained wrong digest"); 1143 1101 }
Note: See TracChangeset
for help on using the changeset viewer.