1 /*
2 * Copyright (c) 2014 Chris Anderson
3 * Copyright (c) 2014 Brian Swetland
4 *
5 * Use of this source code is governed by a MIT-style
6 * license that can be found in the LICENSE file or at
7 * https://opensource.org/licenses/MIT
8 */
9 #pragma once
10
11 #include <endian.h>
12 #include <lk/list.h>
13 #include <stdint.h>
14 #include <iovec.h>
15 #include <sys/types.h>
16
17 #include <lib/pktbuf.h>
18
19 #define IPV4(a,b,c,d) (((a)&0xFF)|(((b)&0xFF)<<8)|(((c)&0xFF)<<16)|(((d)&0xFF)<<24))
20 #define IPV4_SPLIT(a) (a & 0xFF), ((a >> 8) & 0xFF), ((a >> 16) & 0xFF), ((a >> 24) & 0xFF)
21 #define IPV4_PACK(a) (a[3] << 24 | a[2] << 16 | a[1] << 8 | a[0])
22 #define IPV4_BCAST (0xFFFFFFFF)
23 #define IPV4_NONE (0)
24
25 typedef int (*tx_func_t)(pktbuf_t *p);
26 typedef void (*udp_callback_t)(void *data, size_t len,
27 uint32_t srcaddr, uint16_t srcport, void *arg);
28
29 /* initialize minip with static configuration */
30 void minip_init(tx_func_t tx_func, void *tx_arg,
31 uint32_t ip, uint32_t netmask, uint32_t gateway);
32
33 /* initialize minip with DHCP configuration */
34 void minip_init_dhcp(tx_func_t tx_func, void *tx_arg);
35
36 /* packet rx hook to hand to ethernet driver */
37 void minip_rx_driver_callback(pktbuf_t *p);
38
39 /* global configuration state */
40 void minip_get_macaddr(uint8_t *addr);
41 void minip_set_macaddr(const uint8_t *addr);
42
43 uint32_t minip_get_ipaddr(void);
44 void minip_set_ipaddr(const uint32_t addr);
45
46 void minip_set_hostname(const char *name);
47 const char *minip_get_hostname(void);
48
49 uint32_t minip_parse_ipaddr(const char *addr, size_t len);
50
51 /* udp */
52 typedef struct udp_socket udp_socket_t;
53
54 int udp_listen(uint16_t port, udp_callback_t cb, void *arg);
55 status_t udp_open(uint32_t host, uint16_t sport, uint16_t dport, udp_socket_t **handle);
56 status_t udp_send(void *buf, size_t len, udp_socket_t *handle);
57 status_t udp_send_iovec(const iovec_t *iov, uint iov_count, udp_socket_t *handle);
58 status_t udp_close(udp_socket_t *handle);
59
60 /* tcp */
61 typedef struct tcp_socket tcp_socket_t;
62
63 status_t tcp_open_listen(tcp_socket_t **handle, uint16_t port);
64 status_t tcp_accept_timeout(tcp_socket_t *listen_socket, tcp_socket_t **accept_socket, lk_time_t timeout);
65 status_t tcp_close(tcp_socket_t *socket);
66 ssize_t tcp_read(tcp_socket_t *socket, void *buf, size_t len);
67 ssize_t tcp_write(tcp_socket_t *socket, const void *buf, size_t len);
68
tcp_accept(tcp_socket_t * listen_socket,tcp_socket_t ** accept_socket)69 static inline status_t tcp_accept(tcp_socket_t *listen_socket, tcp_socket_t **accept_socket) {
70 return tcp_accept_timeout(listen_socket, accept_socket, INFINITE_TIME);
71 }
72
73 /* utilities */
74 void gen_random_mac_address(uint8_t *mac_addr);
75