- revised packet and socket interface
- tested
This commit is contained in:
+46
-4
@@ -23,8 +23,6 @@
|
|||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static status_t destroy(packet_t *this)
|
static status_t destroy(packet_t *this)
|
||||||
{
|
{
|
||||||
pfree(this->data.ptr);
|
pfree(this->data.ptr);
|
||||||
@@ -32,15 +30,59 @@ static status_t destroy(packet_t *this)
|
|||||||
return SUCCESS;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
packet_t *packet_create()
|
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(int family)
|
||||||
{
|
{
|
||||||
packet_t *this = alloc_thing(packet_t, "packet_t");
|
packet_t *this = alloc_thing(packet_t, "packet_t");
|
||||||
|
|
||||||
this->destroy = destroy;
|
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.len = 0;
|
||||||
this->data.ptr = NULL;
|
this->data.ptr = NULL;
|
||||||
return this;
|
return this;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-11
@@ -42,26 +42,29 @@
|
|||||||
typedef struct packet_s packet_t;
|
typedef struct packet_s packet_t;
|
||||||
struct packet_s {
|
struct packet_s {
|
||||||
/**
|
/**
|
||||||
* senders address and port
|
* Address family, such as AF_INET
|
||||||
*/
|
*/
|
||||||
struct {
|
int family;
|
||||||
struct sockaddr_in addr;
|
size_t sockaddr_len;
|
||||||
size_t len;
|
|
||||||
} sender;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* receivers address and port
|
* source address structure
|
||||||
*/
|
*/
|
||||||
struct {
|
struct sockaddr source;
|
||||||
struct sockaddr_in addr;
|
|
||||||
size_t len;
|
/**
|
||||||
} receiver;
|
* destination address structure
|
||||||
|
*/
|
||||||
|
struct sockaddr destination;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* message data
|
* message data
|
||||||
*/
|
*/
|
||||||
chunk_t 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
|
* @brief
|
||||||
*
|
*
|
||||||
@@ -77,6 +80,6 @@ struct packet_s {
|
|||||||
* @param
|
* @param
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
packet_t *packet_create();
|
packet_t *packet_create(int family);
|
||||||
|
|
||||||
#endif /*PACKET_H_*/
|
#endif /*PACKET_H_*/
|
||||||
|
|||||||
+8
-11
@@ -52,12 +52,13 @@ status_t receiver(private_socket_t *this, packet_t **packet)
|
|||||||
{
|
{
|
||||||
|
|
||||||
char buffer[MAX_PACKET];
|
char buffer[MAX_PACKET];
|
||||||
packet_t *pkt = packet_create();
|
|
||||||
|
packet_t *pkt = packet_create(AF_INET);
|
||||||
|
|
||||||
/* do the read */
|
/* do the read */
|
||||||
pkt->sender.len = sizeof(pkt->sender.addr);
|
|
||||||
pkt->data.len = recvfrom(this->socket_fd, buffer, MAX_PACKET, 0,
|
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)
|
if (pkt->data.len < 0)
|
||||||
{
|
{
|
||||||
pkt->destroy(pkt);
|
pkt->destroy(pkt);
|
||||||
@@ -78,14 +79,13 @@ status_t sender(private_socket_t *this, packet_t *packet)
|
|||||||
{
|
{
|
||||||
ssize_t bytes_sent;
|
ssize_t bytes_sent;
|
||||||
|
|
||||||
printf("@%d\n", __LINE__);
|
|
||||||
/* send data */
|
/* send data */
|
||||||
bytes_sent = sendto(this->socket_fd, packet->data.ptr, packet->data.len,
|
bytes_sent = sendto(this->socket_fd, packet->data.ptr, packet->data.len,
|
||||||
0, &(packet->receiver.addr), packet->receiver.len);
|
0, &(packet->destination), packet->sockaddr_len);
|
||||||
|
|
||||||
printf("bytes: %d\n", bytes_sent);
|
|
||||||
if (bytes_sent != packet->data.len)
|
if (bytes_sent != packet->data.len)
|
||||||
{
|
{
|
||||||
|
perror("Sendto error");
|
||||||
return FAILED;
|
return FAILED;
|
||||||
}
|
}
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
@@ -99,7 +99,7 @@ status_t destroyer(private_socket_t *this)
|
|||||||
return SUCCESS;
|
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");
|
private_socket_t *this = alloc_thing(socket_t, "private_socket_t");
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
@@ -109,7 +109,6 @@ socket_t *socket_create()
|
|||||||
this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver;
|
this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver;
|
||||||
this->public.destroy = (status_t(*)(socket_t*))destroyer;
|
this->public.destroy = (status_t(*)(socket_t*))destroyer;
|
||||||
|
|
||||||
printf("@%d\n", __LINE__);
|
|
||||||
/* create default ipv4 socket */
|
/* create default ipv4 socket */
|
||||||
this->socket_fd = socket(PF_INET, SOCK_DGRAM, 0);
|
this->socket_fd = socket(PF_INET, SOCK_DGRAM, 0);
|
||||||
if (this->socket_fd < 0) {
|
if (this->socket_fd < 0) {
|
||||||
@@ -117,15 +116,13 @@ socket_t *socket_create()
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("@%d\n", __LINE__);
|
|
||||||
addr.sin_family = AF_INET;
|
addr.sin_family = AF_INET;
|
||||||
addr.sin_addr.s_addr = INADDR_ANY;
|
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) {
|
if (bind(this->socket_fd,(struct sockaddr*)&addr, sizeof(addr)) < 0) {
|
||||||
pfree(this);
|
pfree(this);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("@%d\n", __LINE__);
|
|
||||||
return (socket_t*)this;
|
return (socket_t*)this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ struct socket_s {
|
|||||||
* @param
|
* @param
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
socket_t *socket_create();
|
socket_t *socket_create(u_int16_t port);
|
||||||
|
|
||||||
|
|
||||||
#endif /*SOCKET_H_*/
|
#endif /*SOCKET_H_*/
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ static void test_send_queue_sender(send_queue_test_t * testinfo)
|
|||||||
int i;
|
int i;
|
||||||
for (i = 0; i < testinfo->insert_item_count; 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,(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");
|
testinfo->tester->assert_true(testinfo->tester,(testinfo->send_queue->add(testinfo->send_queue,packet) == SUCCESS), "add packet call check");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,20 +32,33 @@
|
|||||||
*/
|
*/
|
||||||
void test_socket(tester_t *tester)
|
void test_socket(tester_t *tester)
|
||||||
{
|
{
|
||||||
socket_t *skt = socket_create();
|
int packet_count = 5;
|
||||||
packet_t *pkt = packet_create();
|
int current;
|
||||||
|
socket_t *skt = socket_create(4500);
|
||||||
|
packet_t *pkt = packet_create(AF_INET);
|
||||||
char *test_string = "Testing functionality of socket_t";
|
char *test_string = "Testing functionality of socket_t";
|
||||||
|
|
||||||
pkt->data.ptr = test_string;
|
pkt->data.ptr = test_string;
|
||||||
pkt->data.len = strlen(test_string);
|
pkt->data.len = strlen(test_string);
|
||||||
|
|
||||||
pkt->receiver.addr.sin_family = AF_INET;
|
/* send to previously bound socket */
|
||||||
pkt->receiver.addr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
pkt->set_destination(pkt, "127.0.0.1", 4500);
|
||||||
pkt->receiver.addr.sin_port = htons(500);
|
|
||||||
|
|
||||||
skt->send(skt, pkt);
|
/* 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);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user