|
| 1 | +# wolfIP API Documentation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +wolfIP is a minimal TCP/IP stack designed for resource-constrained embedded systems. It features zero dynamic memory allocation, using pre-allocated buffers and a fixed number of concurrent sockets. |
| 6 | + |
| 7 | +## Key Features |
| 8 | + |
| 9 | +- No dynamic memory allocation |
| 10 | +- Fixed number of concurrent sockets |
| 11 | +- Pre-allocated buffers for packet processing |
| 12 | +- BSD-like non-blocking socket API with callbacks |
| 13 | +- Protocol Support: |
| 14 | + - ARP (RFC 826) |
| 15 | + - IPv4 (RFC 791) |
| 16 | + - ICMP (RFC 792) - ping replies only |
| 17 | + - DHCP (RFC 2131) - client only |
| 18 | + - DNS (RFC 1035) - client only |
| 19 | + - UDP (RFC 768) - unicast only |
| 20 | + - TCP (RFC 793) with options (Timestamps, MSS) |
| 21 | + |
| 22 | +## Core Data Structures |
| 23 | + |
| 24 | +### Device Driver Interface |
| 25 | +```c |
| 26 | +struct ll { |
| 27 | + uint8_t mac[6]; // Device MAC address |
| 28 | + char ifname[16]; // Interface name |
| 29 | + int (*poll)(struct ll *ll, void *buf, uint32_t len); // Receive function |
| 30 | + int (*send)(struct ll *ll, void *buf, uint32_t len); // Transmit function |
| 31 | +}; |
| 32 | +``` |
| 33 | + |
| 34 | +### IP Configuration |
| 35 | +```c |
| 36 | +struct ipconf { |
| 37 | + struct ll *ll; // Link layer device |
| 38 | + ip4 ip; // IPv4 address |
| 39 | + ip4 mask; // Subnet mask |
| 40 | + ip4 gw; // Default gateway |
| 41 | +}; |
| 42 | +``` |
| 43 | + |
| 44 | +### Socket Address Structures |
| 45 | +```c |
| 46 | +struct wolfIP_sockaddr_in { |
| 47 | + uint16_t sin_family; // Address family (AF_INET) |
| 48 | + uint16_t sin_port; // Port number |
| 49 | + struct sin_addr { |
| 50 | + uint32_t s_addr; // IPv4 address |
| 51 | + } sin_addr; |
| 52 | +}; |
| 53 | + |
| 54 | +struct wolfIP_sockaddr { |
| 55 | + uint16_t sa_family; // Address family |
| 56 | +}; |
| 57 | +``` |
| 58 | + |
| 59 | +## Socket Interface Functions |
| 60 | + |
| 61 | +### Socket Creation and Control |
| 62 | +```c |
| 63 | +int wolfIP_sock_socket(struct wolfIP *s, int domain, int type, int protocol); |
| 64 | +``` |
| 65 | +Creates a new socket. |
| 66 | +- Parameters: |
| 67 | + - s: wolfIP instance |
| 68 | + - domain: Address family (AF_INET) |
| 69 | + - type: Socket type (SOCK_STREAM/SOCK_DGRAM) |
| 70 | + - protocol: Protocol (usually 0) |
| 71 | +- Returns: Socket descriptor or negative error code |
| 72 | +
|
| 73 | +```c |
| 74 | +int wolfIP_sock_bind(struct wolfIP *s, int sockfd, const struct wolfIP_sockaddr *addr, socklen_t addrlen); |
| 75 | +``` |
| 76 | +Binds a socket to a local address. |
| 77 | +- Parameters: |
| 78 | + - s: wolfIP instance |
| 79 | + - sockfd: Socket descriptor |
| 80 | + - addr: Local address to bind to |
| 81 | + - addrlen: Length of address structure |
| 82 | +- Returns: 0 on success, negative error code on failure |
| 83 | + |
| 84 | +```c |
| 85 | +int wolfIP_sock_listen(struct wolfIP *s, int sockfd, int backlog); |
| 86 | +``` |
| 87 | +Marks a socket as passive (listening for connections). |
| 88 | +- Parameters: |
| 89 | + - s: wolfIP instance |
| 90 | + - sockfd: Socket descriptor |
| 91 | + - backlog: Maximum length of pending connections queue |
| 92 | +- Returns: 0 on success, negative error code on failure |
| 93 | +
|
| 94 | +### Connection Management |
| 95 | +```c |
| 96 | +int wolfIP_sock_connect(struct wolfIP *s, int sockfd, const struct wolfIP_sockaddr *addr, socklen_t addrlen); |
| 97 | +``` |
| 98 | +Initiates a connection on a socket. |
| 99 | +- Parameters: |
| 100 | + - s: wolfIP instance |
| 101 | + - sockfd: Socket descriptor |
| 102 | + - addr: Address to connect to |
| 103 | + - addrlen: Length of address structure |
| 104 | +- Returns: 0 on success, negative error code on failure |
| 105 | + |
| 106 | +```c |
| 107 | +int wolfIP_sock_accept(struct wolfIP *s, int sockfd, struct wolfIP_sockaddr *addr, socklen_t *addrlen); |
| 108 | +``` |
| 109 | +Accepts a connection on a listening socket. |
| 110 | +- Parameters: |
| 111 | + - s: wolfIP instance |
| 112 | + - sockfd: Listening socket descriptor |
| 113 | + - addr: Address of connecting peer |
| 114 | + - addrlen: Length of address structure |
| 115 | +- Returns: New socket descriptor or negative error code |
| 116 | +
|
| 117 | +### Data Transfer |
| 118 | +```c |
| 119 | +int wolfIP_sock_send(struct wolfIP *s, int sockfd, const void *buf, size_t len, int flags); |
| 120 | +int wolfIP_sock_recv(struct wolfIP *s, int sockfd, void *buf, size_t len, int flags); |
| 121 | +``` |
| 122 | +Send/receive data on a connected socket. |
| 123 | +- Parameters: |
| 124 | + - s: wolfIP instance |
| 125 | + - sockfd: Socket descriptor |
| 126 | + - buf: Data buffer |
| 127 | + - len: Buffer length |
| 128 | + - flags: Operation flags |
| 129 | +- Returns: Number of bytes transferred or negative error code |
| 130 | + |
| 131 | +```c |
| 132 | +int wolfIP_sock_sendto(struct wolfIP *s, int sockfd, const void *buf, size_t len, int flags, const struct wolfIP_sockaddr *dest_addr, socklen_t addrlen); |
| 133 | +int wolfIP_sock_recvfrom(struct wolfIP *s, int sockfd, void *buf, size_t len, int flags, struct wolfIP_sockaddr *src_addr, socklen_t *addrlen); |
| 134 | +``` |
| 135 | +Send/receive data on a datagram socket. |
| 136 | +- Parameters similar to send/recv with additional address parameters |
| 137 | +
|
| 138 | +## Stack Interface Functions |
| 139 | +
|
| 140 | +```c |
| 141 | +void wolfIP_init(struct wolfIP *s); |
| 142 | +``` |
| 143 | +Initializes the TCP/IP stack. |
| 144 | +- Parameters: |
| 145 | + - s: wolfIP instance to initialize |
| 146 | + |
| 147 | +```c |
| 148 | +void wolfIP_init_static(struct wolfIP **s); |
| 149 | +``` |
| 150 | +Initializes a static wolfIP instance. |
| 151 | +- Parameters: |
| 152 | + - s: Pointer to wolfIP instance pointer |
| 153 | +
|
| 154 | +```c |
| 155 | +int wolfIP_poll(struct wolfIP *s, uint64_t now); |
| 156 | +``` |
| 157 | +Processes pending network events. |
| 158 | +- Parameters: |
| 159 | + - s: wolfIP instance |
| 160 | + - now: Current timestamp |
| 161 | +- Returns: Number of events processed |
| 162 | + |
| 163 | +```c |
| 164 | +void wolfIP_ipconfig_set(struct wolfIP *s, ip4 ip, ip4 mask, ip4 gw); |
| 165 | +void wolfIP_ipconfig_get(struct wolfIP *s, ip4 *ip, ip4 *mask, ip4 *gw); |
| 166 | +``` |
| 167 | +Set/get IP configuration. |
| 168 | +- Parameters: |
| 169 | + - s: wolfIP instance |
| 170 | + - ip: IPv4 address |
| 171 | + - mask: Subnet mask |
| 172 | + - gw: Default gateway |
| 173 | +
|
| 174 | +## DHCP Client Functions |
| 175 | +
|
| 176 | +```c |
| 177 | +int dhcp_client_init(struct wolfIP *s); |
| 178 | +``` |
| 179 | +Initializes DHCP client. |
| 180 | +- Parameters: |
| 181 | + - s: wolfIP instance |
| 182 | +- Returns: 0 on success, negative error code on failure |
| 183 | + |
| 184 | +```c |
| 185 | +int dhcp_bound(struct wolfIP *s); |
| 186 | +``` |
| 187 | +Checks if DHCP client is bound. |
| 188 | +- Parameters: |
| 189 | + - s: wolfIP instance |
| 190 | +- Returns: 1 if bound, 0 otherwise |
| 191 | +
|
| 192 | +## DNS Client Functions |
| 193 | +
|
| 194 | +```c |
| 195 | +int nslookup(struct wolfIP *s, const char *name, uint16_t *id, void (*lookup_cb)(uint32_t ip)); |
| 196 | +``` |
| 197 | +Performs DNS lookup. |
| 198 | +- Parameters: |
| 199 | + - s: wolfIP instance |
| 200 | + - name: Hostname to resolve |
| 201 | + - id: Transaction ID |
| 202 | + - lookup_cb: Callback function for result |
| 203 | +- Returns: 0 on success, negative error code on failure |
| 204 | + |
| 205 | +## Utility Functions |
| 206 | + |
| 207 | +```c |
| 208 | +uint32_t atou(const char *s); |
| 209 | +``` |
| 210 | +Converts ASCII string to unsigned integer. |
| 211 | +
|
| 212 | +```c |
| 213 | +ip4 atoip4(const char *ip); |
| 214 | +``` |
| 215 | +Converts dotted decimal IP address string to 32-bit integer. |
| 216 | + |
| 217 | +```c |
| 218 | +void iptoa(ip4 ip, char *buf); |
| 219 | +``` |
| 220 | +Converts 32-bit IP address to dotted decimal string. |
| 221 | +
|
| 222 | +## Event Callback Registration |
| 223 | +
|
| 224 | +```c |
| 225 | +void wolfIP_register_callback(struct wolfIP *s, int sock_fd, void (*cb)(int sock_fd, uint16_t events, void *arg), void *arg); |
| 226 | +``` |
| 227 | +Registers event callback for a socket. |
| 228 | +- Parameters: |
| 229 | + - s: wolfIP instance |
| 230 | + - sock_fd: Socket descriptor |
| 231 | + - cb: Callback function |
| 232 | + - arg: User data for callback |
| 233 | + |
| 234 | +Event flags: |
| 235 | +- CB_EVENT_READABLE (0x01): Data available or connection accepted |
| 236 | +- CB_EVENT_TIMEOUT (0x02): Operation timed out |
| 237 | +- CB_EVENT_WRITABLE (0x04): Connected or space available to send |
| 238 | +- CB_EVENT_CLOSED (0x10): Connection closed by peer |
0 commit comments