uhcp.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2016 Kaspar Schleiser <kaspar@schleiser.de>
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
29 #include <stdint.h>
30 #include <stddef.h>
31 #include <arpa/inet.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
38 #define UHCP_MAGIC (0x55484350) /* "UHCP" in hex */
39 
41 #define UHCP_VER (0)
42 
44 #define UHCP_PORT (12345U)
45 
47 #define UHCP_PORT_STR "12345"
48 
50 typedef enum {
52  UHCP_PUSH
54 
58 typedef struct __attribute__((packed)) {
59  uint32_t uhcp_magic;
60  uint8_t ver_type;
62 } uhcp_hdr_t;
63 
69 typedef struct __attribute__((packed)) {
71  uint8_t prefix_len;
72 } uhcp_req_t;
73 
79 typedef struct __attribute__((packed)) {
81  uint8_t prefix_len;
83  uint8_t prefix[];
84 } uhcp_push_t;
85 
87 typedef unsigned uhcp_iface_t;
88 
101 void uhcp_handle_udp(uint8_t *buf, size_t len, uint8_t *src, uint16_t port, uhcp_iface_t iface);
102 
116 void uhcp_handle_req(uhcp_req_t *req, uint8_t *src, uint16_t port, uhcp_iface_t iface);
117 
131 void uhcp_handle_push(uhcp_push_t *req, uint8_t *src, uint16_t port, uhcp_iface_t iface);
132 
151 void uhcp_handle_prefix(uint8_t *prefix, uint8_t prefix_len, uint16_t lifetime, uint8_t *src, uhcp_iface_t iface);
152 
161 static inline void uhcp_hdr_set(uhcp_hdr_t *hdr, uhcp_type_t type)
162 {
163  hdr->uhcp_magic = htonl(UHCP_MAGIC);
164  hdr->ver_type = (UHCP_VER << 4) | (type & 0xF);
165 }
166 
178 int udp_sendto(uint8_t *buf, size_t len, uint8_t *dst, uint16_t dst_port, uhcp_iface_t dst_iface);
179 
180 #ifdef __cplusplus
181 }
182 #endif
183 
static uint32_t htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition: byteorder.h:522
void uhcp_handle_udp(uint8_t *buf, size_t len, uint8_t *src, uint16_t port, uhcp_iface_t iface)
handle incoming UDP packet
#define UHCP_VER
UHCP version of this header.
Definition: uhcp.h:41
void uhcp_handle_prefix(uint8_t *prefix, uint8_t prefix_len, uint16_t lifetime, uint8_t *src, uhcp_iface_t iface)
handle incoming prefix (as parsed from push packet)
uhcp_type_t
Enum containing possible UHCP packet types.
Definition: uhcp.h:50
unsigned uhcp_iface_t
typedef for interface handle
Definition: uhcp.h:87
void uhcp_handle_push(uhcp_push_t *req, uint8_t *src, uint16_t port, uhcp_iface_t iface)
handle incoming UHCP push packet
static void uhcp_hdr_set(uhcp_hdr_t *hdr, uhcp_type_t type)
function to set constant values in UHCP header
Definition: uhcp.h:161
int udp_sendto(uint8_t *buf, size_t len, uint8_t *dst, uint16_t dst_port, uhcp_iface_t dst_iface)
UDP send function used by UHCP client / server.
void uhcp_handle_req(uhcp_req_t *req, uint8_t *src, uint16_t port, uhcp_iface_t iface)
handle incoming UHCP request packet
#define UHCP_MAGIC
UHCP magic number.
Definition: uhcp.h:38
@ UHCP_REQ
packet is a request packet
Definition: uhcp.h:51
@ UHCP_PUSH
packet is a push / answer packet
Definition: uhcp.h:52
Definitions for internet operations.
UHCP packet header struct.
Definition: uhcp.h:58
uint32_t uhcp_magic
always contains UHCP in hex
Definition: uhcp.h:59
uint8_t ver_type
four bits version number, four bits packet type (see uchp_type_t)
Definition: uhcp.h:60
struct for push packets
Definition: uhcp.h:79
uhcp_hdr_t hdr
member holding parent type
Definition: uhcp.h:80
uint8_t prefix_len
contains the prefix length of assigned prefix
Definition: uhcp.h:81
struct for request packets
Definition: uhcp.h:69
uint8_t prefix_len
contains the requested prefix length
Definition: uhcp.h:71
uhcp_hdr_t hdr
member holding parent type
Definition: uhcp.h:70