- first attempt of socket abstraction (more to do)
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* @file packet.h
|
||||
*
|
||||
* @brief UDP-Packet, contains data, sender and receiver
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2005 Jan Hutter, Martin Willi
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
#include "packet.h"
|
||||
|
||||
|
||||
|
||||
|
||||
status_t destroy(packet_t *this)
|
||||
{
|
||||
pfree(this->data.ptr);
|
||||
pfree(this);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
packet_t *packet_create()
|
||||
{
|
||||
packet_t *this = alloc_thing(packet_t, "packet_t");
|
||||
|
||||
this->data.len = 0;
|
||||
this->data.ptr = NULL;
|
||||
return this;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* @file packet.h
|
||||
*
|
||||
* @brief UDP-Packet, contains data, sender and receiver
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2005 Jan Hutter, Martin Willi
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
#ifndef PACKET_H_
|
||||
#define PACKET_H_
|
||||
|
||||
|
||||
#include "types.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdlib.h>
|
||||
#include <freeswan.h>
|
||||
#include <pluto/constants.h>
|
||||
#include <pluto/defs.h>
|
||||
|
||||
|
||||
/**
|
||||
* @brief UDP-Packet, contains data, sender and receiver
|
||||
*/
|
||||
typedef struct packet_s packet_t;
|
||||
struct packet_s {
|
||||
/**
|
||||
* senders address and port
|
||||
*/
|
||||
struct {
|
||||
struct sockaddr_in addr;
|
||||
size_t len;
|
||||
} sender;
|
||||
|
||||
/**
|
||||
* receivers address and port
|
||||
*/
|
||||
struct {
|
||||
struct sockaddr_in addr;
|
||||
size_t len;
|
||||
} receiver;
|
||||
|
||||
/**
|
||||
* message data
|
||||
*/
|
||||
chunk_t data;
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
status_t (*destroy) (packet_t *packet);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
packet_t *packet_create();
|
||||
|
||||
#endif /*PACKET_H_*/
|
||||
@@ -0,0 +1,131 @@
|
||||
/**
|
||||
* @file socket.c
|
||||
*
|
||||
* @brief management of sockets
|
||||
*
|
||||
* receiver reads from here, sender writes to here
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2005 Jan Hutter, Martin Willi
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
#include "socket.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <freeswan.h>
|
||||
#include <pluto/constants.h>
|
||||
#include <pluto/defs.h>
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
/**
|
||||
* public functions
|
||||
*/
|
||||
socket_t public;
|
||||
|
||||
/**
|
||||
* currently we only have one socket, maybe more in the future ?
|
||||
*/
|
||||
int socket_fd;
|
||||
} private_socket_t;
|
||||
|
||||
|
||||
status_t receiver(private_socket_t *this, packet_t **packet)
|
||||
{
|
||||
|
||||
char buffer[MAX_PACKET];
|
||||
packet_t *pkt = packet_create();
|
||||
|
||||
/* 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));
|
||||
if (pkt->data.len < 0)
|
||||
{
|
||||
pkt->destroy(pkt);
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
/* fill in packet */
|
||||
pkt->data.ptr = alloc_bytes(pkt->data.len, "data in packet_t");
|
||||
memcpy(pkt->data.ptr, buffer, pkt->data.len);
|
||||
|
||||
/* return packet */
|
||||
*packet = pkt;
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
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);
|
||||
if (bytes_sent != packet->data.len)
|
||||
{
|
||||
return FAILED;
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
status_t destroyer(private_socket_t *this)
|
||||
{
|
||||
close(this->socket_fd);
|
||||
pfree(this);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
socket_t *socket_create()
|
||||
{
|
||||
private_socket_t *this = alloc_thing(socket_t, "private_socket_t");
|
||||
struct sockaddr_in addr;
|
||||
|
||||
/* public functions */
|
||||
this->public.send = (status_t(*)(socket_t*, packet_t*))sender;
|
||||
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) {
|
||||
pfree(this);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
printf("@%d\n", __LINE__);
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
addr.sin_port = 500;
|
||||
if (bind(this->socket_fd,(struct sockaddr*)&addr, sizeof(addr)) < 0) {
|
||||
pfree(this);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
printf("@%d\n", __LINE__);
|
||||
return (socket_t*)this;
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* @file socket.h
|
||||
*
|
||||
* @brief management of sockets
|
||||
*
|
||||
* receiver reads from here, sender writes to here
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2005 Jan Hutter, Martin Willi
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
#ifndef SOCKET_H_
|
||||
#define SOCKET_H_
|
||||
|
||||
|
||||
#include "types.h"
|
||||
#include "packet.h"
|
||||
|
||||
|
||||
/**
|
||||
* maximum size of a packet
|
||||
* 3000 Bytes should be sufficient, see IKEv2 draft
|
||||
*/
|
||||
#define MAX_PACKET 3000
|
||||
|
||||
|
||||
/**
|
||||
* @brief abstraction of one (ipv4), or in future, of multiple sockets
|
||||
*
|
||||
*/
|
||||
typedef struct socket_s socket_t;
|
||||
struct socket_s {
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
status_t (*receive) (socket_t *sock, packet_t **packet);
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
status_t (*send) (socket_t *sock, packet_t *packet);
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
status_t (*destroy) (socket_t *sock);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
socket_t *socket_create();
|
||||
|
||||
|
||||
#endif /*SOCKET_H_*/
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* @file thread_pool_test.c
|
||||
*
|
||||
* @brief Tests to test the Socket (type socket_t)
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2005 Jan Hutter, Martin Willi
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "../tester.h"
|
||||
#include "../socket.h"
|
||||
|
||||
/*
|
||||
* Description in header file
|
||||
*/
|
||||
void test_socket(tester_t *tester)
|
||||
{
|
||||
socket_t *skt = socket_create();
|
||||
packet_t *pkt = packet_create();
|
||||
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);
|
||||
pkt->destroy(pkt);
|
||||
skt->receive(skt, &pkt);
|
||||
|
||||
tester->assert_false(tester, strcmp(test_string, pkt->data.ptr), "packet exchange");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* @file socket_test.h
|
||||
*
|
||||
* @brief Tests to test the Socket (type socket_t)
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2005 Jan Hutter, Martin Willi
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
#ifndef SOCKET_TEST_H_
|
||||
#define SOCKET_TEST_H_
|
||||
|
||||
/**
|
||||
* @brief Test function for the type socket_t
|
||||
*
|
||||
* @param tester tester object
|
||||
*/
|
||||
void test_socket(tester_t *tester);
|
||||
|
||||
/**
|
||||
* Test for socket_t
|
||||
*/
|
||||
test_t socket_test = {test_socket,"Socket"};
|
||||
|
||||
|
||||
|
||||
#endif /*SOCKET_TEST_H_*/
|
||||
Reference in New Issue
Block a user