Changeset da6f167 for protocols/jabber


Ignore:
Timestamp:
2015-02-22T18:53:21Z (10 years ago)
Author:
dequis <dx@…>
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)
Message:

s5bytestream: refactor some copypasted code into functions

  • get_ft_by_sid()
  • generate_pseudoaddr() (also uses g_compute_checksum_for_string() to make the code shorter)
  • jabber_streamhost_new()

Behavior should be the same.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/jabber/s5bytestream.c

    r90adf84 rda6f167  
    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) {
     
    262261}
    263262
     263static 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 */
     276static 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
     283static 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
    264294/*
    265295 * Parses an incoming bytestream request and calls jabber_bs_handshake on success.
     
    270300        struct jabber_data *jd = ic->proto_data;
    271301        struct jabber_transfer *tf = NULL;
    272         GSList *tflist;
    273302        struct bs_transfer *bt;
    274303        GSList *shlist = NULL;
    275304        struct xt_node *shnode;
    276 
    277         sha1_state_t sha;
    278         char hash_hex[41];
    279         unsigned char hash[20];
    280         int i;
    281305
    282306        if (!(iq_id   = xt_find_attr(node, "id")) ||
     
    303327                    (port_s = xt_find_attr(shnode, "port")) &&
    304328                    (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));
    310330                }
    311331                shnode = shnode->next;
     
    317337        }
    318338
    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))) {
    332340                imcb_log(ic, "WARNING: Received bytestream request from %s that doesn't match an SI request", ini_jid);
    333341                return XT_HANDLED;
     
    339347
    340348        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         }
    352349
    353350        bt = g_new0(struct bs_transfer, 1);
     
    356353        bt->sh = shlist->data;
    357354        bt->phase = BS_PHASE_CONNECT;
    358         bt->pseudoadr = g_strdup(hash_hex);
     355        bt->pseudoaddr = generate_pseudoaddr(sid, ini_jid, tgt_jid);
    359356        tf->streamhandle = bt;
    360357        tf->ft->free = jabber_bs_free_transfer;
     
    451448                        .rsv = 0,
    452449                        .atyp = 0x03,
    453                         .addrlen = strlen(bt->pseudoadr),
     450                        .addrlen = strlen(bt->pseudoaddr),
    454451                        .port = 0
    455452                };
     
    468465
    469466                /* copy hash into connect message */
    470                 memcpy(socks5_connect.address, bt->pseudoadr, socks5_connect.addrlen);
     467                memcpy(socks5_connect.address, bt->pseudoaddr, socks5_connect.addrlen);
    471468
    472469                ASSERTSOCKOP(send(fd, &socks5_connect, sizeof(struct socks5_message), 0), "Sending SOCKS5 Connect");
     
    731728        struct jabber_data *jd = ic->proto_data;
    732729        struct bs_transfer *bt;
    733         GSList *tflist;
    734730        struct xt_node *c;
    735731        char *sid, *jid;
     
    750746        /* Let's see if we can find out what this bytestream should be for... */
    751747
    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))) {
    761749                imcb_log(ic, "WARNING: Received SOCKS5 bytestream reply to unknown request");
    762750                return XT_HANDLED;
     
    838826{
    839827        char *sid;
    840         GSList *tflist;
    841828        struct jabber_transfer *tf = NULL;
    842829        struct xt_node *query;
     
    846833        sid = xt_find_attr(query, "sid");
    847834
    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))) {
    857836                imcb_log(ic, "WARNING: Received SOCKS5 bytestream activation for unknown stream");
    858837                return XT_HANDLED;
     
    883862        *port++ = '\0';
    884863
    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));
    889866
    890867        return sh;
     
    910887                if (strcmp(proxy, "<local>") == 0) {
    911888                        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));
    916891                                bt->streamhosts = g_slist_append(bt->streamhosts, sh);
    917892
     
    948923{
    949924        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         }
    965925
    966926        bt = g_new0(struct bs_transfer, 1);
    967927        bt->tf = tf;
    968928        bt->phase = BS_PHASE_CONNECT;
    969         bt->pseudoadr = g_strdup(hash_hex);
     929        bt->pseudoaddr = generate_pseudoaddr(tf->sid, tf->ini_jid, tf->tgt_jid);
    970930        tf->streamhandle = bt;
    971931        tf->ft->free = jabber_bs_free_transfer;
     
    974934        jabber_si_set_proxies(bt);
    975935
    976         ret = jabber_bs_send_request(tf, bt->streamhosts);
    977 
    978         return ret;
     936        return jabber_bs_send_request(tf, bt->streamhosts);
    979937}
    980938
     
    11391097                                               socks5_connect.atyp);
    11401098                }
    1141                 if (!(memcmp(socks5_connect.address, bt->pseudoadr, 40) == 0)) {
     1099                if (!(memcmp(socks5_connect.address, bt->pseudoaddr, 40) == 0)) {
    11421100                        return jabber_bs_abort(bt, "SOCKS5 Connect message contained wrong digest");
    11431101                }
Note: See TracChangeset for help on using the changeset viewer.