changeset 15900:9cfe41743c65

More complete namespacing of nat-pmp
author Mark Doliner <mark@kingant.net>
date Sun, 25 Mar 2007 00:41:31 +0000
parents 4e44a9eaceec
children ab2f466b6a13
files libpurple/nat-pmp.c libpurple/nat-pmp.h
diffstat 2 files changed, 55 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/nat-pmp.c	Sat Mar 24 23:28:25 2007 +0000
+++ b/libpurple/nat-pmp.c	Sun Mar 25 00:41:31 2007 +0000
@@ -31,14 +31,12 @@
 #include "nat-pmp.h"
 #include "debug.h"
 
-#include <sys/types.h>
+#include <arpa/inet.h>
+#include <net/route.h>
+#include <netinet/in.h>
 #include <sys/socket.h>
 #include <sys/sysctl.h>
-
-#include <net/route.h>
-#include <netinet/in.h>
-
-#include <arpa/inet.h>
+#include <sys/types.h>
 
 #include <netdb.h>
 #include <stdio.h>
@@ -47,13 +45,12 @@
 #include <err.h>
 
 #include <errno.h>
-#include <assert.h>
 #include <sys/types.h>
 #include <net/if.h>
 
 #ifdef NET_RT_DUMP2
 
-#define PMP_DEBUG
+#define PMP_DEBUG 1
 
 /*
  *	Thanks to R. Matthew Emerson for the fixes on this
@@ -243,8 +240,8 @@
 	int sendfd;
 	int req_attempts = 1;	
 	struct timeval req_timeout;
-	pmp_ip_request_t req;
-	pmp_ip_response_t resp;
+	PurplePmpIpRequest req;
+	PurplePmpIpResponse resp;
 	struct sockaddr_in *publicsockaddr = NULL;
 
 	req_timeout.tv_sec = 0;
@@ -253,8 +250,8 @@
 	sendfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
 	
 	//	Clean out both req and resp structures
-	bzero(&req, sizeof(pmp_ip_request_t));
-	bzero(&resp, sizeof(pmp_ip_response_t));
+	bzero(&req, sizeof(PurplePmpIpRequest));
+	bzero(&resp, sizeof(PurplePmpIpResponse));
 	req.version = 0;
 	req.opcode	= 0;
 	
@@ -268,6 +265,7 @@
 		struct sockaddr_in addr;
 		socklen_t len = sizeof(struct sockaddr_in);
 
+		/* TODO: Non-blocking! */
 		if (sendto(sendfd, &req, sizeof(req), 0, (struct sockaddr *)(gateway), sizeof(struct sockaddr)) < 0)
 		{
 			purple_debug_info("nat-pmp", "There was an error sending the NAT-PMP public IP request! (%s)\n", strerror(errno));
@@ -280,7 +278,8 @@
 			return NULL;
 		}		
 		
