comparison libpurple/protocols/yahoo/yahoo_filexfer.c @ 25058:ccbded331513

Support for P2P file transfer (SEND), when we are client to the peer
author Sulabh Mahajan <sulabh@soc.pidgin.im>
date Sat, 21 Jun 2008 14:34:38 +0000
parents ef47df3e5dd4
children 2fb8c39d0494
comparison
equal deleted inserted replaced
25057:f9f3d8bae136 25058:ccbded331513
1201 { 1201 {
1202 gchar* cookies; 1202 gchar* cookies;
1203 cookies = yahoo_get_cookies(xd->gc); 1203 cookies = yahoo_get_cookies(xd->gc);
1204 if(purple_xfer_get_type(xfer) == PURPLE_XFER_SEND && xd->status_15 == ACCEPTED) 1204 if(purple_xfer_get_type(xfer) == PURPLE_XFER_SEND && xd->status_15 == ACCEPTED)
1205 { 1205 {
1206 xd->txbuf = g_strdup_printf("POST /relay?token=%s&sender=%s&recver=%s HTTP/1.1\r\nCookie:%s\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5)\r\nHost: %s\r\nContent-Length: %ld\r\nCache-Control: no-cache\r\n\r\n", 1206 if(xd->info_val_249 == 2)
1207 {
1208 /*sending file via p2p, we are connected as client*/
1209 xd->txbuf = g_strdup_printf("POST /%s HTTP/1.1\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5)\r\nHost: %s\r\nContent-Length: %ld\r\nCache-Control: no-cache\r\n\r\n",
1210 xd->path,
1211 xd->host,
1212 (long int)xfer->size); /*to do, add Referer*/
1213 }
1214 else
1215 {
1216 /*sending file via relaying*/
1217 xd->txbuf = g_strdup_printf("POST /relay?token=%s&sender=%s&recver=%s HTTP/1.1\r\nCookie:%s\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5)\r\nHost: %s\r\nContent-Length: %ld\r\nCache-Control: no-cache\r\n\r\n",
1207 purple_url_encode(xd->xfer_idstring_for_relay), 1218 purple_url_encode(xd->xfer_idstring_for_relay),
1208 purple_normalize(account, purple_account_get_username(account)), 1219 purple_normalize(account, purple_account_get_username(account)),
1209 xfer->who, 1220 xfer->who,
1210 cookies, 1221 cookies,
1211 xd->host, 1222 xd->host,
1212 (long int)xfer->size); 1223 (long int)xfer->size);
1224 }
1213 } 1225 }
1214 else if(purple_xfer_get_type(xfer) == PURPLE_XFER_RECEIVE && xd->status_15 == STARTED) 1226 else if(purple_xfer_get_type(xfer) == PURPLE_XFER_RECEIVE && xd->status_15 == STARTED)
1215 { 1227 {
1216 if(xd->info_val_249 == 1) /*receiving file via p2p, if xd->info_val_249 is 1*/ 1228 if(xd->info_val_249 == 1)
1217 { 1229 {
1230 /*receiving file via p2p, connected as client*/
1218 xd->txbuf = g_strdup_printf("HEAD /%s HTTP/1.1\r\nAccept:*/*\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5)\r\nHost: %s\r\nContent-Length: 0\r\nCache-Control: no-cache\r\n\r\n",xd->path,xd->host); 1231 xd->txbuf = g_strdup_printf("HEAD /%s HTTP/1.1\r\nAccept:*/*\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5)\r\nHost: %s\r\nContent-Length: 0\r\nCache-Control: no-cache\r\n\r\n",xd->path,xd->host);
1219 } 1232 }
1220 else /*receiving file via relaying*/ 1233 else
1221 { 1234 {
1235 /*receiving file via relaying*/
1222 xd->txbuf = g_strdup_printf("HEAD /relay?token=%s&sender=%s&recver=%s HTTP/1.1\r\nAccept:*/*\r\nCookie:%s\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5)\r\nHost:%s\r\nContent-Length: 0\r\nCache-Control: no-cache\r\n\r\n", 1236 xd->txbuf = g_strdup_printf("HEAD /relay?token=%s&sender=%s&recver=%s HTTP/1.1\r\nAccept:*/*\r\nCookie:%s\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5)\r\nHost:%s\r\nContent-Length: 0\r\nCache-Control: no-cache\r\n\r\n",
1223 purple_url_encode(xd->xfer_idstring_for_relay), 1237 purple_url_encode(xd->xfer_idstring_for_relay),
1224 purple_normalize(account, purple_account_get_username(account)), 1238 purple_normalize(account, purple_account_get_username(account)),
1225 xfer->who, 1239 xfer->who,
1226 cookies, 1240 cookies,
1227 xd->host); 1241 xd->host);
1228 } 1242 }
1229 } 1243 }
1230 else if(purple_xfer_get_type(xfer) == PURPLE_XFER_RECEIVE && xd->status_15 == HEAD_REPLY_RECEIVED) 1244 else if(purple_xfer_get_type(xfer) == PURPLE_XFER_RECEIVE && xd->status_15 == HEAD_REPLY_RECEIVED)
1231 { 1245 {
1232 if(xd->info_val_249 == 1) /*receiving file via p2p*/ 1246 if(xd->info_val_249 == 1)
1233 { 1247 {
1248 /*receiving file via p2p, connected as client*/
1234 xd->txbuf = g_strdup_printf("GET /%s HTTP/1.1\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5)\r\nHost: %s\r\nConnection: Keep-Alive\r\n\r\n",xd->path,xd->host); 1249 xd->txbuf = g_strdup_printf("GET /%s HTTP/1.1\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5)\r\nHost: %s\r\nConnection: Keep-Alive\r\n\r\n",xd->path,xd->host);
1235 } 1250 }
1236 else /*receiving file via relaying*/ 1251 else
1237 { 1252 {
1253 /*receiving file via relaying*/
1238 xd->txbuf = g_strdup_printf("GET /relay?token=%s&sender=%s&recver=%s HTTP/1.1\r\nCookie:%s\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5)\r\nHost:%s\r\nConnection: Keep-Alive\r\n\r\n", 1254 xd->txbuf = g_strdup_printf("GET /relay?token=%s&sender=%s&recver=%s HTTP/1.1\r\nCookie:%s\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5)\r\nHost:%s\r\nConnection: Keep-Alive\r\n\r\n",
1239 purple_url_encode(xd->xfer_idstring_for_relay), 1255 purple_url_encode(xd->xfer_idstring_for_relay),
1240 purple_normalize(account, purple_account_get_username(account)), 1256 purple_normalize(account, purple_account_get_username(account)),
1241 xfer->who, 1257 xfer->who,
1242 cookies, 1258 cookies,
1258 { 1274 {
1259 xd->tx_handler = purple_input_add(source, PURPLE_INPUT_WRITE, 1275 xd->tx_handler = purple_input_add(source, PURPLE_INPUT_WRITE,
1260 yahoo_xfer_send_cb_15, xfer); 1276 yahoo_xfer_send_cb_15, xfer);
1261 yahoo_xfer_send_cb_15(xfer, source, PURPLE_INPUT_WRITE); 1277 yahoo_xfer_send_cb_15(xfer, source, PURPLE_INPUT_WRITE);
1262 } 1278 }
1279 }
1280
1281 /*send (p2p) file transfer information when we are connected as client*/
1282 static void yahoo_p2p_client_send_ft_info(PurpleConnection *gc, PurpleXfer *xfer)
1283 {
1284 struct yahoo_xfer_data *xd;
1285 struct yahoo_packet *pkt;
1286 PurpleAccount *account;
1287 struct yahoo_data *yd;
1288 gchar *filename;
1289
1290 if (!(xd = xfer->data))
1291 return;
1292
1293 account = purple_connection_get_account(gc);
1294 yd = gc->proto_data;
1295
1296 pkt = yahoo_packet_new(YAHOO_SERVICE_FILETRANS_INFO_15, YAHOO_STATUS_AVAILABLE, yd->session_id);
1297 filename = g_path_get_basename(purple_xfer_get_local_filename(xfer));
1298
1299 yahoo_packet_hash(pkt, "ssssi",
1300 1, purple_normalize(account, purple_account_get_username(account)),
1301 5, xfer->who,
1302 265, xd->xfer_peer_idstring,
1303 27, filename,
1304 249, 2); /*249= 2:we are connected as p2p client, and sending file*/
1305 xd->info_val_249 = 2;
1306
1307 g_free(filename);
1308 yahoo_packet_send_and_free(pkt, yd);
1263 } 1309 }
1264 1310
1265 void yahoo_process_filetrans_15(PurpleConnection *gc, struct yahoo_packet *pkt) 1311 void yahoo_process_filetrans_15(PurpleConnection *gc, struct yahoo_packet *pkt)
1266 { 1312 {
1267 char *from = NULL; 1313 char *from = NULL;
1340 * supposed to mention the ip address... 1386 * supposed to mention the ip address...
1341 * purple connect does not give me a way of finding the ip address... 1387 * purple connect does not give me a way of finding the ip address...
1342 * so, purple dnsquery is used... but retries, trying with next ip 1388 * so, purple dnsquery is used... but retries, trying with next ip
1343 * address etc. is not implemented..TODO 1389 * address etc. is not implemented..TODO
1344 */ 1390 */
1391
1392 /*p2p connection exists, we being p2p client*/
1393 if( g_hash_table_lookup(yd->peers, from) ) {
1394 /*send p2p file transfer information when we are connected as client*/
1395 yahoo_p2p_client_send_ft_info(gc, xfer);
1396 return;
1397 }
1398
1345 if (yd->jp) 1399 if (yd->jp)
1346 { 1400 {
1347 purple_dnsquery_a(YAHOOJP_XFER_RELAY_HOST, YAHOOJP_XFER_RELAY_PORT, 1401 purple_dnsquery_a(YAHOOJP_XFER_RELAY_HOST, YAHOOJP_XFER_RELAY_PORT,
1348 yahoo_xfer_dns_connected_15, xfer); 1402 yahoo_xfer_dns_connected_15, xfer);
1349 } 1403 }
1468 case 66: 1522 case 66:
1469 val_66 = strtol(pair->value, NULL, 10); 1523 val_66 = strtol(pair->value, NULL, 10);
1470 break; 1524 break;
1471 case 249: 1525 case 249:
1472 val_249 = strtol(pair->value, NULL, 10); 1526 val_249 = strtol(pair->value, NULL, 10);
1473 /* 249 has value 1 when doing p2p transfer and value 3 when relaying through yahoo server */ 1527 /* 249 has value 1 or 2 when doing p2p transfer and value 3 when relaying through yahoo server */
1474 break; 1528 break;
1475 case 250: 1529 case 250:
1476 url = pair->value; 1530 url = pair->value;
1477 break; 1531 break;
1478 case 251: 1532 case 251:
1535 struct yahoo_data *yd; 1589 struct yahoo_data *yd;
1536 struct yahoo_xfer_data *xfer_data; 1590 struct yahoo_xfer_data *xfer_data;
1537 GSList *l; 1591 GSList *l;
1538 PurpleAccount *account; 1592 PurpleAccount *account;
1539 long val_66 = 0; 1593 long val_66 = 0;
1594 gchar *url = NULL;
1595 int val_249;
1540 1596
1541 yd = gc->proto_data; 1597 yd = gc->proto_data;
1542 for (l = pkt->hash; l; l = l->next) { 1598 for (l = pkt->hash; l; l = l->next) {
1543 struct yahoo_pair *pair = l->data; 1599 struct yahoo_pair *pair = l->data;
1544 1600
1549 case 265: 1605 case 265:
1550 xfer_peer_idstring = pair->value; 1606 xfer_peer_idstring = pair->value;
1551 break; 1607 break;
1552 case 66: 1608 case 66:
1553 val_66 = atol(pair->value); 1609 val_66 = atol(pair->value);
1610 break;
1611 case 249:
1612 val_249 = atol(pair->value);
1613 break;
1614 case 250:
1615 url = pair->value; /*we get a p2p url here when sending file, connected as client*/
1616 break;
1554 } 1617 }
1555 } 1618 }
1556 1619
1557 xfer = g_hash_table_lookup(yd->xfer_peer_idstring_map, xfer_peer_idstring); 1620 xfer = g_hash_table_lookup(yd->xfer_peer_idstring_map, xfer_peer_idstring);
1558 if(!xfer) return; 1621 if(!xfer) return;
1559 1622
1560 if(val_66 == -1 || !(xfer_idstring_for_relay)) 1623 if(val_66 == -1 || ( (!(xfer_idstring_for_relay)) && (val_249 != 2) ))
1561 { 1624 {
1562 purple_xfer_cancel_remote(xfer); 1625 purple_xfer_cancel_remote(xfer);
1563 return; 1626 return;
1564 } 1627 }
1565 1628
1629 if( (val_249 == 2) && (!(url)) )
1630 {
1631 purple_xfer_cancel_remote(xfer);
1632 return;
1633 }
1634
1566 xfer_data = xfer->data; 1635 xfer_data = xfer->data;
1636 if(url)
1637 purple_url_parse(url, &(xfer_data->host), &(xfer_data->port), &(xfer_data->path), NULL, NULL);
1638
1567 xfer_data->xfer_idstring_for_relay = g_strdup(xfer_idstring_for_relay); 1639 xfer_data->xfer_idstring_for_relay = g_strdup(xfer_idstring_for_relay);
1568 xfer_data->status_15 = ACCEPTED; 1640 xfer_data->status_15 = ACCEPTED;
1569 account = purple_connection_get_account(gc); 1641 account = purple_connection_get_account(gc);
1570 1642
1571 if (purple_proxy_connect(NULL, account, xfer_data->host, xfer_data->port, 1643 if (purple_proxy_connect(NULL, account, xfer_data->host, xfer_data->port,