/
lwipv6.go
231 lines (197 loc) · 7.55 KB
/
lwipv6.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
Go bindings for the LWIPv6 TCP/IP hybrid stack.
The LWIPv6 stack is unclear on the ramifications of using multiple stacks, and Go's
concurrency story makes it even more unclear. Until we figure out some good test
scenarios, this library assumes you will be using exactly 1 stack and it's the default.
For simplicity, some redundant runtime reconfigure options are omitted for the time being.
*/
package lwipv6
/*
#cgo LDFLAGS: -llwipv6
#include <stdlib.h>
#include <stddef.h>
#include <lwipv6.h>
*/
import "C"
import (
"net"
"encoding/binary"
"unsafe"
)
type LWIPStackFlag uint32
const (
LWIP_STACK_FLAG_FORWARDING LWIPStackFlag = 1
LWIP_STACK_FLAG_USERFILTER LWIPStackFlag = 0x2
LWIP_STACK_FLAG_UF_NAT LWIPStackFlag = 0x10000
)
type LWIPStackCapability uint32
const (
/* Allows binding to TCP/UDP sockets below 1024 */
LWIP_CAP_NET_BIND_SERVICE LWIPStackCapability = 1<<10
/* Allow broadcasting, listen to multicast */
LWIP_CAP_NET_BROADCAST LWIPStackCapability = 1<<11
/* Allow interface configuration */
LWIP_CAP_NET_ADMIN LWIPStackCapability = 1<<12
/* Allow use of RAW sockets */
/* Allow use of PACKET sockets */
LWIP_CAP_NET_RAW LWIPStackCapability = 1<<13
)
type SlirpFlags uint32
const (
SLIRP_LISTEN_UDP SlirpFlags = 0x1000
SLIRP_LISTEN_TCP SlirpFlags = 0x2000
SLIRP_LISTEN_UNIXSTREAM SlirpFlags = 0x3000
SLIRP_LISTEN_TYPEMASK SlirpFlags = 0x7000
SLIRP_LISTEN_ONCE SlirpFlags = 0x8000
)
var (
stack *C.struct_stack
)
func IsInitialized() bool {
return (stack != nil)
}
// Initialize the network stack. Multiple calls lead to a stack panic.
func Initialize(flags LWIPStackFlag) {
if stack != nil {
panic("LWIPv6 already initialized.")
}
// Initialize LWIPv6
C.lwip_init()
// Setup the default network stack
stack = C.lwip_add_stack(C.ulong(flags))
C.lwip_stack_set(stack)
}
// Finalizes and closes down the stack. This should only be done after all
// network operatons are finished.
func Finish() {
C.lwip_stack_set(nil)
C.lwip_del_stack(stack)
stack = nil
C.lwip_fini()
}
// Wraps an LWIP network interface. Returned by interface add methods.
type networkInterface struct {
netif *C.struct_netif
}
// Convert an net.IPNet to an LWIP ip_addr struct
func convert_IPNet_to_LWIP(ipNet net.IPNet) (*C.struct_ip_addr, *C.struct_ip_addr) {
lwipAddr := convert_IP_to_LWIP(ipNet.IP)
lwipNetmask := convert_IPMask_to_LWIP(ipNet.Mask)
return lwipAddr, lwipNetmask
}
// Convert a net.IPMask to an LWIP netmask.
func convert_IPMask_to_LWIP(netMask net.IPMask) (*C.struct_ip_addr) {
// Golang actually uses IP the same internally as LWIP. But, since net.IP is a
// possibly variable length buffer, we need to handle the case where we had to
// stretch it to 16 bytes for the netmask.
lwipNetmask := convert_IP_to_LWIP((net.IP)(netMask))
if len(netMask) == net.IPv4len {
lwipNetmask.addr[0] = C.uint32_t(^uint32(0))
lwipNetmask.addr[1] = C.uint32_t(^uint32(0))
lwipNetmask.addr[2] = C.uint32_t(^uint32(0))
}
return lwipNetmask
}
// Convert a net.IP address to an LWIP compatible one
func convert_IP_to_LWIP(netAddr net.IP) *C.struct_ip_addr {
longIP := []byte(netAddr.To16())
lwipAddr := new(C.struct_ip_addr)
for idx := 0; idx < 4; idx++ {
lwipAddr.addr[idx] = C.uint32_t(binary.LittleEndian.Uint32(longIP[4*idx:(4*idx)+4]))
}
return lwipAddr
}
// Converts an LWIP address struct to a a Go IP (in long format)
func convert_LWIP_to_IP(ip_addr *C.struct_ip_addr) net.IP {
longIP := make ([]byte, 16)
for idx := 0; idx < 4; idx++ {
binary.LittleEndian.PutUint32(longIP[4*idx:(4*idx)+4], uint32(ip_addr.addr[idx]))
}
return net.IP(longIP)
}
type LWIPInterfaceType int
const (
IF_VDE LWIPInterfaceType = iota
IF_TAP LWIPInterfaceType = iota
IF_TUN LWIPInterfaceType = iota
IF_SLIRP LWIPInterfaceType = iota
)
// Creates a virtual network interface
func CreateInterface(ifType LWIPInterfaceType, arg string, flags int ) *networkInterface {
var netif *C.struct_netif
lwipArg := C.CString(arg) // FIXME: possible memory leak
switch ifType {
case IF_VDE:
netif = C.lwip_add_vdeif(stack, unsafe.Pointer(lwipArg), C.int(flags))
case IF_TAP:
netif = C.lwip_add_tapif(stack, unsafe.Pointer(lwipArg), C.int(flags))
case IF_TUN:
netif = C.lwip_add_tunif(stack, unsafe.Pointer(lwipArg), C.int(flags))
case IF_SLIRP:
netif = C.lwip_add_slirpif(stack, unsafe.Pointer(lwipArg), C.int(flags))
default:
return nil
}
return &networkInterface{
netif : netif,
}
}
// Add an address to an interface
func (this *networkInterface) AddAddress(addr net.IPNet) int {
lwipAddr, lwipNetmask := convert_IPNet_to_LWIP(addr)
return int(C.lwip_add_addr(this.netif, lwipAddr, lwipNetmask))
}
// Remove an address from an interface
func (this *networkInterface) DelAddress(addr net.IPNet) int {
lwipAddr, lwipNetmask := convert_IPNet_to_LWIP(addr)
return int(C.lwip_del_addr(this.netif, lwipAddr, lwipNetmask))
}
func (this *networkInterface) IfUp(flags uint32) int {
return int(C.lwip_ifup_flags(this.netif, C.int(flags)))
}
func (this *networkInterface) IfDown() int {
return int(C.lwip_ifdown(this.netif))
}
/*
The decaying list of functions to implement
int lwip_add_route(struct stack *stack, struct ip_addr *addr, struct ip_addr *netmask, struct ip_addr *nexthop, struct netif *netif, int flags);
int lwip_del_route(struct stack *stack, struct ip_addr *addr, struct ip_addr *netmask, struct ip_addr *nexthop, struct netif *netif, int flags);
int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen);
int lwip_shutdown(int s, int how);
int lwip_getpeername (int s, struct sockaddr *name, socklen_t *namelen);
int lwip_getsockname (int s, struct sockaddr *name, socklen_t *namelen);
int lwip_getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen);
int lwip_setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen);
int lwip_close(int s);
int lwip_connect(int s, const struct sockaddr *name, socklen_t namelen);
int lwip_listen(int s, int backlog);
ssize_t lwip_recv(int s, void *mem, int len, unsigned int flags);
ssize_t lwip_read(int s, void *mem, int len);
ssize_t lwip_recvfrom(int s, void *mem, int len, unsigned int flags,
struct sockaddr *from, socklen_t *fromlen);
ssize_t lwip_send(int s, const void *dataptr, int size, unsigned int flags);
ssize_t lwip_sendto(int s, const void *dataptr, int size, unsigned int flags,
const struct sockaddr *to, socklen_t tolen);
ssize_t lwip_recvmsg(int fd, struct msghdr *msg, int flags);
ssize_t lwip_sendmsg(int fd, const struct msghdr *msg, int flags);
int lwip_msocket(struct stack *stack, int domain, int type, int protocol);
int lwip_socket(int domain, int type, int protocol);
ssize_t lwip_write(int s, void *dataptr, int size);
int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
struct timeval *timeout);
int lwip_pselect(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
const struct timespec *timeout, const sigset_t *sigmask);
int lwip_poll(struct pollfd *fds, nfds_t nfds, int timeout);
int lwip_ppoll(struct pollfd *fds, nfds_t nfds,
const struct timespec *timeout, const sigset_t *sigmask);
int lwip_ioctl(int s, long cmd, void *argp);
int lwip_fcntl64(int s, int cmd, long arg);
int lwip_fcntl(int s, int cmd, long arg);
struct iovec;
ssize_t lwip_writev(int s, struct iovec *vector, int count);
ssize_t lwip_readv(int s, struct iovec *vector, int count);
void lwip_radv_load_config(struct stack *stack,FILE *filein);
int lwip_radv_load_configfile(struct stack *stack,void *arg);
int lwip_event_subscribe(lwipvoidfun cb, void *arg, int fd, int how);
*/