refactor: Move plain connection code to own folder

This is a step to a more modular project setup.
This commit is contained in:
Lukas Woodtli 2024-06-28 12:32:56 +02:00 committed by Lukas Woodtli
parent 56b60a45d5
commit 88c53d7e43
7 changed files with 68 additions and 108 deletions

View File

@ -35,9 +35,9 @@
#include <signal.h>
#include <inttypes.h>
#include "commandline.h"
#include "connection.h"
#include "bootstrap_info.h"
#include "commandline.h"
#include "udp/connection.h"
#define CMD_STATUS_NEW 0
#define CMD_STATUS_SENT 1

View File

@ -62,7 +62,7 @@
#ifdef WITH_TINYDTLS
#include "dtlsconnection.h"
#else
#include "connection.h"
#include "udp/connection.h"
#endif
#include <arpa/inet.h>

View File

@ -56,7 +56,7 @@
*/
#include "liblwm2m.h"
#include "connection.h"
#include "udp/connection.h"
#include <arpa/inet.h>
#include <ctype.h>

View File

@ -72,7 +72,7 @@
#include <inttypes.h>
#include "commandline.h"
#include "connection.h"
#include "udp/connection.h"
#define MAX_PACKET_SIZE 2048

View File

@ -16,18 +16,17 @@
*
*******************************************************************************/
#include "udp/connection.h"
#include "commandline.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "connection.h"
#include "commandline.h"
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
int create_socket(const char * portStr, int addressFamily)
{
int create_socket(const char *portStr, int addressFamily) {
int s = -1;
struct addrinfo hints;
struct addrinfo *res;
@ -38,18 +37,14 @@ int create_socket(const char * portStr, int addressFamily)
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
if (0 != getaddrinfo(NULL, portStr, &hints, &res))
{
if (0 != getaddrinfo(NULL, portStr, &hints, &res)) {
return -1;
}
for(p = res ; p != NULL && s == -1 ; p = p->ai_next)
{
for (p = res; p != NULL && s == -1; p = p->ai_next) {
s = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (s >= 0)
{
if (-1 == bind(s, p->ai_addr, p->ai_addrlen))
{
if (s >= 0) {
if (-1 == bind(s, p->ai_addr, p->ai_addrlen)) {
close(s);
s = -1;
}
@ -61,18 +56,12 @@ int create_socket(const char * portStr, int addressFamily)
return s;
}
connection_t * connection_find(connection_t * connList,
struct sockaddr_storage * addr,
size_t addrLen)
{
connection_t * connP;
connection_t *connection_find(connection_t *connList, struct sockaddr_storage *addr, size_t addrLen) {
connection_t *connP;
connP = connList;
while (connP != NULL)
{
if ((connP->addrLen == addrLen)
&& (memcmp(&(connP->addr), addr, addrLen) == 0))
{
while (connP != NULL) {
if ((connP->addrLen == addrLen) && (memcmp(&(connP->addr), addr, addrLen) == 0)) {
return connP;
}
connP = connP->next;
@ -81,16 +70,11 @@ connection_t * connection_find(connection_t * connList,
return connP;
}
connection_t * connection_new_incoming(connection_t * connList,
int sock,
struct sockaddr * addr,
size_t addrLen)
{
connection_t * connP;
connection_t *connection_new_incoming(connection_t *connList, int sock, struct sockaddr *addr, size_t addrLen) {
connection_t *connP;
connP = (connection_t *)lwm2m_malloc(sizeof(connection_t));
if (connP != NULL)
{
if (connP != NULL) {
connP->sock = sock;
memcpy(&(connP->addr), addr, addrLen);
connP->addrLen = addrLen;
@ -100,44 +84,36 @@ connection_t * connection_new_incoming(connection_t * connList,
return connP;
}
connection_t * connection_create(connection_t * connList,
int sock,
char * host,
char * port,
int addressFamily)
{
connection_t *connection_create(connection_t *connList, int sock, char *host, char *port, int addressFamily) {
struct addrinfo hints;
struct addrinfo *servinfo = NULL;
struct addrinfo *p;
int s;
struct sockaddr *sa;
socklen_t sl;
connection_t * connP = NULL;
connection_t *connP = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = addressFamily;
hints.ai_socktype = SOCK_DGRAM;
if (0 != getaddrinfo(host, port, &hints, &servinfo) || servinfo == NULL) return NULL;
if (0 != getaddrinfo(host, port, &hints, &servinfo) || servinfo == NULL)
return NULL;
// we test the various addresses
s = -1;
for(p = servinfo ; p != NULL && s == -1 ; p = p->ai_next)
{
for (p = servinfo; p != NULL && s == -1; p = p->ai_next) {
s = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (s >= 0)
{
if (s >= 0) {
sa = p->ai_addr;
sl = p->ai_addrlen;
if (-1 == connect(s, p->ai_addr, p->ai_addrlen))
{
if (-1 == connect(s, p->ai_addr, p->ai_addrlen)) {
close(s);
s = -1;
}
}
}
if (s >= 0)
{
if (s >= 0) {
connP = connection_new_incoming(connList, sock, sa, sl);
close(s);
}
@ -148,11 +124,9 @@ connection_t * connection_create(connection_t * connList,
return connP;
}
void connection_free(connection_t * connList)
{
while (connList != NULL)
{
connection_t * nextP;
void connection_free(connection_t *connList) {
while (connList != NULL) {
connection_t *nextP;
nextP = connList->next;
lwm2m_free(connList);
@ -161,10 +135,7 @@ void connection_free(connection_t * connList)
}
}
int connection_send(connection_t *connP,
uint8_t * buffer,
size_t length)
{
int connection_send(connection_t *connP, uint8_t *buffer, size_t length) {
int nbSent;
size_t offset;
@ -174,14 +145,11 @@ int connection_send(connection_t *connP,
s[0] = 0;
if (AF_INET == connP->addr.sin6_family)
{
if (AF_INET == connP->addr.sin6_family) {
struct sockaddr_in *saddr = (struct sockaddr_in *)&connP->addr;
inet_ntop(saddr->sin_family, &saddr->sin_addr, s, INET6_ADDRSTRLEN);
port = saddr->sin_port;
}
else if (AF_INET6 == connP->addr.sin6_family)
{
} else if (AF_INET6 == connP->addr.sin6_family) {
struct sockaddr_in6 *saddr = (struct sockaddr_in6 *)&connP->addr;
inet_ntop(saddr->sin6_family, &saddr->sin6_addr, s, INET6_ADDRSTRLEN);
port = saddr->sin6_port;
@ -195,43 +163,35 @@ int connection_send(connection_t *connP,
#endif
offset = 0;
while (offset != length)
{
nbSent = sendto(connP->sock, buffer + offset, length - offset, 0, (struct sockaddr *)&(connP->addr), connP->addrLen);
if (nbSent == -1) return -1;
while (offset != length) {
nbSent =
sendto(connP->sock, buffer + offset, length - offset, 0, (struct sockaddr *)&(connP->addr), connP->addrLen);
if (nbSent == -1)
return -1;
offset += nbSent;
}
return 0;
}
uint8_t lwm2m_buffer_send(void * sessionH,
uint8_t * buffer,
size_t length,
void * userdata)
{
connection_t * connP = (connection_t*) sessionH;
uint8_t lwm2m_buffer_send(void *sessionH, uint8_t *buffer, size_t length, void *userdata) {
connection_t *connP = (connection_t *)sessionH;
(void)userdata; /* unused */
if (connP == NULL)
{
if (connP == NULL) {
fprintf(stderr, "#> failed sending %zu bytes, missing connection\r\n", length);
return COAP_500_INTERNAL_SERVER_ERROR ;
return COAP_500_INTERNAL_SERVER_ERROR;
}
if (-1 == connection_send(connP, buffer, length))
{
if (-1 == connection_send(connP, buffer, length)) {
fprintf(stderr, "#> failed sending %zu bytes\r\n", length);
return COAP_500_INTERNAL_SERVER_ERROR ;
return COAP_500_INTERNAL_SERVER_ERROR;
}
return COAP_NO_ERROR;
}
bool lwm2m_session_is_equal(void * session1,
void * session2,
void * userData)
{
bool lwm2m_session_is_equal(void *session1, void *session2, void *userData) {
(void)userData; /* unused */
return (session1 == session2);

View File

@ -18,38 +18,37 @@
#ifndef CONNECTION_H_
#define CONNECTION_H_
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <liblwm2m.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <liblwm2m.h>
#include <unistd.h>
#define LWM2M_STANDARD_PORT_STR "5683"
#define LWM2M_STANDARD_PORT 5683
#define LWM2M_DTLS_PORT_STR "5684"
#define LWM2M_DTLS_PORT 5684
#define LWM2M_STANDARD_PORT 5683
#define LWM2M_DTLS_PORT_STR "5684"
#define LWM2M_DTLS_PORT 5684
#define LWM2M_BSSERVER_PORT_STR "5685"
#define LWM2M_BSSERVER_PORT 5685
#define LWM2M_BSSERVER_PORT 5685
typedef struct _connection_t
{
struct _connection_t * next;
int sock;
struct sockaddr_in6 addr;
size_t addrLen;
typedef struct _connection_t {
struct _connection_t *next;
int sock;
struct sockaddr_in6 addr;
size_t addrLen;
} connection_t;
int create_socket(const char * portStr, int ai_family);
int create_socket(const char *portStr, int ai_family);
connection_t * connection_find(connection_t * connList, struct sockaddr_storage * addr, size_t addrLen);
connection_t * connection_new_incoming(connection_t * connList, int sock, struct sockaddr * addr, size_t addrLen);
connection_t * connection_create(connection_t * connList, int sock, char * host, char * port, int addressFamily);
connection_t *connection_find(connection_t *connList, struct sockaddr_storage *addr, size_t addrLen);
connection_t *connection_new_incoming(connection_t *connList, int sock, struct sockaddr *addr, size_t addrLen);
connection_t *connection_create(connection_t *connList, int sock, char *host, char *port, int addressFamily);
void connection_free(connection_t * connList);
void connection_free(connection_t *connList);
int connection_send(connection_t *connP, uint8_t * buffer, size_t length);
int connection_send(connection_t *connP, uint8_t *buffer, size_t length);
#endif

View File

@ -242,7 +242,8 @@ function(target_sources_shared target)
set_defines(${target})
if(NOT TARGET_PROPERTY_CONN_IMPL)
target_sources(${target} PRIVATE ${WAKAAMA_EXAMPLE_SHARED_DIRECTORY}/connection.c)
target_sources(${target} PRIVATE ${WAKAAMA_TOP_LEVEL_DIRECTORY}/transport/udp/connection.c)
target_include_directories(${target} PUBLIC ${WAKAAMA_TOP_LEVEL_DIRECTORY}/transport/udp/include)
elseif(TARGET_PROPERTY_CONN_IMPL MATCHES "tinydtls")
include(${WAKAAMA_EXAMPLE_SHARED_DIRECTORY}/tinydtls.cmake)
target_sources(${target} PRIVATE ${WAKAAMA_EXAMPLE_SHARED_DIRECTORY}/dtlsconnection.c)