├── .gitignore ├── .travis.yml ├── LICENSE ├── Makefile ├── README.md ├── cmd └── tun2socks │ ├── main.go │ ├── main_d.go │ ├── main_debug.go │ ├── main_dns_cache.go │ ├── main_dns_fallback.go │ ├── main_echo.go │ ├── main_fakedns.go │ ├── main_redirect.go │ ├── main_shadowsocks.go │ ├── main_socks.go │ └── main_v2ray.go ├── common ├── blockdns │ ├── blockdns.go │ └── blockdns_windows.go ├── dns │ ├── cache │ │ ├── cache.go │ │ └── ip_cache.go │ ├── dns.go │ └── fakedns │ │ └── fakedns.go ├── log │ ├── log.go │ ├── logger.go │ ├── simple │ │ └── logger.go │ └── v2ray │ │ └── logger.go ├── packet │ └── packet.go ├── proc │ ├── proc_darwin.go │ ├── proc_linux.go │ ├── proc_other.go │ ├── proc_unix.go │ ├── proc_windows.go │ └── proc_windows_test.go ├── route │ ├── route_darwin.go │ ├── route_linux.go │ └── route_windows.go ├── stats │ ├── session │ │ └── session.go │ └── stats.go └── winsys │ ├── constants.go │ ├── helper.go │ ├── iphlpapi.go │ ├── syscall_windows.go │ ├── type.go │ ├── util.go │ ├── winsys.go │ └── zsyscall_windows.go ├── core ├── addr.go ├── buffer_pool.go ├── c │ ├── core │ │ ├── altcp.c │ │ ├── altcp_alloc.c │ │ ├── altcp_tcp.c │ │ ├── def.c │ │ ├── dns.c │ │ ├── inet_chksum.c │ │ ├── init.c │ │ ├── ip.c │ │ ├── ipv4 │ │ │ ├── autoip.c │ │ │ ├── dhcp.c │ │ │ ├── etharp.c │ │ │ ├── icmp.c │ │ │ ├── igmp.c │ │ │ ├── ip4.c │ │ │ ├── ip4_addr.c │ │ │ └── ip4_frag.c │ │ ├── ipv6 │ │ │ ├── dhcp6.c │ │ │ ├── ethip6.c │ │ │ ├── icmp6.c │ │ │ ├── inet6.c │ │ │ ├── ip6.c │ │ │ ├── ip6_addr.c │ │ │ ├── ip6_frag.c │ │ │ ├── mld6.c │ │ │ └── nd6.c │ │ ├── mem.c │ │ ├── memp.c │ │ ├── netif.c │ │ ├── pbuf.c │ │ ├── raw.c │ │ ├── stats.c │ │ ├── sys.c │ │ ├── tcp.c │ │ ├── tcp_in.c │ │ ├── tcp_out.c │ │ ├── timeouts.c │ │ └── udp.c │ ├── custom │ │ ├── arch │ │ │ ├── bpstruct.h │ │ │ ├── cc.h │ │ │ ├── cc_others.h │ │ │ ├── cc_windows.h │ │ │ ├── epstruct.h │ │ │ ├── perf.h │ │ │ └── sys_arch.h │ │ ├── lwipopts.h │ │ └── sys_arch.c │ └── include │ │ ├── compat │ │ ├── posix │ │ │ ├── arpa │ │ │ │ └── inet.h │ │ │ ├── net │ │ │ │ └── if.h │ │ │ ├── netdb.h │ │ │ └── sys │ │ │ │ └── socket.h │ │ └── stdc │ │ │ └── errno.h │ │ ├── lwip │ │ ├── altcp.h │ │ ├── altcp_tcp.h │ │ ├── altcp_tls.h │ │ ├── api.h │ │ ├── apps │ │ │ ├── FILES │ │ │ ├── altcp_proxyconnect.h │ │ │ ├── altcp_tls_mbedtls_opts.h │ │ │ ├── fs.h │ │ │ ├── http_client.h │ │ │ ├── httpd.h │ │ │ ├── httpd_opts.h │ │ │ ├── lwiperf.h │ │ │ ├── mdns.h │ │ │ ├── mdns_opts.h │ │ │ ├── mdns_priv.h │ │ │ ├── mqtt.h │ │ │ ├── mqtt_opts.h │ │ │ ├── mqtt_priv.h │ │ │ ├── netbiosns.h │ │ │ ├── netbiosns_opts.h │ │ │ ├── smtp.h │ │ │ ├── smtp_opts.h │ │ │ ├── snmp.h │ │ │ ├── snmp_core.h │ │ │ ├── snmp_mib2.h │ │ │ ├── snmp_opts.h │ │ │ ├── snmp_scalar.h │ │ │ ├── snmp_snmpv2_framework.h │ │ │ ├── snmp_snmpv2_usm.h │ │ │ ├── snmp_table.h │ │ │ ├── snmp_threadsync.h │ │ │ ├── snmpv3.h │ │ │ ├── sntp.h │ │ │ ├── sntp_opts.h │ │ │ ├── tftp_opts.h │ │ │ └── tftp_server.h │ │ ├── arch.h │ │ ├── autoip.h │ │ ├── debug.h │ │ ├── def.h │ │ ├── dhcp.h │ │ ├── dhcp6.h │ │ ├── dns.h │ │ ├── err.h │ │ ├── errno.h │ │ ├── etharp.h │ │ ├── ethip6.h │ │ ├── icmp.h │ │ ├── icmp6.h │ │ ├── if_api.h │ │ ├── igmp.h │ │ ├── inet.h │ │ ├── inet_chksum.h │ │ ├── init.h │ │ ├── init.h.cmake.in │ │ ├── ip.h │ │ ├── ip4.h │ │ ├── ip4_addr.h │ │ ├── ip4_frag.h │ │ ├── ip6.h │ │ ├── ip6_addr.h │ │ ├── ip6_frag.h │ │ ├── ip6_zone.h │ │ ├── ip_addr.h │ │ ├── mem.h │ │ ├── memp.h │ │ ├── mld6.h │ │ ├── nd6.h │ │ ├── netbuf.h │ │ ├── netdb.h │ │ ├── netif.h │ │ ├── netifapi.h │ │ ├── opt.h │ │ ├── pbuf.h │ │ ├── priv │ │ │ ├── altcp_priv.h │ │ │ ├── api_msg.h │ │ │ ├── mem_priv.h │ │ │ ├── memp_priv.h │ │ │ ├── memp_std.h │ │ │ ├── nd6_priv.h │ │ │ ├── raw_priv.h │ │ │ ├── sockets_priv.h │ │ │ ├── tcp_priv.h │ │ │ └── tcpip_priv.h │ │ ├── prot │ │ │ ├── autoip.h │ │ │ ├── dhcp.h │ │ │ ├── dhcp6.h │ │ │ ├── dns.h │ │ │ ├── etharp.h │ │ │ ├── ethernet.h │ │ │ ├── iana.h │ │ │ ├── icmp.h │ │ │ ├── icmp6.h │ │ │ ├── ieee.h │ │ │ ├── igmp.h │ │ │ ├── ip.h │ │ │ ├── ip4.h │ │ │ ├── ip6.h │ │ │ ├── mld6.h │ │ │ ├── nd6.h │ │ │ ├── tcp.h │ │ │ └── udp.h │ │ ├── raw.h │ │ ├── sio.h │ │ ├── snmp.h │ │ ├── sockets.h │ │ ├── stats.h │ │ ├── sys.h │ │ ├── tcp.h │ │ ├── tcpbase.h │ │ ├── tcpip.h │ │ ├── timeouts.h │ │ └── udp.h │ │ ├── netif │ │ ├── bridgeif.h │ │ ├── bridgeif_opts.h │ │ ├── etharp.h │ │ ├── ethernet.h │ │ ├── ieee802154.h │ │ ├── lowpan6.h │ │ ├── lowpan6_ble.h │ │ ├── lowpan6_common.h │ │ ├── lowpan6_opts.h │ │ ├── ppp │ │ │ ├── ccp.h │ │ │ ├── chap-md5.h │ │ │ ├── chap-new.h │ │ │ ├── chap_ms.h │ │ │ ├── eap.h │ │ │ ├── ecp.h │ │ │ ├── eui64.h │ │ │ ├── fsm.h │ │ │ ├── ipcp.h │ │ │ ├── ipv6cp.h │ │ │ ├── lcp.h │ │ │ ├── magic.h │ │ │ ├── mppe.h │ │ │ ├── polarssl │ │ │ │ ├── arc4.h │ │ │ │ ├── des.h │ │ │ │ ├── md4.h │ │ │ │ ├── md5.h │ │ │ │ └── sha1.h │ │ │ ├── ppp.h │ │ │ ├── ppp_impl.h │ │ │ ├── ppp_opts.h │ │ │ ├── pppapi.h │ │ │ ├── pppcrypt.h │ │ │ ├── pppdebug.h │ │ │ ├── pppoe.h │ │ │ ├── pppol2tp.h │ │ │ ├── pppos.h │ │ │ ├── upap.h │ │ │ └── vj.h │ │ ├── slipif.h │ │ └── zepif.h │ │ └── posix │ │ ├── errno.h │ │ ├── netdb.h │ │ └── sys │ │ └── socket.h ├── c_core.go ├── c_core_4.go ├── c_core_6.go ├── c_custom.go ├── conn.go ├── core_test.go ├── errors.go ├── handler.go ├── input.go ├── lwip.go ├── lwip_other.go ├── lwip_windows.go ├── output.go ├── output_export.go ├── tcp_callback.go ├── tcp_callback_export.go ├── tcp_conn.go ├── tcp_conn_map.go ├── udp_callback.go ├── udp_callback_export.go ├── udp_conn.go └── udp_conn_map.go ├── filter ├── filter.go ├── icmp_echo.go └── icmp_relay.go ├── go.mod ├── go.sum ├── proxy ├── d │ ├── tcp.go │ └── udp.go ├── dnsfallback │ └── udp.go ├── echo │ ├── tcp.go │ └── udp.go ├── redirect │ ├── tcp.go │ └── udp.go ├── shadowsocks │ ├── tcp.go │ └── udp.go ├── socks │ ├── socks.go │ ├── tcp.go │ └── udp.go └── v2ray │ ├── features.go │ ├── features_other.go │ ├── tcp.go │ └── udp.go ├── scripts ├── recover_route_linux.sh ├── recover_route_macos.sh ├── release.sh ├── run_socks_linux.sh └── run_socks_macos.sh └── tun ├── README.md ├── stop.go ├── tun_darwin.go ├── tun_linux.go └── tun_windows.go /.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | /tools 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | 3 | services: 4 | - docker 5 | 6 | language: go 7 | 8 | go: 9 | - 1.15 10 | 11 | before_install: 12 | - go get src.techknowlogick.com/xgo 13 | 14 | script: make mellow 15 | 16 | deploy: 17 | provider: releases 18 | api_key: 19 | secure: oOy8TAuvFfnZj9E77fr5uejmD/9Qp37uRkfQE1Unv67TwHwizt3NVxU2So5fXj62ZcOraeL+vDi/xFE433hlieZTPt5TO1MQkecIMFpo74bOw8RYRHNQk+FUQQix/thHTvAoF4sRmQSmJL26/EAu59MXpp065AvgZmvwzi2JtBxiGiSFLQMOHn3yBONgZbbGxDa1bAQKk6jicLpVR8qv4wSFiOwyj/yC+NbaVpTzAHhJNTJriJ3EE6ZWDFLRXTl8vK87vKYA4fBtkyATmlOvCX0+Qki6bjiFAx0FGOOizVF1j3wW/RHu4mnDfxQlQ1mQGdpTP1+EmKk9jE2r27ndLoZvNXMi6Jm8L5EXFJWScwYMkBxM07d6L82Z+A1jFbT1KpMAWFOvXPDPCeyIKcOfWhYAhTvfbek+N0xHxi8+rjEtnejLqIECsbETSq8pA+YIZobFffXWoSLkE6gTI56bi/Jw1nCIFiKXBHJReBV/ZeQM/pgjXMBi8ySpKmuXwcH5fA6zDYxx2u+xcn2vZX01sJ/sVoWd/oT73dnbmUZtcL+Hmu7QppP5s1F4LmlkMo8QjUqj25V0QNkttqtipmHHIJFKkmpG/hVeyS3cmgb0U86jJ/HxRO61rBYuPapjJul05+9atRR0ZTFJCmY2pj/FWNcv0riB59W5CZ5pZtLMQsM= 20 | file_glob: true 21 | file: 22 | - "build/core*" 23 | overwrite: true 24 | skip_cleanup: true 25 | on: 26 | tags: true 27 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | GOCMD=go 2 | XGOCMD=xgo 3 | GOBUILD=$(GOCMD) build -a 4 | GOCLEAN=$(GOCMD) clean 5 | VERSION=$(shell git describe --tags) 6 | DEBUG_LDFLAGS='' 7 | RELEASE_LDFLAGS='-s -w -X main.version=$(VERSION)' 8 | STATIC_LDFLAGS='-s -w -X main.version=$(VERSION) -extldflags "-static"' 9 | BUILD_TAGS?=dnscache dnsfallback fakedns stats v2ray 10 | DEBUG_BUILD_TAGS=$(BUILD_TAGS) debug 11 | BUILDDIR=$(shell pwd)/build 12 | CMDDIR=$(shell pwd)/cmd/tun2socks 13 | PROGRAM=tun2socks 14 | 15 | BUILD_CMD="cd $(CMDDIR) && $(GOBUILD) -ldflags $(RELEASE_LDFLAGS) -o $(BUILDDIR)/$(PROGRAM) -v -tags '$(BUILD_TAGS)'" 16 | DBUILD_CMD="cd $(CMDDIR) && $(GOBUILD) -race -ldflags $(DEBUG_LDFLAGS) -o $(BUILDDIR)/$(PROGRAM) -v -tags '$(DEBUG_BUILD_TAGS)'" 17 | XBUILD_CMD="cd $(BUILDDIR) && $(XGOCMD) -ldflags $(RELEASE_LDFLAGS) -tags '$(BUILD_TAGS)' --targets=*/* $(CMDDIR)" 18 | RELEASE_CMD="cd $(BUILDDIR) && $(XGOCMD) -ldflags $(RELEASE_LDFLAGS) -tags '$(BUILD_TAGS)' --targets=linux/amd64,linux/arm64,linux/386,linux/mips,linux/mipsle,linux/mips64,linux/mips64le,windows/*,darwin/* $(CMDDIR)" 19 | DARWIN_CMD="cd $(BUILDDIR) && $(XGOCMD) -out core -ldflags $(RELEASE_LDFLAGS) -tags '$(BUILD_TAGS)' --targets=darwin/amd64 $(CMDDIR)" 20 | WINDOWS_CMD="cd $(BUILDDIR) && $(XGOCMD) -out core -ldflags $(RELEASE_LDFLAGS) -tags '$(BUILD_TAGS)' --targets=windows/amd64 $(CMDDIR)" 21 | LINUX_CMD="cd $(BUILDDIR) && $(XGOCMD) -out core -ldflags $(STATIC_LDFLAGS) -tags '$(BUILD_TAGS)' --targets=linux/amd64 $(CMDDIR)" 22 | ALL_LINUX_CMD="cd $(BUILDDIR) && $(XGOCMD) -out core -ldflags $(STATIC_LDFLAGS) -tags '$(BUILD_TAGS)' --targets=linux/amd64,linux/mips,linux/mips64,linux/arm-5,linux/arm-6,linux/arm-7,linux/arm64 $(CMDDIR)" 23 | 24 | all: build 25 | 26 | build: 27 | mkdir -p $(BUILDDIR) 28 | eval $(BUILD_CMD) 29 | 30 | dbuild: 31 | mkdir -p $(BUILDDIR) 32 | eval $(DBUILD_CMD) 33 | 34 | darwin: 35 | mkdir -p $(BUILDDIR) 36 | eval $(DARWIN_CMD) 37 | 38 | windows: 39 | mkdir -p $(BUILDDIR) 40 | eval $(WINDOWS_CMD) 41 | 42 | linux: 43 | mkdir -p $(BUILDDIR) 44 | eval $(LINUX_CMD) 45 | 46 | all_linux: 47 | mkdir -p $(BUILDDIR) 48 | eval $(ALL_LINUX_CMD) 49 | 50 | xbuild: 51 | mkdir -p $(BUILDDIR) 52 | eval $(XBUILD_CMD) 53 | 54 | release: 55 | mkdir -p $(BUILDDIR) 56 | eval $(RELEASE_CMD) 57 | 58 | mellow: darwin windows all_linux 59 | 60 | travisbuild: xbuild 61 | 62 | clean: 63 | rm -rf $(BUILDDIR) 64 | 65 | cleancache: 66 | # go build cache may need to cleanup if changing C source code 67 | $(GOCLEAN) -cache 68 | rm -rf $(BUILDDIR) 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-tun2socks 2 | 3 | [![Build Status](https://travis-ci.com/eycorsican/go-tun2socks.svg?branch=master)](https://travis-ci.com/eycorsican/go-tun2socks) 4 | 5 | A tun2socks implementation written in Go. 6 | -------------------------------------------------------------------------------- /cmd/tun2socks/main_d.go: -------------------------------------------------------------------------------- 1 | // +build d 2 | 3 | package main 4 | 5 | import ( 6 | "flag" 7 | "net" 8 | "strings" 9 | 10 | "github.com/eycorsican/go-tun2socks/common/log" 11 | "github.com/eycorsican/go-tun2socks/core" 12 | "github.com/eycorsican/go-tun2socks/proxy/d" 13 | "github.com/eycorsican/go-tun2socks/proxy/socks" 14 | ) 15 | 16 | func init() { 17 | args.addFlag(fProxyServer) 18 | args.addFlag(fUdpTimeout) 19 | args.addFlag(fStats) 20 | 21 | args.ExceptionApps = flag.String("exceptionApps", "", "A list of exception apps separated by commas") 22 | args.ExceptionSendThrough = flag.String("exceptionSendThrough", "192.168.1.101:0", "Exception send through address") 23 | 24 | registerHandlerCreater("d", func() { 25 | // Verify proxy server address. 26 | proxyAddr, err := net.ResolveTCPAddr("tcp", *args.ProxyServer) 27 | if err != nil { 28 | log.Fatalf("invalid proxy server address: %v", err) 29 | } 30 | proxyHost := proxyAddr.IP.String() 31 | proxyPort := uint16(proxyAddr.Port) 32 | 33 | proxyTCPHandler := socks.NewTCPHandler(proxyHost, proxyPort, fakeDns, sessionStater) 34 | proxyUDPHandler := socks.NewUDPHandler(proxyHost, proxyPort, *args.UdpTimeout, dnsCache, fakeDns, sessionStater) 35 | 36 | sendThrough, err := net.ResolveTCPAddr("tcp", *args.ExceptionSendThrough) 37 | if err != nil { 38 | log.Fatalf("invalid exception send through address: %v", err) 39 | } 40 | apps := strings.Split(*args.ExceptionApps, ",") 41 | tcpHandler := d.NewTCPHandler(proxyTCPHandler, apps, sendThrough) 42 | udpHandler := d.NewUDPHandler(proxyUDPHandler, apps, sendThrough, *args.UdpTimeout) 43 | 44 | core.RegisterTCPConnHandler(tcpHandler) 45 | core.RegisterUDPConnHandler(udpHandler) 46 | }) 47 | } 48 | -------------------------------------------------------------------------------- /cmd/tun2socks/main_debug.go: -------------------------------------------------------------------------------- 1 | // +build debug 2 | 3 | // To view all available profiles, open http://localhost:6060/debug/pprof/ in your browser. 4 | 5 | package main 6 | 7 | import ( 8 | "net/http" 9 | _ "net/http/pprof" 10 | "runtime/debug" 11 | ) 12 | 13 | func init() { 14 | // cgo calls will consume more system threads, better keep an eye on that. 15 | debug.SetMaxThreads(35) 16 | 17 | go func() { 18 | http.ListenAndServe("0.0.0.0:6060", nil) 19 | }() 20 | } 21 | -------------------------------------------------------------------------------- /cmd/tun2socks/main_dns_cache.go: -------------------------------------------------------------------------------- 1 | // +build dnscache 2 | 3 | package main 4 | 5 | import ( 6 | "flag" 7 | 8 | "github.com/eycorsican/go-tun2socks/common/dns/cache" 9 | ) 10 | 11 | func init() { 12 | args.DisableDnsCache = flag.Bool("disableDNSCache", false, "Disable DNS cache") 13 | 14 | addPostFlagsInitFn(func() { 15 | if *args.DisableDnsCache { 16 | dnsCache = nil 17 | } else { 18 | dnsCache = cache.NewSimpleDnsCache() 19 | } 20 | }) 21 | } 22 | -------------------------------------------------------------------------------- /cmd/tun2socks/main_dns_fallback.go: -------------------------------------------------------------------------------- 1 | // +build dnsfallback 2 | 3 | package main 4 | 5 | import ( 6 | "flag" 7 | 8 | "github.com/eycorsican/go-tun2socks/core" 9 | "github.com/eycorsican/go-tun2socks/proxy/dnsfallback" 10 | ) 11 | 12 | func init() { 13 | args.DnsFallback = flag.Bool("dnsFallback", false, "Enable DNS fallback over TCP (overrides the UDP proxy handler).") 14 | 15 | registerHandlerCreater("dnsfallback", func() { 16 | core.RegisterUDPConnHandler(dnsfallback.NewUDPHandler()) 17 | }) 18 | } 19 | -------------------------------------------------------------------------------- /cmd/tun2socks/main_echo.go: -------------------------------------------------------------------------------- 1 | // +build echo 2 | 3 | package main 4 | 5 | import ( 6 | "github.com/eycorsican/go-tun2socks/core" 7 | "github.com/eycorsican/go-tun2socks/proxy/echo" 8 | ) 9 | 10 | func init() { 11 | registerHandlerCreater("echo", func() { 12 | core.RegisterTCPConnHandler(echo.NewTCPHandler()) 13 | core.RegisterUDPConnHandler(echo.NewUDPHandler()) 14 | }) 15 | } 16 | -------------------------------------------------------------------------------- /cmd/tun2socks/main_fakedns.go: -------------------------------------------------------------------------------- 1 | // +build fakedns 2 | 3 | package main 4 | 5 | import ( 6 | "flag" 7 | "strings" 8 | 9 | "github.com/eycorsican/go-tun2socks/common/dns/fakedns" 10 | "github.com/eycorsican/go-tun2socks/common/log" 11 | ) 12 | 13 | func init() { 14 | args.EnableFakeDns = flag.Bool("fakeDns", false, "Enable Fake DNS") 15 | args.FakeDnsMinIP = flag.String("fakeDnsMinIP", "172.30.0.0", "Minimum fake IP used by Fake DNS") 16 | args.FakeDnsMaxIP = flag.String("fakeDnsMaxIP", "172.30.16.255", "Maximum fake IP used by Fake DNS") 17 | args.FakeDnsCacheDir = flag.String("fakeDnsCacheDir", "", "Cache directory used by Fake DNS") 18 | args.FakeDnsExcludeDomains = flag.String("fakeDnsExcludes", "", "A domain keyword list seperated by comma to exclude domains from Fake DNS") 19 | 20 | addPostFlagsInitFn(func() { 21 | if *args.EnableFakeDns { 22 | excludes := strings.Split(*args.FakeDnsExcludeDomains, ",") 23 | var filters []string 24 | for _, filter := range excludes { 25 | filter = strings.TrimSpace(filter) 26 | if len(filter) == 0 { 27 | continue 28 | } 29 | filters = append(filters, filter) 30 | } 31 | fakeDns = fakedns.NewSimpleFakeDns(*args.FakeDnsMinIP, *args.FakeDnsMaxIP, *args.FakeDnsCacheDir, filters) 32 | err := fakeDns.Start() 33 | if err != nil { 34 | log.Errorf("Error starting Fake DNS: %v", err) 35 | } 36 | } else { 37 | fakeDns = nil 38 | } 39 | }) 40 | } 41 | -------------------------------------------------------------------------------- /cmd/tun2socks/main_redirect.go: -------------------------------------------------------------------------------- 1 | // +build redirect 2 | 3 | package main 4 | 5 | import ( 6 | "github.com/eycorsican/go-tun2socks/core" 7 | "github.com/eycorsican/go-tun2socks/proxy/redirect" 8 | ) 9 | 10 | func init() { 11 | args.addFlag(fProxyServer) 12 | args.addFlag(fUdpTimeout) 13 | 14 | registerHandlerCreater("redirect", func() { 15 | core.RegisterTCPConnHandler(redirect.NewTCPHandler(*args.ProxyServer)) 16 | core.RegisterUDPConnHandler(redirect.NewUDPHandler(*args.ProxyServer, *args.UdpTimeout)) 17 | }) 18 | } 19 | -------------------------------------------------------------------------------- /cmd/tun2socks/main_shadowsocks.go: -------------------------------------------------------------------------------- 1 | // +build shadowsocks 2 | 3 | package main 4 | 5 | import ( 6 | "flag" 7 | "net" 8 | "strings" 9 | 10 | sscore "github.com/shadowsocks/go-shadowsocks2/core" 11 | 12 | "github.com/eycorsican/go-tun2socks/common/log" 13 | "github.com/eycorsican/go-tun2socks/core" 14 | "github.com/eycorsican/go-tun2socks/proxy/shadowsocks" 15 | ) 16 | 17 | func init() { 18 | args.addFlag(fProxyServer) 19 | args.addFlag(fUdpTimeout) 20 | 21 | args.ProxyCipher = flag.String("proxyCipher", "AEAD_CHACHA20_POLY1305", "Cipher used for Shadowsocks proxy, available ciphers: "+strings.Join(sscore.ListCipher(), " ")) 22 | args.ProxyPassword = flag.String("proxyPassword", "", "Password used for Shadowsocks proxy") 23 | 24 | registerHandlerCreater("shadowsocks", func() { 25 | // Verify proxy server address. 26 | proxyAddr, err := net.ResolveTCPAddr("tcp", *args.ProxyServer) 27 | if err != nil { 28 | log.Fatalf("invalid proxy server address: %v", err) 29 | } 30 | proxyHost := proxyAddr.IP.String() 31 | proxyPort := uint16(proxyAddr.Port) 32 | 33 | if *args.ProxyCipher == "" || *args.ProxyPassword == "" { 34 | log.Fatalf("invalid cipher or password") 35 | } 36 | core.RegisterTCPConnHandler(shadowsocks.NewTCPHandler(core.ParseTCPAddr(proxyHost, proxyPort).String(), *args.ProxyCipher, *args.ProxyPassword, fakeDns)) 37 | core.RegisterUDPConnHandler(shadowsocks.NewUDPHandler(core.ParseUDPAddr(proxyHost, proxyPort).String(), *args.ProxyCipher, *args.ProxyPassword, *args.UdpTimeout, dnsCache, fakeDns)) 38 | }) 39 | } 40 | -------------------------------------------------------------------------------- /cmd/tun2socks/main_socks.go: -------------------------------------------------------------------------------- 1 | // +build socks 2 | 3 | package main 4 | 5 | import ( 6 | "net" 7 | 8 | "github.com/eycorsican/go-tun2socks/common/log" 9 | "github.com/eycorsican/go-tun2socks/core" 10 | "github.com/eycorsican/go-tun2socks/proxy/socks" 11 | ) 12 | 13 | func init() { 14 | args.addFlag(fProxyServer) 15 | args.addFlag(fUdpTimeout) 16 | args.addFlag(fStats) 17 | 18 | registerHandlerCreater("socks", func() { 19 | // Verify proxy server address. 20 | proxyAddr, err := net.ResolveTCPAddr("tcp", *args.ProxyServer) 21 | if err != nil { 22 | log.Fatalf("invalid proxy server address: %v", err) 23 | } 24 | proxyHost := proxyAddr.IP.String() 25 | proxyPort := uint16(proxyAddr.Port) 26 | 27 | core.RegisterTCPConnHandler(socks.NewTCPHandler(proxyHost, proxyPort, fakeDns, sessionStater)) 28 | core.RegisterUDPConnHandler(socks.NewUDPHandler(proxyHost, proxyPort, *args.UdpTimeout, dnsCache, fakeDns, sessionStater)) 29 | }) 30 | } 31 | -------------------------------------------------------------------------------- /common/blockdns/blockdns.go: -------------------------------------------------------------------------------- 1 | // +build !windows 2 | 3 | package blockdns 4 | 5 | import ( 6 | "errors" 7 | ) 8 | 9 | func FixDnsLeakage(tunName string) error { 10 | return errors.New("not implemented") 11 | } 12 | -------------------------------------------------------------------------------- /common/dns/cache/cache.go: -------------------------------------------------------------------------------- 1 | // This file is copied from https://github.com/yinghuocho/gotun2socks/blob/master/udp.go 2 | 3 | package cache 4 | 5 | import ( 6 | "sync" 7 | "time" 8 | 9 | "github.com/miekg/dns" 10 | 11 | cdns "github.com/eycorsican/go-tun2socks/common/dns" 12 | "github.com/eycorsican/go-tun2socks/common/log" 13 | ) 14 | 15 | const minCleanupInterval = 5 * time.Minute 16 | 17 | type dnsCacheEntry struct { 18 | msg []byte 19 | exp time.Time 20 | } 21 | 22 | type simpleDnsCache struct { 23 | mutex sync.Mutex 24 | storage map[string]*dnsCacheEntry 25 | lastCleanup time.Time 26 | } 27 | 28 | func NewSimpleDnsCache() cdns.DnsCache { 29 | return &simpleDnsCache{ 30 | storage: make(map[string]*dnsCacheEntry), 31 | lastCleanup: time.Now(), 32 | } 33 | } 34 | 35 | func packUint16(i uint16) []byte { return []byte{byte(i >> 8), byte(i)} } 36 | 37 | func cacheKey(q dns.Question) string { 38 | return string(append([]byte(q.Name), packUint16(q.Qtype)...)) 39 | } 40 | 41 | func (c *simpleDnsCache) cleanup() { 42 | newStorage := make(map[string]*dnsCacheEntry) 43 | log.Debugf("cleaning up dns %v cache entries", len(c.storage)) 44 | for key, entry := range c.storage { 45 | if time.Now().Before(entry.exp) { 46 | newStorage[key] = entry 47 | } 48 | } 49 | c.storage = newStorage 50 | log.Debugf("cleanup done, remaining %v entries", len(c.storage)) 51 | } 52 | 53 | func (c *simpleDnsCache) Query(payload []byte) []byte { 54 | request := new(dns.Msg) 55 | e := request.Unpack(payload) 56 | if e != nil { 57 | return nil 58 | } 59 | if len(request.Question) == 0 { 60 | return nil 61 | } 62 | 63 | c.mutex.Lock() 64 | defer c.mutex.Unlock() 65 | key := cacheKey(request.Question[0]) 66 | entry := c.storage[key] 67 | if entry == nil { 68 | return nil 69 | } 70 | if time.Now().After(entry.exp) { 71 | delete(c.storage, key) 72 | return nil 73 | } 74 | 75 | resp := new(dns.Msg) 76 | resp.Unpack(entry.msg) 77 | resp.Id = request.Id 78 | var buf [1024]byte 79 | dnsAnswer, err := resp.PackBuffer(buf[:]) 80 | if err != nil { 81 | return nil 82 | } 83 | log.Debugf("got dns answer from cache with key: %v", key) 84 | return append([]byte(nil), dnsAnswer...) 85 | } 86 | 87 | func (c *simpleDnsCache) Store(payload []byte) { 88 | resp := new(dns.Msg) 89 | e := resp.Unpack(payload) 90 | if e != nil { 91 | return 92 | } 93 | if resp.Rcode != dns.RcodeSuccess { 94 | return 95 | } 96 | if len(resp.Question) == 0 || len(resp.Answer) == 0 { 97 | return 98 | } 99 | 100 | c.mutex.Lock() 101 | defer c.mutex.Unlock() 102 | key := cacheKey(resp.Question[0]) 103 | c.storage[key] = &dnsCacheEntry{ 104 | msg: payload, 105 | exp: time.Now().Add(time.Duration(resp.Answer[0].Header().Ttl) * time.Second), 106 | } 107 | log.Debugf("stored dns answer with key: %v, ttl: %v sec", key, resp.Answer[0].Header().Ttl) 108 | 109 | now := time.Now() 110 | if now.Sub(c.lastCleanup) > minCleanupInterval { 111 | c.cleanup() 112 | c.lastCleanup = now 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /common/dns/cache/ip_cache.go: -------------------------------------------------------------------------------- 1 | package cache 2 | 3 | import ( 4 | "net" 5 | "sync" 6 | "time" 7 | 8 | cdns "github.com/eycorsican/go-tun2socks/common/dns" 9 | "github.com/eycorsican/go-tun2socks/common/log" 10 | ) 11 | 12 | type ipCacheEntry struct { 13 | ips []net.IP 14 | exp time.Time 15 | } 16 | 17 | type simpleIPCache struct { 18 | mutex sync.Mutex 19 | storage map[string]*ipCacheEntry 20 | lastCleanup time.Time 21 | } 22 | 23 | func NewSimpleIPCache() cdns.IPCache { 24 | return &simpleIPCache{ 25 | storage: make(map[string]*ipCacheEntry), 26 | lastCleanup: time.Now(), 27 | } 28 | } 29 | 30 | func (c *simpleIPCache) cleanup() { 31 | newStorage := make(map[string]*ipCacheEntry) 32 | log.Debugf("cleaning up dns %v cache entries", len(c.storage)) 33 | for key, entry := range c.storage { 34 | if time.Now().Before(entry.exp) { 35 | newStorage[key] = entry 36 | } 37 | } 38 | c.storage = newStorage 39 | log.Debugf("cleanup done, remaining %v entries", len(c.storage)) 40 | } 41 | 42 | func (c *simpleIPCache) Query(domain string) []net.IP { 43 | c.mutex.Lock() 44 | defer c.mutex.Unlock() 45 | 46 | entry := c.storage[domain] 47 | if entry == nil { 48 | return nil 49 | } 50 | if time.Now().After(entry.exp) { 51 | delete(c.storage, domain) 52 | return nil 53 | } 54 | 55 | log.Debugf("returning %v ips for %v", len(entry.ips), domain) 56 | return entry.ips 57 | } 58 | 59 | func (c *simpleIPCache) Store(domain string, ips []net.IP, ttl uint32) { 60 | c.mutex.Lock() 61 | defer c.mutex.Unlock() 62 | 63 | c.storage[domain] = &ipCacheEntry{ 64 | ips: ips, 65 | exp: time.Now().Add(time.Duration(ttl) * time.Second), 66 | } 67 | log.Debugf("cache %v ips for %v, ttl: %v", len(ips), domain, ttl) 68 | 69 | now := time.Now() 70 | if now.Sub(c.lastCleanup) > minCleanupInterval { 71 | c.cleanup() 72 | c.lastCleanup = now 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /common/dns/dns.go: -------------------------------------------------------------------------------- 1 | package dns 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | 7 | "github.com/miekg/dns" 8 | ) 9 | 10 | const COMMON_DNS_PORT = 53 11 | 12 | type DnsCache interface { 13 | // Query queries the response for the DNS request with payload `p`, 14 | // the response data should be a valid DNS response payload. 15 | Query(p []byte) []byte 16 | 17 | // Store stores the DNS response with payload `p` to the cache. 18 | Store(p []byte) 19 | } 20 | 21 | type IPCache interface { 22 | Query(string) []net.IP 23 | Store(string, []net.IP, uint32) 24 | } 25 | 26 | type FakeDns interface { 27 | Start() error 28 | Stop() error 29 | 30 | // GenerateFakeResponse generates a fake dns response for the specify request. 31 | GenerateFakeResponse(request []byte) ([]byte, error) 32 | 33 | // QueryDomain returns the corresponding domain for the given IP. 34 | QueryDomain(ip net.IP) string 35 | 36 | // IsFakeIP checks if the given ip is a fake IP. 37 | IsFakeIP(ip net.IP) bool 38 | } 39 | 40 | func ParseDNSQuery(p []byte) (string, string, error) { 41 | req := new(dns.Msg) 42 | err := req.Unpack(p) 43 | if err != nil { 44 | return "", "", fmt.Errorf("unpack message failed") 45 | } 46 | if len(req.Question) != 1 { 47 | return "", "", fmt.Errorf("multi-question") 48 | } 49 | var qt string 50 | qtype := req.Question[0].Qtype 51 | switch qtype { 52 | case dns.TypeA: 53 | qt = "TypeA" 54 | case dns.TypeAAAA: 55 | qt = "TypeAAAA" 56 | default: 57 | return "", "", fmt.Errorf("wrong query type") 58 | } 59 | qclass := req.Question[0].Qclass 60 | if qclass != dns.ClassINET { 61 | return "", "", fmt.Errorf("wrong query class") 62 | } 63 | fqdn := req.Question[0].Name 64 | domain := fqdn[:len(fqdn)-1] 65 | if _, ok := dns.IsDomainName(domain); !ok { 66 | return "", "", fmt.Errorf("invalid domain") 67 | } 68 | return qt, domain, nil 69 | } 70 | -------------------------------------------------------------------------------- /common/log/log.go: -------------------------------------------------------------------------------- 1 | package log 2 | 3 | import ( 4 | "net" 5 | "strconv" 6 | 7 | "github.com/eycorsican/go-tun2socks/common/proc" 8 | ) 9 | 10 | var logger Logger 11 | 12 | func RegisterLogger(l Logger) { 13 | logger = l 14 | } 15 | 16 | func SetLevel(level LogLevel) { 17 | if logger != nil { 18 | logger.SetLevel(level) 19 | } 20 | } 21 | 22 | func Debugf(msg string, args ...interface{}) { 23 | if logger != nil { 24 | logger.Debugf(msg, args...) 25 | } 26 | } 27 | 28 | func Infof(msg string, args ...interface{}) { 29 | if logger != nil { 30 | logger.Infof(msg, args...) 31 | } 32 | } 33 | 34 | func Warnf(msg string, args ...interface{}) { 35 | if logger != nil { 36 | logger.Warnf(msg, args...) 37 | } 38 | } 39 | 40 | func Errorf(msg string, args ...interface{}) { 41 | if logger != nil { 42 | logger.Errorf(msg, args...) 43 | } 44 | } 45 | 46 | func Fatalf(msg string, args ...interface{}) { 47 | if logger != nil { 48 | logger.Fatalf(msg, args...) 49 | } 50 | } 51 | 52 | func Access(process, outbound, network, localAddr, target string) { 53 | var err error 54 | if process == "" { 55 | localHost, localPortStr, _ := net.SplitHostPort(localAddr) 56 | localPortInt, _ := strconv.Atoi(localPortStr) 57 | process, err = proc.GetCommandNameBySocket(network, localHost, uint16(localPortInt)) 58 | if err != nil { 59 | process = "unknown process" 60 | } 61 | } 62 | Infof("[%v] [%v] [%v] %s", outbound, network, process, target) 63 | } 64 | -------------------------------------------------------------------------------- /common/log/logger.go: -------------------------------------------------------------------------------- 1 | package log 2 | 3 | type LogLevel uint8 4 | 5 | const ( 6 | DEBUG LogLevel = iota 7 | INFO 8 | WARN 9 | ERROR 10 | NONE 11 | ) 12 | 13 | type Logger interface { 14 | SetLevel(level LogLevel) 15 | Debugf(msg string, args ...interface{}) 16 | Infof(msg string, args ...interface{}) 17 | Warnf(msg string, args ...interface{}) 18 | Errorf(msg string, args ...interface{}) 19 | Fatalf(msg string, args ...interface{}) 20 | } 21 | -------------------------------------------------------------------------------- /common/log/simple/logger.go: -------------------------------------------------------------------------------- 1 | package simple 2 | 3 | import ( 4 | golog "log" 5 | 6 | "github.com/eycorsican/go-tun2socks/common/log" 7 | ) 8 | 9 | type simpleLogger struct { 10 | level log.LogLevel 11 | } 12 | 13 | func NewSimpleLogger() log.Logger { 14 | return &simpleLogger{log.INFO} 15 | } 16 | 17 | func (l *simpleLogger) SetLevel(level log.LogLevel) { 18 | l.level = level 19 | } 20 | 21 | func (l *simpleLogger) Debugf(msg string, args ...interface{}) { 22 | if l.level <= log.DEBUG { 23 | l.output(msg, args...) 24 | } 25 | } 26 | 27 | func (l *simpleLogger) Infof(msg string, args ...interface{}) { 28 | if l.level <= log.INFO { 29 | l.output(msg, args...) 30 | } 31 | } 32 | 33 | func (l *simpleLogger) Warnf(msg string, args ...interface{}) { 34 | if l.level <= log.WARN { 35 | l.output(msg, args...) 36 | } 37 | } 38 | 39 | func (l *simpleLogger) Errorf(msg string, args ...interface{}) { 40 | if l.level <= log.ERROR { 41 | l.output(msg, args...) 42 | } 43 | } 44 | 45 | func (l *simpleLogger) Fatalf(msg string, args ...interface{}) { 46 | golog.Fatalf(msg, args...) 47 | } 48 | 49 | func (l *simpleLogger) output(msg string, args ...interface{}) { 50 | golog.Printf(msg, args...) 51 | } 52 | 53 | func init() { 54 | log.RegisterLogger(NewSimpleLogger()) 55 | } 56 | -------------------------------------------------------------------------------- /common/log/v2ray/logger.go: -------------------------------------------------------------------------------- 1 | package v2ray 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | 7 | vlog "v2ray.com/core/common/log" 8 | 9 | "github.com/eycorsican/go-tun2socks/common/log" 10 | ) 11 | 12 | type v2rayLogger struct { 13 | handler vlog.Handler 14 | level vlog.Severity 15 | } 16 | 17 | func NewV2RayLogger() log.Logger { 18 | handler := vlog.NewLogger(vlog.CreateStdoutLogWriter()) 19 | return &v2rayLogger{handler: handler, level: vlog.Severity_Info} 20 | } 21 | 22 | func (l *v2rayLogger) SetLevel(level log.LogLevel) { 23 | switch level { 24 | case log.DEBUG: 25 | l.level = vlog.Severity_Debug 26 | case log.INFO: 27 | l.level = vlog.Severity_Info 28 | case log.WARN: 29 | l.level = vlog.Severity_Warning 30 | case log.ERROR: 31 | l.level = vlog.Severity_Error 32 | case log.NONE: 33 | l.level = vlog.Severity_Unknown 34 | } 35 | } 36 | 37 | func (l *v2rayLogger) Debugf(msg string, args ...interface{}) { 38 | l.output(vlog.Severity_Debug, msg, args...) 39 | } 40 | 41 | func (l *v2rayLogger) Infof(msg string, args ...interface{}) { 42 | l.output(vlog.Severity_Info, msg, args...) 43 | } 44 | 45 | func (l *v2rayLogger) Warnf(msg string, args ...interface{}) { 46 | l.output(vlog.Severity_Warning, msg, args...) 47 | } 48 | 49 | func (l *v2rayLogger) Errorf(msg string, args ...interface{}) { 50 | l.output(vlog.Severity_Error, msg, args...) 51 | } 52 | 53 | func (l *v2rayLogger) Fatalf(msg string, args ...interface{}) { 54 | l.output(vlog.Severity_Unknown, msg, args...) 55 | os.Exit(1) 56 | } 57 | 58 | func (l *v2rayLogger) output(level vlog.Severity, msg string, args ...interface{}) { 59 | if level <= l.level { 60 | l.handler.Handle(&vlog.GeneralMessage{ 61 | Severity: level, 62 | Content: fmt.Sprintf(msg, args...), 63 | }) 64 | } 65 | } 66 | 67 | func init() { 68 | log.RegisterLogger(NewV2RayLogger()) 69 | } 70 | -------------------------------------------------------------------------------- /common/packet/packet.go: -------------------------------------------------------------------------------- 1 | package packet 2 | 3 | import ( 4 | "encoding/binary" 5 | "net" 6 | ) 7 | 8 | const ( 9 | IPVERSION_4 = 4 10 | IPVERSION_6 = 6 11 | 12 | PROTOCOL_ICMP = 1 13 | PROTOCOL_TCP = 6 14 | PROTOCOL_UDP = 17 15 | ) 16 | 17 | func PeekIPVersion(data []byte) uint8 { 18 | return uint8((data[0] & 0xf0) >> 4) 19 | } 20 | 21 | func PeekProtocol(data []byte) string { 22 | switch uint8(data[9]) { 23 | case PROTOCOL_ICMP: 24 | return "icmp" 25 | case PROTOCOL_TCP: 26 | return "tcp" 27 | case PROTOCOL_UDP: 28 | return "udp" 29 | default: 30 | return "unknown" 31 | } 32 | } 33 | 34 | func PeekSourceAddress(data []byte) net.IP { 35 | return net.IP(data[12:16]) 36 | } 37 | 38 | func PeekSourcePort(data []byte) uint16 { 39 | ihl := uint8(data[0] & 0x0f) 40 | return binary.BigEndian.Uint16(data[ihl*4 : ihl*4+2]) 41 | } 42 | 43 | func PeekDestinationAddress(data []byte) net.IP { 44 | return net.IP(data[16:20]) 45 | } 46 | 47 | func PeekDestinationPort(data []byte) uint16 { 48 | ihl := uint8(data[0] & 0x0f) 49 | return binary.BigEndian.Uint16(data[ihl*4+2 : ihl*4+4]) 50 | } 51 | 52 | func IsSYNSegment(data []byte) bool { 53 | ihl := uint8(data[0] & 0x0f) 54 | if uint8(data[ihl*4+13]&(1<<1)) == 0 { 55 | return false 56 | } else { 57 | return true 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /common/proc/proc_other.go: -------------------------------------------------------------------------------- 1 | // +build ios android 2 | 3 | package proc 4 | 5 | import ( 6 | "errors" 7 | ) 8 | 9 | func GetCommandNameBySocket(network string, addr string, port uint16) (string, error) { 10 | return "", errors.New("not implemented") 11 | } 12 | -------------------------------------------------------------------------------- /common/proc/proc_windows_test.go: -------------------------------------------------------------------------------- 1 | package proc 2 | 3 | import ( 4 | "net" 5 | "strconv" 6 | "testing" 7 | ) 8 | 9 | func TestGetCommandBySocket(t *testing.T) { 10 | conn, err := net.Dial("tcp", "114.114.114.114:53") 11 | if err != nil { 12 | t.Errorf("failed to dial target: %v", err) 13 | } 14 | laddr := conn.LocalAddr() 15 | host, port, err := net.SplitHostPort(laddr.String()) 16 | portInt, err := strconv.Atoi(port) 17 | if err != nil { 18 | t.Errorf("invalid port: %v", err) 19 | } 20 | _, err := GetProcessesBySocket(laddr.Network(), host, uint16(portInt)) 21 | if err != nil { 22 | t.Errorf("get command failed: %v", err) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /common/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 | -------------------------------------------------------------------------------- /common/route/route_linux.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("ip", "route", "add", dest+"/32", "via", gateway).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 | -------------------------------------------------------------------------------- /common/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 | -------------------------------------------------------------------------------- /common/stats/stats.go: -------------------------------------------------------------------------------- 1 | package stats 2 | 3 | import ( 4 | "sync/atomic" 5 | "time" 6 | ) 7 | 8 | type SessionStater interface { 9 | Start() error 10 | Stop() error 11 | AddSession(key interface{}, session *Session) 12 | GetSession(key interface{}) *Session 13 | RemoveSession(key interface{}) 14 | } 15 | 16 | type Session struct { 17 | UploadBytes int64 `json:"uploadBytes"` 18 | DownloadBytes int64 `json:"downloadBytes"` 19 | 20 | // Process list including current process and its parents. 21 | Processes []string `json:"processes"` 22 | 23 | Network string `json:"network"` 24 | LocalAddr string `json:"localAddr"` 25 | RemoteAddr string `json:"remoteAddr"` 26 | SessionStart time.Time `json:"sessionStart"` 27 | SessionEnd time.Time `json:"sessionEnd"` 28 | Extra string `json:"extra"` 29 | OutboundTag string `json:"outboundTag"` 30 | 31 | FirstChunkReceived bool `json:"-"` 32 | FirstChunkReceive time.Time `json:"-"` 33 | FirstChunkDuration string `json:"firstChunkDuration"` 34 | } 35 | 36 | func (s *Session) handleFirstChunk() { 37 | if !s.FirstChunkReceived { 38 | s.FirstChunkReceived = true 39 | s.FirstChunkReceive = time.Now() 40 | } 41 | } 42 | 43 | func (s *Session) AddUploadBytes(n int64) { 44 | atomic.AddInt64(&s.UploadBytes, n) 45 | } 46 | 47 | func (s *Session) AddDownloadBytes(n int64) { 48 | s.handleFirstChunk() 49 | atomic.AddInt64(&s.DownloadBytes, n) 50 | } 51 | -------------------------------------------------------------------------------- /common/winsys/helper.go: -------------------------------------------------------------------------------- 1 | // +build windows 2 | 3 | package winsys 4 | 5 | import ( 6 | "os" 7 | "unsafe" 8 | 9 | "golang.org/x/sys/windows" 10 | ) 11 | 12 | func CreateDisplayData(name, description string) (*FWPM_DISPLAY_DATA0, error) { 13 | namePtr, err := windows.UTF16PtrFromString(name) 14 | if err != nil { 15 | return nil, err 16 | } 17 | 18 | descriptionPtr, err := windows.UTF16PtrFromString(description) 19 | if err != nil { 20 | return nil, err 21 | } 22 | 23 | return &FWPM_DISPLAY_DATA0{ 24 | Name: namePtr, 25 | Description: descriptionPtr, 26 | }, nil 27 | } 28 | 29 | func GetCurrentProcessAppID() (*FWP_BYTE_BLOB, error) { 30 | currentFile, err := os.Executable() 31 | if err != nil { 32 | return nil, err 33 | } 34 | 35 | curFilePtr, err := windows.UTF16PtrFromString(currentFile) 36 | if err != nil { 37 | return nil, err 38 | } 39 | 40 | var appID *FWP_BYTE_BLOB 41 | err = FwpmGetAppIdFromFileName0(curFilePtr, unsafe.Pointer(&appID)) 42 | if err != nil { 43 | return nil, err 44 | } 45 | return appID, nil 46 | } 47 | -------------------------------------------------------------------------------- /common/winsys/iphlpapi.go: -------------------------------------------------------------------------------- 1 | // +build windows 2 | 3 | package winsys 4 | 5 | import ( 6 | "syscall" 7 | "unsafe" 8 | ) 9 | 10 | var ( 11 | iphlpapi = syscall.NewLazyDLL("iphlpapi.dll") 12 | 13 | procGetTcpStatistics = iphlpapi.NewProc("GetTcpStatistics") 14 | procGetExtendedTcpTable = iphlpapi.NewProc("GetExtendedTcpTable") 15 | procGetExtendedUdpTable = iphlpapi.NewProc("GetExtendedUdpTable") 16 | procGetBestRoute = iphlpapi.NewProc("GetBestRoute") 17 | procGetIpForwardTable = iphlpapi.NewProc("GetIpForwardTable") 18 | procGetInterfaceInfo = iphlpapi.NewProc("GetInterfaceInfo") 19 | procGetIfTable = iphlpapi.NewProc("GetIfTable") 20 | procDeleteIpForwardEntry = iphlpapi.NewProc("DeleteIpForwardEntry") 21 | procCreateIpForwardEntry = iphlpapi.NewProc("CreateIpForwardEntry") 22 | ) 23 | 24 | func GetTcpStatistics(statistics *MIB_TCPSTATS) int { 25 | ret, _, _ := procGetTcpStatistics.Call( 26 | uintptr(unsafe.Pointer(statistics)), 27 | ) 28 | return int(ret) 29 | } 30 | 31 | func GetExtendedTcpTable(tcpTable uintptr, size *uint32, order int32, af uint32, tableClass TCP_TABLE_CLASS) int { 32 | ret, _, _ := procGetExtendedTcpTable.Call( 33 | tcpTable, 34 | uintptr(unsafe.Pointer(size)), 35 | uintptr(order), 36 | uintptr(af), 37 | uintptr(tableClass), 38 | 0, 39 | ) 40 | return int(ret) 41 | } 42 | 43 | func GetExtendedUdpTable(udpTable uintptr, size *uint32, order int32, af uint32, tableClass UDP_TABLE_CLASS) int { 44 | ret, _, _ := procGetExtendedUdpTable.Call( 45 | udpTable, 46 | uintptr(unsafe.Pointer(size)), 47 | uintptr(order), 48 | uintptr(af), 49 | uintptr(tableClass), 50 | 0, 51 | ) 52 | return int(ret) 53 | } 54 | 55 | func GetBestRoute(destAddr, sourceAddr uint32, bestRoute *MIB_IPFORWARDROW) int { 56 | ret, _, _ := procGetBestRoute.Call( 57 | uintptr(destAddr), 58 | uintptr(sourceAddr), 59 | uintptr(unsafe.Pointer(bestRoute)), 60 | ) 61 | return int(ret) 62 | } 63 | 64 | func GetIpForwardTable(table *MIB_IPFORWARDTABLE, size *uint32, order int32) int { 65 | ret, _, _ := procGetIpForwardTable.Call( 66 | uintptr(unsafe.Pointer(table)), 67 | uintptr(unsafe.Pointer(size)), 68 | uintptr(order), 69 | ) 70 | return int(ret) 71 | } 72 | 73 | func GetInterfaceInfo(ifTable *IP_INTERFACE_INFO, outBufLen *uint32) int { 74 | ret, _, _ := procGetInterfaceInfo.Call( 75 | uintptr(unsafe.Pointer(ifTable)), 76 | uintptr(unsafe.Pointer(outBufLen)), 77 | ) 78 | return int(ret) 79 | } 80 | 81 | func GetIfTable(table *MIB_IFTABLE, size *uint32, order int32) int { 82 | ret, _, _ := procGetIfTable.Call( 83 | uintptr(unsafe.Pointer(table)), 84 | uintptr(unsafe.Pointer(size)), 85 | uintptr(order), 86 | ) 87 | return int(ret) 88 | } 89 | 90 | func DeleteIpForwardEntry(route *MIB_IPFORWARDROW) uint32 { 91 | ret, _, _ := procDeleteIpForwardEntry.Call( 92 | uintptr(unsafe.Pointer(route)), 93 | ) 94 | return uint32(ret) 95 | } 96 | 97 | func CreateIpForwardEntry(route *MIB_IPFORWARDROW) uint32 { 98 | ret, _, _ := procCreateIpForwardEntry.Call( 99 | uintptr(unsafe.Pointer(route)), 100 | ) 101 | return uint32(ret) 102 | } 103 | -------------------------------------------------------------------------------- /common/winsys/syscall_windows.go: -------------------------------------------------------------------------------- 1 | // +build windows 2 | 3 | package winsys 4 | 5 | type Handle uintptr 6 | 7 | const InvalidHandle = ^Handle(0) 8 | 9 | // https://docs.microsoft.com/en-us/windows/desktop/api/fwpmu/nf-fwpmu-fwpmengineopen0 10 | //sys FwpmEngineOpen0(serverName *uint16, authnService uint32, authIdentity *uintptr, session *FWPM_SESSION0, engineHandle unsafe.Pointer) (err error) [failretval!=0] = fwpuclnt.FwpmEngineOpen0 11 | 12 | // https://docs.microsoft.com/en-us/windows/desktop/api/fwpmu/nf-fwpmu-fwpmsublayeradd0 13 | //sys FwpmSubLayerAdd0(engineHandle uintptr, subLayer *FWPM_SUBLAYER0, sd uintptr) (err error) [failretval!=0] = fwpuclnt.FwpmSubLayerAdd0 14 | 15 | // https://docs.microsoft.com/en-us/windows/desktop/api/fwpmu/nf-fwpmu-fwpmfilteradd0 16 | //sys FwpmFilterAdd0(engineHandle uintptr, filter *FWPM_FILTER0, sd uintptr, id *uint64) (err error) [failretval!=0] = fwpuclnt.FwpmFilterAdd0 17 | 18 | // https://docs.microsoft.com/en-us/windows/desktop/api/fwpmu/nf-fwpmu-fwpmgetappidfromfilename0 19 | //sys FwpmGetAppIdFromFileName0(fileName *uint16, appID unsafe.Pointer) (err error) [failretval!=0] = fwpuclnt.FwpmGetAppIdFromFileName0 20 | 21 | // https://docs.microsoft.com/en-us/windows/desktop/api/fwpmu/nf-fwpmu-fwpmfreememory0 22 | //sys FwpmFreeMemory0(p unsafe.Pointer) = fwpuclnt.FwpmFreeMemory0 23 | 24 | //sys Module32First(snapshot Handle, moduleEntry *ModuleEntry32) (err error) = kernel32.Module32FirstW 25 | -------------------------------------------------------------------------------- /common/winsys/util.go: -------------------------------------------------------------------------------- 1 | // +build windows 2 | 3 | package winsys 4 | 5 | import ( 6 | "encoding/binary" 7 | "fmt" 8 | "net" 9 | "unicode/utf16" 10 | "unsafe" 11 | ) 12 | 13 | func UTF16PtrToString(cstr *uint16) string { 14 | if cstr != nil { 15 | us := make([]uint16, 0, 256) 16 | for p := uintptr(unsafe.Pointer(cstr)); ; p += 2 { 17 | u := *(*uint16)(unsafe.Pointer(p)) 18 | if u == 0 { 19 | return string(utf16.Decode(us)) 20 | } 21 | us = append(us, u) 22 | } 23 | } 24 | 25 | return "" 26 | } 27 | 28 | func NTOHS(port uint16) uint16 { 29 | buf := make([]byte, 2) 30 | binary.BigEndian.PutUint16(buf, port) 31 | return binary.LittleEndian.Uint16(buf) 32 | } 33 | 34 | // FIXME IPv6 35 | func IPAddrNTOA(addr uint32) string { 36 | buf := make([]byte, 4) 37 | binary.LittleEndian.PutUint32(buf, addr) 38 | return fmt.Sprintf("%d.%d.%d.%d", buf[0], buf[1], buf[2], buf[3]) 39 | } 40 | 41 | // FIXME IPv6 42 | func IPAddrATON(addr string) uint32 { 43 | ip := net.ParseIP(addr) 44 | if ip == nil { 45 | panic("invalid IP") 46 | } 47 | return binary.BigEndian.Uint32([]byte(ip)[net.IPv6len-net.IPv4len:]) 48 | } 49 | -------------------------------------------------------------------------------- /common/winsys/winsys.go: -------------------------------------------------------------------------------- 1 | // +build windows 2 | 3 | package winsys 4 | 5 | //go:generate go run golang.org/x/sys/windows/mkwinsyscall -output zsyscall_windows.go syscall_windows.go 6 | -------------------------------------------------------------------------------- /core/addr.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./c/include 5 | #include "lwip/tcp.h" 6 | #include 7 | */ 8 | import "C" 9 | import ( 10 | "errors" 11 | "net" 12 | "strconv" 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) error { 23 | ccp := C.CString(cp) 24 | defer C.free(unsafe.Pointer(ccp)) 25 | if r := C.ipaddr_aton(ccp, addr); r == 0 { 26 | return errors.New("failed to convert IP address") 27 | } else { 28 | return nil 29 | } 30 | } 31 | 32 | func ParseTCPAddr(addr string, port uint16) *net.TCPAddr { 33 | netAddr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(addr, strconv.Itoa(int(port)))) 34 | if err != nil { 35 | return nil 36 | } 37 | return netAddr 38 | } 39 | 40 | func ParseUDPAddr(addr string, port uint16) *net.UDPAddr { 41 | netAddr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(addr, strconv.Itoa(int(port)))) 42 | if err != nil { 43 | return nil 44 | } 45 | return netAddr 46 | } 47 | -------------------------------------------------------------------------------- /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/c/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/c/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/c/custom/arch/bpstruct.h: -------------------------------------------------------------------------------- 1 | #pragma pack(push,1) 2 | -------------------------------------------------------------------------------- /core/c/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/c/custom/arch/epstruct.h: -------------------------------------------------------------------------------- 1 | #pragma pack(pop) 2 | -------------------------------------------------------------------------------- /core/c/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/c/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 | -------------------------------------------------------------------------------- /core/c/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/c/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/c/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/c/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/c/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/c/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 | -------------------------------------------------------------------------------- /core/c/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/c/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/c/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/c/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/c/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/c/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/c/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/c/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/c/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/c/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/c/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 | -------------------------------------------------------------------------------- /core/c/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/c/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/c/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/c/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/c/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/c/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/c/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/c/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 | -------------------------------------------------------------------------------- /core/c/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/c/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/c/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/c/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 | -------------------------------------------------------------------------------- /core/c/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/c/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 | -------------------------------------------------------------------------------- /core/c/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/c/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/c/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/c/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/c/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/c/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/c/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/c/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 | -------------------------------------------------------------------------------- /core/c/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 | -------------------------------------------------------------------------------- /core/c/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/c/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/c/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 | -------------------------------------------------------------------------------- /core/c/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/c/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/c/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/c/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/c_core.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #include "c/core/init.c" 5 | #include "c/core/def.c" 6 | #include "c/core/dns.c" 7 | #include "c/core/inet_chksum.c" 8 | #include "c/core/ip.c" 9 | #include "c/core/mem.c" 10 | #include "c/core/memp.c" 11 | #include "c/core/netif.c" 12 | #include "c/core/pbuf.c" 13 | #include "c/core/raw.c" 14 | #include "c/core/stats.c" 15 | #include "c/core/sys.c" 16 | #include "c/core/tcp.c" 17 | #include "c/core/tcp_in.c" 18 | #include "c/core/tcp_out.c" 19 | #include "c/core/timeouts.c" 20 | #include "c/core/udp.c" 21 | */ 22 | import "C" 23 | -------------------------------------------------------------------------------- /core/c_core_4.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #include "c/core/ipv4/autoip.c" 5 | #include "c/core/ipv4/dhcp.c" 6 | #include "c/core/ipv4/etharp.c" 7 | #include "c/core/ipv4/icmp.c" 8 | #include "c/core/ipv4/igmp.c" 9 | #include "c/core/ipv4/ip4_frag.c" 10 | #include "c/core/ipv4/ip4.c" 11 | #include "c/core/ipv4/ip4_addr.c" 12 | */ 13 | import "C" 14 | -------------------------------------------------------------------------------- /core/c_core_6.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #include "c/core/ipv6/dhcp6.c" 5 | #include "c/core/ipv6/ethip6.c" 6 | #include "c/core/ipv6/icmp6.c" 7 | #include "c/core/ipv6/inet6.c" 8 | #include "c/core/ipv6/ip6.c" 9 | #include "c/core/ipv6/ip6_addr.c" 10 | #include "c/core/ipv6/ip6_frag.c" 11 | #include "c/core/ipv6/mld6.c" 12 | #include "c/core/ipv6/nd6.c" 13 | */ 14 | import "C" 15 | -------------------------------------------------------------------------------- /core/c_custom.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./c/custom 5 | #include "c/custom/sys_arch.c" 6 | */ 7 | import "C" 8 | -------------------------------------------------------------------------------- /core/conn.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | import ( 4 | "net" 5 | "time" 6 | ) 7 | 8 | // TCPConn abstracts a TCP connection comming from TUN. This connection 9 | // should be handled by a registered TCP proxy handler. It's important 10 | // to note that callback members are called from lwIP, they are already 11 | // in the lwIP thread when they are called, that is, they are holding 12 | // the lwipMutex. 13 | type TCPConn interface { 14 | // Sent will be called when sent data has been acknowledged by peer. 15 | Sent(len uint16) error 16 | 17 | // Receive will be called when data arrives from TUN. 18 | Receive(data []byte) error 19 | 20 | // Err will be called when a fatal error has occurred on the connection. 21 | // The corresponding pcb is already freed when this callback is called 22 | Err(err error) 23 | 24 | // LocalClosed will be called when lwIP receives a FIN segment on a 25 | // connection. 26 | LocalClosed() error 27 | 28 | // Poll will be periodically called by TCP timers. 29 | Poll() error 30 | 31 | // RemoteAddr returns the destination network address. 32 | RemoteAddr() net.Addr 33 | 34 | // LocalAddr returns the local client network address. 35 | LocalAddr() net.Addr 36 | 37 | // Read reads data comming from TUN, note that it reads from an 38 | // underlying pipe that the writer writes in the lwip thread, 39 | // write op blocks until previous written data is consumed, one 40 | // should read out all data as soon as possible. 41 | Read(data []byte) (int, error) 42 | 43 | // Write writes data to TUN. 44 | Write(data []byte) (int, error) 45 | 46 | // Close closes the connection. 47 | Close() error 48 | 49 | // CloseWrite closes the writing side by sending a FIN 50 | // segment to local peer. That means we can write no further 51 | // data to TUN. 52 | CloseWrite() error 53 | 54 | // CloseRead closes the reading side. That means we can no longer 55 | // read more from TUN. 56 | CloseRead() error 57 | 58 | // Abort aborts the connection by sending a RST segment. 59 | Abort() 60 | 61 | SetDeadline(t time.Time) error 62 | SetReadDeadline(t time.Time) error 63 | SetWriteDeadline(t time.Time) error 64 | } 65 | 66 | // TCPConn abstracts a UDP connection comming from TUN. This connection 67 | // should be handled by a registered UDP proxy handler. 68 | type UDPConn interface { 69 | // LocalAddr returns the local client network address. 70 | LocalAddr() *net.UDPAddr 71 | 72 | // ReceiveTo will be called when data arrives from TUN, and the received 73 | // data should be sent to addr. 74 | ReceiveTo(data []byte, addr *net.UDPAddr) error 75 | 76 | // WriteFrom writes data to TUN, addr will be set as source address of 77 | // UDP packets that output to TUN. 78 | WriteFrom(data []byte, addr *net.UDPAddr) (int, error) 79 | 80 | // Close closes the connection. 81 | Close() error 82 | } 83 | -------------------------------------------------------------------------------- /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 | LWIP_ERR_CONN 47 | LWIP_ERR_CLSD 48 | ) 49 | 50 | type lwipError struct { 51 | Code int 52 | } 53 | 54 | func NewLWIPError(code int) error { 55 | return &lwipError{Code: code} 56 | } 57 | 58 | func (e *lwipError) Error() string { 59 | return "error code " + string(e.Code) 60 | } 61 | -------------------------------------------------------------------------------- /core/handler.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | import ( 4 | "net" 5 | ) 6 | 7 | // TCPConnHandler handles TCP connections comming from TUN. 8 | type TCPConnHandler interface { 9 | // Handle handles the conn for target. 10 | Handle(conn net.Conn, target *net.TCPAddr) error 11 | } 12 | 13 | // UDPConnHandler handles UDP connections comming from TUN. 14 | type UDPConnHandler interface { 15 | // Connect connects the proxy server. Note that target can be nil. 16 | Connect(conn UDPConn, target *net.UDPAddr) error 17 | 18 | // ReceiveTo will be called when data arrives from TUN. 19 | ReceiveTo(conn UDPConn, data []byte, addr *net.UDPAddr) error 20 | } 21 | 22 | var tcpConnHandler TCPConnHandler 23 | var udpConnHandler UDPConnHandler 24 | 25 | func RegisterTCPConnHandler(h TCPConnHandler) { 26 | tcpConnHandler = h 27 | } 28 | 29 | func RegisterUDPConnHandler(h UDPConnHandler) { 30 | udpConnHandler = h 31 | } 32 | -------------------------------------------------------------------------------- /core/input.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./c/include 5 | #include "lwip/pbuf.h" 6 | #include "lwip/tcp.h" 7 | 8 | err_t 9 | input(struct pbuf *p) 10 | { 11 | return (*netif_list).input(p, netif_list); 12 | } 13 | */ 14 | import "C" 15 | import ( 16 | "encoding/binary" 17 | "errors" 18 | "unsafe" 19 | ) 20 | 21 | type ipver byte 22 | 23 | const ( 24 | ipv4 ipver = 4 25 | ipv6 ipver = 6 26 | ) 27 | 28 | type proto byte 29 | 30 | const ( 31 | proto_icmp proto = 1 32 | proto_tcp proto = 6 33 | proto_udp proto = 17 34 | ) 35 | 36 | func peekIPVer(p []byte) (ipver, error) { 37 | if len(p) < 1 { 38 | return 0, errors.New("short IP packet") 39 | } 40 | return ipver((p[0] & 0xf0) >> 4), nil 41 | } 42 | 43 | func moreFrags(ipv ipver, p []byte) bool { 44 | switch ipv { 45 | case ipv4: 46 | if (p[6] & 0x20) > 0 /* has MF (More Fragments) bit set */ { 47 | return true 48 | } 49 | case ipv6: 50 | // FIXME Just too lazy to implement this for IPv6, for now 51 | // returning true simply indicate do the copy anyway. 52 | return true 53 | } 54 | return false 55 | } 56 | 57 | func fragOffset(ipv ipver, p []byte) uint16 { 58 | switch ipv { 59 | case ipv4: 60 | return binary.BigEndian.Uint16(p[6:8]) & 0x1fff 61 | case ipv6: 62 | // FIXME Just too lazy to implement this for IPv6, for now 63 | // returning a value greater than 0 simply indicate do the 64 | // copy anyway. 65 | return 1 66 | } 67 | return 0 68 | } 69 | 70 | func peekNextProto(ipv ipver, p []byte) (proto, error) { 71 | switch ipv { 72 | case ipv4: 73 | if len(p) < 9 { 74 | return 0, errors.New("short IPv4 packet") 75 | } 76 | return proto(p[9]), nil 77 | case ipv6: 78 | if len(p) < 6 { 79 | return 0, errors.New("short IPv6 packet") 80 | } 81 | return proto(p[6]), nil 82 | default: 83 | return 0, errors.New("unknown IP version") 84 | } 85 | } 86 | 87 | func input(pkt []byte) (int, error) { 88 | if len(pkt) == 0 { 89 | return 0, nil 90 | } 91 | 92 | ipv, err := peekIPVer(pkt) 93 | if err != nil { 94 | return 0, err 95 | } 96 | 97 | nextProto, err := peekNextProto(ipv, pkt) 98 | if err != nil { 99 | return 0, err 100 | } 101 | 102 | lwipMutex.Lock() 103 | defer lwipMutex.Unlock() 104 | 105 | var buf *C.struct_pbuf 106 | 107 | if nextProto == proto_udp && !(moreFrags(ipv, pkt) || fragOffset(ipv, pkt) > 0) { 108 | // Copying data is not necessary for unfragmented UDP packets, and we would like to 109 | // have all data in one pbuf. 110 | buf = C.pbuf_alloc_reference(unsafe.Pointer(&pkt[0]), C.u16_t(len(pkt)), C.PBUF_REF) 111 | } else { 112 | // TODO Copy the data only when lwip need to keep it, e.g. in 113 | // case we are returning ERR_CONN in tcpRecvFn. 114 | // 115 | // Allocating from PBUF_POOL results in a pbuf chain that may 116 | // contain multiple pbufs. 117 | buf = C.pbuf_alloc(C.PBUF_RAW, C.u16_t(len(pkt)), C.PBUF_POOL) 118 | C.pbuf_take(buf, unsafe.Pointer(&pkt[0]), C.u16_t(len(pkt))) 119 | } 120 | 121 | ierr := C.input(buf) 122 | if ierr != C.ERR_OK { 123 | C.pbuf_free(buf) 124 | return 0, errors.New("packet not handled") 125 | } 126 | return len(pkt), nil 127 | } 128 | -------------------------------------------------------------------------------- /core/lwip_other.go: -------------------------------------------------------------------------------- 1 | // +build linux darwin 2 | 3 | package core 4 | 5 | /* 6 | #cgo CFLAGS: -I./c/include 7 | #include "lwip/init.h" 8 | */ 9 | import "C" 10 | 11 | func lwipInit() { 12 | C.lwip_init() // Initialze modules. 13 | } 14 | -------------------------------------------------------------------------------- /core/lwip_windows.go: -------------------------------------------------------------------------------- 1 | // +build windows 2 | 3 | package core 4 | 5 | /* 6 | #cgo CFLAGS: -I./c/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 | -------------------------------------------------------------------------------- /core/output.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./c/include 5 | #include "lwip/tcp.h" 6 | 7 | extern err_t output(struct pbuf *p); 8 | 9 | err_t 10 | output_ip4(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_ip4; 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/output_export.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./c/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 | totlen := int(p.tot_len) 18 | if p.tot_len == p.len { 19 | buf := (*[1 << 30]byte)(unsafe.Pointer(p.payload))[:totlen:totlen] 20 | OutputFn(buf[:totlen]) 21 | } else { 22 | buf := NewBytes(totlen) 23 | C.pbuf_copy_partial(p, unsafe.Pointer(&buf[0]), p.tot_len, 0) // data copy here! 24 | OutputFn(buf[:totlen]) 25 | FreeBytes(buf) 26 | } 27 | return C.ERR_OK 28 | } 29 | -------------------------------------------------------------------------------- /core/tcp_callback.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./c/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 | -------------------------------------------------------------------------------- /core/tcp_conn_map.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./c/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/udp_callback.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./c/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/udp_callback_export.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | /* 4 | #cgo CFLAGS: -I./c/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 | } 33 | conn, found := udpConns.Load(connId) 34 | if !found { 35 | if udpConnHandler == nil { 36 | panic("must register a UDP connection handler") 37 | } 38 | var err error 39 | conn, err = newUDPConn(pcb, 40 | udpConnHandler, 41 | *addr, 42 | port, 43 | srcAddr, 44 | dstAddr) 45 | if err != nil { 46 | return 47 | } 48 | udpConns.Store(connId, conn) 49 | } 50 | 51 | var buf []byte 52 | var totlen = int(p.tot_len) 53 | if p.tot_len == p.len { 54 | buf = (*[1 << 30]byte)(unsafe.Pointer(p.payload))[:totlen:totlen] 55 | } else { 56 | buf = NewBytes(totlen) 57 | defer FreeBytes(buf) 58 | C.pbuf_copy_partial(p, unsafe.Pointer(&buf[0]), p.tot_len, 0) 59 | } 60 | 61 | conn.(UDPConn).ReceiveTo(buf[:totlen], dstAddr) 62 | } 63 | -------------------------------------------------------------------------------- /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 | } 12 | -------------------------------------------------------------------------------- /filter/filter.go: -------------------------------------------------------------------------------- 1 | package filter 2 | 3 | import ( 4 | "io" 5 | ) 6 | 7 | // Filter is used for filtering IP packets comming from TUN. 8 | type Filter interface { 9 | io.Writer 10 | } 11 | -------------------------------------------------------------------------------- /filter/icmp_echo.go: -------------------------------------------------------------------------------- 1 | package filter 2 | 3 | import ( 4 | "io" 5 | "time" 6 | 7 | "github.com/eycorsican/go-tun2socks/common/log" 8 | "github.com/eycorsican/go-tun2socks/common/packet" 9 | ) 10 | 11 | type icmpEchoFilter struct { 12 | writer io.Writer 13 | delay int 14 | } 15 | 16 | func NewICMPEchoFilter(w io.Writer, delay int) Filter { 17 | return &icmpEchoFilter{writer: w, delay: delay} 18 | } 19 | 20 | func (w *icmpEchoFilter) Write(buf []byte) (int, error) { 21 | if uint8(buf[9]) == packet.PROTOCOL_ICMP { 22 | payload := make([]byte, len(buf)) 23 | copy(payload, buf) 24 | go func(data []byte) { 25 | time.Sleep(time.Duration(w.delay) * time.Millisecond) 26 | _, err := w.writer.Write(data) 27 | if err != nil { 28 | log.Fatalf("failed to input data to the stack: %v", err) 29 | } 30 | }(payload) 31 | return len(buf), nil 32 | } else { 33 | return w.writer.Write(buf) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /filter/icmp_relay.go: -------------------------------------------------------------------------------- 1 | package filter 2 | 3 | import ( 4 | "io" 5 | "net" 6 | "runtime" 7 | "sync" 8 | "syscall" 9 | "time" 10 | 11 | "github.com/google/gopacket" 12 | "github.com/google/gopacket/layers" 13 | "golang.org/x/net/icmp" 14 | 15 | "github.com/eycorsican/go-tun2socks/common/log" 16 | "github.com/eycorsican/go-tun2socks/common/packet" 17 | "github.com/eycorsican/go-tun2socks/core" 18 | ) 19 | 20 | type icmpRelayFilter struct { 21 | writer io.Writer 22 | tunDev io.Writer 23 | sendThrough string 24 | privileged bool 25 | } 26 | 27 | func NewICMPRelayFilter(w io.Writer, tunDev io.Writer, sendThrough string, privileged bool) Filter { 28 | return &icmpRelayFilter{writer: w, tunDev: tunDev, sendThrough: sendThrough, privileged: privileged} 29 | } 30 | 31 | func (w *icmpRelayFilter) Write(buf []byte) (int, error) { 32 | if uint8(buf[9]) == packet.PROTOCOL_ICMP && 33 | packet.IPVERSION_4 == packet.PeekIPVersion(buf) { 34 | packet := gopacket.NewPacket(buf, layers.LayerTypeIPv4, gopacket.Default) // copy 35 | if ip4Layer := packet.Layer(layers.LayerTypeIPv4); ip4Layer != nil { 36 | if ip4, ok := ip4Layer.(*layers.IPv4); ok { 37 | go w.relayICMPv4(ip4.Payload, ip4.SrcIP, ip4.DstIP) 38 | } else { 39 | log.Errorf("error convert IPv4 layer") 40 | } 41 | } 42 | return len(buf), nil 43 | } else { 44 | return w.writer.Write(buf) 45 | } 46 | } 47 | 48 | func (w *icmpRelayFilter) relayICMPv4(data []byte, srcIP, dstIP net.IP) { 49 | var network string 50 | var dstAddr net.Addr 51 | 52 | if w.privileged { 53 | network = "ip4:icmp" 54 | dstAddr = &net.IPAddr{IP: dstIP} 55 | } else { 56 | network = "udp4" 57 | dstAddr = &net.UDPAddr{IP: dstIP} 58 | } 59 | 60 | conn, err := icmp.ListenPacket(network, w.sendThrough) 61 | if err != nil { 62 | log.Errorf("listen ICMP failed: %v", err) 63 | return 64 | } 65 | defer conn.Close() 66 | 67 | wg := new(sync.WaitGroup) 68 | 69 | if runtime.GOOS == "windows" { 70 | wg.Add(1) 71 | 72 | go func() { 73 | defer wg.Done() 74 | 75 | buf := core.NewBytes(core.BufSize) 76 | defer core.FreeBytes(buf) 77 | 78 | conn.SetReadDeadline(time.Now().Add(4 * time.Second)) 79 | n, _, err := conn.ReadFrom(buf) 80 | if err != nil { 81 | log.Debugf("read remote failed: %v", err) 82 | return 83 | } 84 | 85 | ip := &layers.IPv4{ 86 | Version: 4, 87 | IHL: 5, 88 | Length: uint16(20 + n), 89 | TTL: 64, 90 | Id: 2048, 91 | SrcIP: dstIP, 92 | DstIP: srcIP, 93 | Protocol: layers.IPProtocolICMPv4, 94 | } 95 | pktbuf := gopacket.NewSerializeBuffer() 96 | opts := gopacket.SerializeOptions{ComputeChecksums: true} 97 | err = gopacket.SerializeLayers(pktbuf, opts, ip, gopacket.Payload(buf[:n])) 98 | if err != nil { 99 | log.Debugf("serialize packet failed: %v", err) 100 | return 101 | } 102 | 103 | w.tunDev.Write(pktbuf.Bytes()) 104 | }() 105 | } 106 | 107 | wg.Add(1) 108 | 109 | go func() { 110 | defer wg.Done() 111 | 112 | for { 113 | conn.SetWriteDeadline(time.Now().Add(2 * time.Millisecond)) 114 | if _, err := conn.WriteTo(data, dstAddr); err != nil { 115 | if neterr, ok := err.(*net.OpError); ok { 116 | if neterr.Err == syscall.ENOBUFS { 117 | log.Debugf("failed to send ICMP packet: %v", err) 118 | continue 119 | } 120 | } 121 | log.Debugf("failed to send ICMP packet: %v", err) 122 | } 123 | return 124 | } 125 | }() 126 | 127 | wg.Wait() 128 | } 129 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/mellow-io/go-tun2socks 2 | 3 | go 1.13 4 | 5 | require ( 6 | github.com/eycorsican/go-tun2socks v1.16.9 7 | github.com/google/gopacket v1.1.17 8 | github.com/miekg/dns v1.1.22 9 | github.com/shadowsocks/go-shadowsocks2 v0.0.11 10 | github.com/songgao/water v0.0.0-20190725173103-fd331bda3f4b 11 | golang.org/x/net v0.0.0-20191021144547-ec77196f6094 12 | golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 13 | golang.org/x/text v0.3.2 14 | v2ray.com/core v4.19.1+incompatible 15 | ) 16 | 17 | replace github.com/eycorsican/go-tun2socks => github.com/mellow-io/go-tun2socks v1.0.9-0.20200814044818-ee3275c43e54 18 | 19 | replace v2ray.com/core => github.com/mellow-io/v2ray-core v0.0.0-20200621073531-898a5935c60d 20 | 21 | replace github.com/songgao/water => github.com/mellow-io/water v0.0.0-20200621073504-499fe8e42129 22 | -------------------------------------------------------------------------------- /proxy/d/tcp.go: -------------------------------------------------------------------------------- 1 | package d 2 | 3 | import ( 4 | "io" 5 | "net" 6 | "strconv" 7 | 8 | "github.com/eycorsican/go-tun2socks/common/log" 9 | "github.com/eycorsican/go-tun2socks/common/proc" 10 | "github.com/eycorsican/go-tun2socks/core" 11 | ) 12 | 13 | // This handler allows you chain another proxy behind tun2socks locally, typically a rule-based proxy client, e.g. V2Ray. 14 | // 15 | // Rule-based proxy clients are very useful, they are able to dispatch requests to different servers based on powerful rule filters. 16 | // By using this setup, you are able to make all your TCP/UDP traffic under control with your favorite rule-based proxy client. 17 | // 18 | // Here's an example setup on macOS: 19 | // 20 | // tun2socks -tunGw 10.255.0.1 -fakeDns -proxyType d -proxyServer 127.0.0.1:1086 -exceptionSendThrough 192.168.1.189:0 -exceptionApps "v2ray" 21 | // 22 | // route delete default 23 | // route add default 10.255.0.1 24 | // route add default 192.168.1.1 -ifscope en0 25 | // 26 | // Where 192.168.1.189 is the default interface address, in my case, it's the WiFi interface and it's en0. 27 | // 192.168.1.1 is the default gateway. 28 | // It's very important to have two default routes, and the default route to TUN should has the highest priority. 29 | // 30 | // Start v2ray (or any other chainable proxy clients) and has SOCKS inbound listen on 127.0.0.1:1086. 31 | // 32 | // Optinally with all outbounds have sendThrough set to 192.168.1.189, if applicable. 33 | // https://v2ray.com/chapter_02/01_overview.html#outboundobject 34 | 35 | type tcpHandler struct { 36 | proxyHandler core.TCPConnHandler 37 | exceptionApps []string 38 | sendThrough net.Addr 39 | } 40 | 41 | func NewTCPHandler(proxyHandler core.TCPConnHandler, exceptionApps []string, sendThrough net.Addr) core.TCPConnHandler { 42 | return &tcpHandler{ 43 | proxyHandler, 44 | exceptionApps, 45 | sendThrough, 46 | } 47 | } 48 | 49 | func (h *tcpHandler) isExceptionApp(name string) bool { 50 | for _, app := range h.exceptionApps { 51 | if name == app { 52 | return true 53 | } 54 | } 55 | return false 56 | } 57 | 58 | func (h *tcpHandler) relay(lhs, rhs net.Conn) { 59 | cls := func() { 60 | rhs.Close() 61 | lhs.Close() 62 | } 63 | 64 | go func() { 65 | io.Copy(rhs, lhs) 66 | cls() 67 | }() 68 | 69 | io.Copy(lhs, rhs) 70 | cls() 71 | } 72 | 73 | func (h *tcpHandler) Handle(conn net.Conn, target *net.TCPAddr) error { 74 | localHost, localPortStr, _ := net.SplitHostPort(conn.LocalAddr().String()) 75 | localPortInt, _ := strconv.Atoi(localPortStr) 76 | cmd, err := proc.GetCommandNameBySocket("tcp", localHost, uint16(localPortInt)) 77 | if err != nil { 78 | cmd = "unknown process" 79 | } 80 | 81 | if h.isExceptionApp(cmd) { 82 | dialer := net.Dialer{LocalAddr: h.sendThrough} 83 | rc, err := dialer.Dial("tcp", target.String()) 84 | if err != nil { 85 | return err 86 | } 87 | 88 | go h.relay(conn, rc) 89 | 90 | log.Access(cmd, "direct", target.Network(), conn.LocalAddr().String(), target.String()) 91 | 92 | return nil 93 | } else { 94 | return h.proxyHandler.Handle(conn, target) 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /proxy/d/udp.go: -------------------------------------------------------------------------------- 1 | package d 2 | 3 | import ( 4 | "net" 5 | "strconv" 6 | "sync" 7 | "time" 8 | 9 | "github.com/eycorsican/go-tun2socks/common/log" 10 | "github.com/eycorsican/go-tun2socks/common/proc" 11 | "github.com/eycorsican/go-tun2socks/core" 12 | ) 13 | 14 | type udpHandler struct { 15 | sync.Mutex 16 | 17 | proxyHandler core.UDPConnHandler 18 | exceptionApps []string 19 | sendThrough net.Addr 20 | exceptionConns map[core.UDPConn]*net.UDPConn 21 | timeout time.Duration 22 | } 23 | 24 | func (h *udpHandler) isExceptionApp(name string) bool { 25 | for _, app := range h.exceptionApps { 26 | if name == app { 27 | return true 28 | } 29 | } 30 | return false 31 | } 32 | 33 | func NewUDPHandler(proxyHandler core.UDPConnHandler, exceptionApps []string, sendThrough net.Addr, timeout time.Duration) core.UDPConnHandler { 34 | return &udpHandler{ 35 | proxyHandler: proxyHandler, 36 | exceptionApps: exceptionApps, 37 | sendThrough: sendThrough, 38 | exceptionConns: make(map[core.UDPConn]*net.UDPConn), 39 | timeout: timeout, 40 | } 41 | } 42 | 43 | func (h *udpHandler) handleInput(conn core.UDPConn, pc *net.UDPConn) { 44 | buf := core.NewBytes(core.BufSize) 45 | 46 | defer func() { 47 | h.Close(conn) 48 | core.FreeBytes(buf) 49 | }() 50 | 51 | for { 52 | pc.SetDeadline(time.Now().Add(h.timeout)) 53 | n, addr, err := pc.ReadFromUDP(buf) 54 | if err != nil { 55 | return 56 | } 57 | 58 | _, err = conn.WriteFrom(buf[:n], addr) 59 | if err != nil { 60 | return 61 | } 62 | } 63 | } 64 | 65 | func (h *udpHandler) Connect(conn core.UDPConn, target *net.UDPAddr) error { 66 | localHost, localPortStr, _ := net.SplitHostPort(conn.LocalAddr().String()) 67 | localPortInt, _ := strconv.Atoi(localPortStr) 68 | cmd, err := proc.GetCommandNameBySocket("udp", localHost, uint16(localPortInt)) 69 | if err != nil { 70 | cmd = "unknown process" 71 | } 72 | 73 | if h.isExceptionApp(cmd) { 74 | bindAddr, _ := net.ResolveUDPAddr( 75 | "udp", 76 | h.sendThrough.String(), 77 | ) 78 | pc, err := net.ListenUDP("udp", bindAddr) 79 | if err != nil { 80 | return err 81 | } 82 | h.Lock() 83 | h.exceptionConns[conn] = pc 84 | h.Unlock() 85 | 86 | go h.handleInput(conn, pc) 87 | 88 | log.Access(cmd, "direct", target.Network(), conn.LocalAddr().String(), target.String()) 89 | 90 | return nil 91 | } else { 92 | return h.proxyHandler.Connect(conn, target) 93 | } 94 | } 95 | 96 | func (h *udpHandler) ReceiveTo(conn core.UDPConn, data []byte, addr *net.UDPAddr) error { 97 | h.Lock() 98 | defer h.Unlock() 99 | 100 | if pc, found := h.exceptionConns[conn]; found { 101 | _, err := pc.WriteTo(data, addr) 102 | if err != nil { 103 | return err 104 | } 105 | return nil 106 | } else { 107 | return h.proxyHandler.ReceiveTo(conn, data, addr) 108 | } 109 | } 110 | 111 | func (h *udpHandler) Close(conn core.UDPConn) { 112 | conn.Close() 113 | 114 | h.Lock() 115 | defer h.Unlock() 116 | 117 | if pc, ok := h.exceptionConns[conn]; ok { 118 | pc.Close() 119 | delete(h.exceptionConns, conn) 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /proxy/dnsfallback/udp.go: -------------------------------------------------------------------------------- 1 | package dnsfallback 2 | 3 | import ( 4 | "encoding/binary" 5 | "errors" 6 | "net" 7 | 8 | "github.com/eycorsican/go-tun2socks/common/dns" 9 | "github.com/eycorsican/go-tun2socks/core" 10 | ) 11 | 12 | // UDP handler that intercepts DNS queries and replies with a truncated response (TC bit) 13 | // in order for the client to retry over TCP. This DNS/TCP fallback mechanism is 14 | // useful for proxy servers that do not support UDP. 15 | // Note that non-DNS UDP traffic is dropped. 16 | type udpHandler struct{} 17 | 18 | const ( 19 | dnsHeaderLength = 12 20 | dnsMaskQr = uint8(0x80) 21 | dnsMaskTc = uint8(0x02) 22 | dnsMaskRcode = uint8(0x0F) 23 | ) 24 | 25 | func NewUDPHandler() core.UDPConnHandler { 26 | return &udpHandler{} 27 | } 28 | 29 | func (h *udpHandler) Connect(conn core.UDPConn, udpAddr *net.UDPAddr) error { 30 | if udpAddr.Port != dns.COMMON_DNS_PORT { 31 | return errors.New("Cannot handle non-DNS packet") 32 | } 33 | return nil 34 | } 35 | 36 | func (h *udpHandler) ReceiveTo(conn core.UDPConn, data []byte, addr *net.UDPAddr) error { 37 | if len(data) < dnsHeaderLength { 38 | return errors.New("Received malformed DNS query") 39 | } 40 | // DNS Header 41 | // 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 42 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 43 | // | ID | 44 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 45 | // |QR| Opcode |AA|TC|RD|RA| Z | RCODE | 46 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 47 | // | QDCOUNT | 48 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 49 | // | ANCOUNT | 50 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 51 | // | NSCOUNT | 52 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 53 | // | ARCOUNT | 54 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 55 | // Set response and truncated bits 56 | data[2] |= dnsMaskQr | dnsMaskTc 57 | // Set response code to 'no error'. 58 | data[3] &= ^dnsMaskRcode 59 | // Set ANCOUNT to QDCOUNT. This is technically incorrect, since the response does not 60 | // include an answer. However, without it some DNS clients (i.e. Windows 7) do not retry 61 | // over TCP. 62 | var qdcount = binary.BigEndian.Uint16(data[4:6]) 63 | binary.BigEndian.PutUint16(data[6:], qdcount) 64 | _, err := conn.WriteFrom(data, addr) 65 | return err 66 | } 67 | -------------------------------------------------------------------------------- /proxy/echo/tcp.go: -------------------------------------------------------------------------------- 1 | package echo 2 | 3 | import ( 4 | "io" 5 | "net" 6 | 7 | "github.com/eycorsican/go-tun2socks/core" 8 | ) 9 | 10 | // An echo proxy, do nothing but echo back data to the sender, the handler was 11 | // created for testing purposes, it may causes issues when more than one clients 12 | // are connecting the handler simultaneously. 13 | type tcpHandler struct{} 14 | 15 | func NewTCPHandler() core.TCPConnHandler { 16 | return &tcpHandler{} 17 | } 18 | 19 | func (h *tcpHandler) echoBack(conn net.Conn) { 20 | io.Copy(conn, conn) 21 | } 22 | 23 | func (h *tcpHandler) Handle(conn net.Conn, target *net.TCPAddr) error { 24 | go h.echoBack(conn) 25 | return nil 26 | } 27 | -------------------------------------------------------------------------------- /proxy/echo/udp.go: -------------------------------------------------------------------------------- 1 | package echo 2 | 3 | import ( 4 | "net" 5 | 6 | "github.com/eycorsican/go-tun2socks/common/log" 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.UDPConnHandler { 14 | return &udpHandler{} 15 | } 16 | 17 | func (h *udpHandler) Connect(conn core.UDPConn, target *net.UDPAddr) error { 18 | return nil 19 | } 20 | 21 | func (h *udpHandler) ReceiveTo(conn core.UDPConn, data []byte, addr *net.UDPAddr) 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.WriteFrom(b, addr) 26 | if err != nil { 27 | log.Warnf("failed to echo back data: %v", err) 28 | } 29 | }(payload) 30 | return nil 31 | } 32 | -------------------------------------------------------------------------------- /proxy/redirect/tcp.go: -------------------------------------------------------------------------------- 1 | package redirect 2 | 3 | import ( 4 | "io" 5 | "net" 6 | 7 | "github.com/eycorsican/go-tun2socks/common/log" 8 | "github.com/eycorsican/go-tun2socks/core" 9 | ) 10 | 11 | // To do a benchmark using iperf3 locally, you may follow these steps: 12 | // 13 | // 1. Setup and configure the TUN device and start tun2socks with the 14 | // redirect handler using the following command: 15 | // tun2socks -proxyType redirect -proxyServer 127.0.0.1:1234 16 | // Tun2socks will redirect all traffic to 127.0.0.1:1234. 17 | // 18 | // 2. Route traffic targeting 1.2.3.4 to the TUN interface (240.0.0.1): 19 | // route add 1.2.3.4/32 240.0.0.1 20 | // 21 | // 3. Run iperf3 server locally and listening on 1234 port: 22 | // iperf3 -s -p 1234 23 | // 24 | // 4. Run iperf3 client locally and connect to 1.2.3.4:1234: 25 | // iperf3 -c 1.2.3.4 -p 1234 26 | // 27 | // It works this way: 28 | // iperf3 client -> 1.2.3.4:1234 -> routing table -> TUN (240.0.0.1) -> tun2socks -> tun2socks redirect anything to 127.0.0.1:1234 -> iperf3 server 29 | // 30 | type tcpHandler struct { 31 | target string 32 | } 33 | 34 | type duplexConn interface { 35 | net.Conn 36 | CloseWrite() error 37 | CloseRead() error 38 | } 39 | 40 | func NewTCPHandler(target string) core.TCPConnHandler { 41 | return &tcpHandler{target: target} 42 | } 43 | 44 | func (h *tcpHandler) handleInput(conn net.Conn, input io.ReadCloser) { 45 | defer func() { 46 | if tcpConn, ok := conn.(core.TCPConn); ok { 47 | tcpConn.CloseWrite() 48 | } else { 49 | conn.Close() 50 | } 51 | if tcpInput, ok := input.(duplexConn); ok { 52 | tcpInput.CloseRead() 53 | } else { 54 | input.Close() 55 | } 56 | }() 57 | 58 | io.Copy(conn, input) 59 | } 60 | 61 | func (h *tcpHandler) handleOutput(conn net.Conn, output io.WriteCloser) { 62 | defer func() { 63 | if tcpConn, ok := conn.(core.TCPConn); ok { 64 | tcpConn.CloseRead() 65 | } else { 66 | conn.Close() 67 | } 68 | if tcpOutput, ok := output.(duplexConn); ok { 69 | tcpOutput.CloseWrite() 70 | } else { 71 | output.Close() 72 | } 73 | }() 74 | 75 | io.Copy(output, conn) 76 | } 77 | 78 | func (h *tcpHandler) Handle(conn net.Conn, target *net.TCPAddr) error { 79 | c, err := net.Dial("tcp", h.target) 80 | if err != nil { 81 | return err 82 | } 83 | go h.handleInput(conn, c) 84 | go h.handleOutput(conn, c) 85 | log.Infof("new proxy connection for target: %s:%s", target.Network(), target.String()) 86 | return nil 87 | } 88 | -------------------------------------------------------------------------------- /proxy/redirect/udp.go: -------------------------------------------------------------------------------- 1 | package redirect 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "net" 7 | "sync" 8 | "time" 9 | 10 | "github.com/eycorsican/go-tun2socks/common/log" 11 | "github.com/eycorsican/go-tun2socks/core" 12 | ) 13 | 14 | type udpHandler struct { 15 | sync.Mutex 16 | 17 | timeout time.Duration 18 | udpConns map[core.UDPConn]*net.UDPConn 19 | udpTargetAddrs map[core.UDPConn]*net.UDPAddr 20 | target string 21 | } 22 | 23 | func NewUDPHandler(target string, timeout time.Duration) core.UDPConnHandler { 24 | return &udpHandler{ 25 | timeout: timeout, 26 | udpConns: make(map[core.UDPConn]*net.UDPConn, 8), 27 | udpTargetAddrs: make(map[core.UDPConn]*net.UDPAddr, 8), 28 | target: target, 29 | } 30 | } 31 | 32 | func (h *udpHandler) fetchUDPInput(conn core.UDPConn, pc *net.UDPConn) { 33 | buf := core.NewBytes(core.BufSize) 34 | 35 | defer func() { 36 | h.Close(conn) 37 | core.FreeBytes(buf) 38 | }() 39 | 40 | for { 41 | pc.SetDeadline(time.Now().Add(h.timeout)) 42 | n, addr, err := pc.ReadFromUDP(buf) 43 | if err != nil { 44 | // log.Printf("failed to read UDP data from remote: %v", err) 45 | return 46 | } 47 | 48 | _, err = conn.WriteFrom(buf[:n], addr) 49 | if err != nil { 50 | log.Warnf("failed to write UDP data to TUN") 51 | return 52 | } 53 | } 54 | } 55 | 56 | func (h *udpHandler) Connect(conn core.UDPConn, target *net.UDPAddr) error { 57 | bindAddr := &net.UDPAddr{IP: nil, Port: 0} 58 | pc, err := net.ListenUDP("udp", bindAddr) 59 | if err != nil { 60 | log.Errorf("failed to bind udp address") 61 | return err 62 | } 63 | tgtAddr, _ := net.ResolveUDPAddr("udp", h.target) 64 | h.Lock() 65 | h.udpTargetAddrs[conn] = tgtAddr 66 | h.udpConns[conn] = pc 67 | h.Unlock() 68 | go h.fetchUDPInput(conn, pc) 69 | log.Infof("new proxy connection for target: %s:%s", target.Network(), target.String()) 70 | return nil 71 | } 72 | 73 | func (h *udpHandler) ReceiveTo(conn core.UDPConn, data []byte, addr *net.UDPAddr) error { 74 | h.Lock() 75 | pc, ok1 := h.udpConns[conn] 76 | tgtAddr, ok2 := h.udpTargetAddrs[conn] 77 | h.Unlock() 78 | 79 | if ok1 && ok2 { 80 | _, err := pc.WriteToUDP(data, tgtAddr) 81 | if err != nil { 82 | log.Warnf("failed to write UDP payload to SOCKS5 server: %v", err) 83 | return errors.New("failed to write UDP data") 84 | } 85 | return nil 86 | } else { 87 | return errors.New(fmt.Sprintf("proxy connection %v->%v does not exists", conn.LocalAddr(), addr)) 88 | } 89 | } 90 | 91 | func (h *udpHandler) Close(conn core.UDPConn) { 92 | conn.Close() 93 | 94 | h.Lock() 95 | defer h.Unlock() 96 | 97 | if _, ok := h.udpTargetAddrs[conn]; ok { 98 | delete(h.udpTargetAddrs, conn) 99 | } 100 | if pc, ok := h.udpConns[conn]; ok { 101 | pc.Close() 102 | delete(h.udpConns, conn) 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /proxy/shadowsocks/tcp.go: -------------------------------------------------------------------------------- 1 | package shadowsocks 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "io" 7 | "net" 8 | "strconv" 9 | 10 | sscore "github.com/shadowsocks/go-shadowsocks2/core" 11 | sssocks "github.com/shadowsocks/go-shadowsocks2/socks" 12 | 13 | "github.com/eycorsican/go-tun2socks/common/dns" 14 | "github.com/eycorsican/go-tun2socks/common/log" 15 | "github.com/eycorsican/go-tun2socks/core" 16 | ) 17 | 18 | type tcpHandler struct { 19 | cipher sscore.Cipher 20 | server string 21 | fakeDns dns.FakeDns 22 | } 23 | 24 | func (h *tcpHandler) handleInput(conn net.Conn, input io.ReadCloser) { 25 | defer func() { 26 | conn.Close() 27 | input.Close() 28 | }() 29 | io.Copy(conn, input) 30 | } 31 | 32 | func (h *tcpHandler) handleOutput(conn net.Conn, output io.WriteCloser) { 33 | defer func() { 34 | conn.Close() 35 | output.Close() 36 | }() 37 | io.Copy(output, conn) 38 | } 39 | 40 | func NewTCPHandler(server, cipher, password string, fakeDns dns.FakeDns) core.TCPConnHandler { 41 | ciph, err := sscore.PickCipher(cipher, []byte{}, password) 42 | if err != nil { 43 | log.Errorf("failed to pick a cipher: %v", err) 44 | } 45 | return &tcpHandler{ 46 | cipher: ciph, 47 | server: server, 48 | fakeDns: fakeDns, 49 | } 50 | } 51 | 52 | func (h *tcpHandler) Handle(conn net.Conn, target *net.TCPAddr) error { 53 | if target == nil { 54 | log.Fatalf("unexpected nil target") 55 | } 56 | 57 | // Connect the relay server. 58 | rc, err := net.Dial("tcp", h.server) 59 | if err != nil { 60 | return errors.New(fmt.Sprintf("dial remote server failed: %v", err)) 61 | } 62 | rc = h.cipher.StreamConn(rc) 63 | 64 | // Replace with a domain name if target address IP is a fake IP. 65 | var targetHost string 66 | if h.fakeDns != nil && h.fakeDns.IsFakeIP(target.IP) { 67 | targetHost = h.fakeDns.QueryDomain(target.IP) 68 | } else { 69 | targetHost = target.IP.String() 70 | } 71 | dest := net.JoinHostPort(targetHost, strconv.Itoa(target.Port)) 72 | 73 | // Write target address. 74 | tgt := sssocks.ParseAddr(dest) 75 | _, err = rc.Write(tgt) 76 | if err != nil { 77 | return fmt.Errorf("send target address failed: %v", err) 78 | } 79 | 80 | go h.handleInput(conn, rc) 81 | go h.handleOutput(conn, rc) 82 | 83 | log.Infof("new proxy connection for target: %s:%s", target.Network(), dest) 84 | return nil 85 | } 86 | -------------------------------------------------------------------------------- /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/quic" 40 | _ "v2ray.com/core/transport/internet/tcp" 41 | _ "v2ray.com/core/transport/internet/tls" 42 | _ "v2ray.com/core/transport/internet/udp" 43 | _ "v2ray.com/core/transport/internet/websocket" 44 | 45 | // Transport headers 46 | _ "v2ray.com/core/transport/internet/headers/http" 47 | _ "v2ray.com/core/transport/internet/headers/noop" 48 | _ "v2ray.com/core/transport/internet/headers/srtp" 49 | _ "v2ray.com/core/transport/internet/headers/tls" 50 | _ "v2ray.com/core/transport/internet/headers/utp" 51 | _ "v2ray.com/core/transport/internet/headers/wechat" 52 | _ "v2ray.com/core/transport/internet/headers/wireguard" 53 | 54 | // JSON config support. Choose only one from the two below. 55 | // The following line loads JSON from v2ctl 56 | // _ "v2ray.com/core/main/json" 57 | // The following line loads JSON internally 58 | _ "v2ray.com/core/main/jsonem" 59 | // Load config from file or http(s) 60 | // _ "v2ray.com/core/main/confloader/external" 61 | ) 62 | -------------------------------------------------------------------------------- /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/app/reverse" 12 | 13 | _ "v2ray.com/core/transport/internet/domainsocket" 14 | ) 15 | -------------------------------------------------------------------------------- /scripts/recover_route_linux.sh: -------------------------------------------------------------------------------- 1 | sudo ip route add default via 192.168.1.1 2 | -------------------------------------------------------------------------------- /scripts/recover_route_macos.sh: -------------------------------------------------------------------------------- 1 | sudo route -n flush 2 | sudo route -n flush 3 | sudo route -n flush 4 | sudo ifconfig en0 down 5 | sudo ifconfig en0 up 6 | -------------------------------------------------------------------------------- /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=(\ 18 | "tun2socks-darwin-10.6-amd64" \ 19 | "tun2socks-linux-386" \ 20 | "tun2socks-linux-amd64" \ 21 | "tun2socks-linux-arm64" \ 22 | "tun2socks-linux-mips" \ 23 | "tun2socks-linux-mips64" \ 24 | "tun2socks-linux-mips64le" \ 25 | "tun2socks-linux-mipsle" \ 26 | "tun2socks-windows-4.0-386.exe" \ 27 | "tun2socks-windows-4.0-amd64.exe" \ 28 | ) 29 | eval $list_assets_cmd 30 | if [ $? -ne 0 ]; then 31 | cd build 32 | for i in ${executables[@]}; do 33 | zip "$i.zip" "$i" 34 | done 35 | cd .. 36 | fi 37 | 38 | owner=eycorsican 39 | repo=go-tun2socks 40 | base_url=https://api.github.com 41 | 42 | content_type_json="Content-Type: application/json" 43 | content_type_zip="Content-Type: application/zip" 44 | 45 | api_create_release=/repos/$owner/$repo/releases 46 | api_get_release_by_tag=/repos/$owner/$repo/releases/tags/$tag 47 | api_create_release_data="{\ 48 | \"tag_name\": \"${tag}\",\ 49 | \"target_commitish\": \"master\",\ 50 | \"name\": \"${tag}\", 51 | \"body\": \"${description}\", 52 | \"draft\": false,\ 53 | \"prerelease\": false\ 54 | }" 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\"])"` 2>/dev/null 59 | 60 | # If there is release with the tag exists, delete it first. 61 | if [ $? -eq 0 ]; then 62 | api_delete_release=/repos/$owner/$repo/releases/$release_id 63 | curl -u $owner:$token -H "$content_type_json" -X DELETE "${base_url}${api_delete_release}" 64 | fi 65 | 66 | # Create a release. 67 | curl -u $owner:$token -H "$content_type_json" -X POST "${base_url}${api_create_release}" --data "${api_create_release_data}" 68 | 69 | # Get the release id by tag name. 70 | release_id=`curl -u $owner:$token -H "$content_type_json" -X GET "${base_url}${api_get_release_by_tag}" | \ 71 | python -c "import sys;import json;print(json.loads(\"\".join(sys.stdin.readlines()))[\"id\"])"` 72 | 73 | # Upload assets. 74 | eval $list_assets_cmd | while read -r asset_name; do 75 | upload_url=https://uploads.github.com/repos/$owner/$repo/releases/$release_id/assets?name=$(basename $asset_name) 76 | curl --progress-bar -u $owner:$token -H "$content_type_zip" --data-binary @"$asset_name" -X POST $upload_url 77 | if [ $? -ne "0" ]; then 78 | exit 1 79 | fi 80 | done 81 | -------------------------------------------------------------------------------- /scripts/run_socks_linux.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -x 4 | 5 | config_route() { 6 | sleep 2 7 | sudo ip addr add 10.255.0.1/24 dev tun1 8 | sudo ip link set dev tun1 up 9 | sudo ip route del default 10 | sudo ip route add default via 10.255.0.1 11 | } 12 | 13 | config_route & 14 | sudo ./build/tun2socks -tunAddr 10.255.0.2 -tunGw 10.255.0.1 -proxyServer 192.168.1.1:1086 -fakeDns -loglevel info -stats 15 | -------------------------------------------------------------------------------- /scripts/run_socks_macos.sh: -------------------------------------------------------------------------------- 1 | config_route() { 2 | sleep 2 3 | sudo route delete default 4 | sudo route add default 10.255.0.1 5 | } 6 | 7 | config_route & 8 | sudo ./build/tun2socks -tunAddr 10.255.0.2 -tunGw 10.255.0.1 -proxyServer 192.168.1.1:1086 -fakeDns -loglevel info -stats 9 | -------------------------------------------------------------------------------- /tun/README.md: -------------------------------------------------------------------------------- 1 | Files in this directory are copied from https://github.com/yinghuocho/gotun2socks, with slightly modifies. 2 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | PlatformSpecificParams: water.PlatformSpecificParams{ 37 | Name: name, 38 | }, 39 | }) 40 | if err != nil { 41 | return nil, err 42 | } 43 | name = tunDev.Name() 44 | ip := net.ParseIP(addr) 45 | if ip == nil { 46 | return nil, errors.New("invalid IP address") 47 | } 48 | 49 | var params string 50 | if isIPv4(ip) { 51 | params = fmt.Sprintf("%s inet %s netmask %s %s", name, addr, mask, gw) 52 | } else if isIPv6(ip) { 53 | prefixlen, err := strconv.Atoi(mask) 54 | if err != nil { 55 | return nil, errors.New(fmt.Sprintf("parse IPv6 prefixlen failed: %v", err)) 56 | } 57 | params = fmt.Sprintf("%s inet6 %s/%d", name, addr, prefixlen) 58 | } else { 59 | return nil, errors.New("invalid IP address") 60 | } 61 | 62 | out, err := exec.Command("ifconfig", strings.Split(params, " ")...).Output() 63 | if err != nil { 64 | if len(out) != 0 { 65 | return nil, errors.New(fmt.Sprintf("%v, output: %s", err, out)) 66 | } 67 | return nil, err 68 | } 69 | return tunDev, nil 70 | } 71 | -------------------------------------------------------------------------------- /tun/tun_linux.go: -------------------------------------------------------------------------------- 1 | package tun 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "io" 7 | "net" 8 | "os/exec" 9 | "strings" 10 | 11 | "github.com/songgao/water" 12 | ) 13 | 14 | func OpenTunDevice(name, addr, gw, mask string, dnsServers []string) (io.ReadWriteCloser, error) { 15 | cfg := water.Config{ 16 | DeviceType: water.TUN, 17 | } 18 | cfg.Name = name 19 | tunDev, err := water.New(cfg) 20 | if err != nil { 21 | return nil, err 22 | } 23 | name = tunDev.Name() 24 | 25 | ipMask := net.IPMask(net.ParseIP(mask).To4()) 26 | maskSize, _ := ipMask.Size() 27 | 28 | params := fmt.Sprintf("addr add %s/%d dev %s", gw, maskSize, name) 29 | out, err := exec.Command("ip", strings.Split(params, " ")...).Output() 30 | if err != nil { 31 | if len(out) != 0 { 32 | return nil, errors.New(fmt.Sprintf("%v, output: %s", err, out)) 33 | } 34 | return nil, err 35 | } 36 | 37 | params = fmt.Sprintf("link set dev %s up", name) 38 | out, err = exec.Command("ip", strings.Split(params, " ")...).Output() 39 | if err != nil { 40 | if len(out) != 0 { 41 | return nil, errors.New(fmt.Sprintf("%v, output: %s", err, out)) 42 | } 43 | return nil, err 44 | } 45 | 46 | return tunDev, nil 47 | } 48 | --------------------------------------------------------------------------------