1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Refactor destiny/socket.h

This commit is contained in:
Martin Lenders 2013-09-23 12:40:16 +02:00
parent b223a8baf0
commit 3149e83826
14 changed files with 361 additions and 280 deletions

View File

@ -22,10 +22,11 @@
#include <vtimer.h>
#include "udp.h"
#include "tcp.h"
#include "socket.h"
#include "tcp_timer.h"
#include "destiny.h"
#include "socket.h"
char tcp_stack_buffer[TCP_STACK_SIZE];
char udp_stack_buffer[UDP_STACK_SIZE];

View File

@ -34,6 +34,7 @@
#define DESTINY_H
#include "destiny/in.h"
#include "destiny/socket.h"
/**
* Initializes transport layer.

View File

@ -0,0 +1,215 @@
/**
* destiny/socket.h - Destiny socket API
*
* Copyright (C) 2013 INRIA.
*
* This file subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*
* @ingroup destiny
* @{
* @file
* @brief Header for BSD socket API
* @author Oliver Gesch <oliver.gesch@googlemail.com>
* @author Martin Lenders <mlenders@inf.fu-berlin.de>
*/
#ifndef DESTINY_SOCKET_H
#define DESTINY_SOCKET_H
#include <stdint.h>
#include "ipv6.h"
#include "destiny/in.h"
/**
* POSIX compatible type for address family.
*
* @see <a href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_socket.h.html">
* IEEE Std 1003.1, 2013 Edition - sys/socket.h
* </a>
*/
typedef uint8_t sa_family_t;
typedef uint32_t socklen_t; ///< POSIX compatible type for address lengths.
/*
* Types
*/
#define SOCK_STREAM 1 /* stream socket */
#define SOCK_DGRAM 2 /* datagram socket */
#define SOCK_RAW 3 /* raw-protocol interface */
#define SOCK_RDM 4 /* reliably-delivered message */
#define SOCK_SEQPACKET 5 /* sequenced packet stream */
/*
* Address families.
*/
#define AF_UNSPEC 0 /* unspecified */
#define AF_LOCAL 1 /* local to host (pipes, portals) */
#define AF_UNIX AF_LOCAL /* backward compatibility */
#define AF_INET 2 /* internetwork: UDP, TCP, etc. */
#define AF_IMPLINK 3 /* arpanet imp addresses */
#define AF_PUP 4 /* pup protocols: e.g. BSP */
#define AF_CHAOS 5 /* mit CHAOS protocols */
#define AF_NS 6 /* XEROX NS protocols */
#define AF_ISO 7 /* ISO protocols */
#define AF_OSI AF_ISO
#define AF_ECMA 8 /* European computer manufacturers */
#define AF_DATAKIT 9 /* datakit protocols */
#define AF_CCITT 10 /* CCITT protocols, X.25 etc */
#define AF_SNA 11 /* IBM SNA */
#define AF_DECnet 12 /* DECnet */
#define AF_DLI 13 /* DEC Direct data link interface */
#define AF_LAT 14 /* LAT */
#define AF_HYLINK 15 /* NSC Hyperchannel */
#define AF_APPLETALK 16 /* Apple Talk */
#define AF_ROUTE 17 /* Internal Routing Protocol */
#define AF_LINK 18 /* Link layer interface */
#define pseudo_AF_XTP 19 /* eXpress Transfer Protocol (no AF) */
#define AF_COIP 20 /* connection-oriented IP, aka ST II */
#define AF_CNT 21 /* Computer Network Technology */
#define pseudo_AF_RTIP 22 /* Help Identify RTIP packets */
#define AF_IPX 23 /* Novell Internet Protocol */
#define AF_SIP 24 /* Simple Internet Protocol */
#define pseudo_AF_PIP 25 /* Help Identify PIP packets */
#define AF_ISDN 26 /* Integrated Services Digital Network*/
#define AF_E164 AF_ISDN /* CCITT E.164 recommendation */
#define pseudo_AF_KEY 27 /* Internal key-management function */
#define AF_INET6 28 /* IPv6 */
#define AF_NATM 29 /* native ATM access */
#define AF_ATM 30 /* ATM */
#define pseudo_AF_HDRCMPLT 31 /* Used by BPF to not rewrite headers
* in interface output routine
*/
#define AF_NETGRAPH 32 /* Netgraph sockets */
#define AF_MAX 33
/*
* Protocol families, same as address families for now.
*/
#define PF_UNSPEC AF_UNSPEC
#define PF_LOCAL AF_LOCAL
#define PF_UNIX PF_LOCAL /* backward compatibility */
#define PF_INET AF_INET
#define PF_IMPLINK AF_IMPLINK
#define PF_PUP AF_PUP
#define PF_CHAOS AF_CHAOS
#define PF_NS AF_NS
#define PF_ISO AF_ISO
#define PF_OSI AF_ISO
#define PF_ECMA AF_ECMA
#define PF_DATAKIT AF_DATAKIT
#define PF_CCITT AF_CCITT
#define PF_SNA AF_SNA
#define PF_DECnet AF_DECnet
#define PF_DLI AF_DLI
#define PF_LAT AF_LAT
#define PF_HYLINK AF_HYLINK
#define PF_APPLETALK AF_APPLETALK
#define PF_ROUTE AF_ROUTE
#define PF_LINK AF_LINK
#define PF_XTP pseudo_AF_XTP /* really just proto family, no AF */
#define PF_COIP AF_COIP
#define PF_CNT AF_CNT
#define PF_SIP AF_SIP
#define PF_IPX AF_IPX /* same format as AF_NS */
#define PF_RTIP pseudo_AF_RTIP /* same format as AF_INET */
#define PF_PIP pseudo_AF_PIP
#define PF_ISDN AF_ISDN
#define PF_KEY pseudo_AF_KEY
#define PF_INET6 AF_INET6
#define PF_NATM AF_NATM
#define PF_ATM AF_ATM
#define PF_NETGRAPH AF_NETGRAPH
#define PF_MAX AF_MAX
#define DESTINY_SOCKET_STATIC_MSS 48
#define DESTINY_SOCKET_STATIC_WINDOW 1 * DESTINY_SOCKET_STATIC_MSS
#define DESTINY_SOCKET_MAX_TCP_BUFFER 1 * DESTINY_SOCKET_STATIC_WINDOW
typedef struct __attribute__((packed)) {
uint8_t sin6_family; /* AF_INET6 */
uint16_t sin6_port; /* transport layer port # */
uint32_t sin6_flowinfo; /* IPv6 flow information */
ipv6_addr_t sin6_addr; /* IPv6 address */
} sockaddr6_t;
int destiny_socket(int domain, int type, int protocol);
int destiny_socket_connect(int socket, sockaddr6_t *addr,
socklen_t addrlen);
/**
* Receives data through socket *s* and saves it in buffer *buf*. Roughly
* identical to POSIX's <a href="http://man.he.net/man2/recv">recv(2)</a>.
*
* @param[in] s The ID of the socket to receive from.
* @param[in] buf Buffer to store received data in.
* @param[in] len Length of buffer.
* @param[in] flags Flags for possible later implementations (currently
* unused).
*
* @return Number of received bytes, -1 on error.
*/
int32_t destiny_socket_recv(int s, void *buf, uint32_t len, int flags);
/**
* Receives data through socket *s* and saves it in buffer *buf*. The address
* of the sender is stored in *from*. Roughly identical to POSIX's
* <a href="http://man.he.net/man2/recvfrom">recvfrom(2)</a>.
*
* @param[in] s The ID of the socket to receive from.
* @param[in] buf Buffer to store received data in.
* @param[in] len Length of buffer.
* @param[in] flags Flags for possible later implementations (currently
* unused).
* @param[in] from IPv6 Address of the data's sender.
* @param[in] fromlen Length of address in *from* in byte (always 16).
*
* @return Number of received bytes, -1 on error.
*/
int32_t destiny_socket_recvfrom(int s, void *buf, uint32_t len, int flags,
sockaddr6_t *from, socklen_t *fromlen);
/**
* Sends data *buf* through socket *s*. Roughly identical to POSIX's
* <a href="http://man.he.net/man2/send">send(2)</a>.
*
* @param[in] s The ID of the socket to send through.
* @param[in] buf Buffer to send the data from.
* @param[in] len Length of buffer.
* @param[in] flags Flags for possible later implementations (currently
* unused).
*
* @return Number of send bytes, -1 on error.
*/
int32_t destiny_socket_send(int s, const void *buf, uint32_t len, int flags);
/**
* Sends data *buf* through socket *s* to foreign host with IPv6 address *addr*.
* Roughly identical to POSIX's <a href="http://man.he.net/man2/send">send(2)</a>.
*
* @param[in] s The ID of the socket to send through.
* @param[in] buf Buffer to send the data from.
* @param[in] len Length of buffer.
* @param[in] flags Flags for possible later implementations (currently
* unused).
* @param[in] from IPv6 Address to send data to.
* @param[in] fromlen Length of address in *from* in byte (always 16).
*
* @return Number of send bytes, -1 on error.
*/
int32_t destiny_socket_sendto(int s, const void *buf, uint32_t len, int flags,
sockaddr6_t *to, socklen_t tolen);
int32_t destiny_socket_send(int s, void *msg, uint32_t len, int flags);
int32_t destiny_socket_recv(int s, void *buf, uint32_t len, int flags);
int destiny_socket_close(int s);
int destiny_socket_bind(int s, sockaddr6_t *name, int namelen);
int destiny_socket_listen(int s, int backlog);
int destiny_socket_accept(int s, sockaddr6_t *addr, socklen_t *addrlen);
void destiny_socket_print_sockets(void);
/**
* @}
*/
#endif /* DESTINY_SOCKET_H */

