1 /* 2 Copyright (c) 2021 Fraunhofer AISEC. See the COPYRIGHT 3 file at the top-level directory of this distribution. 4 5 Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or 6 http://www.apache.org/licenses/LICENSE-2.0> or the MIT license 7 <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your 8 option. This file may not be copied, modified, or distributed 9 except according to those terms. 10 */ 11 #ifndef SOCK_H 12 #define SOCK_H 13 14 #include <stdint.h> 15 #include <stddef.h> 16 17 #ifdef LINUX_SOCKETS 18 #include <arpa/inet.h> 19 #include <sys/socket.h> 20 #else 21 #include <zephyr/net/net_pkt.h> 22 #include <zephyr/net/net_if.h> 23 #include <zephyr/net/net_core.h> 24 #include <zephyr/net/net_context.h> 25 #include <zephyr/net/udp.h> 26 #include <zephyr/net/socket.h> 27 #endif 28 29 #ifndef PORT 30 #define PORT 5683 31 #endif 32 33 #define MAXLINE 1024 34 35 enum sock_type { 36 SOCK_CLIENT, 37 SOCK_SERVER, 38 }; 39 40 enum ip_addr_type { 41 IPv4, 42 IPv6, 43 }; 44 45 /** 46 * @brief Initializes a IPv4 client or server socket 47 * @param sock_t CLIENT or SERVER 48 * @param ipv6_addr_str ip address as string 49 * @param servaddr address struct 50 * @param servaddr_len length of servaddr 51 * @param sockfd socket file descriptor 52 * @retval error code 53 */ 54 int ipv4_sock_init(enum sock_type sock_t, const char *ipv4_addr_str, 55 struct sockaddr_in *servaddr, size_t servaddr_len, 56 int *sockfd); 57 58 /** 59 * @brief Initializes a IPv6 client or server socket 60 * @param sock_t CLIENT or SERVER 61 * @param ipv6_addr_str ip address as string 62 * @param servaddr address struct 63 * @param servaddr_len length of servaddr 64 * @param sockfd socket file descriptor 65 * @retval error code 66 */ 67 int ipv6_sock_init(enum sock_type sock_t, const char *ipv6_addr_str, 68 struct sockaddr_in6 *servaddr, size_t servaddr_len, 69 int *sockfd); 70 71 /** 72 * @brief Initializes an UDP client or server socket. 73 * @param sock_t CLIENT or SERVER 74 * @param addr_str ip address as string 75 * @param ip_t IPv4 or IPv6 76 * @param servaddr struct of type sockaddr_in or sockaddr_in6 77 * @param servaddr_len length of servaddr 78 * @retval error code 79 */ 80 int sock_init(enum sock_type sock_t, const char *addr_str, 81 enum ip_addr_type ip_t, void *servaddr, size_t servaddr_len, 82 int *sockfd); 83 #endif 84