- updated comments
This commit is contained in:
parent
6dfc0b7949
commit
b9439120a3
|
@ -61,24 +61,41 @@ struct packet_s {
|
|||
* message 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);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @brief destroy the packet, freeing contained data
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
* @param packet packet to destroy
|
||||
* @return SUCCESS
|
||||
*/
|
||||
status_t (*destroy) (packet_t *packet);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @brief create an empty packet
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
* @param family address-family, such as AF_INET
|
||||
* @return NULL when family not supported
|
||||
*/
|
||||
packet_t *packet_create(int family);
|
||||
|
||||
|
|
|
@ -47,10 +47,11 @@ typedef struct {
|
|||
int socket_fd;
|
||||
} private_socket_t;
|
||||
|
||||
|
||||
/**
|
||||
* implementation of socket_t.receive
|
||||
*/
|
||||
status_t receiver(private_socket_t *this, packet_t **packet)
|
||||
{
|
||||
|
||||
char buffer[MAX_PACKET];
|
||||
|
||||
packet_t *pkt = packet_create(AF_INET);
|
||||
|
@ -58,10 +59,13 @@ status_t receiver(private_socket_t *this, packet_t **packet)
|
|||
/* do the read */
|
||||
pkt->data.len = recvfrom(this->socket_fd, buffer, MAX_PACKET, 0,
|
||||
&(pkt->source), &(pkt->sockaddr_len));
|
||||
/* TODO: get senders destination address, using
|
||||
* IP_PKTINFO and recvmsg */
|
||||
|
||||
if (pkt->data.len < 0)
|
||||
{
|
||||
pkt->destroy(pkt);
|
||||
/* TODO: log detailed error */
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
|
@ -75,6 +79,9 @@ status_t receiver(private_socket_t *this, packet_t **packet)
|
|||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* implementation of socket_t.send
|
||||
*/
|
||||
status_t sender(private_socket_t *this, packet_t *packet)
|
||||
{
|
||||
ssize_t bytes_sent;
|
||||
|
@ -85,13 +92,16 @@ status_t sender(private_socket_t *this, packet_t *packet)
|
|||
|
||||
if (bytes_sent != packet->data.len)
|
||||
{
|
||||
perror("Sendto error");
|
||||
/* TODO: log detailed error */
|
||||
return FAILED;
|
||||
}
|
||||
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);
|
||||
pfree(this);
|
||||
|
@ -107,20 +117,23 @@ socket_t *socket_create(u_int16_t port)
|
|||
/* 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;
|
||||
this->public.destroy = (status_t(*)(socket_t*))destroy;
|
||||
|
||||
/* create default ipv4 socket */
|
||||
this->socket_fd = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
if (this->socket_fd < 0) {
|
||||
pfree(this);
|
||||
/* TODO: log detailed error */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* bind socket to all interfaces */
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
addr.sin_port = htons(port);
|
||||
if (bind(this->socket_fd,(struct sockaddr*)&addr, sizeof(addr)) < 0) {
|
||||
pfree(this);
|
||||
/* TODO: log detailed error */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
|
@ -44,41 +44,51 @@
|
|||
typedef struct socket_s socket_t;
|
||||
struct socket_s {
|
||||
/**
|
||||
* @brief
|
||||
* @brief receive a packet
|
||||
*
|
||||
* reads a packet from one of the sockets.
|
||||
* source will be set, dest not implemented
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
* @param sock socket_t object to work on
|
||||
* @param packet pinter gets address from allocated packet_t
|
||||
* @return FAILED when unable to receive
|
||||
* SUCCESS when packet successfully received
|
||||
*/
|
||||
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
|
||||
* @return
|
||||
* @param sock socket_t object to work on
|
||||
* @param packet[out] packet_t to send
|
||||
* @return FAILED when unable to send
|
||||
* SUCCESS when packet successfully sent
|
||||
*/
|
||||
status_t (*send) (socket_t *sock, packet_t *packet);
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @brief destroy sockets
|
||||
*
|
||||
* close sockets and destroy socket_t object
|
||||
*
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
* @param sock socket_t to destroy
|
||||
* @return SUCCESS
|
||||
*/
|
||||
status_t (*destroy) (socket_t *sock);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @brief socket_t constructor
|
||||
*
|
||||
* currently creates one socket, listening on all addresses
|
||||
* on port.
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
* @param port port to bind socket to
|
||||
* @return the created socket, or NULL on error
|
||||
*/
|
||||
socket_t *socket_create(u_int16_t port);
|
||||
|
||||
|
|
Loading…
Reference in New Issue