aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Willi <martin@strongswan.org>2005-11-07 09:11:58 +0000
committerMartin Willi <martin@strongswan.org>2005-11-07 09:11:58 +0000
commit9317c2a1c68c987eb34e499762e95e9d81f09beb (patch)
tree49dab900b2bd5664b08f121554f64526094dd067
parente15f7292ec4663977c1ee0414634ca738949a087 (diff)
downloadstrongswan-9317c2a1c68c987eb34e499762e95e9d81f09beb.tar.bz2
strongswan-9317c2a1c68c987eb34e499762e95e9d81f09beb.tar.xz
- revised packet and socket interface
- tested
-rw-r--r--Source/charon/packet.c52
-rw-r--r--Source/charon/packet.h25
-rw-r--r--Source/charon/socket.c21
-rw-r--r--Source/charon/socket.h2
-rw-r--r--Source/charon/tests/send_queue_test.c2
-rw-r--r--Source/charon/tests/socket_test.c31
6 files changed, 94 insertions, 39 deletions
diff --git a/Source/charon/packet.c b/Source/charon/packet.c
index a34a16919..f67d7a6e4 100644
--- a/Source/charon/packet.c
+++ b/Source/charon/packet.c
@@ -23,8 +23,6 @@
#include "packet.h"
-
-
static status_t destroy(packet_t *this)
{
pfree(this->data.ptr);
@@ -32,15 +30,59 @@ static status_t destroy(packet_t *this)
return SUCCESS;
}
+/**
+ * @brief helper function to set address used by set_dest & set_source
+ */
+static status_t set_addr(int family, struct sockaddr *saddr, char *address, u_int16_t port)
+{
+ switch (family)
+ {
+ /* IPv4 */
+ case AF_INET:
+ {
+ struct sockaddr_in *sin = (struct sockaddr_in*)saddr;
+ sin->sin_family = AF_INET;
+ sin->sin_addr.s_addr = inet_addr("127.0.0.1");
+ sin->sin_port = htons(port);
+ return SUCCESS;;
+ }
+ }
+ return NOT_SUPPORTED;
+}
+
+status_t set_destination(packet_t *this, char *address, u_int16_t port)
+{
+ struct sockaddr *saddr = &(this->destination);
+ return set_addr(this->family, saddr, address, port);
+}
+
+status_t set_source(packet_t *this, char *address, u_int16_t port)
+{
+ struct sockaddr *saddr = &(this->source);
+ return set_addr(this->family, saddr, address, port);
+}
+
-packet_t *packet_create()
+packet_t *packet_create(int family)
{
packet_t *this = alloc_thing(packet_t, "packet_t");
this->destroy = destroy;
-
+ this->set_destination = set_destination;
+ this->set_source = set_source;
+
+ this->family = family;
+ switch (family)
+ {
+ case AF_INET:
+ this->sockaddr_len = sizeof(struct sockaddr_in);
+ break;
+ default: /* not supported */
+ pfree(this);
+ return NULL;
+ }
+
this->data.len = 0;
this->data.ptr = NULL;
return this;
-
}
diff --git a/Source/charon/packet.h b/Source/charon/packet.h
index b5e73e095..c56e859b2 100644
--- a/Source/charon/packet.h
+++ b/Source/charon/packet.h
@@ -42,26 +42,29 @@
typedef struct packet_s packet_t;
struct packet_s {
/**
- * senders address and port
+ * Address family, such as AF_INET
*/
- struct {
- struct sockaddr_in addr;
- size_t len;
- } sender;
+ int family;
+ size_t sockaddr_len;
+
+ /**
+ * source address structure
+ */
+ struct sockaddr source;
/**
- * receivers address and port
+ * destination address structure
*/
- struct {
- struct sockaddr_in addr;
- size_t len;
- } receiver;
+ struct sockaddr destination;
/**
* message data
*/
chunk_t data;
+ status_t (*set_destination) (packet_t *packet, char *address, u_int16_t port);
+ status_t (*set_source) (packet_t *packet, char *address, u_int16_t port);
+
/**
* @brief
*
@@ -77,6 +80,6 @@ struct packet_s {
* @param
* @return
*/
-packet_t *packet_create();
+packet_t *packet_create(int family);
#endif /*PACKET_H_*/
diff --git a/Source/charon/socket.c b/Source/charon/socket.c
index ba1d8d838..3180c23c1 100644
--- a/Source/charon/socket.c
+++ b/Source/charon/socket.c
@@ -52,12 +52,13 @@ status_t receiver(private_socket_t *this, packet_t **packet)
{
char buffer[MAX_PACKET];
- packet_t *pkt = packet_create();
+
+ packet_t *pkt = packet_create(AF_INET);
/* do the read */
- pkt->sender.len = sizeof(pkt->sender.addr);
pkt->data.len = recvfrom(this->socket_fd, buffer, MAX_PACKET, 0,
- &(pkt->sender.addr), &(pkt->sender.len));
+ &(pkt->source), &(pkt->sockaddr_len));
+
if (pkt->data.len < 0)
{
pkt->destroy(pkt);
@@ -78,14 +79,13 @@ status_t sender(private_socket_t *this, packet_t *packet)
{
ssize_t bytes_sent;
- printf("@%d\n", __LINE__);
/* send data */
bytes_sent = sendto(this->socket_fd, packet->data.ptr, packet->data.len,
- 0, &(packet->receiver.addr), packet->receiver.len);
-
- printf("bytes: %d\n", bytes_sent);
+ 0, &(packet->destination), packet->sockaddr_len);
+
if (bytes_sent != packet->data.len)
{
+ perror("Sendto error");
return FAILED;
}
return SUCCESS;
@@ -99,7 +99,7 @@ status_t destroyer(private_socket_t *this)
return SUCCESS;
}
-socket_t *socket_create()
+socket_t *socket_create(u_int16_t port)
{
private_socket_t *this = alloc_thing(socket_t, "private_socket_t");
struct sockaddr_in addr;
@@ -109,7 +109,6 @@ socket_t *socket_create()
this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver;
this->public.destroy = (status_t(*)(socket_t*))destroyer;
- printf("@%d\n", __LINE__);
/* create default ipv4 socket */
this->socket_fd = socket(PF_INET, SOCK_DGRAM, 0);
if (this->socket_fd < 0) {
@@ -117,15 +116,13 @@ socket_t *socket_create()
return NULL;
}
- printf("@%d\n", __LINE__);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
- addr.sin_port = 500;
+ addr.sin_port = htons(port);
if (bind(this->socket_fd,(struct sockaddr*)&addr, sizeof(addr)) < 0) {
pfree(this);
return NULL;
}
- printf("@%d\n", __LINE__);
return (socket_t*)this;
}
diff --git a/Source/charon/socket.h b/Source/charon/socket.h
index 2d50f42df..7da3e13c7 100644
--- a/Source/charon/socket.h
+++ b/Source/charon/socket.h
@@ -80,7 +80,7 @@ struct socket_s {
* @param
* @return
*/
-socket_t *socket_create();
+socket_t *socket_create(u_int16_t port);
#endif /*SOCKET_H_*/
diff --git a/Source/charon/tests/send_queue_test.c b/Source/charon/tests/send_queue_test.c
index 0298899a9..603b6b0d4 100644
--- a/Source/charon/tests/send_queue_test.c
+++ b/Source/charon/tests/send_queue_test.c
@@ -67,7 +67,7 @@ static void test_send_queue_sender(send_queue_test_t * testinfo)
int i;
for (i = 0; i < testinfo->insert_item_count; i++)
{
- packet_t *packet = packet_create();
+ packet_t *packet = packet_create(AF_INET);
testinfo->tester->assert_true(testinfo->tester,(packet != NULL), "create packet call check");
testinfo->tester->assert_true(testinfo->tester,(testinfo->send_queue->add(testinfo->send_queue,packet) == SUCCESS), "add packet call check");
}
diff --git a/Source/charon/tests/socket_test.c b/Source/charon/tests/socket_test.c
index 230808782..8d735ac13 100644
--- a/Source/charon/tests/socket_test.c
+++ b/Source/charon/tests/socket_test.c
@@ -32,20 +32,33 @@
*/
void test_socket(tester_t *tester)
{
- socket_t *skt = socket_create();
- packet_t *pkt = packet_create();
+ int packet_count = 5;
+ int current;
+ socket_t *skt = socket_create(4500);
+ packet_t *pkt = packet_create(AF_INET);
char *test_string = "Testing functionality of socket_t";
pkt->data.ptr = test_string;
pkt->data.len = strlen(test_string);
- pkt->receiver.addr.sin_family = AF_INET;
- pkt->receiver.addr.sin_addr.s_addr = inet_addr("127.0.0.1");
- pkt->receiver.addr.sin_port = htons(500);
-
- skt->send(skt, pkt);
+ /* send to previously bound socket */
+ pkt->set_destination(pkt, "127.0.0.1", 4500);
+
+ /* send packet_count packets */
+ for (current = 0; current < packet_count; current++)
+ {
+ if (skt->send(skt, pkt) == FAILED)
+ {
+ tester->assert_true(tester, 0, "packet send");
+ }
+ }
pkt->destroy(pkt);
- skt->receive(skt, &pkt);
- tester->assert_false(tester, strcmp(test_string, pkt->data.ptr), "packet exchange");
+ /* receive packet_count packets */
+ for (current = 0; current < packet_count; current++)
+ {
+ skt->receive(skt, &pkt);
+ tester->assert_false(tester, strcmp(test_string, pkt->data.ptr), "packet exchange");
+ pkt->destroy(pkt);
+ }
}