- updated comments

This commit is contained in:
Martin Willi 2005-11-07 11:46:18 +00:00
parent 6dfc0b7949
commit b9439120a3
3 changed files with 67 additions and 27 deletions

View File

@ -61,24 +61,41 @@ struct packet_s {
* message data * message data
*/ */
chunk_t data; chunk_t data;
/**
* @brief set destination of packet, using address string
*
* @param address ip address string
* @param port port number
* @return SUCCESS
* NOT_SUPPORTED
*/
status_t (*set_destination) (packet_t *packet, char *address, u_int16_t port); status_t (*set_destination) (packet_t *packet, char *address, u_int16_t port);
/**
* @brief set destination of packet, using address string
*
* @param address ip address string
* @param port port number
* @return SUCCESS
* NOT_SUPPORTED
*/
status_t (*set_source) (packet_t *packet, char *address, u_int16_t port); status_t (*set_source) (packet_t *packet, char *address, u_int16_t port);
/** /**
* @brief * @brief destroy the packet, freeing contained data
* *
* @param * @param packet packet to destroy
* @return * @return SUCCESS
*/ */
status_t (*destroy) (packet_t *packet); status_t (*destroy) (packet_t *packet);
}; };
/** /**
* @brief * @brief create an empty packet
* *
* @param * @param family address-family, such as AF_INET
* @return * @return NULL when family not supported
*/ */
packet_t *packet_create(int family); packet_t *packet_create(int family);

View File

@ -47,10 +47,11 @@ typedef struct {
int socket_fd; int socket_fd;
} private_socket_t; } private_socket_t;
/**
* implementation of socket_t.receive
*/
status_t receiver(private_socket_t *this, packet_t **packet) status_t receiver(private_socket_t *this, packet_t **packet)
{ {
char buffer[MAX_PACKET]; char buffer[MAX_PACKET];
packet_t *pkt = packet_create(AF_INET); packet_t *pkt = packet_create(AF_INET);
@ -58,10 +59,13 @@ status_t receiver(private_socket_t *this, packet_t **packet)
/* do the read */ /* do the read */
pkt->data.len = recvfrom(this->socket_fd, buffer, MAX_PACKET, 0, pkt->data.len = recvfrom(this->socket_fd, buffer, MAX_PACKET, 0,
&(pkt->source), &(pkt->sockaddr_len)); &(pkt->source), &(pkt->sockaddr_len));
/* TODO: get senders destination address, using
* IP_PKTINFO and recvmsg */
if (pkt->data.len < 0) if (pkt->data.len < 0)
{ {
pkt->destroy(pkt); pkt->destroy(pkt);
/* TODO: log detailed error */
return FAILED; return FAILED;
} }
@ -75,6 +79,9 @@ status_t receiver(private_socket_t *this, packet_t **packet)
return SUCCESS; return SUCCESS;
} }
/**
* implementation of socket_t.send
*/
status_t sender(private_socket_t *this, packet_t *packet) status_t sender(private_socket_t *this, packet_t *packet)
{ {
ssize_t bytes_sent; ssize_t bytes_sent;
@ -85,13 +92,16 @@ status_t sender(private_socket_t *this, packet_t *packet)
if (bytes_sent != packet->data.len) if (bytes_sent != packet->data.len)
{ {
perror("Sendto error"); /* TODO: log detailed error */
return FAILED; return FAILED;
} }
return SUCCESS; return SUCCESS;
} }
status_t destroyer(private_socket_t *this) /**
* implementation of socket_t.destroy
*/
status_t destroy(private_socket_t *this)
{ {
close(this->socket_fd); close(this->socket_fd);
pfree(this); pfree(this);
@ -107,20 +117,23 @@ socket_t *socket_create(u_int16_t port)
/* public functions */ /* public functions */
this->public.send = (status_t(*)(socket_t*, packet_t*))sender; this->public.send = (status_t(*)(socket_t*, packet_t*))sender;
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*))destroy;
/* 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) {
pfree(this); pfree(this);
/* TODO: log detailed error */
return NULL; return NULL;
} }
/* bind socket to all interfaces */
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 = htons(port); 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);
/* TODO: log detailed error */
return NULL; return NULL;
} }

View File

@ -44,41 +44,51 @@
typedef struct socket_s socket_t; typedef struct socket_s socket_t;
struct socket_s { struct socket_s {
/** /**
* @brief * @brief receive a packet
*
* reads a packet from one of the sockets.
* source will be set, dest not implemented
* *
* *
* * @param sock socket_t object to work on
* @param * @param packet pinter gets address from allocated packet_t
* @return * @return FAILED when unable to receive
* SUCCESS when packet successfully received
*/ */
status_t (*receive) (socket_t *sock, packet_t **packet); status_t (*receive) (socket_t *sock, packet_t **packet);
/** /**
* @brief * @brief send a packet
* *
* sends a packet via desired socket.
* uses source and dest in packet.
* *
* * @param sock socket_t object to work on
* @param * @param packet[out] packet_t to send
* @return * @return FAILED when unable to send
* SUCCESS when packet successfully sent
*/ */
status_t (*send) (socket_t *sock, packet_t *packet); status_t (*send) (socket_t *sock, packet_t *packet);
/** /**
* @brief * @brief destroy sockets
* *
* close sockets and destroy socket_t object
* *
* * @param sock socket_t to destroy
* @param * @return SUCCESS
* @return
*/ */
status_t (*destroy) (socket_t *sock); status_t (*destroy) (socket_t *sock);
}; };
/** /**
* @brief * @brief socket_t constructor
*
* currently creates one socket, listening on all addresses
* on port.
* *
* @param * @param port port to bind socket to
* @return * @return the created socket, or NULL on error
*/ */
socket_t *socket_create(u_int16_t port); socket_t *socket_create(u_int16_t port);