From 25de5e761daab8b897a4f09ff8503e6f43c299f9 Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Wed, 3 May 2017 21:10:08 +0200 Subject: initial commit --- code/posix/Makefile | 17 ++++++++ code/posix/time.c | 24 +++++++++++ code/posix/transport.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++ code/posix/transport.h | 10 +++++ 4 files changed, 165 insertions(+) create mode 100644 code/posix/Makefile create mode 100644 code/posix/time.c create mode 100644 code/posix/transport.c create mode 100644 code/posix/transport.h (limited to 'code/posix') diff --git a/code/posix/Makefile b/code/posix/Makefile new file mode 100644 index 0000000..64cf5f6 --- /dev/null +++ b/code/posix/Makefile @@ -0,0 +1,17 @@ +CFLAGS=-I.. $(PIC) +obj_tr = transport.o +obj_tm = time.o + +%.o: %.c + $(CC) $(CFLAGS) -c $< + +all: libecptr.a libecptm.a + +libecptr.a: $(obj_tr) + $(AR) rcs libecptr.a $(obj_tr) + +libecptm.a: $(obj_tm) + $(AR) rcs libecptm.a $(obj_tm) + +clean: + rm -f *.o *.a diff --git a/code/posix/time.c b/code/posix/time.c new file mode 100644 index 0000000..3dd3920 --- /dev/null +++ b/code/posix/time.c @@ -0,0 +1,24 @@ +#include +#include + +#include + +static unsigned int t_abstime_ms(unsigned int msec) { + struct timeval tv; + unsigned int ms_now; + + gettimeofday(&tv, NULL); + ms_now = tv.tv_sec * 1000 + tv.tv_usec / 1000; + return ms_now + msec; +} + +static void t_sleep_ms(unsigned int msec) { + usleep(msec*1000); +} + +int ecp_time_init(ECPTimeIface *t) { + t->init = 1; + t->abstime_ms = t_abstime_ms; + t->sleep_ms = t_sleep_ms; + return 0; +} diff --git a/code/posix/transport.c b/code/posix/transport.c new file mode 100644 index 0000000..1787c66 --- /dev/null +++ b/code/posix/transport.c @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include +#include +#include + +#include + +#define ADDR_S_MAX 32 + +static int t_addr_eq(ECPNetAddr *addr1, ECPNetAddr *addr2) { + if (addr1->port != addr2->port) return 0; + if (memcmp(addr1->host, addr2->host, sizeof(addr1->host)) != 0) return 0; + return 1; +} + +static int t_addr_set(ECPNetAddr *addr, void *addr_s) { + int rv; + char addr_c[ADDR_S_MAX]; + char *colon = NULL; + char *endptr = NULL; + + memset(addr_c, 0, sizeof(addr_c)); + strncpy(addr_c, addr_s, ADDR_S_MAX-1); + colon = strchr(addr_c, ':'); + if (colon == NULL) return -1; + *colon = '\0'; + colon++; + if (*colon == '\0') return -1; + rv = inet_pton(AF_INET, addr_c, addr->host); + if (rv != 1) return -1; + addr->port = strtol(colon, &endptr, 10); + if (*endptr != '\0') return -1; + + return 0; +} + +static int t_open(int *sock, void *addr_s) { + struct sockaddr_in _myaddr; + + memset((char *)&_myaddr, 0, sizeof(_myaddr)); + _myaddr.sin_family = AF_INET; + if (addr_s) { + ECPNetAddr addr; + int rv = t_addr_set(&addr, addr_s); + if (rv) return rv; + + memcpy((void *)&_myaddr.sin_addr, addr.host, sizeof(addr.host)); + _myaddr.sin_port = htons(addr.port); + } else { + _myaddr.sin_addr.s_addr = htonl(INADDR_ANY); + _myaddr.sin_port = htons(0); + } + + *sock = socket(PF_INET, SOCK_DGRAM, 0); + if (*sock < 0) return *sock; + + int rv = bind(*sock, (struct sockaddr *)&_myaddr, sizeof(_myaddr)); + if (rv < 0) { + close(*sock); + return rv; + } + return 0; +} + +static void t_close(int *sock) { + close(*sock); +} + +static int t_poll(int *sock, int timeout) { + struct pollfd fds[] = { + {*sock, POLLIN, 0} + }; + return poll(fds, 1, timeout); +} + +static ssize_t t_send(int *sock, void *msg, size_t msg_size, ECPNetAddr *addr) { + struct sockaddr_in servaddr; + + memset((void *)&servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(addr->port); + memcpy((void *)&servaddr.sin_addr, addr->host, sizeof(addr->host)); + return sendto(*sock, msg, msg_size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr)); +} + +static ssize_t t_recv(int *sock, void *msg, size_t msg_size, ECPNetAddr *addr) { + struct sockaddr_in servaddr; + socklen_t addrlen = sizeof(servaddr); + memset((void *)&servaddr, 0, sizeof(servaddr)); + + ssize_t recvlen = recvfrom(*sock, msg, msg_size, 0, (struct sockaddr *)&servaddr, &addrlen); + if (recvlen < 0) return recvlen; + + if (addr) { + addr->port = ntohs(servaddr.sin_port); + memcpy(addr->host, (void *)&servaddr.sin_addr, sizeof(addr->host)); + } + return recvlen; +} + +int ecp_transport_init(ECPTransportIface *t) { + t->init = 1; + t->open = t_open; + t->close = t_close; + t->poll = t_poll; + t->send = t_send; + t->recv = t_recv; + t->addr_eq = t_addr_eq; + t->addr_set = t_addr_set; + return 0; +} diff --git a/code/posix/transport.h b/code/posix/transport.h new file mode 100644 index 0000000..c35e13e --- /dev/null +++ b/code/posix/transport.h @@ -0,0 +1,10 @@ +#include + +#define ECP_IPv4_ADDR_SIZE 4 + +typedef int ECPNetSock; +typedef struct ECPNetAddr { + unsigned char host[ECP_IPv4_ADDR_SIZE]; + uint16_t port; +} ECPNetAddr; + -- cgit v1.2.3