diff options
author | Uros Majstorovic <majstor@majstor.org> | 2018-01-11 21:28:22 +0100 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2018-01-11 21:28:22 +0100 |
commit | 0d0e9facfcea3cf96da3b63285865182fdd5477e (patch) | |
tree | cca42e7aab184cad981c794073a4376aa2616193 /code/ecp/posix | |
parent | 05e11f491b84a110b9ca08612df370b91e9bdcbc (diff) |
refatoring dirs
Diffstat (limited to 'code/ecp/posix')
-rw-r--r-- | code/ecp/posix/Makefile | 20 | ||||
-rw-r--r-- | code/ecp/posix/time.c | 24 | ||||
-rw-r--r-- | code/ecp/posix/transport.c | 116 | ||||
-rw-r--r-- | code/ecp/posix/transport.h | 10 |
4 files changed, 170 insertions, 0 deletions
diff --git a/code/ecp/posix/Makefile b/code/ecp/posix/Makefile new file mode 100644 index 0000000..6dd42e2 --- /dev/null +++ b/code/ecp/posix/Makefile @@ -0,0 +1,20 @@ +include ../../Makefile.platform +CFLAGS=$(CFLAGS_PL) $(PIC) -I.. + +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/ecp/posix/time.c b/code/ecp/posix/time.c new file mode 100644 index 0000000..c7a238a --- /dev/null +++ b/code/ecp/posix/time.c @@ -0,0 +1,24 @@ +#include <unistd.h> +#include <sys/time.h> + +#include <core.h> + +static ecp_cts_t t_abstime_ms(ecp_cts_t msec) { + struct timeval tv; + ecp_cts_t ms_now; + + gettimeofday(&tv, NULL); + ms_now = tv.tv_sec * 1000 + tv.tv_usec / 1000; + return ms_now + msec; +} + +static void t_sleep_ms(ecp_cts_t 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/ecp/posix/transport.c b/code/ecp/posix/transport.c new file mode 100644 index 0000000..df8e0f5 --- /dev/null +++ b/code/ecp/posix/transport.c @@ -0,0 +1,116 @@ +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <poll.h> +#include <stdio.h> + +#include <core.h> + +#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; + uint16_t hport; + + 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; + hport = strtol(colon, &endptr, 10); + if (*endptr != '\0') return -1; + addr->port = htons(hport); + + 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 = 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 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 = 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, int timeout) { + struct sockaddr_in servaddr; + socklen_t addrlen = sizeof(servaddr); + struct pollfd fds[] = { + {*sock, POLLIN, 0} + }; + + int rv = poll(fds, 1, timeout); + memset((void *)&servaddr, 0, sizeof(servaddr)); + if (rv == 1) { + ssize_t recvlen = recvfrom(*sock, msg, msg_size, 0, (struct sockaddr *)&servaddr, &addrlen); + if (recvlen < 0) return recvlen; + if (recvlen < ECP_MIN_PKT) return ECP_ERR_RECV; + + if (addr) { + addr->port = servaddr.sin_port; + memcpy(addr->host, (void *)&servaddr.sin_addr, sizeof(addr->host)); + } + return recvlen; + } + return ECP_ERR_TIMEOUT; +} + +int ecp_transport_init(ECPTransportIface *t) { + t->init = 1; + t->open = t_open; + t->close = t_close; + t->send = t_send; + t->recv = t_recv; + t->addr_eq = t_addr_eq; + t->addr_set = t_addr_set; + return ECP_OK; +} diff --git a/code/ecp/posix/transport.h b/code/ecp/posix/transport.h new file mode 100644 index 0000000..c35e13e --- /dev/null +++ b/code/ecp/posix/transport.h @@ -0,0 +1,10 @@ +#include <stdint.h> + +#define ECP_IPv4_ADDR_SIZE 4 + +typedef int ECPNetSock; +typedef struct ECPNetAddr { + unsigned char host[ECP_IPv4_ADDR_SIZE]; + uint16_t port; +} ECPNetAddr; + |