Ignore:
Timestamp:
2015-05-04T21:58:50Z (9 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Children:
5726a0d
Parents:
531eabd (diff), 5ca1416 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Catch up with master.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/jabber/s5bytestream.c

    r531eabd rb1dc403  
    2323
    2424#include "jabber.h"
    25 #include "sha1.h"
    2625#include "lib/ftutil.h"
    2726#include <poll.h>
     
    4241
    4342        /* SHA1( SID + Initiator JID + Target JID) */
    44         char *pseudoadr;
     43        char *pseudoaddr;
    4544
    4645        gint connect_timeout;
     
    130129        }
    131130
    132         g_free(bt->pseudoadr);
     131        g_free(bt->pseudoaddr);
    133132
    134133        while (bt->streamhosts) {
     
    254253}
    255254
     255void 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
    256280/* Bad luck */
    257281void jabber_bs_canceled(file_transfer_t *ft, char *reason)
     
    260284
    261285        imcb_log(tf->ic, "File transfer aborted: %s", reason);
     286}
     287
     288static 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 */
     301static 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
     308static 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;
    262317}
    263318
     
    270325        struct jabber_data *jd = ic->proto_data;
    271326        struct jabber_transfer *tf = NULL;
    272         GSList *tflist;
    273327        struct bs_transfer *bt;
    274328        GSList *shlist = NULL;
    275329        struct xt_node *shnode;
    276 
    277         sha1_state_t sha;
    278         char hash_hex[41];
    279         unsigned char hash[20];
    280         int i;
    281330
    282331        if (!(iq_id   = xt_find_attr(node, "id")) ||
     
    303352                    (port_s = xt_find_attr(shnode, "port")) &&
    304353                    (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));
    310355                }
    311356                shnode = shnode->next;
     
    317362        }
    318363
    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))) {
    332365                imcb_log(ic, "WARNING: Received bytestream request from %s that doesn't match an SI request", ini_jid);
    333366                return XT_HANDLED;
     
    339372
    340373        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         }
    352374
    353375        bt = g_new0(struct bs_transfer, 1);
     
    356378        bt->sh = shlist->data;
    357379        bt->phase = BS_PHASE_CONNECT;
    358         bt->pseudoadr = g_strdup(hash_hex);
     380        bt->pseudoaddr = generate_pseudoaddr(sid, ini_jid, tgt_jid);
    359381        tf->streamhandle = bt;
    360382        tf->ft->free = jabber_bs_free_transfer;
     
    451473                        .rsv = 0,
    452474                        .atyp = 0x03,
    453                         .addrlen = strlen(bt->pseudoadr),
     475                        .addrlen = strlen(bt->pseudoaddr),
    454476                        .port = 0
    455477                };
     
    468490
    469491                /* copy hash into connect message */
    470                 memcpy(socks5_connect.address, bt->pseudoadr, socks5_connect.addrlen);
     492                memcpy(socks5_connect.address, bt->pseudoaddr, socks5_connect.addrlen);
    471493
    472494                ASSERTSOCKOP(send(fd, &socks5_connect, sizeof(struct socks5_message), 0), "Sending SOCKS5 Connect");
     
    558580        if (shlist && shlist->next) {
    559581                bt->sh = shlist->next->data;
     582                jabber_bs_remove_events(bt);
    560583                return jabber_bs_recv_handshake(bt, -1, 0);
    561584        }
     
    731754        struct jabber_data *jd = ic->proto_data;
    732755        struct bs_transfer *bt;
    733         GSList *tflist;
    734756        struct xt_node *c;
    735757        char *sid, *jid;
     
    750772        /* Let's see if we can find out what this bytestream should be for... */
    751773
    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))) {
    761775                imcb_log(ic, "WARNING: Received SOCKS5 bytestream reply to unknown request");
    762776                return XT_HANDLED;
     
    776790                /* using a proxy, abort listen */
    777791
    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);
    792793
    793794                GSList *shlist;
     
    838839{
    839840        char *sid;
    840         GSList *tflist;
    841841        struct jabber_transfer *tf = NULL;
    842842        struct xt_node *query;
     
    846846        sid = xt_find_attr(query, "sid");
    847847
    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))) {
    857849                imcb_log(ic, "WARNING: Received SOCKS5 bytestream activation for unknown stream");
    858850                return XT_HANDLED;
     
    883875        *port++ = '\0';
    884876
    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));
    889879
    890880        return sh;
     
    910900                if (strcmp(proxy, "<local>") == 0) {
    911901                        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));
    916904                                bt->streamhosts = g_slist_append(bt->streamhosts, sh);
    917905
     
    948936{
    949937        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         }
    965938
    966939        bt = g_new0(struct bs_transfer, 1);
    967940        bt->tf = tf;
    968941        bt->phase = BS_PHASE_CONNECT;
    969         bt->pseudoadr = g_strdup(hash_hex);
     942        bt->pseudoaddr = generate_pseudoaddr(tf->sid, tf->ini_jid, tf->tgt_jid);
    970943        tf->streamhandle = bt;
    971944        tf->ft->free = jabber_bs_free_transfer;
     
    974947        jabber_si_set_proxies(bt);
    975948
    976         ret = jabber_bs_send_request(tf, bt->streamhosts);
    977 
    978         return ret;
     949        return jabber_bs_send_request(tf, bt->streamhosts);
    979950}
    980951
     
    11391110                                               socks5_connect.atyp);
    11401111                }
    1141                 if (!(memcmp(socks5_connect.address, bt->pseudoadr, 40) == 0)) {
     1112                if (!(memcmp(socks5_connect.address, bt->pseudoaddr, 40) == 0)) {
    11421113                        return jabber_bs_abort(bt, "SOCKS5 Connect message contained wrong digest");
    11431114                }
Note: See TracChangeset for help on using the changeset viewer.