-		if (recvfrom(sendfd, &resp, sizeof(pmp_ip_response_t), 0, (struct sockaddr *)(&addr), &len) < 0)
+		/* TODO: Non-blocking! */
+		if (recvfrom(sendfd, &resp, sizeof(PurplePmpIpResponse), 0, (struct sockaddr *)(&addr), &len) < 0)
 		{			
 			if ( (errno != EAGAIN) || (req_attempts == 9) )
 			{
@@ -333,9 +332,9 @@
 }
 
 /*!
- *	will return NULL on error, or a pointer to the pmp_map_response_t type
+ *	will return NULL on error, or a pointer to the PurplePmpMapResponse type
  */
-pmp_map_response_t *
+PurplePmpMapResponse *
 purple_pmp_create_map(PurplePmpType type, uint16_t privateport, uint16_t publicport, uint32_t lifetime)
 {
 	struct sockaddr_in *gateway = default_gw();
@@ -353,8 +352,8 @@
 	int sendfd;
 	int req_attempts = 1;	
 	struct timeval req_timeout;
-	pmp_map_request_t req;
-	pmp_map_response_t *resp = (pmp_map_response_t *)(malloc(sizeof(pmp_map_response_t)));
+	PurplePmpMapRequest req;
+	PurplePmpMapResponse *resp = (PurplePmpMapResponse *)(malloc(sizeof(PurplePmpMapResponse)));
 	
 	req_timeout.tv_sec = 0;
 	req_timeout.tv_usec = PMP_TIMEOUT;
@@ -362,8 +361,8 @@
 	sendfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
 	
 	//	Clean out both req and resp structures
-	bzero(&req, sizeof(pmp_map_request_t));
-	bzero(resp, sizeof(pmp_map_response_t));
+	bzero(&req, sizeof(PurplePmpMapRequest));
+	bzero(resp, sizeof(PurplePmpMapResponse));
 	req.version = 0;
 	req.opcode	= ((type == PURPLE_PMP_TYPE_UDP) ? PMP_MAP_OPCODE_UDP : PMP_MAP_OPCODE_TCP);	
 	req.privateport = htons(privateport); //	What a difference byte ordering makes...d'oh!
@@ -378,6 +377,7 @@
 		purple_debug_info("nat-pmp", "\tTimeout: %ds %dus, Request #: %d\n", req_timeout.tv_sec, req_timeout.tv_usec, req_attempts);
 #endif
 
+		/* TODO: Non-blocking! */
 		if (sendto(sendfd, &req, sizeof(req), 0, (struct sockaddr *)(gateway), sizeof(struct sockaddr)) < 0)
 		{
 			purple_debug_info("nat-pmp", "There was an error sending the NAT-PMP mapping request! (%s)\n", strerror(errno));
@@ -390,7 +390,8 @@
 			return NULL;
 		}		
 		
-		if (recvfrom(sendfd, resp, sizeof(pmp_map_response_t), 0, NULL, NULL) < 0)
+		/* TODO: Non-blocking! */
+		if (recvfrom(sendfd, resp, sizeof(PurplePmpMapResponse), 0, NULL, NULL) < 0)
 		{			
 			if ( (errno != EAGAIN) || (req_attempts == 9) )
 			{
@@ -434,12 +435,12 @@
 
 /*!
  *	pmp_destroy_map(uint8_t,uint16_t) 
- *	will return NULL on error, or a pointer to the pmp_map_response_t type
+ *	will return NULL on error, or a pointer to the PurplePmpMapResponse type
  */
-pmp_map_response_t *
+PurplePmpMapResponse *
 purple_pmp_destroy_map(PurplePmpType type, uint16_t privateport)
 {
-	pmp_map_response_t *response;
+	PurplePmpMapResponse *response;
 	
 	response = purple_pmp_create_map(((type == PURPLE_PMP_TYPE_UDP) ? PMP_MAP_OPCODE_UDP : PMP_MAP_OPCODE_TCP),
 							privateport, 0, 0);
@@ -460,13 +461,13 @@
 	return NULL;
 }
 
-pmp_map_response_t *
+PurplePmpMapResponse *
 purple_pmp_create_map(PurplePmpType type, uint16_t privateport, uint16_t publicport, uint32_t lifetime)
 {
 	return NULL;
 }
 
-pmp_map_response_t *
+PurplePmpMapResponse *
 purple_pmp_destroy_map(PurplePmpType type, uint16_t privateport)
 {
 	return NULL;
--- a/libpurple/nat-pmp.h	Sat Mar 24 23:28:25 2007 +0000
+++ b/libpurple/nat-pmp.h	Sun Mar 25 00:41:31 2007 +0000
@@ -1,5 +1,5 @@
 /**
-* @file nat-pmp.h NAT-PMP Implementation
+ * @file nat-pmp.h NAT-PMP Implementation
  * @ingroup core
  *
  * purple
@@ -28,20 +28,12 @@
  * OF SUCH DAMAGE.
  */
 
-#ifndef _PMPMAPPER_H
-#define _PMPMAPPER_H
+#ifndef _PURPLE_NAT_PMP_H
+#define _PURPLE_NAT_PMP_H
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <assert.h>
-#include <sys/socket.h>
-#include <sys/sysctl.h>
-#include <sys/types.h>
-#include <net/if.h>
-#include <net/route.h>
+#include <stdint.h>
 
-#define PURPLE_PMP_LIFETIME		3600	//	3600 seconds
+#define PURPLE_PMP_LIFETIME 3600 /* seconds */
 
 /*
  *	uint8_t:	version, opcodes
@@ -57,7 +49,7 @@
 typedef struct {
 	uint8_t	version;
 	uint8_t opcode;
-} pmp_ip_request_t;
+} PurplePmpIpRequest;
 
 typedef struct {
 	uint8_t		version;
@@ -65,7 +57,7 @@
 	uint16_t	resultcode;
 	uint32_t	epoch;
 	uint32_t	address;
-} pmp_ip_response_t;
+} PurplePmpIpResponse;
 
 typedef struct {
 	uint8_t		version;
@@ -74,7 +66,7 @@
 	uint16_t	privateport;
 	uint16_t	publicport;
 	uint32_t	lifetime;
-} pmp_map_request_t;
+} PurplePmpMapRequest;
 
 typedef struct {
 	uint8_t		version;
@@ -84,10 +76,26 @@
 	uint16_t	privateport;
 	uint16_t	publicport;
 	uint32_t	lifetime;
-} pmp_map_response_t;
+} PurplePmpMapResponse;
 
+/**
+ *
+ */
+/*
+ * TODO: This should probably cache the result of this lookup requests
+ *       so that subsequent calls to this function do not require a
+ *       round-trip exchange with the local router.
+ */
 char *purple_pmp_get_public_ip();
-pmp_map_response_t *purple_pmp_create_map(PurplePmpType type, uint16_t privateport, uint16_t publicport, uint32_t lifetime);
-pmp_map_response_t *purple_pmp_destroy_map(PurplePmpType type, uint16_t privateport);
-	
-#endif
\ No newline at end of file
+
+/**
+ *
+ */
+PurplePmpMapResponse *purple_pmp_create_map(PurplePmpType type, uint16_t privateport, uint16_t publicport, uint32_t lifetime);
+
+/**
+ *
+ */
+PurplePmpMapResponse *purple_pmp_destroy_map(PurplePmpType type, uint16_t privateport);
+
+#endif /* _PURPLE_NAT_PMP_H_ */