View File

@ -24,7 +24,6 @@
#include "ipv6.h"
#include "udp.h"
#include "tcp.h"
#include "socket.h"
#include "vtimer.h"
#include "hwtimer.h"
#include "tcp_timer.h"
@ -32,8 +31,24 @@
#include "../net_help/net_help.h"
#include "../net_help/msg_help.h"
#include "socket.h"
#define EPHEMERAL_PORTS 49152
socket_internal_t sockets[MAX_SOCKETS];
void print_socket(socket_t *current_socket);
void print_internal_socket(socket_internal_t *current_socket_internal);
void printf_tcp_context(tcp_hc_context_t *current_tcp_context);
int exists_socket(int socket);
void set_socket_address(sockaddr6_t *sockaddr, sa_family_t sin6_family,
uint16_t sin6_port, uint32_t sin6_flowinfo,
ipv6_addr_t *sin6_addr);
void set_tcp_packet(tcp_hdr_t *tcp_hdr, uint16_t src_port, uint16_t dst_port,
uint32_t seq_nr, uint32_t ack_nr,
uint8_t dataOffset_reserved, uint8_t reserved_flags,
uint16_t window, uint16_t checksum, uint16_t urg_pointer);
void printf_tcp_context(tcp_hc_context_t *current_tcp_context)
{
printf("Context: %u\n", current_tcp_context->context_id);
@ -152,7 +167,7 @@ void print_internal_socket(socket_internal_t *current_socket_internal)
printf("\n--------------------------\n");
}
socket_internal_t *getSocket(uint8_t s)
socket_internal_t *get_socket(int s)
{
if (exists_socket(s)) {
return &(sockets[s - 1]);
@ -162,19 +177,19 @@ socket_internal_t *getSocket(uint8_t s)
}
}
void print_sockets(void)
void destiny_socket_print_sockets(void)
{
int i;
printf("\n--- Socket list: ---\n");
for (i = 1; i < MAX_SOCKETS + 1; i++) {
if (getSocket(i) != NULL) {
print_internal_socket(getSocket(i));
if (get_socket(i) != NULL) {
print_internal_socket(get_socket(i));
}
}
}
bool exists_socket(uint8_t socket)
int exists_socket(int socket)
{
if (sockets[socket - 1].socket_id == 0) {
return false;
@ -192,10 +207,10 @@ void close_socket(socket_internal_t *current_socket)
bool isUDPSocket(uint8_t s)
{
if ((exists_socket(s)) &&
(getSocket(s)->socket_values.domain == PF_INET6) &&
(getSocket(s)->socket_values.type == SOCK_DGRAM) &&
((getSocket(s)->socket_values.protocol == IPPROTO_UDP) ||
(getSocket(s)->socket_values.protocol == 0))) {
(get_socket(s)->socket_values.domain == PF_INET6) &&
(get_socket(s)->socket_values.type == SOCK_DGRAM) &&
((get_socket(s)->socket_values.protocol == IPPROTO_UDP) ||
(get_socket(s)->socket_values.protocol == 0))) {
return true;
}
else {
@ -203,13 +218,13 @@ bool isUDPSocket(uint8_t s)
}
}
bool isTCPSocket(uint8_t s)
bool is_tcp_socket(int s)
{
if ((exists_socket(s)) &&
(getSocket(s)->socket_values.domain == PF_INET6) &&
(getSocket(s)->socket_values.type == SOCK_STREAM) &&
((getSocket(s)->socket_values.protocol == IPPROTO_TCP) ||
(getSocket(s)->socket_values.protocol == 0))) {
(get_socket(s)->socket_values.domain == PF_INET6) &&
(get_socket(s)->socket_values.type == SOCK_STREAM) &&
((get_socket(s)->socket_values.protocol == IPPROTO_TCP) ||
(get_socket(s)->socket_values.protocol == 0))) {
return true;
}
else {
@ -227,14 +242,14 @@ int bind_udp_socket(int s, sockaddr6_t *name, int namelen, uint8_t pid)
for (i = 1; i < MAX_SOCKETS + 1; i++) {
if (isUDPSocket(i) &&
(getSocket(i)->socket_values.local_address.sin6_port == name->sin6_port)) {
(get_socket(i)->socket_values.local_address.sin6_port == name->sin6_port)) {
return -1;
}
}
memcpy(&getSocket(s)->socket_values.local_address, name, namelen);
getSocket(s)->recv_pid = pid;
return 1;
memcpy(&get_socket(s)->socket_values.local_address, name, namelen);
get_socket(s)->recv_pid = pid;
return 0;
}
int bind_tcp_socket(int s, sockaddr6_t *name, int namelen, uint8_t pid)
@ -246,23 +261,23 @@ int bind_tcp_socket(int s, sockaddr6_t *name, int namelen, uint8_t pid)
}
for (i = 1; i < MAX_SOCKETS + 1; i++) {
if (isTCPSocket(i) &&
(getSocket(i)->socket_values.local_address.sin6_port == name->sin6_port)) {
if (is_tcp_socket(i) &&
(get_socket(i)->socket_values.local_address.sin6_port == name->sin6_port)) {
return -1;
}
}
memcpy(&getSocket(s)->socket_values.local_address, name, namelen);
getSocket(s)->recv_pid = pid;
getSocket(s)->socket_values.tcp_control.rto = TCP_INITIAL_ACK_TIMEOUT;
return 1;
memcpy(&get_socket(s)->socket_values.local_address, name, namelen);
get_socket(s)->recv_pid = pid;
get_socket(s)->socket_values.tcp_control.rto = TCP_INITIAL_ACK_TIMEOUT;
return 0;
}
int socket(int domain, int type, int protocol)
int destiny_socket(int domain, int type, int protocol)
{
int i = 1;
while (getSocket(i) != NULL) {
while (get_socket(i) != NULL) {
i++;
}
@ -286,9 +301,9 @@ socket_internal_t *get_udp_socket(ipv6_hdr_t *ipv6_header, udp_hdr_t *udp_header
while (i < MAX_SOCKETS + 1) {
if (isUDPSocket(i) &&
(getSocket(i)->socket_values.local_address.sin6_port ==
(get_socket(i)->socket_values.local_address.sin6_port ==
udp_header->dst_port)) {
return getSocket(i);
return get_socket(i);
}
i++;
@ -317,15 +332,15 @@ socket_internal_t *get_tcp_socket(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header
memset(compare, 0, 16);
while (i < MAX_SOCKETS + 1) {
current_socket = getSocket(i);
current_socket = get_socket(i);
/* Check for matching 4 touple, ESTABLISHED connection */
if (isTCPSocket(i) && is_four_touple(current_socket, ipv6_header,
if (is_tcp_socket(i) && is_four_touple(current_socket, ipv6_header,
tcp_header)) {
return current_socket;
}
/* Sockets in LISTEN and SYN_RCVD state should only be tested on local TCP values */
else if (isTCPSocket(i) &&
else if (is_tcp_socket(i) &&
((current_socket->socket_values.tcp_control.state == LISTEN) ||
(current_socket->socket_values.tcp_control.state == SYN_RCVD)) &&
(current_socket->socket_values.local_address.sin6_addr.uint8[15] ==
@ -442,7 +457,7 @@ int send_tcp(socket_internal_t *current_socket, tcp_hdr_t *current_tcp_packet,
current_mss_option.kind = TCP_MSS_OPTION;
current_mss_option.len = sizeof(tcp_mss_option_t);
current_mss_option.mss = STATIC_MSS;
current_mss_option.mss = DESTINY_SOCKET_STATIC_MSS;
memcpy(((uint8_t *)current_tcp_packet) + TCP_HDR_LEN,
&current_mss_option, sizeof(tcp_mss_option_t));
}
@ -499,7 +514,7 @@ void set_tcp_cb(tcp_cb_t *tcp_control, uint32_t rcv_nxt, uint16_t rcv_wnd,
tcp_control->send_wnd = send_wnd;
}
int connect(int socket, sockaddr6_t *addr, uint32_t addrlen)
int destiny_socket_connect(int socket, sockaddr6_t *addr, uint32_t addrlen)
{
/* Variables */
ipv6_addr_t src_addr;
@ -511,7 +526,7 @@ int connect(int socket, sockaddr6_t *addr, uint32_t addrlen)
tcp_hdr_t *current_tcp_packet = ((tcp_hdr_t *)(&send_buffer[IPV6_HDR_LEN]));
/* Check if socket exists */
current_int_tcp_socket = getSocket(socket);
current_int_tcp_socket = get_socket(socket);
if (current_int_tcp_socket == NULL) {
return -1;
@ -553,7 +568,7 @@ int connect(int socket, sockaddr6_t *addr, uint32_t addrlen)
sizeof(tcp_hc_context_t));
#endif
set_tcp_cb(&current_tcp_socket->tcp_control, 0, STATIC_WINDOW,
set_tcp_cb(&current_tcp_socket->tcp_control, 0, DESTINY_SOCKET_STATIC_WINDOW,
current_tcp_socket->tcp_control.send_iss,
current_tcp_socket->tcp_control.send_iss, 0);
@ -608,7 +623,7 @@ int connect(int socket, sockaddr6_t *addr, uint32_t addrlen)
*((uint16_t *)(((uint8_t *)tcp_header) + TCP_HDR_LEN + 2));
}
else {
current_tcp_socket->tcp_control.mss = STATIC_MSS;
current_tcp_socket->tcp_control.mss = DESTINY_SOCKET_STATIC_MSS;
}
current_tcp_socket->tcp_control.rcv_irs = tcp_header->seq_nr;
@ -660,7 +675,7 @@ int connect(int socket, sockaddr6_t *addr, uint32_t addrlen)
current_int_tcp_socket->recv_pid = 255;
print_sockets();
destiny_socket_print_sockets();
return 0;
}
@ -695,7 +710,7 @@ void calculate_rto(tcp_cb_t *tcp_control, long current_time)
tcp_control->rto = rto;
}
int32_t send(int s, void *msg, uint32_t len, int flags)
int32_t destiny_socket_send(int s, const void *buf, uint32_t len, int flags)
{
/* Variables */
msg_t recv_msg;
@ -708,11 +723,11 @@ int32_t send(int s, void *msg, uint32_t len, int flags)
tcp_hdr_t *current_tcp_packet = ((tcp_hdr_t *)(&send_buffer[IPV6_HDR_LEN]));
/* Check if socket exists and is TCP socket */
if (!isTCPSocket(s)) {
if (!is_tcp_socket(s)) {
return -1;
}
current_int_tcp_socket = getSocket(s);
current_int_tcp_socket = get_socket(s);
current_tcp_socket = &current_int_tcp_socket->socket_values;
/* Check for ESTABLISHED STATE */
@ -742,14 +757,14 @@ int32_t send(int s, void *msg, uint32_t len, int flags)
current_tcp_socket->tcp_control.mss) {
/* Window size > Maximum Segment Size */
if ((len - total_sent_bytes) > current_tcp_socket->tcp_control.mss) {
memcpy(&send_buffer[IPV6_HDR_LEN + TCP_HDR_LEN], msg,
memcpy(&send_buffer[IPV6_HDR_LEN + TCP_HDR_LEN], buf,
current_tcp_socket->tcp_control.mss);
sent_bytes = current_tcp_socket->tcp_control.mss;
total_sent_bytes += sent_bytes;
}
else {
memcpy(&send_buffer[IPV6_HDR_LEN + TCP_HDR_LEN],
msg + total_sent_bytes, len - total_sent_bytes);
buf + total_sent_bytes, len - total_sent_bytes);
sent_bytes = len - total_sent_bytes;
total_sent_bytes = len;
}
@ -757,14 +772,14 @@ int32_t send(int s, void *msg, uint32_t len, int flags)
else {
/* Window size <= Maximum Segment Size */
if ((len - total_sent_bytes) > current_tcp_socket->tcp_control.send_wnd) {
memcpy(&send_buffer[IPV6_HDR_LEN + TCP_HDR_LEN], msg,
memcpy(&send_buffer[IPV6_HDR_LEN + TCP_HDR_LEN], buf,
current_tcp_socket->tcp_control.send_wnd);
sent_bytes = current_tcp_socket->tcp_control.send_wnd;
total_sent_bytes += sent_bytes;
}
else {
memcpy(&send_buffer[IPV6_HDR_LEN + TCP_HDR_LEN],
msg + total_sent_bytes, len - total_sent_bytes);
buf + total_sent_bytes, len - total_sent_bytes);
sent_bytes = len - total_sent_bytes;
total_sent_bytes = len;
}
@ -895,7 +910,7 @@ uint8_t read_from_socket(socket_internal_t *current_int_tcp_socket,
}
}
int recv(int s, void *buf, uint32_t len, int flags)
int32_t destiny_socket_recv(int s, void *buf, uint32_t len, int flags)
{
/* Variables */
uint8_t read_bytes;
@ -903,12 +918,12 @@ int recv(int s, void *buf, uint32_t len, int flags)
socket_internal_t *current_int_tcp_socket;
/* Check if socket exists */
if (!isTCPSocket(s)) {
if (!is_tcp_socket(s)) {
printf("INFO: NO TCP SOCKET!\n");
return -1;
}
current_int_tcp_socket = getSocket(s);
current_int_tcp_socket = get_socket(s);
/* Setting Thread PID */
current_int_tcp_socket->recv_pid = thread_getpid();
@ -937,15 +952,15 @@ int recv(int s, void *buf, uint32_t len, int flags)
return -1;
}
int32_t recvfrom(int s, void *buf, uint32_t len, int flags, sockaddr6_t *from,
uint32_t *fromlen)
int32_t destiny_socket_recvfrom(int s, void *buf, uint32_t len, int flags,
sockaddr6_t *from, uint32_t *fromlen)
{
if (isUDPSocket(s)) {
msg_t m_recv, m_send;
ipv6_hdr_t *ipv6_header;
udp_hdr_t *udp_header;
uint8_t *payload;
getSocket(s)->recv_pid = thread_getpid();
get_socket(s)->recv_pid = thread_getpid();
msg_receive(&m_recv);
@ -964,8 +979,8 @@ int32_t recvfrom(int s, void *buf, uint32_t len, int flags, sockaddr6_t *from,
msg_reply(&m_recv, &m_send);
return udp_header->length - UDP_HDR_LEN;
}
else if (isTCPSocket(s)) {
return recv(s, buf, len, flags);
else if (is_tcp_socket(s)) {
return destiny_socket_recv(s, buf, len, flags);
}
else {
printf("Socket Type not supported!\n");
@ -973,11 +988,11 @@ int32_t recvfrom(int s, void *buf, uint32_t len, int flags, sockaddr6_t *from,
}
}
int32_t sendto(int s, const void *msg, uint32_t len, int flags,
sockaddr6_t *to, uint32_t tolen)
int32_t destiny_socket_sendto(int s, const void *buf, uint32_t len, int flags,
sockaddr6_t *to, uint32_t tolen)
{
if (isUDPSocket(s) &&
(getSocket(s)->socket_values.foreign_address.sin6_port == 0)) {
(get_socket(s)->socket_values.foreign_address.sin6_port == 0)) {
uint8_t send_buffer[BUFFER_SIZE];
ipv6_hdr_t *temp_ipv6_header = ((ipv6_hdr_t *)(&send_buffer));
@ -991,7 +1006,7 @@ int32_t sendto(int s, const void *msg, uint32_t len, int flags,
current_udp_packet->dst_port = to->sin6_port;
current_udp_packet->checksum = 0;
memcpy(payload, msg, len);
memcpy(payload, buf, len);
current_udp_packet->length = UDP_HDR_LEN + len;
temp_ipv6_header->length = UDP_HDR_LEN + len;
@ -1008,12 +1023,12 @@ int32_t sendto(int s, const void *msg, uint32_t len, int flags,
}
}
int close(int s)
int destiny_socket_close(int s)
{
socket_internal_t *current_socket = getSocket(s);
socket_internal_t *current_socket = get_socket(s);
if (current_socket != NULL) {
if (isTCPSocket(s)) {
if (is_tcp_socket(s)) {
/* Variables */
msg_t m_recv;
uint8_t send_buffer[BUFFER_SIZE];
@ -1021,14 +1036,14 @@ int close(int s)
tcp_hdr_t *current_tcp_packet = ((tcp_hdr_t *)(&send_buffer[IPV6_HDR_LEN]));
/* Check if socket exists and is TCP socket */
if (!isTCPSocket(s)) {
if (!is_tcp_socket(s)) {
return -1;
}
/* Check for ESTABLISHED STATE */
if (current_socket->socket_values.tcp_control.state != ESTABLISHED) {
close_socket(current_socket);
return 1;
return 0;
}
current_socket->send_pid = thread_getpid();
@ -1049,7 +1064,7 @@ int close(int s)
}
else if (isUDPSocket(s)) {
close_socket(current_socket);
return 1;
return 0;
}
return -1;
@ -1059,10 +1074,10 @@ int close(int s)
}
}
int bind(int s, sockaddr6_t *name, int namelen)
int destiny_socket_bind(int s, sockaddr6_t *addr, int addrlen)
{
if (exists_socket(s)) {
socket_t *current_socket = &getSocket(s)->socket_values;
socket_t *current_socket = &get_socket(s)->socket_values;
switch(current_socket->domain) {
case (PF_INET): {
@ -1077,7 +1092,7 @@ int bind(int s, sockaddr6_t *name, int namelen)
case (SOCK_STREAM): {
if ((current_socket->protocol == 0) ||
(current_socket->protocol == IPPROTO_TCP)) {
return bind_tcp_socket(s, name, namelen,
return bind_tcp_socket(s, addr, addrlen,
thread_getpid());
break;
}
@ -1093,7 +1108,7 @@ int bind(int s, sockaddr6_t *name, int namelen)
case (SOCK_DGRAM): {
if ((current_socket->protocol == 0) ||
(current_socket->protocol == IPPROTO_UDP)) {
return bind_udp_socket(s, name, namelen,
return bind_udp_socket(s, addr, addrlen,
thread_getpid());
break;
}
@ -1141,10 +1156,10 @@ int bind(int s, sockaddr6_t *name, int namelen)
return -1;
}
int listen(int s, int backlog)
int destiny_socket_listen(int s, int backlog)
{
if (isTCPSocket(s) && getSocket(s)->socket_values.tcp_control.state == CLOSED) {
socket_internal_t *current_socket = getSocket(s);
if (is_tcp_socket(s) && get_socket(s)->socket_values.tcp_control.state == CLOSED) {
socket_internal_t *current_socket = get_socket(s);
current_socket->socket_values.tcp_control.state = LISTEN;
return 0;
}
@ -1153,15 +1168,15 @@ int listen(int s, int backlog)
}
}
socket_internal_t *getWaitingConnectionSocket(int socket,
ipv6_hdr_t *ipv6_header,
tcp_hdr_t *tcp_header)
socket_internal_t *get_waiting_connection_socket(int socket,
ipv6_hdr_t *ipv6_header,
tcp_hdr_t *tcp_header)
{
int i;
socket_internal_t *current_socket, *listening_socket = getSocket(socket);
socket_internal_t *current_socket, *listening_socket = get_socket(socket);
for (i = 1; i < MAX_SOCKETS + 1; i++) {
current_socket = getSocket(i);
current_socket = get_socket(i);
/* Connection establishment ACK, Check for 4 touple and state */
if ((ipv6_header != NULL) && (tcp_header != NULL)) {
@ -1264,18 +1279,18 @@ int handle_new_tcp_connection(socket_internal_t *current_queued_int_socket,
/* Waiting for Clients ACK waiting period to time out */
vtimer_usleep(TCP_SYN_INITIAL_TIMEOUT / 2);
print_sockets();
destiny_socket_print_sockets();
return current_queued_int_socket->socket_id;
}
int accept(int s, sockaddr6_t *addr, uint32_t *addrlen)
int destiny_socket_accept(int s, sockaddr6_t *addr, uint32_t *addrlen)
{
socket_internal_t *server_socket = getSocket(s);
socket_internal_t *server_socket = get_socket(s);
if (isTCPSocket(s) && (server_socket->socket_values.tcp_control.state == LISTEN)) {
if (is_tcp_socket(s) && (server_socket->socket_values.tcp_control.state == LISTEN)) {
socket_internal_t *current_queued_socket =
getWaitingConnectionSocket(s, NULL, NULL);
get_waiting_connection_socket(s, NULL, NULL);
if (current_queued_socket != NULL) {
return handle_new_tcp_connection(current_queued_socket,
@ -1290,7 +1305,7 @@ int accept(int s, sockaddr6_t *addr, uint32_t *addrlen)
msg_receive(&msg_recv_client_syn);
}
current_queued_socket = getWaitingConnectionSocket(s, NULL, NULL);
current_queued_socket = get_waiting_connection_socket(s, NULL, NULL);
return handle_new_tcp_connection(current_queued_socket,
server_socket, thread_getpid());
@ -1306,8 +1321,8 @@ socket_internal_t *new_tcp_queued_socket(ipv6_hdr_t *ipv6_header,
{
int queued_socket_id;
queued_socket_id = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
socket_internal_t *current_queued_socket = getSocket(queued_socket_id);
queued_socket_id = destiny_socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
socket_internal_t *current_queued_socket = get_socket(queued_socket_id);
/* Foreign address */
set_socket_address(&current_queued_socket->socket_values.foreign_address,
@ -1326,7 +1341,7 @@ socket_internal_t *new_tcp_queued_socket(ipv6_hdr_t *ipv6_header,
*((uint16_t *)(((uint8_t *)tcp_header) + TCP_HDR_LEN + 2));
}
else {
current_queued_socket->socket_values.tcp_control.mss = STATIC_MSS;
current_queued_socket->socket_values.tcp_control.mss = DESTINY_SOCKET_STATIC_MSS;
}
current_queued_socket->socket_values.tcp_control.rcv_irs =
@ -1337,7 +1352,7 @@ socket_internal_t *new_tcp_queued_socket(ipv6_hdr_t *ipv6_header,
mutex_unlock(&global_sequence_clunter_mutex);
current_queued_socket->socket_values.tcp_control.state = SYN_RCVD;
set_tcp_cb(&current_queued_socket->socket_values.tcp_control,
tcp_header->seq_nr + 1, STATIC_WINDOW,
tcp_header->seq_nr + 1, DESTINY_SOCKET_STATIC_WINDOW,
current_queued_socket->socket_values.tcp_control.send_iss,
current_queued_socket->socket_values.tcp_control.send_iss,
tcp_header->window);

View File

@ -1,151 +1,18 @@
/**
* Destiny socket API
*
* Copyright (C) 2013 INRIA.
*
* This file subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*
* @ingroup destiny
* @{
* @file socket.h
* @brief header for BSD socket API
* @author Oliver Gesch <oliver.gesch@googlemail.com>
* @}
*/
#ifndef SOCKET_H_
#define SOCKET_H_
#include <stdint.h>
#include "ipv6.h"
#include "tcp.h"
#include "udp.h"
#include "destiny/in.h"
#include "ipv6.h"
#ifndef _DESTINY_SOCKET
#define _DESTINY_SOCKET
#include "destiny/socket.h"
#include "cpu.h"
/*
* POSIX compatibility
*/
typedef uint8_t sa_family_t;
typedef uint32_t socklen_t;
/*
* Types
*/
#define SOCK_STREAM 1 /* stream socket */
#define SOCK_DGRAM 2 /* datagram socket */
#define SOCK_RAW 3 /* raw-protocol interface */
#define SOCK_RDM 4 /* reliably-delivered message */
#define SOCK_SEQPACKET 5 /* sequenced packet stream */
/*
* Address families.
*/
#define AF_UNSPEC 0 /* unspecified */
#define AF_LOCAL 1 /* local to host (pipes, portals) */
#define AF_UNIX AF_LOCAL /* backward compatibility */
#define AF_INET 2 /* internetwork: UDP, TCP, etc. */
#define AF_IMPLINK 3 /* arpanet imp addresses */
#define AF_PUP 4 /* pup protocols: e.g. BSP */
#define AF_CHAOS 5 /* mit CHAOS protocols */
#define AF_NS 6 /* XEROX NS protocols */
#define AF_ISO 7 /* ISO protocols */
#define AF_OSI AF_ISO
#define AF_ECMA 8 /* European computer manufacturers */
#define AF_DATAKIT 9 /* datakit protocols */
#define AF_CCITT 10 /* CCITT protocols, X.25 etc */
#define AF_SNA 11 /* IBM SNA */
#define AF_DECnet 12 /* DECnet */
#define AF_DLI 13 /* DEC Direct data link interface */
#define AF_LAT 14 /* LAT */
#define AF_HYLINK 15 /* NSC Hyperchannel */
#define AF_APPLETALK 16 /* Apple Talk */
#define AF_ROUTE 17 /* Internal Routing Protocol */
#define AF_LINK 18 /* Link layer interface */
#define pseudo_AF_XTP 19 /* eXpress Transfer Protocol (no AF) */
#define AF_COIP 20 /* connection-oriented IP, aka ST II */
#define AF_CNT 21 /* Computer Network Technology */
#define pseudo_AF_RTIP 22 /* Help Identify RTIP packets */
#define AF_IPX 23 /* Novell Internet Protocol */
#define AF_SIP 24 /* Simple Internet Protocol */
#define pseudo_AF_PIP 25 /* Help Identify PIP packets */
#define AF_ISDN 26 /* Integrated Services Digital Network*/
#define AF_E164 AF_ISDN /* CCITT E.164 recommendation */
#define pseudo_AF_KEY 27 /* Internal key-management function */
#define AF_INET6 28 /* IPv6 */
#define AF_NATM 29 /* native ATM access */
#define AF_ATM 30 /* ATM */
#define pseudo_AF_HDRCMPLT 31 /* Used by BPF to not rewrite headers
* in interface output routine
*/
#define AF_NETGRAPH 32 /* Netgraph sockets */
#define AF_MAX 33
/*
* Protocol families, same as address families for now.
*/
#define PF_UNSPEC AF_UNSPEC
#define PF_LOCAL AF_LOCAL
#define PF_UNIX PF_LOCAL /* backward compatibility */
#define PF_INET AF_INET
#define PF_IMPLINK AF_IMPLINK
#define PF_PUP AF_PUP
#define PF_CHAOS AF_CHAOS
#define PF_NS AF_NS
#define PF_ISO AF_ISO
#define PF_OSI AF_ISO
#define PF_ECMA AF_ECMA
#define PF_DATAKIT AF_DATAKIT
#define PF_CCITT AF_CCITT
#define PF_SNA AF_SNA
#define PF_DECnet AF_DECnet
#define PF_DLI AF_DLI
#define PF_LAT AF_LAT
#define PF_HYLINK AF_HYLINK
#define PF_APPLETALK AF_APPLETALK
#define PF_ROUTE AF_ROUTE
#define PF_LINK AF_LINK
#define PF_XTP pseudo_AF_XTP /* really just proto family, no AF */
#define PF_COIP AF_COIP
#define PF_CNT AF_CNT
#define PF_SIP AF_SIP
#define PF_IPX AF_IPX /* same format as AF_NS */
#define PF_RTIP pseudo_AF_RTIP /* same format as AF_INET */
#define PF_PIP pseudo_AF_PIP
#define PF_ISDN AF_ISDN
#define PF_KEY pseudo_AF_KEY
#define PF_INET6 AF_INET6
#define PF_NATM AF_NATM
#define PF_ATM AF_ATM
#define PF_NETGRAPH AF_NETGRAPH
#define PF_MAX AF_MAX
#include "tcp.h"
#include "udp.h"
#define MAX_SOCKETS 5
// #define MAX_QUEUED_SOCKETS 2
#define EPHEMERAL_PORTS 49152
#define STATIC_MSS 48
#define STATIC_WINDOW 1 * STATIC_MSS
#define MAX_TCP_BUFFER 1 * STATIC_WINDOW
#define INC_PACKET 0
#define OUT_PACKET 1
#define SEND_MSG_BUF_SIZE 64
typedef struct socka6 {
uint8_t sin6_family; /* AF_INET6 */
uint16_t sin6_port; /* transport layer port # */
uint32_t sin6_flowinfo; /* IPv6 flow information */
ipv6_addr_t sin6_addr; /* IPv6 address */
} sockaddr6_t;
typedef struct tcp_hc_con {
typedef struct __attribute__((packed)) {
uint16_t context_id;
uint32_t seq_rcv; // Last received packet values
uint32_t ack_rcv;
@ -156,7 +23,7 @@ typedef struct tcp_hc_con {
uint8_t hc_type;
} tcp_hc_context_t;
typedef struct tcp_control_block {
typedef struct __attribute__((packed)) {
uint32_t send_una;
uint32_t send_nxt;
uint16_t send_wnd;
@ -179,10 +46,9 @@ typedef struct tcp_control_block {
#ifdef TCP_HC
tcp_hc_context_t tcp_context;
#endif
} tcp_cb_t;
typedef struct sock_t {
typedef struct __attribute__((packed)) {
uint8_t domain;
uint8_t type;
uint8_t protocol;
@ -191,60 +57,38 @@ typedef struct sock_t {
sockaddr6_t foreign_address;
} socket_t;
typedef struct socket_in_t {
typedef struct __attribute__((packed)) {
uint8_t socket_id;
uint8_t recv_pid;
uint8_t send_pid;
uint8_t tcp_input_buffer_end;
mutex_t tcp_buffer_mutex;
socket_t socket_values;
uint8_t tcp_input_buffer[MAX_TCP_BUFFER];
uint8_t tcp_input_buffer[DESTINY_SOCKET_MAX_TCP_BUFFER];
} socket_internal_t;
extern socket_internal_t sockets[MAX_SOCKETS];
int socket(int domain, int type, int protocol);
int connect(int socket, sockaddr6_t *addr, socklen_t addrlen);
socket_internal_t *getWaitingConnectionSocket(int socket,
ipv6_hdr_t *ipv6_header,
tcp_hdr_t *tcp_header);
socket_internal_t *get_waiting_connection_socket(int socket,
ipv6_hdr_t *ipv6_header,
tcp_hdr_t *tcp_header);
void close_socket(socket_internal_t *current_socket);
int32_t recvfrom(int s, void *buf, uint32_t len, int flags, sockaddr6_t *from,
socklen_t *fromlen);
int32_t sendto(int s, const void *msg, uint32_t len, int flags,
sockaddr6_t *to, socklen_t tolen);
int32_t send(int s, void *msg, uint32_t len, int flags);
int recv(int s, void *buf, uint32_t len, int flags);
int close(int s);
int bind(int s, sockaddr6_t *name, int namelen);
int listen(int s, int backlog);
int accept(int s, sockaddr6_t *addr, socklen_t *addrlen);
void socket_init(void);
socket_internal_t *get_udp_socket(ipv6_hdr_t *ipv6_header, udp_hdr_t *udp_header);
socket_internal_t *get_tcp_socket(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header);
socket_internal_t *getSocket(uint8_t s);
void print_sockets(void);
void print_internal_socket(socket_internal_t *current_socket_internal);
void print_socket(socket_t *current_socket);
void printf_tcp_context(tcp_hc_context_t *current_tcp_context);
bool exists_socket(uint8_t socket);
socket_internal_t *get_socket(int s);
socket_internal_t *get_udp_socket(ipv6_hdr_t *ipv6_header,
udp_hdr_t *udp_header);
socket_internal_t *get_tcp_socket(ipv6_hdr_t *ipv6_header,
tcp_hdr_t *tcp_header);
socket_internal_t *new_tcp_queued_socket(ipv6_hdr_t *ipv6_header,
tcp_hdr_t *tcp_header);
void print_tcp_status(int in_or_out, ipv6_hdr_t *ipv6_header,
tcp_hdr_t *tcp_header, socket_t *tcp_socket);
void set_socket_address(sockaddr6_t *sockaddr, sa_family_t sin6_family,
uint16_t sin6_port, uint32_t sin6_flowinfo,
ipv6_addr_t *sin6_addr);
void set_tcp_cb(tcp_cb_t *tcp_control, uint32_t rcv_nxt, uint16_t rcv_wnd,
uint32_t send_nxt, uint32_t send_una, uint16_t send_wnd);
void set_tcp_packet(tcp_hdr_t *tcp_hdr, uint16_t src_port, uint16_t dst_port,
uint32_t seq_nr, uint32_t ack_nr,
uint8_t dataOffset_reserved, uint8_t reserved_flags,
uint16_t window, uint16_t checksum, uint16_t urg_pointer);
int check_tcp_consistency(socket_t *current_tcp_socket, tcp_hdr_t *tcp_header);
void switch_tcp_packet_byte_order(tcp_hdr_t *current_tcp_packet);
int send_tcp(socket_internal_t *current_socket, tcp_hdr_t *current_tcp_packet,
ipv6_hdr_t *temp_ipv6_header, uint8_t flags,
uint8_t payload_length);
bool isTCPSocket(uint8_t s);
#endif /* SOCKET_H_ */
bool is_tcp_socket(int s);
#endif /* _DESTINY_SOCKET */

View File

@ -27,11 +27,12 @@
#include "tcp_hc.h"
#include "tcp.h"
#include "destiny/in.h"
#include "socket.h"
#include "../net_help/net_help.h"
#include "../net_help/msg_help.h"
#include "sixlowpan.h"
#include "socket.h"
void printTCPHeader(tcp_hdr_t *tcp_header)
{
printf("\nBEGIN: TCP HEADER\n");
@ -122,8 +123,8 @@ void handle_tcp_ack_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header,
msg_send(&m_send_tcp, tcp_socket->send_pid, 0);
return;
}
else if (getWaitingConnectionSocket(tcp_socket->socket_id, ipv6_header,
tcp_header) != NULL) {
else if (get_waiting_connection_socket(tcp_socket->socket_id, ipv6_header,
tcp_header) != NULL) {
m_send_tcp.content.ptr = (char *)tcp_header;
net_msg_send_recv(&m_send_tcp, &m_recv_tcp, tcp_socket->recv_pid, TCP_ACK);
return;
@ -158,10 +159,10 @@ void handle_tcp_syn_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header,
#ifdef TCP_HC
update_tcp_hc_context(true, new_socket, tcp_header);
#endif
/* notify socket function accept(..) that a new connection request
* has arrived. No need to wait for an answer because the server
* accept() function isnt reading from anything other than the
* queued sockets */
/* notify socket function destiny_socket_accept(..) that a new
* connection request has arrived. No need to wait for an answer
* because the server destiny_socket_accept() function isnt reading
* from anything other than the queued sockets */
net_msg_send(&m_send_tcp, tcp_socket->recv_pid, 0, TCP_SYN);
}
else {

View File

@ -21,11 +21,12 @@
#include <stdint.h>
#include "tcp_hc.h"
#include "socket.h"
#include "tcp.h"
#include "ipv6.h"
#include "../net_help/net_help.h"
#include "socket.h"
#ifdef TCP_HC
socket_internal_t *get_tcp_socket_by_context(ipv6_hdr_t *current_ipv6_header,
@ -34,7 +35,7 @@ socket_internal_t *get_tcp_socket_by_context(ipv6_hdr_t *current_ipv6_header,
socket_internal_t *temp_socket;
for (int i = 1; i < MAX_SOCKETS + 1; i++) {
temp_socket = getSocket(i);
temp_socket = get_socket(i);
if ((temp_socket != NULL) &&
ipv6_addr_is_equal(&temp_socket->socket_values.foreign_address.sin6_addr,

View File

@ -10,7 +10,7 @@
#include "tcp.h"
#include "sixlowpan.h"
#include "socket.h"
#include "destiny/socket.h"
#ifdef TCP_HC

View File

@ -25,10 +25,11 @@
#include "vtimer.h"
#include "thread.h"
#include "destiny.h"
#include "socket.h"
#include "../net_help/msg_help.h"
#include "sixlowpan.h"
#include "socket.h"
void handle_synchro_timeout(socket_internal_t *current_socket)
{
msg_t send;
@ -101,9 +102,9 @@ void check_sockets(void)
uint8_t i = 1;
while (i < MAX_SOCKETS + 1) {
current_socket = getSocket(i);
current_socket = get_socket(i);
if (isTCPSocket(i)) {
if (is_tcp_socket(i)) {
switch(current_socket->socket_values.tcp_control.state) {
case ESTABLISHED: {
handle_established(current_socket);

View File

@ -24,11 +24,12 @@
#include "msg.h"
#include "ipv6.h"
#include "sixlowpan.h"
#include "socket.h"
#include "destiny/in.h"
#include "../net_help/net_help.h"
#include "../net_help/msg_help.h"
#include "socket.h"
uint16_t udp_csum(ipv6_hdr_t *ipv6_header, udp_hdr_t *udp_header)
{
uint16_t sum;

View File

@ -1,4 +1,4 @@
MODULE:=$(shell basename $(CURDIR))
INCLUDES += -I$(RIOTBASE)/sys/net/destiny/ -I$(RIOTBASE)/sys/include -I$(RIOTBASE)/core/include -I$(RIOTBASE)/sys/net/sixlowpan/include
INCLUDES += -I$(RIOTBASE)/sys/net/destiny/include -I$(RIOTBASE)/sys/include -I$(RIOTBASE)/core/include -I$(RIOTBASE)/sys/net/sixlowpan/include
include $(RIOTBASE)/Makefile.base

View File

@ -18,7 +18,7 @@
#include <string.h>
#include <stdint.h>
#include "socket.h"
#include "destiny/socket.h"
#include "inet_ntop.h"

View File

@ -18,7 +18,7 @@
#include <string.h>
#include <stdint.h>
#include "socket.h"
#include "destiny/socket.h"
#include "inet_pton.h"

View File

@ -31,6 +31,7 @@
#include "icmp.h"
#include "lowpan.h"
#include "destiny/socket.h"
#include "net_help.h"
#define IP_PKT_RECV_BUF_SIZE (64)