├── .gitignore ├── core ├── src │ ├── custom │ │ └── arch │ │ │ ├── bpstruct.h │ │ │ ├── epstruct.h │ │ │ ├── perf.h │ │ │ ├── cc.h │ │ │ └── sys_arch.h │ ├── include │ │ ├── lwip │ │ │ ├── apps │ │ │ │ ├── FILES │ │ │ │ ├── snmp_snmpv2_usm.h │ │ │ │ ├── snmp_snmpv2_framework.h │ │ │ │ ├── netbiosns.h │ │ │ │ ├── smtp_opts.h │ │ │ │ ├── netbiosns_opts.h │ │ │ │ ├── altcp_tls_mbedtls_opts.h │ │ │ │ ├── mdns_priv.h │ │ │ │ ├── mdns_opts.h │ │ │ │ ├── sntp.h │ │ │ │ ├── altcp_proxyconnect.h │ │ │ │ ├── snmp_mib2.h │ │ │ │ ├── mqtt_opts.h │ │ │ │ ├── tftp_opts.h │ │ │ │ └── tftp_server.h │ │ │ ├── prot │ │ │ │ ├── ip.h │ │ │ │ ├── udp.h │ │ │ │ ├── mld6.h │ │ │ │ ├── iana.h │ │ │ │ ├── igmp.h │ │ │ │ ├── ieee.h │ │ │ │ ├── autoip.h │ │ │ │ └── icmp.h │ │ │ ├── ethip6.h │ │ │ ├── if_api.h │ │ │ ├── priv │ │ │ │ ├── raw_priv.h │ │ │ │ └── mem_priv.h │ │ │ ├── mem.h │ │ │ ├── icmp6.h │ │ │ ├── altcp_tcp.h │ │ │ ├── tcpbase.h │ │ │ ├── nd6.h │ │ │ └── ip4_frag.h │ │ ├── netif │ │ │ ├── etharp.h │ │ │ ├── ppp │ │ │ │ ├── chap-md5.h │ │ │ │ ├── chap_ms.h │ │ │ │ ├── ecp.h │ │ │ │ ├── polarssl │ │ │ │ │ ├── arc4.h │ │ │ │ │ ├── des.h │ │ │ │ │ ├── md5.h │ │ │ │ │ ├── md4.h │ │ │ │ │ └── sha1.h │ │ │ │ ├── pppdebug.h │ │ │ │ └── eui64.h │ │ │ ├── zepif.h │ │ │ ├── ethernet.h │ │ │ ├── lowpan6.h │ │ │ ├── lowpan6_ble.h │ │ │ ├── lowpan6_common.h │ │ │ └── slipif.h │ │ ├── posix │ │ │ ├── errno.h │ │ │ ├── netdb.h │ │ │ └── sys │ │ │ │ └── socket.h │ │ └── compat │ │ │ ├── posix │ │ │ ├── netdb.h │ │ │ ├── arpa │ │ │ │ └── inet.h │ │ │ ├── sys │ │ │ │ └── socket.h │ │ │ └── net │ │ │ │ └── if.h │ │ │ └── stdc │ │ │ └── errno.h │ └── core │ │ ├── ipv6 │ │ └── inet6.c │ │ └── altcp_alloc.c ├── udp_conn_map.go ├── lwip_other.go ├── lwip_windows.go ├── udp_callback.go ├── buffer_pool.go ├── input.go ├── output_export.go ├── output.go ├── handler.go ├── connection.go ├── udp_callback_export.go ├── tcp_conn_map.go ├── tcp_callback.go ├── errors.go ├── util.go ├── lwip.go ├── udp_conn.go └── tcp_callback_export.go ├── tun ├── README.md ├── stop.go ├── tun_darwin.go └── tun_linux.go ├── route ├── route_linux.go ├── route_darwin.go ├── route_windows.go └── util.go ├── .travis.yml ├── proxy ├── v2ray │ ├── features_other.go │ └── features.go ├── echo │ ├── udp.go │ └── tcp.go ├── direct │ ├── tcp.go │ └── udp.go ├── socks │ └── tcp.go └── dns_cache.go ├── LICENSE ├── scripts └── release.sh └── filter └── filter.go /.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | /tools 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /core/src/custom/arch/bpstruct.h: -------------------------------------------------------------------------------- 1 | #pragma pack(push,1) 2 | -------------------------------------------------------------------------------- /core/src/custom/arch/epstruct.h: -------------------------------------------------------------------------------- 1 | #pragma pack(pop) 2 | -------------------------------------------------------------------------------- /tun/README.md: -------------------------------------------------------------------------------- 1 | Files in this directory are copied from https://github.com/yinghuocho/gotun2socks, with slightly modifies. 2 | -------------------------------------------------------------------------------- /core/src/custom/arch/perf.h: -------------------------------------------------------------------------------- 1 | #ifndef perf_h 2 | #define perf_h 3 | 4 | #define PERF_START 5 | #define PERF_STOP(x) 6 | 7 | #endif /* perf_h */ 8 | -------------------------------------------------------------------------------- /core/src/custom/arch/cc.h: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | // both win32 and win64 are defined here 3 | #include "cc_windows.h" 4 | #else 5 | #include "cc_others.h" 6 | #endif 7 | -------------------------------------------------------------------------------- /core/src/include/lwip/apps/FILES: -------------------------------------------------------------------------------- 1 | This directory contains application headers. 2 | Every application shall provide one api file APP.h and optionally one options file APP_opts.h 3 | -------------------------------------------------------------------------------- /core/src/include/netif/etharp.h: -------------------------------------------------------------------------------- 1 | /* ARP has been moved to core/ipv4, provide this #include for compatibility only */ 2 | #include "lwip/etharp.h" 3 | #include "netif/ethernet.h" 4 | -------------------------------------------------------------------------------- /core/udp_conn_map.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | import ( 4 | "sync" 5 | ) 6 | 7 | var udpConns sync.Map 8 | 9 | type udpConnId struct { 10 | src string 11 | dst string 12 | } 13 | -------------------------------------------------------------------------------- /route/route_linux.go: -------------------------------------------------------------------------------- 1 | package route 2 | 3 | import ( 4 | "errors" 5 | ) 6 | 7 | func AddRoute(dest, netmask, gateway string) error { 8 | return errors.New("not implemented") 9 | } 10 | -------------------------------------------------------------------------------- /core/src/custom/arch/sys_arch.h: -------------------------------------------------------------------------------- 1 | #ifndef __ARCH_SYS_ARCH_H__ 2 | #define __ARCH_SYS_ARCH_H__ 3 | 4 | #define SYS_MBOX_NULL NULL 5 | #define SYS_SEM_NULL NULL 6 | 7 | #endif /* __ARCH_SYS_ARCH_H__ */ 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | 3 | services: 4 | - docker 5 | 6 | language: go 7 | go: 8 | - "1.11" 9 | 10 | before_install: 11 | - go get github.com/karalabe/xgo 12 | 13 | install: go get -d ./... 14 | 15 | script: make xbuild 16 | -------------------------------------------------------------------------------- /core/lwip_other.go: -------------------------------------------------------------------------------- 1 | // +build linux darwin 2 | 3 | package core 4 | 5 | /* 6 | #cgo CFLAGS: -I./src/include 7 | #include "lwip/init.h" 8 | */ 9 | import "C" 10 | 11 | func lwipInit() { 12 | C.lwip_init() // Initialze modules. 13 | } 14 | -------------------------------------------------------------------------------- /proxy/v2ray/features_other.go: -------------------------------------------------------------------------------- 1 | // +build !ios,!android 2 | 3 | package v2ray 4 | 5 | import ( 6 | _ "v2ray.com/core/app/commander" 7 | _ "v2ray.com/core/app/log/command" 8 | _ "v2ray.com/core/app/proxyman/command" 9 | _ "v2ray.com/core/app/stats/command" 10 | 11 | _ "v2ray.com/core/transport/internet/domainsocket" 12 | ) 13 | -------------------------------------------------------------------------------- /core/lwip_windows.go: -------------------------------------------------------------------------------- 1 | // +build windows 2 | 3 | package core 4 | 5 | /* 6 | #cgo CFLAGS: -I./src/include 7 | #include "lwip/sys.h" 8 | #include "lwip/init.h" 9 | */ 10 | import "C" 11 | 12 | func lwipInit() { 13 | C.sys_init() // Initialze sys_arch layer, must be called before anything else. 14 | C.lwip_init() // Initialze modules. 15 | } 16 | -------------------------------------------------------------------------------- /route/route_darwin.go: -------------------------------------------------------------------------------- 1 | package route 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "os/exec" 7 | ) 8 | 9 | func AddRoute(dest, netmask, gateway string) error { 10 | out, err := exec.Command("route", "add", dest, gateway, "-netmask", netmask).Output() 11 | if err != nil { 12 | if len(out) != 0 { 13 | return errors.New(fmt.Sprintf("%v, output: %s", err, out)) 14 | } 15 | return err 16 | } 17 | return nil 18 | } 19 | -------------------------------------------------------------------------------- /route/route_windows.go: -------------------------------------------------------------------------------- 1 | package route 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "os/exec" 7 | ) 8 | 9 | func AddRoute(dest, netmask, gateway string) error { 10 | out, err := exec.Command("route", "add", dest, "mask", netmask, gateway, "metric", "5").Output() 11 | if err != nil { 12 | if len(out) != 0 { 13 | return errors.New(fmt.Sprintf("%v, output: %s", err, out)) 14 | } 15 | return err 16 | } 17 | return nil 18 | } 19 | -------------------------------------------------------------------------------- /core/udp_callback.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./src/include 5 | #include "lwip/udp.h" 6 | 7 | extern void UDPRecvFn(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port, const ip_addr_t *dest_addr, u16_t dest_port); 8 | 9 | void 10 | set_udp_recv_callback(struct udp_pcb *pcb, void *recv_arg) { 11 | udp_recv(pcb, UDPRecvFn, recv_arg); 12 | } 13 | */ 14 | import "C" 15 | import ( 16 | "unsafe" 17 | ) 18 | 19 | func SetUDPRecvCallback(pcb *C.struct_udp_pcb, recvArg unsafe.Pointer) { 20 | C.set_udp_recv_callback(pcb, recvArg) 21 | } 22 | -------------------------------------------------------------------------------- /core/src/include/lwip/apps/snmp_snmpv2_usm.h: -------------------------------------------------------------------------------- 1 | /* 2 | Generated by LwipMibCompiler 3 | */ 4 | 5 | #ifndef LWIP_HDR_APPS_SNMP_USER_BASED_SM_MIB_H 6 | #define LWIP_HDR_APPS_SNMP_USER_BASED_SM_MIB_H 7 | 8 | #include "lwip/apps/snmp_opts.h" 9 | #if LWIP_SNMP 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif /* __cplusplus */ 14 | 15 | #include "lwip/apps/snmp_core.h" 16 | 17 | extern const struct snmp_mib snmpusmmib; 18 | 19 | #ifdef __cplusplus 20 | } 21 | #endif /* __cplusplus */ 22 | 23 | #endif /* LWIP_SNMP */ 24 | #endif /* LWIP_HDR_APPS_SNMP_USER_BASED_SM_MIB_H */ 25 | -------------------------------------------------------------------------------- /core/buffer_pool.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | import ( 4 | "sync" 5 | ) 6 | 7 | var pool *sync.Pool 8 | 9 | const BufSize = 2 * 1024 10 | 11 | func SetBufferPool(p *sync.Pool) { 12 | pool = p 13 | } 14 | 15 | func NewBytes(size int) []byte { 16 | if size <= BufSize { 17 | return pool.Get().([]byte) 18 | } else { 19 | return make([]byte, size) 20 | } 21 | } 22 | 23 | func FreeBytes(b []byte) { 24 | if len(b) >= BufSize { 25 | pool.Put(b) 26 | } 27 | } 28 | 29 | func init() { 30 | SetBufferPool(&sync.Pool{ 31 | New: func() interface{} { 32 | return make([]byte, BufSize) 33 | }, 34 | }) 35 | } 36 | -------------------------------------------------------------------------------- /core/input.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./src/include 5 | #include "lwip/pbuf.h" 6 | #include "lwip/timeouts.h" 7 | #include "lwip/tcp.h" 8 | 9 | err_t 10 | input(struct pbuf *p) 11 | { 12 | return (*netif_list).input(p, netif_list); 13 | } 14 | */ 15 | import "C" 16 | import ( 17 | "unsafe" 18 | ) 19 | 20 | func Input(pkt []byte) (int, error) { 21 | buf := C.pbuf_alloc_reference(unsafe.Pointer(&pkt[0]), C.u16_t(len(pkt)), C.PBUF_ROM) 22 | lwipMutex.Lock() 23 | err := C.input(buf) 24 | if err != C.ERR_OK { 25 | C.pbuf_free(buf) 26 | // TODO 27 | panic("why failed!?") 28 | } 29 | C.sys_check_timeouts() 30 | lwipMutex.Unlock() 31 | return len(pkt), nil 32 | } 33 | -------------------------------------------------------------------------------- /core/output_export.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./src/include 5 | #include "lwip/tcp.h" 6 | */ 7 | import "C" 8 | import ( 9 | "unsafe" 10 | ) 11 | 12 | //export Output 13 | func Output(p *C.struct_pbuf) C.err_t { 14 | // In most case, all data are in the same pbuf struct, data copying can be avoid by 15 | // backing Go slice with C array. Buf if there are multiple pbuf structs holding the 16 | // data, we must copy data for sending them in one pass. 17 | if p.tot_len == p.len { 18 | buf := (*[1 << 30]byte)(unsafe.Pointer(p.payload))[:int(p.len):int(p.len)] 19 | OutputFn(buf) 20 | } else { 21 | buf := NewBytes(int(p.tot_len)) 22 | C.pbuf_copy_partial(p, unsafe.Pointer(&buf[0]), p.tot_len, 0) // data copy here! 23 | OutputFn(buf[:int(p.tot_len)]) 24 | FreeBytes(buf) 25 | } 26 | return C.ERR_OK 27 | } 28 | -------------------------------------------------------------------------------- /core/output.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./src/include 5 | #include "lwip/tcp.h" 6 | 7 | extern err_t Output(struct pbuf *p); 8 | 9 | err_t 10 | output(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr) 11 | { 12 | return Output(p); 13 | } 14 | 15 | err_t 16 | output_ip6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr) 17 | { 18 | return Output(p); 19 | } 20 | 21 | void 22 | set_output() 23 | { 24 | if (netif_list != NULL) { 25 | (*netif_list).output = output; 26 | (*netif_list).output_ip6 = output_ip6; 27 | } 28 | } 29 | */ 30 | import "C" 31 | import ( 32 | "errors" 33 | ) 34 | 35 | var OutputFn func([]byte) (int, error) 36 | 37 | func RegisterOutputFn(fn func([]byte) (int, error)) { 38 | OutputFn = fn 39 | C.set_output() 40 | } 41 | 42 | func init() { 43 | OutputFn = func(data []byte) (int, error) { 44 | return 0, errors.New("output function not set") 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /core/src/include/lwip/apps/snmp_snmpv2_framework.h: -------------------------------------------------------------------------------- 1 | /* 2 | Generated by LwipMibCompiler 3 | */ 4 | 5 | #ifndef LWIP_HDR_APPS_SNMP_FRAMEWORK_MIB_H 6 | #define LWIP_HDR_APPS_SNMP_FRAMEWORK_MIB_H 7 | 8 | #include "lwip/apps/snmp_opts.h" 9 | #if LWIP_SNMP 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif /* __cplusplus */ 14 | 15 | #include "lwip/apps/snmp_core.h" 16 | 17 | extern const struct snmp_obj_id usmNoAuthProtocol; 18 | extern const struct snmp_obj_id usmHMACMD5AuthProtocol; 19 | extern const struct snmp_obj_id usmHMACSHAAuthProtocol; 20 | 21 | extern const struct snmp_obj_id usmNoPrivProtocol; 22 | extern const struct snmp_obj_id usmDESPrivProtocol; 23 | extern const struct snmp_obj_id usmAESPrivProtocol; 24 | 25 | extern const struct snmp_mib snmpframeworkmib; 26 | 27 | #ifdef __cplusplus 28 | } 29 | #endif /* __cplusplus */ 30 | 31 | #endif /* LWIP_SNMP */ 32 | #endif /* LWIP_HDR_APPS_SNMP_FRAMEWORK_MIB_H */ 33 | -------------------------------------------------------------------------------- /tun/stop.go: -------------------------------------------------------------------------------- 1 | package tun 2 | 3 | import ( 4 | "bytes" 5 | "log" 6 | "net" 7 | ) 8 | 9 | var stopMarker = []byte{2, 2, 2, 2, 2, 2, 2, 2} 10 | 11 | // Close of Windows and Linux tun/tap device do not interrupt blocking Read. 12 | // sendStopMarker is used to issue a specific packet to notify threads blocking 13 | // on Read. 14 | func sendStopMarker(src, dst string) { 15 | l, _ := net.ResolveUDPAddr("udp", src+":2222") 16 | r, _ := net.ResolveUDPAddr("udp", dst+":2222") 17 | conn, err := net.DialUDP("udp", l, r) 18 | if err != nil { 19 | log.Printf("fail to send stopmarker: %s", err) 20 | return 21 | } 22 | defer conn.Close() 23 | conn.Write(stopMarker) 24 | } 25 | 26 | func isStopMarker(pkt []byte, src, dst net.IP) bool { 27 | n := len(pkt) 28 | // at least should be 20(ip) + 8(udp) + 8(stopmarker) 29 | if n < 20+8+8 { 30 | return false 31 | } 32 | return pkt[0]&0xf0 == 0x40 && pkt[9] == 0x11 && src.Equal(pkt[12:16]) && 33 | dst.Equal(pkt[16:20]) && bytes.Compare(pkt[n-8:n], stopMarker) == 0 34 | } 35 | -------------------------------------------------------------------------------- /proxy/echo/udp.go: -------------------------------------------------------------------------------- 1 | package echo 2 | 3 | import ( 4 | "log" 5 | "net" 6 | 7 | "github.com/eycorsican/go-tun2socks/core" 8 | ) 9 | 10 | // An echo server, do nothing but echo back data to the sender. 11 | type udpHandler struct{} 12 | 13 | func NewUDPHandler() core.ConnectionHandler { 14 | return &udpHandler{} 15 | } 16 | 17 | func (h *udpHandler) Connect(conn core.Connection, target net.Addr) error { 18 | return nil 19 | } 20 | 21 | func (h *udpHandler) DidReceive(conn core.Connection, data []byte) error { 22 | // Dispatch to another goroutine, otherwise will result in deadlock. 23 | payload := append([]byte(nil), data...) 24 | go func(b []byte) { 25 | _, err := conn.Write(b) 26 | if err != nil { 27 | log.Printf("failed to echo back data: %v", err) 28 | } 29 | }(payload) 30 | return nil 31 | } 32 | 33 | func (h *udpHandler) DidSend(conn core.Connection, len uint16) { 34 | } 35 | 36 | func (h *udpHandler) DidClose(conn core.Connection) { 37 | } 38 | 39 | func (h *udpHandler) LocalDidClose(conn core.Connection) { 40 | } 41 | -------------------------------------------------------------------------------- /core/handler.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | import ( 4 | "net" 5 | ) 6 | 7 | // ConnectionHandler handles connections comming from TUN. 8 | type ConnectionHandler interface { 9 | // Connect connects the proxy server. 10 | Connect(conn Connection, target net.Addr) error 11 | 12 | // DidReceive will be called when data arrives from TUN. 13 | DidReceive(conn Connection, data []byte) error 14 | 15 | // DidSend will be called when sent data has been acknowledged by local clients. 16 | DidSend(conn Connection, len uint16) 17 | 18 | // DidClose will be called when the connection has been closed. 19 | DidClose(conn Connection) 20 | 21 | // LocalDidClose will be called when local client has close the connection. 22 | LocalDidClose(conn Connection) 23 | } 24 | 25 | var tcpConnectionHandler ConnectionHandler 26 | var udpConnectionHandler ConnectionHandler 27 | 28 | func RegisterTCPConnectionHandler(h ConnectionHandler) { 29 | tcpConnectionHandler = h 30 | } 31 | 32 | func RegisterUDPConnectionHandler(h ConnectionHandler) { 33 | udpConnectionHandler = h 34 | } 35 | -------------------------------------------------------------------------------- /route/util.go: -------------------------------------------------------------------------------- 1 | package route 2 | 3 | import ( 4 | vnet "v2ray.com/core/common/net" 5 | ) 6 | 7 | const ( 8 | IPVERSION_4 = 4 9 | IPVERSION_6 = 6 10 | 11 | PROTOCOL_ICMP = 1 12 | PROTOCOL_TCP = 6 13 | PROTOCOL_UDP = 17 14 | ) 15 | 16 | func PeekIPVersion(data []byte) uint8 { 17 | return uint8((data[0] & 0xf0) >> 4) 18 | } 19 | 20 | func PeekProtocol(data []byte) string { 21 | switch uint8(data[9]) { 22 | case PROTOCOL_ICMP: 23 | return "icmp" 24 | case PROTOCOL_TCP: 25 | return "tcp" 26 | case PROTOCOL_UDP: 27 | return "udp" 28 | default: 29 | return "unknown" 30 | } 31 | } 32 | 33 | func PeekDestinationAddress(data []byte) vnet.Address { 34 | return vnet.IPAddress(data[16:20]) 35 | } 36 | 37 | func PeekDestinationPort(data []byte) vnet.Port { 38 | ihl := uint8(data[0] & 0x0f) 39 | return vnet.PortFromBytes(data[ihl*4+2 : ihl*4+4]) 40 | } 41 | 42 | func IsSYNSegment(data []byte) bool { 43 | ihl := uint8(data[0] & 0x0f) 44 | if uint8(data[ihl*4+13]&(1<<1)) == 0 { 45 | return false 46 | } else { 47 | return true 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 eycorsican 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /core/connection.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | import ( 4 | "net" 5 | ) 6 | 7 | // Connection abstracts a TCP/UDP connection comming from TUN. This connection 8 | // should be handled by a registered TCP/UDP proxy handler. 9 | type Connection interface { 10 | // RemoteAddr returns the destination network address. 11 | RemoteAddr() net.Addr 12 | 13 | // LocalAddr returns the local client network address. 14 | LocalAddr() net.Addr 15 | 16 | // Receive receives data from TUN. 17 | Receive(data []byte) error 18 | 19 | // Write writes data to TUN. 20 | Write(data []byte) (int, error) 21 | 22 | // Sent will be called when sent data has been acknowledged by clients (TCP only). 23 | Sent(len uint16) error 24 | 25 | // Close closes the connection (TCP only). 26 | Close() error 27 | 28 | // Abort aborts the connection to client by sending a RST segment (TCP only). 29 | Abort() 30 | 31 | // Err will be called when a fatal error has occurred on the connection (TCP only). 32 | Err(err error) 33 | 34 | // LocalDidClose will be called when local client has close the connection (TCP only). 35 | LocalDidClose() error 36 | 37 | // Poll will be periodically called by timers (TCP only). 38 | Poll() error 39 | } 40 | -------------------------------------------------------------------------------- /core/udp_callback_export.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./src/include 5 | #include "lwip/udp.h" 6 | */ 7 | import "C" 8 | import ( 9 | "unsafe" 10 | ) 11 | 12 | //export UDPRecvFn 13 | func UDPRecvFn(arg unsafe.Pointer, pcb *C.struct_udp_pcb, p *C.struct_pbuf, addr *C.ip_addr_t, port C.u16_t, destAddr *C.ip_addr_t, destPort C.u16_t) { 14 | defer func() { 15 | if p != nil { 16 | C.pbuf_free(p) 17 | } 18 | }() 19 | 20 | if pcb == nil { 21 | return 22 | } 23 | 24 | srcAddr := ParseUDPAddr(IPAddrNTOA(*addr), uint16(port)) 25 | dstAddr := ParseUDPAddr(IPAddrNTOA(*destAddr), uint16(destPort)) 26 | if srcAddr == nil || dstAddr == nil { 27 | panic("invalid UDP address") 28 | } 29 | 30 | connId := udpConnId{ 31 | src: srcAddr.String(), 32 | dst: dstAddr.String(), 33 | } 34 | conn, found := udpConns.Load(connId) 35 | if !found { 36 | if udpConnectionHandler == nil { 37 | panic("no registered UDP connection handlers found") 38 | } 39 | var err error 40 | conn, err = NewUDPConnection(pcb, 41 | udpConnectionHandler, 42 | *addr, 43 | *destAddr, 44 | port, 45 | destPort) 46 | if err != nil { 47 | return 48 | } 49 | udpConns.Store(connId, conn) 50 | } 51 | 52 | buf := (*[1 << 30]byte)(unsafe.Pointer(p.payload))[:int(p.tot_len):int(p.tot_len)] 53 | conn.(Connection).Receive(buf) 54 | } 55 | -------------------------------------------------------------------------------- /core/tcp_conn_map.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./src/include 5 | #include "lwip/tcp.h" 6 | #include 7 | 8 | void* 9 | new_conn_key_arg() 10 | { 11 | return malloc(sizeof(uint32_t)); 12 | } 13 | 14 | void 15 | free_conn_key_arg(void *arg) 16 | { 17 | free(arg); 18 | } 19 | 20 | void 21 | set_conn_key_val(void *arg, uint32_t val) 22 | { 23 | *((uint32_t*)arg) = val; 24 | } 25 | 26 | uint32_t 27 | get_conn_key_val(void *arg) 28 | { 29 | return *((uint32_t*)arg); 30 | } 31 | */ 32 | import "C" 33 | import ( 34 | "sync" 35 | "unsafe" 36 | ) 37 | 38 | var tcpConns sync.Map 39 | 40 | // We need such a key-value mechanism because when passing a Go pointer 41 | // to C, the Go pointer will only be valid during the call. 42 | // If we pass a Go pointer to tcp_arg(), this pointer will not be usable 43 | // in subsequent callbacks (e.g.: tcp_recv(), tcp_err()). 44 | // 45 | // Instead we need to pass a C pointer to tcp_arg(), we manually allocate 46 | // the memory in C and return its pointer to Go code. After the connection 47 | // end, the memory should be freed manually. 48 | // 49 | // See also: 50 | // https://github.com/golang/go/issues/12416 51 | func NewConnKeyArg() unsafe.Pointer { 52 | return C.new_conn_key_arg() 53 | } 54 | 55 | func FreeConnKeyArg(p unsafe.Pointer) { 56 | C.free_conn_key_arg(p) 57 | } 58 | 59 | func SetConnKeyVal(p unsafe.Pointer, val uint32) { 60 | C.set_conn_key_val(p, C.uint32_t(val)) 61 | } 62 | 63 | func GetConnKeyVal(p unsafe.Pointer) uint32 { 64 | return uint32(C.get_conn_key_val(p)) 65 | } 66 | -------------------------------------------------------------------------------- /core/tcp_callback.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./src/include 5 | #include "lwip/tcp.h" 6 | 7 | extern err_t TCPAcceptFn(void *arg, struct tcp_pcb *newpcb, err_t err); 8 | 9 | void 10 | set_tcp_accept_callback(struct tcp_pcb *pcb) { 11 | tcp_accept(pcb, TCPAcceptFn); 12 | } 13 | 14 | extern err_t TCPRecvFn(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err); 15 | 16 | void 17 | set_tcp_recv_callback(struct tcp_pcb *pcb) { 18 | tcp_recv(pcb, TCPRecvFn); 19 | } 20 | 21 | extern err_t TCPSentFn(void *arg, struct tcp_pcb *tpcb, u16_t len); 22 | 23 | void 24 | set_tcp_sent_callback(struct tcp_pcb *pcb) { 25 | tcp_sent(pcb, TCPSentFn); 26 | } 27 | 28 | extern void TCPErrFn(void *arg, err_t err); 29 | 30 | void 31 | set_tcp_err_callback(struct tcp_pcb *pcb) { 32 | tcp_err(pcb, TCPErrFn); 33 | } 34 | 35 | extern err_t TCPPollFn(void *arg, struct tcp_pcb *tpcb); 36 | 37 | void 38 | set_tcp_poll_callback(struct tcp_pcb *pcb, u8_t interval) { 39 | tcp_poll(pcb, TCPPollFn, interval); 40 | } 41 | */ 42 | import "C" 43 | 44 | func SetTCPAcceptCallback(pcb *C.struct_tcp_pcb) { 45 | C.set_tcp_accept_callback(pcb) 46 | } 47 | 48 | func SetTCPRecvCallback(pcb *C.struct_tcp_pcb) { 49 | C.set_tcp_recv_callback(pcb) 50 | } 51 | 52 | func SetTCPSentCallback(pcb *C.struct_tcp_pcb) { 53 | C.set_tcp_sent_callback(pcb) 54 | } 55 | 56 | func SetTCPErrCallback(pcb *C.struct_tcp_pcb) { 57 | C.set_tcp_err_callback(pcb) 58 | } 59 | 60 | func SetTCPPollCallback(pcb *C.struct_tcp_pcb, interval C.u8_t) { 61 | C.set_tcp_poll_callback(pcb, interval) 62 | } 63 | -------------------------------------------------------------------------------- /proxy/echo/tcp.go: -------------------------------------------------------------------------------- 1 | package echo 2 | 3 | import ( 4 | "net" 5 | 6 | "github.com/eycorsican/go-tun2socks/core" 7 | ) 8 | 9 | var bufSize = 10 * 1024 10 | 11 | type connEntry struct { 12 | data []byte 13 | conn core.Connection 14 | } 15 | 16 | // An echo proxy, do nothing but echo back data to the sender, the handler was 17 | // created for testing purposes, it may causes issues when more than one clients 18 | // are connecting the handler simultaneously. 19 | type tcpHandler struct { 20 | buf chan *connEntry 21 | } 22 | 23 | func NewTCPHandler() core.ConnectionHandler { 24 | handler := &tcpHandler{ 25 | buf: make(chan *connEntry, bufSize), 26 | } 27 | go handler.echoBack() 28 | return handler 29 | } 30 | 31 | func (h *tcpHandler) echoBack() { 32 | for { 33 | e := <-h.buf 34 | _, err := e.conn.Write(e.data) 35 | if err != nil { 36 | e.conn.Close() 37 | } 38 | } 39 | } 40 | 41 | func (h *tcpHandler) Connect(conn core.Connection, target net.Addr) error { 42 | return nil 43 | } 44 | 45 | func (h *tcpHandler) DidReceive(conn core.Connection, data []byte) error { 46 | payload := append([]byte(nil), data...) 47 | // This function runs in lwIP thread, we can't block, so discarding data if 48 | // buf if full. 49 | select { 50 | case h.buf <- &connEntry{data: payload, conn: conn}: 51 | default: 52 | } 53 | return nil 54 | } 55 | 56 | func (h *tcpHandler) DidSend(conn core.Connection, len uint16) { 57 | } 58 | 59 | func (h *tcpHandler) DidClose(conn core.Connection) { 60 | } 61 | 62 | func (h *tcpHandler) LocalDidClose(conn core.Connection) { 63 | } 64 | -------------------------------------------------------------------------------- /tun/tun_darwin.go: -------------------------------------------------------------------------------- 1 | package tun 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "io" 7 | "net" 8 | "os/exec" 9 | "strconv" 10 | "strings" 11 | 12 | "github.com/songgao/water" 13 | ) 14 | 15 | func isIPv4(ip net.IP) bool { 16 | if ip.To4() != nil { 17 | return true 18 | } 19 | return false 20 | } 21 | 22 | func isIPv6(ip net.IP) bool { 23 | // To16() also valid for ipv4, ensure it's not an ipv4 address 24 | if ip.To4() != nil { 25 | return false 26 | } 27 | if ip.To16() != nil { 28 | return true 29 | } 30 | return false 31 | } 32 | 33 | func OpenTunDevice(name, addr, gw, mask string, dnsServers []string) (io.ReadWriteCloser, error) { 34 | tunDev, err := water.New(water.Config{ 35 | DeviceType: water.TUN, 36 | }) 37 | name = tunDev.Name() 38 | if err != nil { 39 | return nil, err 40 | } 41 | ip := net.ParseIP(addr) 42 | if ip == nil { 43 | return nil, errors.New("invalid IP address") 44 | } 45 | 46 | var params string 47 | if isIPv4(ip) { 48 | params = fmt.Sprintf("%s inet %s netmask %s %s", name, addr, mask, gw) 49 | } else if isIPv6(ip) { 50 | prefixlen, err := strconv.Atoi(mask) 51 | if err != nil { 52 | return nil, errors.New(fmt.Sprintf("parse IPv6 prefixlen failed: %v", err)) 53 | } 54 | params = fmt.Sprintf("%s inet6 %s/%d", name, addr, prefixlen) 55 | } else { 56 | return nil, errors.New("invalid IP address") 57 | } 58 | 59 | out, err := exec.Command("ifconfig", strings.Split(params, " ")...).Output() 60 | if err != nil { 61 | if len(out) != 0 { 62 | return nil, errors.New(fmt.Sprintf("%v, output: %s", err, out)) 63 | } 64 | return nil, err 65 | } 66 | return tunDev, nil 67 | } 68 | -------------------------------------------------------------------------------- /core/errors.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | // Error codes defined in lwIP. 4 | // /** Definitions for error constants. */ 5 | // typedef enum { 6 | // /** No error, everything OK. */ 7 | // ERR_OK = 0, 8 | // /** Out of memory error. */ 9 | // ERR_MEM = -1, 10 | // /** Buffer error. */ 11 | // ERR_BUF = -2, 12 | // /** Timeout. */ 13 | // ERR_TIMEOUT = -3, 14 | // /** Routing problem. */ 15 | // ERR_RTE = -4, 16 | // /** Operation in progress */ 17 | // ERR_INPROGRESS = -5, 18 | // /** Illegal value. */ 19 | // ERR_VAL = -6, 20 | // /** Operation would block. */ 21 | // ERR_WOULDBLOCK = -7, 22 | // /** Address in use. */ 23 | // ERR_USE = -8, 24 | // /** Already connecting. */ 25 | // ERR_ALREADY = -9, 26 | // /** Conn already established.*/ 27 | // ERR_ISCONN = -10, 28 | // /** Not connected. */ 29 | // ERR_CONN = -11, 30 | // /** Low-level netif error */ 31 | // ERR_IF = -12, 32 | // 33 | // /** Connection aborted. */ 34 | // ERR_ABRT = -13, 35 | // /** Connection reset. */ 36 | // ERR_RST = -14, 37 | // /** Connection closed. */ 38 | // ERR_CLSD = -15, 39 | // /** Illegal argument. */ 40 | // ERR_ARG = -16 41 | // } err_enum_t; 42 | 43 | const ( 44 | LWIP_ERR_OK int = iota 45 | LWIP_ERR_ABRT 46 | ) 47 | 48 | type lwipError struct { 49 | Code int 50 | } 51 | 52 | func NewLWIPError(code int) error { 53 | return &lwipError{Code: code} 54 | } 55 | 56 | func (e *lwipError) Error() string { 57 | return "error code " + string(e.Code) 58 | } 59 | -------------------------------------------------------------------------------- /core/src/include/netif/ppp/chap-md5.h: -------------------------------------------------------------------------------- 1 | /* 2 | * chap-md5.h - New CHAP/MD5 implementation. 3 | * 4 | * Copyright (c) 2003 Paul Mackerras. All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * 2. The name(s) of the authors of this software must not be used to 14 | * endorse or promote products derived from this software without 15 | * prior written permission. 16 | * 17 | * 3. Redistributions of any form whatsoever must retain the following 18 | * acknowledgment: 19 | * "This product includes software developed by Paul Mackerras 20 | * ". 21 | * 22 | * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO 23 | * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 24 | * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY 25 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 26 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 27 | * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 28 | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 29 | */ 30 | 31 | #include "netif/ppp/ppp_opts.h" 32 | #if PPP_SUPPORT && CHAP_SUPPORT /* don't build if not configured for use in lwipopts.h */ 33 | 34 | extern const struct chap_digest_type md5_digest; 35 | 36 | #endif /* PPP_SUPPORT && CHAP_SUPPORT */ 37 | -------------------------------------------------------------------------------- /core/src/include/posix/errno.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * This file is a posix wrapper for lwip/errno.h. 4 | */ 5 | 6 | /* 7 | * Redistribution and use in source and binary forms, with or without modification, 8 | * are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. The name of the author may not be used to endorse or promote products 16 | * derived from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 20 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 21 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 23 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 26 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 27 | * OF SUCH DAMAGE. 28 | * 29 | * This file is part of the lwIP TCP/IP stack. 30 | * 31 | */ 32 | 33 | #include "lwip/errno.h" 34 | -------------------------------------------------------------------------------- /core/src/include/posix/netdb.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * This file is a posix wrapper for lwip/netdb.h. 4 | */ 5 | 6 | /* 7 | * Redistribution and use in source and binary forms, with or without modification, 8 | * are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. The name of the author may not be used to endorse or promote products 16 | * derived from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 20 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 21 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 23 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 26 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 27 | * OF SUCH DAMAGE. 28 | * 29 | * This file is part of the lwIP TCP/IP stack. 30 | * 31 | */ 32 | 33 | #include "lwip/netdb.h" 34 | -------------------------------------------------------------------------------- /core/src/include/compat/posix/netdb.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * This file is a posix wrapper for lwip/netdb.h. 4 | */ 5 | 6 | /* 7 | * Redistribution and use in source and binary forms, with or without modification, 8 | * are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. The name of the author may not be used to endorse or promote products 16 | * derived from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 20 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 21 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 23 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 26 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 27 | * OF SUCH DAMAGE. 28 | * 29 | * This file is part of the lwIP TCP/IP stack. 30 | * 31 | */ 32 | 33 | #include "lwip/netdb.h" 34 | -------------------------------------------------------------------------------- /core/src/include/compat/stdc/errno.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * This file is a posix/stdc wrapper for lwip/errno.h. 4 | */ 5 | 6 | /* 7 | * Redistribution and use in source and binary forms, with or without modification, 8 | * are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. The name of the author may not be used to endorse or promote products 16 | * derived from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 20 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 21 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 23 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 26 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 27 | * OF SUCH DAMAGE. 28 | * 29 | * This file is part of the lwIP TCP/IP stack. 30 | * 31 | */ 32 | 33 | #include "lwip/errno.h" 34 | -------------------------------------------------------------------------------- /core/src/include/posix/sys/socket.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * This file is a posix wrapper for lwip/sockets.h. 4 | */ 5 | 6 | /* 7 | * Redistribution and use in source and binary forms, with or without modification, 8 | * are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. The name of the author may not be used to endorse or promote products 16 | * derived from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 20 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 21 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 23 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 26 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 27 | * OF SUCH DAMAGE. 28 | * 29 | * This file is part of the lwIP TCP/IP stack. 30 | * 31 | */ 32 | 33 | #include "lwip/sockets.h" 34 | -------------------------------------------------------------------------------- /core/src/include/compat/posix/arpa/inet.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * This file is a posix wrapper for lwip/sockets.h. 4 | */ 5 | 6 | /* 7 | * Redistribution and use in source and binary forms, with or without modification, 8 | * are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. The name of the author may not be used to endorse or promote products 16 | * derived from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 20 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 21 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 23 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 26 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 27 | * OF SUCH DAMAGE. 28 | * 29 | * This file is part of the lwIP TCP/IP stack. 30 | * 31 | */ 32 | 33 | #include "lwip/sockets.h" 34 | -------------------------------------------------------------------------------- /core/src/include/compat/posix/sys/socket.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * This file is a posix wrapper for lwip/sockets.h. 4 | */ 5 | 6 | /* 7 | * Redistribution and use in source and binary forms, with or without modification, 8 | * are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. The name of the author may not be used to endorse or promote products 16 | * derived from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 20 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 21 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 23 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 26 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 27 | * OF SUCH DAMAGE. 28 | * 29 | * This file is part of the lwIP TCP/IP stack. 30 | * 31 | */ 32 | 33 | #include "lwip/sockets.h" 34 | -------------------------------------------------------------------------------- /core/src/include/compat/posix/net/if.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * This file is a posix wrapper for lwip/if_api.h. 4 | */ 5 | 6 | /* 7 | * Copyright (c) 2017 Joel Cunningham, Garmin International, Inc. 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, 14 | * this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 3. The name of the author may not be used to endorse or promote products 19 | * derived from this software without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 | * OF SUCH DAMAGE. 31 | * 32 | * This file is part of the lwIP TCP/IP stack. 33 | * 34 | */ 35 | 36 | #include "lwip/if_api.h" 37 | -------------------------------------------------------------------------------- /core/util.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./src/include 5 | #include "lwip/tcp.h" 6 | #include 7 | */ 8 | import "C" 9 | import ( 10 | "fmt" 11 | "net" 12 | "sync" 13 | "unsafe" 14 | ) 15 | 16 | // ipaddr_ntoa() is using a global static buffer to return result, 17 | // reentrants are not allowed, caller is required to lock lwipMutex. 18 | func IPAddrNTOA(ipaddr C.struct_ip_addr) string { 19 | return C.GoString(C.ipaddr_ntoa(&ipaddr)) 20 | } 21 | 22 | func IPAddrATON(cp string, addr *C.struct_ip_addr) { 23 | ccp := C.CString(cp) 24 | C.ipaddr_aton(ccp, addr) 25 | C.free(unsafe.Pointer(ccp)) 26 | } 27 | 28 | func ParseTCPAddr(addr string, port uint16) net.Addr { 29 | ip := net.ParseIP(addr).To4() 30 | if ip != nil { 31 | // Seems an IPv4 address. 32 | netAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", addr, port)) 33 | if err != nil { 34 | return nil 35 | } 36 | return netAddr 37 | } 38 | ip = net.ParseIP(addr).To16() 39 | if ip != nil { 40 | // Seems an IPv6 address. 41 | netAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("[%s]:%d", addr, port)) 42 | if err != nil { 43 | return nil 44 | } 45 | return netAddr 46 | } 47 | return nil 48 | } 49 | 50 | func ParseUDPAddr(addr string, port uint16) net.Addr { 51 | ip := net.ParseIP(addr).To4() 52 | if ip != nil { 53 | // Seems an IPv4 address. 54 | netAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", addr, port)) 55 | if err != nil { 56 | return nil 57 | } 58 | return netAddr 59 | } 60 | ip = net.ParseIP(addr).To16() 61 | if ip != nil { 62 | // Seems an IPv6 address. 63 | netAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("[%s]:%d", addr, port)) 64 | if err != nil { 65 | return nil 66 | } 67 | return netAddr 68 | } 69 | return nil 70 | } 71 | 72 | func GetSyncMapLen(m sync.Map) int { 73 | length := 0 74 | m.Range(func(_, _ interface{}) bool { 75 | length++ 76 | return true 77 | }) 78 | return length 79 | } 80 | -------------------------------------------------------------------------------- /core/src/include/netif/ppp/chap_ms.h: -------------------------------------------------------------------------------- 1 | /* 2 | * chap_ms.h - Challenge Handshake Authentication Protocol definitions. 3 | * 4 | * Copyright (c) 1995 Eric Rosenquist. All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * 3. The name(s) of the authors of this software must not be used to 19 | * endorse or promote products derived from this software without 20 | * prior written permission. 21 | * 22 | * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO 23 | * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 24 | * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY 25 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 26 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 27 | * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 28 | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 29 | * 30 | * $Id: chap_ms.h,v 1.13 2004/11/15 22:13:26 paulus Exp $ 31 | */ 32 | 33 | #include "netif/ppp/ppp_opts.h" 34 | #if PPP_SUPPORT && MSCHAP_SUPPORT /* don't build if not configured for use in lwipopts.h */ 35 | 36 | #ifndef CHAPMS_INCLUDE 37 | #define CHAPMS_INCLUDE 38 | 39 | extern const struct chap_digest_type chapms_digest; 40 | extern const struct chap_digest_type chapms2_digest; 41 | 42 | #endif /* CHAPMS_INCLUDE */ 43 | 44 | #endif /* PPP_SUPPORT && MSCHAP_SUPPORT */ 45 | -------------------------------------------------------------------------------- /proxy/direct/tcp.go: -------------------------------------------------------------------------------- 1 | package direct 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "io" 7 | "log" 8 | "net" 9 | "sync" 10 | "time" 11 | 12 | "github.com/eycorsican/go-tun2socks/core" 13 | ) 14 | 15 | type tcpHandler struct { 16 | sync.Mutex 17 | conns map[core.Connection]net.Conn 18 | } 19 | 20 | func NewTCPHandler() core.ConnectionHandler { 21 | return &tcpHandler{ 22 | conns: make(map[core.Connection]net.Conn, 16), 23 | } 24 | } 25 | 26 | func (h *tcpHandler) fetchInput(conn core.Connection, input io.Reader) { 27 | _, err := io.Copy(conn, input) 28 | if err != nil { 29 | h.Close(conn) 30 | conn.Close() 31 | } 32 | } 33 | 34 | func (h *tcpHandler) Connect(conn core.Connection, target net.Addr) error { 35 | c, err := net.Dial("tcp", target.String()) 36 | if err != nil { 37 | return err 38 | } 39 | h.Lock() 40 | h.conns[conn] = c 41 | h.Unlock() 42 | c.SetReadDeadline(time.Time{}) 43 | go h.fetchInput(conn, c) 44 | log.Printf("new proxy connection for target: %s:%s", target.Network(), target.String()) 45 | return nil 46 | } 47 | 48 | func (h *tcpHandler) DidReceive(conn core.Connection, data []byte) error { 49 | h.Lock() 50 | c, ok := h.conns[conn] 51 | h.Unlock() 52 | if ok { 53 | _, err := c.Write(data) 54 | if err != nil { 55 | h.Close(conn) 56 | return errors.New(fmt.Sprintf("write remote failed: %v", err)) 57 | } 58 | return nil 59 | } else { 60 | h.Close(conn) 61 | return errors.New(fmt.Sprintf("proxy connection %v->%v does not exists", conn.LocalAddr(), conn.RemoteAddr())) 62 | } 63 | } 64 | 65 | func (h *tcpHandler) DidSend(conn core.Connection, len uint16) { 66 | // unused 67 | } 68 | 69 | func (h *tcpHandler) DidClose(conn core.Connection) { 70 | h.Close(conn) 71 | } 72 | 73 | func (h *tcpHandler) LocalDidClose(conn core.Connection) { 74 | h.Close(conn) 75 | } 76 | 77 | func (h *tcpHandler) Close(conn core.Connection) { 78 | h.Lock() 79 | defer h.Unlock() 80 | 81 | if c, found := h.conns[conn]; found { 82 | c.Close() 83 | } 84 | delete(h.conns, conn) 85 | } 86 | -------------------------------------------------------------------------------- /core/src/include/lwip/apps/netbiosns.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * NETBIOS name service responder 4 | */ 5 | 6 | /* 7 | * Redistribution and use in source and binary forms, with or without modification, 8 | * are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. The name of the author may not be used to endorse or promote products 16 | * derived from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 20 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 21 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 23 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 26 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 27 | * OF SUCH DAMAGE. 28 | * 29 | * This file is part of the lwIP TCP/IP stack. 30 | * 31 | */ 32 | #ifndef LWIP_HDR_APPS_NETBIOS_H 33 | #define LWIP_HDR_APPS_NETBIOS_H 34 | 35 | #include "lwip/apps/netbiosns_opts.h" 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | void netbiosns_init(void); 42 | #ifndef NETBIOS_LWIP_NAME 43 | void netbiosns_set_name(const char* hostname); 44 | #endif 45 | void netbiosns_stop(void); 46 | 47 | #ifdef __cplusplus 48 | } 49 | #endif 50 | 51 | #endif /* LWIP_HDR_APPS_NETBIOS_H */ 52 | -------------------------------------------------------------------------------- /core/src/include/lwip/apps/smtp_opts.h: -------------------------------------------------------------------------------- 1 | #ifndef LWIP_HDR_APPS_SMTP_OPTS_H 2 | #define LWIP_HDR_APPS_SMTP_OPTS_H 3 | 4 | #include "lwip/opt.h" 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | /** 11 | * @defgroup smtp_opts Options 12 | * @ingroup smtp 13 | * 14 | * @{ 15 | */ 16 | 17 | /** Set this to 1 to enable data handler callback on BODY */ 18 | #ifndef SMTP_BODYDH 19 | #define SMTP_BODYDH 0 20 | #endif 21 | 22 | /** SMTP_DEBUG: Enable debugging for SNTP. */ 23 | #ifndef SMTP_DEBUG 24 | #define SMTP_DEBUG LWIP_DBG_OFF 25 | #endif 26 | 27 | /** Maximum length reserved for server name including terminating 0 byte */ 28 | #ifndef SMTP_MAX_SERVERNAME_LEN 29 | #define SMTP_MAX_SERVERNAME_LEN 256 30 | #endif 31 | 32 | /** Maximum length reserved for username */ 33 | #ifndef SMTP_MAX_USERNAME_LEN 34 | #define SMTP_MAX_USERNAME_LEN 32 35 | #endif 36 | 37 | /** Maximum length reserved for password */ 38 | #ifndef SMTP_MAX_PASS_LEN 39 | #define SMTP_MAX_PASS_LEN 32 40 | #endif 41 | 42 | /** Set this to 0 if you know the authentication data will not change 43 | * during the smtp session, which saves some heap space. */ 44 | #ifndef SMTP_COPY_AUTHDATA 45 | #define SMTP_COPY_AUTHDATA 1 46 | #endif 47 | 48 | /** Set this to 0 to save some code space if you know for sure that all data 49 | * passed to this module conforms to the requirements in the SMTP RFC. 50 | * WARNING: use this with care! 51 | */ 52 | #ifndef SMTP_CHECK_DATA 53 | #define SMTP_CHECK_DATA 1 54 | #endif 55 | 56 | /** Set this to 1 to enable AUTH PLAIN support */ 57 | #ifndef SMTP_SUPPORT_AUTH_PLAIN 58 | #define SMTP_SUPPORT_AUTH_PLAIN 1 59 | #endif 60 | 61 | /** Set this to 1 to enable AUTH LOGIN support */ 62 | #ifndef SMTP_SUPPORT_AUTH_LOGIN 63 | #define SMTP_SUPPORT_AUTH_LOGIN 1 64 | #endif 65 | 66 | /* Memory allocation/deallocation can be overridden... */ 67 | #ifndef SMTP_STATE_MALLOC 68 | #define SMTP_STATE_MALLOC(size) mem_malloc(size) 69 | #define SMTP_STATE_FREE(ptr) mem_free(ptr) 70 | #endif 71 | 72 | /** 73 | * @} 74 | */ 75 | 76 | #ifdef __cplusplus 77 | } 78 | #endif 79 | 80 | #endif /* SMTP_OPTS_H */ 81 | 82 | -------------------------------------------------------------------------------- /core/lwip.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./src/include 5 | #include "lwip/tcp.h" 6 | #include "lwip/udp.h" 7 | */ 8 | import "C" 9 | import ( 10 | "sync" 11 | "unsafe" 12 | ) 13 | 14 | type LWIPStack interface { 15 | Write([]byte) (int, error) 16 | Close() error 17 | } 18 | 19 | // lwIP runs in a single thread, locking is needed in Go runtime. 20 | var lwipMutex = &sync.Mutex{} 21 | 22 | type lwipStack struct { 23 | tpcb *C.struct_tcp_pcb 24 | upcb *C.struct_udp_pcb 25 | } 26 | 27 | func NewLWIPStack() LWIPStack { 28 | tcpPCB := C.tcp_new() 29 | if tcpPCB == nil { 30 | panic("tcp_new return nil") 31 | } 32 | 33 | err := C.tcp_bind(tcpPCB, C.IP_ADDR_ANY, 0) 34 | switch err { 35 | case C.ERR_OK: 36 | break 37 | case C.ERR_VAL: 38 | panic("invalid PCB state") 39 | case C.ERR_USE: 40 | panic("port in use") 41 | default: 42 | C.memp_free(C.MEMP_TCP_PCB, unsafe.Pointer(tcpPCB)) 43 | panic("unknown tcp_bind return value") 44 | } 45 | 46 | tcpPCB = C.tcp_listen_with_backlog(tcpPCB, C.TCP_DEFAULT_LISTEN_BACKLOG) 47 | if tcpPCB == nil { 48 | panic("can not allocate tcp pcb") 49 | } 50 | 51 | // We can't call C function with Go functions as arguments here, it will 52 | // fail in compile time: 53 | // cannot use TCPAcceptFn (type func(unsafe.Pointer, *_Ctype_struct_tcp_pcb, _Ctype_schar) _Ctype_schar) as type *[0]byte in argument to func literal 54 | // I can't find other workarounds. 55 | // C.tcp_accept(tcpPCB, TCPAcceptFn) 56 | SetTCPAcceptCallback(tcpPCB) 57 | 58 | udpPCB := C.udp_new() 59 | if udpPCB == nil { 60 | panic("could not allocate udp pcb") 61 | } 62 | 63 | err = C.udp_bind(udpPCB, C.IP_ADDR_ANY, 0) 64 | if err != C.ERR_OK { 65 | panic("address already in use") 66 | } 67 | 68 | SetUDPRecvCallback(udpPCB, nil) 69 | 70 | return &lwipStack{ 71 | tpcb: tcpPCB, 72 | upcb: udpPCB, 73 | } 74 | } 75 | 76 | func (s *lwipStack) Write(data []byte) (int, error) { 77 | return Input(data) 78 | } 79 | 80 | func (s *lwipStack) Close() error { 81 | tcpConns.Range(func(_, c interface{}) bool { 82 | c.(*tcpConn).Abort() 83 | return true 84 | }) 85 | udpConns.Range(func(_, c interface{}) bool { 86 | c.(*udpConn).Close() 87 | return true 88 | }) 89 | return nil 90 | } 91 | 92 | func init() { 93 | lwipInit() 94 | } 95 | -------------------------------------------------------------------------------- /core/src/core/ipv6/inet6.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * 4 | * INET v6 addresses. 5 | */ 6 | 7 | /* 8 | * Copyright (c) 2010 Inico Technologies Ltd. 9 | * All rights reserved. 10 | * 11 | * Redistribution and use in source and binary forms, with or without modification, 12 | * are permitted provided that the following conditions are met: 13 | * 14 | * 1. Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, 17 | * this list of conditions and the following disclaimer in the documentation 18 | * and/or other materials provided with the distribution. 19 | * 3. The name of the author may not be used to endorse or promote products 20 | * derived from this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 25 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 27 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 30 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 31 | * OF SUCH DAMAGE. 32 | * 33 | * This file is part of the lwIP TCP/IP stack. 34 | * 35 | * Author: Ivan Delamer 36 | * 37 | * 38 | * Please coordinate changes and requests with Ivan Delamer 39 | * 40 | */ 41 | 42 | #include "lwip/opt.h" 43 | 44 | #if LWIP_IPV6 && LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */ 45 | 46 | #include "lwip/def.h" 47 | #include "lwip/inet.h" 48 | 49 | /** This variable is initialized by the system to contain the wildcard IPv6 address. 50 | */ 51 | const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 52 | 53 | #endif /* LWIP_IPV6 */ 54 | -------------------------------------------------------------------------------- /core/src/include/netif/ppp/ecp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ecp.h - Definitions for PPP Encryption Control Protocol. 3 | * 4 | * Copyright (c) 2002 Google, Inc. 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions 9 | * are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in 16 | * the documentation and/or other materials provided with the 17 | * distribution. 18 | * 19 | * 3. The name(s) of the authors of this software must not be used to 20 | * endorse or promote products derived from this software without 21 | * prior written permission. 22 | * 23 | * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO 24 | * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 25 | * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY 26 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 27 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 28 | * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 29 | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 30 | * 31 | * $Id: ecp.h,v 1.2 2003/01/10 07:12:36 fcusack Exp $ 32 | */ 33 | 34 | #include "netif/ppp/ppp_opts.h" 35 | #if PPP_SUPPORT && ECP_SUPPORT /* don't build if not configured for use in lwipopts.h */ 36 | 37 | #ifndef ECP_H 38 | #define ECP_H 39 | 40 | #ifdef __cplusplus 41 | extern "C" { 42 | #endif 43 | 44 | typedef struct ecp_options { 45 | bool required; /* Is ECP required? */ 46 | unsigned enctype; /* Encryption type */ 47 | } ecp_options; 48 | 49 | extern fsm ecp_fsm[]; 50 | extern ecp_options ecp_wantoptions[]; 51 | extern ecp_options ecp_gotoptions[]; 52 | extern ecp_options ecp_allowoptions[]; 53 | extern ecp_options ecp_hisoptions[]; 54 | 55 | extern const struct protent ecp_protent; 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | 61 | #endif /* ECP_H */ 62 | #endif /* PPP_SUPPORT && ECP_SUPPORT */ 63 | -------------------------------------------------------------------------------- /proxy/v2ray/features.go: -------------------------------------------------------------------------------- 1 | package v2ray 2 | 3 | import ( 4 | // The following are necessary as they register handlers in their init functions. 5 | 6 | // Required features. Can't remove unless there is replacements. 7 | _ "v2ray.com/core/app/dispatcher" 8 | _ "v2ray.com/core/app/proxyman/inbound" 9 | _ "v2ray.com/core/app/proxyman/outbound" 10 | 11 | // Default commander and all its services. This is an optional feature. 12 | // _ "v2ray.com/core/app/commander" 13 | // _ "v2ray.com/core/app/log/command" 14 | // _ "v2ray.com/core/app/proxyman/command" 15 | // _ "v2ray.com/core/app/stats/command" 16 | 17 | // Other optional features. 18 | _ "v2ray.com/core/app/dns" 19 | _ "v2ray.com/core/app/log" 20 | _ "v2ray.com/core/app/policy" 21 | _ "v2ray.com/core/app/router" 22 | _ "v2ray.com/core/app/stats" 23 | 24 | // Inbound and outbound proxies. 25 | _ "v2ray.com/core/proxy/blackhole" 26 | _ "v2ray.com/core/proxy/dokodemo" 27 | _ "v2ray.com/core/proxy/freedom" 28 | _ "v2ray.com/core/proxy/http" 29 | _ "v2ray.com/core/proxy/mtproto" 30 | _ "v2ray.com/core/proxy/shadowsocks" 31 | _ "v2ray.com/core/proxy/socks" 32 | _ "v2ray.com/core/proxy/vmess/inbound" 33 | _ "v2ray.com/core/proxy/vmess/outbound" 34 | 35 | // Transports 36 | _ "v2ray.com/core/transport/internet/domainsocket" 37 | _ "v2ray.com/core/transport/internet/http" 38 | _ "v2ray.com/core/transport/internet/kcp" 39 | _ "v2ray.com/core/transport/internet/tcp" 40 | _ "v2ray.com/core/transport/internet/tls" 41 | _ "v2ray.com/core/transport/internet/udp" 42 | _ "v2ray.com/core/transport/internet/websocket" 43 | 44 | // Transport headers 45 | _ "v2ray.com/core/transport/internet/headers/http" 46 | _ "v2ray.com/core/transport/internet/headers/noop" 47 | _ "v2ray.com/core/transport/internet/headers/srtp" 48 | _ "v2ray.com/core/transport/internet/headers/tls" 49 | _ "v2ray.com/core/transport/internet/headers/utp" 50 | _ "v2ray.com/core/transport/internet/headers/wechat" 51 | _ "v2ray.com/core/transport/internet/headers/wireguard" 52 | 53 | // JSON config support. Choose only one from the two below. 54 | // The following line loads JSON from v2ctl 55 | // _ "v2ray.com/core/main/json" 56 | // The following line loads JSON internally 57 | _ "v2ray.com/core/main/jsonem" 58 | // Load config from file or http(s) 59 | // _ "v2ray.com/core/main/confloader/external" 60 | ) 61 | -------------------------------------------------------------------------------- /core/src/include/lwip/prot/ip.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * IP protocol definitions 4 | */ 5 | 6 | /* 7 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, 14 | * this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 3. The name of the author may not be used to endorse or promote products 19 | * derived from this software without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 | * OF SUCH DAMAGE. 31 | * 32 | * This file is part of the lwIP TCP/IP stack. 33 | * 34 | * Author: Adam Dunkels 35 | * 36 | */ 37 | #ifndef LWIP_HDR_PROT_IP_H 38 | #define LWIP_HDR_PROT_IP_H 39 | 40 | #include "lwip/arch.h" 41 | 42 | #ifdef __cplusplus 43 | extern "C" { 44 | #endif 45 | 46 | #define IP_PROTO_ICMP 1 47 | #define IP_PROTO_IGMP 2 48 | #define IP_PROTO_UDP 17 49 | #define IP_PROTO_UDPLITE 136 50 | #define IP_PROTO_TCP 6 51 | 52 | /** This operates on a void* by loading the first byte */ 53 | #define IP_HDR_GET_VERSION(ptr) ((*(u8_t*)(ptr)) >> 4) 54 | 55 | #ifdef __cplusplus 56 | } 57 | #endif 58 | 59 | #endif /* LWIP_HDR_PROT_IP_H */ 60 | -------------------------------------------------------------------------------- /scripts/release.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Create GITHUB_API_TOKEN and export it first. 4 | 5 | set -x 6 | 7 | tag=v1.7 8 | description="- Support IPv6 on macOS and Linux\n- Utilizing V2Ray's DNS client for DNS resolving" 9 | list_assets_cmd="ls -1 build/*.zip" 10 | token= 11 | if [ -z ${GITHUB_API_TOKEN+x} ]; then 12 | read -p 'Input the github API token:' token 13 | else 14 | token=$GITHUB_API_TOKEN 15 | fi 16 | 17 | declare -a executables=("tun2socks-darwin-10.6-amd64" "tun2socks-linux-amd64" "tun2socks-windows-4.0-amd64.exe") 18 | eval $list_assets_cmd 19 | if [ $? -ne 0 ]; then 20 | for i in ${executables[@]}; do 21 | zip "build/$i.zip" "build/$i" 22 | done 23 | fi 24 | 25 | owner=eycorsican 26 | repo=go-tun2socks 27 | base_url=https://api.github.com 28 | 29 | content_type_json="Content-Type: application/json" 30 | content_type_zip="Content-Type: application/zip" 31 | 32 | api_create_release=/repos/$owner/$repo/releases 33 | api_get_release_by_tag=/repos/$owner/$repo/releases/tags/$tag 34 | api_create_release_data="{\ 35 | \"tag_name\": \"${tag}\",\ 36 | \"target_commitish\": \"master\",\ 37 | \"name\": \"${tag}\", 38 | \"body\": \"${description}\", 39 | \"draft\": false,\ 40 | \"prerelease\": false\ 41 | }" 42 | 43 | # Get the release id by tag name. 44 | release_id=`curl -u $owner:$token -H "$content_type_json" -X GET "${base_url}${api_get_release_by_tag}" | \ 45 | python -c "import sys;import json;print(json.loads(\"\".join(sys.stdin.readlines()))[\"id\"])"` 2>/dev/null 46 | 47 | # If there is release with the tag exists, delete it first. 48 | if [ $? -eq 0 ]; then 49 | api_delete_release=/repos/$owner/$repo/releases/$release_id 50 | curl -u $owner:$token -H "$content_type_json" -X DELETE "${base_url}${api_delete_release}" 51 | fi 52 | 53 | # Create a release. 54 | curl -u $owner:$token -H "$content_type_json" -X POST "${base_url}${api_create_release}" --data "${api_create_release_data}" 55 | 56 | # Get the release id by tag name. 57 | release_id=`curl -u $owner:$token -H "$content_type_json" -X GET "${base_url}${api_get_release_by_tag}" | \ 58 | python -c "import sys;import json;print(json.loads(\"\".join(sys.stdin.readlines()))[\"id\"])"` 59 | 60 | # Upload assets. 61 | eval $list_assets_cmd | while read -r asset_name; do 62 | upload_url=https://uploads.github.com/repos/$owner/$repo/releases/$release_id/assets?name=$(basename $asset_name) 63 | curl --progress-bar -u $owner:$token -H "$content_type_zip" --data-binary @"$asset_name" -X POST $upload_url 64 | if [ $? -ne "0" ]; then 65 | exit 1 66 | fi 67 | done 68 | -------------------------------------------------------------------------------- /core/src/include/lwip/ethip6.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * 4 | * Ethernet output for IPv6. Uses ND tables for link-layer addressing. 5 | */ 6 | 7 | /* 8 | * Copyright (c) 2010 Inico Technologies Ltd. 9 | * All rights reserved. 10 | * 11 | * Redistribution and use in source and binary forms, with or without modification, 12 | * are permitted provided that the following conditions are met: 13 | * 14 | * 1. Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, 17 | * this list of conditions and the following disclaimer in the documentation 18 | * and/or other materials provided with the distribution. 19 | * 3. The name of the author may not be used to endorse or promote products 20 | * derived from this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 25 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 27 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 30 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 31 | * OF SUCH DAMAGE. 32 | * 33 | * This file is part of the lwIP TCP/IP stack. 34 | * 35 | * Author: Ivan Delamer 36 | * 37 | * 38 | * Please coordinate changes and requests with Ivan Delamer 39 | * 40 | */ 41 | 42 | #ifndef LWIP_HDR_ETHIP6_H 43 | #define LWIP_HDR_ETHIP6_H 44 | 45 | #include "lwip/opt.h" 46 | 47 | #if LWIP_IPV6 && LWIP_ETHERNET /* don't build if not configured for use in lwipopts.h */ 48 | 49 | #include "lwip/pbuf.h" 50 | #include "lwip/ip6.h" 51 | #include "lwip/ip6_addr.h" 52 | #include "lwip/netif.h" 53 | 54 | 55 | #ifdef __cplusplus 56 | extern "C" { 57 | #endif 58 | 59 | 60 | err_t ethip6_output(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr); 61 | 62 | #ifdef __cplusplus 63 | } 64 | #endif 65 | 66 | #endif /* LWIP_IPV6 && LWIP_ETHERNET */ 67 | 68 | #endif /* LWIP_HDR_ETHIP6_H */ 69 | -------------------------------------------------------------------------------- /core/src/include/lwip/prot/udp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * UDP protocol definitions 4 | */ 5 | 6 | /* 7 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, 14 | * this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 3. The name of the author may not be used to endorse or promote products 19 | * derived from this software without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 | * OF SUCH DAMAGE. 31 | * 32 | * This file is part of the lwIP TCP/IP stack. 33 | * 34 | * Author: Adam Dunkels 35 | * 36 | */ 37 | #ifndef LWIP_HDR_PROT_UDP_H 38 | #define LWIP_HDR_PROT_UDP_H 39 | 40 | #include "lwip/arch.h" 41 | 42 | #ifdef __cplusplus 43 | extern "C" { 44 | #endif 45 | 46 | #define UDP_HLEN 8 47 | 48 | /* Fields are (of course) in network byte order. */ 49 | #ifdef PACK_STRUCT_USE_INCLUDES 50 | # include "arch/bpstruct.h" 51 | #endif 52 | PACK_STRUCT_BEGIN 53 | struct udp_hdr { 54 | PACK_STRUCT_FIELD(u16_t src); 55 | PACK_STRUCT_FIELD(u16_t dest); /* src/dest UDP ports */ 56 | PACK_STRUCT_FIELD(u16_t len); 57 | PACK_STRUCT_FIELD(u16_t chksum); 58 | } PACK_STRUCT_STRUCT; 59 | PACK_STRUCT_END 60 | #ifdef PACK_STRUCT_USE_INCLUDES 61 | # include "arch/epstruct.h" 62 | #endif 63 | 64 | #ifdef __cplusplus 65 | } 66 | #endif 67 | 68 | #endif /* LWIP_HDR_PROT_UDP_H */ 69 | -------------------------------------------------------------------------------- /proxy/socks/tcp.go: -------------------------------------------------------------------------------- 1 | package socks 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "io" 7 | "log" 8 | "net" 9 | "sync" 10 | "time" 11 | 12 | "golang.org/x/net/proxy" 13 | 14 | "github.com/eycorsican/go-tun2socks/core" 15 | ) 16 | 17 | type tcpHandler struct { 18 | sync.Mutex 19 | 20 | proxyHost string 21 | proxyPort uint16 22 | conns map[core.Connection]net.Conn 23 | } 24 | 25 | func NewTCPHandler(proxyHost string, proxyPort uint16) core.ConnectionHandler { 26 | return &tcpHandler{ 27 | proxyHost: proxyHost, 28 | proxyPort: proxyPort, 29 | conns: make(map[core.Connection]net.Conn, 16), 30 | } 31 | } 32 | 33 | func (h *tcpHandler) fetchInput(conn core.Connection, input io.Reader) { 34 | defer func() { 35 | h.Close(conn) 36 | conn.Close() // also close tun2socks connection here 37 | }() 38 | 39 | _, err := io.Copy(conn, input) 40 | if err != nil { 41 | // log.Printf("fetch input failed: %v", err) 42 | return 43 | } 44 | } 45 | 46 | func (h *tcpHandler) getConn(conn core.Connection) (net.Conn, bool) { 47 | h.Lock() 48 | defer h.Unlock() 49 | if c, ok := h.conns[conn]; ok { 50 | return c, true 51 | } 52 | return nil, false 53 | } 54 | 55 | func (h *tcpHandler) Connect(conn core.Connection, target net.Addr) error { 56 | dialer, err := proxy.SOCKS5("tcp", core.ParseTCPAddr(h.proxyHost, h.proxyPort).String(), nil, nil) 57 | if err != nil { 58 | return err 59 | } 60 | c, err := dialer.Dial(target.Network(), target.String()) 61 | if err != nil { 62 | return err 63 | } 64 | h.Lock() 65 | h.conns[conn] = c 66 | h.Unlock() 67 | c.SetDeadline(time.Time{}) 68 | go h.fetchInput(conn, c) 69 | log.Printf("new proxy connection for target: %s:%s", target.Network(), target.String()) 70 | return nil 71 | } 72 | 73 | func (h *tcpHandler) DidReceive(conn core.Connection, data []byte) error { 74 | if c, found := h.getConn(conn); found { 75 | _, err := c.Write(data) 76 | if err != nil { 77 | h.Close(conn) 78 | return errors.New(fmt.Sprintf("write remote failed: %v", err)) 79 | } 80 | return nil 81 | } else { 82 | return errors.New(fmt.Sprintf("proxy connection %v->%v does not exists", conn.LocalAddr(), conn.RemoteAddr())) 83 | } 84 | } 85 | 86 | func (h *tcpHandler) DidSend(conn core.Connection, len uint16) { 87 | } 88 | 89 | func (h *tcpHandler) DidClose(conn core.Connection) { 90 | h.Close(conn) 91 | } 92 | 93 | func (h *tcpHandler) LocalDidClose(conn core.Connection) { 94 | h.Close(conn) 95 | } 96 | 97 | func (h *tcpHandler) Close(conn core.Connection) { 98 | if c, found := h.getConn(conn); found { 99 | c.Close() 100 | h.Lock() 101 | delete(h.conns, conn) 102 | h.Unlock() 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /proxy/dns_cache.go: -------------------------------------------------------------------------------- 1 | // This file is copied from https://github.com/yinghuocho/gotun2socks/blob/master/udp.go 2 | 3 | package proxy 4 | 5 | import ( 6 | "log" 7 | "sync" 8 | "time" 9 | 10 | "github.com/miekg/dns" 11 | ) 12 | 13 | const COMMON_DNS_PORT = 53 14 | 15 | type dnsCacheEntry struct { 16 | msg *dns.Msg 17 | exp time.Time 18 | } 19 | 20 | type DNSCache struct { 21 | servers []string 22 | mutex sync.Mutex 23 | storage map[string]*dnsCacheEntry 24 | } 25 | 26 | func NewDNSCache() *DNSCache { 27 | cache := &DNSCache{storage: make(map[string]*dnsCacheEntry)} 28 | go cache.cleanUp() 29 | return cache 30 | } 31 | 32 | func packUint16(i uint16) []byte { return []byte{byte(i >> 8), byte(i)} } 33 | 34 | func cacheKey(q dns.Question) string { 35 | return string(append([]byte(q.Name), packUint16(q.Qtype)...)) 36 | } 37 | 38 | func (c *DNSCache) cleanUp() { 39 | ticker := time.NewTicker(5 * time.Minute) 40 | defer ticker.Stop() 41 | for { 42 | select { 43 | case <-ticker.C: 44 | c.mutex.Lock() 45 | log.Printf("cleaning up dns cache, %v entries", len(c.storage)) 46 | newStorage := make(map[string]*dnsCacheEntry) 47 | for key, entry := range c.storage { 48 | if time.Now().Before(entry.exp) { 49 | newStorage[key] = entry 50 | } 51 | } 52 | c.storage = newStorage 53 | log.Printf("cleanup done, remaining %v entries", len(c.storage)) 54 | c.mutex.Unlock() 55 | } 56 | } 57 | } 58 | 59 | func (c *DNSCache) Query(payload []byte) *dns.Msg { 60 | request := new(dns.Msg) 61 | e := request.Unpack(payload) 62 | if e != nil { 63 | return nil 64 | } 65 | if len(request.Question) == 0 { 66 | return nil 67 | } 68 | 69 | c.mutex.Lock() 70 | defer c.mutex.Unlock() 71 | key := cacheKey(request.Question[0]) 72 | entry := c.storage[key] 73 | if entry == nil { 74 | return nil 75 | } 76 | if time.Now().After(entry.exp) { 77 | delete(c.storage, key) 78 | return nil 79 | } 80 | entry.msg.Id = request.Id 81 | // log.Printf("got dns answer with key: %v", key) 82 | return entry.msg 83 | } 84 | 85 | func (c *DNSCache) Store(payload []byte) { 86 | resp := new(dns.Msg) 87 | e := resp.Unpack(payload) 88 | if e != nil { 89 | return 90 | } 91 | if resp.Rcode != dns.RcodeSuccess { 92 | return 93 | } 94 | if len(resp.Question) == 0 || len(resp.Answer) == 0 { 95 | return 96 | } 97 | 98 | c.mutex.Lock() 99 | defer c.mutex.Unlock() 100 | key := cacheKey(resp.Question[0]) 101 | c.storage[key] = &dnsCacheEntry{ 102 | msg: resp, 103 | exp: time.Now().Add(time.Duration(resp.Answer[0].Header().Ttl) * time.Second), 104 | } 105 | log.Printf("stored dns answer with key: %v, ttl: %v sec", key, resp.Answer[0].Header().Ttl) 106 | } 107 | -------------------------------------------------------------------------------- /core/src/include/lwip/prot/mld6.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * MLD6 protocol definitions 4 | */ 5 | 6 | /* 7 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, 14 | * this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 3. The name of the author may not be used to endorse or promote products 19 | * derived from this software without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 | * OF SUCH DAMAGE. 31 | * 32 | * This file is part of the lwIP TCP/IP stack. 33 | * 34 | * Author: Adam Dunkels 35 | * 36 | */ 37 | #ifndef LWIP_HDR_PROT_MLD6_H 38 | #define LWIP_HDR_PROT_MLD6_H 39 | 40 | #include "lwip/arch.h" 41 | #include "lwip/prot/ip6.h" 42 | 43 | #ifdef __cplusplus 44 | extern "C" { 45 | #endif 46 | 47 | #define MLD6_HBH_HLEN 8 48 | /** Multicast listener report/query/done message header. */ 49 | #ifdef PACK_STRUCT_USE_INCLUDES 50 | # include "arch/bpstruct.h" 51 | #endif 52 | PACK_STRUCT_BEGIN 53 | struct mld_header { 54 | PACK_STRUCT_FLD_8(u8_t type); 55 | PACK_STRUCT_FLD_8(u8_t code); 56 | PACK_STRUCT_FIELD(u16_t chksum); 57 | PACK_STRUCT_FIELD(u16_t max_resp_delay); 58 | PACK_STRUCT_FIELD(u16_t reserved); 59 | PACK_STRUCT_FLD_S(ip6_addr_p_t multicast_address); 60 | /* Options follow. */ 61 | } PACK_STRUCT_STRUCT; 62 | PACK_STRUCT_END 63 | #ifdef PACK_STRUCT_USE_INCLUDES 64 | # include "arch/epstruct.h" 65 | #endif 66 | 67 | #ifdef __cplusplus 68 | } 69 | #endif 70 | 71 | #endif /* LWIP_HDR_PROT_MLD6_H */ 72 | -------------------------------------------------------------------------------- /core/src/include/lwip/apps/netbiosns_opts.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * NETBIOS name service responder options 4 | */ 5 | 6 | /* 7 | * Redistribution and use in source and binary forms, with or without modification, 8 | * are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. The name of the author may not be used to endorse or promote products 16 | * derived from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 20 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 21 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 23 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 26 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 27 | * OF SUCH DAMAGE. 28 | * 29 | * This file is part of the lwIP TCP/IP stack. 30 | * 31 | */ 32 | #ifndef LWIP_HDR_APPS_NETBIOS_OPTS_H 33 | #define LWIP_HDR_APPS_NETBIOS_OPTS_H 34 | 35 | #include "lwip/opt.h" 36 | 37 | /** 38 | * @defgroup netbiosns_opts Options 39 | * @ingroup netbiosns 40 | * @{ 41 | */ 42 | 43 | /** NetBIOS name of lwip device 44 | * This must be uppercase until NETBIOS_STRCMP() is defined to a string 45 | * comparision function that is case insensitive. 46 | * If you want to use the netif's hostname, use this (with LWIP_NETIF_HOSTNAME): 47 | * (ip_current_netif() != NULL ? ip_current_netif()->hostname != NULL ? ip_current_netif()->hostname : "" : "") 48 | * 49 | * If this is not defined, netbiosns_set_name() can be called at runtime to change the name. 50 | */ 51 | #ifdef __DOXYGEN__ 52 | #define NETBIOS_LWIP_NAME "NETBIOSLWIPDEV" 53 | #endif 54 | 55 | /** Respond to NetBIOS name queries 56 | * Default is disabled 57 | */ 58 | #if !defined LWIP_NETBIOS_RESPOND_NAME_QUERY || defined __DOXYGEN__ 59 | #define LWIP_NETBIOS_RESPOND_NAME_QUERY 0 60 | #endif 61 | 62 | /** 63 | * @} 64 | */ 65 | 66 | #endif /* LWIP_HDR_APPS_NETBIOS_OPTS_H */ 67 | -------------------------------------------------------------------------------- /core/src/include/lwip/if_api.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * Interface Identification APIs from: 4 | * RFC 3493: Basic Socket Interface Extensions for IPv6 5 | * Section 4: Interface Identification 6 | */ 7 | 8 | /* 9 | * Copyright (c) 2017 Joel Cunningham, Garmin International, Inc. 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * 1. Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 3. The name of the author may not be used to endorse or promote products 21 | * derived from this software without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 24 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 26 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 27 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 28 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 32 | * OF SUCH DAMAGE. 33 | * 34 | * This file is part of the lwIP TCP/IP stack. 35 | * 36 | * Author: Joel Cunningham 37 | * 38 | */ 39 | #ifndef LWIP_HDR_IF_H 40 | #define LWIP_HDR_IF_H 41 | 42 | #include "lwip/opt.h" 43 | 44 | #if LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */ 45 | 46 | #include "lwip/netif.h" 47 | 48 | #ifdef __cplusplus 49 | extern "C" { 50 | #endif 51 | 52 | #define IF_NAMESIZE NETIF_NAMESIZE 53 | 54 | char * lwip_if_indextoname(unsigned int ifindex, char *ifname); 55 | unsigned int lwip_if_nametoindex(const char *ifname); 56 | 57 | #if LWIP_COMPAT_SOCKETS 58 | #define if_indextoname(ifindex, ifname) lwip_if_indextoname(ifindex,ifname) 59 | #define if_nametoindex(ifname) lwip_if_nametoindex(ifname) 60 | #endif /* LWIP_COMPAT_SOCKETS */ 61 | 62 | #ifdef __cplusplus 63 | } 64 | #endif 65 | 66 | #endif /* LWIP_SOCKET */ 67 | 68 | #endif /* LWIP_HDR_IF_H */ 69 | -------------------------------------------------------------------------------- /core/udp_conn.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./src/include 5 | #include "lwip/udp.h" 6 | */ 7 | import "C" 8 | import ( 9 | "errors" 10 | "fmt" 11 | "net" 12 | "unsafe" 13 | ) 14 | 15 | type udpConn struct { 16 | pcb *C.struct_udp_pcb 17 | handler ConnectionHandler 18 | localAddr net.Addr 19 | remoteAddr net.Addr 20 | localIP C.ip_addr_t 21 | remoteIP C.ip_addr_t 22 | remotePort C.u16_t 23 | localPort C.u16_t 24 | closed bool 25 | } 26 | 27 | func NewUDPConnection(pcb *C.struct_udp_pcb, handler ConnectionHandler, localIP, remoteIP C.ip_addr_t, localPort, remotePort C.u16_t) (Connection, error) { 28 | conn := &udpConn{ 29 | handler: handler, 30 | pcb: pcb, 31 | localAddr: ParseUDPAddr(IPAddrNTOA(localIP), uint16(localPort)), 32 | remoteAddr: ParseUDPAddr(IPAddrNTOA(remoteIP), uint16(remotePort)), 33 | localIP: localIP, 34 | remoteIP: remoteIP, 35 | localPort: localPort, 36 | remotePort: remotePort, 37 | closed: false, 38 | } 39 | err := handler.Connect(conn, conn.RemoteAddr()) 40 | if err != nil { 41 | return nil, err 42 | } 43 | return conn, nil 44 | } 45 | 46 | func (conn *udpConn) RemoteAddr() net.Addr { 47 | return conn.remoteAddr 48 | } 49 | 50 | func (conn *udpConn) LocalAddr() net.Addr { 51 | return conn.localAddr 52 | } 53 | 54 | func (conn *udpConn) Receive(data []byte) error { 55 | if conn.closed { 56 | return errors.New("connection closed") 57 | } 58 | err := conn.handler.DidReceive(conn, data) 59 | if err != nil { 60 | return errors.New(fmt.Sprintf("write proxy failed: %v", err)) 61 | } 62 | return nil 63 | } 64 | 65 | func (conn *udpConn) Write(data []byte) (int, error) { 66 | if conn.closed { 67 | return 0, errors.New("connection closed") 68 | } 69 | if conn.pcb == nil { 70 | return 0, errors.New("nil udp pcb") 71 | } 72 | buf := C.pbuf_alloc_reference(unsafe.Pointer(&data[0]), C.u16_t(len(data)), C.PBUF_ROM) 73 | C.udp_sendto(conn.pcb, buf, &conn.localIP, conn.localPort, &conn.remoteIP, conn.remotePort) 74 | C.pbuf_free(buf) 75 | return len(data), nil 76 | } 77 | 78 | func (conn *udpConn) Sent(len uint16) error { 79 | // unused 80 | return nil 81 | } 82 | 83 | func (conn *udpConn) Close() error { 84 | connId := udpConnId{ 85 | src: conn.LocalAddr().String(), 86 | dst: conn.RemoteAddr().String(), 87 | } 88 | conn.closed = true 89 | udpConns.Delete(connId) 90 | return nil 91 | } 92 | 93 | func (conn *udpConn) Err(err error) { 94 | // unused 95 | } 96 | 97 | func (conn *udpConn) Abort() { 98 | // unused 99 | } 100 | 101 | func (conn *udpConn) LocalDidClose() error { 102 | // unused 103 | return nil 104 | } 105 | 106 | func (conn *udpConn) Poll() error { 107 | // unused 108 | return nil 109 | } 110 | -------------------------------------------------------------------------------- /core/src/include/lwip/priv/raw_priv.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * raw API internal implementations (do not use in application code) 4 | */ 5 | 6 | /* 7 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, 14 | * this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 3. The name of the author may not be used to endorse or promote products 19 | * derived from this software without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 | * OF SUCH DAMAGE. 31 | * 32 | * This file is part of the lwIP TCP/IP stack. 33 | * 34 | * Author: Adam Dunkels 35 | * 36 | */ 37 | #ifndef LWIP_HDR_RAW_PRIV_H 38 | #define LWIP_HDR_RAW_PRIV_H 39 | 40 | #include "lwip/opt.h" 41 | 42 | #if LWIP_RAW /* don't build if not configured for use in lwipopts.h */ 43 | 44 | #include "lwip/raw.h" 45 | 46 | #ifdef __cplusplus 47 | extern "C" { 48 | #endif 49 | 50 | /** return codes for raw_input */ 51 | typedef enum raw_input_state 52 | { 53 | RAW_INPUT_NONE = 0, /* pbuf did not match any pcbs */ 54 | RAW_INPUT_EATEN, /* pbuf handed off and delivered to pcb */ 55 | RAW_INPUT_DELIVERED /* pbuf only delivered to pcb (pbuf can still be referenced) */ 56 | } raw_input_state_t; 57 | 58 | /* The following functions are the lower layer interface to RAW. */ 59 | raw_input_state_t raw_input(struct pbuf *p, struct netif *inp); 60 | 61 | void raw_netif_ip_addr_changed(const ip_addr_t* old_addr, const ip_addr_t* new_addr); 62 | 63 | #ifdef __cplusplus 64 | } 65 | #endif 66 | 67 | #endif /* LWIP_RAW */ 68 | 69 | #endif /* LWIP_HDR_RAW_PRIV_H */ 70 | -------------------------------------------------------------------------------- /core/src/include/lwip/mem.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * Heap API 4 | */ 5 | 6 | /* 7 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, 14 | * this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 3. The name of the author may not be used to endorse or promote products 19 | * derived from this software without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 | * OF SUCH DAMAGE. 31 | * 32 | * This file is part of the lwIP TCP/IP stack. 33 | * 34 | * Author: Adam Dunkels 35 | * 36 | */ 37 | #ifndef LWIP_HDR_MEM_H 38 | #define LWIP_HDR_MEM_H 39 | 40 | #include "lwip/opt.h" 41 | 42 | #ifdef __cplusplus 43 | extern "C" { 44 | #endif 45 | 46 | #if MEM_LIBC_MALLOC 47 | 48 | #include "lwip/arch.h" 49 | 50 | typedef size_t mem_size_t; 51 | #define MEM_SIZE_F SZT_F 52 | 53 | #elif MEM_USE_POOLS 54 | 55 | typedef u16_t mem_size_t; 56 | #define MEM_SIZE_F U16_F 57 | 58 | #else 59 | 60 | /* MEM_SIZE would have to be aligned, but using 64000 here instead of 61 | * 65535 leaves some room for alignment... 62 | */ 63 | #if MEM_SIZE > 64000L 64 | typedef u32_t mem_size_t; 65 | #define MEM_SIZE_F U32_F 66 | #else 67 | typedef u16_t mem_size_t; 68 | #define MEM_SIZE_F U16_F 69 | #endif /* MEM_SIZE > 64000 */ 70 | #endif 71 | 72 | void mem_init(void); 73 | void *mem_trim(void *mem, mem_size_t size); 74 | void *mem_malloc(mem_size_t size); 75 | void *mem_calloc(mem_size_t count, mem_size_t size); 76 | void mem_free(void *mem); 77 | 78 | #ifdef __cplusplus 79 | } 80 | #endif 81 | 82 | #endif /* LWIP_HDR_MEM_H */ 83 | -------------------------------------------------------------------------------- /core/src/include/lwip/icmp6.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * 4 | * IPv6 version of ICMP, as per RFC 4443. 5 | */ 6 | 7 | /* 8 | * Copyright (c) 2010 Inico Technologies Ltd. 9 | * All rights reserved. 10 | * 11 | * Redistribution and use in source and binary forms, with or without modification, 12 | * are permitted provided that the following conditions are met: 13 | * 14 | * 1. Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, 17 | * this list of conditions and the following disclaimer in the documentation 18 | * and/or other materials provided with the distribution. 19 | * 3. The name of the author may not be used to endorse or promote products 20 | * derived from this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 25 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 27 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 30 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 31 | * OF SUCH DAMAGE. 32 | * 33 | * This file is part of the lwIP TCP/IP stack. 34 | * 35 | * Author: Ivan Delamer 36 | * 37 | * 38 | * Please coordinate changes and requests with Ivan Delamer 39 | * 40 | */ 41 | #ifndef LWIP_HDR_ICMP6_H 42 | #define LWIP_HDR_ICMP6_H 43 | 44 | #include "lwip/opt.h" 45 | #include "lwip/pbuf.h" 46 | #include "lwip/ip6_addr.h" 47 | #include "lwip/netif.h" 48 | #include "lwip/prot/icmp6.h" 49 | 50 | #ifdef __cplusplus 51 | extern "C" { 52 | #endif 53 | 54 | #if LWIP_ICMP6 && LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */ 55 | 56 | void icmp6_input(struct pbuf *p, struct netif *inp); 57 | void icmp6_dest_unreach(struct pbuf *p, enum icmp6_dur_code c); 58 | void icmp6_packet_too_big(struct pbuf *p, u32_t mtu); 59 | void icmp6_time_exceeded(struct pbuf *p, enum icmp6_te_code c); 60 | void icmp6_time_exceeded_with_addrs(struct pbuf *p, enum icmp6_te_code c, 61 | const ip6_addr_t *src_addr, const ip6_addr_t *dest_addr); 62 | void icmp6_param_problem(struct pbuf *p, enum icmp6_pp_code c, const void *pointer); 63 | 64 | #endif /* LWIP_ICMP6 && LWIP_IPV6 */ 65 | 66 | 67 | #ifdef __cplusplus 68 | } 69 | #endif 70 | 71 | 72 | #endif /* LWIP_HDR_ICMP6_H */ 73 | -------------------------------------------------------------------------------- /core/src/include/lwip/apps/altcp_tls_mbedtls_opts.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * Application layered TCP/TLS connection API (to be used from TCPIP thread) 4 | * 5 | * This file contains options for an mbedtls port of the TLS layer. 6 | */ 7 | 8 | /* 9 | * Copyright (c) 2017 Simon Goldschmidt 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * 1. Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 3. The name of the author may not be used to endorse or promote products 21 | * derived from this software without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 24 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 26 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 27 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 28 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 32 | * OF SUCH DAMAGE. 33 | * 34 | * This file is part of the lwIP TCP/IP stack. 35 | * 36 | * Author: Simon Goldschmidt 37 | * 38 | */ 39 | #ifndef LWIP_HDR_ALTCP_TLS_OPTS_H 40 | #define LWIP_HDR_ALTCP_TLS_OPTS_H 41 | 42 | #include "lwip/opt.h" 43 | 44 | #if LWIP_ALTCP /* don't build if not configured for use in lwipopts.h */ 45 | 46 | /** LWIP_ALTCP_TLS_MBEDTLS==1: use mbedTLS for TLS support for altcp API 47 | * mbedtls include directory must be reachable via include search path 48 | */ 49 | #ifndef LWIP_ALTCP_TLS_MBEDTLS 50 | #define LWIP_ALTCP_TLS_MBEDTLS 0 51 | #endif 52 | 53 | /** Configure debug level of this file */ 54 | #ifndef ALTCP_MBEDTLS_DEBUG 55 | #define ALTCP_MBEDTLS_DEBUG LWIP_DBG_OFF 56 | #endif 57 | 58 | /** Set a session timeout in seconds for the basic session cache 59 | * ATTENTION: Using a session cache can lower security by reusing keys! 60 | */ 61 | #ifndef ALTCP_MBEDTLS_SESSION_CACHE_TIMEOUT_SECONDS 62 | #define ALTCP_MBEDTLS_SESSION_CACHE_TIMEOUT_SECONDS 0 63 | #endif 64 | 65 | #endif /* LWIP_ALTCP */ 66 | 67 | #endif /* LWIP_HDR_ALTCP_TLS_OPTS_H */ 68 | -------------------------------------------------------------------------------- /core/src/include/lwip/apps/mdns_priv.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * MDNS responder private definitions 4 | */ 5 | 6 | /* 7 | * Copyright (c) 2015 Verisure Innovation AB 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, 14 | * this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 3. The name of the author may not be used to endorse or promote products 19 | * derived from this software without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 | * OF SUCH DAMAGE. 31 | * 32 | * This file is part of the lwIP TCP/IP stack. 33 | * 34 | * Author: Erik Ekman 35 | * 36 | */ 37 | #ifndef LWIP_HDR_MDNS_PRIV_H 38 | #define LWIP_HDR_MDNS_PRIV_H 39 | 40 | #include "lwip/apps/mdns_opts.h" 41 | #include "lwip/pbuf.h" 42 | 43 | #ifdef __cplusplus 44 | extern "C" { 45 | #endif 46 | 47 | #if LWIP_MDNS_RESPONDER 48 | 49 | /* Domain struct and methods - visible for unit tests */ 50 | 51 | #define MDNS_DOMAIN_MAXLEN 256 52 | #define MDNS_READNAME_ERROR 0xFFFF 53 | 54 | struct mdns_domain { 55 | /* Encoded domain name */ 56 | u8_t name[MDNS_DOMAIN_MAXLEN]; 57 | /* Total length of domain name, including zero */ 58 | u16_t length; 59 | /* Set if compression of this domain is not allowed */ 60 | u8_t skip_compression; 61 | }; 62 | 63 | err_t mdns_domain_add_label(struct mdns_domain *domain, const char *label, u8_t len); 64 | u16_t mdns_readname(struct pbuf *p, u16_t offset, struct mdns_domain *domain); 65 | int mdns_domain_eq(struct mdns_domain *a, struct mdns_domain *b); 66 | u16_t mdns_compress_domain(struct pbuf *pbuf, u16_t *offset, struct mdns_domain *domain); 67 | 68 | #endif /* LWIP_MDNS_RESPONDER */ 69 | 70 | #ifdef __cplusplus 71 | } 72 | #endif 73 | 74 | #endif /* LWIP_HDR_MDNS_PRIV_H */ 75 | -------------------------------------------------------------------------------- /core/src/include/lwip/altcp_tcp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * Application layered TCP connection API (to be used from TCPIP thread)\n 4 | * This interface mimics the tcp callback API to the application while preventing 5 | * direct linking (much like virtual functions). 6 | * This way, an application can make use of other application layer protocols 7 | * on top of TCP without knowing the details (e.g. TLS, proxy connection). 8 | * 9 | * This file contains the base implementation calling into tcp. 10 | */ 11 | 12 | /* 13 | * Copyright (c) 2017 Simon Goldschmidt 14 | * All rights reserved. 15 | * 16 | * Redistribution and use in source and binary forms, with or without modification, 17 | * are permitted provided that the following conditions are met: 18 | * 19 | * 1. Redistributions of source code must retain the above copyright notice, 20 | * this list of conditions and the following disclaimer. 21 | * 2. Redistributions in binary form must reproduce the above copyright notice, 22 | * this list of conditions and the following disclaimer in the documentation 23 | * and/or other materials provided with the distribution. 24 | * 3. The name of the author may not be used to endorse or promote products 25 | * derived from this software without specific prior written permission. 26 | * 27 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 28 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 29 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 30 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 31 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 32 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 35 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 36 | * OF SUCH DAMAGE. 37 | * 38 | * This file is part of the lwIP TCP/IP stack. 39 | * 40 | * Author: Simon Goldschmidt 41 | * 42 | */ 43 | #ifndef LWIP_HDR_ALTCP_TCP_H 44 | #define LWIP_HDR_ALTCP_TCP_H 45 | 46 | #include "lwip/opt.h" 47 | 48 | #if LWIP_ALTCP /* don't build if not configured for use in lwipopts.h */ 49 | 50 | #include "lwip/altcp.h" 51 | 52 | #ifdef __cplusplus 53 | extern "C" { 54 | #endif 55 | 56 | struct altcp_pcb *altcp_tcp_new_ip_type(u8_t ip_type); 57 | 58 | #define altcp_tcp_new() altcp_tcp_new_ip_type(IPADDR_TYPE_V4) 59 | #define altcp_tcp_new_ip6() altcp_tcp_new_ip_type(IPADDR_TYPE_V6) 60 | 61 | struct altcp_pcb *altcp_tcp_alloc(void *arg, u8_t ip_type); 62 | 63 | struct tcp_pcb; 64 | struct altcp_pcb *altcp_tcp_wrap(struct tcp_pcb *tpcb); 65 | 66 | #ifdef __cplusplus 67 | } 68 | #endif 69 | 70 | #endif /* LWIP_ALTCP */ 71 | 72 | #endif /* LWIP_HDR_ALTCP_TCP_H */ 73 | -------------------------------------------------------------------------------- /proxy/direct/udp.go: -------------------------------------------------------------------------------- 1 | package direct 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "log" 7 | "net" 8 | "sync" 9 | "time" 10 | 11 | "github.com/eycorsican/go-tun2socks/core" 12 | ) 13 | 14 | type udpHandler struct { 15 | sync.Mutex 16 | 17 | udpConns map[core.Connection]*net.UDPConn 18 | udpTargetAddrs map[core.Connection]*net.UDPAddr 19 | } 20 | 21 | func NewUDPHandler() core.ConnectionHandler { 22 | return &udpHandler{ 23 | udpConns: make(map[core.Connection]*net.UDPConn, 8), 24 | udpTargetAddrs: make(map[core.Connection]*net.UDPAddr, 8), 25 | } 26 | } 27 | 28 | func (h *udpHandler) fetchUDPInput(conn core.Connection, pc *net.UDPConn) { 29 | buf := core.NewBytes(core.BufSize) 30 | 31 | defer func() { 32 | h.Close(conn) 33 | core.FreeBytes(buf) 34 | }() 35 | 36 | for { 37 | pc.SetDeadline(time.Now().Add(16 * time.Second)) 38 | n, _, err := pc.ReadFromUDP(buf) 39 | if err != nil { 40 | // log.Printf("failed to read UDP data from remote: %v", err) 41 | return 42 | } 43 | 44 | _, err = conn.Write(buf[:n]) 45 | if err != nil { 46 | log.Printf("failed to write UDP data to TUN") 47 | return 48 | } 49 | } 50 | } 51 | 52 | func (h *udpHandler) Connect(conn core.Connection, target net.Addr) error { 53 | bindAddr := &net.UDPAddr{IP: nil, Port: 0} 54 | pc, err := net.ListenUDP("udp", bindAddr) 55 | if err != nil { 56 | log.Printf("failed to bind udp address") 57 | return err 58 | } 59 | tgtAddr, _ := net.ResolveUDPAddr(target.Network(), target.String()) 60 | h.Lock() 61 | h.udpTargetAddrs[conn] = tgtAddr 62 | h.udpConns[conn] = pc 63 | h.Unlock() 64 | go h.fetchUDPInput(conn, pc) 65 | log.Printf("new proxy connection for target: %s:%s", target.Network(), target.String()) 66 | return nil 67 | } 68 | 69 | func (h *udpHandler) DidReceive(conn core.Connection, data []byte) error { 70 | h.Lock() 71 | pc, ok1 := h.udpConns[conn] 72 | addr, ok2 := h.udpTargetAddrs[conn] 73 | h.Unlock() 74 | 75 | if ok1 && ok2 { 76 | _, err := pc.WriteToUDP(data, addr) 77 | if err != nil { 78 | log.Printf("failed to write UDP payload to SOCKS5 server: %v", err) 79 | return errors.New("failed to write UDP data") 80 | } 81 | return nil 82 | } else { 83 | return errors.New(fmt.Sprintf("proxy connection %v->%v does not exists", conn.LocalAddr(), conn.RemoteAddr())) 84 | } 85 | } 86 | 87 | func (h *udpHandler) DidSend(conn core.Connection, len uint16) { 88 | // unused 89 | } 90 | 91 | func (h *udpHandler) DidClose(conn core.Connection) { 92 | // unused 93 | } 94 | 95 | func (h *udpHandler) LocalDidClose(conn core.Connection) { 96 | // unused 97 | } 98 | 99 | func (h *udpHandler) Close(conn core.Connection) { 100 | conn.Close() 101 | 102 | h.Lock() 103 | defer h.Unlock() 104 | 105 | if _, ok := h.udpTargetAddrs[conn]; ok { 106 | delete(h.udpTargetAddrs, conn) 107 | } 108 | if pc, ok := h.udpConns[conn]; ok { 109 | pc.Close() 110 | delete(h.udpConns, conn) 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /core/src/include/lwip/apps/mdns_opts.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * MDNS responder 4 | */ 5 | 6 | /* 7 | * Copyright (c) 2015 Verisure Innovation AB 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, 14 | * this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 3. The name of the author may not be used to endorse or promote products 19 | * derived from this software without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 | * OF SUCH DAMAGE. 31 | * 32 | * This file is part of the lwIP TCP/IP stack. 33 | * 34 | * Author: Erik Ekman 35 | * 36 | */ 37 | 38 | #ifndef LWIP_HDR_APPS_MDNS_OPTS_H 39 | #define LWIP_HDR_APPS_MDNS_OPTS_H 40 | 41 | #include "lwip/opt.h" 42 | 43 | /** 44 | * @defgroup mdns_opts Options 45 | * @ingroup mdns 46 | * @{ 47 | */ 48 | 49 | /** 50 | * LWIP_MDNS_RESPONDER==1: Turn on multicast DNS module. UDP must be available for MDNS 51 | * transport. IGMP is needed for IPv4 multicast. 52 | */ 53 | #ifndef LWIP_MDNS_RESPONDER 54 | #define LWIP_MDNS_RESPONDER 0 55 | #endif /* LWIP_MDNS_RESPONDER */ 56 | 57 | /** The maximum number of services per netif */ 58 | #ifndef MDNS_MAX_SERVICES 59 | #define MDNS_MAX_SERVICES 1 60 | #endif 61 | 62 | /** MDNS_RESP_USENETIF_EXTCALLBACK==1: register an ext_callback on the netif 63 | * to automatically restart probing/announcing on status or address change. 64 | */ 65 | #ifndef MDNS_RESP_USENETIF_EXTCALLBACK 66 | #define MDNS_RESP_USENETIF_EXTCALLBACK LWIP_NETIF_EXT_STATUS_CALLBACK 67 | #endif 68 | 69 | /** 70 | * MDNS_DEBUG: Enable debugging for multicast DNS. 71 | */ 72 | #ifndef MDNS_DEBUG 73 | #define MDNS_DEBUG LWIP_DBG_OFF 74 | #endif 75 | 76 | /** 77 | * @} 78 | */ 79 | 80 | #endif /* LWIP_HDR_APPS_MDNS_OPTS_H */ 81 | 82 | -------------------------------------------------------------------------------- /core/src/include/lwip/tcpbase.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * Base TCP API definitions shared by TCP and ALTCP\n 4 | * See also @ref tcp_raw 5 | */ 6 | 7 | /* 8 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 9 | * All rights reserved. 10 | * 11 | * Redistribution and use in source and binary forms, with or without modification, 12 | * are permitted provided that the following conditions are met: 13 | * 14 | * 1. Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, 17 | * this list of conditions and the following disclaimer in the documentation 18 | * and/or other materials provided with the distribution. 19 | * 3. The name of the author may not be used to endorse or promote products 20 | * derived from this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 25 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 27 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 30 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 31 | * OF SUCH DAMAGE. 32 | * 33 | * This file is part of the lwIP TCP/IP stack. 34 | * 35 | * Author: Adam Dunkels 36 | * 37 | */ 38 | #ifndef LWIP_HDR_TCPBASE_H 39 | #define LWIP_HDR_TCPBASE_H 40 | 41 | #include "lwip/opt.h" 42 | 43 | #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */ 44 | 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | 50 | #if LWIP_WND_SCALE 51 | typedef u32_t tcpwnd_size_t; 52 | #else 53 | typedef u16_t tcpwnd_size_t; 54 | #endif 55 | 56 | enum tcp_state { 57 | CLOSED = 0, 58 | LISTEN = 1, 59 | SYN_SENT = 2, 60 | SYN_RCVD = 3, 61 | ESTABLISHED = 4, 62 | FIN_WAIT_1 = 5, 63 | FIN_WAIT_2 = 6, 64 | CLOSE_WAIT = 7, 65 | CLOSING = 8, 66 | LAST_ACK = 9, 67 | TIME_WAIT = 10 68 | }; 69 | /* ATTENTION: this depends on state number ordering! */ 70 | #define TCP_STATE_IS_CLOSING(state) ((state) >= FIN_WAIT_1) 71 | 72 | /* Flags for "apiflags" parameter in tcp_write */ 73 | #define TCP_WRITE_FLAG_COPY 0x01 74 | #define TCP_WRITE_FLAG_MORE 0x02 75 | 76 | #define TCP_PRIO_MIN 1 77 | #define TCP_PRIO_NORMAL 64 78 | #define TCP_PRIO_MAX 127 79 | 80 | const char* tcp_debug_state_str(enum tcp_state s); 81 | 82 | #ifdef __cplusplus 83 | } 84 | #endif 85 | 86 | #endif /* LWIP_TCP */ 87 | 88 | #endif /* LWIP_HDR_TCPBASE_H */ 89 | -------------------------------------------------------------------------------- /core/src/include/lwip/apps/sntp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * SNTP client API 4 | */ 5 | 6 | /* 7 | * Copyright (c) 2007-2009 Frédéric Bernon, Simon Goldschmidt 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, 14 | * this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 3. The name of the author may not be used to endorse or promote products 19 | * derived from this software without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 | * OF SUCH DAMAGE. 31 | * 32 | * This file is part of the lwIP TCP/IP stack. 33 | * 34 | * Author: Frédéric Bernon, Simon Goldschmidt 35 | * 36 | */ 37 | #ifndef LWIP_HDR_APPS_SNTP_H 38 | #define LWIP_HDR_APPS_SNTP_H 39 | 40 | #include "lwip/apps/sntp_opts.h" 41 | #include "lwip/ip_addr.h" 42 | 43 | #ifdef __cplusplus 44 | extern "C" { 45 | #endif 46 | 47 | /* SNTP operating modes: default is to poll using unicast. 48 | The mode has to be set before calling sntp_init(). */ 49 | #define SNTP_OPMODE_POLL 0 50 | #define SNTP_OPMODE_LISTENONLY 1 51 | void sntp_setoperatingmode(u8_t operating_mode); 52 | u8_t sntp_getoperatingmode(void); 53 | 54 | void sntp_init(void); 55 | void sntp_stop(void); 56 | u8_t sntp_enabled(void); 57 | 58 | void sntp_setserver(u8_t idx, const ip_addr_t *addr); 59 | const ip_addr_t* sntp_getserver(u8_t idx); 60 | 61 | #if SNTP_MONITOR_SERVER_REACHABILITY 62 | u8_t sntp_getreachability(u8_t idx); 63 | #endif /* SNTP_MONITOR_SERVER_REACHABILITY */ 64 | 65 | #if SNTP_SERVER_DNS 66 | void sntp_setservername(u8_t idx, const char *server); 67 | const char *sntp_getservername(u8_t idx); 68 | #endif /* SNTP_SERVER_DNS */ 69 | 70 | #if SNTP_GET_SERVERS_FROM_DHCP 71 | void sntp_servermode_dhcp(int set_servers_from_dhcp); 72 | #else /* SNTP_GET_SERVERS_FROM_DHCP */ 73 | #define sntp_servermode_dhcp(x) 74 | #endif /* SNTP_GET_SERVERS_FROM_DHCP */ 75 | 76 | #ifdef __cplusplus 77 | } 78 | #endif 79 | 80 | #endif /* LWIP_HDR_APPS_SNTP_H */ 81 | -------------------------------------------------------------------------------- /core/src/include/lwip/apps/altcp_proxyconnect.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * Application layered TCP connection API that executes a proxy-connect. 4 | * 5 | * This file provides a starting layer that executes a proxy-connect e.g. to 6 | * set up TLS connections through a http proxy. 7 | */ 8 | 9 | /* 10 | * Copyright (c) 2018 Simon Goldschmidt 11 | * All rights reserved. 12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 16 | * 1. Redistributions of source code must retain the above copyright notice, 17 | * this list of conditions and the following disclaimer. 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, 19 | * this list of conditions and the following disclaimer in the documentation 20 | * and/or other materials provided with the distribution. 21 | * 3. The name of the author may not be used to endorse or promote products 22 | * derived from this software without specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 25 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 27 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 29 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 33 | * OF SUCH DAMAGE. 34 | * 35 | * This file is part of the lwIP TCP/IP stack. 36 | * 37 | * Author: Simon Goldschmidt 38 | * 39 | */ 40 | 41 | #ifndef LWIP_HDR_APPS_ALTCP_PROXYCONNECT_H 42 | #define LWIP_HDR_APPS_ALTCP_PROXYCONNECT_H 43 | 44 | #include "lwip/opt.h" 45 | 46 | #if LWIP_ALTCP /* don't build if not configured for use in lwipopts.h */ 47 | 48 | #include "lwip/ip_addr.h" 49 | 50 | #ifdef __cplusplus 51 | extern "C" { 52 | #endif 53 | 54 | struct altcp_proxyconnect_config { 55 | ip_addr_t proxy_addr; 56 | u16_t proxy_port; 57 | }; 58 | 59 | 60 | struct altcp_pcb *altcp_proxyconnect_new(struct altcp_proxyconnect_config *config, struct altcp_pcb *inner_pcb); 61 | struct altcp_pcb *altcp_proxyconnect_new_tcp(struct altcp_proxyconnect_config *config, u8_t ip_type); 62 | 63 | struct altcp_pcb *altcp_proxyconnect_alloc(void *arg, u8_t ip_type); 64 | 65 | #if LWIP_ALTCP_TLS 66 | struct altcp_proxyconnect_tls_config { 67 | struct altcp_proxyconnect_config proxy; 68 | struct altcp_tls_config *tls_config; 69 | }; 70 | 71 | struct altcp_pcb *altcp_proxyconnect_tls_alloc(void *arg, u8_t ip_type); 72 | #endif /* LWIP_ALTCP_TLS */ 73 | 74 | #ifdef __cplusplus 75 | } 76 | #endif 77 | 78 | #endif /* LWIP_ALTCP */ 79 | #endif /* LWIP_HDR_APPS_ALTCP_PROXYCONNECT_H */ 80 | -------------------------------------------------------------------------------- /core/src/include/netif/ppp/polarssl/arc4.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file arc4.h 3 | * 4 | * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine 5 | * 6 | * Copyright (C) 2009 Paul Bakker 7 | * 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright 15 | * notice, this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the names of PolarSSL or XySSL nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | #include "netif/ppp/ppp_opts.h" 37 | #if LWIP_INCLUDED_POLARSSL_ARC4 38 | 39 | #ifndef LWIP_INCLUDED_POLARSSL_ARC4_H 40 | #define LWIP_INCLUDED_POLARSSL_ARC4_H 41 | 42 | /** 43 | * \brief ARC4 context structure 44 | */ 45 | typedef struct 46 | { 47 | int x; /*!< permutation index */ 48 | int y; /*!< permutation index */ 49 | unsigned char m[256]; /*!< permutation table */ 50 | } 51 | arc4_context; 52 | 53 | #ifdef __cplusplus 54 | extern "C" { 55 | #endif 56 | 57 | /** 58 | * \brief ARC4 key schedule 59 | * 60 | * \param ctx ARC4 context to be initialized 61 | * \param key the secret key 62 | * \param keylen length of the key 63 | */ 64 | void arc4_setup( arc4_context *ctx, unsigned char *key, int keylen ); 65 | 66 | /** 67 | * \brief ARC4 cipher function 68 | * 69 | * \param ctx ARC4 context 70 | * \param buf buffer to be processed 71 | * \param buflen amount of data in buf 72 | */ 73 | void arc4_crypt( arc4_context *ctx, unsigned char *buf, int buflen ); 74 | 75 | #ifdef __cplusplus 76 | } 77 | #endif 78 | 79 | #endif /* LWIP_INCLUDED_POLARSSL_ARC4_H */ 80 | 81 | #endif /* LWIP_INCLUDED_POLARSSL_ARC4 */ 82 | -------------------------------------------------------------------------------- /core/src/include/netif/zepif.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * 4 | * A netif implementing the ZigBee Eencapsulation Protocol (ZEP). 5 | * This is used to tunnel 6LowPAN over UDP. 6 | */ 7 | 8 | /* 9 | * Copyright (c) 2018 Simon Goldschmidt 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * 1. Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 3. The name of the author may not be used to endorse or promote products 21 | * derived from this software without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 24 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 26 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 27 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 28 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 32 | * OF SUCH DAMAGE. 33 | * 34 | * This file is part of the lwIP TCP/IP stack. 35 | * 36 | * Author: Simon Goldschmidt 37 | * 38 | */ 39 | 40 | #ifndef LWIP_HDR_ZEPIF_H 41 | #define LWIP_HDR_ZEPIF_H 42 | 43 | #include "lwip/opt.h" 44 | #include "netif/lowpan6.h" 45 | 46 | #if LWIP_IPV6 && LWIP_UDP /* don't build if not configured for use in lwipopts.h */ 47 | 48 | #include "lwip/netif.h" 49 | 50 | #ifdef __cplusplus 51 | extern "C" { 52 | #endif 53 | 54 | #define ZEPIF_DEFAULT_UDP_PORT 17754 55 | 56 | /** Pass this struct as 'state' to netif_add to control the behaviour 57 | * of this netif. If NULL is passed, default behaviour is chosen */ 58 | struct zepif_init { 59 | /** The UDP port used to ZEP frames from (0 = default) */ 60 | u16_t zep_src_udp_port; 61 | /** The UDP port used to ZEP frames to (0 = default) */ 62 | u16_t zep_dst_udp_port; 63 | /** The IP address to sed ZEP frames from (NULL = ANY) */ 64 | const ip_addr_t *zep_src_ip_addr; 65 | /** The IP address to sed ZEP frames to (NULL = BROADCAST) */ 66 | const ip_addr_t *zep_dst_ip_addr; 67 | /** If != NULL, the udp pcb is bound to this netif */ 68 | const struct netif *zep_netif; 69 | /** MAC address of the 6LowPAN device */ 70 | u8_t addr[6]; 71 | }; 72 | 73 | err_t zepif_init(struct netif *netif); 74 | 75 | #ifdef __cplusplus 76 | } 77 | #endif 78 | 79 | #endif /* LWIP_IPV6 && LWIP_UDP */ 80 | 81 | #endif /* LWIP_HDR_ZEPIF_H */ 82 | -------------------------------------------------------------------------------- /core/src/include/netif/ethernet.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * Ethernet input function - handles INCOMING ethernet level traffic 4 | * To be used in most low-level netif implementations 5 | */ 6 | 7 | /* 8 | * Copyright (c) 2001-2003 Swedish Institute of Computer Science. 9 | * Copyright (c) 2003-2004 Leon Woestenberg 10 | * Copyright (c) 2003-2004 Axon Digital Design B.V., The Netherlands. 11 | * All rights reserved. 12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 16 | * 1. Redistributions of source code must retain the above copyright notice, 17 | * this list of conditions and the following disclaimer. 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, 19 | * this list of conditions and the following disclaimer in the documentation 20 | * and/or other materials provided with the distribution. 21 | * 3. The name of the author may not be used to endorse or promote products 22 | * derived from this software without specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 25 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 27 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 29 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 33 | * OF SUCH DAMAGE. 34 | * 35 | * This file is part of the lwIP TCP/IP stack. 36 | * 37 | * Author: Adam Dunkels 38 | * 39 | */ 40 | 41 | #ifndef LWIP_HDR_NETIF_ETHERNET_H 42 | #define LWIP_HDR_NETIF_ETHERNET_H 43 | 44 | #include "lwip/opt.h" 45 | 46 | #include "lwip/pbuf.h" 47 | #include "lwip/netif.h" 48 | #include "lwip/prot/ethernet.h" 49 | 50 | #ifdef __cplusplus 51 | extern "C" { 52 | #endif 53 | 54 | #if LWIP_ARP || LWIP_ETHERNET 55 | 56 | /** Define this to 1 and define LWIP_ARP_FILTER_NETIF_FN(pbuf, netif, type) 57 | * to a filter function that returns the correct netif when using multiple 58 | * netifs on one hardware interface where the netif's low-level receive 59 | * routine cannot decide for the correct netif (e.g. when mapping multiple 60 | * IP addresses to one hardware interface). 61 | */ 62 | #ifndef LWIP_ARP_FILTER_NETIF 63 | #define LWIP_ARP_FILTER_NETIF 0 64 | #endif 65 | 66 | err_t ethernet_input(struct pbuf *p, struct netif *netif); 67 | err_t ethernet_output(struct netif* netif, struct pbuf* p, const struct eth_addr* src, const struct eth_addr* dst, u16_t eth_type); 68 | 69 | extern const struct eth_addr ethbroadcast, ethzero; 70 | 71 | #endif /* LWIP_ARP || LWIP_ETHERNET */ 72 | 73 | #ifdef __cplusplus 74 | } 75 | #endif 76 | 77 | #endif /* LWIP_HDR_NETIF_ETHERNET_H */ 78 | -------------------------------------------------------------------------------- /core/src/include/lwip/apps/snmp_mib2.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * SNMP MIB2 API 4 | */ 5 | 6 | /* 7 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, 14 | * this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 3. The name of the author may not be used to endorse or promote products 19 | * derived from this software without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 | * OF SUCH DAMAGE. 31 | * 32 | * This file is part of the lwIP TCP/IP stack. 33 | * 34 | * Author: Dirk Ziegelmeier 35 | * 36 | */ 37 | #ifndef LWIP_HDR_APPS_SNMP_MIB2_H 38 | #define LWIP_HDR_APPS_SNMP_MIB2_H 39 | 40 | #include "lwip/apps/snmp_opts.h" 41 | 42 | #ifdef __cplusplus 43 | extern "C" { 44 | #endif 45 | 46 | #if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */ 47 | #if SNMP_LWIP_MIB2 48 | 49 | #include "lwip/apps/snmp_core.h" 50 | 51 | extern const struct snmp_mib mib2; 52 | 53 | #if SNMP_USE_NETCONN 54 | #include "lwip/apps/snmp_threadsync.h" 55 | void snmp_mib2_lwip_synchronizer(snmp_threadsync_called_fn fn, void* arg); 56 | extern struct snmp_threadsync_instance snmp_mib2_lwip_locks; 57 | #endif 58 | 59 | #ifndef SNMP_SYSSERVICES 60 | #define SNMP_SYSSERVICES ((1 << 6) | (1 << 3) | ((IP_FORWARD) << 2)) 61 | #endif 62 | 63 | void snmp_mib2_set_sysdescr(const u8_t* str, const u16_t* len); /* read-only be defintion */ 64 | void snmp_mib2_set_syscontact(u8_t *ocstr, u16_t *ocstrlen, u16_t bufsize); 65 | void snmp_mib2_set_syscontact_readonly(const u8_t *ocstr, const u16_t *ocstrlen); 66 | void snmp_mib2_set_sysname(u8_t *ocstr, u16_t *ocstrlen, u16_t bufsize); 67 | void snmp_mib2_set_sysname_readonly(const u8_t *ocstr, const u16_t *ocstrlen); 68 | void snmp_mib2_set_syslocation(u8_t *ocstr, u16_t *ocstrlen, u16_t bufsize); 69 | void snmp_mib2_set_syslocation_readonly(const u8_t *ocstr, const u16_t *ocstrlen); 70 | 71 | #endif /* SNMP_LWIP_MIB2 */ 72 | #endif /* LWIP_SNMP */ 73 | 74 | #ifdef __cplusplus 75 | } 76 | #endif 77 | 78 | #endif /* LWIP_HDR_APPS_SNMP_MIB2_H */ 79 | -------------------------------------------------------------------------------- /core/tcp_callback_export.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./src/include 5 | #include "lwip/tcp.h" 6 | */ 7 | import "C" 8 | import ( 9 | "errors" 10 | "fmt" 11 | "unsafe" 12 | ) 13 | 14 | // These exported callback functions must be placed in a seperated file. 15 | // 16 | // See also: 17 | // https://github.com/golang/go/issues/20639 18 | // https://golang.org/cmd/cgo/#hdr-C_references_to_Go 19 | 20 | //export TCPAcceptFn 21 | func TCPAcceptFn(arg unsafe.Pointer, newpcb *C.struct_tcp_pcb, err C.err_t) C.err_t { 22 | if err != C.ERR_OK { 23 | return err 24 | } 25 | _, err2 := NewTCPConnection(newpcb, tcpConnectionHandler) 26 | if err2 != nil { 27 | return C.ERR_CONN 28 | } 29 | return C.ERR_OK 30 | } 31 | 32 | //export TCPRecvFn 33 | func TCPRecvFn(arg unsafe.Pointer, tpcb *C.struct_tcp_pcb, p *C.struct_pbuf, err C.err_t) C.err_t { 34 | if err != C.ERR_OK && err != C.ERR_ABRT { 35 | return err 36 | } 37 | 38 | // Only free the pbuf when returning ERR_OK or ERR_ABRT. 39 | defer func() { 40 | if p != nil { 41 | C.pbuf_free(p) 42 | } 43 | }() 44 | 45 | conn, ok := tcpConns.Load(GetConnKeyVal(arg)) 46 | if !ok { 47 | // The connection does not exists. 48 | C.tcp_abort(tpcb) 49 | return C.ERR_ABRT 50 | } 51 | 52 | if p == nil { 53 | // The connection has been closed. 54 | err := conn.(Connection).LocalDidClose() 55 | if err.(*lwipError).Code == LWIP_ERR_ABRT { 56 | return C.ERR_ABRT 57 | } else if err.(*lwipError).Code == LWIP_ERR_OK { 58 | return C.ERR_OK 59 | } 60 | } 61 | 62 | // create Go slice backed by C array, the slice will not garbage collect by Go runtime 63 | buf := (*[1 << 30]byte)(unsafe.Pointer(p.payload))[:int(p.tot_len):int(p.tot_len)] 64 | handlerErr := conn.(Connection).Receive(buf) 65 | 66 | if handlerErr != nil { 67 | C.tcp_abort(tpcb) 68 | return C.ERR_ABRT 69 | } 70 | 71 | return C.ERR_OK 72 | } 73 | 74 | //export TCPSentFn 75 | func TCPSentFn(arg unsafe.Pointer, tpcb *C.struct_tcp_pcb, len C.u16_t) C.err_t { 76 | if conn, ok := tcpConns.Load(GetConnKeyVal(arg)); ok { 77 | err := conn.(Connection).Sent(uint16(len)) 78 | if err.(*lwipError).Code == LWIP_ERR_ABRT { 79 | return C.ERR_ABRT 80 | } else { 81 | return C.ERR_OK 82 | } 83 | } else { 84 | C.tcp_abort(tpcb) 85 | return C.ERR_ABRT 86 | } 87 | } 88 | 89 | //export TCPErrFn 90 | func TCPErrFn(arg unsafe.Pointer, err C.err_t) { 91 | if conn, ok := tcpConns.Load(GetConnKeyVal(arg)); ok { 92 | switch err { 93 | case C.ERR_ABRT: 94 | // Aborted through tcp_abort or by a TCP timer 95 | conn.(Connection).Err(errors.New("connection aborted")) 96 | case C.ERR_RST: 97 | // The connection was reset by the remote host 98 | conn.(Connection).Err(errors.New("connection reseted")) 99 | default: 100 | conn.(Connection).Err(errors.New(fmt.Sprintf("lwip error code %v", int(err)))) 101 | } 102 | } 103 | } 104 | 105 | //export TCPPollFn 106 | func TCPPollFn(arg unsafe.Pointer, tpcb *C.struct_tcp_pcb) C.err_t { 107 | if conn, ok := tcpConns.Load(GetConnKeyVal(arg)); ok { 108 | err := conn.(Connection).Poll() 109 | if err.(*lwipError).Code == LWIP_ERR_ABRT { 110 | return C.ERR_ABRT 111 | } else if err.(*lwipError).Code == LWIP_ERR_OK { 112 | return C.ERR_OK 113 | } 114 | } 115 | return C.ERR_OK 116 | } 117 | -------------------------------------------------------------------------------- /core/src/include/netif/lowpan6.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * 4 | * 6LowPAN output for IPv6. Uses ND tables for link-layer addressing. Fragments packets to 6LowPAN units. 5 | */ 6 | 7 | /* 8 | * Copyright (c) 2015 Inico Technologies Ltd. 9 | * All rights reserved. 10 | * 11 | * Redistribution and use in source and binary forms, with or without modification, 12 | * are permitted provided that the following conditions are met: 13 | * 14 | * 1. Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, 17 | * this list of conditions and the following disclaimer in the documentation 18 | * and/or other materials provided with the distribution. 19 | * 3. The name of the author may not be used to endorse or promote products 20 | * derived from this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 25 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 27 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 30 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 31 | * OF SUCH DAMAGE. 32 | * 33 | * This file is part of the lwIP TCP/IP stack. 34 | * 35 | * Author: Ivan Delamer 36 | * 37 | * 38 | * Please coordinate changes and requests with Ivan Delamer 39 | * 40 | */ 41 | 42 | #ifndef LWIP_HDR_LOWPAN6_H 43 | #define LWIP_HDR_LOWPAN6_H 44 | 45 | #include "netif/lowpan6_opts.h" 46 | 47 | #if LWIP_IPV6 48 | 49 | #include "netif/lowpan6_common.h" 50 | #include "lwip/pbuf.h" 51 | #include "lwip/ip.h" 52 | #include "lwip/ip_addr.h" 53 | #include "lwip/netif.h" 54 | 55 | #ifdef __cplusplus 56 | extern "C" { 57 | #endif 58 | 59 | /** 1 second period for reassembly */ 60 | #define LOWPAN6_TMR_INTERVAL 1000 61 | 62 | void lowpan6_tmr(void); 63 | 64 | err_t lowpan6_set_context(u8_t idx, const ip6_addr_t * context); 65 | err_t lowpan6_set_short_addr(u8_t addr_high, u8_t addr_low); 66 | 67 | #if LWIP_IPV4 68 | err_t lowpan4_output(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr); 69 | #endif /* LWIP_IPV4 */ 70 | err_t lowpan6_output(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr); 71 | err_t lowpan6_input(struct pbuf * p, struct netif *netif); 72 | err_t lowpan6_if_init(struct netif *netif); 73 | 74 | /* pan_id in network byte order. */ 75 | err_t lowpan6_set_pan_id(u16_t pan_id); 76 | 77 | u16_t lowpan6_calc_crc(const void *buf, u16_t len); 78 | 79 | #if !NO_SYS 80 | err_t tcpip_6lowpan_input(struct pbuf *p, struct netif *inp); 81 | #endif /* !NO_SYS */ 82 | 83 | #ifdef __cplusplus 84 | } 85 | #endif 86 | 87 | #endif /* LWIP_IPV6 */ 88 | 89 | #endif /* LWIP_HDR_LOWPAN6_H */ 90 | -------------------------------------------------------------------------------- /core/src/include/lwip/prot/iana.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * IANA assigned numbers (RFC 1700 and successors) 4 | * 5 | * @defgroup iana IANA assigned numbers 6 | * @ingroup infrastructure 7 | */ 8 | 9 | /* 10 | * Copyright (c) 2017 Dirk Ziegelmeier. 11 | * All rights reserved. 12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 16 | * 1. Redistributions of source code must retain the above copyright notice, 17 | * this list of conditions and the following disclaimer. 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, 19 | * this list of conditions and the following disclaimer in the documentation 20 | * and/or other materials provided with the distribution. 21 | * 3. The name of the author may not be used to endorse or promote products 22 | * derived from this software without specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 25 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 27 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 29 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 33 | * OF SUCH DAMAGE. 34 | * 35 | * This file is part of the lwIP TCP/IP stack. 36 | * 37 | * Author: Dirk Ziegelmeier 38 | * 39 | */ 40 | 41 | #ifndef LWIP_HDR_PROT_IANA_H 42 | #define LWIP_HDR_PROT_IANA_H 43 | 44 | #ifdef __cplusplus 45 | extern "C" { 46 | #endif 47 | 48 | /** 49 | * @ingroup iana 50 | * Hardware types 51 | */ 52 | enum lwip_iana_hwtype { 53 | /** Ethernet */ 54 | LWIP_IANA_HWTYPE_ETHERNET = 1 55 | }; 56 | 57 | /** 58 | * @ingroup iana 59 | * Port numbers 60 | * https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt 61 | */ 62 | enum lwip_iana_port_number { 63 | /** SMTP */ 64 | LWIP_IANA_PORT_SMTP = 25, 65 | /** DHCP server */ 66 | LWIP_IANA_PORT_DHCP_SERVER = 67, 67 | /** DHCP client */ 68 | LWIP_IANA_PORT_DHCP_CLIENT = 68, 69 | /** TFTP */ 70 | LWIP_IANA_PORT_TFTP = 69, 71 | /** HTTP */ 72 | LWIP_IANA_PORT_HTTP = 80, 73 | /** SNTP */ 74 | LWIP_IANA_PORT_SNTP = 123, 75 | /** NETBIOS */ 76 | LWIP_IANA_PORT_NETBIOS = 137, 77 | /** SNMP */ 78 | LWIP_IANA_PORT_SNMP = 161, 79 | /** SNMP traps */ 80 | LWIP_IANA_PORT_SNMP_TRAP = 162, 81 | /** HTTPS */ 82 | LWIP_IANA_PORT_HTTPS = 443, 83 | /** SMTPS */ 84 | LWIP_IANA_PORT_SMTPS = 465, 85 | /** MQTT */ 86 | LWIP_IANA_PORT_MQTT = 1883, 87 | /** MDNS */ 88 | LWIP_IANA_PORT_MDNS = 5353, 89 | /** Secure MQTT */ 90 | LWIP_IANA_PORT_SECURE_MQTT = 8883 91 | }; 92 | 93 | #ifdef __cplusplus 94 | } 95 | #endif 96 | 97 | #endif /* LWIP_HDR_PROT_IANA_H */ 98 | -------------------------------------------------------------------------------- /core/src/core/altcp_alloc.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * Application layered TCP connection API (to be used from TCPIP thread)\n 4 | * This interface mimics the tcp callback API to the application while preventing 5 | * direct linking (much like virtual functions). 6 | * This way, an application can make use of other application layer protocols 7 | * on top of TCP without knowing the details (e.g. TLS, proxy connection). 8 | * 9 | * This file contains allocation implementation that combine several layers. 10 | */ 11 | 12 | /* 13 | * Copyright (c) 2017 Simon Goldschmidt 14 | * All rights reserved. 15 | * 16 | * Redistribution and use in source and binary forms, with or without modification, 17 | * are permitted provided that the following conditions are met: 18 | * 19 | * 1. Redistributions of source code must retain the above copyright notice, 20 | * this list of conditions and the following disclaimer. 21 | * 2. Redistributions in binary form must reproduce the above copyright notice, 22 | * this list of conditions and the following disclaimer in the documentation 23 | * and/or other materials provided with the distribution. 24 | * 3. The name of the author may not be used to endorse or promote products 25 | * derived from this software without specific prior written permission. 26 | * 27 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 28 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 29 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 30 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 31 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 32 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 35 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 36 | * OF SUCH DAMAGE. 37 | * 38 | * This file is part of the lwIP TCP/IP stack. 39 | * 40 | * Author: Simon Goldschmidt 41 | * 42 | */ 43 | 44 | #include "lwip/opt.h" 45 | 46 | #if LWIP_ALTCP /* don't build if not configured for use in lwipopts.h */ 47 | 48 | #include "lwip/altcp.h" 49 | #include "lwip/altcp_tcp.h" 50 | #include "lwip/altcp_tls.h" 51 | #include "lwip/priv/altcp_priv.h" 52 | #include "lwip/mem.h" 53 | 54 | #include 55 | 56 | #if LWIP_ALTCP_TLS 57 | 58 | /** This standard allocator function creates an altcp pcb for 59 | * TLS over TCP */ 60 | struct altcp_pcb * 61 | altcp_tls_new(struct altcp_tls_config *config, u8_t ip_type) 62 | { 63 | struct altcp_pcb *inner_conn, *ret; 64 | LWIP_UNUSED_ARG(ip_type); 65 | 66 | inner_conn = altcp_tcp_new_ip_type(ip_type); 67 | if (inner_conn == NULL) { 68 | return NULL; 69 | } 70 | ret = altcp_tls_wrap(config, inner_conn); 71 | if (ret == NULL) { 72 | altcp_close(inner_conn); 73 | } 74 | return ret; 75 | } 76 | 77 | /** This standard allocator function creates an altcp pcb for 78 | * TLS over TCP */ 79 | struct altcp_pcb * 80 | altcp_tls_alloc(void *arg, u8_t ip_type) 81 | { 82 | return altcp_tls_new((struct altcp_tls_config *)arg, ip_type); 83 | } 84 | 85 | #endif /* LWIP_ALTCP_TLS */ 86 | 87 | #endif /* LWIP_ALTCP */ 88 | -------------------------------------------------------------------------------- /core/src/include/lwip/prot/igmp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * IGMP protocol definitions 4 | */ 5 | 6 | /* 7 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, 14 | * this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 3. The name of the author may not be used to endorse or promote products 19 | * derived from this software without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 | * OF SUCH DAMAGE. 31 | * 32 | * This file is part of the lwIP TCP/IP stack. 33 | * 34 | * Author: Adam Dunkels 35 | * 36 | */ 37 | #ifndef LWIP_HDR_PROT_IGMP_H 38 | #define LWIP_HDR_PROT_IGMP_H 39 | 40 | #include "lwip/arch.h" 41 | #include "lwip/prot/ip4.h" 42 | 43 | #ifdef __cplusplus 44 | extern "C" { 45 | #endif 46 | 47 | /* 48 | * IGMP constants 49 | */ 50 | #define IGMP_TTL 1 51 | #define IGMP_MINLEN 8 52 | #define ROUTER_ALERT 0x9404U 53 | #define ROUTER_ALERTLEN 4 54 | 55 | /* 56 | * IGMP message types, including version number. 57 | */ 58 | #define IGMP_MEMB_QUERY 0x11 /* Membership query */ 59 | #define IGMP_V1_MEMB_REPORT 0x12 /* Ver. 1 membership report */ 60 | #define IGMP_V2_MEMB_REPORT 0x16 /* Ver. 2 membership report */ 61 | #define IGMP_LEAVE_GROUP 0x17 /* Leave-group message */ 62 | 63 | /* Group membership states */ 64 | #define IGMP_GROUP_NON_MEMBER 0 65 | #define IGMP_GROUP_DELAYING_MEMBER 1 66 | #define IGMP_GROUP_IDLE_MEMBER 2 67 | 68 | /** 69 | * IGMP packet format. 70 | */ 71 | #ifdef PACK_STRUCT_USE_INCLUDES 72 | # include "arch/bpstruct.h" 73 | #endif 74 | PACK_STRUCT_BEGIN 75 | struct igmp_msg { 76 | PACK_STRUCT_FLD_8(u8_t igmp_msgtype); 77 | PACK_STRUCT_FLD_8(u8_t igmp_maxresp); 78 | PACK_STRUCT_FIELD(u16_t igmp_checksum); 79 | PACK_STRUCT_FLD_S(ip4_addr_p_t igmp_group_address); 80 | } PACK_STRUCT_STRUCT; 81 | PACK_STRUCT_END 82 | #ifdef PACK_STRUCT_USE_INCLUDES 83 | # include "arch/epstruct.h" 84 | #endif 85 | 86 | #ifdef __cplusplus 87 | } 88 | #endif 89 | 90 | #endif /* LWIP_HDR_PROT_IGMP_H */ 91 | -------------------------------------------------------------------------------- /core/src/include/lwip/prot/ieee.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * IEEE assigned numbers 4 | * 5 | * @defgroup ieee IEEE assigned numbers 6 | * @ingroup infrastructure 7 | */ 8 | 9 | /* 10 | * Copyright (c) 2017 Dirk Ziegelmeier. 11 | * All rights reserved. 12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 16 | * 1. Redistributions of source code must retain the above copyright notice, 17 | * this list of conditions and the following disclaimer. 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, 19 | * this list of conditions and the following disclaimer in the documentation 20 | * and/or other materials provided with the distribution. 21 | * 3. The name of the author may not be used to endorse or promote products 22 | * derived from this software without specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 25 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 27 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 29 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 33 | * OF SUCH DAMAGE. 34 | * 35 | * This file is part of the lwIP TCP/IP stack. 36 | * 37 | * Author: Dirk Ziegelmeier 38 | * 39 | */ 40 | 41 | #ifndef LWIP_HDR_PROT_IEEE_H 42 | #define LWIP_HDR_PROT_IEEE_H 43 | 44 | #ifdef __cplusplus 45 | extern "C" { 46 | #endif 47 | 48 | /** 49 | * @ingroup ieee 50 | * A list of often ethtypes (although lwIP does not use all of them). 51 | */ 52 | enum lwip_ieee_eth_type { 53 | /** Internet protocol v4 */ 54 | ETHTYPE_IP = 0x0800U, 55 | /** Address resolution protocol */ 56 | ETHTYPE_ARP = 0x0806U, 57 | /** Wake on lan */ 58 | ETHTYPE_WOL = 0x0842U, 59 | /** RARP */ 60 | ETHTYPE_RARP = 0x8035U, 61 | /** Virtual local area network */ 62 | ETHTYPE_VLAN = 0x8100U, 63 | /** Internet protocol v6 */ 64 | ETHTYPE_IPV6 = 0x86DDU, 65 | /** PPP Over Ethernet Discovery Stage */ 66 | ETHTYPE_PPPOEDISC = 0x8863U, 67 | /** PPP Over Ethernet Session Stage */ 68 | ETHTYPE_PPPOE = 0x8864U, 69 | /** Jumbo Frames */ 70 | ETHTYPE_JUMBO = 0x8870U, 71 | /** Process field network */ 72 | ETHTYPE_PROFINET = 0x8892U, 73 | /** Ethernet for control automation technology */ 74 | ETHTYPE_ETHERCAT = 0x88A4U, 75 | /** Link layer discovery protocol */ 76 | ETHTYPE_LLDP = 0x88CCU, 77 | /** Serial real-time communication system */ 78 | ETHTYPE_SERCOS = 0x88CDU, 79 | /** Media redundancy protocol */ 80 | ETHTYPE_MRP = 0x88E3U, 81 | /** Precision time protocol */ 82 | ETHTYPE_PTP = 0x88F7U, 83 | /** Q-in-Q, 802.1ad */ 84 | ETHTYPE_QINQ = 0x9100U 85 | }; 86 | 87 | #ifdef __cplusplus 88 | } 89 | #endif 90 | 91 | #endif /* LWIP_HDR_PROT_IEEE_H */ 92 | -------------------------------------------------------------------------------- /core/src/include/netif/lowpan6_ble.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * 6LowPAN over BLE for IPv6 (RFC7668). 4 | */ 5 | 6 | /* 7 | * Copyright (c) 2017 Benjamin Aigner 8 | * Copyright (c) 2015 Inico Technologies Ltd. , Author: Ivan Delamer 9 | * 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * 1. Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 3. The name of the author may not be used to endorse or promote products 21 | * derived from this software without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 24 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 26 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 27 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 28 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 32 | * OF SUCH DAMAGE. 33 | * 34 | * Author: Benjamin Aigner 35 | * 36 | * Based on the original 6lowpan implementation of lwIP ( @see 6lowpan.c) 37 | */ 38 | 39 | #ifndef LWIP_HDR_LOWPAN6_BLE_H 40 | #define LWIP_HDR_LOWPAN6_BLE_H 41 | 42 | #include "netif/lowpan6_opts.h" 43 | 44 | #if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */ 45 | 46 | #include "netif/lowpan6_common.h" 47 | #include "lwip/pbuf.h" 48 | #include "lwip/ip.h" 49 | #include "lwip/ip_addr.h" 50 | #include "lwip/netif.h" 51 | 52 | #ifdef __cplusplus 53 | extern "C" { 54 | #endif 55 | 56 | err_t rfc7668_output(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr); 57 | err_t rfc7668_input(struct pbuf * p, struct netif *netif); 58 | err_t rfc7668_set_local_addr_eui64(struct netif *netif, const u8_t *local_addr, size_t local_addr_len); 59 | err_t rfc7668_set_local_addr_mac48(struct netif *netif, const u8_t *local_addr, size_t local_addr_len, int is_public_addr); 60 | err_t rfc7668_set_peer_addr_eui64(struct netif *netif, const u8_t *peer_addr, size_t peer_addr_len); 61 | err_t rfc7668_set_peer_addr_mac48(struct netif *netif, const u8_t *peer_addr, size_t peer_addr_len, int is_public_addr); 62 | err_t rfc7668_set_context(u8_t index, const ip6_addr_t * context); 63 | err_t rfc7668_if_init(struct netif *netif); 64 | 65 | #if !NO_SYS 66 | err_t tcpip_rfc7668_input(struct pbuf *p, struct netif *inp); 67 | #endif 68 | 69 | void ble_addr_to_eui64(uint8_t *dst, const uint8_t *src, int public_addr); 70 | void eui64_to_ble_addr(uint8_t *dst, const uint8_t *src); 71 | 72 | #ifdef __cplusplus 73 | } 74 | #endif 75 | 76 | #endif /* LWIP_IPV6 */ 77 | 78 | #endif /* LWIP_HDR_LOWPAN6_BLE_H */ 79 | -------------------------------------------------------------------------------- /core/src/include/netif/ppp/polarssl/des.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file des.h 3 | * 4 | * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine 5 | * 6 | * Copyright (C) 2009 Paul Bakker 7 | * 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright 15 | * notice, this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the names of PolarSSL or XySSL nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | #include "netif/ppp/ppp_opts.h" 37 | #if LWIP_INCLUDED_POLARSSL_DES 38 | 39 | #ifndef LWIP_INCLUDED_POLARSSL_DES_H 40 | #define LWIP_INCLUDED_POLARSSL_DES_H 41 | 42 | #define DES_ENCRYPT 1 43 | #define DES_DECRYPT 0 44 | 45 | /** 46 | * \brief DES context structure 47 | */ 48 | typedef struct 49 | { 50 | int mode; /*!< encrypt/decrypt */ 51 | unsigned long sk[32]; /*!< DES subkeys */ 52 | } 53 | des_context; 54 | 55 | #ifdef __cplusplus 56 | extern "C" { 57 | #endif 58 | 59 | /** 60 | * \brief DES key schedule (56-bit, encryption) 61 | * 62 | * \param ctx DES context to be initialized 63 | * \param key 8-byte secret key 64 | */ 65 | void des_setkey_enc( des_context *ctx, unsigned char key[8] ); 66 | 67 | /** 68 | * \brief DES key schedule (56-bit, decryption) 69 | * 70 | * \param ctx DES context to be initialized 71 | * \param key 8-byte secret key 72 | */ 73 | void des_setkey_dec( des_context *ctx, unsigned char key[8] ); 74 | 75 | /** 76 | * \brief DES-ECB block encryption/decryption 77 | * 78 | * \param ctx DES context 79 | * \param input 64-bit input block 80 | * \param output 64-bit output block 81 | */ 82 | void des_crypt_ecb( des_context *ctx, 83 | const unsigned char input[8], 84 | unsigned char output[8] ); 85 | 86 | #ifdef __cplusplus 87 | } 88 | #endif 89 | 90 | #endif /* LWIP_INCLUDED_POLARSSL_DES_H */ 91 | 92 | #endif /* LWIP_INCLUDED_POLARSSL_DES */ 93 | -------------------------------------------------------------------------------- /core/src/include/netif/lowpan6_common.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * 4 | * Common 6LowPAN routines for IPv6. Uses ND tables for link-layer addressing. Fragments packets to 6LowPAN units. 5 | */ 6 | 7 | /* 8 | * Copyright (c) 2015 Inico Technologies Ltd. 9 | * All rights reserved. 10 | * 11 | * Redistribution and use in source and binary forms, with or without modification, 12 | * are permitted provided that the following conditions are met: 13 | * 14 | * 1. Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, 17 | * this list of conditions and the following disclaimer in the documentation 18 | * and/or other materials provided with the distribution. 19 | * 3. The name of the author may not be used to endorse or promote products 20 | * derived from this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 25 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 27 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 30 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 31 | * OF SUCH DAMAGE. 32 | * 33 | * This file is part of the lwIP TCP/IP stack. 34 | * 35 | * Author: Ivan Delamer 36 | * 37 | * 38 | * Please coordinate changes and requests with Ivan Delamer 39 | * 40 | */ 41 | 42 | #ifndef LWIP_HDR_LOWPAN6_COMMON_H 43 | #define LWIP_HDR_LOWPAN6_COMMON_H 44 | 45 | #include "netif/lowpan6_opts.h" 46 | 47 | #if LWIP_IPV6 /* don't build if IPv6 is disabled in lwipopts.h */ 48 | 49 | #include "lwip/pbuf.h" 50 | #include "lwip/ip.h" 51 | #include "lwip/ip6_addr.h" 52 | #include "lwip/netif.h" 53 | 54 | #ifdef __cplusplus 55 | extern "C" { 56 | #endif 57 | 58 | /** Helper define for a link layer address, which can be encoded as 0, 2 or 8 bytes */ 59 | struct lowpan6_link_addr { 60 | /* encoded length of the address */ 61 | u8_t addr_len; 62 | /* address bytes */ 63 | u8_t addr[8]; 64 | }; 65 | 66 | s8_t lowpan6_get_address_mode(const ip6_addr_t *ip6addr, const struct lowpan6_link_addr *mac_addr); 67 | 68 | #if LWIP_6LOWPAN_IPHC 69 | err_t lowpan6_compress_headers(struct netif *netif, u8_t *inbuf, size_t inbuf_size, u8_t *outbuf, size_t outbuf_size, 70 | u8_t *lowpan6_header_len_out, u8_t *hidden_header_len_out, ip6_addr_t *lowpan6_contexts, 71 | const struct lowpan6_link_addr *src, const struct lowpan6_link_addr *dst); 72 | struct pbuf *lowpan6_decompress(struct pbuf *p, u16_t datagram_size, ip6_addr_t *lowpan6_contexts, 73 | struct lowpan6_link_addr *src, struct lowpan6_link_addr *dest); 74 | #endif /* LWIP_6LOWPAN_IPHC */ 75 | 76 | #ifdef __cplusplus 77 | } 78 | #endif 79 | 80 | #endif /* LWIP_IPV6 */ 81 | 82 | #endif /* LWIP_HDR_LOWPAN6_COMMON_H */ 83 | -------------------------------------------------------------------------------- /core/src/include/netif/slipif.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * 4 | * SLIP netif API 5 | */ 6 | 7 | /* 8 | * Copyright (c) 2001, Swedish Institute of Computer Science. 9 | * All rights reserved. 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions 13 | * are met: 14 | * 1. Redistributions of source code must retain the above copyright 15 | * notice, this list of conditions and the following disclaimer. 16 | * 2. Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * 3. Neither the name of the Institute nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 | * SUCH DAMAGE. 34 | * 35 | * This file is part of the lwIP TCP/IP stack. 36 | * 37 | * Author: Adam Dunkels 38 | * 39 | */ 40 | #ifndef LWIP_HDR_NETIF_SLIPIF_H 41 | #define LWIP_HDR_NETIF_SLIPIF_H 42 | 43 | #include "lwip/opt.h" 44 | #include "lwip/netif.h" 45 | 46 | /** Set this to 1 to start a thread that blocks reading on the serial line 47 | * (using sio_read()). 48 | */ 49 | #ifndef SLIP_USE_RX_THREAD 50 | #define SLIP_USE_RX_THREAD !NO_SYS 51 | #endif 52 | 53 | /** Set this to 1 to enable functions to pass in RX bytes from ISR context. 54 | * If enabled, slipif_received_byte[s]() process incoming bytes and put assembled 55 | * packets on a queue, which is fed into lwIP from slipif_poll(). 56 | * If disabled, slipif_poll() polls the serial line (using sio_tryread()). 57 | */ 58 | #ifndef SLIP_RX_FROM_ISR 59 | #define SLIP_RX_FROM_ISR 0 60 | #endif 61 | 62 | /** Set this to 1 (default for SLIP_RX_FROM_ISR) to queue incoming packets 63 | * received by slipif_received_byte[s]() as long as PBUF_POOL pbufs are available. 64 | * If disabled, packets will be dropped if more than one packet is received. 65 | */ 66 | #ifndef SLIP_RX_QUEUE 67 | #define SLIP_RX_QUEUE SLIP_RX_FROM_ISR 68 | #endif 69 | 70 | #ifdef __cplusplus 71 | extern "C" { 72 | #endif 73 | 74 | err_t slipif_init(struct netif * netif); 75 | void slipif_poll(struct netif *netif); 76 | #if SLIP_RX_FROM_ISR 77 | void slipif_process_rxqueue(struct netif *netif); 78 | void slipif_received_byte(struct netif *netif, u8_t data); 79 | void slipif_received_bytes(struct netif *netif, u8_t *data, u8_t len); 80 | #endif /* SLIP_RX_FROM_ISR */ 81 | 82 | #ifdef __cplusplus 83 | } 84 | #endif 85 | 86 | #endif /* LWIP_HDR_NETIF_SLIPIF_H */ 87 | 88 | -------------------------------------------------------------------------------- /filter/filter.go: -------------------------------------------------------------------------------- 1 | package filter 2 | 3 | import ( 4 | "context" 5 | "io" 6 | "log" 7 | "time" 8 | 9 | vnet "v2ray.com/core/common/net" 10 | vsession "v2ray.com/core/common/session" 11 | vrouting "v2ray.com/core/features/routing" 12 | 13 | "github.com/eycorsican/go-tun2socks/route" 14 | ) 15 | 16 | // Filter is used for filtering IP packets comming from TUN. 17 | type Filter interface { 18 | io.Writer 19 | } 20 | 21 | type icmpFilter struct { 22 | writer io.Writer 23 | delay int 24 | } 25 | 26 | func NewICMPFilter(w io.Writer, delay int) Filter { 27 | return &icmpFilter{writer: w, delay: delay} 28 | } 29 | 30 | func (w *icmpFilter) Write(buf []byte) (int, error) { 31 | if uint8(buf[9]) == route.PROTOCOL_ICMP { 32 | payload := make([]byte, len(buf)) 33 | copy(payload, buf) 34 | go func(data []byte) { 35 | time.Sleep(time.Duration(w.delay) * time.Millisecond) 36 | _, err := w.writer.Write(data) 37 | if err != nil { 38 | log.Fatal("failed to input data to the stack: %v", err) 39 | } 40 | }(payload) 41 | return len(buf), nil 42 | } else { 43 | return w.writer.Write(buf) 44 | } 45 | } 46 | 47 | type routingFilter struct { 48 | writer io.Writer 49 | router vrouting.Router 50 | gateway string 51 | } 52 | 53 | func NewRoutingFilter(w io.Writer, router vrouting.Router, gateway string) Filter { 54 | return &routingFilter{writer: w, router: router, gateway: gateway} 55 | } 56 | 57 | func (w *routingFilter) Write(buf []byte) (int, error) { 58 | ipVersion := route.PeekIPVersion(buf) 59 | if ipVersion == route.IPVERSION_6 { 60 | // TODO No IPv6 support currently 61 | return w.writer.Write(buf) 62 | } 63 | 64 | if ipVersion != route.IPVERSION_4 && ipVersion != route.IPVERSION_6 { 65 | log.Fatal("not an IP packet: %v", buf) 66 | } 67 | 68 | protocol := route.PeekProtocol(buf) 69 | if protocol != "tcp" && protocol != "udp" { 70 | return w.writer.Write(buf) 71 | } 72 | if protocol == "tcp" && !route.IsSYNSegment(buf) { 73 | return w.writer.Write(buf) 74 | } 75 | 76 | destAddr := route.PeekDestinationAddress(buf) 77 | destPort := route.PeekDestinationPort(buf) 78 | 79 | var dest vnet.Destination 80 | switch protocol { 81 | case "tcp": 82 | dest = vnet.TCPDestination(destAddr, destPort) 83 | case "udp": 84 | dest = vnet.UDPDestination(destAddr, destPort) 85 | default: 86 | panic("invalid protocol") 87 | } 88 | 89 | ctx := vsession.ContextWithOutbound(context.Background(), &vsession.Outbound{ 90 | Target: dest, 91 | }) 92 | 93 | tag, err := w.router.PickRoute(ctx) 94 | if err == nil && tag == "direct" { 95 | err := route.AddRoute(dest.Address.String(), "255.255.255.255", w.gateway) 96 | if err == nil { 97 | // Discarding the packet so it will be retransmitted, and hopefully retransmitted packets will 98 | // use the new route. 99 | // 100 | // TODO: On macOS, it appears that even though the route is added to the routing table, subsequent 101 | // retransmitted packets will continue using the old routing policy. Local client must create a new 102 | // socket for utilizing the new route. Other platforms are not tested. 103 | // Maybe this helps: https://www.unix.com/man-page/osx/4/route/ 104 | // log.Printf("added a direct route for destination %v, packets need re-routing, dropped", dest) 105 | return len(buf), nil 106 | } else { 107 | log.Printf("adding route for %v failed: %v", dest, err) 108 | } 109 | } 110 | 111 | return w.writer.Write(buf) 112 | } 113 | -------------------------------------------------------------------------------- /core/src/include/lwip/prot/autoip.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * AutoIP protocol definitions 4 | */ 5 | 6 | /* 7 | * 8 | * Copyright (c) 2007 Dominik Spies 9 | * All rights reserved. 10 | * 11 | * Redistribution and use in source and binary forms, with or without modification, 12 | * are permitted provided that the following conditions are met: 13 | * 14 | * 1. Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, 17 | * this list of conditions and the following disclaimer in the documentation 18 | * and/or other materials provided with the distribution. 19 | * 3. The name of the author may not be used to endorse or promote products 20 | * derived from this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 25 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 27 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 30 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 31 | * OF SUCH DAMAGE. 32 | * 33 | * Author: Dominik Spies 34 | * 35 | * This is a AutoIP implementation for the lwIP TCP/IP stack. It aims to conform 36 | * with RFC 3927. 37 | * 38 | */ 39 | 40 | #ifndef LWIP_HDR_PROT_AUTOIP_H 41 | #define LWIP_HDR_PROT_AUTOIP_H 42 | 43 | #ifdef __cplusplus 44 | extern "C" { 45 | #endif 46 | 47 | /* 169.254.0.0 */ 48 | #define AUTOIP_NET 0xA9FE0000 49 | /* 169.254.1.0 */ 50 | #define AUTOIP_RANGE_START (AUTOIP_NET | 0x0100) 51 | /* 169.254.254.255 */ 52 | #define AUTOIP_RANGE_END (AUTOIP_NET | 0xFEFF) 53 | 54 | /* RFC 3927 Constants */ 55 | #define PROBE_WAIT 1 /* second (initial random delay) */ 56 | #define PROBE_MIN 1 /* second (minimum delay till repeated probe) */ 57 | #define PROBE_MAX 2 /* seconds (maximum delay till repeated probe) */ 58 | #define PROBE_NUM 3 /* (number of probe packets) */ 59 | #define ANNOUNCE_NUM 2 /* (number of announcement packets) */ 60 | #define ANNOUNCE_INTERVAL 2 /* seconds (time between announcement packets) */ 61 | #define ANNOUNCE_WAIT 2 /* seconds (delay before announcing) */ 62 | #define MAX_CONFLICTS 10 /* (max conflicts before rate limiting) */ 63 | #define RATE_LIMIT_INTERVAL 60 /* seconds (delay between successive attempts) */ 64 | #define DEFEND_INTERVAL 10 /* seconds (min. wait between defensive ARPs) */ 65 | 66 | /* AutoIP client states */ 67 | typedef enum { 68 | AUTOIP_STATE_OFF = 0, 69 | AUTOIP_STATE_PROBING = 1, 70 | AUTOIP_STATE_ANNOUNCING = 2, 71 | AUTOIP_STATE_BOUND = 3 72 | } autoip_state_enum_t; 73 | 74 | #ifdef __cplusplus 75 | } 76 | #endif 77 | 78 | #endif /* LWIP_HDR_PROT_AUTOIP_H */ 79 | -------------------------------------------------------------------------------- /tun/tun_linux.go: -------------------------------------------------------------------------------- 1 | package tun 2 | 3 | import ( 4 | // "errors" 5 | // "fmt" 6 | "io" 7 | "log" 8 | // "net" 9 | "os" 10 | // "os/exec" 11 | "syscall" 12 | "unsafe" 13 | ) 14 | 15 | const ( 16 | IFF_TUN = 0x0001 17 | IFF_TAP = 0x0002 18 | IFF_NO_PI = 0x1000 19 | ) 20 | 21 | type ifReq struct { 22 | Name [0x10]byte 23 | Flags uint16 24 | pad [0x28 - 0x10 - 2]byte 25 | } 26 | 27 | func OpenTunDevice(name, addr, gw, mask string, dns []string) (io.ReadWriteCloser, error) { 28 | // config address 29 | // log.Printf("configuring tun device address") 30 | // cmd := exec.Command("ifconfig", name, addr, "netmask", mask, "mtu", "1500") 31 | // err = cmd.Run() 32 | // if err != nil { 33 | // file.Close() 34 | // log.Printf("failed to configure tun device address") 35 | // return nil, err 36 | // } 37 | // addTunCmd := exec.Command("ip", "tuntap", "add", "mode", "tun", "dev", name) 38 | // // err = addTunCmd.Run() 39 | // output, err := addTunCmd.CombinedOutput() 40 | // if err != nil { 41 | // // file.Close() 42 | // log.Printf("failed to add tun device: %v, %v", string(output), err) 43 | // return nil, err 44 | // } 45 | 46 | // configureTunCmd := exec.Command("ip", "addr", "add", fmt.Sprintf("%s/%s", addr, mask), "dev", name) 47 | // output, err = configureTunCmd.CombinedOutput() 48 | // if err != nil { 49 | // // file.Close() 50 | // log.Printf("failed to configure address for tun device: %v, %v", string(output), err) 51 | // return nil, err 52 | // } 53 | 54 | // linkTunCmd := exec.Command("ip", "link", "set", "dev", name, "up") 55 | // output, err = linkTunCmd.CombinedOutput() 56 | // if err != nil { 57 | // // file.Close() 58 | // log.Printf("failed to link tun device: %v, %v", string(output), err) 59 | // return nil, err 60 | // } 61 | 62 | file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0) 63 | if err != nil { 64 | return nil, err 65 | } 66 | var req ifReq 67 | copy(req.Name[:], name) 68 | req.Flags = IFF_TUN | IFF_NO_PI 69 | log.Printf("openning tun device") 70 | _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), uintptr(syscall.TUNSETIFF), uintptr(unsafe.Pointer(&req))) 71 | if errno != 0 { 72 | err = errno 73 | return nil, err 74 | } 75 | 76 | syscall.SetNonblock(int(file.Fd()), false) 77 | return &tunDev{ 78 | f: file, 79 | addr: addr, 80 | // addrIP: net.ParseIP(addr).To4(), 81 | gw: gw, 82 | // gwIP: net.ParseIP(gw).To4(), 83 | }, nil 84 | } 85 | 86 | func NewTunDev(fd uintptr, name string, addr string, gw string) io.ReadWriteCloser { 87 | syscall.SetNonblock(int(fd), false) 88 | return &tunDev{ 89 | f: os.NewFile(fd, name), 90 | addr: addr, 91 | // addrIP: net.ParseIP(addr).To4(), 92 | gw: gw, 93 | // gwIP: net.ParseIP(gw).To4(), 94 | } 95 | } 96 | 97 | type tunDev struct { 98 | name string 99 | addr string 100 | // addrIP net.IP 101 | gw string 102 | // gwIP net.IP 103 | // marker []byte 104 | f *os.File 105 | } 106 | 107 | func (dev *tunDev) Read(data []byte) (int, error) { 108 | n, e := dev.f.Read(data) 109 | // if e == nil && isStopMarker(data[:n], dev.addrIP, dev.gwIP) { 110 | // return 0, errors.New("received stop marker") 111 | // } 112 | return n, e 113 | } 114 | 115 | func (dev *tunDev) Write(data []byte) (int, error) { 116 | return dev.f.Write(data) 117 | } 118 | 119 | func (dev *tunDev) Close() error { 120 | // log.Printf("send stop marker") 121 | // sendStopMarker(dev.addr, dev.gw) 122 | return dev.f.Close() 123 | } 124 | -------------------------------------------------------------------------------- /core/src/include/lwip/apps/mqtt_opts.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * MQTT client options 4 | */ 5 | 6 | /* 7 | * Copyright (c) 2016 Erik Andersson 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, 14 | * this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 3. The name of the author may not be used to endorse or promote products 19 | * derived from this software without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 | * OF SUCH DAMAGE. 31 | * 32 | * This file is part of the lwIP TCP/IP stack. 33 | * 34 | * Author: Erik Andersson 35 | * 36 | */ 37 | #ifndef LWIP_HDR_APPS_MQTT_OPTS_H 38 | #define LWIP_HDR_APPS_MQTT_OPTS_H 39 | 40 | #include "lwip/opt.h" 41 | 42 | #ifdef __cplusplus 43 | extern "C" { 44 | #endif 45 | 46 | /** 47 | * @defgroup mqtt_opts Options 48 | * @ingroup mqtt 49 | * @{ 50 | */ 51 | 52 | /** 53 | * Output ring-buffer size, must be able to fit largest outgoing publish message topic+payloads 54 | */ 55 | #ifndef MQTT_OUTPUT_RINGBUF_SIZE 56 | #define MQTT_OUTPUT_RINGBUF_SIZE 256 57 | #endif 58 | 59 | /** 60 | * Number of bytes in receive buffer, must be at least the size of the longest incoming topic + 8 61 | * If one wants to avoid fragmented incoming publish, set length to max incoming topic length + max payload length + 8 62 | */ 63 | #ifndef MQTT_VAR_HEADER_BUFFER_LEN 64 | #define MQTT_VAR_HEADER_BUFFER_LEN 128 65 | #endif 66 | 67 | /** 68 | * Maximum number of pending subscribe, unsubscribe and publish requests to server . 69 | */ 70 | #ifndef MQTT_REQ_MAX_IN_FLIGHT 71 | #define MQTT_REQ_MAX_IN_FLIGHT 4 72 | #endif 73 | 74 | /** 75 | * Seconds between each cyclic timer call. 76 | */ 77 | #ifndef MQTT_CYCLIC_TIMER_INTERVAL 78 | #define MQTT_CYCLIC_TIMER_INTERVAL 5 79 | #endif 80 | 81 | /** 82 | * Publish, subscribe and unsubscribe request timeout in seconds. 83 | */ 84 | #ifndef MQTT_REQ_TIMEOUT 85 | #define MQTT_REQ_TIMEOUT 30 86 | #endif 87 | 88 | /** 89 | * Seconds for MQTT connect response timeout after sending connect request 90 | */ 91 | #ifndef MQTT_CONNECT_TIMOUT 92 | #define MQTT_CONNECT_TIMOUT 100 93 | #endif 94 | 95 | /** 96 | * @} 97 | */ 98 | 99 | #ifdef __cplusplus 100 | } 101 | #endif 102 | 103 | #endif /* LWIP_HDR_APPS_MQTT_OPTS_H */ 104 | -------------------------------------------------------------------------------- /core/src/include/lwip/apps/tftp_opts.h: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * @file tftp_opts.h 4 | * 5 | * @author Logan Gunthorpe 6 | * 7 | * @brief Trivial File Transfer Protocol (RFC 1350) implementation options 8 | * 9 | * Copyright (c) Deltatee Enterprises Ltd. 2013 10 | * All rights reserved. 11 | * 12 | */ 13 | 14 | /* 15 | * Redistribution and use in source and binary forms, with or without 16 | * modification,are permitted provided that the following conditions are met: 17 | * 18 | * 1. Redistributions of source code must retain the above copyright notice, 19 | * this list of conditions and the following disclaimer. 20 | * 2. Redistributions in binary form must reproduce the above copyright notice, 21 | * this list of conditions and the following disclaimer in the documentation 22 | * and/or other materials provided with the distribution. 23 | * 3. The name of the author may not be used to endorse or promote products 24 | * derived from this software without specific prior written permission. 25 | * 26 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 27 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 28 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 29 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 31 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 32 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 34 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 35 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 | * 37 | * Author: Logan Gunthorpe 38 | * 39 | */ 40 | 41 | #ifndef LWIP_HDR_APPS_TFTP_OPTS_H 42 | #define LWIP_HDR_APPS_TFTP_OPTS_H 43 | 44 | #include "lwip/opt.h" 45 | #include "lwip/prot/iana.h" 46 | 47 | /** 48 | * @defgroup tftp_opts Options 49 | * @ingroup tftp 50 | * @{ 51 | */ 52 | 53 | /** 54 | * Enable TFTP debug messages 55 | */ 56 | #if !defined TFTP_DEBUG || defined __DOXYGEN__ 57 | #define TFTP_DEBUG LWIP_DBG_OFF 58 | #endif 59 | 60 | /** 61 | * TFTP server port 62 | */ 63 | #if !defined TFTP_PORT || defined __DOXYGEN__ 64 | #define TFTP_PORT LWIP_IANA_PORT_TFTP 65 | #endif 66 | 67 | /** 68 | * TFTP timeout 69 | */ 70 | #if !defined TFTP_TIMEOUT_MSECS || defined __DOXYGEN__ 71 | #define TFTP_TIMEOUT_MSECS 10000 72 | #endif 73 | 74 | /** 75 | * Max. number of retries when a file is read from server 76 | */ 77 | #if !defined TFTP_MAX_RETRIES || defined __DOXYGEN__ 78 | #define TFTP_MAX_RETRIES 5 79 | #endif 80 | 81 | /** 82 | * TFTP timer cyclic interval 83 | */ 84 | #if !defined TFTP_TIMER_MSECS || defined __DOXYGEN__ 85 | #define TFTP_TIMER_MSECS (TFTP_TIMEOUT_MSECS / 10) 86 | #endif 87 | 88 | /** 89 | * Max. length of TFTP filename 90 | */ 91 | #if !defined TFTP_MAX_FILENAME_LEN || defined __DOXYGEN__ 92 | #define TFTP_MAX_FILENAME_LEN 20 93 | #endif 94 | 95 | /** 96 | * Max. length of TFTP mode 97 | */ 98 | #if !defined TFTP_MAX_MODE_LEN || defined __DOXYGEN__ 99 | #define TFTP_MAX_MODE_LEN 7 100 | #endif 101 | 102 | /** 103 | * @} 104 | */ 105 | 106 | #endif /* LWIP_HDR_APPS_TFTP_OPTS_H */ 107 | -------------------------------------------------------------------------------- /core/src/include/lwip/nd6.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * 4 | * Neighbor discovery and stateless address autoconfiguration for IPv6. 5 | * Aims to be compliant with RFC 4861 (Neighbor discovery) and RFC 4862 6 | * (Address autoconfiguration). 7 | */ 8 | 9 | /* 10 | * Copyright (c) 2010 Inico Technologies Ltd. 11 | * All rights reserved. 12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 16 | * 1. Redistributions of source code must retain the above copyright notice, 17 | * this list of conditions and the following disclaimer. 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, 19 | * this list of conditions and the following disclaimer in the documentation 20 | * and/or other materials provided with the distribution. 21 | * 3. The name of the author may not be used to endorse or promote products 22 | * derived from this software without specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 25 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 27 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 29 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 33 | * OF SUCH DAMAGE. 34 | * 35 | * This file is part of the lwIP TCP/IP stack. 36 | * 37 | * Author: Ivan Delamer 38 | * 39 | * 40 | * Please coordinate changes and requests with Ivan Delamer 41 | * 42 | */ 43 | 44 | #ifndef LWIP_HDR_ND6_H 45 | #define LWIP_HDR_ND6_H 46 | 47 | #include "lwip/opt.h" 48 | 49 | #if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */ 50 | 51 | #include "lwip/ip6_addr.h" 52 | #include "lwip/err.h" 53 | 54 | #ifdef __cplusplus 55 | extern "C" { 56 | #endif 57 | 58 | /** 1 second period */ 59 | #define ND6_TMR_INTERVAL 1000 60 | 61 | /** Router solicitations are sent in 4 second intervals (see RFC 4861, ch. 6.3.7) */ 62 | #ifndef ND6_RTR_SOLICITATION_INTERVAL 63 | #define ND6_RTR_SOLICITATION_INTERVAL 4000 64 | #endif 65 | 66 | struct pbuf; 67 | struct netif; 68 | 69 | void nd6_tmr(void); 70 | void nd6_input(struct pbuf *p, struct netif *inp); 71 | void nd6_clear_destination_cache(void); 72 | struct netif *nd6_find_route(const ip6_addr_t *ip6addr); 73 | err_t nd6_get_next_hop_addr_or_queue(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr, const u8_t **hwaddrp); 74 | u16_t nd6_get_destination_mtu(const ip6_addr_t *ip6addr, struct netif *netif); 75 | #if LWIP_ND6_TCP_REACHABILITY_HINTS 76 | void nd6_reachability_hint(const ip6_addr_t *ip6addr); 77 | #endif /* LWIP_ND6_TCP_REACHABILITY_HINTS */ 78 | void nd6_cleanup_netif(struct netif *netif); 79 | #if LWIP_IPV6_MLD 80 | void nd6_adjust_mld_membership(struct netif *netif, s8_t addr_idx, u8_t new_state); 81 | #endif /* LWIP_IPV6_MLD */ 82 | void nd6_restart_netif(struct netif *netif); 83 | 84 | #ifdef __cplusplus 85 | } 86 | #endif 87 | 88 | #endif /* LWIP_IPV6 */ 89 | 90 | #endif /* LWIP_HDR_ND6_H */ 91 | -------------------------------------------------------------------------------- /core/src/include/netif/ppp/polarssl/md5.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file md5.h 3 | * 4 | * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine 5 | * 6 | * Copyright (C) 2009 Paul Bakker 7 | * 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright 15 | * notice, this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the names of PolarSSL or XySSL nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | #include "netif/ppp/ppp_opts.h" 37 | #if LWIP_INCLUDED_POLARSSL_MD5 38 | 39 | #ifndef LWIP_INCLUDED_POLARSSL_MD5_H 40 | #define LWIP_INCLUDED_POLARSSL_MD5_H 41 | 42 | /** 43 | * \brief MD5 context structure 44 | */ 45 | typedef struct 46 | { 47 | unsigned long total[2]; /*!< number of bytes processed */ 48 | unsigned long state[4]; /*!< intermediate digest state */ 49 | unsigned char buffer[64]; /*!< data block being processed */ 50 | } 51 | md5_context; 52 | 53 | #ifdef __cplusplus 54 | extern "C" { 55 | #endif 56 | 57 | /** 58 | * \brief MD5 context setup 59 | * 60 | * \param ctx context to be initialized 61 | */ 62 | void md5_starts( md5_context *ctx ); 63 | 64 | /** 65 | * \brief MD5 process buffer 66 | * 67 | * \param ctx MD5 context 68 | * \param input buffer holding the data 69 | * \param ilen length of the input data 70 | */ 71 | void md5_update( md5_context *ctx, const unsigned char *input, int ilen ); 72 | 73 | /** 74 | * \brief MD5 final digest 75 | * 76 | * \param ctx MD5 context 77 | * \param output MD5 checksum result 78 | */ 79 | void md5_finish( md5_context *ctx, unsigned char output[16] ); 80 | 81 | /** 82 | * \brief Output = MD5( input buffer ) 83 | * 84 | * \param input buffer holding the data 85 | * \param ilen length of the input data 86 | * \param output MD5 checksum result 87 | */ 88 | void md5( unsigned char *input, int ilen, unsigned char output[16] ); 89 | 90 | #ifdef __cplusplus 91 | } 92 | #endif 93 | 94 | #endif /* LWIP_INCLUDED_POLARSSL_MD5_H */ 95 | 96 | #endif /* LWIP_INCLUDED_POLARSSL_MD5 */ 97 | -------------------------------------------------------------------------------- /core/src/include/netif/ppp/polarssl/md4.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file md4.h 3 | * 4 | * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine 5 | * 6 | * Copyright (C) 2009 Paul Bakker 7 | * 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright 15 | * notice, this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the names of PolarSSL or XySSL nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | #include "netif/ppp/ppp_opts.h" 37 | #if LWIP_INCLUDED_POLARSSL_MD4 38 | 39 | #ifndef LWIP_INCLUDED_POLARSSL_MD4_H 40 | #define LWIP_INCLUDED_POLARSSL_MD4_H 41 | 42 | /** 43 | * \brief MD4 context structure 44 | */ 45 | typedef struct 46 | { 47 | unsigned long total[2]; /*!< number of bytes processed */ 48 | unsigned long state[4]; /*!< intermediate digest state */ 49 | unsigned char buffer[64]; /*!< data block being processed */ 50 | } 51 | md4_context; 52 | 53 | #ifdef __cplusplus 54 | extern "C" { 55 | #endif 56 | 57 | /** 58 | * \brief MD4 context setup 59 | * 60 | * \param ctx context to be initialized 61 | */ 62 | void md4_starts( md4_context *ctx ); 63 | 64 | /** 65 | * \brief MD4 process buffer 66 | * 67 | * \param ctx MD4 context 68 | * \param input buffer holding the data 69 | * \param ilen length of the input data 70 | */ 71 | void md4_update( md4_context *ctx, const unsigned char *input, int ilen ); 72 | 73 | /** 74 | * \brief MD4 final digest 75 | * 76 | * \param ctx MD4 context 77 | * \param output MD4 checksum result 78 | */ 79 | void md4_finish( md4_context *ctx, unsigned char output[16] ); 80 | 81 | /** 82 | * \brief Output = MD4( input buffer ) 83 | * 84 | * \param input buffer holding the data 85 | * \param ilen length of the input data 86 | * \param output MD4 checksum result 87 | */ 88 | void md4( unsigned char *input, int ilen, unsigned char output[16] ); 89 | 90 | 91 | #ifdef __cplusplus 92 | } 93 | #endif 94 | 95 | #endif /* LWIP_INCLUDED_POLARSSL_MD4_H */ 96 | 97 | #endif /* LWIP_INCLUDED_POLARSSL_MD4 */ 98 | -------------------------------------------------------------------------------- /core/src/include/netif/ppp/polarssl/sha1.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file sha1.h 3 | * 4 | * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine 5 | * 6 | * Copyright (C) 2009 Paul Bakker 7 | * 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright 15 | * notice, this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the names of PolarSSL or XySSL nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | #include "netif/ppp/ppp_opts.h" 37 | #if LWIP_INCLUDED_POLARSSL_SHA1 38 | 39 | #ifndef LWIP_INCLUDED_POLARSSL_SHA1_H 40 | #define LWIP_INCLUDED_POLARSSL_SHA1_H 41 | 42 | /** 43 | * \brief SHA-1 context structure 44 | */ 45 | typedef struct 46 | { 47 | unsigned long total[2]; /*!< number of bytes processed */ 48 | unsigned long state[5]; /*!< intermediate digest state */ 49 | unsigned char buffer[64]; /*!< data block being processed */ 50 | } 51 | sha1_context; 52 | 53 | #ifdef __cplusplus 54 | extern "C" { 55 | #endif 56 | 57 | /** 58 | * \brief SHA-1 context setup 59 | * 60 | * \param ctx context to be initialized 61 | */ 62 | void sha1_starts( sha1_context *ctx ); 63 | 64 | /** 65 | * \brief SHA-1 process buffer 66 | * 67 | * \param ctx SHA-1 context 68 | * \param input buffer holding the data 69 | * \param ilen length of the input data 70 | */ 71 | void sha1_update( sha1_context *ctx, const unsigned char *input, int ilen ); 72 | 73 | /** 74 | * \brief SHA-1 final digest 75 | * 76 | * \param ctx SHA-1 context 77 | * \param output SHA-1 checksum result 78 | */ 79 | void sha1_finish( sha1_context *ctx, unsigned char output[20] ); 80 | 81 | /** 82 | * \brief Output = SHA-1( input buffer ) 83 | * 84 | * \param input buffer holding the data 85 | * \param ilen length of the input data 86 | * \param output SHA-1 checksum result 87 | */ 88 | void sha1( unsigned char *input, int ilen, unsigned char output[20] ); 89 | 90 | #ifdef __cplusplus 91 | } 92 | #endif 93 | 94 | #endif /* LWIP_INCLUDED_POLARSSL_SHA1_H */ 95 | 96 | #endif /* LWIP_INCLUDED_POLARSSL_SHA1 */ 97 | -------------------------------------------------------------------------------- /core/src/include/netif/ppp/pppdebug.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * pppdebug.h - System debugging utilities. 3 | * 4 | * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc. 5 | * portions Copyright (c) 1998 Global Election Systems Inc. 6 | * portions Copyright (c) 2001 by Cognizant Pty Ltd. 7 | * 8 | * The authors hereby grant permission to use, copy, modify, distribute, 9 | * and license this software and its documentation for any purpose, provided 10 | * that existing copyright notices are retained in all copies and that this 11 | * notice and the following disclaimer are included verbatim in any 12 | * distributions. No written agreement, license, or royalty fee is required 13 | * for any of the authorized uses. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 | * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | * 26 | ****************************************************************************** 27 | * REVISION HISTORY (please don't use tabs!) 28 | * 29 | * 03-01-01 Marc Boucher 30 | * Ported to lwIP. 31 | * 98-07-29 Guy Lancaster , Global Election Systems Inc. 32 | * Original. 33 | * 34 | ***************************************************************************** 35 | */ 36 | 37 | #include "netif/ppp/ppp_opts.h" 38 | #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */ 39 | 40 | #ifndef PPPDEBUG_H 41 | #define PPPDEBUG_H 42 | 43 | #ifdef __cplusplus 44 | extern "C" { 45 | #endif 46 | 47 | /* Trace levels. */ 48 | #define LOG_CRITICAL (PPP_DEBUG | LWIP_DBG_LEVEL_SEVERE) 49 | #define LOG_ERR (PPP_DEBUG | LWIP_DBG_LEVEL_SEVERE) 50 | #define LOG_NOTICE (PPP_DEBUG | LWIP_DBG_LEVEL_WARNING) 51 | #define LOG_WARNING (PPP_DEBUG | LWIP_DBG_LEVEL_WARNING) 52 | #define LOG_INFO (PPP_DEBUG) 53 | #define LOG_DETAIL (PPP_DEBUG) 54 | #define LOG_DEBUG (PPP_DEBUG) 55 | 56 | #if PPP_DEBUG 57 | 58 | #define MAINDEBUG(a) LWIP_DEBUGF(LWIP_DBG_LEVEL_WARNING, a) 59 | #define SYSDEBUG(a) LWIP_DEBUGF(LWIP_DBG_LEVEL_WARNING, a) 60 | #define FSMDEBUG(a) LWIP_DEBUGF(LWIP_DBG_LEVEL_WARNING, a) 61 | #define LCPDEBUG(a) LWIP_DEBUGF(LWIP_DBG_LEVEL_WARNING, a) 62 | #define IPCPDEBUG(a) LWIP_DEBUGF(LWIP_DBG_LEVEL_WARNING, a) 63 | #define IPV6CPDEBUG(a) LWIP_DEBUGF(LWIP_DBG_LEVEL_WARNING, a) 64 | #define UPAPDEBUG(a) LWIP_DEBUGF(LWIP_DBG_LEVEL_WARNING, a) 65 | #define CHAPDEBUG(a) LWIP_DEBUGF(LWIP_DBG_LEVEL_WARNING, a) 66 | #define PPPDEBUG(a, b) LWIP_DEBUGF(a, b) 67 | 68 | #else /* PPP_DEBUG */ 69 | 70 | #define MAINDEBUG(a) 71 | #define SYSDEBUG(a) 72 | #define FSMDEBUG(a) 73 | #define LCPDEBUG(a) 74 | #define IPCPDEBUG(a) 75 | #define IPV6CPDEBUG(a) 76 | #define UPAPDEBUG(a) 77 | #define CHAPDEBUG(a) 78 | #define PPPDEBUG(a, b) 79 | 80 | #endif /* PPP_DEBUG */ 81 | 82 | #ifdef __cplusplus 83 | } 84 | #endif 85 | 86 | #endif /* PPPDEBUG_H */ 87 | 88 | #endif /* PPP_SUPPORT */ 89 | -------------------------------------------------------------------------------- /core/src/include/netif/ppp/eui64.h: -------------------------------------------------------------------------------- 1 | /* 2 | * eui64.h - EUI64 routines for IPv6CP. 3 | * 4 | * Copyright (c) 1999 Tommi Komulainen. All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * 3. The name(s) of the authors of this software must not be used to 19 | * endorse or promote products derived from this software without 20 | * prior written permission. 21 | * 22 | * 4. Redistributions of any form whatsoever must retain the following 23 | * acknowledgment: 24 | * "This product includes software developed by Tommi Komulainen 25 | * ". 26 | * 27 | * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO 28 | * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 29 | * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY 30 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 31 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 32 | * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 33 | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 34 | * 35 | * $Id: eui64.h,v 1.6 2002/12/04 23:03:32 paulus Exp $ 36 | */ 37 | 38 | #include "netif/ppp/ppp_opts.h" 39 | #if PPP_SUPPORT && PPP_IPV6_SUPPORT /* don't build if not configured for use in lwipopts.h */ 40 | 41 | #ifndef EUI64_H 42 | #define EUI64_H 43 | 44 | #ifdef __cplusplus 45 | extern "C" { 46 | #endif 47 | 48 | /* 49 | * @todo: 50 | * 51 | * Maybe this should be done by processing struct in6_addr directly... 52 | */ 53 | typedef union 54 | { 55 | u8_t e8[8]; 56 | u16_t e16[4]; 57 | u32_t e32[2]; 58 | } eui64_t; 59 | 60 | #define eui64_iszero(e) (((e).e32[0] | (e).e32[1]) == 0) 61 | #define eui64_equals(e, o) (((e).e32[0] == (o).e32[0]) && \ 62 | ((e).e32[1] == (o).e32[1])) 63 | #define eui64_zero(e) (e).e32[0] = (e).e32[1] = 0; 64 | 65 | #define eui64_copy(s, d) memcpy(&(d), &(s), sizeof(eui64_t)) 66 | 67 | #define eui64_magic(e) do { \ 68 | (e).e32[0] = magic(); \ 69 | (e).e32[1] = magic(); \ 70 | (e).e8[0] &= ~2; \ 71 | } while (0) 72 | #define eui64_magic_nz(x) do { \ 73 | eui64_magic(x); \ 74 | } while (eui64_iszero(x)) 75 | #define eui64_magic_ne(x, y) do { \ 76 | eui64_magic(x); \ 77 | } while (eui64_equals(x, y)) 78 | 79 | #define eui64_get(ll, cp) do { \ 80 | eui64_copy((*cp), (ll)); \ 81 | (cp) += sizeof(eui64_t); \ 82 | } while (0) 83 | 84 | #define eui64_put(ll, cp) do { \ 85 | eui64_copy((ll), (*cp)); \ 86 | (cp) += sizeof(eui64_t); \ 87 | } while (0) 88 | 89 | #define eui64_set32(e, l) do { \ 90 | (e).e32[0] = 0; \ 91 | (e).e32[1] = lwip_htonl(l); \ 92 | } while (0) 93 | #define eui64_setlo32(e, l) eui64_set32(e, l) 94 | 95 | char *eui64_ntoa(eui64_t); /* Returns ascii representation of id */ 96 | 97 | #ifdef __cplusplus 98 | } 99 | #endif 100 | 101 | #endif /* EUI64_H */ 102 | #endif /* PPP_SUPPORT && PPP_IPV6_SUPPORT */ 103 | -------------------------------------------------------------------------------- /core/src/include/lwip/apps/tftp_server.h: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * @file tftp_server.h 4 | * 5 | * @author Logan Gunthorpe 6 | * 7 | * @brief Trivial File Transfer Protocol (RFC 1350) 8 | * 9 | * Copyright (c) Deltatee Enterprises Ltd. 2013 10 | * All rights reserved. 11 | * 12 | */ 13 | 14 | /* 15 | * Redistribution and use in source and binary forms, with or without 16 | * modification,are permitted provided that the following conditions are met: 17 | * 18 | * 1. Redistributions of source code must retain the above copyright notice, 19 | * this list of conditions and the following disclaimer. 20 | * 2. Redistributions in binary form must reproduce the above copyright notice, 21 | * this list of conditions and the following disclaimer in the documentation 22 | * and/or other materials provided with the distribution. 23 | * 3. The name of the author may not be used to endorse or promote products 24 | * derived from this software without specific prior written permission. 25 | * 26 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 27 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 28 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 29 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 31 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 32 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 34 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 35 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 | * 37 | * Author: Logan Gunthorpe 38 | * 39 | */ 40 | 41 | #ifndef LWIP_HDR_APPS_TFTP_SERVER_H 42 | #define LWIP_HDR_APPS_TFTP_SERVER_H 43 | 44 | #include "lwip/apps/tftp_opts.h" 45 | #include "lwip/err.h" 46 | #include "lwip/pbuf.h" 47 | 48 | #ifdef __cplusplus 49 | extern "C" { 50 | #endif 51 | 52 | /** @ingroup tftp 53 | * TFTP context containing callback functions for TFTP transfers 54 | */ 55 | struct tftp_context { 56 | /** 57 | * Open file for read/write. 58 | * @param fname Filename 59 | * @param mode Mode string from TFTP RFC 1350 (netascii, octet, mail) 60 | * @param write Flag indicating read (0) or write (!= 0) access 61 | * @returns File handle supplied to other functions 62 | */ 63 | void* (*open)(const char* fname, const char* mode, u8_t write); 64 | /** 65 | * Close file handle 66 | * @param handle File handle returned by open() 67 | */ 68 | void (*close)(void* handle); 69 | /** 70 | * Read from file 71 | * @param handle File handle returned by open() 72 | * @param buf Target buffer to copy read data to 73 | * @param bytes Number of bytes to copy to buf 74 | * @returns >= 0: Success; < 0: Error 75 | */ 76 | int (*read)(void* handle, void* buf, int bytes); 77 | /** 78 | * Write to file 79 | * @param handle File handle returned by open() 80 | * @param pbuf PBUF adjusted such that payload pointer points 81 | * to the beginning of write data. In other words, 82 | * TFTP headers are stripped off. 83 | * @returns >= 0: Success; < 0: Error 84 | */ 85 | int (*write)(void* handle, struct pbuf* p); 86 | }; 87 | 88 | err_t tftp_init(const struct tftp_context* ctx); 89 | void tftp_cleanup(void); 90 | 91 | #ifdef __cplusplus 92 | } 93 | #endif 94 | 95 | #endif /* LWIP_HDR_APPS_TFTP_SERVER_H */ 96 | -------------------------------------------------------------------------------- /core/src/include/lwip/priv/mem_priv.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * lwIP internal memory implementations (do not use in application code) 4 | */ 5 | 6 | /* 7 | * Copyright (c) 2018 Swedish Institute of Computer Science. 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, 14 | * this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 3. The name of the author may not be used to endorse or promote products 19 | * derived from this software without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 | * OF SUCH DAMAGE. 31 | * 32 | * This file is part of the lwIP TCP/IP stack. 33 | * 34 | * Author: Simon Goldschmidt 35 | * 36 | */ 37 | 38 | #ifndef LWIP_HDR_MEM_PRIV_H 39 | #define LWIP_HDR_MEM_PRIV_H 40 | 41 | #include "lwip/opt.h" 42 | 43 | #ifdef __cplusplus 44 | extern "C" { 45 | #endif 46 | 47 | #include "lwip/mem.h" 48 | 49 | #if MEM_OVERFLOW_CHECK || MEMP_OVERFLOW_CHECK 50 | /* if MEM_OVERFLOW_CHECK or MEMP_OVERFLOW_CHECK is turned on, we reserve some 51 | * bytes at the beginning and at the end of each element, initialize them as 52 | * 0xcd and check them later. 53 | * If MEM(P)_OVERFLOW_CHECK is >= 2, on every call to mem(p)_malloc or mem(p)_free, 54 | * every single element in each pool/heap is checked! 55 | * This is VERY SLOW but also very helpful. 56 | * MEM_SANITY_REGION_BEFORE and MEM_SANITY_REGION_AFTER can be overridden in 57 | * lwipopts.h to change the amount reserved for checking. */ 58 | #ifndef MEM_SANITY_REGION_BEFORE 59 | #define MEM_SANITY_REGION_BEFORE 16 60 | #endif /* MEM_SANITY_REGION_BEFORE*/ 61 | #if MEM_SANITY_REGION_BEFORE > 0 62 | #define MEM_SANITY_REGION_BEFORE_ALIGNED LWIP_MEM_ALIGN_SIZE(MEM_SANITY_REGION_BEFORE) 63 | #else 64 | #define MEM_SANITY_REGION_BEFORE_ALIGNED 0 65 | #endif /* MEM_SANITY_REGION_BEFORE*/ 66 | #ifndef MEM_SANITY_REGION_AFTER 67 | #define MEM_SANITY_REGION_AFTER 16 68 | #endif /* MEM_SANITY_REGION_AFTER*/ 69 | #if MEM_SANITY_REGION_AFTER > 0 70 | #define MEM_SANITY_REGION_AFTER_ALIGNED LWIP_MEM_ALIGN_SIZE(MEM_SANITY_REGION_AFTER) 71 | #else 72 | #define MEM_SANITY_REGION_AFTER_ALIGNED 0 73 | #endif /* MEM_SANITY_REGION_AFTER*/ 74 | 75 | void mem_overflow_init_raw(void *p, size_t size); 76 | void mem_overflow_check_raw(void *p, size_t size, const char *descr1, const char *descr2); 77 | 78 | #endif /* MEM_OVERFLOW_CHECK || MEMP_OVERFLOW_CHECK */ 79 | 80 | #ifdef __cplusplus 81 | } 82 | #endif 83 | 84 | #endif /* LWIP_HDR_MEMP_PRIV_H */ 85 | -------------------------------------------------------------------------------- /core/src/include/lwip/ip4_frag.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * IP fragmentation/reassembly 4 | */ 5 | 6 | /* 7 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, 14 | * this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 3. The name of the author may not be used to endorse or promote products 19 | * derived from this software without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 | * OF SUCH DAMAGE. 31 | * 32 | * This file is part of the lwIP TCP/IP stack. 33 | * 34 | * Author: Jani Monoses 35 | * 36 | */ 37 | 38 | #ifndef LWIP_HDR_IP4_FRAG_H 39 | #define LWIP_HDR_IP4_FRAG_H 40 | 41 | #include "lwip/opt.h" 42 | #include "lwip/err.h" 43 | #include "lwip/pbuf.h" 44 | #include "lwip/netif.h" 45 | #include "lwip/ip_addr.h" 46 | #include "lwip/ip.h" 47 | 48 | #if LWIP_IPV4 49 | 50 | #ifdef __cplusplus 51 | extern "C" { 52 | #endif 53 | 54 | #if IP_REASSEMBLY 55 | /* The IP reassembly timer interval in milliseconds. */ 56 | #define IP_TMR_INTERVAL 1000 57 | 58 | /** IP reassembly helper struct. 59 | * This is exported because memp needs to know the size. 60 | */ 61 | struct ip_reassdata { 62 | struct ip_reassdata *next; 63 | struct pbuf *p; 64 | struct ip_hdr iphdr; 65 | u16_t datagram_len; 66 | u8_t flags; 67 | u8_t timer; 68 | }; 69 | 70 | void ip_reass_init(void); 71 | void ip_reass_tmr(void); 72 | struct pbuf * ip4_reass(struct pbuf *p); 73 | #endif /* IP_REASSEMBLY */ 74 | 75 | #if IP_FRAG 76 | #if !LWIP_NETIF_TX_SINGLE_PBUF 77 | #ifndef LWIP_PBUF_CUSTOM_REF_DEFINED 78 | #define LWIP_PBUF_CUSTOM_REF_DEFINED 79 | /** A custom pbuf that holds a reference to another pbuf, which is freed 80 | * when this custom pbuf is freed. This is used to create a custom PBUF_REF 81 | * that points into the original pbuf. */ 82 | struct pbuf_custom_ref { 83 | /** 'base class' */ 84 | struct pbuf_custom pc; 85 | /** pointer to the original pbuf that is referenced */ 86 | struct pbuf *original; 87 | }; 88 | #endif /* LWIP_PBUF_CUSTOM_REF_DEFINED */ 89 | #endif /* !LWIP_NETIF_TX_SINGLE_PBUF */ 90 | 91 | err_t ip4_frag(struct pbuf *p, struct netif *netif, const ip4_addr_t *dest); 92 | #endif /* IP_FRAG */ 93 | 94 | #ifdef __cplusplus 95 | } 96 | #endif 97 | 98 | #endif /* LWIP_IPV4 */ 99 | 100 | #endif /* LWIP_HDR_IP4_FRAG_H */ 101 | -------------------------------------------------------------------------------- /core/src/include/lwip/prot/icmp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * ICMP protocol definitions 4 | */ 5 | 6 | /* 7 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright notice, 14 | * this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 3. The name of the author may not be used to endorse or promote products 19 | * derived from this software without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 | * OF SUCH DAMAGE. 31 | * 32 | * This file is part of the lwIP TCP/IP stack. 33 | * 34 | * Author: Adam Dunkels 35 | * 36 | */ 37 | #ifndef LWIP_HDR_PROT_ICMP_H 38 | #define LWIP_HDR_PROT_ICMP_H 39 | 40 | #include "lwip/arch.h" 41 | 42 | #ifdef __cplusplus 43 | extern "C" { 44 | #endif 45 | 46 | #define ICMP_ER 0 /* echo reply */ 47 | #define ICMP_DUR 3 /* destination unreachable */ 48 | #define ICMP_SQ 4 /* source quench */ 49 | #define ICMP_RD 5 /* redirect */ 50 | #define ICMP_ECHO 8 /* echo */ 51 | #define ICMP_TE 11 /* time exceeded */ 52 | #define ICMP_PP 12 /* parameter problem */ 53 | #define ICMP_TS 13 /* timestamp */ 54 | #define ICMP_TSR 14 /* timestamp reply */ 55 | #define ICMP_IRQ 15 /* information request */ 56 | #define ICMP_IR 16 /* information reply */ 57 | #define ICMP_AM 17 /* address mask request */ 58 | #define ICMP_AMR 18 /* address mask reply */ 59 | 60 | #ifdef PACK_STRUCT_USE_INCLUDES 61 | # include "arch/bpstruct.h" 62 | #endif 63 | /** This is the standard ICMP header only that the u32_t data 64 | * is split to two u16_t like ICMP echo needs it. 65 | * This header is also used for other ICMP types that do not 66 | * use the data part. 67 | */ 68 | PACK_STRUCT_BEGIN 69 | struct icmp_echo_hdr { 70 | PACK_STRUCT_FLD_8(u8_t type); 71 | PACK_STRUCT_FLD_8(u8_t code); 72 | PACK_STRUCT_FIELD(u16_t chksum); 73 | PACK_STRUCT_FIELD(u16_t id); 74 | PACK_STRUCT_FIELD(u16_t seqno); 75 | } PACK_STRUCT_STRUCT; 76 | PACK_STRUCT_END 77 | #ifdef PACK_STRUCT_USE_INCLUDES 78 | # include "arch/epstruct.h" 79 | #endif 80 | 81 | /* Compatibility defines, old versions used to combine type and code to an u16_t */ 82 | #define ICMPH_TYPE(hdr) ((hdr)->type) 83 | #define ICMPH_CODE(hdr) ((hdr)->code) 84 | #define ICMPH_TYPE_SET(hdr, t) ((hdr)->type = (t)) 85 | #define ICMPH_CODE_SET(hdr, c) ((hdr)->code = (c)) 86 | 87 | #ifdef __cplusplus 88 | } 89 | #endif 90 | 91 | #endif /* LWIP_HDR_PROT_ICMP_H */ 92 | --------------------------------------------------------------------------------