comparison src/upnp.c @ 11492:18c6a6659770

[gaim-migrate @ 13735] <nosnilmot> garimbo: spell RECIEVE <garimbo> Possible spellings for RECIEVE: RECEIVE RECEIVER RECIFE RELIEVE RECEIVED RECEIVES REVIVE REEVE RESERVE DECEIVE RELIEVER RECEDE RECEIVERS RECIPE RECITE RELIVE (not critical, but it was bugging me) a compile warning fix sneaked in here too committer: Tailor Script <tailor@pidgin.im>
author Stu Tomlinson <stu@nosnilmot.com>
date Sat, 10 Sep 2005 18:42:32 +0000
parents d9d60002065b
children 7897207b522d
comparison
equal deleted inserted replaced
11491:26791d1e69ff 11492:18c6a6659770
54 ****************************************************************/ 54 ****************************************************************/
55 #define HTTP_OK "200 OK" 55 #define HTTP_OK "200 OK"
56 #define DEFAULT_HTTP_PORT 80 56 #define DEFAULT_HTTP_PORT 80
57 #define MAX_PORT_SIZE 6 57 #define MAX_PORT_SIZE 6
58 #define SIZEOF_HTTP 7 /* size of "http://" string */ 58 #define SIZEOF_HTTP 7 /* size of "http://" string */
59 #define RECIEVE_TIMEOUT 10000 59 #define RECEIVE_TIMEOUT 10000
60 #define CONSECUTIVE_RECIEVE_TIMEOUT 500 60 #define CONSECUTIVE_RECEIVE_TIMEOUT 500
61 #define DISCOVERY_TIMEOUT 1000 61 #define DISCOVERY_TIMEOUT 1000
62 62
63 63
64 /*************************************************************** 64 /***************************************************************
65 ** Discovery/Description Defines * 65 ** Discovery/Description Defines *
79 "MAN: \"ssdp:discover\"\r\n" \ 79 "MAN: \"ssdp:discover\"\r\n" \
80 "ST: urn:schemas-upnp-org:service:" \ 80 "ST: urn:schemas-upnp-org:service:" \
81 "%s\r\n" \ 81 "%s\r\n" \
82 "\r\n" 82 "\r\n"
83 83
84 #define MAX_DISCOVERY_RECIEVE_SIZE 400 84 #define MAX_DISCOVERY_RECEIVE_SIZE 400
85 #define MAX_DESCRIPTION_RECIEVE_SIZE 7000 85 #define MAX_DESCRIPTION_RECEIVE_SIZE 7000
86 #define MAX_DESCRIPTION_HTTP_HEADER_SIZE 100 86 #define MAX_DESCRIPTION_HTTP_HEADER_SIZE 100
87 87
88 88
89 /****************************************************************** 89 /******************************************************************
90 ** Action Defines * 90 ** Action Defines *
161 int sizeRecv; 161 int sizeRecv;
162 extern int errno; 162 extern int errno;
163 NetResponseData* nrd = data; 163 NetResponseData* nrd = data;
164 164
165 sizeRecv = recv(sock, &(nrd->recvBuffer[nrd->totalSizeRecv]), 165 sizeRecv = recv(sock, &(nrd->recvBuffer[nrd->totalSizeRecv]),
166 MAX_DESCRIPTION_RECIEVE_SIZE-nrd->totalSizeRecv, 0); 166 MAX_DESCRIPTION_RECEIVE_SIZE-nrd->totalSizeRecv, 0);
167 if(sizeRecv < 0 && errno != EINTR) { 167 if(sizeRecv < 0 && errno != EINTR) {
168 gaim_debug_error("upnp", 168 gaim_debug_error("upnp",
169 "gaim_upnp_http_read(): recv < 0: %i!\n\n", errno); 169 "gaim_upnp_http_read(): recv < 0: %i!\n\n", errno);
170 g_free(nrd->recvBuffer); 170 g_free(nrd->recvBuffer);
171 nrd->recvBuffer = NULL; 171 nrd->recvBuffer = NULL;
191 } 191 }
192 nrd->done = TRUE; 192 nrd->done = TRUE;
193 } else { 193 } else {
194 gaim_timeout_remove(nrd->tima); 194 gaim_timeout_remove(nrd->tima);
195 gaim_input_remove(nrd->inpa); 195 gaim_input_remove(nrd->inpa);
196 nrd->tima = gaim_timeout_add(CONSECUTIVE_RECIEVE_TIMEOUT, 196 nrd->tima = gaim_timeout_add(CONSECUTIVE_RECEIVE_TIMEOUT,
197 (GSourceFunc)gaim_upnp_timeout, nrd); 197 (GSourceFunc)gaim_upnp_timeout, nrd);
198 nrd->inpa = gaim_input_add(sock, GAIM_INPUT_READ, 198 nrd->inpa = gaim_input_add(sock, GAIM_INPUT_READ,
199 gaim_upnp_http_read, nrd); 199 gaim_upnp_http_read, nrd);
200 } 200 }
201 } 201 }
225 sizeSent = 0; 225 sizeSent = 0;
226 } 226 }
227 totalSizeSent += sizeSent; 227 totalSizeSent += sizeSent;
228 } 228 }
229 229
230 nrd->tima = gaim_timeout_add(RECIEVE_TIMEOUT, 230 nrd->tima = gaim_timeout_add(RECEIVE_TIMEOUT,
231 (GSourceFunc)gaim_upnp_timeout, nrd); 231 (GSourceFunc)gaim_upnp_timeout, nrd);
232 nrd->inpa = gaim_input_add(sock, GAIM_INPUT_READ, 232 nrd->inpa = gaim_input_add(sock, GAIM_INPUT_READ,
233 gaim_upnp_http_read, nrd); 233 gaim_upnp_http_read, nrd);
234 while (!nrd->done) { 234 while (!nrd->done) {
235 gtk_main_iteration(); 235 gtk_main_iteration();
243 gchar* httpRequest) 243 gchar* httpRequest)
244 { 244 {
245 gchar* recvBuffer; 245 gchar* recvBuffer;
246 NetResponseData* nrd = (NetResponseData*)g_malloc0(sizeof(NetResponseData)); 246 NetResponseData* nrd = (NetResponseData*)g_malloc0(sizeof(NetResponseData));
247 nrd->sendBuffer = httpRequest; 247 nrd->sendBuffer = httpRequest;
248 nrd->recvBuffer = (gchar*)g_malloc(MAX_DESCRIPTION_RECIEVE_SIZE); 248 nrd->recvBuffer = (gchar*)g_malloc(MAX_DESCRIPTION_RECEIVE_SIZE);
249 249
250 nrd->tima = gaim_timeout_add(RECIEVE_TIMEOUT, 250 nrd->tima = gaim_timeout_add(RECEIVE_TIMEOUT,
251 (GSourceFunc)gaim_upnp_timeout, nrd); 251 (GSourceFunc)gaim_upnp_timeout, nrd);
252 252
253 if(gaim_proxy_connect(NULL, address, port, 253 if(gaim_proxy_connect(NULL, address, port,
254 gaim_upnp_http_send, nrd)) { 254 gaim_upnp_http_send, nrd)) {
255 255
575 gaim_timeout_remove(nrd->tima); 575 gaim_timeout_remove(nrd->tima);
576 length = sizeof(struct sockaddr_in); 576 length = sizeof(struct sockaddr_in);
577 577
578 do { 578 do {
579 sizeRecv = recvfrom(sock, nrd->recvBuffer, 579 sizeRecv = recvfrom(sock, nrd->recvBuffer,
580 MAX_DISCOVERY_RECIEVE_SIZE, 0, 580 MAX_DISCOVERY_RECEIVE_SIZE, 0,
581 (struct sockaddr*)&from, &length); 581 (struct sockaddr*)&from, &length);
582 582
583 if(sizeRecv > 0) { 583 if(sizeRecv > 0) {
584 nrd->recvBuffer[sizeRecv] = '\0'; 584 nrd->recvBuffer[sizeRecv] = '\0';
585 }else if(errno != EINTR) { 585 }else if(errno != EINTR) {
610 gchar wanIP[] = "WANIPConnection:1"; 610 gchar wanIP[] = "WANIPConnection:1";
611 gchar wanPPP[] = "WANPPPConnection:1"; 611 gchar wanPPP[] = "WANPPPConnection:1";
612 gchar* serviceToUse; 612 gchar* serviceToUse;
613 gchar* sendMessage = NULL; 613 gchar* sendMessage = NULL;
614 614
615 /* UDP RECIEVE VARIABLES */ 615 /* UDP RECEIVE VARIABLES */
616 GaimUPnPControlInfo* controlInfo = g_malloc(sizeof(GaimUPnPControlInfo)); 616 GaimUPnPControlInfo* controlInfo = g_malloc(sizeof(GaimUPnPControlInfo));
617 NetResponseData* nrd = g_malloc(sizeof(NetResponseData)); 617 NetResponseData* nrd = g_malloc(sizeof(NetResponseData));
618 618
619 /* Set up the sockets */ 619 /* Set up the sockets */
620 sock = socket(AF_INET, SOCK_DGRAM, 0); 620 sock = socket(AF_INET, SOCK_DGRAM, 0);
663 } else { 663 } else {
664 serviceToUse = wanPPP; 664 serviceToUse = wanPPP;
665 } 665 }
666 sendMessage = g_strdup_printf(SEARCH_REQUEST_STRING, serviceToUse); 666 sendMessage = g_strdup_printf(SEARCH_REQUEST_STRING, serviceToUse);
667 667
668 nrd->recvBuffer = (char*)g_malloc(MAX_DISCOVERY_RECIEVE_SIZE); 668 nrd->recvBuffer = (char*)g_malloc(MAX_DISCOVERY_RECEIVE_SIZE);
669 669
670 while(totalSizeSent < strlen(sendMessage)) { 670 while(totalSizeSent < strlen(sendMessage)) {
671 sizeSent = sendto(sock,(void*)&sendMessage[totalSizeSent], 671 sizeSent = sendto(sock,(void*)&sendMessage[totalSizeSent],
672 strlen(&sendMessage[totalSizeSent]),0, 672 strlen(&sendMessage[totalSizeSent]),0,
673 (struct sockaddr*)&server, 673 (struct sockaddr*)&server,
862 nrd->done = TRUE; 862 nrd->done = TRUE;
863 863
864 close(sock); 864 close(sock);
865 } 865 }
866 866
867 static const gchar* 867 static gchar*
868 gaim_upnp_get_local_ip_address(const gchar* address) 868 gaim_upnp_get_local_ip_address(const gchar* address)
869 { 869 {
870 const gchar* ip; 870 gchar* ip;
871 gchar* pathOfControl; 871 gchar* pathOfControl;
872 gchar* addressOfControl; 872 gchar* addressOfControl;
873 int port = 0; 873 int port = 0;
874 NetResponseData* nrd = (NetResponseData*)g_malloc0(sizeof(NetResponseData)); 874 NetResponseData* nrd = (NetResponseData*)g_malloc0(sizeof(NetResponseData));
875 875
881 } 881 }
882 if(port == 0 || port == -1) { 882 if(port == 0 || port == -1) {
883 port = DEFAULT_HTTP_PORT; 883 port = DEFAULT_HTTP_PORT;
884 } 884 }
885 885
886 nrd->tima = gaim_timeout_add(RECIEVE_TIMEOUT, 886 nrd->tima = gaim_timeout_add(RECEIVE_TIMEOUT,
887 (GSourceFunc)gaim_upnp_timeout, nrd); 887 (GSourceFunc)gaim_upnp_timeout, nrd);
888 888
889 if(gaim_proxy_connect(NULL, addressOfControl, port, 889 if(gaim_proxy_connect(NULL, addressOfControl, port,
890 gaim_upnp_get_local_system_ip, nrd)) { 890 gaim_upnp_get_local_system_ip, nrd)) {
891 891
915 const gchar* protocol) 915 const gchar* protocol)
916 { 916 {
917 gchar* httpResponse; 917 gchar* httpResponse;
918 gchar actionName[] = "AddPortMapping"; 918 gchar actionName[] = "AddPortMapping";
919 gchar* actionParams; 919 gchar* actionParams;
920 const gchar* internalIP; 920 gchar* internalIP;
921 921
922 /* get the internal IP */ 922 /* get the internal IP */
923 if((internalIP = gaim_upnp_get_local_ip_address(controlInfo->controlURL)) 923 if((internalIP = gaim_upnp_get_local_ip_address(controlInfo->controlURL))
924 == NULL) { 924 == NULL) {
925 gaim_debug_error("upnp", 925 gaim_debug_error("upnp",
936 actionParams); 936 actionParams);
937 if(httpResponse == NULL) { 937 if(httpResponse == NULL) {
938 gaim_debug_error("upnp", 938 gaim_debug_error("upnp",
939 "gaim_upnp_set_port_mapping(): Failed In httpResponse\n\n"); 939 "gaim_upnp_set_port_mapping(): Failed In httpResponse\n\n");
940 g_free(actionParams); 940 g_free(actionParams);
941 g_free(internalIP);
942 return FALSE;
943 }
944
945 /* determine if port mapping was a success */
946 if(strstr(httpResponse, HTTP_OK) == NULL) {
947 gaim_debug_error("upnp",
948 "gaim_upnp_set_port_mapping(): Failed HTTP_OK\n\n%s\n\n", httpResponse);
949 g_free(actionParams);
950 g_free(httpResponse);
951 g_free(internalIP);
952 return FALSE;
953 }
954
955 g_free(actionParams);
956 g_free(httpResponse);
957
958 gaim_debug_info("upnp", "NAT Added Port Forward On Port: %d: To IP: %s\n", portMap, internalIP);
959 g_free(internalIP);
960 return TRUE;
961 }
962
963
964 gboolean
965 gaim_upnp_remove_port_mapping(const GaimUPnPControlInfo* controlInfo,
966 unsigned short portMap,
967 const char* protocol)
968 {
969 gchar* httpResponse;
970 gchar actionName[] = "DeletePortMapping";
971 gchar* actionParams;
972
973 /* make the portMappingParams variable */
974 actionParams = g_strdup_printf(DELETE_PORT_MAPPING_PARAMS,
975 portMap, protocol);
976
977 httpResponse = gaim_upnp_generate_action_message_and_send(controlInfo,
978 actionName,
979 actionParams);
980
981 if(httpResponse == NULL) {
982 gaim_debug_error("upnp",
983 "gaim_upnp_remove_port_mapping(): Failed In httpResponse\n\n");
984 g_free(actionParams);
941 return FALSE; 985 return FALSE;
942 } 986 }
943 987
944 /* determine if port mapping was a success */ 988 /* determine if port mapping was a success */
945 if(strstr(httpResponse, HTTP_OK) == NULL) { 989 if(strstr(httpResponse, HTTP_OK) == NULL) {
951 } 995 }
952 996
953 g_free(actionParams); 997 g_free(actionParams);
954 g_free(httpResponse); 998 g_free(httpResponse);
955 999
956 gaim_debug_info("upnp", "NAT Added Port Forward On Port: %d: To IP: %s\n", portMap, internalIP);
957 return TRUE;
958 }
959
960
961 gboolean
962 gaim_upnp_remove_port_mapping(const GaimUPnPControlInfo* controlInfo,
963 unsigned short portMap,
964 const char* protocol)
965 {
966 gchar* httpResponse;
967 gchar actionName[] = "DeletePortMapping";
968 gchar* actionParams;
969
970 /* make the portMappingParams variable */
971 actionParams = g_strdup_printf(DELETE_PORT_MAPPING_PARAMS,
972 portMap, protocol);
973
974 httpResponse = gaim_upnp_generate_action_message_and_send(controlInfo,
975 actionName,
976 actionParams);
977
978 if(httpResponse == NULL) {
979 gaim_debug_error("upnp",
980 "gaim_upnp_remove_port_mapping(): Failed In httpResponse\n\n");
981 g_free(actionParams);
982 return FALSE;
983 }
984
985 /* determine if port mapping was a success */
986 if(strstr(httpResponse, HTTP_OK) == NULL) {
987 gaim_debug_error("upnp",
988 "gaim_upnp_set_port_mapping(): Failed HTTP_OK\n\n%s\n\n", httpResponse);
989 g_free(actionParams);
990 g_free(httpResponse);
991 return FALSE;
992 }
993
994 g_free(actionParams);
995 g_free(httpResponse);
996
997 gaim_debug_info("upnp", "NAT Removed Port Forward On Port: %d\n", portMap); 1000 gaim_debug_info("upnp", "NAT Removed Port Forward On Port: %d\n", portMap);
998 return TRUE; 1001 return TRUE;
999 } 1002 }