Index: protocols/jabber/s5bytestream.c
===================================================================
--- protocols/jabber/s5bytestream.c	(revision 5ebff60479fc7a9f7f50ac03b124c91d4e6ebe11)
+++ protocols/jabber/s5bytestream.c	(revision da6f167b1df952f8d7b9362bfb8adc1d91e8c9c4)
@@ -23,5 +23,4 @@
 
 #include "jabber.h"
-#include "sha1.h"
 #include "lib/ftutil.h"
 #include <poll.h>
@@ -42,5 +41,5 @@
 
 	/* SHA1( SID + Initiator JID + Target JID) */
-	char *pseudoadr;
+	char *pseudoaddr;
 
 	gint connect_timeout;
@@ -130,5 +129,5 @@
 	}
 
-	g_free(bt->pseudoadr);
+	g_free(bt->pseudoaddr);
 
 	while (bt->streamhosts) {
@@ -262,4 +261,35 @@
 }
 
+static struct jabber_transfer *get_ft_by_sid(GSList *tflist, char *sid) {
+	GSList *l;
+	for (l = tflist; l; l = g_slist_next(l)) {
+		struct jabber_transfer *tft = l->data;
+		if ((strcmp(tft->sid, sid) == 0)) { 
+			return tft;
+		}
+	}
+	return NULL;
+}
+
+/* SHA1( SID + Initiator JID + Target JID ) is given to the streamhost which it will match against the initiator's value
+ * Returns a newly allocated string */
+static char *generate_pseudoaddr(char *sid, char *ini_jid, char *tgt_jid) {
+	char *contents = g_strconcat(sid, ini_jid, tgt_jid, NULL);
+	char *hash_hex = g_compute_checksum_for_string(G_CHECKSUM_SHA1, contents, -1);
+	g_free(contents);
+	return hash_hex;
+}
+
+static jabber_streamhost_t *jabber_streamhost_new(char *jid, char *host, int port)
+{
+	jabber_streamhost_t *sh = g_new0(jabber_streamhost_t, 1);
+	sh->jid = g_strdup(jid);
+	sh->host = g_strdup(host);
+	if (port) {
+		g_snprintf(sh->port, sizeof(sh->port), "%u", port);
+	}
+	return sh;
+}
+
 /*
  * Parses an incoming bytestream request and calls jabber_bs_handshake on success.
@@ -270,13 +300,7 @@
 	struct jabber_data *jd = ic->proto_data;
 	struct jabber_transfer *tf = NULL;
-	GSList *tflist;
 	struct bs_transfer *bt;
 	GSList *shlist = NULL;
 	struct xt_node *shnode;
-
-	sha1_state_t sha;
-	char hash_hex[41];
-	unsigned char hash[20];
-	int i;
 
 	if (!(iq_id   = xt_find_attr(node, "id")) ||
@@ -303,9 +327,5 @@
 		    (port_s = xt_find_attr(shnode, "port")) &&
 		    (sscanf(port_s, "%d", &port) == 1)) {
-			jabber_streamhost_t *sh = g_new0(jabber_streamhost_t, 1);
-			sh->jid = g_strdup(jid);
-			sh->host = g_strdup(host);
-			sprintf(sh->port, "%u", port);
-			shlist = g_slist_append(shlist, sh);
+			shlist = g_slist_append(shlist, jabber_streamhost_new(jid, host, port));
 		}
 		shnode = shnode->next;
@@ -317,17 +337,5 @@
 	}
 
-	/* Let's see if we can find out what this bytestream should be for... */
-
-	for (tflist = jd->filetransfers; tflist; tflist = g_slist_next(tflist)) {
-		struct jabber_transfer *tft = tflist->data;
-		if ((strcmp(tft->sid, sid) == 0) &&
-		    (strcmp(tft->ini_jid, ini_jid) == 0) &&
-		    (strcmp(tft->tgt_jid, tgt_jid) == 0)) {
-			tf = tft;
-			break;
-		}
-	}
-
-	if (!tf) {
+	if (!(tf = get_ft_by_sid(jd->filetransfers, sid))) {
 		imcb_log(ic, "WARNING: Received bytestream request from %s that doesn't match an SI request", ini_jid);
 		return XT_HANDLED;
@@ -339,15 +347,4 @@
 
 	tf->ft->canceled = jabber_bs_canceled;
-
-	/* SHA1( SID + Initiator JID + Target JID ) is given to the streamhost which it will match against the initiator's value */
-	sha1_init(&sha);
-	sha1_append(&sha, (unsigned char *) sid, strlen(sid));
-	sha1_append(&sha, (unsigned char *) ini_jid, strlen(ini_jid));
-	sha1_append(&sha, (unsigned char *) tgt_jid, strlen(tgt_jid));
-	sha1_finish(&sha, hash);
-
-	for (i = 0; i < 20; i++) {
-		sprintf(hash_hex + i * 2, "%02x", hash[i]);
-	}
 
 	bt = g_new0(struct bs_transfer, 1);
@@ -356,5 +353,5 @@
 	bt->sh = shlist->data;
 	bt->phase = BS_PHASE_CONNECT;
-	bt->pseudoadr = g_strdup(hash_hex);
+	bt->pseudoaddr = generate_pseudoaddr(sid, ini_jid, tgt_jid);
 	tf->streamhandle = bt;
 	tf->ft->free = jabber_bs_free_transfer;
@@ -451,5 +448,5 @@
 			.rsv = 0,
 			.atyp = 0x03,
-			.addrlen = strlen(bt->pseudoadr),
+			.addrlen = strlen(bt->pseudoaddr),
 			.port = 0
 		};
@@ -468,5 +465,5 @@
 
 		/* copy hash into connect message */
-		memcpy(socks5_connect.address, bt->pseudoadr, socks5_connect.addrlen);
+		memcpy(socks5_connect.address, bt->pseudoaddr, socks5_connect.addrlen);
 
 		ASSERTSOCKOP(send(fd, &socks5_connect, sizeof(struct socks5_message), 0), "Sending SOCKS5 Connect");
@@ -731,5 +728,4 @@
 	struct jabber_data *jd = ic->proto_data;
 	struct bs_transfer *bt;
-	GSList *tflist;
 	struct xt_node *c;
 	char *sid, *jid;
@@ -750,13 +746,5 @@
 	/* Let's see if we can find out what this bytestream should be for... */
 
-	for (tflist = jd->filetransfers; tflist; tflist = g_slist_next(tflist)) {
-		struct jabber_transfer *tft = tflist->data;
-		if ((strcmp(tft->sid, sid) == 0)) {
-			tf = tft;
-			break;
-		}
-	}
-
-	if (!tf) {
+	if (!(tf = get_ft_by_sid(jd->filetransfers, sid))) {
 		imcb_log(ic, "WARNING: Received SOCKS5 bytestream reply to unknown request");
 		return XT_HANDLED;
@@ -838,5 +826,4 @@
 {
 	char *sid;
-	GSList *tflist;
 	struct jabber_transfer *tf = NULL;
 	struct xt_node *query;
@@ -846,13 +833,5 @@
 	sid = xt_find_attr(query, "sid");
 
-	for (tflist = jd->filetransfers; tflist; tflist = g_slist_next(tflist)) {
-		struct jabber_transfer *tft = tflist->data;
-		if ((strcmp(tft->sid, sid) == 0)) {
-			tf = tft;
-			break;
-		}
-	}
-
-	if (!tf) {
+	if (!(tf = get_ft_by_sid(jd->filetransfers, sid))) {
 		imcb_log(ic, "WARNING: Received SOCKS5 bytestream activation for unknown stream");
 		return XT_HANDLED;
@@ -883,8 +862,6 @@
 	*port++ = '\0';
 
-	sh = g_new0(jabber_streamhost_t, 1);
-	sh->jid = g_strdup(jid);
-	sh->host = g_strdup(host);
-	g_snprintf(sh->port, sizeof(sh->port), "%s", port);
+	sh = jabber_streamhost_new(jid, host, 0);
+	strncpy(sh->port, port, sizeof(sh->port));
 
 	return sh;
@@ -910,8 +887,6 @@
 		if (strcmp(proxy, "<local>") == 0) {
 			if ((tf->fd = ft_listen(&tf->saddr, host, port, jd->fd, FALSE, &errmsg)) != -1) {
-				sh = g_new0(jabber_streamhost_t, 1);
-				sh->jid = g_strdup(tf->ini_jid);
-				sh->host = g_strdup(host);
-				g_snprintf(sh->port, sizeof(sh->port), "%s", port);
+				sh = jabber_streamhost_new(tf->ini_jid, host, 0);
+				strncpy(sh->port, port, sizeof(sh->port));
 				bt->streamhosts = g_slist_append(bt->streamhosts, sh);
 
@@ -948,24 +923,9 @@
 {
 	struct bs_transfer *bt;
-	sha1_state_t sha;
-	char hash_hex[41];
-	unsigned char hash[20];
-	int i, ret;
-
-	/* SHA1( SID + Initiator JID + Target JID ) is given to the streamhost which it will match against the initiator's value */
-	sha1_init(&sha);
-	sha1_append(&sha, (unsigned char *) tf->sid, strlen(tf->sid));
-	sha1_append(&sha, (unsigned char *) tf->ini_jid, strlen(tf->ini_jid));
-	sha1_append(&sha, (unsigned char *) tf->tgt_jid, strlen(tf->tgt_jid));
-	sha1_finish(&sha, hash);
-
-	for (i = 0; i < 20; i++) {
-		sprintf(hash_hex + i * 2, "%02x", hash[i]);
-	}
 
 	bt = g_new0(struct bs_transfer, 1);
 	bt->tf = tf;
 	bt->phase = BS_PHASE_CONNECT;
-	bt->pseudoadr = g_strdup(hash_hex);
+	bt->pseudoaddr = generate_pseudoaddr(tf->sid, tf->ini_jid, tf->tgt_jid);
 	tf->streamhandle = bt;
 	tf->ft->free = jabber_bs_free_transfer;
@@ -974,7 +934,5 @@
 	jabber_si_set_proxies(bt);
 
-	ret = jabber_bs_send_request(tf, bt->streamhosts);
-
-	return ret;
+	return jabber_bs_send_request(tf, bt->streamhosts);
 }
 
@@ -1139,5 +1097,5 @@
 			                       socks5_connect.atyp);
 		}
-		if (!(memcmp(socks5_connect.address, bt->pseudoadr, 40) == 0)) {
+		if (!(memcmp(socks5_connect.address, bt->pseudoaddr, 40) == 0)) {
 			return jabber_bs_abort(bt, "SOCKS5 Connect message contained wrong digest");
 		}
