Commit 34adfd2a authored by Robert Schmidt's avatar Robert Schmidt

ASYNC_IF link_{send,receive}_packet(): infer socket type automatically

parent ec9e70a0
...@@ -100,8 +100,6 @@ typedef uint8_t mod_id_t; // module or enb id ...@@ -100,8 +100,6 @@ typedef uint8_t mod_id_t; // module or enb id
typedef uint8_t lcid_t; typedef uint8_t lcid_t;
typedef int32_t err_code_t; typedef int32_t err_code_t;
#define CHANNEL_UDP 1
typedef struct { typedef struct {
/* general info */ /* general info */
......
...@@ -47,7 +47,7 @@ static void *link_manager_sender_thread(void *_manager) ...@@ -47,7 +47,7 @@ static void *link_manager_sender_thread(void *_manager)
while (manager->run) { while (manager->run) {
while (message_get(manager->send_queue, &data, &size, &priority) == 0) { while (message_get(manager->send_queue, &data, &size, &priority) == 0) {
link_send_packet(manager->socket_link, data, size, manager->type, manager->peer_addr, manager->port); link_send_packet(manager->socket_link, data, size, manager->peer_addr, manager->port);
free(data); free(data);
} }
// if (message_get(manager->send_queue, &data, &size, &priority)) // if (message_get(manager->send_queue, &data, &size, &priority))
...@@ -76,7 +76,7 @@ static void *link_manager_receiver_thread(void *_manager) ...@@ -76,7 +76,7 @@ static void *link_manager_receiver_thread(void *_manager)
LOG_D(MAC, "starting link manager receiver thread\n"); LOG_D(MAC, "starting link manager receiver thread\n");
while (manager->run) { while (manager->run) {
if (link_receive_packet(manager->socket_link, &data, &size, manager->type)) if (link_receive_packet(manager->socket_link, &data, &size))
goto error; goto error;
/* todo: priority */ /* todo: priority */
if (message_put(manager->receive_queue, data, size, 0)) if (message_put(manager->receive_queue, data, size, 0))
......
...@@ -68,6 +68,7 @@ socket_link_t *new_link_server(int port) ...@@ -68,6 +68,7 @@ socket_link_t *new_link_server(int port)
LOG_E(MAC, "%s:%d: socket: %s\n", __FILE__, __LINE__, strerror(errno)); LOG_E(MAC, "%s:%d: socket: %s\n", __FILE__, __LINE__, strerror(errno));
goto error; goto error;
} }
ret->type = SOCK_STREAM;
reuse = 1; reuse = 1;
if (setsockopt(socket_server, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) { if (setsockopt(socket_server, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) {
...@@ -135,6 +136,7 @@ socket_link_t *new_link_client(const char *server, int port) ...@@ -135,6 +136,7 @@ socket_link_t *new_link_client(const char *server, int port)
LOG_E(MAC, "%s:%d: socket: %s\n", __FILE__, __LINE__, strerror(errno)); LOG_E(MAC, "%s:%d: socket: %s\n", __FILE__, __LINE__, strerror(errno));
goto error; goto error;
} }
ret->type = SOCK_STREAM;
no_delay = 1; no_delay = 1;
if (setsockopt(ret->socket_fd, SOL_TCP, TCP_NODELAY, &no_delay, sizeof(no_delay)) == -1) { if (setsockopt(ret->socket_fd, SOL_TCP, TCP_NODELAY, &no_delay, sizeof(no_delay)) == -1) {
...@@ -183,6 +185,7 @@ socket_link_t *new_link_udp_server(const char *bind_addr, int bind_port) ...@@ -183,6 +185,7 @@ socket_link_t *new_link_udp_server(const char *bind_addr, int bind_port)
if ((socket_server=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { if ((socket_server=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
goto error; goto error;
} }
ret->type = SOCK_DGRAM;
// zero out the structure // zero out the structure
memset((char *) &si_me, 0, sizeof(si_me)); memset((char *) &si_me, 0, sizeof(si_me));
...@@ -231,6 +234,7 @@ socket_link_t *new_link_udp_client(const char *server, int port){ ...@@ -231,6 +234,7 @@ socket_link_t *new_link_udp_client(const char *server, int port){
if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){ if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){
goto error; goto error;
} }
ret->type = SOCK_DGRAM;
memset((char *) &si_other, 0, sizeof(si_other)); memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET; si_other.sin_family = AF_INET;
...@@ -302,6 +306,7 @@ socket_link_t *new_link_sctp_server(int port) ...@@ -302,6 +306,7 @@ socket_link_t *new_link_sctp_server(int port)
ret->socket_fd = -1; ret->socket_fd = -1;
ret->socket_fd = accept (listenSock, NULL, NULL); ret->socket_fd = accept (listenSock, NULL, NULL);
ret->type = SOCK_STREAM;
return ret; return ret;
...@@ -334,6 +339,7 @@ socket_link_t *new_link_sctp_client(const char *server, int port) ...@@ -334,6 +339,7 @@ socket_link_t *new_link_sctp_client(const char *server, int port)
perror("socket()"); perror("socket()");
exit(1); exit(1);
} }
ret->type = SOCK_STREAM;
bzero ((void *) &servaddr, sizeof (servaddr)); bzero ((void *) &servaddr, sizeof (servaddr));
servaddr.sin_family = AF_INET; servaddr.sin_family = AF_INET;
...@@ -469,57 +475,54 @@ socket_closed: ...@@ -469,57 +475,54 @@ socket_closed:
/* /*
* return -1 on error and 0 if the sending was fine * return -1 on error and 0 if the sending was fine
*/ */
int link_send_packet(socket_link_t *link, void *data, int size, uint16_t proto_type, const char *peer_addr, int peer_port) int link_send_packet(socket_link_t *link, void *data, int size, const char *peer_addr, int peer_port)
{ {
char sizebuf[4]; char sizebuf[4];
int32_t s = size; int32_t s = size;
switch (link->type) {
if ((proto_type == 0) || (proto_type == 2)) case SOCK_STREAM:
{
/* send the size first, maximum is 2^31 bytes */ /* send the size first, maximum is 2^31 bytes */
sizebuf[0] = (s >> 24) & 255; sizebuf[0] = (s >> 24) & 255;
sizebuf[1] = (s >> 16) & 255; sizebuf[1] = (s >> 16) & 255;
sizebuf[2] = (s >> 8) & 255; sizebuf[2] = (s >> 8) & 255;
sizebuf[3] = s & 255; sizebuf[3] = s & 255;
if (socket_send(link->socket_fd, sizebuf, 4) == -1) if (socket_send(link->socket_fd, sizebuf, 4) == -1)
goto error; return -1;
link->bytes_sent += 4; link->bytes_sent += 4;
if (socket_send(link->socket_fd, data, size) == -1) if (socket_send(link->socket_fd, data, size) == -1)
goto error; return -1;
break;
} case SOCK_DGRAM:
else if (proto_type == 1 )
{
/* UDP is connectionless -> only send the data */ /* UDP is connectionless -> only send the data */
if (socket_udp_send(link->socket_fd, data, size, peer_addr, peer_port) == -1) if (socket_udp_send(link->socket_fd, data, size, peer_addr, peer_port) == -1)
goto error; return -1;
break;
default:
LOG_E(MAC, "unknown socket type %d\n", link->type);
return -1;
} }
link->bytes_sent += size; link->bytes_sent += size;
link->packets_sent++; link->packets_sent++;
return 0; return 0;
error:
return -1;
} }
/* /*
* return -1 on error and 0 if the sending was fine * return -1 on error and 0 if the sending was fine
*/ */
int link_receive_packet(socket_link_t *link, void **ret_data, int *ret_size, uint16_t proto_type) int link_receive_packet(socket_link_t *link, void **ret_data, int *ret_size)
{ {
unsigned char sizebuf[4]; unsigned char sizebuf[4];
int32_t size = 0; int32_t size = 0;
void *data = NULL; void *data = NULL;
/* received the size first, maximum is 2^31 bytes */ /* received the size first, maximum is 2^31 bytes */
if ((proto_type == 0) || (proto_type == 2)) switch (link->type) {
{ case SOCK_STREAM:
if (socket_receive(link->socket_fd, sizebuf, 4) == -1) if (socket_receive(link->socket_fd, sizebuf, 4) == -1)
goto error; goto error;
...@@ -538,9 +541,8 @@ int link_receive_packet(socket_link_t *link, void **ret_data, int *ret_size, uin ...@@ -538,9 +541,8 @@ int link_receive_packet(socket_link_t *link, void **ret_data, int *ret_size, uin
if (socket_receive(link->socket_fd, data, size) == -1) if (socket_receive(link->socket_fd, data, size) == -1)
goto error; goto error;
} break;
else if (proto_type == 1) case SOCK_DGRAM:
{
/* we get a single packet (no size, UDP could lose it). Therefore, prepare /* we get a single packet (no size, UDP could lose it). Therefore, prepare
* for the maximum UDP packet size */ * for the maximum UDP packet size */
size = 65535; size = 65535;
...@@ -553,6 +555,10 @@ int link_receive_packet(socket_link_t *link, void **ret_data, int *ret_size, uin ...@@ -553,6 +555,10 @@ int link_receive_packet(socket_link_t *link, void **ret_data, int *ret_size, uin
size = socket_udp_receive(link->socket_fd, data, size); size = socket_udp_receive(link->socket_fd, data, size);
if (size < 0) if (size < 0)
goto error; goto error;
break;
default:
LOG_E(MAC, "unknown socket type %d\n", link->type);
goto error;
} }
link->bytes_received += size; link->bytes_received += size;
......
...@@ -37,9 +37,10 @@ ...@@ -37,9 +37,10 @@
extern "C" { extern "C" {
#endif #endif
typedef struct { typedef struct {
int socket_fd; int socket_fd;
int peer_port; int type;
uint64_t bytes_sent; uint64_t bytes_sent;
uint64_t packets_sent; uint64_t packets_sent;
uint64_t bytes_received; uint64_t bytes_received;
...@@ -53,8 +54,8 @@ socket_link_t *new_link_udp_server(const char *bind_addr, int bind_port); ...@@ -53,8 +54,8 @@ socket_link_t *new_link_udp_server(const char *bind_addr, int bind_port);
socket_link_t *new_link_udp_client(const char *server, int port); socket_link_t *new_link_udp_client(const char *server, int port);
socket_link_t *new_link_sctp_server(int port); socket_link_t *new_link_sctp_server(int port);
socket_link_t *new_link_sctp_client(const char *server, int port); socket_link_t *new_link_sctp_client(const char *server, int port);
int link_send_packet(socket_link_t *link, void *data, int size, uint16_t proto_type, const char *peer_addr, int port); int link_send_packet(socket_link_t *link, void *data, int size, const char *peer_addr, int port);
int link_receive_packet(socket_link_t *link, void **data, int *size, uint16_t proto_type); int link_receive_packet(socket_link_t *link, void **data, int *size);
int close_link(socket_link_t *link); int close_link(socket_link_t *link);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment