├── .gitignore ├── .travis.yml ├── CMakeLists.txt ├── README.md ├── appveyor.yml ├── bean ├── ConnInfo.cpp ├── ConnInfo.h ├── EncHead.cpp ├── EncHead.h ├── RConfig.cpp ├── RConfig.h ├── TcpInfo.cpp └── TcpInfo.h ├── build_app.sh ├── build_release.sh ├── callbacks ├── ConnReset.cpp ├── ConnReset.h ├── INetConnKeepAlive.h ├── IReset.h ├── KeepAliveRouteObserver.cpp ├── KeepAliveRouteObserver.h ├── NetConnKeepAlive.cpp ├── NetConnKeepAlive.h ├── RConnReset.cpp └── RConnReset.h ├── cap ├── CapUtil.cpp ├── CapUtil.h ├── RCap.cpp ├── RCap.h ├── cap_headers.h ├── cap_util.cpp └── cap_util.h ├── client ├── CConnErrHandler.cpp ├── CConnErrHandler.h ├── CNetGroup.cpp ├── CNetGroup.h ├── CSockApp.cpp ├── CSockApp.h ├── ClientGroup.cpp ├── ClientGroup.h ├── ClientNetManager.cpp ├── ClientNetManager.h ├── ClientNetObserver.cpp ├── ClientNetObserver.h ├── csock.cpp ├── csock.h └── main.cpp ├── conn ├── BtmUdpConn.cpp ├── BtmUdpConn.h ├── CConn.cpp ├── CConn.h ├── DefaultFakeConn.cpp ├── DefaultFakeConn.h ├── FakeTcp.cpp ├── FakeTcp.h ├── FakeUdp.cpp ├── FakeUdp.h ├── IAppGroup.cpp ├── IAppGroup.h ├── IBtmConn.cpp ├── IBtmConn.h ├── IConn.cpp ├── IConn.h ├── IGroup.cpp ├── IGroup.h ├── INetConn.cpp ├── INetConn.h ├── INetConnErrorHandler.h ├── INetGroup.cpp ├── INetGroup.h ├── RConn.cpp ├── RConn.h ├── RawTcp.cpp └── RawTcp.h ├── doc ├── README.zh-cn.md └── img │ ├── btdonation.jpeg │ ├── ethdonation.jpeg │ ├── kcptun.png │ ├── kcptun_do_sg.png │ ├── kcptun_telecom.png │ ├── normal.png │ ├── pid.png │ ├── principle.png │ ├── rsock.png │ ├── rsock_11tcp_telcom.png │ ├── rsock_11udp_tcp_telcom.png │ ├── rsock_11udp_telcom.png │ ├── rsock_do_sg_11tcp.png │ ├── rsock_do_sg_11udp.png │ ├── rsock_do_sg_11udp_tcp.png │ ├── running.png │ └── shadowsocks.png ├── include ├── args.hxx ├── plog │ ├── Appenders │ │ ├── ConsoleAppender.h │ │ ├── IAppender.h │ │ └── RollingFileAppender.h │ ├── Converters │ │ └── UTF8Converter.h │ ├── Formatters │ │ ├── CsvFormatter.h │ │ └── TxtFormatter.h │ ├── Init.h │ ├── Log.h │ ├── Logger.h │ ├── Record.h │ ├── Severity.h │ ├── Util.h │ └── WinApi.h ├── rcommon.h ├── rscomm.h └── rstype.h ├── net ├── INetManager.cpp ├── INetManager.h ├── NetManagerTimer.cpp ├── NetManagerTimer.h ├── NetUtil.cpp ├── NetUtil.h ├── TcpAckPool.cpp ├── TcpAckPool.h ├── TcpListenPool.cpp └── TcpListenPool.h ├── server ├── SConn.cpp ├── SConn.h ├── SNetGroup.cpp ├── SNetGroup.h ├── SSockApp.cpp ├── SSockApp.h ├── ServerGroup.cpp ├── ServerGroup.h ├── ServerNetManager.cpp ├── ServerNetManager.h ├── SubGroup.cpp ├── SubGroup.h ├── main.cpp ├── ssock.cpp └── ssock.h ├── src ├── ISockApp.cpp ├── ISockApp.h ├── app │ ├── AppNetObserver.cpp │ ├── AppNetObserver.h │ ├── AppTimer.cpp │ └── AppTimer.h ├── os │ ├── common │ │ └── FdUtil_common.cpp │ ├── include │ │ ├── FdUtil.h │ │ ├── ProcUtil.h │ │ ├── os.h │ │ ├── os_unix.h │ │ ├── os_util.h │ │ └── os_win.h │ ├── unix │ │ ├── FdUtil.cpp │ │ ├── ProcUtil.cpp │ │ ├── conn │ │ │ ├── UnixDgramSyncConn.cpp │ │ │ └── UnixDgramSyncConn.h │ │ └── os_util.cpp │ └── win │ │ ├── FdUtil.cpp │ │ ├── ProcUtil.cpp │ │ └── os_util.cpp ├── rcommon.c ├── service │ ├── IBaseService.h │ ├── INetObserver.h │ ├── IObserver.h │ ├── IRouteObserver.h │ ├── IService.cpp │ ├── IService.h │ ├── ITimerObserver.h │ ├── NetService.cpp │ ├── NetService.h │ ├── RouteService.cpp │ ├── RouteService.h │ ├── ServiceUtil.cpp │ ├── ServiceUtil.h │ ├── TimerService.cpp │ └── TimerService.h ├── singletons │ ├── ConfManager.cpp │ ├── ConfManager.h │ ├── HandlerUtil.cpp │ ├── HandlerUtil.h │ ├── RouteManager.cpp │ ├── RouteManager.h │ ├── ServiceManager.cpp │ ├── ServiceManager.h │ ├── Singleton.cpp │ └── Singleton.h ├── sync │ ├── IPacketSyncConn.cpp │ ├── IPacketSyncConn.h │ ├── ISyncConn.cpp │ ├── ISyncConn.h │ ├── LoopStreamSyncConn.cpp │ ├── LoopStreamSyncConn.h │ ├── SyncConnFactory.cpp │ ├── SyncConnFactory.h │ ├── TcpStreamSyncConn.cpp │ ├── TcpStreamSyncConn.h │ ├── UdpSyncConn.cpp │ └── UdpSyncConn.h └── util │ ├── ICloseable.h │ ├── KeyGenerator.cpp │ ├── KeyGenerator.h │ ├── RouteUtil.cpp │ ├── RouteUtil.h │ ├── TcpCmpFn.cpp │ ├── TcpCmpFn.h │ ├── TcpUtil.cpp │ └── TcpUtil.h ├── test ├── test_client.cpp ├── test_server.cpp ├── udp_echo_client.cpp └── udp_echo_server.cpp ├── thirdparty ├── json11.cpp ├── json11.hpp ├── md5.c └── md5.h ├── util ├── Handler.cpp ├── Handler.h ├── PortPair.cpp ├── PortPair.h ├── RPortList.cpp ├── RPortList.h ├── ShotHandler.cpp ├── ShotHandler.h ├── TextUtils.cpp ├── TextUtils.h ├── UvUtil.cpp ├── UvUtil.h ├── enc.c ├── enc.h ├── rhash.cpp ├── rhash.h ├── rsutil.cpp └── rsutil.h └── xbuild ├── bak └── Windows_x86 │ ├── Packet.dll │ └── wpcap.dll ├── cmake ├── Darwin_x86_64.toolchain.cmake ├── Linux_x86_64.toolchain.cmake ├── Linux_x86_64.universe_toolchain.cmake ├── Windows_x86.toolchain.cmake ├── base.cmake ├── test_darwin.cmake ├── travis_Darwin_x86_64.cmake └── travis_Linux_x86_64.cmake ├── include ├── libdnet │ ├── dnet.h │ ├── dnet │ │ ├── Makefile.am │ │ ├── Makefile.in │ │ ├── addr.h │ │ ├── arp.h │ │ ├── blob.h │ │ ├── eth.h │ │ ├── fw.h │ │ ├── icmp.h │ │ ├── intf.h │ │ ├── ip.h │ │ ├── ip6.h │ │ ├── os.h │ │ ├── rand.h │ │ ├── route.h │ │ ├── tcp.h │ │ ├── tun.h │ │ └── udp.h │ ├── err.h │ └── queue.h ├── libdnet_win │ ├── dnet.h │ └── dnet │ │ ├── addr.h │ │ ├── macros.h │ │ ├── os.h │ │ └── route.h ├── libnet │ ├── bpf.h │ ├── config.h │ ├── getopt.h │ ├── gnuc.h │ ├── ifaddrlist.h │ ├── libnet.h │ ├── libnet │ │ ├── libnet-asn1.h │ │ ├── libnet-functions.h │ │ ├── libnet-headers.h │ │ ├── libnet-macros.h │ │ ├── libnet-structures.h │ │ ├── libnet-types.h │ │ └── stdint.h │ ├── libnet_posix.h │ └── libnet_win.h ├── libpcap │ ├── pcap-bpf.h │ ├── pcap-namedb.h │ ├── pcap.h │ └── pcap │ │ ├── bluetooth.h │ │ ├── bpf.h │ │ ├── can_socketcan.h │ │ ├── dlt.h │ │ ├── export-defs.h │ │ ├── ipnet.h │ │ ├── namedb.h │ │ ├── nflog.h │ │ ├── pcap.h │ │ ├── sll.h │ │ ├── usb.h │ │ └── vlan.h ├── libuv │ ├── android-ifaddrs.h │ ├── pthread-barrier.h │ ├── stdint-msvc2008.h │ ├── tree.h │ ├── uv-aix.h │ ├── uv-bsd.h │ ├── uv-darwin.h │ ├── uv-errno.h │ ├── uv-linux.h │ ├── uv-os390.h │ ├── uv-posix.h │ ├── uv-sunos.h │ ├── uv-threadpool.h │ ├── uv-unix.h │ ├── uv-version.h │ ├── uv-win.h │ └── uv.h └── winpcap │ ├── Packet32.h │ ├── Win32-Extensions.h │ ├── bittypes.h │ ├── ip6_misc.h │ ├── pcap-bpf.h │ ├── pcap-namedb.h │ ├── pcap-stdinc.h │ ├── pcap.h │ ├── pcap │ ├── bluetooth.h │ ├── bpf.h │ ├── namedb.h │ ├── pcap.h │ ├── sll.h │ ├── usb.h │ └── vlan.h │ └── remote-ext.h ├── lib ├── Darwin_x86_64 │ ├── libdnet.a │ ├── libnet.a │ ├── libpcap.a │ └── libuv.a ├── Linux_x86_64 │ ├── libdnet.a │ ├── libnet.a │ ├── libpcap.a │ └── libuv.a └── Windows_x86 │ ├── dnet.dll │ ├── dnet.lib │ ├── libnet.dll │ ├── libuv.dll │ ├── net.lib │ ├── packet.lib │ ├── ucrtbased.dll │ ├── uv.lib │ ├── vcruntime140d.dll │ └── wpcap.lib └── sh ├── base_func.sh └── travis_install.sh /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | /local.properties 3 | /.idea/* 4 | .DS_Store 5 | /build 6 | /captures 7 | .externalNativeBuild 8 | cmake-build-debug/ 9 | *.gch 10 | *.out 11 | *.dSYM 12 | *.o 13 | a.out* 14 | /xbuild/build 15 | .vs 16 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | ### linux & osx dynamic linking, linux & os partially static linking 2 | # newer cmake, gcc 3 | language: cpp 4 | os: 5 | - linux 6 | - osx 7 | 8 | compiler: 9 | - gcc 10 | - clang 11 | 12 | matrix: 13 | exclude: 14 | - os: linux 15 | compiler: clang 16 | 17 | - os: osx 18 | compiler: gcc 19 | 20 | before_install: 21 | - if which apt-get ; then sudo sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y && sudo apt-get update -q; fi 22 | install: 23 | - if which apt-get; then sudo aptitude -f install g++-6 -y; fi 24 | before_script: 25 | - sudo chmod a+x xbuild/sh/travis_install.sh 26 | script: 27 | - ./xbuild/sh/travis_install.sh -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: 1.0.{build}-{branch} 2 | 3 | branches: 4 | only: 5 | - master 6 | - dev 7 | - windows 8 | skip_tags: true 9 | 10 | skip_commits: 11 | message: \[ci skip\]|\[skip ci\] # Regex for matching commit message 12 | 13 | max_jobs: 1 14 | 15 | image: Visual Studio 2017 16 | 17 | clone_folder: c:\projects\myproject 18 | 19 | init: 20 | - ps: Update-AppveyorBuild -Version "$(Get-Date -format yyyy-MM-dd).$env:appveyor_build_number" 21 | 22 | install: 23 | - cd c:\projects\myproject 24 | - mkdir build 25 | - cd build 26 | - echo %APPVEYOR_REPO_COMMIT_TIMESTAMP% > VERSION.txt 27 | - git rev-parse HEAD >> VERSION.txt 28 | - cmake -DCMAKE_TOOLCHAIN_FILE=..\xbuild\cmake\Windows_x86.toolchain.cmake .. 29 | 30 | configuration: Debug 31 | 32 | build: 33 | parallel: true # enable MSBuild parallel builds 34 | project: build\rSock.sln # path to Visual Studio solution or project 35 | 36 | matrix: 37 | fast_finish: true 38 | 39 | exclude: # only x86 32bit debug 40 | - platform: x64 41 | 42 | - platform: 86 43 | configuration: Release 44 | 45 | after_build: 46 | - 7z a rsock-Windows-x86-%APPVEYOR_BUILD_VERSION%.zip %APPVEYOR_BUILD_FOLDER%\build\Debug\*.exe %APPVEYOR_BUILD_FOLDER%\xbuild\lib\Windows_x86\*.dll %APPVEYOR_BUILD_FOLDER%\build\VERSION.txt 47 | - appveyor PushArtifact rsock-Windows-x86-%APPVEYOR_BUILD_VERSION%.zip 48 | 49 | #environment: 50 | # MY_MAIL: 51 | # secure: qovhbBYOQ/jzEyiE3AGBA/Na4cUhT+JkzWJ7ppq/avI= 52 | 53 | #notifications: 54 | # - provider: mail 55 | # to: 56 | # - %MY_MAIL% 57 | # subject: 'Build {{status}}' # optional 58 | # message: "{{message}}, {{commitId}}, ..." # optional 59 | # on_build_status_changed: true 60 | -------------------------------------------------------------------------------- /bean/ConnInfo.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/17/18. 3 | // 4 | 5 | #include 6 | #include 7 | #include "ConnInfo.h" 8 | #include "../util/enc.h" 9 | #include "../util/rsutil.h" 10 | #include 11 | 12 | char *ConnInfo::Encode(char *buf, int len) const { 13 | if (len < sizeof(*this)) { 14 | return nullptr; 15 | } 16 | char *p = encode_uint32(src, buf); 17 | p = encode_uint32(dst, p); 18 | p = encode_uint16(sp, p); 19 | p = encode_uint16(dp, p); 20 | return p; 21 | } 22 | 23 | const char *ConnInfo::Decode(const char *buf, int len) { 24 | if (len < sizeof(src) + sizeof(dst) + sizeof(sp) + sizeof(dp)) { 25 | return nullptr; 26 | } 27 | auto p = decode_uint32(&src, buf); 28 | p = decode_uint32(&dst, p); 29 | p = decode_uint16(&sp, p); 30 | p = decode_uint16(&dp, p); 31 | return p; 32 | } 33 | 34 | std::string ConnInfo::ToStr() const { 35 | std::ostringstream out; 36 | out << (IsUdp() ? "udp" : "tcp") << ":"; 37 | out << "src:" << InAddr2Ip(src) << ", sp:" << sp; 38 | out << ", dst:" << InAddr2Ip(dst) << ", dp: " << dp; 39 | return out.str(); 40 | } 41 | 42 | void ConnInfo::Reverse() { 43 | std::swap(src, dst); 44 | std::swap(sp, dp); 45 | } 46 | 47 | char *ConnInfo::EncodeBase(char *buf, int len) const { 48 | return this->ConnInfo::Encode(buf, len); 49 | } 50 | 51 | const char *ConnInfo::DecodeBase(const char *buf, int len) { 52 | return this->ConnInfo::Decode(buf, len); 53 | } 54 | -------------------------------------------------------------------------------- /bean/ConnInfo.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/17/18. 3 | // 4 | 5 | #ifndef RSOCK_NETINFO_H 6 | #define RSOCK_NETINFO_H 7 | 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | struct EncHead; 14 | struct sockaddr; 15 | 16 | struct ConnInfo { 17 | uint32_t src = 0; 18 | uint32_t dst = 0; 19 | uint16_t sp = 0; 20 | uint16_t dp = 0; 21 | EncHead *head = nullptr; // not responsable to free this pointer 22 | 23 | virtual ~ConnInfo() = default; 24 | 25 | virtual bool IsUdp() const { return true; } 26 | 27 | virtual char *Encode(char *buf, int len) const; 28 | 29 | char *EncodeBase(char *buf, int len) const;; 30 | 31 | virtual const char *Decode(const char *buf, int len); 32 | 33 | const char *DecodeBase(const char *buf, int len); 34 | 35 | virtual std::string ToStr() const; 36 | 37 | ConnInfo() = default; 38 | 39 | ConnInfo(const ConnInfo &info) = default; 40 | 41 | ConnInfo &operator=(const ConnInfo &) = default; 42 | 43 | virtual void Reverse(); 44 | }; 45 | 46 | #endif //RSOCK_NETINFO_H -------------------------------------------------------------------------------- /bean/EncHead.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/16/18. 3 | // 4 | 5 | #include "EncHead.h" 6 | #include "../util/enc.h" 7 | #include "../src/util/KeyGenerator.h" 8 | 9 | char *EncHead::Enc2Buf(char *p, int buf_len) { 10 | if (p && buf_len >= GetSize()) { 11 | auto old = p; 12 | this->len = GetSize(); 13 | p = encode_uint8(this->len, p); // len 14 | p = encode_uint8(mCmd, p); // cmd 15 | std::copy(mIdBuf.begin(), mIdBuf.end(), p); 16 | p += mIdBuf.size(); // id 17 | p = encode_uint32(mConv, p); // conv 18 | p = KeyGenerator::EncodeKey(p, mConnKey); // key 19 | p = encode_uint8(resereved, p); // reserved 20 | return old + this->len; 21 | } 22 | return nullptr; 23 | } 24 | 25 | // 19 right now 26 | uint8_t EncHead::GetMinEncSize() { 27 | return 0 28 | + sizeof(uint8_t) // len. not used right now 29 | + sizeof(uint8_t) // cmd 30 | + ID_BUF_SIZE // id_buf 31 | + sizeof(uint32_t) // conv 32 | + sizeof(IntKeyType) // netConnKey 33 | + sizeof(uint8_t) // resereved 34 | ; 35 | 36 | 37 | } 38 | 39 | const char *EncHead::DecodeBuf(EncHead &e, const char *p, int buf_len) { 40 | if (p && buf_len >= GetMinEncSize()) { 41 | auto old = p; 42 | p = decode_uint8(&e.len, p); 43 | if (e.len > buf_len) { 44 | return nullptr; 45 | } 46 | p = decode_uint8(&e.mCmd, p); 47 | std::copy(p, p + e.mIdBuf.size(), e.mIdBuf.begin()); 48 | p += e.mIdBuf.size(); 49 | p = decode_uint32(&e.mConv, p); 50 | p = KeyGenerator::DecodeKey(p, &e.mConnKey); // caution here. mConnKey must be right type 51 | p = decode_uint8(&e.resereved, p); 52 | return old + e.len; 53 | } 54 | return nullptr; 55 | } 56 | 57 | uint8_t EncHead::GetSize() { 58 | return GetMinEncSize(); 59 | } 60 | -------------------------------------------------------------------------------- /bean/EncHead.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/16/18. 3 | // 4 | 5 | #ifndef RSOCK_ENCHEAD_H 6 | #define RSOCK_ENCHEAD_H 7 | 8 | #include 9 | #include 10 | #include "rstype.h" 11 | 12 | struct EncHead { 13 | public: 14 | enum TYPE { 15 | TYPE_DATA = 0, 16 | TYPE_CONV_RST = 1, 17 | TYPE_NETCONN_RST = 2, 18 | TYPE_KEEP_ALIVE_REQ = 3, 19 | TYPE_KEEP_ALIVE_RESP = 4, 20 | }; 21 | 22 | private: 23 | // fields that are gonna encoded. { 24 | uint8_t len = EncHead::GetMinEncSize(); // head len 25 | uint8_t mCmd = TYPE_DATA; 26 | IdBufType mIdBuf{{0}}; 27 | uint32_t mConv = 0; 28 | IntKeyType mConnKey = 0; // represents inetconn 29 | uint8_t resereved = 0; 30 | // } 31 | 32 | public: 33 | // must correspond to fields that are gonna encoded; 15 bytes right now 34 | static uint8_t GetMinEncSize(); 35 | 36 | uint8_t GetSize(); 37 | 38 | static const char *DecodeBuf(EncHead &, const char *p, int buf_len); 39 | 40 | char *Enc2Buf(char *p, int buf_len); 41 | 42 | EncHead &operator=(const EncHead &) = default; 43 | 44 | uint8_t Cmd() { return mCmd; } 45 | 46 | void SetCmd(uint8_t cmd) { mCmd = cmd; } 47 | 48 | IdBufType IdBuf() { return mIdBuf; } 49 | 50 | void SetIdBuf(const IdBufType &idBufType) { mIdBuf = idBufType; } 51 | 52 | uint32_t Conv() { return mConv; } 53 | 54 | void SetConv(uint32_t conv) { mConv = conv; } 55 | 56 | void SetConnKey(IntKeyType key) { mConnKey = key; } 57 | 58 | IntKeyType ConnKey() { return mConnKey; } 59 | 60 | static bool IsRstFlag(uint8_t cmd) { return cmd == TYPE_CONV_RST || cmd == TYPE_NETCONN_RST; } 61 | 62 | static bool IsKeepAliveFlag(uint8_t cmd) { return cmd == TYPE_KEEP_ALIVE_RESP || cmd == TYPE_KEEP_ALIVE_REQ; } 63 | }; 64 | 65 | #endif //RSOCK_ENCHEAD_H 66 | -------------------------------------------------------------------------------- /bean/TcpInfo.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/17/18. 3 | // 4 | 5 | #include 6 | #include 7 | #include "TcpInfo.h" 8 | #include "../util/enc.h" 9 | 10 | uint32_t TcpInfo::UpdateAck(uint32_t ack) { 11 | this->ack = ack; 12 | return this->ack; 13 | } 14 | 15 | uint32_t TcpInfo::UpdateSeq(uint32_t seq) { 16 | this->seq = seq; 17 | return this->seq; 18 | } 19 | 20 | char *TcpInfo::Encode(char *buf, int len) const { 21 | if (len < sizeof(*this)) { 22 | return nullptr; 23 | } 24 | 25 | char *p = ConnInfo::Encode(buf, len); 26 | if (nullptr == p) { 27 | return nullptr; 28 | } 29 | p = encode_uint32(seq, p); 30 | p = encode_uint32(ack, p); 31 | p = encode_uint8(flag, p); 32 | return p; 33 | } 34 | 35 | const char *TcpInfo::Decode(const char *buf, int len) { 36 | auto p = ConnInfo::Decode(buf, len); 37 | if (!p) { 38 | return nullptr; 39 | } 40 | if (p - buf < (sizeof(seq) + sizeof(ack) + sizeof(flag))) { 41 | return nullptr; 42 | } 43 | p = decode_uint32(&seq, p); 44 | p = decode_uint32(&ack, p); 45 | p = decode_uint8(&flag, p); 46 | return p; 47 | } 48 | 49 | std::string TcpInfo::ToStr() const { 50 | auto s = ConnInfo::ToStr(); 51 | s += ", seq:" + std::to_string(seq) + ", ack: " + std::to_string(ack) + ", flag: " + std::to_string(flag); 52 | return s; 53 | } 54 | 55 | TcpInfo::TcpInfo(const ConnInfo &info) : ConnInfo(info) { 56 | seq = 0; 57 | ack = 0; 58 | } 59 | 60 | void TcpInfo::Reverse() { 61 | ConnInfo::Reverse(); 62 | std::swap(seq, ack); 63 | } 64 | -------------------------------------------------------------------------------- /bean/TcpInfo.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/17/18. 3 | // 4 | 5 | #ifndef RSOCK_TCPINFO_H 6 | #define RSOCK_TCPINFO_H 7 | 8 | #include 9 | #include "../cap/cap_headers.h" 10 | #include "ConnInfo.h" 11 | 12 | struct TcpInfo : ConnInfo { 13 | uint32_t seq = 0; 14 | uint32_t ack = 0; 15 | uint8_t flag = TH_ACK; 16 | 17 | uint32_t UpdateAck(uint32_t ack); 18 | 19 | uint32_t UpdateSeq(uint32_t seq); 20 | 21 | bool IsUdp() const override { return false; }; 22 | 23 | char *Encode(char *buf, int len) const override; 24 | 25 | const char *Decode(const char *buf, int len) override; 26 | 27 | std::string ToStr() const override; 28 | 29 | void Reverse() override; 30 | 31 | bool HasCloseFlag() { 32 | return static_cast(flag & (TH_FIN | TH_RST)); 33 | } 34 | 35 | TcpInfo() = default; 36 | 37 | TcpInfo(const TcpInfo &info) = default; 38 | 39 | TcpInfo(const ConnInfo &info); 40 | }; 41 | 42 | #endif //RSOCK_TCPINFO_H -------------------------------------------------------------------------------- /callbacks/ConnReset.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 2/12/18. 3 | // 4 | 5 | #ifndef RSOCK_RSTCONN_H 6 | #define RSOCK_RSTCONN_H 7 | 8 | #include 9 | #include "IReset.h" 10 | 11 | class IAppGroup; 12 | 13 | struct ConnInfo; 14 | struct rbuf_t; 15 | 16 | class ConnReset : public IReset { 17 | public: 18 | explicit ConnReset(IAppGroup *appGroup); 19 | 20 | int SendNetConnRst(const ConnInfo &src, IntKeyType key) override; 21 | 22 | int SendConvRst(uint32_t conv) override; 23 | 24 | int Input(uint8_t cmd, ssize_t nread, const rbuf_t &rbuf) override; 25 | 26 | void Close() override; 27 | 28 | int OnRecvNetConnRst(const ConnInfo &src, IntKeyType key) override; 29 | 30 | int OnRecvConvRst(const ConnInfo &src, uint32_t rstConv) override; 31 | 32 | private: 33 | IAppGroup *mAppGroup = nullptr; 34 | }; 35 | 36 | 37 | #endif //RSOCK_RSTCONN_H 38 | -------------------------------------------------------------------------------- /callbacks/INetConnKeepAlive.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 2/22/18. 3 | // 4 | 5 | #ifndef RSOCK_IKEEPALIVE_H 6 | #define RSOCK_IKEEPALIVE_H 7 | 8 | #include "rcommon.h" 9 | #include "rscomm.h" 10 | 11 | class INetConn; 12 | 13 | struct ConnInfo; 14 | 15 | class INetConnKeepAlive { 16 | public: 17 | virtual ~INetConnKeepAlive() = default; 18 | 19 | virtual int Init() = 0; 20 | 21 | virtual int Input(uint8_t cmd, ssize_t nread, const rbuf_t &rbuf) = 0; 22 | 23 | virtual int SendResponse(IntKeyType connKey) = 0; 24 | 25 | virtual int SendRequest(IntKeyType connKey) = 0; 26 | 27 | // virtual int InputResponse(ssize_t nread, const rbuf_t &rbuf) = 0; 28 | 29 | virtual int Close() = 0; 30 | 31 | virtual int OnRecvResponse(IntKeyType connKey) = 0; 32 | 33 | virtual int RemoveRequest(IntKeyType connkey) = 0; 34 | 35 | virtual int RemoveAllRequest() = 0; 36 | }; 37 | 38 | #endif //RSOCK_IKEEPALIVE_H 39 | -------------------------------------------------------------------------------- /callbacks/IReset.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 2/24/18. 3 | // 4 | 5 | #ifndef RSOCK_IRESET_H 6 | #define RSOCK_IRESET_H 7 | 8 | #include "rcommon.h" 9 | #include "rscomm.h" 10 | 11 | struct ConnInfo; 12 | 13 | class IReset { 14 | public: 15 | virtual ~IReset() = default; 16 | 17 | virtual void Close() = 0; 18 | 19 | virtual int Input(uint8_t cmd, ssize_t nread, const rbuf_t &rbuf) = 0; 20 | 21 | virtual int SendConvRst(uint32_t conv) = 0; 22 | 23 | virtual int SendNetConnRst(const ConnInfo &src, IntKeyType key) = 0; 24 | 25 | virtual int OnRecvNetConnRst(const ConnInfo &src, IntKeyType key) = 0; 26 | 27 | virtual int OnRecvConvRst(const ConnInfo &src, uint32_t rstConv) = 0; 28 | }; 29 | 30 | #endif //RSOCK_IRESET_H 31 | -------------------------------------------------------------------------------- /callbacks/KeepAliveRouteObserver.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/12/18. 3 | // 4 | 5 | #include 6 | #include "KeepAliveRouteObserver.h" 7 | #include "INetConnKeepAlive.h" 8 | #include "../src/service/ServiceUtil.h" 9 | #include "../src/service/RouteService.h" 10 | 11 | KeepAliveRouteObserver::KeepAliveRouteObserver(INetConnKeepAlive *keepAlive) { 12 | mKeepAlive = keepAlive; 13 | } 14 | 15 | void KeepAliveRouteObserver::OnNetConnected(const std::string &ifName, const std::string &ip) { 16 | LOGD << "Online, remove all pending request"; 17 | mAlive = true; 18 | mKeepAlive->RemoveAllRequest(); 19 | } 20 | 21 | void KeepAliveRouteObserver::OnNetDisconnected() { 22 | LOGD << "Offline, remove all pending request"; 23 | mAlive = false; 24 | mKeepAlive->RemoveAllRequest(); 25 | } 26 | 27 | int KeepAliveRouteObserver::Init() { 28 | return ServiceUtil::GetService(ServiceManager::ROUTE_SERVICE)->RegisterObserver(this); 29 | } 30 | 31 | int KeepAliveRouteObserver::Close() { 32 | return ServiceUtil::GetService(ServiceManager::ROUTE_SERVICE)->UnRegisterObserver(this); 33 | } 34 | 35 | bool KeepAliveRouteObserver::Online() const { 36 | return mAlive; 37 | } 38 | -------------------------------------------------------------------------------- /callbacks/KeepAliveRouteObserver.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/12/18. 3 | // 4 | 5 | #ifndef RSOCK_KEEPALIVEROUTEOBSERVER_H 6 | #define RSOCK_KEEPALIVEROUTEOBSERVER_H 7 | 8 | #include "../src/service/IRouteObserver.h" 9 | 10 | class INetConnKeepAlive; 11 | 12 | class KeepAliveRouteObserver : public IRouteObserver { 13 | public: 14 | explicit KeepAliveRouteObserver(INetConnKeepAlive *keepAlive); 15 | 16 | int Init() override; 17 | 18 | int Close() override; 19 | 20 | void OnNetConnected(const std::string &ifName, const std::string &ip) override; 21 | 22 | void OnNetDisconnected() override; 23 | 24 | bool Online() const; 25 | 26 | private: 27 | INetConnKeepAlive *mKeepAlive = nullptr; 28 | bool mAlive = true; 29 | }; 30 | 31 | 32 | #endif //RSOCK_KEEPALIVEROUTEOBSERVER_H 33 | -------------------------------------------------------------------------------- /callbacks/NetConnKeepAlive.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 2/23/18. 3 | // 4 | 5 | #ifndef RSOCK_APPGROUPKEEPALIVE_H 6 | #define RSOCK_APPGROUPKEEPALIVE_H 7 | 8 | #include 9 | #include "INetConnKeepAlive.h" 10 | #include "../src/service/ITimerObserver.h" 11 | 12 | class IAppGroup; 13 | 14 | class IReset; 15 | 16 | class KeepAliveRouteObserver; 17 | 18 | /* 19 | * The class will send keepalive request during interval seconds. 20 | * If it doesn't receive after 3 trials, it will report the conn is dead 21 | */ 22 | class NetConnKeepAlive : public INetConnKeepAlive, public ITimerObserver { 23 | public: 24 | // be same as EncHead 25 | 26 | explicit NetConnKeepAlive(IAppGroup *group, IReset *reset, uint32_t flush_interval_ms); 27 | 28 | int Input(uint8_t cmd, ssize_t nread, const rbuf_t &rbuf) override; 29 | 30 | int SendResponse(IntKeyType connKey) override; 31 | 32 | int SendRequest(IntKeyType connKey) override; 33 | 34 | int Close() override; 35 | 36 | int OnRecvResponse(IntKeyType connKey) override; 37 | 38 | int Init() override; 39 | 40 | void OnFlush(uint64_t timestamp) override; 41 | 42 | uint64_t IntervalMs() const override; 43 | 44 | int RemoveRequest(IntKeyType connKey) override; 45 | 46 | int RemoveAllRequest() override; 47 | 48 | private: 49 | void onNetConnDead(IntKeyType key); 50 | 51 | // in case some invalid request is still in the pool. remove them 52 | void removeInvalidRequest(); 53 | 54 | bool canSendRequest(INetConn *conn, uint64_t timestampMs) const; 55 | 56 | private: 57 | const int MAX_RETRY = 3; 58 | const uint32_t FLUSH_INTERVAL = 0; // shoud be same with RConfig.keepAlive 59 | KeepAliveRouteObserver *mRouteObserver = nullptr; 60 | IAppGroup *mAppGroup = nullptr; 61 | std::map mReqMap; 62 | IReset *mReset = nullptr; 63 | static const int REQUEST_DELAY = 15000; // 15s 64 | }; 65 | 66 | 67 | #endif //RSOCK_APPGROUPKEEPALIVE_H 68 | -------------------------------------------------------------------------------- /callbacks/RConnReset.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 2/20/18. 3 | // 4 | 5 | #include 6 | #include "RConnReset.h" 7 | #include "../conn/RConn.h" 8 | #include "../bean/TcpInfo.h" 9 | #include "../util/rsutil.h" 10 | 11 | RConnReset::RConnReset(RConn *rConn) { 12 | assert(rConn); 13 | mConn = rConn; 14 | } 15 | 16 | int RConnReset::SendReset(const ConnInfo &info) { 17 | if (!info.IsUdp()) { 18 | TcpInfo tcpInfo(info); 19 | tcpInfo.flag = TH_RST; 20 | return mConn->ResetSend(tcpInfo); 21 | } else { 22 | // todo: send icmp port unreachable 23 | } 24 | return 0; 25 | } 26 | 27 | void RConnReset::Close() { 28 | mConn = nullptr; 29 | } 30 | -------------------------------------------------------------------------------- /callbacks/RConnReset.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 2/20/18. 3 | // 4 | 5 | #ifndef RSOCK_RCONNRESET_H 6 | #define RSOCK_RCONNRESET_H 7 | 8 | struct ConnInfo; 9 | 10 | class RConn; 11 | 12 | class RConnReset { 13 | public: 14 | explicit RConnReset(RConn *rConn); 15 | 16 | virtual ~RConnReset() = default; 17 | 18 | virtual int SendReset(const ConnInfo &info); 19 | 20 | virtual void Close(); 21 | 22 | private: 23 | RConn *mConn = nullptr; 24 | }; 25 | 26 | 27 | #endif //RSOCK_RCONNRESET_H 28 | -------------------------------------------------------------------------------- /cap/CapUtil.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/2/18. 3 | // 4 | 5 | #include 6 | #include 7 | #include "CapUtil.h" 8 | 9 | int CapUtil::DataLink(const std::string &dev) { 10 | char err[PCAP_ERRBUF_SIZE] = {0}; 11 | const int TIMEOUT = 20; 12 | pcap_t *cap = pcap_open_live(dev.c_str(), 65535, 0, TIMEOUT, err); 13 | if (!cap) { 14 | LOGE << "failed to open on " << dev << ": " << err; 15 | return -1; 16 | } 17 | int link = pcap_datalink(cap); 18 | pcap_close(cap); 19 | return link; 20 | } 21 | -------------------------------------------------------------------------------- /cap/CapUtil.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/2/18. 3 | // 4 | 5 | #ifndef RSOCK_CAPUTIL_H 6 | #define RSOCK_CAPUTIL_H 7 | 8 | #include 9 | 10 | class CapUtil { 11 | public: 12 | static int DataLink(const std::string &dev); 13 | }; 14 | 15 | 16 | #endif //RSOCK_CAPUTIL_H 17 | -------------------------------------------------------------------------------- /cap/RCap.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 12/23/17. 3 | // 4 | 5 | #ifndef RSOCK_RCAP_H 6 | #define RSOCK_RCAP_H 7 | 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | #include "cap_util.h" 14 | #include "../util/RPortList.h" 15 | #include "../src/service/IRouteObserver.h" 16 | 17 | class RCap : public IRouteObserver { 18 | public: 19 | struct CapThreadArgs { 20 | RCap *instance = nullptr; 21 | pcap_handler handler = nullptr; 22 | u_char *args = nullptr; 23 | }; 24 | 25 | RCap(const std::string &dev, const std::string &selfIp, const RPortList &selfPorts, const RPortList &srcPorts, 26 | const std::string &srcIp, int timeout_ms, bool server); 27 | 28 | // ~RCap() override = default; 29 | 30 | int Init() override; 31 | 32 | int Close() override; 33 | 34 | // wait thread to close 35 | virtual int WaitAndClose(); 36 | 37 | // run in a seperate thread. 38 | uv_thread_t Start(pcap_handler handler, u_char *args); 39 | 40 | // run in the calling thread. 41 | void Run(pcap_handler handler, u_char *args); 42 | 43 | int Datalink(); 44 | 45 | virtual void SetCapHandler(pcap_handler handler, u_char *args); 46 | 47 | virtual void HandlePkt(u_char *args, const struct pcap_pkthdr *hdr, const u_char *pkt); 48 | 49 | void OnNetConnected(const std::string &ifName, const std::string &ip) override; 50 | 51 | void OnNetDisconnected() override; 52 | 53 | protected: 54 | static void threadCb(void *threadArg); 55 | 56 | static void capHandler(u_char *, const struct pcap_pkthdr *, const u_char *); 57 | 58 | private: 59 | int initDevAndIp(); 60 | 61 | int doInit(); 62 | 63 | void joinPcapThread(); 64 | 65 | private: 66 | std::string mSrcIp; 67 | std::string mDstIp; 68 | std::string mDev; 69 | pcap_t *mCap = nullptr; 70 | RPortList mSrcPorts; 71 | RPortList mDestPorts; 72 | bool mDone = false; 73 | const int TIMEOUT; 74 | 75 | bool mInited = false; 76 | u_char *mArgs = nullptr; 77 | pcap_handler mHandler = nullptr; 78 | bool mIsServer = false; 79 | uv_thread_t mCapThread = 0; 80 | }; 81 | 82 | 83 | #endif //RSOCK_RCAP_H 84 | -------------------------------------------------------------------------------- /cap/cap_headers.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 12/11/17. 3 | // 4 | 5 | #ifndef OMNIPIPE_CAP_HEADERS_H 6 | #define OMNIPIPE_CAP_HEADERS_H 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | #include 13 | #include "os.h" 14 | 15 | 16 | #define OM_PROTO_IP 0x0008 // network order 17 | //#define OM_PROTO_IP 0x0800 // little endian 18 | #define OM_IPV4 0x4 19 | 20 | /* source: www.tcpdump.org/pcap.htm */ 21 | /* Ethernet addresses are 6 bytes */ 22 | #ifndef ETHER_ADDR_LEN 23 | #define ETHER_ADDR_LEN 6 24 | #endif // !ETHER_ADDR_LEN 25 | 26 | 27 | /* Ethernet header */ 28 | struct oetherhdr { 29 | u_char ether_dhost[ETHER_ADDR_LEN]; /* Destination host address */ 30 | u_char ether_shost[ETHER_ADDR_LEN]; /* Source host address */ 31 | u_short ether_type; /* IP? ARP? RARP? etc */ 32 | }; 33 | 34 | #define IP_HL(ip) (((ip)->ip_vhl) & 0x0f) 35 | #define IP_V(ip) (((ip)->ip_vhl) >> 4) 36 | 37 | #ifdef __cplusplus 38 | } 39 | #endif 40 | #endif //OMNIPIPE_CAP_HEADERS_H 41 | -------------------------------------------------------------------------------- /cap/cap_util.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 12/22/17. 3 | // 4 | 5 | #ifndef RSOCK_CAP_UTIL_H 6 | #define RSOCK_CAP_UTIL_H 7 | 8 | 9 | //typedef char CAP_ERR_TYPE[PCAP_ERRBUF_SIZE]; 10 | 11 | #include 12 | #include 13 | #include 14 | #include "rstype.h" 15 | 16 | class RPortList; 17 | struct in_addr; 18 | 19 | uint32_t NetIntOfIp(const char *ip); 20 | 21 | bool DevIpMatch(const std::string &dev, const std::string &ip); 22 | 23 | int devWithIpv4(std::string &devName, const std::string &ip); 24 | 25 | int ipv4OfDev(const char *dev, char *ip_buf, char *err); 26 | 27 | const std::string BuildFilterStr(const std::string &proto, const std::string &srcIp, const std::string &dstIp, 28 | RPortList &srcPorts, RPortList &dstPorts, bool isServer); 29 | 30 | #endif //RSOCK_CAP_UTIL_H 31 | -------------------------------------------------------------------------------- /client/CConnErrHandler.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/9/18. 3 | // 4 | 5 | #include 6 | #include 7 | #include "CConnErrHandler.h" 8 | #include "CSockApp.h" 9 | #include "ClientNetManager.h" 10 | #include "../bean/ConnInfo.h" 11 | #include "ClientGroup.h" 12 | #include "../conn/INetGroup.h" 13 | 14 | CConnErrHandler::CConnErrHandler(CSockApp *app) { 15 | mApp = app; 16 | } 17 | 18 | void CConnErrHandler::OnNetConnErr(const ConnInfo &info, int errCode) { 19 | if (!mApp->IsClosing()) { 20 | auto manager = mApp->GetNetManager(); 21 | auto *clientNetManager = dynamic_cast(manager); 22 | assert(clientNetManager); 23 | LOGE << "conn " << info.ToStr() << ", err, reconnect it"; 24 | if (!info.IsUdp()) { 25 | auto cb = std::bind(&CConnErrHandler::TcpDialAsyncCb, this, std::placeholders::_1, std::placeholders::_2); 26 | ConnInfo newInfo = info; 27 | newInfo.sp = 0; // sp = 0; 28 | clientNetManager->DialTcpAsync(newInfo, cb); 29 | } else { 30 | // todo: dial udp. succeeds immediately 31 | } 32 | } 33 | } 34 | 35 | void CConnErrHandler::TcpDialAsyncCb(INetConn *conn, const ConnInfo &info) { 36 | auto *clientGroup = dynamic_cast(mApp->GetBridgeConn()); 37 | assert(clientGroup); 38 | LOGD << "reconnecting conn " << info.ToStr() << ((conn != nullptr) ? " succeeds." : "failed"); 39 | if (conn) { 40 | if (conn->Init()) { 41 | LOGE << "conn " << conn->ToStr() << " init failed"; 42 | conn->Close(); 43 | delete conn; 44 | return; 45 | } 46 | clientGroup->GetNetGroup()->AddNetConn(conn); 47 | } 48 | } 49 | 50 | int CConnErrHandler::Close() { 51 | mApp = nullptr; 52 | return 0; // remove all callback in clientNetManager? 53 | } 54 | 55 | -------------------------------------------------------------------------------- /client/CConnErrHandler.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/9/18. 3 | // 4 | 5 | #ifndef RSOCK_CNONNERRHANDLER_H 6 | #define RSOCK_CNONNERRHANDLER_H 7 | 8 | 9 | #include "../conn/INetConnErrorHandler.h" 10 | #include "../src/util/ICloseable.h" 11 | 12 | class CSockApp; 13 | 14 | class INetConn; 15 | 16 | class CConnErrHandler : public INetConnErrorHandler, public ICloseable { 17 | public: 18 | explicit CConnErrHandler(CSockApp *app); 19 | 20 | void OnNetConnErr(const ConnInfo &info, int errCode) override; 21 | 22 | void TcpDialAsyncCb(INetConn *conn, const ConnInfo &info); 23 | 24 | int Close() override; 25 | 26 | private: 27 | CSockApp *mApp = nullptr; 28 | }; 29 | 30 | 31 | #endif //RSOCK_CNONNERRHANDLER_H 32 | -------------------------------------------------------------------------------- /client/CNetGroup.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/17/18. 3 | // 4 | 5 | #include "CNetGroup.h" 6 | #include "../src/service/RouteService.h" 7 | #include "../src/service/ServiceUtil.h" 8 | 9 | CNetGroup::CNetGroup(const std::string &groupId, uv_loop_t *loop) : INetGroup(groupId, loop) {} 10 | 11 | INetConn *CNetGroup::CreateNetConn(IntKeyType key, const ConnInfo *info) { 12 | return nullptr; 13 | } 14 | 15 | int CNetGroup::Send(ssize_t nread, const rbuf_t &rbuf) { 16 | int n = INetGroup::Send(nread, rbuf); 17 | if (ERR_NO_CONN == n) { // no connection 18 | auto *service = ServiceUtil::GetService(ServiceManager::ROUTE_SERVICE); 19 | service->CheckNetworkStatusDelayed(); 20 | } 21 | return n; 22 | } 23 | -------------------------------------------------------------------------------- /client/CNetGroup.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/17/18. 3 | // 4 | 5 | #ifndef RSOCK_CNETGROUP_H 6 | #define RSOCK_CNETGROUP_H 7 | 8 | 9 | #include "../conn/INetGroup.h" 10 | 11 | class CNetGroup : public INetGroup { 12 | public: 13 | CNetGroup(const std::string &groupId, uv_loop_t *loop); 14 | 15 | INetConn *CreateNetConn(IntKeyType key, const ConnInfo *info) override; 16 | 17 | int Send(ssize_t nread, const rbuf_t &rbuf) override; 18 | }; 19 | 20 | 21 | #endif //RSOCK_CNETGROUP_H 22 | -------------------------------------------------------------------------------- /client/CSockApp.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/26/17. 3 | // 4 | 5 | #ifndef RSOCK_CSOCKAPP_H 6 | #define RSOCK_CSOCKAPP_H 7 | 8 | 9 | #include "../src/ISockApp.h" 10 | #include "../conn/IGroup.h" 11 | 12 | struct ConnInfo; 13 | 14 | class INetConn; 15 | 16 | class INetConnErrorHandler; 17 | 18 | class CNetGroup; 19 | 20 | class CSockApp : public ISockApp { 21 | public: 22 | explicit CSockApp(); 23 | 24 | RCap *CreateCap(const RConfig &conf) override; 25 | 26 | RConn *CreateBtmConn(RConfig &conf, uv_loop_t *loop, TcpAckPool *ackPool) override; 27 | 28 | IConn *CreateBridgeConn(RConfig &conf, IConn *btm, uv_loop_t *loop, INetManager *netManager) override; 29 | 30 | INetManager *CreateNetManager(const RConfig &conf, uv_loop_t *loop, TcpAckPool *ackPool) override; 31 | 32 | int Init() override; 33 | 34 | void Close() override; 35 | 36 | private: 37 | void addUdpNetConn(CNetGroup *group, IGroup *btm); 38 | 39 | void addTcpNetConn(RConfig &conf, CNetGroup *group, INetManager *netManager); 40 | 41 | private: 42 | INetConnErrorHandler *mErrorHandler = nullptr; 43 | }; 44 | 45 | 46 | #endif //RSOCK_CSOCKAPP_H 47 | -------------------------------------------------------------------------------- /client/ClientGroup.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 12/17/17. 3 | // 4 | 5 | #ifndef RSOCK_ICLIENTGROUP_H 6 | #define RSOCK_ICLIENTGROUP_H 7 | 8 | #include "os.h" 9 | 10 | #include "../conn/IAppGroup.h" 11 | 12 | class CConn; 13 | 14 | class RPortList; 15 | 16 | class ClientNetObserver; 17 | 18 | class ClientGroup : public IAppGroup { 19 | public: 20 | ClientGroup(const std::string &groupId, const std::string &listenUnPath, const std::string &listenUdpIp, 21 | uint16_t listenUdpPort, uv_loop_t *loop, INetGroup *fakeGroup, IConn *btm); 22 | 23 | int Init() override; 24 | 25 | int OnRecv(ssize_t nread, const rbuf_t &rbuf) override; 26 | 27 | int Close() override; 28 | 29 | bool RemoveConn(IConn *conn) override; 30 | 31 | private: 32 | int subconnRecv(ssize_t nread, const rbuf_t &rbuf); 33 | 34 | int send2Origin(ssize_t nread, const rbuf_t &rbuf, const sockaddr *origin); 35 | 36 | int unSendOrigin(ssize_t nread, const rbuf_t &rbuf, struct sockaddr_un *addr); 37 | 38 | static void send_cb(uv_udp_send_t *req, int status); 39 | 40 | static void udpRecvCb(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, 41 | unsigned flags); 42 | 43 | void onLocalRecv(ssize_t nread, const char *base, const struct sockaddr *addr); 44 | 45 | CConn *newConn(const std::string &key, const struct sockaddr *addr, uint32_t conv); 46 | 47 | static void pollCb(uv_poll_t *handle, int status, int events); 48 | 49 | int cconSend(ssize_t nread, const rbuf_t &rbuf); 50 | 51 | private: 52 | uint32_t mConvCounter = 1; 53 | 54 | uv_udp_t *mUdp = nullptr; 55 | uv_poll_t *mUnPoll = nullptr; 56 | ClientNetObserver *mNetObserver = nullptr; 57 | int mUnSock; 58 | 59 | struct sockaddr_in *mUdpAddr = nullptr; 60 | struct sockaddr_un *mUnAddr = nullptr; 61 | 62 | uv_loop_t *mLoop = nullptr; 63 | // necessary. it map clientAddr to conv, use this conv to communicate with server 64 | std::map mConvMap; 65 | }; 66 | 67 | 68 | #endif //RSOCK_ICLIENTGROUP_H 69 | -------------------------------------------------------------------------------- /client/ClientNetManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/20/18. 3 | // 4 | 5 | #ifndef RSOCK_NETMANAGER_H 6 | #define RSOCK_NETMANAGER_H 7 | 8 | #include 9 | #include 10 | 11 | #include "rscomm.h" 12 | #include "../bean/TcpInfo.h" 13 | #include "../net/INetManager.h" 14 | #include "../src/service/IRouteObserver.h" 15 | 16 | class BtmUdpConn; 17 | 18 | 19 | // must do initialization 20 | class ClientNetManager : public INetManager, public IRouteObserver { 21 | public: 22 | using NetDialCb = std::function; 23 | 24 | explicit ClientNetManager(uv_loop_t *loop, TcpAckPool *ackPool); 25 | 26 | int Init() override; 27 | 28 | int Close() override; 29 | 30 | INetConn *DialTcpSync(const ConnInfo &info); 31 | 32 | int DialTcpAsync(const ConnInfo &info, const NetDialCb &cb); 33 | 34 | void OnFlush(uint64_t now) override; 35 | 36 | void OnNetConnected(const std::string &ifName, const std::string &ip) override; 37 | 38 | void OnNetDisconnected() override; 39 | 40 | private: 41 | struct DialHelper { 42 | TcpInfo info; // no necessary to use reference or pointer 43 | INetConn *conn = nullptr; 44 | NetDialCb cb = nullptr; 45 | int nRetry = 0; 46 | uint64_t nextRetryMs = 0; 47 | uint64_t durationMs = 1000; 48 | uv_connect_t *req = nullptr; 49 | 50 | void dialFailed(uint64_t now); 51 | 52 | DialHelper &operator=(const DialHelper &) = default; // simple struct. use default copy 53 | }; 54 | 55 | private: 56 | static void connectCb(uv_connect_t *req, int status); 57 | 58 | void onTcpConnect(uv_connect_t *req, int status); 59 | 60 | void flushPending(uint64_t now); 61 | 62 | INetConn *createINetConn(uv_tcp_t *tcp); 63 | 64 | private: 65 | // std::numeric_limits::max() 66 | const int MAX_RETRY; // maximum number of times to try to dial 67 | 68 | std::list mPending; 69 | bool mNetworkAlive = true; 70 | }; 71 | 72 | 73 | #endif //RSOCK_NETMANAGER_H 74 | -------------------------------------------------------------------------------- /client/ClientNetObserver.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/9/18. 3 | // 4 | 5 | #include "ClientNetObserver.h" 6 | #include "ClientGroup.h" 7 | #include "../src/service/ServiceUtil.h" 8 | #include "../src/service/NetService.h" 9 | 10 | ClientNetObserver::ClientNetObserver(ClientGroup *group) { 11 | mGroup = group; 12 | } 13 | 14 | void ClientNetObserver::OnTcpFinOrRst(const TcpInfo &info) { 15 | mGroup->ProcessTcpFinOrRst(info); 16 | } 17 | 18 | int ClientNetObserver::Init() { 19 | return ServiceUtil::GetService(ServiceManager::NET_SERVICE)->RegisterObserver(this); 20 | } 21 | 22 | int ClientNetObserver::Close() { 23 | return ServiceUtil::GetService(ServiceManager::NET_SERVICE)->UnRegisterObserver(this); 24 | } 25 | -------------------------------------------------------------------------------- /client/ClientNetObserver.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/9/18. 3 | // 4 | 5 | #ifndef RSOCK_CLIENTNETOBSERVER_H 6 | #define RSOCK_CLIENTNETOBSERVER_H 7 | 8 | 9 | #include "../src/service/INetObserver.h" 10 | 11 | class ClientGroup; 12 | 13 | class ClientNetObserver : public INetObserver { 14 | public: 15 | explicit ClientNetObserver(ClientGroup *group); 16 | 17 | void OnTcpFinOrRst(const TcpInfo &info) override; 18 | 19 | int Init() override; 20 | 21 | int Close() override; 22 | 23 | private: 24 | ClientGroup *mGroup = nullptr; 25 | }; 26 | 27 | 28 | #endif //RSOCK_CLIENTNETOBSERVER_H 29 | -------------------------------------------------------------------------------- /client/csock.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "CSockApp.h" 3 | #include "csock.h" 4 | 5 | int csock_main(int argc, char **argv) { 6 | uv_default_loop(); 7 | ISockApp *app = new CSockApp(); 8 | 9 | int nret = app->Parse(argc, reinterpret_cast(argv)); 10 | if (!nret) { 11 | if (!(nret = app->Init())) { 12 | app->Start(); 13 | } 14 | } 15 | app->Close(); 16 | 17 | delete app; 18 | return nret; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /client/csock.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/27/17. 3 | // 4 | 5 | #ifndef RSOCK_CSOCK_MAIN_H 6 | #define RSOCK_CSOCK_MAIN_H 7 | 8 | int csock_main(int argc, char **argv); 9 | 10 | #endif //RSOCK_CSOCK_MAIN_H 11 | -------------------------------------------------------------------------------- /client/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/27/17. 3 | // 4 | 5 | #include "csock.h" 6 | 7 | int main(int argc, char *argv[]) { 8 | return csock_main(argc, argv); 9 | } -------------------------------------------------------------------------------- /conn/BtmUdpConn.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/16/18. 3 | // 4 | 5 | #ifndef RSOCK_UDPCONN_H 6 | #define RSOCK_UDPCONN_H 7 | 8 | 9 | #include "../bean/ConnInfo.h" 10 | #include "IBtmConn.h" 11 | 12 | 13 | // SetOutputCb is not callable 14 | class BtmUdpConn : public IBtmConn { 15 | public: 16 | BtmUdpConn(const std::string &key, uv_udp_t *udp, const ConnInfo &info); 17 | // BtmUdpConn(const std::string &key, uv_udp_t *udp); 18 | 19 | int Init() override; 20 | 21 | int Close() override; 22 | 23 | bool IsUdp() override; 24 | 25 | static void send_cb(uv_udp_send_t *req, int status); 26 | 27 | static void recv_cb(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, 28 | unsigned flags); 29 | 30 | ConnInfo *GetInfo() override { return &mInfo; } 31 | 32 | int Output(ssize_t nread, const rbuf_t &rbuf) override; 33 | 34 | bool Alive() override; 35 | 36 | private: 37 | void udpRecv(ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr); 38 | 39 | inline void fillInfo(const struct sockaddr *addr); 40 | 41 | private: 42 | uv_udp_t *mUdp = nullptr; 43 | ConnInfo mInfo; 44 | ConnInfo mSrcInfo; 45 | }; 46 | 47 | #endif //RSOCK_UDPCONN_H 48 | -------------------------------------------------------------------------------- /conn/CConn.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/25/17. 3 | // 4 | 5 | #include 6 | #include 7 | #include "CConn.h" 8 | #include "../util/rsutil.h" 9 | #include "os.h" 10 | 11 | CConn::CConn(const std::string &key, const SA *addr, uint32_t conv) : IConn(key) { 12 | mAddr = new_addr(addr); 13 | mConv = conv; 14 | } 15 | 16 | int CConn::Close() { 17 | IConn::Close(); 18 | if (mAddr) { 19 | free(mAddr); 20 | mAddr = nullptr; 21 | } 22 | return 0; 23 | } 24 | 25 | uint32_t CConn::Conv() { 26 | return mConv; 27 | } 28 | 29 | int CConn::Output(ssize_t nread, const rbuf_t &rbuf) { 30 | rbuf_t buf = new_buf(nread, rbuf, this); 31 | return IConn::Output(nread, buf); 32 | } 33 | 34 | int CConn::OnRecv(ssize_t nread, const rbuf_t &rbuf) { 35 | rbuf_t buf = new_buf(nread, rbuf, this); 36 | return IConn::OnRecv(nread, buf); 37 | } 38 | 39 | int CConn::Init() { 40 | IConn::Init(); 41 | SetPrintableStr(BuildPrintableStr(mAddr)); 42 | return 0; 43 | } 44 | 45 | const std::string CConn::BuildPrintableStr(const SA *addr) { 46 | if (!addr) { 47 | return "printableStr null"; 48 | } 49 | 50 | if (addr->sa_family == AF_INET) { 51 | struct sockaddr_in *addr4 = (struct sockaddr_in *) addr; 52 | std::ostringstream out; 53 | out << InAddr2Ip(addr4->sin_addr) << ":" << ntohs(addr4->sin_port); 54 | return out.str(); 55 | } 56 | return BuildKey(addr); 57 | } 58 | 59 | const std::string CConn::BuildKey(const SA *addr) { 60 | if (addr->sa_family == AF_INET) { 61 | struct sockaddr_in *addr4 = (struct sockaddr_in *) addr; 62 | uint64_t key = addr4->sin_addr.s_addr; 63 | key <<= 32; 64 | key |= ntohs(addr4->sin_port); 65 | return std::to_string(key); 66 | } else if (addr->sa_family == AF_UNIX) { 67 | struct sockaddr_un *un = (struct sockaddr_un *) addr; 68 | return un->sun_path; 69 | } 70 | return ""; 71 | } 72 | -------------------------------------------------------------------------------- /conn/CConn.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/25/17. 3 | // 4 | 5 | #ifndef RSOCK_CCONN_H 6 | #define RSOCK_CCONN_H 7 | 8 | 9 | #include 10 | #include "IConn.h" 11 | 12 | class CConn : public IConn { 13 | public: 14 | CConn(const std::string &key, const SA *addr, uint32_t conv); 15 | 16 | int Init() override; 17 | 18 | int Close() override; 19 | 20 | uint32_t Conv(); 21 | 22 | int Output(ssize_t nread, const rbuf_t &rbuf) override; 23 | 24 | int OnRecv(ssize_t nread, const rbuf_t &rbuf) override; 25 | 26 | SA *GetAddr() { return mAddr; } 27 | 28 | static const std::string BuildPrintableStr(const SA *addr); 29 | 30 | static const std::string BuildKey(const SA *addr); 31 | 32 | private: 33 | uint32_t mConv = 0; 34 | SA *mAddr = nullptr; 35 | }; 36 | 37 | 38 | #endif //RSOCK_CCONN_H 39 | -------------------------------------------------------------------------------- /conn/DefaultFakeConn.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 5/7/18. 3 | // 4 | 5 | #include "DefaultFakeConn.h" 6 | #include "../src/util/KeyGenerator.h" 7 | 8 | DefaultFakeConn::DefaultFakeConn() : INetConn(KeyGenerator::INVALID_KEY) {} 9 | 10 | bool DefaultFakeConn::Alive() { 11 | return true; 12 | } 13 | 14 | bool DefaultFakeConn::IsUdp() { 15 | return false; 16 | } 17 | 18 | ConnInfo *DefaultFakeConn::GetInfo() { 19 | return nullptr; 20 | } 21 | 22 | int DefaultFakeConn::OnRecv(ssize_t nread, const rbuf_t &rbuf) { 23 | return IConn::OnRecv(nread, rbuf); 24 | } 25 | 26 | int DefaultFakeConn::Init() { 27 | int nret = INetConn::Init(); 28 | if (nret) { 29 | return nret; 30 | } 31 | SetPrintableStr("DefaultFakeConn"); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /conn/DefaultFakeConn.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 5/7/18. 3 | // 4 | 5 | #ifndef RSOCK_DEFAULTFAKECONN_H 6 | #define RSOCK_DEFAULTFAKECONN_H 7 | 8 | 9 | #include "INetConn.h" 10 | 11 | class DefaultFakeConn : public INetConn { 12 | public: 13 | DefaultFakeConn(); 14 | 15 | // always return true 16 | bool Alive() override; 17 | 18 | bool IsUdp() override; 19 | 20 | ConnInfo *GetInfo() override; 21 | 22 | int OnRecv(ssize_t nread, const rbuf_t &rbuf) override; 23 | 24 | int Init() override; 25 | }; 26 | 27 | 28 | #endif //RSOCK_DEFAULTFAKECONN_H 29 | -------------------------------------------------------------------------------- /conn/FakeTcp.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/16/18. 3 | // 4 | 5 | #ifndef RSOCK_FAKETCP_H 6 | #define RSOCK_FAKETCP_H 7 | 8 | 9 | #include "INetConn.h" 10 | #include "../bean/TcpInfo.h" 11 | 12 | class FakeTcp : public INetConn { 13 | public: 14 | /* 15 | * @param info The info fetched from TcpAckPool. Don't forget to FakeTcp.seq and FakeTcp.ack with the info. 16 | */ 17 | FakeTcp(uv_tcp_t *tcp, IntKeyType key, const TcpInfo &info); 18 | 19 | int Init() override; 20 | 21 | int Output(ssize_t nread, const rbuf_t &rbuf) override; 22 | 23 | int OnRecv(ssize_t nread, const rbuf_t &buf) override; 24 | 25 | int Close() override; 26 | 27 | ConnInfo *GetInfo() override; 28 | 29 | void SetISN(uint32_t isn); 30 | 31 | void SetAckISN(uint32_t isn); 32 | 33 | bool IsUdp() override; 34 | 35 | private: 36 | void onTcpError(FakeTcp *conn, int err); 37 | 38 | static void read_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf); 39 | 40 | private: 41 | uv_stream_t *mTcp = nullptr; 42 | TcpInfo mInfo; 43 | }; 44 | 45 | #endif //RSOCK_FAKETCP_H 46 | -------------------------------------------------------------------------------- /conn/FakeUdp.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/17/18. 3 | // 4 | 5 | #include 6 | #include "FakeUdp.h" 7 | #include "../src/util/KeyGenerator.h" 8 | 9 | FakeUdp::FakeUdp(IntKeyType key, const ConnInfo &info) : INetConn(key) { 10 | mInfo = info; 11 | assert(KeyGenerator::KeyForUdp(mInfo) == key); 12 | } 13 | 14 | ConnInfo *FakeUdp::GetInfo() { 15 | return &mInfo; 16 | } 17 | 18 | bool FakeUdp::IsUdp() { 19 | return true; 20 | } 21 | 22 | bool FakeUdp::Alive() { 23 | return true; 24 | } 25 | -------------------------------------------------------------------------------- /conn/FakeUdp.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/17/18. 3 | // 4 | 5 | #ifndef RSOCK_FAKEUDP_H 6 | #define RSOCK_FAKEUDP_H 7 | 8 | #include "../bean/ConnInfo.h" 9 | #include "INetConn.h" 10 | 11 | class FakeUdp : public INetConn { 12 | public: 13 | FakeUdp(IntKeyType key, const ConnInfo &info); 14 | 15 | ConnInfo *GetInfo() override; 16 | 17 | inline bool IsUdp() override; 18 | 19 | // The FakeUdp is always true in case keepalive fails 20 | bool Alive() override; 21 | 22 | private: 23 | ConnInfo mInfo; 24 | }; 25 | 26 | 27 | #endif //RSOCK_FAKEUDP_H 28 | -------------------------------------------------------------------------------- /conn/IAppGroup.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/19/18. 3 | // 4 | 5 | #ifndef RSOCK_IAPPCONN_H 6 | #define RSOCK_IAPPCONN_H 7 | 8 | #include 9 | #include "IGroup.h" 10 | #include "../bean/EncHead.h" 11 | #include "../src/service/INetObserver.h" 12 | 13 | class INetGroup; 14 | 15 | class INetConn; 16 | 17 | class INetConnKeepAlive; 18 | 19 | class IReset; 20 | 21 | struct ConnInfo; 22 | 23 | class IAppGroup : public IGroup { 24 | public: 25 | IAppGroup(const std::string &groupId, INetGroup *fakeNetGroup, IConn *btm); 26 | 27 | int Init() override; 28 | 29 | int Close() override; 30 | 31 | int Send(ssize_t nread, const rbuf_t &rbuf) override; 32 | 33 | int Input(ssize_t nread, const rbuf_t &rbuf) override; 34 | 35 | INetGroup *GetNetGroup() const { return mFakeNetGroup; } 36 | 37 | void Flush(uint64_t now) override; 38 | 39 | INetGroup *NetGroup() { return mFakeNetGroup; } 40 | 41 | /* 42 | * return true if this rst/fin information is processed by this group 43 | */ 44 | bool ProcessTcpFinOrRst(const TcpInfo &info); 45 | 46 | // bool OnUdpRst(const ConnInfo &info) override; 47 | 48 | bool Alive() override; 49 | 50 | virtual int SendConvRst(uint32_t conv); 51 | 52 | virtual int doSendCmd(uint8_t cmd, ssize_t nread, const rbuf_t &rbuf); 53 | 54 | // todo: refactor mhead and this method 55 | virtual int SendNetConnReset(ssize_t nread, const rbuf_t &rbuf, IntKeyType keyOfConnToReset); 56 | 57 | protected: 58 | EncHead mHead; // todo: refactor mhead. error prone 59 | 60 | private: 61 | IReset *mResetHelper = nullptr; 62 | INetConnKeepAlive *mKeepAlive = nullptr; 63 | INetGroup *mFakeNetGroup = nullptr; 64 | }; 65 | 66 | 67 | #endif //RSOCK_IAPPCONN_H 68 | -------------------------------------------------------------------------------- /conn/IBtmConn.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 2/20/18. 3 | // 4 | 5 | #include "IBtmConn.h" 6 | 7 | IBtmConn::IBtmConn(const std::string &key) : IConn(key) {} 8 | -------------------------------------------------------------------------------- /conn/IBtmConn.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 2/20/18. 3 | // 4 | 5 | #ifndef RSOCK_IBTMCONN_H 6 | #define RSOCK_IBTMCONN_H 7 | 8 | 9 | #include "IConn.h" 10 | 11 | struct ConnInfo; 12 | 13 | class IBtmConn : public IConn { 14 | public: 15 | explicit IBtmConn(const std::string &key); 16 | 17 | virtual ConnInfo *GetInfo() = 0; 18 | 19 | virtual bool IsUdp() = 0; 20 | }; 21 | 22 | 23 | #endif //RSOCK_IBTMCONN_H 24 | -------------------------------------------------------------------------------- /conn/IConn.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 12/16/17. 3 | // 4 | 5 | #include 6 | #include "IConn.h" 7 | 8 | IConn::IConn(const std::string &key): IConn(key, key) { 9 | } 10 | 11 | IConn::IConn(const std::string &key, const std::string &printableStr) { 12 | mKey = key; 13 | mPrintableStr = printableStr; 14 | } 15 | 16 | int IConn::Init() { 17 | assert(mInited == false); 18 | mInited = true; 19 | return 0; 20 | } 21 | 22 | int IConn::Close() { 23 | mOnRecvCb = nullptr; 24 | mOutputCb = nullptr; 25 | return 0; 26 | } 27 | 28 | int IConn::Send(ssize_t nread, const rbuf_t &rbuf) { 29 | assert(mInited); 30 | int n = Output(nread, rbuf); 31 | afterSend(n); 32 | return n; 33 | } 34 | 35 | int IConn::Output(ssize_t nread, const rbuf_t &rbuf) { 36 | assert(mInited); 37 | if (mOutputCb) { 38 | return mOutputCb(nread, rbuf); 39 | } 40 | return 0; 41 | } 42 | 43 | int IConn::Input(ssize_t nread, const rbuf_t &rbuf) { 44 | assert(mInited); 45 | int n = OnRecv(nread, rbuf); 46 | afterInput(n); 47 | return n; 48 | } 49 | 50 | int IConn::OnRecv(ssize_t nread, const rbuf_t &rbuf) { 51 | assert(mInited); 52 | if (mOnRecvCb) { 53 | return mOnRecvCb(nread, rbuf); 54 | } 55 | return 0; 56 | } 57 | 58 | IConn::~IConn() { 59 | assert(mOutputCb == nullptr); // must call Close() manually 60 | assert(mOnRecvCb == nullptr); 61 | } 62 | 63 | void IConn::DataStat::Flush() { 64 | mAlive = (prev_in != curr_in && prev_out != curr_out); 65 | prev_in = curr_in; 66 | prev_out = curr_out; 67 | } 68 | 69 | void IConn::DataStat::afterSend(ssize_t nread) { 70 | if (nread > 0) { 71 | curr_out++; 72 | } 73 | } 74 | 75 | void IConn::DataStat::afterInput(ssize_t nread) { 76 | if (nread > 0) { 77 | curr_in++; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /conn/IGroup.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 12/16/17. 3 | // 4 | 5 | #ifndef RSOCK_IGROUP_H 6 | #define RSOCK_IGROUP_H 7 | 8 | #include 9 | #include "IConn.h" 10 | #include "rstype.h" 11 | 12 | // each group has a groupId. each subconn in group shares same groupId. ip:port, 13 | // each subconn has a key. ip:port:conv, or sockpath:conv 14 | class IGroup : public IConn { 15 | public: 16 | explicit IGroup(const std::string &groupId, IConn *btm); 17 | 18 | virtual IConn *ConnOfKey(const std::string &key); 19 | 20 | virtual void AddConn(IConn *conn, const IConnCb &outCb, const IConnCb &recvCb); 21 | 22 | // will not close conn or delete conn 23 | virtual bool RemoveConn(IConn *conn); 24 | 25 | /* 26 | * Detect error during flush. 27 | * Close the conn by default 28 | */ 29 | virtual void OnConnDead(IConn *conn); 30 | 31 | /* 32 | * Remove and then close the conn. 33 | */ 34 | virtual bool CloseConn(IConn *conn); 35 | 36 | int Init() override; 37 | 38 | int Close() override; 39 | 40 | void Flush(uint64_t now) override; 41 | 42 | std::map &GetAllConns(); 43 | 44 | int Size() const; 45 | 46 | // if false, parent will continue to process. if true, parent will not process 47 | 48 | bool Alive() override; 49 | 50 | protected: 51 | std::map mConns; 52 | IConn *mBtm = nullptr; 53 | }; 54 | 55 | 56 | #endif //RSOCK_IGROUP_H 57 | -------------------------------------------------------------------------------- /conn/INetConn.h: -------------------------------------------------------------------------------- 1 | 2 | // 3 | // Created by System Administrator on 1/16/18. 4 | // 5 | 6 | #ifndef RSOCK_INETCONN_H 7 | #define RSOCK_INETCONN_H 8 | 9 | #include "IConn.h" 10 | #include "../bean/EncHead.h" 11 | #include "rscomm.h" 12 | 13 | struct ConnInfo; 14 | 15 | class INetConnErrorHandler; 16 | 17 | class INetConn : public IConn { 18 | public: 19 | enum { 20 | ERR_NO_ERR = 0, 21 | ERR_TIMEOUT = 1, 22 | ERR_FIN_RST = 2, 23 | }; 24 | 25 | explicit INetConn(IntKeyType key); 26 | 27 | int Init() override; 28 | 29 | int Close() override; 30 | 31 | virtual bool IsUdp() = 0; 32 | 33 | virtual ConnInfo *GetInfo() = 0; 34 | 35 | // set ConnInfo.data = EncHead; 36 | int Output(ssize_t nread, const rbuf_t &rbuf) override; 37 | 38 | int OnRecv(ssize_t nread, const rbuf_t &rbuf) override; 39 | 40 | static const std::string BuildPrintableStr(const ConnInfo &info); 41 | 42 | static const std::string BuildPrintableStr(const ConnInfo &info, IntKeyType key); 43 | 44 | /* 45 | * return when is the connection established 46 | */ 47 | uint64_t EstablishedTimeStampMs() const; 48 | 49 | bool IsNew() const; 50 | 51 | IntKeyType IntKey() const; 52 | 53 | virtual void NotifyErr(int err); 54 | 55 | bool Alive() override; 56 | 57 | private: 58 | const IntKeyType mIntKey = 0; 59 | bool mNew = true; 60 | bool mAlive = true; 61 | int mErrCode = ERR_NO_ERR; 62 | const uint64_t mEstablishedTimeStampMs = 0xffffffff; 63 | }; 64 | 65 | #endif //RSOCK_INETCONN_H 66 | -------------------------------------------------------------------------------- /conn/INetConnErrorHandler.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/9/18. 3 | // 4 | 5 | #ifndef RSOCK_INETCONNERRORCALLBACK_H 6 | #define RSOCK_INETCONNERRORCALLBACK_H 7 | 8 | #include 9 | 10 | struct ConnInfo; 11 | 12 | class INetConnErrorHandler { 13 | public: 14 | virtual ~INetConnErrorHandler() = default; 15 | 16 | virtual void OnNetConnErr(const ConnInfo &info, int errCode) = 0; 17 | }; 18 | 19 | #endif //RSOCK_INETCONNERRORCALLBACK_H 20 | -------------------------------------------------------------------------------- /conn/RConn.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/18/18. 3 | // 4 | 5 | #ifndef RSOCK_RCONN_H 6 | #define RSOCK_RCONN_H 7 | 8 | #include 9 | #include "IConn.h" 10 | #include "IGroup.h" 11 | 12 | class RawTcp; 13 | 14 | struct pcap_pkthdr; 15 | 16 | class TcpAckPool; 17 | 18 | class RConnReset; 19 | 20 | class IBtmConn; 21 | 22 | class RConn : public IGroup { 23 | public: 24 | // using INetReset = std::function; 25 | 26 | RConn(const std::string &hashKey, const std::string &dev, uv_loop_t *loop, TcpAckPool *ackPool, 27 | bool isServer); 28 | 29 | static const int HEAD_SIZE; 30 | 31 | virtual void AddUdpConn(IBtmConn *conn); 32 | 33 | // check if hash matches. drop data if not match 34 | int OnRecv(ssize_t nread, const rbuf_t &rbuf) override; 35 | 36 | // direct output to udp or raw tcp 37 | int Output(ssize_t nread, const rbuf_t &rbuf) override; 38 | 39 | int ResetSend(const ConnInfo &info); 40 | 41 | int Init() override; 42 | 43 | int Close() override; 44 | 45 | static void CapInputCb(u_char *args, const pcap_pkthdr *hdr, const u_char *packet); 46 | 47 | private: 48 | using IGroup::AddConn; 49 | 50 | private: 51 | RConnReset *mReset = nullptr; 52 | RawTcp *mRawTcp = nullptr; 53 | const std::string mHashKey; 54 | }; 55 | 56 | 57 | #endif //RSOCK_RCONN_H 58 | -------------------------------------------------------------------------------- /doc/img/btdonation.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/doc/img/btdonation.jpeg -------------------------------------------------------------------------------- /doc/img/ethdonation.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/doc/img/ethdonation.jpeg -------------------------------------------------------------------------------- /doc/img/kcptun.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/doc/img/kcptun.png -------------------------------------------------------------------------------- /doc/img/kcptun_do_sg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/doc/img/kcptun_do_sg.png -------------------------------------------------------------------------------- /doc/img/kcptun_telecom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/doc/img/kcptun_telecom.png -------------------------------------------------------------------------------- /doc/img/normal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/doc/img/normal.png -------------------------------------------------------------------------------- /doc/img/pid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/doc/img/pid.png -------------------------------------------------------------------------------- /doc/img/principle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/doc/img/principle.png -------------------------------------------------------------------------------- /doc/img/rsock.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/doc/img/rsock.png -------------------------------------------------------------------------------- /doc/img/rsock_11tcp_telcom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/doc/img/rsock_11tcp_telcom.png -------------------------------------------------------------------------------- /doc/img/rsock_11udp_tcp_telcom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/doc/img/rsock_11udp_tcp_telcom.png -------------------------------------------------------------------------------- /doc/img/rsock_11udp_telcom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/doc/img/rsock_11udp_telcom.png -------------------------------------------------------------------------------- /doc/img/rsock_do_sg_11tcp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/doc/img/rsock_do_sg_11tcp.png -------------------------------------------------------------------------------- /doc/img/rsock_do_sg_11udp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/doc/img/rsock_do_sg_11udp.png -------------------------------------------------------------------------------- /doc/img/rsock_do_sg_11udp_tcp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/doc/img/rsock_do_sg_11udp_tcp.png -------------------------------------------------------------------------------- /doc/img/running.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/doc/img/running.png -------------------------------------------------------------------------------- /doc/img/shadowsocks.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/doc/img/shadowsocks.png -------------------------------------------------------------------------------- /include/plog/Appenders/ConsoleAppender.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | namespace plog 8 | { 9 | template 10 | class ConsoleAppender : public IAppender 11 | { 12 | public: 13 | #ifdef _WIN32 14 | ConsoleAppender() : m_isatty(!!_isatty(_fileno(stdout))), m_stdoutHandle() 15 | { 16 | if (m_isatty) 17 | { 18 | m_stdoutHandle = GetStdHandle(stdHandle::kOutput); 19 | } 20 | } 21 | #else 22 | ConsoleAppender() : m_isatty(!!isatty(fileno(stdout))) {} 23 | #endif 24 | 25 | virtual void write(const Record& record) 26 | { 27 | util::nstring str = Formatter::format(record); 28 | util::MutexLock lock(m_mutex); 29 | 30 | writestr(str); 31 | } 32 | 33 | protected: 34 | void writestr(const util::nstring& str) 35 | { 36 | #ifdef _WIN32 37 | if (m_isatty) 38 | { 39 | WriteConsoleW(m_stdoutHandle, str.c_str(), static_cast(str.size()), NULL, NULL); 40 | } 41 | else 42 | { 43 | std::cout << util::toNarrow(str, codePage::kActive) << std::flush; 44 | } 45 | #else 46 | std::cout << str << std::flush; 47 | #endif 48 | } 49 | 50 | private: 51 | #ifdef __BORLANDC__ 52 | static int _isatty(int fd) { return ::isatty(fd); } 53 | #endif 54 | 55 | protected: 56 | util::Mutex m_mutex; 57 | const bool m_isatty; 58 | #ifdef _WIN32 59 | HANDLE m_stdoutHandle; 60 | #endif 61 | }; 62 | } 63 | -------------------------------------------------------------------------------- /include/plog/Appenders/IAppender.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace plog 5 | { 6 | class IAppender 7 | { 8 | public: 9 | virtual ~IAppender() 10 | { 11 | } 12 | 13 | virtual void write(const Record& record) = 0; 14 | }; 15 | } 16 | -------------------------------------------------------------------------------- /include/plog/Converters/UTF8Converter.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace plog 5 | { 6 | class UTF8Converter 7 | { 8 | public: 9 | static std::string header(const util::nstring& str) 10 | { 11 | const char kBOM[] = "\xEF\xBB\xBF"; 12 | 13 | return std::string(kBOM) + convert(str); 14 | } 15 | 16 | #ifdef _WIN32 17 | static std::string convert(const util::nstring& str) 18 | { 19 | return util::toNarrow(str, codePage::kUTF8); 20 | } 21 | #else 22 | static const std::string& convert(const util::nstring& str) 23 | { 24 | return str; 25 | } 26 | #endif 27 | }; 28 | } 29 | -------------------------------------------------------------------------------- /include/plog/Formatters/CsvFormatter.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | namespace plog 7 | { 8 | class CsvFormatter 9 | { 10 | public: 11 | static util::nstring header() 12 | { 13 | return PLOG_NSTR("Date;Time;Severity;TID;This;Function;Message\n"); 14 | } 15 | 16 | static util::nstring format(const Record& record) 17 | { 18 | tm t; 19 | util::localtime_s(&t, &record.getTime().time); 20 | 21 | util::nostringstream ss; 22 | ss << t.tm_year + 1900 << PLOG_NSTR("/") << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_mon + 1 << PLOG_NSTR("/") << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_mday << PLOG_NSTR(";"); 23 | ss << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_hour << PLOG_NSTR(":") << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_min << PLOG_NSTR(":") << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_sec << PLOG_NSTR(".") << std::setfill(PLOG_NSTR('0')) << std::setw(3) << record.getTime().millitm << PLOG_NSTR(";"); 24 | ss << severityToString(record.getSeverity()) << PLOG_NSTR(";"); 25 | ss << record.getTid() << PLOG_NSTR(";"); 26 | ss << record.getObject() << PLOG_NSTR(";"); 27 | ss << record.getFunc() << PLOG_NSTR("@") << record.getLine() << PLOG_NSTR(";"); 28 | 29 | util::nstring message = record.getMessage(); 30 | 31 | if (message.size() > kMaxMessageSize) 32 | { 33 | message.resize(kMaxMessageSize); 34 | message.append(PLOG_NSTR("...")); 35 | } 36 | 37 | util::nistringstream split(message); 38 | util::nstring token; 39 | 40 | while (!split.eof()) 41 | { 42 | std::getline(split, token, PLOG_NSTR('"')); 43 | ss << PLOG_NSTR("\"") << token << PLOG_NSTR("\""); 44 | } 45 | 46 | ss << PLOG_NSTR("\n"); 47 | 48 | return ss.str(); 49 | } 50 | 51 | static const size_t kMaxMessageSize = 32000; 52 | }; 53 | } 54 | -------------------------------------------------------------------------------- /include/plog/Formatters/TxtFormatter.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | namespace plog 7 | { 8 | class TxtFormatter 9 | { 10 | public: 11 | static util::nstring header() 12 | { 13 | return util::nstring(); 14 | } 15 | 16 | static util::nstring format(const Record& record) 17 | { 18 | tm t; 19 | util::localtime_s(&t, &record.getTime().time); 20 | 21 | util::nostringstream ss; 22 | ss << t.tm_year + 1900 << "-" << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_mon + 1 << PLOG_NSTR("-") << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_mday << PLOG_NSTR(" "); 23 | ss << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_hour << PLOG_NSTR(":") << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_min << PLOG_NSTR(":") << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_sec << PLOG_NSTR(".") << std::setfill(PLOG_NSTR('0')) << std::setw(3) << record.getTime().millitm << PLOG_NSTR(" "); 24 | ss << std::setfill(PLOG_NSTR(' ')) << std::setw(5) << std::left << severityToString(record.getSeverity()) << PLOG_NSTR(" "); 25 | ss << PLOG_NSTR("[") << record.getTid() << PLOG_NSTR("] "); 26 | ss << PLOG_NSTR("[") << record.getFunc() << PLOG_NSTR("@") << record.getLine() << PLOG_NSTR("] "); 27 | ss << record.getMessage() << PLOG_NSTR("\n"); 28 | 29 | return ss.str(); 30 | } 31 | }; 32 | } 33 | -------------------------------------------------------------------------------- /include/plog/Logger.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | #ifndef PLOG_DEFAULT_INSTANCE 7 | # define PLOG_DEFAULT_INSTANCE 0 8 | #endif 9 | 10 | namespace plog 11 | { 12 | template 13 | class Logger : public util::Singleton >, public IAppender 14 | { 15 | public: 16 | Logger(Severity maxSeverity = none) : m_maxSeverity(maxSeverity) 17 | { 18 | } 19 | 20 | Logger& addAppender(IAppender* appender) 21 | { 22 | assert(appender != this); 23 | m_appenders.push_back(appender); 24 | return *this; 25 | } 26 | 27 | Severity getMaxSeverity() const 28 | { 29 | return m_maxSeverity; 30 | } 31 | 32 | void setMaxSeverity(Severity severity) 33 | { 34 | m_maxSeverity = severity; 35 | } 36 | 37 | bool checkSeverity(Severity severity) const 38 | { 39 | return severity <= m_maxSeverity; 40 | } 41 | 42 | virtual void write(const Record& record) 43 | { 44 | if (checkSeverity(record.getSeverity())) 45 | { 46 | *this += record; 47 | } 48 | } 49 | 50 | void operator+=(const Record& record) 51 | { 52 | for (std::vector::iterator it = m_appenders.begin(); it != m_appenders.end(); ++it) 53 | { 54 | (*it)->write(record); 55 | } 56 | } 57 | 58 | private: 59 | Severity m_maxSeverity; 60 | std::vector m_appenders; 61 | }; 62 | 63 | template 64 | inline Logger* get() 65 | { 66 | return Logger::getInstance(); 67 | } 68 | 69 | inline Logger* get() 70 | { 71 | return Logger::getInstance(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /include/plog/Severity.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace plog 4 | { 5 | enum Severity 6 | { 7 | none = 0, 8 | fatal = 1, 9 | error = 2, 10 | warning = 3, 11 | info = 4, 12 | debug = 5, 13 | verbose = 6 14 | }; 15 | 16 | inline const char* severityToString(Severity severity) 17 | { 18 | switch (severity) 19 | { 20 | case fatal: 21 | return "FATAL"; 22 | case error: 23 | return "ERROR"; 24 | case warning: 25 | return "WARN"; 26 | case info: 27 | return "INFO"; 28 | case debug: 29 | return "DEBUG"; 30 | case verbose: 31 | return "VERB"; 32 | default: 33 | return "NONE"; 34 | } 35 | } 36 | 37 | inline Severity severityFromString(const char* str) 38 | { 39 | for (Severity severity = fatal; severity <= verbose; severity = static_cast(severity + 1)) 40 | { 41 | if (severityToString(severity)[0] == str[0]) 42 | { 43 | return severity; 44 | } 45 | } 46 | 47 | return none; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /include/rcommon.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 11/13/17. 3 | // 4 | 5 | #ifndef RPIPE_RCOMMON_H 6 | #define RPIPE_RCOMMON_H 7 | 8 | 9 | #include 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | #include 16 | 17 | typedef struct rbuf_t { 18 | char *base; 19 | int len; 20 | void *data; 21 | } rbuf_t; 22 | 23 | void free_rbuf(rbuf_t *buf); 24 | 25 | typedef struct rwrite_req_t { 26 | uv_write_t write; 27 | uv_buf_t buf; 28 | } rwrite_req_t; 29 | 30 | void alloc_buf(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf); 31 | void free_rwrite_req(rwrite_req_t *req); 32 | 33 | typedef struct rudp_send_t { 34 | uv_udp_send_t udp_send; 35 | uv_buf_t buf; 36 | struct sockaddr *addr; 37 | } rudp_send_t; 38 | 39 | void free_rudp_send(rudp_send_t *send); 40 | 41 | void close_cb(uv_handle_t *handle); 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | #endif //RPIPE_RCOMMON_H 47 | -------------------------------------------------------------------------------- /include/rscomm.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 12/17/17. 3 | // 4 | 5 | #ifndef RSOCK_RSCOMM_H 6 | #define RSOCK_RSCOMM_H 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | #include 13 | 14 | #ifndef OM_TTL_OUT 15 | #define OM_TTL_OUT 64 16 | #endif 17 | 18 | #define MAC_LEN 6 19 | #define OM_MAX_PKT_SIZE 1500 20 | 21 | #define OM_PIPE_TCP_SEND 0b0001 22 | #define OM_PIPE_TCP_RECV 0b0010 23 | #define OM_PIPE_UDP_SEND 0b0100 24 | #define OM_PIPE_UDP_RECV 0b1000 25 | 26 | #define OM_PIPE_TCP (OM_PIPE_TCP_RECV|OM_PIPE_TCP_SEND) // 3 27 | #define OM_PIPE_UDP (OM_PIPE_UDP_SEND|OM_PIPE_UDP_RECV) // 12 28 | 29 | #define OM_PIPE_ALL (OM_PIPE_TCP|OM_PIPE_UDP) 30 | 31 | #define MD5_LEN 16 32 | 33 | #ifndef RLOG_FILE_PATH 34 | #ifdef _WIN32 35 | #define RLOG_FILE_PATH "./rsock.log" 36 | #else 37 | #define RLOG_FILE_PATH "/var/log/rsock/rsock.log" 38 | #endif 39 | #endif 40 | 41 | #ifndef RSOCK_BUILD_TIME 42 | #define RSOCK_BUILD_TIME "Unknown" 43 | #endif 44 | 45 | #ifndef RSOCK_UV_MAX_BUF 46 | #define RSOCK_UV_MAX_BUF 65536 // 64K 47 | #endif 48 | 49 | #define OM_PCAP_TIMEOUT 10 50 | 51 | typedef struct sockaddr SA; 52 | typedef struct sockaddr_in SA4; 53 | 54 | //#define OM_ACKPOOL_FLUSH_SEC 5 55 | 56 | using IntKeyType = uint64_t ; 57 | 58 | #ifdef __cplusplus 59 | } 60 | #endif 61 | 62 | #endif //RSOCK_RSCOMM_H 63 | -------------------------------------------------------------------------------- /include/rstype.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/23/17. 3 | // 4 | 5 | #ifndef RSOCK_RSTYPE_H 6 | #define RSOCK_RSTYPE_H 7 | 8 | #include 9 | #include 10 | 11 | #define HASH_BUF_SIZE 8 12 | #define ID_BUF_SIZE 8 13 | 14 | // todo: declare a class. that can be used as key 15 | // last 8 digits of md5 16 | using HashBufType = std::array; 17 | using IdBufType = std::array; 18 | 19 | //using PortLists = std::vector; 20 | 21 | #endif //RSOCK_RSTYPE_H 22 | -------------------------------------------------------------------------------- /net/INetManager.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/27/18. 3 | // 4 | 5 | #include 6 | #include 7 | #include "INetManager.h" 8 | #include "TcpAckPool.h" 9 | #include "NetUtil.h" 10 | #include "../conn/BtmUdpConn.h" 11 | #include "NetManagerTimer.h" 12 | 13 | INetManager::INetManager(uv_loop_t *loop, TcpAckPool *ackPool) { 14 | mLoop = loop; 15 | mTcpAckPool = ackPool; 16 | } 17 | 18 | int INetManager::Init() { 19 | mTimer = new NetManagerTimer(this, FLUSH_INTERVAL); 20 | return mTimer->Init(); 21 | } 22 | 23 | int INetManager::Close() { 24 | if (mTimer) { 25 | mTimer->Close(); 26 | delete mTimer; 27 | mTimer = nullptr; 28 | } 29 | mTcpAckPool = nullptr; 30 | return 0; 31 | } 32 | 33 | // todo: should be used here?? 34 | IBtmConn *INetManager::BindUdp(const ConnInfo &info) { 35 | return NetUtil::CreateBtmUdpConn(mLoop, info); 36 | } 37 | 38 | void INetManager::closeTcp(uv_tcp_t *tcp) { 39 | if (tcp) { 40 | uv_close((uv_handle_t *) tcp, close_cb); 41 | } 42 | } 43 | 44 | bool INetManager::Wait2GetInfo(TcpInfo &info) { 45 | return mTcpAckPool->Wait2TransferInfo(info, BLOCK_WAIT_MS); 46 | } 47 | 48 | void INetManager::OnFlush(uint64_t timestamp) { 49 | } 50 | -------------------------------------------------------------------------------- /net/INetManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/27/18. 3 | // 4 | 5 | #ifndef RSOCK_CONNPOOL_H 6 | #define RSOCK_CONNPOOL_H 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include "uv.h" 13 | 14 | class INetConn; 15 | 16 | class IBtmConn; 17 | 18 | class TcpAckPool; 19 | 20 | struct ConnInfo; 21 | 22 | struct TcpInfo; 23 | 24 | class NetManagerTimer; 25 | 26 | /* 27 | * Consider combine some func of TcpAckPool 28 | */ 29 | class INetManager { 30 | public: 31 | explicit INetManager(uv_loop_t *loop, TcpAckPool *ackPool); 32 | 33 | virtual ~INetManager() = default; 34 | 35 | virtual int Init(); 36 | 37 | virtual int Close(); 38 | 39 | INetManager &operator=(const INetManager &) = delete; 40 | 41 | IBtmConn *BindUdp(const ConnInfo &info); 42 | 43 | virtual bool Wait2GetInfo(TcpInfo &info); 44 | 45 | virtual void OnFlush(uint64_t timestamp); 46 | 47 | protected: 48 | void closeTcp(uv_tcp_t *tcp); 49 | 50 | protected: 51 | uv_loop_t *mLoop = nullptr; 52 | TcpAckPool *mTcpAckPool = nullptr; 53 | 54 | protected: 55 | const std::chrono::milliseconds BLOCK_WAIT_MS = std::chrono::milliseconds(500); 56 | const uint64_t FLUSH_INTERVAL = 1000; // 1s 57 | NetManagerTimer *mTimer = nullptr; 58 | }; 59 | 60 | #endif //RSOCK_CONNPOOL_H -------------------------------------------------------------------------------- /net/NetManagerTimer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/3/18. 3 | // 4 | 5 | #include "NetManagerTimer.h" 6 | #include "INetManager.h" 7 | #include "../src/service/ServiceUtil.h" 8 | #include "../src/service/TimerService.h" 9 | 10 | NetManagerTimer::NetManagerTimer(INetManager *netManager, uint64_t interval) : FLUSH_INTERVAL(interval) { 11 | mManager = netManager; 12 | } 13 | 14 | int NetManagerTimer::Init() { 15 | return ServiceUtil::GetService(ServiceManager::TIMER_SERVICE)->RegisterObserver(this); 16 | } 17 | 18 | int NetManagerTimer::Close() { 19 | return ServiceUtil::GetService(ServiceManager::TIMER_SERVICE)->UnRegisterObserver(this); 20 | } 21 | 22 | void NetManagerTimer::OnFlush(uint64_t timestamp) { 23 | mManager->OnFlush(timestamp); 24 | } 25 | 26 | uint64_t NetManagerTimer::IntervalMs() const { 27 | return FLUSH_INTERVAL; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /net/NetManagerTimer.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/3/18. 3 | // 4 | 5 | #ifndef RSOCK_NETMANAGERTIMER_H 6 | #define RSOCK_NETMANAGERTIMER_H 7 | 8 | 9 | #include "../src/service/ITimerObserver.h" 10 | 11 | class INetManager; 12 | 13 | class NetManagerTimer : public ITimerObserver { 14 | public: 15 | NetManagerTimer(INetManager *netManager, uint64_t interval); 16 | 17 | int Init() override; 18 | 19 | int Close() override; 20 | 21 | void OnFlush(uint64_t timestamp) override; 22 | 23 | uint64_t IntervalMs() const override; 24 | 25 | private: 26 | const uint64_t FLUSH_INTERVAL = 0; 27 | INetManager *mManager = nullptr; 28 | }; 29 | 30 | 31 | #endif //RSOCK_NETMANAGERTIMER_H 32 | -------------------------------------------------------------------------------- /net/NetUtil.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/18/18. 3 | // 4 | 5 | #ifndef RSOCK_NETUTIL_H 6 | #define RSOCK_NETUTIL_H 7 | 8 | #include 9 | #include 10 | #include "uv.h" 11 | #include "rscomm.h" 12 | 13 | class BtmUdpConn; 14 | 15 | class FakeTcp; 16 | 17 | struct ConnInfo; 18 | 19 | struct TcpInfo; 20 | 21 | class NetUtil { 22 | public: 23 | static BtmUdpConn *CreateBtmUdpConn(uv_loop_t *loop, const ConnInfo &info); 24 | 25 | static uv_tcp_t *CreateTcp(uv_loop_t *loop, const ConnInfo &info); 26 | 27 | // static FakeTcp *CreateTcpConn(uv_loop_t *loop, const ConnInfo &info); 28 | 29 | // static FakeTcp *CreateTcpConn(uv_tcp_t *tcp); 30 | 31 | static uv_connect_t *ConnectTcp(uv_loop_t *loop, const ConnInfo &info, const uv_connect_cb &cb, void *data); 32 | 33 | private: 34 | static int createTcpSock(const SA4 *target, const SA4 *self); 35 | 36 | static int createTcpSock(const ConnInfo &info); 37 | }; 38 | 39 | 40 | #endif //RSOCK_NETUTIL_H 41 | -------------------------------------------------------------------------------- /net/TcpAckPool.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/22/18. 3 | // 4 | 5 | #ifndef RSOCK_TCPACKHELPER_H 6 | #define RSOCK_TCPACKHELPER_H 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | 15 | #include "rscomm.h" 16 | 17 | #include "../bean/TcpInfo.h" 18 | #include "../src/service/ITimerObserver.h" 19 | #include "../src/util/TcpCmpFn.h" 20 | 21 | // store ack information of incomming connection 22 | class TcpAckPool : public ITimerObserver { 23 | public: 24 | explicit TcpAckPool(uint64_t expireMs); 25 | 26 | int Init() override; 27 | 28 | int Close() override; 29 | 30 | // sp or dp == 0 is not valid 31 | bool AddInfoFromPeer(const TcpInfo &infoFromPeer, uint8_t flags); 32 | 33 | ssize_t RemoveInfo(const TcpInfo &tcpInfo); 34 | 35 | /* 36 | * This will tranfer captured tcpInfo. You need to increment Caution: see TcpUtil. 37 | */ 38 | bool Wait2TransferInfo(TcpInfo &info, const std::chrono::milliseconds milliSec); 39 | 40 | std::string Dump(); 41 | 42 | uint64_t PersistMs() const; 43 | 44 | void OnFlush(uint64_t timestamp) override; 45 | 46 | bool ContainsInfo(const TcpInfo &info, const std::chrono::milliseconds milliSec); 47 | 48 | protected: 49 | // no lock protection 50 | bool getInfoIfExists(TcpInfo &info); 51 | 52 | ssize_t locklessRemove(const TcpInfo &tcpInfo); 53 | 54 | private: 55 | // Be same with app keepalive. if not removed from the pool manually, the conn info will be removed automatically 56 | const uint64_t EXPIRE_INTERVAL_MS = 0; 57 | std::map mInfoPool; 58 | std::mutex mMutex; 59 | std::condition_variable mCondVar; 60 | }; 61 | 62 | 63 | #endif //RSOCK_TCPACKHELPER_H 64 | -------------------------------------------------------------------------------- /net/TcpListenPool.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/27/18. 3 | // 4 | 5 | #ifndef RSOCK_NETLISTENPOOL_H 6 | #define RSOCK_NETLISTENPOOL_H 7 | 8 | #include 9 | #include 10 | 11 | #include "uv.h" 12 | #include "../util/RPortList.h" 13 | 14 | class TcpListenPool { 15 | public: 16 | using NewConnCb = std::function; 17 | 18 | explicit TcpListenPool(const RPortList &ports, const std::string &ip, uv_loop_t *loop); 19 | 20 | virtual int Init(); 21 | 22 | virtual void Close(); 23 | 24 | void SetNewConnCb(const NewConnCb &cb); 25 | 26 | protected: 27 | // if status != 0, stream is is listened tcp that occur error. otherwise new connection 28 | virtual void newConn(uv_stream_t *stream, int status); 29 | 30 | private: 31 | void clearPool(); 32 | 33 | static void new_conn_cb(uv_stream_t *server, int status); 34 | 35 | uv_tcp_t *initTcp(const SA4 *addr); 36 | 37 | private: 38 | RPortList mPorts; 39 | NewConnCb mNewConnCb = nullptr; 40 | 41 | uv_loop_t *mLoop = nullptr; 42 | std::vector mPool; 43 | const std::string mIp; 44 | }; 45 | 46 | 47 | #endif //RSOCK_NETLISTENPOOL_H 48 | -------------------------------------------------------------------------------- /server/SConn.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 12/16/17. 3 | // 4 | 5 | #ifndef RSOCK_RTASK_H 6 | #define RSOCK_RTASK_H 7 | 8 | struct sockaddr; 9 | struct sockaddr_in; 10 | 11 | #include 12 | #include "../conn/IConn.h" 13 | 14 | 15 | /* 16 | * Ipv4 udp target conn 17 | */ 18 | class SConn : public IConn { 19 | public: 20 | explicit SConn(const std::string &key, uv_loop_t *loop, const SA *target, uint32_t conv); 21 | 22 | int Init() override; 23 | 24 | int Close() override; 25 | 26 | // to target 27 | int OnRecv(ssize_t nread, const rbuf_t &rbuf) override; 28 | 29 | uint32_t Conv(); 30 | 31 | private: 32 | static void udpRecvCb(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf, const SA *addr, 33 | unsigned flags); 34 | 35 | static void sendCb(uv_udp_send_t *req, int status); 36 | 37 | private: 38 | SA4 *mTarget = nullptr; // udp, unix sock 39 | SA4 *mSelfAddr = nullptr; // addr in server 40 | // struct sockaddr_in *mOrigin; 41 | uv_udp_t *mUdp = nullptr; 42 | uint32_t mConv = 0; 43 | }; 44 | 45 | 46 | #endif //RSOCK_RTASK_H 47 | -------------------------------------------------------------------------------- /server/SNetGroup.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/17/18. 3 | // 4 | 5 | #include 6 | #include "SNetGroup.h" 7 | #include "../conn/FakeUdp.h" 8 | #include "ServerNetManager.h" 9 | #include "../src/util/KeyGenerator.h" 10 | #include "../conn/FakeTcp.h" 11 | 12 | using namespace std::placeholders; 13 | 14 | SNetGroup::SNetGroup(const std::string &groupId, uv_loop_t *loop, ServerNetManager *netManager) 15 | : INetGroup(groupId, loop) { 16 | mNetManager = netManager; 17 | assert(mNetManager); 18 | } 19 | 20 | INetConn *SNetGroup::CreateNetConn(IntKeyType key, const ConnInfo *info) { 21 | INetConn *c = nullptr; 22 | if (info->IsUdp()) { 23 | c = new FakeUdp(key, *info); 24 | } else { 25 | TcpInfo tcpInfo(*info); 26 | auto tcp = mNetManager->GetTcp(tcpInfo); 27 | if (tcp) { 28 | c = new FakeTcp(tcp, key, tcpInfo); 29 | // c = new FakeTcp(tcp, KeyGenerator::KeyForConnInfo(tcpInfo)); bug: Can't use key generation here!!! 30 | } 31 | } 32 | 33 | if (c) { 34 | EncHead *hd = info->head; 35 | if (!hd || c->Init()) { 36 | LOGE << "conn " << c->ToStr() << " init failed"; 37 | c->Close(); 38 | delete c; 39 | return nullptr; 40 | } 41 | return c; 42 | } 43 | 44 | LOGW << "Cannot create conn, no netconn: " << key << ", info: " << info->ToStr(); 45 | return nullptr; 46 | } 47 | -------------------------------------------------------------------------------- /server/SNetGroup.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/17/18. 3 | // 4 | 5 | #ifndef RSOCK_SNETGROUP_H 6 | #define RSOCK_SNETGROUP_H 7 | 8 | 9 | #include "../conn/INetGroup.h" 10 | class ServerNetManager; 11 | 12 | class SNetGroup : public INetGroup { 13 | public: 14 | SNetGroup(const std::string &groupId, uv_loop_t *loop, ServerNetManager *netManager); 15 | 16 | INetConn *CreateNetConn(IntKeyType key, const ConnInfo *info) override; 17 | 18 | private: 19 | ServerNetManager *mNetManager = nullptr; 20 | }; 21 | 22 | 23 | #endif //RSOCK_SNETGROUP_H 24 | -------------------------------------------------------------------------------- /server/SSockApp.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/26/17. 3 | // 4 | 5 | #include 6 | #include 7 | #include "SSockApp.h" 8 | #include "ServerGroup.h" 9 | #include "../util/rhash.h" 10 | #include "../conn/RConn.h" 11 | #include "ServerNetManager.h" 12 | #include "../bean/TcpInfo.h" 13 | #include "../cap/RCap.h" 14 | #include "../conn/INetConn.h" 15 | #include "../bean/RConfig.h" 16 | 17 | SSockApp::SSockApp() : ISockApp(true) {} 18 | 19 | RCap *SSockApp::CreateCap(const RConfig &conf) { 20 | return new RCap(conf.param.dev, conf.param.selfCapIp, conf.param.capPorts, {}, "", conf.param.cap_timeout, true); 21 | } 22 | 23 | RConn *SSockApp::CreateBtmConn(RConfig &conf, uv_loop_t *loop, TcpAckPool *ackPool) { 24 | // note: tcp is listened in ServerNetManager 25 | RConn *rconn = new RConn(conf.param.hashKey, conf.param.dev, loop, ackPool, true); 26 | // listen udp directly if enabled 27 | if (conf.param.type & OM_PIPE_UDP) { 28 | auto ports = conf.param.capPorts.GetRawList(); 29 | std::vector zeros(ports.size(), 0); 30 | auto vec = bindUdpConns(conf.param.selfCapInt, ports, 0, zeros); 31 | for (auto c: vec) { 32 | rconn->AddUdpConn(c); 33 | } 34 | } 35 | 36 | return rconn; 37 | } 38 | 39 | IConn *SSockApp::CreateBridgeConn(RConfig &conf, IConn *btm, uv_loop_t *loop, INetManager *netManager) { 40 | SA4 target = {0}; 41 | target.sin_family = AF_INET; 42 | target.sin_port = htons(conf.param.targetPort); 43 | target.sin_addr.s_addr = inet_addr(conf.param.targetIp.c_str()); 44 | return new ServerGroup(IdBuf2Str(conf.param.id), loop, (const SA *) (&target), btm, 45 | dynamic_cast(netManager)); 46 | } 47 | 48 | INetManager *SSockApp::CreateNetManager(const RConfig &conf, uv_loop_t *loop, TcpAckPool *ackPool) { 49 | auto portList = conf.param.capPorts; 50 | if (!(conf.param.type & OM_PIPE_TCP)) { 51 | portList = {}; 52 | } 53 | return new ServerNetManager(loop, portList, conf.param.selfCapIp, ackPool); 54 | } 55 | -------------------------------------------------------------------------------- /server/SSockApp.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/26/17. 3 | // 4 | 5 | #ifndef RSOCK_SSOCKAPP_H 6 | #define RSOCK_SSOCKAPP_H 7 | 8 | 9 | #include "../src/ISockApp.h" 10 | 11 | class SSockApp : public ISockApp { 12 | public: 13 | explicit SSockApp(); 14 | 15 | RCap *CreateCap(const RConfig &conf) override; 16 | 17 | RConn *CreateBtmConn(RConfig &conf, uv_loop_t *loop, TcpAckPool *ackPool) override; 18 | 19 | IConn *CreateBridgeConn(RConfig &conf, IConn *btm, uv_loop_t *loop, INetManager *netManager) override; 20 | 21 | INetManager *CreateNetManager(const RConfig &conf, uv_loop_t *loop, TcpAckPool *ackPool) override; 22 | }; 23 | 24 | 25 | #endif //RSOCK_SSOCKAPP_H 26 | -------------------------------------------------------------------------------- /server/ServerGroup.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/17/18. 3 | // 4 | 5 | #ifndef RSOCK_SERVERGROUP_H 6 | #define RSOCK_SERVERGROUP_H 7 | 8 | 9 | #include "../conn/IGroup.h" 10 | #include "../src/service/INetObserver.h" 11 | 12 | struct ConnInfo; 13 | 14 | class ServerNetManager; 15 | 16 | class ServerGroup : public IGroup, public INetObserver { 17 | public: 18 | ServerGroup(const std::string &groupId, uv_loop_t *loop, const struct sockaddr *target, IConn *btm, 19 | ServerNetManager *netManager); 20 | 21 | int OnRecv(ssize_t nread, const rbuf_t &rbuf) override; 22 | 23 | int Close() override; 24 | 25 | void OnTcpFinOrRst(const TcpInfo &info) override; 26 | 27 | int Init() override; 28 | 29 | private: 30 | IConn *newConn(const std::string &groupId, uv_loop_t *loop, const struct sockaddr *target, const ConnInfo &info); 31 | 32 | private: 33 | uv_loop_t *mLoop = nullptr; 34 | sockaddr *mTarget = nullptr; 35 | ServerNetManager *mNetManager = nullptr; 36 | }; 37 | 38 | 39 | #endif //RSOCK_SERVERGROUP_H 40 | -------------------------------------------------------------------------------- /server/ServerNetManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/27/18. 3 | // 4 | 5 | #ifndef RSOCK_SERVERNETMANAGER_H 6 | #define RSOCK_SERVERNETMANAGER_H 7 | 8 | 9 | #include "../net/INetManager.h" 10 | #include "../net/TcpListenPool.h" 11 | #include "../src/util/TcpCmpFn.h" 12 | 13 | class TcpAckPool; 14 | 15 | 16 | /* 17 | * The uv_tcp_t pool. 18 | * A valid tcp connection has 2 requirements: 19 | * 1. The INetManager contains a uv_tcp_t handle; 20 | * 2. The TcpAckPool has corresponding record of tcp. 21 | * After RConfig.appKeepAliveSec, the expired record will be cleared if not manually fetched or removed. 22 | */ 23 | class ServerNetManager : public INetManager { 24 | public: 25 | ServerNetManager(uv_loop_t *loop, const RPortList &ports, const std::string &ip, TcpAckPool *ackPool); 26 | 27 | int Init() override; 28 | 29 | int Close() override; 30 | 31 | virtual void OnNewConnection(uv_tcp_t *tcp); 32 | 33 | virtual bool ContainsTcp(const TcpInfo &info); 34 | 35 | void OnFlush(uint64_t timestamp) override; 36 | 37 | /* 38 | * return tcp handle and set info.ack, info.seq if both INetManager and TcpAckPool contains the info record 39 | * if doesn't exist, return null and info is not modified. 40 | */ 41 | virtual uv_tcp_t *GetTcp(TcpInfo &info); 42 | 43 | protected: 44 | /* 45 | * Will close conn if add failed: failed to get tcpInfo of conn 46 | */ 47 | void add2Pool(uv_tcp_t *tcp); 48 | 49 | private: 50 | 51 | struct ConnHelper { 52 | uv_tcp_t *conn = nullptr; 53 | 54 | uint64_t expireMs = 0; 55 | 56 | ConnHelper(uv_tcp_t *aConn, uint64_t expireMs) { 57 | conn = aConn; 58 | this->expireMs = expireMs; 59 | } 60 | }; 61 | 62 | const uint64_t POOL_PERSIST_MS = 0; // assigned to TcpAckPool.PersistMs in ctor 63 | TcpListenPool mListenPool; 64 | std::map mPool; 65 | }; 66 | 67 | 68 | #endif //RSOCK_SERVERNETMANAGER_H 69 | -------------------------------------------------------------------------------- /server/SubGroup.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/19/18. 3 | // 4 | 5 | #ifndef RSOCK_SUBCONN_H 6 | #define RSOCK_SUBCONN_H 7 | 8 | 9 | #include "../conn/IAppGroup.h" 10 | #include "../bean/EncHead.h" 11 | 12 | class SubGroup : public IAppGroup { 13 | public: 14 | SubGroup(const std::string &groupId, uv_loop_t *loop, const struct sockaddr *target, INetGroup *fakeNetGroup, 15 | IConn *btm); 16 | 17 | int Close() override; 18 | 19 | int OnRecv(ssize_t nread, const rbuf_t &rbuf) override; 20 | 21 | private: 22 | IConn *newConn(const std::string &key, uint32_t conv); 23 | 24 | int sconnSend(ssize_t nread, const rbuf_t &rbuf); 25 | 26 | private: 27 | 28 | uv_loop_t *mLoop = nullptr; 29 | struct sockaddr *mTarget = nullptr; 30 | }; 31 | 32 | 33 | #endif //RSOCK_SUBCONN_H 34 | -------------------------------------------------------------------------------- /server/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/27/17. 3 | // 4 | #include "ssock.h" 5 | 6 | int main(int argc, char **argv) { 7 | return ssock_main(argc, argv); 8 | } -------------------------------------------------------------------------------- /server/ssock.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/27/17. 3 | // 4 | 5 | #include "ssock.h" 6 | #include "SSockApp.h" 7 | 8 | int ssock_main(int argc, char **argv) { 9 | uv_default_loop(); 10 | ISockApp *app = new SSockApp(); 11 | 12 | int nret = app->Parse(argc, reinterpret_cast(argv)); 13 | if (!nret) { 14 | if (!(nret = app->Init())) { 15 | nret = app->Start(); 16 | } 17 | } 18 | app->Close(); 19 | 20 | delete app; 21 | return nret; 22 | } 23 | -------------------------------------------------------------------------------- /server/ssock.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/27/17. 3 | // 4 | 5 | #ifndef RSOCK_SSOCK_H 6 | #define RSOCK_SSOCK_H 7 | 8 | int ssock_main(int argc, char **argv); 9 | 10 | #endif //RSOCK_SSOCK_H 11 | -------------------------------------------------------------------------------- /src/app/AppNetObserver.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/9/18. 3 | // 4 | 5 | #include "AppNetObserver.h" 6 | #include "../ISockApp.h" 7 | #include "../service/ServiceUtil.h" 8 | #include "../service/NetService.h" 9 | 10 | void AppNetObserver::OnTcpFinOrRst(const TcpInfo &info) { 11 | mApp->OnTcpFinOrRst(info); 12 | } 13 | 14 | int AppNetObserver::Init() { 15 | return ServiceUtil::GetService(ServiceManager::NET_SERVICE)->RegisterObserver(this); 16 | } 17 | 18 | int AppNetObserver::Close() { 19 | return ServiceUtil::GetService(ServiceManager::NET_SERVICE)->UnRegisterObserver(this); 20 | } 21 | 22 | AppNetObserver::AppNetObserver(ISockApp *app) { 23 | mApp = app; 24 | } 25 | -------------------------------------------------------------------------------- /src/app/AppNetObserver.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/9/18. 3 | // 4 | 5 | #ifndef RSOCK_APPNETOBSERVER_H 6 | #define RSOCK_APPNETOBSERVER_H 7 | 8 | 9 | #include "../service/INetObserver.h" 10 | 11 | class ISockApp; 12 | 13 | class AppNetObserver: public INetObserver { 14 | public: 15 | explicit AppNetObserver(ISockApp *app); 16 | 17 | void OnTcpFinOrRst(const TcpInfo &info) override; 18 | 19 | int Init() override; 20 | 21 | int Close() override; 22 | 23 | private: 24 | ISockApp *mApp = nullptr; 25 | }; 26 | 27 | 28 | #endif //RSOCK_APPNETOBSERVER_H 29 | -------------------------------------------------------------------------------- /src/app/AppTimer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 5/25/18. 3 | // 4 | 5 | #include 6 | #include "AppTimer.h" 7 | #include "../ISockApp.h" 8 | #include "../service/ServiceUtil.h" 9 | #include "../service/TimerService.h" 10 | 11 | AppTimer::AppTimer(uint64_t interval, ISockApp *app) : INTERVAL(interval) { 12 | assert(interval); 13 | mApp = app; 14 | } 15 | 16 | int AppTimer::Init() { 17 | return ServiceUtil::GetService(ServiceManager::TIMER_SERVICE)->RegisterObserver(this); 18 | } 19 | 20 | int AppTimer::Close() { 21 | return ServiceUtil::GetService(ServiceManager::TIMER_SERVICE)->UnRegisterObserver(this); 22 | } 23 | 24 | void AppTimer::OnFlush(uint64_t timestamp) { 25 | mApp->Flush(timestamp); 26 | } 27 | 28 | uint64_t AppTimer::IntervalMs() const { 29 | return INTERVAL; 30 | } -------------------------------------------------------------------------------- /src/app/AppTimer.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 5/25/18. 3 | // 4 | 5 | #ifndef RSOCK_APPTIMER_H 6 | #define RSOCK_APPTIMER_H 7 | 8 | 9 | #include "../service/ITimerObserver.h" 10 | 11 | class ISockApp; 12 | 13 | class AppTimer : public ITimerObserver { 14 | public: 15 | explicit AppTimer(uint64_t interval, ISockApp *app); 16 | 17 | int Init() override; 18 | 19 | int Close() override; 20 | 21 | void OnFlush(uint64_t timestamp) override; 22 | 23 | uint64_t IntervalMs() const override; 24 | 25 | private: 26 | const uint64_t INTERVAL = 0; 27 | ISockApp *mApp = nullptr; 28 | }; 29 | 30 | 31 | #endif //RSOCK_APPTIMER_H 32 | -------------------------------------------------------------------------------- /src/os/common/FdUtil_common.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | #include "os.h" 7 | #include "FdUtil.h" 8 | 9 | void FdUtil::CheckDgramFd(int fd) { 10 | checkFdType(fd, SOCK_DGRAM); 11 | } 12 | 13 | void FdUtil::CheckStreamFd(int fd) { 14 | checkFdType(fd, SOCK_STREAM); 15 | } 16 | 17 | void FdUtil::checkFdType(int fd, int type) { 18 | assert(fd >= 0); 19 | int currType; 20 | socklen_t len = sizeof(socklen_t); 21 | getsockopt(fd, SOL_SOCKET, SO_TYPE, (SOCKOPT_VAL_TYPE) &currType, &len); 22 | assert(currType == type); 23 | } -------------------------------------------------------------------------------- /src/os/include/FdUtil.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 12/4/17. 3 | // 4 | 5 | #ifndef RPIPE_UTIL_H 6 | #define RPIPE_UTIL_H 7 | 8 | #include 9 | 10 | class FdUtil { 11 | public: 12 | static void CheckDgramFd(int fd); 13 | 14 | static void CheckStreamFd(int fd); 15 | 16 | static int SetNonblocking(int &fd); 17 | 18 | static void SetBlocking(int &fd); 19 | 20 | static bool FileExists(const char *fName); 21 | 22 | static int CreateFile(const std::string &fName, int mode = 0644); 23 | 24 | static int IsDir(const std::string &dirName); 25 | 26 | static int CreateDir(const std::string &dirName, int mode = 0755); 27 | 28 | private: 29 | static void checkFdType(int fd, int type); 30 | }; 31 | 32 | 33 | #endif //RPIPE_UTIL_H 34 | -------------------------------------------------------------------------------- /src/os/include/ProcUtil.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/11/18. 3 | // 4 | 5 | #ifndef RSOCK_PROCUTIL_H 6 | #define RSOCK_PROCUTIL_H 7 | 8 | 9 | class ProcUtil { 10 | public: 11 | static int MakeDaemon(); 12 | 13 | static bool IsRoot(); 14 | }; 15 | 16 | 17 | #endif //RSOCK_PROCUTIL_H 18 | -------------------------------------------------------------------------------- /src/os/include/os.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Administrator on 2018/4/4. 3 | // 4 | 5 | #ifndef RSOCK_OS_H 6 | #define RSOCK_OS_H 7 | 8 | // only little endian supported 9 | #ifndef LITTLE_ENDIAN 10 | #define LITTLE_ENDIAN 1 11 | #endif 12 | #ifndef BYTE_ORDER 13 | #define BYTE_ORDER LITTLE_ENDIAN 14 | #endif 15 | 16 | #ifdef _WIN32 17 | #include "os_win.h" 18 | #else 19 | #include "os_unix.h" 20 | #endif 21 | 22 | #endif //RSOCK_OS_H 23 | -------------------------------------------------------------------------------- /src/os/include/os_unix.h: -------------------------------------------------------------------------------- 1 | #ifndef OS_UNIX_H 2 | #define OS_UNIX_H 3 | 4 | #ifndef _WIN32 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #define SOCKOPT_VAL_TYPE void* 20 | 21 | #ifndef RSOCK_SOCK_BUF_TIMES 22 | #define RSOCK_SOCK_BUF_TIMES 64 // 64 * original buf size 23 | #endif 24 | 25 | #endif // !_WIN32 26 | 27 | #endif // !OS_UNIX_H 28 | -------------------------------------------------------------------------------- /src/os/include/os_util.h: -------------------------------------------------------------------------------- 1 | #ifndef OS_UTIL_H 2 | #define OS_UTIL_H 3 | 4 | #include 5 | 6 | struct timeval; 7 | 8 | struct in_addr; 9 | 10 | struct uv_loop_s; 11 | 12 | int os_init_onstartup(); 13 | 14 | int os_clean(); 15 | 16 | void rgettimeofday(struct timeval *val); 17 | 18 | void CloseSocket(int sock); 19 | 20 | int GetPrevSockErr(); 21 | 22 | #endif // !OS_H 23 | -------------------------------------------------------------------------------- /src/os/unix/FdUtil.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 12/4/17. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "os.h" 10 | #include "FdUtil.h" 11 | #include "os_util.h" 12 | 13 | 14 | int FdUtil::SetNonblocking(int &fd) { 15 | int oflag = fcntl(fd, F_GETFL, 0); 16 | int newFlag = oflag | O_NONBLOCK; 17 | return fcntl(fd, F_SETFL, newFlag); 18 | } 19 | 20 | void FdUtil::SetBlocking(int &fd) { 21 | int oflag = fcntl(fd, F_GETFL, 0); 22 | int newFlag = oflag & (~O_NONBLOCK); 23 | fcntl(fd, F_SETFL, newFlag); 24 | } 25 | 26 | bool FdUtil::FileExists(const char *fName) { 27 | return access(fName, F_OK) == 0; 28 | } 29 | 30 | int FdUtil::CreateFile(const std::string &fName, int mode) { 31 | auto pos = fName.rfind('/'); 32 | int nret = 0; 33 | if (pos != std::string::npos) { 34 | auto dirName = fName.substr(0, pos); 35 | if (!FileExists(dirName.c_str()) || 1 != IsDir(dirName)) { 36 | nret = CreateDir(dirName, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); 37 | if (nret) { 38 | return nret; 39 | } 40 | } 41 | nret = open(fName.c_str(), O_WRONLY|O_CREAT, mode); 42 | if (nret >= 0) { 43 | CloseSocket(nret); // just close the fd descriptor 44 | } 45 | } 46 | return nret; 47 | } 48 | 49 | int FdUtil::IsDir(const std::string &dirName) { 50 | struct stat info = {0}; 51 | int nret = stat(dirName.c_str(), &info); 52 | if (0 == nret) { 53 | return S_ISDIR(info.st_mode); 54 | } 55 | return nret; 56 | } 57 | 58 | int FdUtil::CreateDir(const std::string &dirName, int mode) { 59 | return mkdir(dirName.c_str(), mode); 60 | } -------------------------------------------------------------------------------- /src/os/unix/ProcUtil.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/11/18. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "ProcUtil.h" 13 | 14 | 15 | int ProcUtil::MakeDaemon() { 16 | const int pid = fork(); 17 | if (pid == 0) { 18 | const int n = setsid(); 19 | if (-1 == n) { 20 | return n; 21 | } 22 | umask(0); 23 | const int nret = chdir("/"); 24 | if (nret == -1) { 25 | return nret; 26 | } 27 | for (auto f: {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO}) { 28 | close(f); // since we know it's used in unix. just use close to avoid introduce os_util.h 29 | } 30 | int fd = open("/dev/null", O_RDWR); 31 | dup2(fd, STDOUT_FILENO); 32 | dup2(fd, STDERR_FILENO); 33 | } else if (pid > 0) { // parent; 34 | } else { 35 | assert(0); 36 | } 37 | 38 | return pid; 39 | } 40 | 41 | bool ProcUtil::IsRoot() { 42 | return geteuid() == 0; 43 | } -------------------------------------------------------------------------------- /src/os/unix/conn/UnixDgramSyncConn.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "uv.h" 3 | #include 4 | #include 5 | #include "UnixDgramSyncConn.h" 6 | #include "os_util.h" 7 | #include "../../../../util/rsutil.h" 8 | #include "rscomm.h" 9 | 10 | UnixDgramSyncConn::UnixDgramSyncConn(struct uv_loop_s *loop, const Callback &cb, void *obj) 11 | : IPacketSyncConn(loop, cb, obj) { 12 | } 13 | 14 | int UnixDgramSyncConn::CreateSockPair(uv_loop_s *loop, sock_pair_t *socks) { 15 | int fd2[2] = {-1, -1}; 16 | int nret = socketpair(AF_UNIX, SOCK_DGRAM, 0, fd2); 17 | 18 | socks->writeFd = fd2[1]; 19 | socks->readFd = fd2[0]; 20 | trySetGoodBufSize(RSOCK_SOCK_BUF_TIMES, socks->readFd, socks->writeFd); 21 | 22 | socks->ptr = poll_dgram_fd(fd2[0], loop, pollCb, this, &nret); 23 | if (!socks->ptr) { 24 | LOGE << "poll_dgram_fd failed: " << uv_strerror(nret); 25 | return nret; 26 | } 27 | return 0; 28 | } 29 | 30 | void UnixDgramSyncConn::CloseSockPair(sock_pair_t *socks) { 31 | uv_poll_stop((uv_poll_t *) socks->ptr); 32 | IPacketSyncConn::CloseSockPair(socks); 33 | } 34 | 35 | void UnixDgramSyncConn::pollCb(uv_poll_t *handle, int status, int events) { 36 | if (status) { 37 | LOGE << "unix socket poll err: " << uv_strerror(status); 38 | return; 39 | } 40 | if (events & UV_READABLE) { 41 | UnixDgramSyncConn *conn = (UnixDgramSyncConn *) (handle->data); 42 | char buf[OM_MAX_PKT_SIZE] = {0}; 43 | ssize_t nread = read(conn->mSocks.readFd, buf, OM_MAX_PKT_SIZE); 44 | if (nread > 0) { 45 | conn->Input(nread, new_buf(nread, buf, nullptr)); 46 | } 47 | } 48 | } 49 | 50 | -------------------------------------------------------------------------------- /src/os/unix/conn/UnixDgramSyncConn.h: -------------------------------------------------------------------------------- 1 | #ifndef UNIX_DGRAM_SYNC_CONN_H 2 | #define UNIX_DGRAM_SYNC_CONN_H 3 | 4 | #include "../../../sync/IPacketSyncConn.h" 5 | 6 | class UnixDgramSyncConn : public IPacketSyncConn { 7 | public: 8 | UnixDgramSyncConn(struct uv_loop_s *loop, const Callback &cb, void *obj); 9 | 10 | int CreateSockPair(struct uv_loop_s *loop, sock_pair_t *socks) override; 11 | 12 | void CloseSockPair(sock_pair_t *socks) override; 13 | 14 | protected: 15 | static void pollCb(uv_poll_t *handle, int status, int events); 16 | }; 17 | 18 | #endif // UNIX_DGRAM_SYNC_CONN_H 19 | -------------------------------------------------------------------------------- /src/os/unix/os_util.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "os.h" 4 | 5 | #include "os_util.h" 6 | 7 | int os_init_onstartup() { 8 | return 0; 9 | } 10 | 11 | int os_clean() { 12 | return 0; 13 | } 14 | 15 | void rgettimeofday(struct timeval *val) { 16 | gettimeofday(val, 0); 17 | } 18 | 19 | 20 | void CloseSocket(int sock) { 21 | if (sock >= 0) { 22 | close(sock); 23 | } 24 | } 25 | 26 | int GetPrevSockErr() { 27 | return errno; 28 | } 29 | -------------------------------------------------------------------------------- /src/os/win/FdUtil.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 12/4/17. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "os.h" 10 | #include "FdUtil.h" 11 | 12 | static int setBlockingMode(int &fd, u_long mode) { 13 | return ioctlsocket(fd, FIONBIO, &mode); 14 | } 15 | 16 | int FdUtil::SetNonblocking(int &fd) { 17 | return setBlockingMode(fd, 1); 18 | } 19 | 20 | void FdUtil::SetBlocking(int &fd) { 21 | setBlockingMode(fd, 0); 22 | } 23 | 24 | bool FdUtil::FileExists(const char *fName) { 25 | return _access(fName, F_OK) == 0; 26 | } 27 | 28 | int FdUtil::CreateFile(const std::string &fName, int mode) { 29 | if (!FileExists(fName.c_str())) { 30 | HANDLE fileHandle = ::CreateFile(fName.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_NEW, 0, 0); 31 | if (fileHandle == INVALID_HANDLE_VALUE) { 32 | fprintf(stdout, "failed to create file: %s, err: %d\n", fName.c_str(), GetLastError()); 33 | return -1; 34 | } 35 | 36 | CloseHandle(fileHandle); 37 | } 38 | 39 | return 1; 40 | } 41 | 42 | int FdUtil::IsDir(const std::string &dirName) { 43 | DWORD dwAttrib = GetFileAttributes(dirName.c_str()); 44 | 45 | return (dwAttrib != INVALID_FILE_ATTRIBUTES && 46 | (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); 47 | } 48 | 49 | int FdUtil::CreateDir(const std::string &dirName, int mode) { 50 | return CreateDirectory(dirName.c_str(), NULL); 51 | } -------------------------------------------------------------------------------- /src/os/win/ProcUtil.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 1/11/18. 3 | // 4 | 5 | #include "ProcUtil.h" 6 | 7 | int ProcUtil::MakeDaemon() { 8 | return 0; 9 | } 10 | 11 | bool ProcUtil::IsRoot() { 12 | return true; 13 | } -------------------------------------------------------------------------------- /src/os/win/os_util.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "os.h" 3 | #include "os_util.h" 4 | 5 | static struct WSAData gs_wsaData; 6 | 7 | int os_init_onstartup() { 8 | return WSAStartup(MAKEWORD(2, 2), &gs_wsaData); 9 | } 10 | 11 | int os_clean() { 12 | return WSACleanup(); 13 | } 14 | 15 | void win_gettimeofday(struct timeval *tp, void *tzp) { 16 | time_t clock; 17 | struct tm tm; 18 | SYSTEMTIME wtm; 19 | GetLocalTime(&wtm); 20 | tm.tm_year = wtm.wYear - 1900; 21 | tm.tm_mon = wtm.wMonth - 1; 22 | tm.tm_mday = wtm.wDay; 23 | tm.tm_hour = wtm.wHour; 24 | tm.tm_min = wtm.wMinute; 25 | tm.tm_sec = wtm.wSecond; 26 | tm.tm_isdst = -1; 27 | clock = mktime(&tm); 28 | tp->tv_sec = clock; 29 | tp->tv_usec = wtm.wMilliseconds * 1000; 30 | } 31 | 32 | 33 | void rgettimeofday(struct timeval *val) { 34 | win_gettimeofday(val, 0); 35 | } 36 | 37 | void CloseSocket(int sock) { 38 | if (sock >= 0) { 39 | ::closesocket(sock); 40 | } 41 | } 42 | 43 | int GetPrevSockErr() { 44 | return WSAGetLastError(); 45 | } 46 | -------------------------------------------------------------------------------- /src/rcommon.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 11/13/17. 3 | // 4 | 5 | #include 6 | #include "rcommon.h" 7 | 8 | void alloc_buf(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) { 9 | buf->base = (char *) malloc(suggested_size); 10 | buf->len = suggested_size; 11 | } 12 | 13 | void free_rbuf(rbuf_t *buf) { 14 | if (buf) { 15 | if (buf->base) { 16 | free(buf->base); 17 | buf->base = NULL; 18 | } 19 | free(buf); 20 | } 21 | } 22 | 23 | void free_rwrite_req(rwrite_req_t *req) { 24 | if (req) { 25 | if (req->buf.base) { 26 | free(req->buf.base); 27 | } 28 | free(req); 29 | } 30 | } 31 | 32 | void free_rudp_send(rudp_send_t *send) { 33 | if (send) { 34 | if (send->buf.base) { 35 | free(send->buf.base); 36 | } 37 | if (send->addr) { 38 | free(send->addr); 39 | } 40 | free(send); 41 | } 42 | } 43 | 44 | void close_cb(uv_handle_t *handle) { 45 | free(handle); 46 | } -------------------------------------------------------------------------------- /src/service/IBaseService.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/8/18. 3 | // 4 | 5 | #ifndef RSOCK_IBASESERVICE_H 6 | #define RSOCK_IBASESERVICE_H 7 | 8 | 9 | #include "IService.h" 10 | 11 | template 12 | class IBaseService : public IService { 13 | public: 14 | virtual int RegisterObserver(T *observer); 15 | 16 | virtual int UnRegisterObserver(T *observer); 17 | 18 | private: 19 | using IService::RegisterObserver; 20 | using IService::UnRegisterObserver; 21 | }; 22 | 23 | template 24 | int IBaseService::RegisterObserver(T *observer) { 25 | //return IService::RegisterObserver(dynamic_cast(observer)); 26 | return IService::RegisterObserver(observer); 27 | } 28 | 29 | template 30 | int IBaseService::UnRegisterObserver(T *observer) { 31 | //return IService::UnRegisterObserver(dynamic_cast(observer)); 32 | return IService::UnRegisterObserver(observer); 33 | } 34 | 35 | 36 | #endif //RSOCK_IBASESERVICE_H 37 | -------------------------------------------------------------------------------- /src/service/INetObserver.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/9/18. 3 | // 4 | 5 | #ifndef RSOCK_INETOBSERVER_H 6 | #define RSOCK_INETOBSERVER_H 7 | 8 | #include "IObserver.h" 9 | 10 | struct TcpInfo; 11 | 12 | class INetObserver : public IObserver { 13 | public: 14 | virtual void OnTcpFinOrRst(const TcpInfo &info) = 0; 15 | 16 | // need to add udp and icmp? 17 | }; 18 | 19 | #endif //RSOCK_INETOBSERVER_H 20 | -------------------------------------------------------------------------------- /src/service/IObserver.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 5/23/18. 3 | // 4 | 5 | #ifndef RSOCK_IOBSERVER_H 6 | #define RSOCK_IOBSERVER_H 7 | 8 | class IObserver { 9 | public: 10 | 11 | virtual ~IObserver() = default; 12 | 13 | virtual int Init() = 0; 14 | 15 | /* 16 | * Subclass should override this method and call Service.Unregister(this). 17 | */ 18 | virtual int Close() = 0; 19 | }; 20 | 21 | #endif //RSOCK_IOBSERVER_H 22 | -------------------------------------------------------------------------------- /src/service/IRouteObserver.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 5/31/18. 3 | // 4 | 5 | #ifndef RSOCK_ROUTEOBSERVER_H 6 | #define RSOCK_ROUTEOBSERVER_H 7 | 8 | #include 9 | #include "IObserver.h" 10 | 11 | class IRouteObserver : public IObserver { 12 | public: 13 | virtual void OnNetConnected(const std::string &ifName, const std::string &ip) = 0; 14 | 15 | virtual void OnNetDisconnected() = 0; 16 | }; 17 | 18 | 19 | #endif //RSOCK_ROUTEOBSERVER_H 20 | -------------------------------------------------------------------------------- /src/service/IService.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 5/21/18. 3 | // 4 | 5 | #ifndef RSOCK_ISERVICE_H 6 | #define RSOCK_ISERVICE_H 7 | 8 | #include 9 | #include 10 | 11 | class IObserver; 12 | 13 | class IService { 14 | public: 15 | virtual ~IService() = default; 16 | 17 | IService(); 18 | 19 | virtual int Init(); 20 | 21 | virtual int Close(); 22 | 23 | /* 24 | * May throw std::logic_error("ConcurrentModificationException"); 25 | */ 26 | virtual int RegisterObserver(IObserver *observer); 27 | 28 | /* 29 | * May throw std::logic_error("ConcurrentModificationException"); 30 | */ 31 | virtual int UnRegisterObserver(IObserver *observer); 32 | 33 | virtual int ObserverSize() const; 34 | 35 | virtual bool ContainsObserver(IObserver *observer); 36 | 37 | /* 38 | * If set to true, the service will no longer notify events 39 | */ 40 | void SetBlock(bool block); 41 | 42 | /* 43 | * Return block status. If true, the server should 44 | */ 45 | bool Blocked() const; 46 | 47 | class IIterator { 48 | public: 49 | explicit IIterator(IService *service); 50 | 51 | virtual ~IIterator(); 52 | 53 | virtual IObserver *Next() = 0; 54 | 55 | virtual bool HasNext() = 0; 56 | 57 | virtual void Remove() = 0; 58 | 59 | // protected: 60 | // virtual IObserver *moveToNext() = 0; 61 | private: 62 | IService *mService = nullptr; 63 | bool mInited = false; 64 | }; 65 | 66 | virtual IIterator *NewIterator(); 67 | 68 | private: 69 | class Iterator : public IIterator { 70 | public: 71 | explicit Iterator(IService *service); 72 | 73 | IObserver *Next() override; 74 | 75 | bool HasNext() override; 76 | 77 | void Remove() override; 78 | 79 | private: 80 | IService *mService = nullptr; 81 | std::vector::iterator mIterator; 82 | }; 83 | 84 | private: 85 | std::vector mObserver; // use vector to main register order. 86 | bool mInited = false; 87 | std::atomic mVisitCount; 88 | bool mBlock = false; 89 | }; 90 | 91 | 92 | #endif //RSOCK_ISERVICE_H 93 | -------------------------------------------------------------------------------- /src/service/ITimerObserver.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 5/23/18. 3 | // 4 | 5 | #ifndef RSOCK_ITIMEROBSERVER_H 6 | #define RSOCK_ITIMEROBSERVER_H 7 | 8 | #include 9 | #include "IObserver.h" 10 | 11 | class TimerService; 12 | 13 | class ITimerObserver : public IObserver { 14 | public: 15 | /* 16 | * The method called when timeout. 17 | */ 18 | virtual void OnFlush(uint64_t timestamp) = 0; 19 | 20 | /* 21 | * @return Time interval(ms) to repeat. Cannot be 0. 22 | * Default value is 1000ms. 23 | */ 24 | virtual uint64_t IntervalMs() const { return 1000; }; 25 | }; 26 | 27 | #endif //RSOCK_ITIMEROBSERVER_H 28 | -------------------------------------------------------------------------------- /src/service/NetService.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/9/18. 3 | // 4 | 5 | #include "NetService.h" 6 | #include "ServiceUtil.h" 7 | #include "INetObserver.h" 8 | 9 | void NetService::NotifyTcpFinOrRst(const TcpInfo &info) { 10 | if (!Blocked()) { 11 | auto fn = [&](INetObserver *o) { 12 | o->OnTcpFinOrRst(info); 13 | }; 14 | ServiceUtil::ForEach(this, fn); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /src/service/NetService.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/9/18. 3 | // 4 | 5 | #ifndef RSOCK_NETSERVICE_H 6 | #define RSOCK_NETSERVICE_H 7 | 8 | 9 | #include "IBaseService.h" 10 | 11 | //class INetObserver; 12 | #include "INetObserver.h" 13 | 14 | struct TcpInfo; 15 | 16 | class NetService final : public IBaseService { 17 | public: 18 | void NotifyTcpFinOrRst(const TcpInfo &info); 19 | }; 20 | 21 | 22 | #endif //RSOCK_NETSERVICE_H 23 | -------------------------------------------------------------------------------- /src/service/RouteService.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 5/31/18. 3 | // 4 | 5 | #ifndef RSOCK_ROUTESERVICE_H 6 | #define RSOCK_ROUTESERVICE_H 7 | 8 | #include "uv.h" 9 | #include "../../util/Handler.h" 10 | #include "IBaseService.h" 11 | #include "IRouteObserver.h" 12 | 13 | //class IRouteObserver; 14 | 15 | class Handler; 16 | 17 | class RouteService final : public IBaseService { 18 | public: 19 | explicit RouteService(); 20 | 21 | int Close() override; 22 | 23 | /* 24 | * Check network status with delay. 25 | */ 26 | void CheckNetworkStatusDelayed(); 27 | 28 | /* 29 | * Check network status immediately. 30 | */ 31 | void CheckNetworkStatusNow(); 32 | 33 | /* 34 | * There are cases where only online event is reported while offline event is not. 35 | * i.e when the network switches and during switching, the caller doesn't detect network change(some conn is still alive), 36 | * so it doesn't call CheckXXX of this class. So, there is no offline reported. 37 | * After some time the caller detect no network, it call CheckXXX of this class. 38 | * The service successfully detects a new network, it will report online event. 39 | */ 40 | void NotifyOffline(); 41 | 42 | void NotifyOnline(const std::string &dev, const std::string &ip); 43 | 44 | protected: 45 | void handleMessage(const Handler::Message &m); 46 | 47 | private: 48 | void doubleIntervalSec(); 49 | 50 | private: 51 | 52 | Handler::SPHandler mHandler = nullptr; 53 | 54 | uint64_t mCheckIntervalSec = 1; 55 | 56 | static const int MSG_CHECK = 0; 57 | 58 | }; 59 | 60 | 61 | #endif //RSOCK_ROUTESERVICE_H 62 | -------------------------------------------------------------------------------- /src/service/ServiceUtil.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/1/18. 3 | // 4 | 5 | #include 6 | #include "../singletons/ServiceManager.h" 7 | 8 | template 9 | T ServiceUtil::GetService(const std::string &name) { 10 | ServiceManager *manager = ServiceManager::GetInstance(); 11 | auto iservice = manager->GetService(name); 12 | auto service = dynamic_cast(iservice); 13 | return service; 14 | } 15 | 16 | //ServiceUtil::SPIterator ServiceUtil::NewIterator(IService *service) { 17 | // auto it = service->NewIterator(); 18 | // return std::shared_ptr(it); 19 | //} 20 | 21 | template 22 | void ServiceUtil::ForEach(IService *service, const F &f, Args... args) { 23 | // auto it = NewIterator(service); 24 | auto it = std::shared_ptr(service->NewIterator()); 25 | while (it->HasNext()) { 26 | auto e = it->Next(); 27 | auto *irb = dynamic_cast(e); 28 | f(irb, args...); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/service/ServiceUtil.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/1/18. 3 | // 4 | 5 | #ifndef RSOCK_SERVICEUTIL_H 6 | #define RSOCK_SERVICEUTIL_H 7 | 8 | #include 9 | #include "IService.h" 10 | 11 | class ServiceUtil { 12 | public: 13 | template 14 | static T GetService(const std::string &name); 15 | 16 | // using SPIterator = std::shared_ptr; 17 | // static SPIterator NewIterator(IService *service); // failed to compile. why? 18 | 19 | 20 | /* 21 | * If you want to unregister observer during iteration, 22 | * you must not use this method and call IService.NewIterator yourself, then use Iterator.Remove. 23 | * Unregister during ServiceUtil::ForEach will throw exception. 24 | */ 25 | template 26 | static void ForEach(IService *service, const F &f, Args... args);; 27 | }; 28 | 29 | #include "ServiceUtil.cpp" 30 | 31 | #endif //RSOCK_SERVICEUTIL_H 32 | -------------------------------------------------------------------------------- /src/service/TimerService.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 5/21/18. 3 | // 4 | 5 | #ifndef RSOCK_TIMERSERVICE_H 6 | #define RSOCK_TIMERSERVICE_H 7 | 8 | 9 | #include 10 | #include 11 | #include "uv.h" 12 | #include "IBaseService.h" 13 | 14 | //class ITimerObserver; 15 | #include "ITimerObserver.h" 16 | 17 | class TimerService final : public IBaseService { 18 | public: 19 | /* 20 | * The helper class to specify time precision. The default is in seconds. 21 | */ 22 | enum Precision { 23 | MS100, // 100ms 24 | SECONDS, // 1sec 25 | }; 26 | 27 | explicit TimerService(uv_loop_t *loop); 28 | 29 | TimerService(uv_loop_t *loop, Precision precision); 30 | 31 | int Close() override; 32 | 33 | /* 34 | * Same as RegisterObserver(ITimerObserver *, uint64_t , uint64_t ). But the delay is equal to interval. 35 | */ 36 | int RegisterObserver(ITimerObserver *observer) override; 37 | 38 | /* 39 | * Register time observer. 40 | * @param observer the time observer to be registered. 41 | * @param interval time interval to notify observer. 42 | * @param delay delay time of first notification. 43 | * @return value of repeat interval. If non-repeat, you'd better use ShotHandler. 44 | */ 45 | int RegisterObserver(ITimerObserver *observer, uint64_t delay); 46 | 47 | int UnRegisterObserver(ITimerObserver *observer) override; 48 | 49 | protected: 50 | void onFlush(); 51 | 52 | private: 53 | 54 | uint64_t nextExpireTs(uint64_t start); 55 | 56 | static void timerCb(uv_timer_t *timer); 57 | 58 | void setupTimer(); 59 | 60 | void destroyTimer(); 61 | 62 | private: 63 | uv_loop_t *mLoop = nullptr; 64 | uv_timer_t *mTimer = nullptr; 65 | std::map mExpireMap; 66 | Precision mPrecision = SECONDS; 67 | }; 68 | 69 | 70 | #endif //RSOCK_TIMERSERVICE_H 71 | -------------------------------------------------------------------------------- /src/singletons/ConfManager.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/1/18. 3 | // 4 | 5 | #include "ConfManager.h" 6 | #include "../../bean/RConfig.h" 7 | 8 | int ConfManager::Init() { 9 | assert(mConf); 10 | return 0; 11 | } 12 | 13 | int ConfManager::Close() { 14 | return 0; 15 | } 16 | 17 | std::string ConfManager::GetDev() const { 18 | return mConf->param.dev; 19 | } 20 | 21 | std::string ConfManager::GetIp() const { 22 | return mConf->param.selfCapIp; 23 | } 24 | 25 | void ConfManager::UpdateDevInfo(const std::string &ifName, const std::string &ip) { 26 | mConf->param.dev = ifName; 27 | mConf->param.selfCapIp = ip; 28 | } 29 | 30 | const RConfig &ConfManager::Conf() const { 31 | return *mConf; 32 | } 33 | 34 | ConfManager::ConfManager() { 35 | mConf = new RConfig(); 36 | } 37 | 38 | ConfManager::~ConfManager() { 39 | if (mConf) { 40 | delete mConf; 41 | mConf = nullptr; 42 | } 43 | } 44 | 45 | RConfig &ConfManager::Conf() { 46 | return *mConf; 47 | } 48 | 49 | void ConfManager::SetDev(const std::string &dev) { 50 | mConf->param.dev = dev; 51 | } 52 | 53 | void ConfManager::SetIp(const std::string &ip) { 54 | mConf->param.selfCapIp = ip; 55 | } 56 | -------------------------------------------------------------------------------- /src/singletons/ConfManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/1/18. 3 | // 4 | 5 | #ifndef RSOCK_CONFIGMANAGER_H 6 | #define RSOCK_CONFIGMANAGER_H 7 | 8 | #include 9 | 10 | #include "../util/ICloseable.h" 11 | #include "Singleton.h" 12 | 13 | struct RConfig; 14 | 15 | class ConfManager final : public ICloseable, public Singleton { 16 | public: 17 | ~ConfManager() override; 18 | 19 | int Init(); 20 | 21 | int Close() override; 22 | 23 | std::string GetDev() const; 24 | 25 | std::string GetIp() const; 26 | 27 | void SetDev(const std::string &dev); 28 | 29 | void SetIp(const std::string &ip); 30 | 31 | RConfig &Conf(); 32 | 33 | const RConfig &Conf() const; 34 | 35 | // If implement IRouteObserver, and ConfManager.UpdateDevInfo is called behind other registers 36 | // and those registers rely on ConfManager, then they may get wrong information 37 | // Program logic should not assume the register order. 38 | void UpdateDevInfo(const std::string &ifName, const std::string &ip); 39 | 40 | ConfManager(); 41 | 42 | private: 43 | RConfig *mConf = nullptr; 44 | }; 45 | 46 | 47 | #endif //RSOCK_CONFIGMANAGER_H 48 | -------------------------------------------------------------------------------- /src/singletons/HandlerUtil.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/10/18. 3 | // 4 | 5 | #include "HandlerUtil.h" 6 | 7 | HandlerUtil::HandlerUtil(struct uv_loop_s *loop) : Singleton() { 8 | mLoop = loop; 9 | } 10 | 11 | int HandlerUtil::Close() { 12 | mLoop = nullptr; 13 | return 0; 14 | } 15 | 16 | Handler::SPHandler HandlerUtil::ObtainHandler(const Handler::Callback &cb, uv_loop_t *loop) { 17 | if (!loop) { 18 | loop = HandlerUtil::GetInstance(nullptr)->mLoop; 19 | } 20 | assert(loop); 21 | if (cb) { 22 | return Handler::NewHandler(loop, cb); 23 | } else { 24 | return Handler::NewHandler(loop); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/singletons/HandlerUtil.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/10/18. 3 | // 4 | 5 | #ifndef RSOCK_HANDLERUTIL_H 6 | #define RSOCK_HANDLERUTIL_H 7 | 8 | 9 | #include "Singleton.h" 10 | #include "../../util/Handler.h" 11 | 12 | struct uv_loop_s; 13 | 14 | class HandlerUtil : public Singleton, public ICloseable { 15 | public: 16 | static Handler::SPHandler ObtainHandler(const Handler::Callback &cb = nullptr, uv_loop_t *loop = nullptr); 17 | 18 | explicit HandlerUtil(struct uv_loop_s *loop); 19 | 20 | int Close() override; 21 | 22 | private: 23 | struct uv_loop_s *mLoop = nullptr; 24 | }; 25 | 26 | 27 | #endif //RSOCK_HANDLERUTIL_H 28 | -------------------------------------------------------------------------------- /src/singletons/RouteManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 5/31/18. 3 | // 4 | 5 | #ifndef RSOCK_ROUTEMANAGER_H 6 | #define RSOCK_ROUTEMANAGER_H 7 | 8 | #include 9 | #include 10 | #include "Singleton.h" 11 | 12 | 13 | struct addr; 14 | 15 | class RouteManager : public Singleton, public ICloseable { 16 | public: 17 | RouteManager(); 18 | 19 | explicit RouteManager(const std::vector &dns); 20 | 21 | int Init(); 22 | 23 | int Close() override; 24 | 25 | int GetWanInfo(std::string &ifName, std::string &ip); 26 | 27 | /* 28 | * @param target The target to connect to. Which is used to determine the default gateway. 29 | * The last added has the most priority. If there was already one record in container, 30 | * it's priority will be changed to highest 31 | */ 32 | void AddTargetFront(const std::string &target); 33 | 34 | std::deque GetDns() const; 35 | 36 | private: 37 | std::deque mConnectTarget; 38 | 39 | int getDefGateway(struct addr &defGateway); 40 | 41 | int getIfaceInfo(std::string &ifName, std::string &ip, uint32_t gw); 42 | }; 43 | 44 | 45 | #endif //RSOCK_ROUTEMANAGER_H 46 | -------------------------------------------------------------------------------- /src/singletons/ServiceManager.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 5/24/18. 3 | // 4 | 5 | #include 6 | #include 7 | #include "ServiceManager.h" 8 | #include "../service/TimerService.h" 9 | 10 | const std::string ServiceManager::TIMER_SERVICE = "TimerService"; 11 | const std::string ServiceManager::ROUTE_SERVICE = "RouteService"; 12 | const std::string ServiceManager::NET_SERVICE = "NetService"; 13 | 14 | void ServiceManager::AddService(const std::string &serviceName, IService *service) { 15 | auto it = mServiceMap.find(serviceName); 16 | assert(it == mServiceMap.end()); 17 | mServiceMap.emplace(serviceName, service); 18 | } 19 | 20 | void ServiceManager::RemoveService(const std::string &serviceName) { 21 | auto it = mServiceMap.find(serviceName); 22 | assert(it != mServiceMap.end()); 23 | mServiceMap.erase(it); 24 | } 25 | 26 | IService *ServiceManager::GetService(const std::string &serviceName) { 27 | auto it = mServiceMap.find(serviceName); 28 | return it != mServiceMap.end() ? it->second : nullptr; 29 | } 30 | 31 | int ServiceManager::Init() { 32 | for (auto &e: mServiceMap) { 33 | int nret = e.second->Init(); 34 | if (nret) { 35 | LOGE << "failed to init service " << e.first << ": " << nret; 36 | return nret; 37 | } 38 | } 39 | 40 | return 0; 41 | } 42 | 43 | int ServiceManager::Close() { 44 | auto aCopy = mServiceMap; 45 | for (auto &e: aCopy) { 46 | RemoveService(e.first); 47 | int nret = e.second->Close(); 48 | LOGE_IF(nret != 0) << "failed to close service: " << e.first << ": " << nret; 49 | delete e.second; 50 | } 51 | assert(mServiceMap.empty()); 52 | return 0; 53 | } -------------------------------------------------------------------------------- /src/singletons/ServiceManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 5/24/18. 3 | // 4 | 5 | #ifndef RSOCK_SERVICEMANAGER_H 6 | #define RSOCK_SERVICEMANAGER_H 7 | 8 | #include 9 | #include 10 | #include "uv.h" 11 | #include "../util/ICloseable.h" 12 | #include "Singleton.h" 13 | 14 | class IService; 15 | 16 | class ServiceManager final : public ICloseable, public Singleton { 17 | public: 18 | static const std::string TIMER_SERVICE; 19 | 20 | static const std::string ROUTE_SERVICE; 21 | 22 | static const std::string NET_SERVICE; 23 | 24 | /* 25 | * Init services already managed by ServiceManager. 26 | */ 27 | int Init(); 28 | 29 | int Close() override; 30 | 31 | void AddService(const std::string &serviceName, IService *service); 32 | 33 | void RemoveService(const std::string &serviceName); 34 | 35 | IService *GetService(const std::string &serviceName); 36 | 37 | private: 38 | std::map mServiceMap; 39 | }; 40 | 41 | 42 | #endif //RSOCK_SERVICEMANAGER_H 43 | -------------------------------------------------------------------------------- /src/singletons/Singleton.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/1/18. 3 | // 4 | 5 | //#include 6 | 7 | template 8 | Singleton::Singleton() { 9 | static_assert(std::is_base_of::value, "Template parameter must implement ICloseable"); 10 | } 11 | 12 | template 13 | Singleton::~Singleton() { 14 | assert(!sInstance); // should be properly closed. 15 | } 16 | 17 | template 18 | template 19 | T *Singleton::GetInstance(const Args &... args) { 20 | if (!sInstance) { 21 | std::lock_guard lk(sMutex); 22 | if (!sInstance) { 23 | if (sDestroyed) { 24 | //LOGE << "already destroyed"; 25 | assert(0); 26 | } 27 | sInstance = new T(args...); 28 | } 29 | } 30 | return sInstance; 31 | } 32 | 33 | template 34 | int Singleton::DestroyInstance() { 35 | int nret = 0; 36 | if (sInstance) { 37 | std::lock_guard lk(sMutex); 38 | if (sInstance) { 39 | sDestroyed = true; 40 | auto *closeable = dynamic_cast(sInstance); 41 | nret = closeable->Close(); 42 | auto p = sInstance; 43 | sInstance = nullptr; 44 | delete p; 45 | } 46 | } 47 | return nret; 48 | } 49 | -------------------------------------------------------------------------------- /src/singletons/Singleton.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/1/18. 3 | // 4 | 5 | #ifndef RSOCK_SINGLETON_H 6 | #define RSOCK_SINGLETON_H 7 | 8 | #include 9 | #include 10 | #include "../util/ICloseable.h" 11 | 12 | template 13 | class Singleton { 14 | public: 15 | template 16 | static T *GetInstance(const Args&...args); 17 | 18 | static int DestroyInstance(); 19 | 20 | virtual ~Singleton(); 21 | 22 | protected: 23 | Singleton(); 24 | 25 | private: 26 | static std::mutex sMutex; 27 | static T *sInstance; 28 | static bool sDestroyed; 29 | }; 30 | 31 | template 32 | std::mutex Singleton::sMutex; 33 | 34 | template 35 | T *Singleton::sInstance = nullptr; 36 | 37 | template 38 | bool Singleton::sDestroyed; 39 | 40 | #include "Singleton.cpp" 41 | 42 | #endif //RSOCK_SINGLETON_H 43 | -------------------------------------------------------------------------------- /src/sync/IPacketSyncConn.cpp: -------------------------------------------------------------------------------- 1 | #include "uv.h" 2 | #include 3 | #include "IPacketSyncConn.h" 4 | #include "os_util.h" 5 | #include "../../util/rsutil.h" 6 | #include "rscomm.h" 7 | 8 | IPacketSyncConn::IPacketSyncConn(uv_loop_t *loop, const Callback cb, void *obj) : ISyncConn(loop, cb, obj) { 9 | assert(mCb); 10 | } 11 | 12 | int IPacketSyncConn::Init() { 13 | int nret = CreateSockPair(mLoop, &mSocks); 14 | 15 | if (nret) { 16 | LOGE << "createSockPair failed: " << nret; 17 | return nret; 18 | } 19 | assert(mSocks.ptr); 20 | 21 | return 0; 22 | } 23 | 24 | void IPacketSyncConn::Close() { 25 | CloseSockPair(&mSocks); 26 | mLoop = nullptr; 27 | } 28 | 29 | int IPacketSyncConn::Send(int nread, const rbuf_t &rbuf) { 30 | return doSend(rbuf.base, nread); 31 | } 32 | 33 | int IPacketSyncConn::doSend(const char *buf, int nread) { 34 | if (nread > 0) { 35 | int nret = sendto(mSocks.writeFd, buf, nread, 0, nullptr, 0); 36 | LOGI_IF(nret < 0) << "sendto error: " << strerror(errno); 37 | return nret; 38 | } 39 | return nread; 40 | } 41 | 42 | void IPacketSyncConn::CloseSockPair(sock_pair_t *sockPair) { 43 | if (sockPair) { 44 | if (sockPair->writeFd >= 0) { 45 | CloseSocket(sockPair->writeFd); 46 | } 47 | if (sockPair->ptr) { 48 | uv_close((uv_handle_t *) sockPair->ptr, close_cb); 49 | } 50 | if (sockPair->readFd >= 0) { 51 | CloseSocket(sockPair->readFd); 52 | } 53 | sockPair->readFd = -1; 54 | sockPair->writeFd = -1; 55 | sockPair->ptr = nullptr; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/sync/IPacketSyncConn.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 5/6/18. 3 | // 4 | 5 | #ifndef RSOCK_IPACKETSYNCCONN_H 6 | #define RSOCK_IPACKETSYNCCONN_H 7 | 8 | 9 | #include "ISyncConn.h" 10 | 11 | class IPacketSyncConn : public ISyncConn { 12 | public: 13 | struct sock_pair_t { 14 | int writeFd = -1; 15 | int readFd = -1; 16 | void *ptr = nullptr; 17 | }; 18 | 19 | IPacketSyncConn(uv_loop_t *loop, const Callback cb, void *obj); 20 | 21 | int Init() override; 22 | 23 | void Close() override; 24 | 25 | int Send(int nread, const rbuf_t &rbuf) override; 26 | 27 | protected: 28 | /* 29 | * Send through socket by default. synchronous way. 30 | */ 31 | virtual int doSend(const char *buf, int nread); 32 | 33 | virtual int CreateSockPair(struct uv_loop_s *loop, sock_pair_t *socks) = 0; 34 | 35 | // default: close(writeFd); uv_close((uv_handle_t*) readPtr); 36 | virtual void CloseSockPair(sock_pair_t *socks); 37 | 38 | protected: 39 | sock_pair_t mSocks; 40 | 41 | }; 42 | 43 | 44 | #endif //RSOCK_IPACKETSYNCCONN_H 45 | -------------------------------------------------------------------------------- /src/sync/ISyncConn.h: -------------------------------------------------------------------------------- 1 | #ifndef ISYNC_CONN_H 2 | #define ISYNC_CONN_H 3 | 4 | #include "rcommon.h" 5 | #include "uv.h" 6 | 7 | class ISyncConn { 8 | public: 9 | using Callback = int(*)(void *obj, ssize_t nread, const rbuf_t &rbuf); 10 | 11 | explicit ISyncConn(uv_loop_t *loop, const Callback cb, void *obj); 12 | 13 | virtual ~ISyncConn() = default; 14 | 15 | virtual int Init() = 0; 16 | 17 | virtual void Close() = 0; 18 | 19 | virtual int Send(int nread, const rbuf_t &rbuf) = 0; 20 | 21 | static void trySetGoodBufSize(int NTIMES, int readSock, int writeSock); 22 | 23 | static void trySetGoodBufSize(int NTIMES, uv_handle_t *readHandle, uv_handle_t *writeHandle); 24 | 25 | protected: 26 | // use cb 27 | virtual int Input(ssize_t nread, const rbuf_t &rbuf); 28 | 29 | protected: 30 | struct uv_loop_s *mLoop = nullptr; 31 | const Callback mCb = nullptr; 32 | void *mObj = nullptr; 33 | }; 34 | 35 | #endif // !ISYNC_CONN_H 36 | -------------------------------------------------------------------------------- /src/sync/LoopStreamSyncConn.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 5/6/18. 3 | // 4 | 5 | #ifndef RSOCK_STREAMDECORATOR_H 6 | #define RSOCK_STREAMDECORATOR_H 7 | 8 | 9 | #include "TcpStreamSyncConn.h" 10 | 11 | class LoopStreamSyncConn : public TcpStreamSyncConn { 12 | public: 13 | LoopStreamSyncConn(uv_loop_t *loop, const Callback cb, void *obj); 14 | 15 | int Init() override; 16 | 17 | void Close() override; 18 | 19 | protected: 20 | int doSend(const char *buf, int nread) override; 21 | 22 | static void writeCb(uv_write_t* req, int status); 23 | 24 | static void threadCb(void *arg); 25 | private: 26 | uv_loop_t *mWriteLoop = nullptr; 27 | uv_tcp_t *mTcp = nullptr; 28 | uv_thread_t mThread = 0; 29 | }; 30 | 31 | 32 | #endif //RSOCK_STREAMDECORATOR_H 33 | -------------------------------------------------------------------------------- /src/sync/SyncConnFactory.cpp: -------------------------------------------------------------------------------- 1 | #include "SyncConnFactory.h" 2 | #include "TcpStreamSyncConn.h" 3 | 4 | #include "UdpSyncConn.h" 5 | 6 | #ifndef _WIN32 7 | 8 | #include "../os/unix/conn/UnixDgramSyncConn.h" 9 | #include "LoopStreamSyncConn.h" 10 | 11 | #endif // !_WIN32 12 | 13 | // speed are almost same 14 | ISyncConn *SyncConnFactory::CreateSysSyncConn(struct uv_loop_s *loop, const ISyncConn::Callback cb, void *obj) { 15 | #ifndef _WIN32 16 | return new UnixDgramSyncConn(loop, cb, obj); 17 | // return new LoopStreamSyncConn(loop, cb, obj); 18 | #else 19 | //return new UdpSyncConn(loop, cb, obj); // extremely low performance on Winddows 20 | return new TcpStreamSyncConn(loop, cb, obj); 21 | // return new LoopStreamSyncConn(loop, cb, obj); 22 | #endif // _WIN32 23 | } 24 | -------------------------------------------------------------------------------- /src/sync/SyncConnFactory.h: -------------------------------------------------------------------------------- 1 | #ifndef SYNC_CONN_FACTORY_H 2 | #define SYNC_CONN_FACTORY_H 3 | 4 | #include "ISyncConn.h" 5 | 6 | class SyncConnFactory { 7 | public: 8 | static ISyncConn* CreateSysSyncConn(struct uv_loop_s *loop, const ISyncConn::Callback cb, void *obj); 9 | }; 10 | 11 | #endif // !SYNC_CONN_FACTORY_H 12 | -------------------------------------------------------------------------------- /src/sync/TcpStreamSyncConn.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 5/5/18. 3 | // 4 | 5 | #ifndef RSOCK_TCPSEGSYNCCONN_H 6 | #define RSOCK_TCPSEGSYNCCONN_H 7 | 8 | #include "rscomm.h" 9 | #include "ISyncConn.h" 10 | 11 | class TcpStreamSyncConn : public ISyncConn { 12 | public: 13 | TcpStreamSyncConn(uv_loop_t *loop, const Callback cb, void *obj); 14 | 15 | int Init() override; 16 | 17 | void Close() override; 18 | 19 | int Send(int nread, const rbuf_t &rbuf) override; 20 | 21 | protected: 22 | virtual int rawInput(int nread, const char *buf); 23 | 24 | virtual int doSend(const char *buf, int nread); 25 | 26 | static void streamCb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf); 27 | 28 | static void connectCb(struct sockaddr_in *addr, int *clientSock); 29 | 30 | int getWriteSock() const; 31 | 32 | void setWriteSock(int writeSock); 33 | 34 | private: 35 | void markTempBuf(int nextLen, const char *buf, int bufLen); 36 | 37 | private: 38 | using COUNT_TYPE = int; 39 | 40 | static const int COUNT_SIZE = sizeof(COUNT_TYPE); 41 | static const int MAX_BUF_SIZE = OM_MAX_PKT_SIZE + COUNT_SIZE * 2; 42 | 43 | uv_stream_t *mReadStream = nullptr; 44 | int mWriteSock = -1; 45 | // starts with data, not len. 46 | char mTempBuf[MAX_BUF_SIZE]; 47 | int mNextLen = 0; 48 | int mBufLen = 0; 49 | char mLargeBuf[RSOCK_UV_MAX_BUF + MAX_BUF_SIZE]; 50 | }; 51 | 52 | 53 | #endif //RSOCK_TCPSEGSYNCCONN_H 54 | -------------------------------------------------------------------------------- /src/sync/UdpSyncConn.h: -------------------------------------------------------------------------------- 1 | #ifndef UDP_SYNC_CONN_H 2 | #define UDP_SYNC_CONN_H 3 | 4 | 5 | #include "IPacketSyncConn.h" 6 | 7 | class UdpSyncConn : public IPacketSyncConn { 8 | public: 9 | UdpSyncConn(struct uv_loop_s *loop, const Callback cb, void *obj); 10 | 11 | int CreateSockPair(struct uv_loop_s *loop, sock_pair_t *socks) override; 12 | 13 | protected: 14 | static void udpCb(struct uv_udp_s* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags); 15 | 16 | }; 17 | 18 | #endif // !UDP_SYNC_CONN_H 19 | -------------------------------------------------------------------------------- /src/util/ICloseable.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/2/18. 3 | // 4 | 5 | #ifndef RSOCK_ICLOSEABLE_H 6 | #define RSOCK_ICLOSEABLE_H 7 | 8 | 9 | class ICloseable { 10 | public: 11 | virtual ~ICloseable() = default; 12 | 13 | virtual int Close() = 0; 14 | }; 15 | 16 | 17 | #endif //RSOCK_ICLOSEABLE_H 18 | -------------------------------------------------------------------------------- /src/util/KeyGenerator.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/8/18. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include "../../bean/TcpInfo.h" 9 | #include "../../util/enc.h" 10 | 11 | #include "KeyGenerator.h" 12 | #include "../../util/rsutil.h" 13 | 14 | uint64_t KeyGenerator::sIcmpConv = 1; 15 | 16 | IntKeyType KeyGenerator::KeyForTcp(const TcpInfo &info) { 17 | assert(!info.IsUdp()); 18 | assert(info.sp != 0 && info.dp != 0); 19 | 20 | uint64_t key = INIT_KEY | TYPE_TCP; 21 | uint64_t dp = info.dp; // don't use bit shift operation directly 22 | uint64_t sp = info.sp; 23 | key |= (dp << 16) | sp; 24 | return key; 25 | } 26 | 27 | IntKeyType KeyGenerator::KeyForUdp(const ConnInfo &info) { 28 | assert(info.IsUdp()); 29 | assert(info.sp != 0 && info.dp != 0); 30 | 31 | uint64_t key = INIT_KEY | TYPE_UDP; 32 | uint64_t dp = info.dp; // don't use bit shift operation directly 33 | uint64_t sp = info.sp; 34 | key |= (dp << 16) | sp; 35 | return key; 36 | } 37 | 38 | IntKeyType KeyGenerator::KeyForConnInfo(const ConnInfo &info) { 39 | if (!info.IsUdp()) { 40 | return KeyForTcp(info); 41 | } else { 42 | return KeyForUdp(info); 43 | } 44 | } 45 | 46 | IntKeyType KeyGenerator::NewKeyForIcmp() { 47 | uint64_t key = INIT_KEY | TYPE_ICMP; 48 | key |= (sIcmpConv++); 49 | return key; 50 | } 51 | 52 | const char *KeyGenerator::DecodeKey(const char *p, IntKeyType *key) { 53 | return decode_uint64(key, p); 54 | } 55 | 56 | int KeyGenerator::DecodeKeySafe(int nread, const char *p, IntKeyType *key) { 57 | if (nread >= sizeof(IntKeyType)) { 58 | return DecodeKey(p, key) - p; 59 | } 60 | return 0; 61 | } 62 | 63 | char *KeyGenerator::EncodeKey(char *p, IntKeyType key) { 64 | return encode_uint64(key, p); 65 | } 66 | 67 | std::string KeyGenerator::BuildConvKey(uint32_t dst, uint32_t conv) { 68 | std::ostringstream out; 69 | out << "conv:" << InAddr2Ip(dst) << ":" << conv; 70 | return out.str(); 71 | } -------------------------------------------------------------------------------- /src/util/KeyGenerator.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/8/18. 3 | // 4 | 5 | #ifndef RSOCK_KEYGENERATOR_H 6 | #define RSOCK_KEYGENERATOR_H 7 | 8 | #include 9 | #include 10 | 11 | struct TcpInfo; 12 | 13 | struct ConnInfo; 14 | 15 | /* 16 | * Generate key for INetConn 17 | */ 18 | class KeyGenerator { 19 | public: 20 | // upper 8bits used for type 21 | static const uint64_t TYPE_INVALID = 0x00000000; 22 | static const uint64_t TYPE_TCP = 0x10000000; 23 | static const uint64_t TYPE_UDP = 0x20000000; 24 | static const uint64_t TYPE_ICMP = 0x30000000; 25 | 26 | static const uint64_t INVALID_KEY = TYPE_INVALID; 27 | 28 | static IntKeyType KeyForConnInfo(const ConnInfo &info); 29 | 30 | static IntKeyType KeyForTcp(const TcpInfo &info); 31 | 32 | static IntKeyType KeyForUdp(const ConnInfo &info); 33 | 34 | /* 35 | * Not thread safe. 36 | */ 37 | static IntKeyType NewKeyForIcmp(); 38 | 39 | static const char *DecodeKey(const char *p, IntKeyType *key); 40 | 41 | static int DecodeKeySafe(int nread, const char *p, IntKeyType *key); 42 | 43 | static char *EncodeKey(char *p, IntKeyType key); 44 | 45 | static std::string StrForIntKey(IntKeyType key) { return std::to_string(key); } 46 | 47 | static std::string BuildConvKey(uint32_t dst, uint32_t conv); 48 | 49 | private: 50 | static uint64_t sIcmpConv; 51 | static const uint64_t INIT_KEY = 0; 52 | }; 53 | 54 | 55 | #endif //RSOCK_KEYGENERATOR_H 56 | -------------------------------------------------------------------------------- /src/util/RouteUtil.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/12/18. 3 | // 4 | 5 | #include "RouteUtil.h" 6 | 7 | 8 | bool RouteUtil::SameNetwork(const std::string &dev1, const std::string &ip1, const std::string &dev2, 9 | const std::string &ip2) { 10 | return (dev1 == dev2) && (ip1 == ip2); 11 | } 12 | -------------------------------------------------------------------------------- /src/util/RouteUtil.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/12/18. 3 | // 4 | 5 | #ifndef RSOCK_ROUTEUTIL_H 6 | #define RSOCK_ROUTEUTIL_H 7 | 8 | #include 9 | 10 | class RouteUtil { 11 | public: 12 | static bool 13 | SameNetwork(const std::string &dev1, const std::string &ip1, const std::string &dev2, const std::string &ip2); 14 | }; 15 | 16 | 17 | #endif //RSOCK_ROUTEUTIL_H 18 | -------------------------------------------------------------------------------- /src/util/TcpCmpFn.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/10/18. 3 | // 4 | 5 | #include "TcpCmpFn.h" 6 | 7 | -------------------------------------------------------------------------------- /src/util/TcpCmpFn.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/10/18. 3 | // 4 | 5 | #ifndef RSOCK_TCPINFOCMP_H 6 | #define RSOCK_TCPINFOCMP_H 7 | 8 | #include 9 | #include "../../bean/TcpInfo.h" 10 | 11 | struct TcpCmpFn { 12 | inline bool operator()(const TcpInfo &info1, const TcpInfo &info2) const { 13 | // return (info1.src < info2.src) || (info1.sp < info2.sp) || (info1.dst < info2.dst) || (info1.dp < info2.dp); 14 | std::vector> l = { 15 | {info1.src, info2.src}, 16 | {(uint32_t) info1.sp, (uint32_t) info2.sp}, 17 | {info1.dst, info2.dst}, 18 | {(uint32_t) info1.dp, (uint32_t) info2.dp}, 19 | }; 20 | for (auto &e: l) { 21 | if (e.first < e.second) { 22 | return true; 23 | } else if (e.first > e.second) { 24 | return false; 25 | } 26 | } 27 | return false; // totally equal: return false 28 | } 29 | 30 | static inline bool Equals(const TcpInfo &info1, const TcpInfo &info2) { 31 | return (info1.src == info2.src) && (info1.sp == info2.sp) && (info1.dst == info2.dst) && (info1.dp == info2.dp); 32 | } 33 | }; 34 | 35 | 36 | #endif //RSOCK_TCPINFOCMP_H 37 | -------------------------------------------------------------------------------- /src/util/TcpUtil.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/12/18. 3 | // 4 | 5 | #include 6 | #include "TcpUtil.h" 7 | #include "../../conn/FakeTcp.h" 8 | 9 | void TcpUtil::SetISN(FakeTcp *c, const TcpInfo &info) { 10 | if (c) { 11 | // during tcp 3-way handshake. client will receive pkt with SYN set. server will receive pkt with SYN && ACK set. 12 | // seq and ack for both side will remain 13 | 14 | // caution: this cannot be exactly right. It may cause indefinite ack. but why?? 15 | c->SetISN(info.ack + 1); 16 | if (info.ack == 0) { 17 | LOGW << "info.ack is zero. set to 1"; 18 | } 19 | } 20 | } 21 | 22 | void TcpUtil::SetAckISN(FakeTcp *conn, const TcpInfo &info) { 23 | if (conn) { 24 | // during tcp 3-way handshake. client will receive pkt with SYN set. server will receive pkt with SYN && ACK set. 25 | // seq and ack for both side will remain 26 | 27 | // caution: this cannot be exactly right. It may cause indefinite ack. but why?? 28 | conn->SetAckISN(info.seq + 1); 29 | if (info.seq == 0) { 30 | LOGW << "info.seq is zero. set to 1"; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/util/TcpUtil.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 6/12/18. 3 | // 4 | 5 | #ifndef RSOCK_TCPUTIL_H 6 | #define RSOCK_TCPUTIL_H 7 | 8 | class FakeTcp; 9 | 10 | struct TcpInfo; 11 | 12 | class TcpUtil { 13 | public: 14 | static void SetISN(FakeTcp *conn, const TcpInfo &info); 15 | 16 | static void SetAckISN(FakeTcp *conn, const TcpInfo &info); 17 | }; 18 | 19 | 20 | #endif //RSOCK_TCPUTIL_H 21 | -------------------------------------------------------------------------------- /test/test_client.cpp: -------------------------------------------------------------------------------- 1 | #include "../client/csock.h" 2 | 3 | //#define DEV "lo0" 4 | #define TIP "127.0.0.1" 5 | //#define TPORTS "20005-20006" 6 | 7 | int main(int argc, char **argv) { 8 | if (argc > 1) { 9 | return csock_main(argc, argv); 10 | } 11 | 12 | char *fakearg[] = { 13 | argv[0], 14 | // "--dev=" DEV, 15 | "--ludp=127.0.0.1:30000", 16 | "--taddr=" TIP, 17 | "-v", 18 | }; 19 | 20 | return csock_main(sizeof(fakearg) / sizeof(fakearg[0]), fakearg); 21 | } -------------------------------------------------------------------------------- /test/test_server.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/27/17. 3 | // 4 | 5 | #include "../server/ssock.h" 6 | 7 | #define DEV "lo0" 8 | #define TADDR "127.0.0.1:30010" 9 | #define LPORTS "20005-20010" 10 | 11 | int main(int argc, char **argv) { 12 | if (argc > 1) { 13 | return ssock_main(argc, argv); 14 | } 15 | 16 | char *fakearg[] = { 17 | argv[0], 18 | "--dev=" DEV, 19 | "--taddr=" TADDR, 20 | "--ports=" LPORTS, 21 | "-v", 22 | }; 23 | 24 | return ssock_main(sizeof(fakearg)/ sizeof(fakearg[0]), fakearg); 25 | } -------------------------------------------------------------------------------- /thirdparty/md5.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 12/11/17. 3 | // 4 | 5 | #ifndef OMNIPIPE_MD5_H 6 | #define OMNIPIPE_MD5_H 7 | 8 | /* 9 | * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. 10 | * MD5 Message-Digest Algorithm (RFC 1321). 11 | * 12 | * Homepage: 13 | * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 14 | * 15 | * Author: 16 | * Alexander Peslyak, better known as Solar Designer 17 | * 18 | * This software was written by Alexander Peslyak in 2001. No copyright is 19 | * claimed, and the software is hereby placed in the public domain. 20 | * In case this attempt to disclaim copyright and place the software in the 21 | * public domain is deemed null and void, then the software is 22 | * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the 23 | * general public under the following terms: 24 | * 25 | * Redistribution and use in source and binary forms, with or without 26 | * modification, are permitted. 27 | * 28 | * There's ABSOLUTELY NO WARRANTY, express or implied. 29 | * 30 | * See md5.c for more information. 31 | */ 32 | 33 | #ifdef HAVE_OPENSSL 34 | #include 35 | #elif !defined(_MD5_H) 36 | #define _MD5_H 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | /* Any 32-bit or wider unsigned integer data type will do */ 42 | typedef unsigned int MD5_u32plus; 43 | 44 | typedef struct { 45 | MD5_u32plus lo, hi; 46 | MD5_u32plus a, b, c, d; 47 | unsigned char buffer[64]; 48 | MD5_u32plus block[16]; 49 | } MD5_CTX; 50 | 51 | extern void MD5_Init(MD5_CTX *ctx); 52 | extern void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size); 53 | extern void MD5_Final(unsigned char *result, MD5_CTX *ctx); 54 | 55 | #ifdef __cplusplus 56 | } 57 | #endif 58 | #endif 59 | #endif //OMNIPIPE_MD5_H 60 | -------------------------------------------------------------------------------- /util/PortPair.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/29/17. 3 | // 4 | 5 | #include 6 | #include "PortPair.h" 7 | 8 | PortPair::PortPair(uint16_t sp, uint16_t dp) { 9 | source = sp; 10 | dest = dp; 11 | } 12 | 13 | bool PortPair::operator==(const PortPair &p1) { 14 | return (p1.source == source) && (p1.dest == dest); 15 | } 16 | 17 | bool PortPair::operator!=(const PortPair &p1) { 18 | return !(*this == p1); 19 | } 20 | -------------------------------------------------------------------------------- /util/PortPair.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/29/17. 3 | // 4 | 5 | #ifndef RSOCK_PORTPAIR_H 6 | #define RSOCK_PORTPAIR_H 7 | 8 | 9 | #include 10 | 11 | struct PortPair { 12 | public: 13 | PortPair() = delete; 14 | 15 | PortPair(uint16_t sp, uint16_t dp); 16 | 17 | bool operator==(const PortPair &p1); 18 | 19 | bool operator!=(const PortPair &p1); 20 | public: 21 | uint16_t source = 0; 22 | uint16_t dest = 0; 23 | }; 24 | 25 | 26 | #endif //RSOCK_PORTPAIR_H 27 | -------------------------------------------------------------------------------- /util/RPortList.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/29/17. 3 | // 4 | 5 | #ifndef RSOCK_PORuint16_tLISuint16_t_H 6 | #define RSOCK_PORuint16_tLISuint16_t_H 7 | 8 | 9 | #include 10 | #include 11 | #include 12 | #include "PortPair.h" 13 | 14 | // simply ports. [1, 2, 3-10] 15 | class RPortList { 16 | public: 17 | using PortList = std::vector; 18 | using PortRangeList = std::vector; 19 | using value_type = uint16_t; 20 | 21 | RPortList() = default; 22 | 23 | RPortList(const RPortList &portList) = default; 24 | 25 | RPortList(const std::initializer_list &list); 26 | 27 | // if named with AddPort16, the method will collide a macro on windows 28 | virtual void AddPort16(uint16_t port); 29 | 30 | // start < end 31 | virtual void AddPortRange(uint16_t start, uint16_t end); 32 | 33 | virtual const PortList &GetSinglePortList(); 34 | 35 | virtual const PortRangeList &GetPortRangeList(); 36 | 37 | virtual const PortList &GetRawList(); 38 | 39 | virtual bool empty() const; 40 | 41 | static std::string ToString(const RPortList &list); 42 | 43 | static bool FromString(RPortList &list, const std::string &str); 44 | 45 | RPortList &operator=(const RPortList &) = default; 46 | 47 | private: 48 | inline bool valid() const; 49 | 50 | inline void invalidState(); 51 | 52 | inline void rebuild(); 53 | 54 | void buildRPortList(const std::initializer_list &list); 55 | 56 | private: 57 | bool mValid = false; 58 | PortList mRawList; 59 | PortList mSingleOnes; 60 | PortRangeList mValentines; 61 | }; 62 | 63 | 64 | #endif //RSOCK_PORuint16_tLISuint16_t_H 65 | -------------------------------------------------------------------------------- /util/ShotHandler.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 2/2/18. 3 | // 4 | 5 | #include "ShotHandler.h" 6 | 7 | ShotHandler::ShotHandler(uv_loop_t *loop) : Handler(loop) {} 8 | 9 | ShotHandler::ShotHandler(uv_loop_t *loop, const Handler::Callback &cb) : Handler(loop, cb) {} 10 | 11 | ShotHandler::SPShotHandler ShotHandler::NewShotHandler(uv_loop_t *loop) { 12 | return NewShotHandler(loop, nullptr); 13 | } 14 | 15 | ShotHandler::SPShotHandler ShotHandler::NewShotHandler(uv_loop_t *loop, const Handler::Callback &cb) { 16 | std::shared_ptr sp(new ShotHandler(loop, cb), &Deleter); 17 | sp->mSelfRef = sp; 18 | return sp; 19 | } 20 | 21 | void ShotHandler::OnTimeout() { 22 | Handler::OnTimeout(); 23 | 24 | if (Size() == 0) { 25 | mSelfRef = nullptr; 26 | } 27 | } 28 | 29 | void ShotHandler::Close() { 30 | Handler::Close(); 31 | mSelfRef = nullptr; 32 | } 33 | 34 | void ShotHandler::Deleter(ShotHandler *handler) { 35 | delete handler; 36 | } 37 | 38 | Handler::Task ShotHandler::Shot(uv_loop_t *loop, const Handler::ITask &task) { 39 | return NewShotHandler(loop)->Post(task); 40 | } 41 | 42 | void ShotHandler::Shot(uv_loop_t *loop, const Handler::Task &task) { 43 | NewShotHandler(loop)->Post(task); 44 | } 45 | 46 | Handler::Task ShotHandler::ShotDelayed(uv_loop_t *loop, const Handler::ITask &task, uint64_t delay) { 47 | return NewShotHandler(loop)->PostDelayed(task, delay); 48 | } 49 | 50 | void ShotHandler::ShotDelayed(uv_loop_t *loop, const Handler::Task &task, uint64_t delay) { 51 | NewShotHandler(loop)->PostDelayed(task, delay); 52 | } 53 | 54 | Handler::Task ShotHandler::ShotAtTime(uv_loop_t *loop, const Handler::ITask &task, uint64_t ts) { 55 | return NewShotHandler(loop)->PostAtTime(task, ts); 56 | } 57 | 58 | void ShotHandler::ShotAtTime(uv_loop_t *loop, const Handler::Task &task, uint64_t ts) { 59 | NewShotHandler(loop)->PostAtTime(task, ts); 60 | } -------------------------------------------------------------------------------- /util/ShotHandler.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 2/2/18. 3 | // 4 | 5 | #ifndef RPIPE_RHANDLER_H 6 | #define RPIPE_RHANDLER_H 7 | 8 | 9 | #include "Handler.h" 10 | 11 | class ShotHandler : public Handler { 12 | public: 13 | using SPShotHandler = std::shared_ptr; 14 | 15 | static SPShotHandler NewShotHandler(uv_loop_t *loop); 16 | 17 | static SPShotHandler NewShotHandler(uv_loop_t *loop, const Callback &cb); 18 | 19 | static Task Shot(uv_loop_t *loop, const ITask &task); 20 | 21 | static void Shot(uv_loop_t *loop, const Task &task); 22 | 23 | static Task ShotDelayed(uv_loop_t *loop, const ITask &task, uint64_t delay); 24 | 25 | static void ShotDelayed(uv_loop_t *loop, const Task &task, uint64_t delay); 26 | 27 | static Task ShotAtTime(uv_loop_t *loop, const ITask &task, uint64_t ts); 28 | 29 | static void ShotAtTime(uv_loop_t *loop, const Task &task, uint64_t ts); 30 | 31 | explicit ShotHandler(uv_loop_t *loop); 32 | 33 | explicit ShotHandler(uv_loop_t *loop, const Callback &cb); 34 | 35 | void Close() override; 36 | 37 | protected: 38 | void OnTimeout() override; 39 | 40 | static void Deleter(ShotHandler *handler); 41 | 42 | private: 43 | using Handler::NewHandler; 44 | 45 | SPShotHandler mSelfRef = nullptr; 46 | }; 47 | 48 | 49 | #endif //RPIPE_RHANDLER_H 50 | -------------------------------------------------------------------------------- /util/TextUtils.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/27/17. 3 | // 4 | 5 | #include 6 | #include 7 | //#include 8 | #include "TextUtils.h" 9 | 10 | template 11 | std::string TextUtils::Vector2String(const std::vector &vec) { 12 | std::ostringstream out; 13 | std::ostream_iterator it(out, ","); 14 | std::copy(vec.begin(), vec.end(), it); 15 | return out.str(); 16 | } 17 | -------------------------------------------------------------------------------- /util/TextUtils.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/27/17. 3 | // 4 | 5 | #ifndef RSOCK_TEXTUTILS_H 6 | #define RSOCK_TEXTUTILS_H 7 | 8 | #include 9 | #include 10 | #include "rstype.h" 11 | 12 | class TextUtils { 13 | public: 14 | template 15 | static std::string Vector2String(const std::vector &vec); 16 | }; 17 | 18 | #include "TextUtils.cpp" 19 | #endif //RSOCK_TEXTUTILS_H 20 | -------------------------------------------------------------------------------- /util/UvUtil.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 2/21/18. 3 | // 4 | 5 | #include 6 | #include "UvUtil.h" 7 | 8 | uv_signal_s *UvUtil::WatchSignal(uv_loop_t *loop, int sig, uv_signal_cb cb, void *data) { 9 | uv_signal_t *uv_signal = static_cast(malloc(sizeof(uv_signal_t))); 10 | if (uv_signal_init(loop, uv_signal)) { 11 | uv_close(reinterpret_cast(uv_signal), close_cb); 12 | return nullptr; 13 | } 14 | uv_signal->data = data; 15 | if (uv_signal_start(uv_signal, cb, sig)) { 16 | uv_close(reinterpret_cast(uv_signal), close_cb); 17 | return nullptr; 18 | } 19 | return uv_signal; 20 | } 21 | 22 | void UvUtil::close_cb(uv_handle_t *handle) { 23 | free(handle); 24 | } 25 | 26 | void UvUtil::stop_and_close_loop_fully(uv_loop_t *loop) { 27 | uv_stop(loop); 28 | uv_walk(loop, close_walk_cb, nullptr); 29 | uv_loop_close(loop); 30 | } 31 | 32 | void UvUtil::close_walk_cb(uv_handle_t *handle, void *arg) { 33 | if (!uv_is_closing(handle)) { 34 | uv_close(handle, nullptr); // should be deleted by itself 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /util/UvUtil.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 2/21/18. 3 | // 4 | 5 | #ifndef RPIPE_UVUTIL_H 6 | #define RPIPE_UVUTIL_H 7 | 8 | 9 | #include 10 | 11 | class UvUtil { 12 | public: 13 | static uv_signal_s *WatchSignal(uv_loop_t *loop, int sig, uv_signal_cb cb, void *data); 14 | 15 | static void close_cb(uv_handle_t *handle); 16 | 17 | static void stop_and_close_loop_fully(uv_loop_t *loop); 18 | 19 | protected: 20 | static void close_walk_cb(uv_handle_t *handle, void *arg); 21 | }; 22 | 23 | 24 | #endif //RPIPE_UVUTIL_H 25 | -------------------------------------------------------------------------------- /util/enc.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created on 10/30/17. 3 | // 4 | 5 | #ifndef SOCKNM_ENC_H 6 | #define SOCKNM_ENC_H 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | #include 13 | 14 | char *encode_uint32(uint32_t i, char *p); 15 | 16 | char *encode_uint64(uint64_t i, char *p); 17 | 18 | char *encode_uint16(uint16_t i, char *p); 19 | 20 | char *encode_uint8(uint8_t i, char *p); 21 | 22 | const char *decode_uint32(uint32_t *i, const char *p); 23 | 24 | const char *decode_uint16(uint16_t *i, const char *p); 25 | 26 | const char *decode_uint8(uint8_t *i, const char *p); 27 | 28 | const char *decode_uint64(uint64_t *i, const char *p); 29 | 30 | int8_t is_little_endian(); 31 | 32 | void big_endian_to_little(uint32_t i, char *p); 33 | 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | #endif //SOCKNM_ENC_H 38 | -------------------------------------------------------------------------------- /util/rhash.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by System Administrator on 12/23/17. 3 | // 4 | 5 | #ifndef RSOCK_RHASH_H 6 | #define RSOCK_RHASH_H 7 | 8 | #include 9 | #include "rstype.h" 10 | 11 | bool ValidIp4(const std::string &ip); 12 | 13 | bool hash_equal(const char *hashed_buf, const std::string &key, const char *data, int data_len); 14 | 15 | bool EmptyIdBuf(const IdBufType &id); 16 | 17 | char * compute_hash(char *hash, const std::string &key, const char *data, int data_len); 18 | 19 | std::string IdBuf2Str(const IdBufType &id); 20 | 21 | IdBufType Str2IdBuf(const std::string &str); 22 | 23 | std::string HashBuf2String(const HashBufType &hash); 24 | 25 | void GenerateIdBuf(IdBufType &hash, const std::string &key); 26 | 27 | #endif //RSOCK_RHASH_H 28 | -------------------------------------------------------------------------------- /xbuild/bak/Windows_x86/Packet.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/bak/Windows_x86/Packet.dll -------------------------------------------------------------------------------- /xbuild/bak/Windows_x86/wpcap.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/bak/Windows_x86/wpcap.dll -------------------------------------------------------------------------------- /xbuild/cmake/Darwin_x86_64.toolchain.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_SYSTEM_NAME Darwin) 2 | set(CMAKE_SYSTEM_VERSION 1) 3 | set(CMAKE_SYSTEM_PROCESSOR x86_64) 4 | 5 | set(CMAKE_C_COMPILER clang) 6 | set(CMAKE_CXX_COMPILER clang++) 7 | 8 | # project. static linking c++ library 9 | include(${CMAKE_CURRENT_LIST_DIR}/base.cmake) 10 | -------------------------------------------------------------------------------- /xbuild/cmake/Linux_x86_64.toolchain.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_SYSTEM_NAME Linux) 2 | set(CMAKE_SYSTEM_VERSION 1) 3 | set(CMAKE_SYSTEM_PROCESSOR x86_64) 4 | 5 | set(LINUX_TOOLCHAIN_NAME x86_64-ubuntu16.04-linux-gnu-) 6 | set(LINUX_TOOLCHAIN_ROOT /Volumes/crosstool-ng/x-tools/x86_64-ubuntu16.04-linux-gnu) 7 | set(CMAKE_SYSROOT ${LINUX_TOOLCHAIN_ROOT}/x86_64-ubuntu16.04-linux-gnu/sysroot) 8 | set(CMAKE_C_COMPILER ${LINUX_TOOLCHAIN_ROOT}/bin/${LINUX_TOOLCHAIN_NAME}gcc) 9 | set(CMAKE_CXX_COMPILER ${LINUX_TOOLCHAIN_ROOT}/bin/${LINUX_TOOLCHAIN_NAME}g++) 10 | set(CMAKE_AR ${LINUX_TOOLCHAIN_ROOT}/bin/${LINUX_TOOLCHAIN_NAME}ar CACHE FILEPATH "Archiver") 11 | set(CMAKE_RANLIB ${LINUX_TOOLCHAIN_ROOT}/bin/${LINUX_TOOLCHAIN_NAME}ranlib CACHE FILEPATH "Ranlib") 12 | set(CMAKE_ASM_COMPILER ${LINUX_TOOLCHAIN_ROOT}/bin/${LINUX_TOOLCHAIN_NAME}as) 13 | set(CMAKE_LINKER ${LINUX_TOOLCHAIN_ROOT}/bin/${LINUX_TOOLCHAIN_NAME}ld) 14 | set(CMAKE_NM ${LINUX_TOOLCHAIN_ROOT}/bin/${LINUX_TOOLCHAIN_NAME}nm) 15 | set(CMAKE_OBJCOPY ${LINUX_TOOLCHAIN_ROOT}/bin/${LINUX_TOOLCHAIN_NAME}objcopy) 16 | set(CMAKE_OBJDUMP ${LINUX_TOOLCHAIN_ROOT}/bin/${LINUX_TOOLCHAIN_NAME}objdump) 17 | set(CMAKE_STRIP ${LINUX_TOOLCHAIN_ROOT}/bin/${LINUX_TOOLCHAIN_NAME}strip) 18 | 19 | 20 | # project. static linking c++ library 21 | include(${CMAKE_CURRENT_LIST_DIR}/base.cmake) -------------------------------------------------------------------------------- /xbuild/cmake/Linux_x86_64.universe_toolchain.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_SYSTEM_NAME Linux) 2 | set(CMAKE_SYSTEM_VERSION 1) 3 | set(CMAKE_SYSTEM_PROCESSOR x86_64) 4 | 5 | #set(CMAKE_C_COMPILER gcc-6) 6 | #set(CMAKE_CXX_COMPILER g++-6) 7 | # project. static linking c++ library 8 | include(${CMAKE_CURRENT_LIST_DIR}/base.cmake) 9 | -------------------------------------------------------------------------------- /xbuild/cmake/Windows_x86.toolchain.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_SYSTEM_NAME Windows) 2 | set(CMAKE_SYSTEM_VERSION 1) 3 | set(CMAKE_SYSTEM_PROCESSOR x86) # force use 32bit 4 | 5 | #set(WINDOWS_TOOLCHAIN_NAME x86_64-w64-mingw32-) 6 | #set(CMAKE_C_COMPILER ${WINDOWS_TOOLCHAIN_NAME}gcc-posix) 7 | #set(CMAKE_CXX_COMPILER ${WINDOWS_TOOLCHAIN_NAME}g++-posix) 8 | # 9 | #SET(CMAKE_RC_COMPILER ${WINDOWS_TOOLCHAIN_NAME}windres) 10 | #set(CMAKE_AR ${WINDOWS_TOOLCHAIN_NAME}ar CACHE FILEPATH "Archiver") 11 | #set(CMAKE_RANLIB ${WINDOWS_TOOLCHAIN_NAME}ranlib CACHE FILEPATH "Ranlib") 12 | #set(CMAKE_ASM_COMPILER ${WINDOWS_TOOLCHAIN_NAME}as) 13 | #set(CMAKE_LINKER ${WINDOWS_TOOLCHAIN_NAME}ld) 14 | #set(CMAKE_NM ${WINDOWS_TOOLCHAIN_NAME}nm) 15 | #set(CMAKE_OBJCOPY ${WINDOWS_TOOLCHAIN_NAME}objcopy) 16 | #set(CMAKE_OBJDUMP ${WINDOWS_TOOLCHAIN_NAME}objdump) 17 | #set(CMAKE_STRIP ${WINDOWS_TOOLCHAIN_NAME}strip) 18 | 19 | # project. static linking c++ library 20 | include(${CMAKE_CURRENT_LIST_DIR}/base.cmake) 21 | -------------------------------------------------------------------------------- /xbuild/cmake/test_darwin.cmake: -------------------------------------------------------------------------------- 1 | # this is for local development only. It main produces test_client and test_server in addition 2 | set(RSOCK_TEST 1) 3 | include(${CMAKE_CURRENT_LIST_DIR}/Darwin_x86_64.toolchain.cmake) -------------------------------------------------------------------------------- /xbuild/cmake/travis_Darwin_x86_64.cmake: -------------------------------------------------------------------------------- 1 | include(${CMAKE_CURRENT_LIST_DIR}/Darwin_x86_64.toolchain.cmake) -------------------------------------------------------------------------------- /xbuild/cmake/travis_Linux_x86_64.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_SYSTEM_NAME Linux) 2 | set(CMAKE_SYSTEM_VERSION 1) 3 | set(CMAKE_SYSTEM_PROCESSOR x86_64) 4 | 5 | set(CMAKE_C_COMPILER gcc-6) 6 | set(CMAKE_CXX_COMPILER g++-6) 7 | # project. static linking c++ library 8 | include(${CMAKE_CURRENT_LIST_DIR}/base.cmake) 9 | -------------------------------------------------------------------------------- /xbuild/include/libdnet/dnet.h: -------------------------------------------------------------------------------- 1 | /* 2 | * dnet.h 3 | * 4 | * Copyright (c) 2001 Dug Song 5 | * 6 | * $Id: dnet.h,v 1.6 2004/09/10 03:10:01 dugsong Exp $ 7 | */ 8 | 9 | #ifndef DNET_H 10 | #define DNET_H 11 | 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | #include 30 | 31 | #endif /* DNET_H */ 32 | -------------------------------------------------------------------------------- /xbuild/include/libdnet/dnet/Makefile.am: -------------------------------------------------------------------------------- 1 | ## $Id: Makefile.am,v 1.5 2004/09/10 02:35:51 dugsong Exp $ 2 | 3 | include $(top_srcdir)/Makefile.am.common 4 | 5 | dnetincludedir = $(includedir)/dnet 6 | 7 | dnetinclude_HEADERS = addr.h arp.h blob.h eth.h fw.h icmp.h intf.h ip.h \ 8 | ip6.h os.h rand.h route.h tcp.h tun.h udp.h 9 | -------------------------------------------------------------------------------- /xbuild/include/libdnet/dnet/addr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * addr.h 3 | * 4 | * Network address operations. 5 | * 6 | * Copyright (c) 2000 Dug Song 7 | * 8 | * $Id: addr.h,v 1.12 2003/02/27 03:44:55 dugsong Exp $ 9 | */ 10 | 11 | #ifndef DNET_ADDR_H 12 | #define DNET_ADDR_H 13 | 14 | #define ADDR_TYPE_NONE 0 /* No address set */ 15 | #define ADDR_TYPE_ETH 1 /* Ethernet */ 16 | #define ADDR_TYPE_IP 2 /* Internet Protocol v4 */ 17 | #define ADDR_TYPE_IP6 3 /* Internet Protocol v6 */ 18 | 19 | struct addr { 20 | uint16_t addr_type; 21 | uint16_t addr_bits; 22 | union { 23 | eth_addr_t __eth; 24 | ip_addr_t __ip; 25 | ip6_addr_t __ip6; 26 | 27 | uint8_t __data8[16]; 28 | uint16_t __data16[8]; 29 | uint32_t __data32[4]; 30 | } __addr_u; 31 | }; 32 | #define addr_eth __addr_u.__eth 33 | #define addr_ip __addr_u.__ip 34 | #define addr_ip6 __addr_u.__ip6 35 | #define addr_data8 __addr_u.__data8 36 | #define addr_data16 __addr_u.__data16 37 | #define addr_data32 __addr_u.__data32 38 | 39 | #define addr_pack(addr, type, bits, data, len) do { \ 40 | (addr)->addr_type = type; \ 41 | (addr)->addr_bits = bits; \ 42 | memmove((addr)->addr_data8, (char *)data, len); \ 43 | } while (0) 44 | 45 | __BEGIN_DECLS 46 | int addr_cmp(const struct addr *a, const struct addr *b); 47 | 48 | int addr_bcast(const struct addr *a, struct addr *b); 49 | int addr_net(const struct addr *a, struct addr *b); 50 | 51 | char *addr_ntop(const struct addr *src, char *dst, size_t size); 52 | int addr_pton(const char *src, struct addr *dst); 53 | 54 | char *addr_ntoa(const struct addr *a); 55 | #define addr_aton addr_pton 56 | 57 | int addr_ntos(const struct addr *a, struct sockaddr *sa); 58 | int addr_ston(const struct sockaddr *sa, struct addr *a); 59 | 60 | int addr_btos(uint16_t bits, struct sockaddr *sa); 61 | int addr_stob(const struct sockaddr *sa, uint16_t *bits); 62 | 63 | int addr_btom(uint16_t bits, void *mask, size_t size); 64 | int addr_mtob(const void *mask, size_t size, uint16_t *bits); 65 | __END_DECLS 66 | 67 | #endif /* DNET_ADDR_H */ 68 | -------------------------------------------------------------------------------- /xbuild/include/libdnet/dnet/blob.h: -------------------------------------------------------------------------------- 1 | /* 2 | * blob.h 3 | * 4 | * Binary blob handling. 5 | * 6 | * Copyright (c) 2002 Dug Song 7 | * 8 | * $Id: blob.h,v 1.2 2002/04/05 03:06:44 dugsong Exp $ 9 | */ 10 | 11 | #ifndef DNET_BLOB_H 12 | #define DNET_BLOB_H 13 | 14 | typedef struct blob { 15 | u_char *base; /* start of data */ 16 | int off; /* offset into data */ 17 | int end; /* end of data */ 18 | int size; /* size of allocation */ 19 | } blob_t; 20 | 21 | __BEGIN_DECLS 22 | blob_t *blob_new(void); 23 | 24 | int blob_read(blob_t *b, void *buf, int len); 25 | int blob_write(blob_t *b, const void *buf, int len); 26 | 27 | int blob_seek(blob_t *b, int off, int whence); 28 | #define blob_skip(b, l) blob_seek(b, l, SEEK_CUR) 29 | #define blob_rewind(b) blob_seek(b, 0, SEEK_SET) 30 | 31 | #define blob_offset(b) ((b)->off) 32 | #define blob_left(b) ((b)->end - (b)->off) 33 | 34 | int blob_index(blob_t *b, const void *buf, int len); 35 | int blob_rindex(blob_t *b, const void *buf, int len); 36 | 37 | int blob_pack(blob_t *b, const char *fmt, ...); 38 | int blob_unpack(blob_t *b, const char *fmt, ...); 39 | 40 | int blob_insert(blob_t *b, const void *buf, int len); 41 | int blob_delete(blob_t *b, void *buf, int len); 42 | 43 | int blob_print(blob_t *b, char *style, int len); 44 | 45 | blob_t *blob_free(blob_t *b); 46 | 47 | int blob_register_alloc(size_t size, void *(*bmalloc)(size_t), 48 | void (*bfree)(void *), void *(*brealloc)(void *, size_t)); 49 | #ifdef va_start 50 | typedef int (*blob_fmt_cb)(int pack, int len, blob_t *b, va_list *arg); 51 | 52 | int blob_register_pack(char c, blob_fmt_cb fmt_cb); 53 | #endif 54 | __END_DECLS 55 | 56 | #endif /* DNET_BLOB_H */ 57 | -------------------------------------------------------------------------------- /xbuild/include/libdnet/dnet/fw.h: -------------------------------------------------------------------------------- 1 | /* 2 | * fw.h 3 | * 4 | * Network firewalling operations. 5 | * 6 | * Copyright (c) 2001 Dug Song 7 | * 8 | * $Id: fw.h,v 1.13 2002/12/14 04:02:36 dugsong Exp $ 9 | */ 10 | 11 | #ifndef DNET_FW_H 12 | #define DNET_FW_H 13 | 14 | struct fw_rule { 15 | char fw_device[INTF_NAME_LEN]; /* interface name */ 16 | uint8_t fw_op; /* operation */ 17 | uint8_t fw_dir; /* direction */ 18 | uint8_t fw_proto; /* IP protocol */ 19 | struct addr fw_src; /* src address / net */ 20 | struct addr fw_dst; /* dst address / net */ 21 | uint16_t fw_sport[2]; /* range / ICMP type */ 22 | uint16_t fw_dport[2]; /* range / ICMP code */ 23 | }; 24 | 25 | #define FW_OP_ALLOW 1 26 | #define FW_OP_BLOCK 2 27 | 28 | #define FW_DIR_IN 1 29 | #define FW_DIR_OUT 2 30 | 31 | #define fw_pack_rule(rule, dev, op, dir, p, s, d, sp1, sp2, dp1, dp2) \ 32 | do { \ 33 | strlcpy((rule)->fw_device, dev, sizeof((rule)->fw_device)); \ 34 | (rule)->fw_op = op; (rule)->fw_dir = dir; \ 35 | (rule)->fw_proto = p; \ 36 | memmove(&(rule)->fw_src, &(s), sizeof((rule)->fw_src)); \ 37 | memmove(&(rule)->fw_dst, &(d), sizeof((rule)->fw_dst)); \ 38 | (rule)->fw_sport[0] = sp1; (rule)->fw_sport[1] = sp2; \ 39 | (rule)->fw_dport[0] = dp1; (rule)->fw_dport[1] = dp2; \ 40 | } while (0) 41 | 42 | typedef struct fw_handle fw_t; 43 | 44 | typedef int (*fw_handler)(const struct fw_rule *rule, void *arg); 45 | 46 | __BEGIN_DECLS 47 | fw_t *fw_open(void); 48 | int fw_add(fw_t *f, const struct fw_rule *rule); 49 | int fw_delete(fw_t *f, const struct fw_rule *rule); 50 | int fw_loop(fw_t *f, fw_handler callback, void *arg); 51 | fw_t *fw_close(fw_t *f); 52 | __END_DECLS 53 | 54 | #endif /* DNET_FW_H */ 55 | -------------------------------------------------------------------------------- /xbuild/include/libdnet/dnet/rand.h: -------------------------------------------------------------------------------- 1 | /* 2 | * rand.h 3 | * 4 | * Pseudo-random number generation, based on OpenBSD arc4random(). 5 | * 6 | * Copyright (c) 2000 Dug Song 7 | * Copyright (c) 1996 David Mazieres 8 | * 9 | * $Id: rand.h,v 1.4 2002/04/07 19:01:25 dugsong Exp $ 10 | */ 11 | 12 | #ifndef DNET_RAND_H 13 | #define DNET_RAND_H 14 | 15 | typedef struct rand_handle rand_t; 16 | 17 | __BEGIN_DECLS 18 | rand_t *rand_open(void); 19 | 20 | int rand_get(rand_t *r, void *buf, size_t len); 21 | int rand_set(rand_t *r, const void *seed, size_t len); 22 | int rand_add(rand_t *r, const void *buf, size_t len); 23 | 24 | uint8_t rand_uint8(rand_t *r); 25 | uint16_t rand_uint16(rand_t *r); 26 | uint32_t rand_uint32(rand_t *r); 27 | 28 | int rand_shuffle(rand_t *r, void *base, size_t nmemb, size_t size); 29 | 30 | rand_t *rand_close(rand_t *r); 31 | __END_DECLS 32 | 33 | #endif /* DNET_RAND_H */ 34 | -------------------------------------------------------------------------------- /xbuild/include/libdnet/dnet/route.h: -------------------------------------------------------------------------------- 1 | /* 2 | * route.c 3 | * 4 | * Kernel route table operations. 5 | * 6 | * Copyright (c) 2000 Dug Song 7 | * 8 | * $Id: route.h,v 1.6 2002/02/04 04:02:22 dugsong Exp $ 9 | */ 10 | 11 | #ifndef DNET_ROUTE_H 12 | #define DNET_ROUTE_H 13 | 14 | /* 15 | * Routing table entry 16 | */ 17 | struct route_entry { 18 | struct addr route_dst; /* destination address */ 19 | struct addr route_gw; /* gateway address */ 20 | }; 21 | 22 | typedef struct route_handle route_t; 23 | 24 | typedef int (*route_handler)(const struct route_entry *entry, void *arg); 25 | 26 | __BEGIN_DECLS 27 | route_t *route_open(void); 28 | int route_add(route_t *r, const struct route_entry *entry); 29 | int route_delete(route_t *r, const struct route_entry *entry); 30 | int route_get(route_t *r, struct route_entry *entry); 31 | int route_loop(route_t *r, route_handler callback, void *arg); 32 | route_t *route_close(route_t *r); 33 | __END_DECLS 34 | 35 | #endif /* DNET_ROUTE_H */ 36 | -------------------------------------------------------------------------------- /xbuild/include/libdnet/dnet/tun.h: -------------------------------------------------------------------------------- 1 | /* 2 | * tun.h 3 | * 4 | * Network tunnel device. 5 | * 6 | * Copyright (c) 2001 Dug Song 7 | * 8 | * $Id: tun.h,v 1.2 2005/01/25 21:29:12 dugsong Exp $ 9 | */ 10 | 11 | #ifndef DNET_TUN_H 12 | #define DNET_TUN_H 13 | 14 | typedef struct tun tun_t; 15 | 16 | __BEGIN_DECLS 17 | tun_t *tun_open(struct addr *src, struct addr *dst, int mtu); 18 | int tun_fileno(tun_t *tun); 19 | const char *tun_name(tun_t *tun); 20 | ssize_t tun_send(tun_t *tun, const void *buf, size_t size); 21 | ssize_t tun_recv(tun_t *tun, void *buf, size_t size); 22 | tun_t *tun_close(tun_t *tun); 23 | __END_DECLS 24 | 25 | #endif /* DNET_TUN_H */ 26 | -------------------------------------------------------------------------------- /xbuild/include/libdnet/dnet/udp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * udp.h 3 | * 4 | * User Datagram Protocol (RFC 768). 5 | * 6 | * Copyright (c) 2000 Dug Song 7 | * 8 | * $Id: udp.h,v 1.8 2002/04/02 05:05:39 dugsong Exp $ 9 | */ 10 | 11 | #ifndef DNET_UDP_H 12 | #define DNET_UDP_H 13 | 14 | #define UDP_HDR_LEN 8 15 | 16 | struct udp_hdr { 17 | uint16_t uh_sport; /* source port */ 18 | uint16_t uh_dport; /* destination port */ 19 | uint16_t uh_ulen; /* udp length (including header) */ 20 | uint16_t uh_sum; /* udp checksum */ 21 | }; 22 | 23 | #define UDP_PORT_MAX 65535 24 | 25 | #define udp_pack_hdr(hdr, sport, dport, ulen) do { \ 26 | struct udp_hdr *udp_pack_p = (struct udp_hdr *)(hdr); \ 27 | udp_pack_p->uh_sport = htons(sport); \ 28 | udp_pack_p->uh_dport = htons(dport); \ 29 | udp_pack_p->uh_ulen = htons(ulen); \ 30 | } while (0) 31 | 32 | #endif /* DNET_UDP_H */ 33 | -------------------------------------------------------------------------------- /xbuild/include/libdnet_win/dnet.h: -------------------------------------------------------------------------------- 1 | /* 2 | * dnet.h 3 | * 4 | * Copyright (c) 2001 Dug Song 5 | * 6 | * $Id: dnet.h,v 1.6 2004/09/10 03:10:01 dugsong Exp $ 7 | */ 8 | 9 | #ifndef DNET_H 10 | #define DNET_H 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | #include 17 | #include 18 | 19 | 20 | #endif /* DNET_H */ 21 | -------------------------------------------------------------------------------- /xbuild/include/libdnet_win/dnet/addr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * addr.h 3 | * 4 | * Network address operations. 5 | * 6 | * Copyright (c) 2000 Dug Song 7 | * 8 | * $Id: addr.h,v 1.12 2003/02/27 03:44:55 dugsong Exp $ 9 | */ 10 | 11 | #ifndef DNET_ADDR_H 12 | #define DNET_ADDR_H 13 | 14 | #define ADDR_TYPE_NONE 0 /* No address set */ 15 | #define ADDR_TYPE_ETH 1 /* Ethernet */ 16 | #define ADDR_TYPE_IP 2 /* Internet Protocol v4 */ 17 | #define ADDR_TYPE_IP6 3 /* Internet Protocol v6 */ 18 | 19 | struct addr { 20 | uint16_t addr_type; 21 | uint16_t addr_bits; 22 | union { 23 | eth_addr_t __eth; 24 | ip_addr_t __ip; 25 | ip6_addr_t __ip6; 26 | 27 | uint8_t __data8[16]; 28 | uint16_t __data16[8]; 29 | uint32_t __data32[4]; 30 | } __addr_u; 31 | }; 32 | #define addr_eth __addr_u.__eth 33 | #define addr_ip __addr_u.__ip 34 | #define addr_ip6 __addr_u.__ip6 35 | #define addr_data8 __addr_u.__data8 36 | #define addr_data16 __addr_u.__data16 37 | #define addr_data32 __addr_u.__data32 38 | 39 | #define addr_pack(addr, type, bits, data, len) do { \ 40 | (addr)->addr_type = type; \ 41 | (addr)->addr_bits = bits; \ 42 | memmove((addr)->addr_data8, (char *)data, len); \ 43 | } while (0) 44 | 45 | __BEGIN_DECLS 46 | int addr_cmp(const struct addr *a, const struct addr *b); 47 | 48 | int addr_bcast(const struct addr *a, struct addr *b); 49 | int addr_net(const struct addr *a, struct addr *b); 50 | 51 | char *addr_ntop(const struct addr *src, char *dst, size_t size); 52 | int addr_pton(const char *src, struct addr *dst); 53 | 54 | char *addr_ntoa(const struct addr *a); 55 | #define addr_aton addr_pton 56 | 57 | int addr_ntos(const struct addr *a, struct sockaddr *sa); 58 | int addr_ston(const struct sockaddr *sa, struct addr *a); 59 | 60 | int addr_btos(uint16_t bits, struct sockaddr *sa); 61 | int addr_stob(const struct sockaddr *sa, uint16_t *bits); 62 | 63 | int addr_btom(uint16_t bits, void *mask, size_t size); 64 | int addr_mtob(const void *mask, size_t size, uint16_t *bits); 65 | __END_DECLS 66 | 67 | #endif /* DNET_ADDR_H */ 68 | -------------------------------------------------------------------------------- /xbuild/include/libdnet_win/dnet/macros.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef DNET_MACROS_H 3 | #define DNET_MACROS_H 4 | 5 | 6 | #define IP_ADDR_ANY (htonl(0x00000000)) /* 0.0.0.0 */ 7 | #define IP_CLASSA_NET (htonl(0xff000000)) 8 | #define IP_ADDR_LOOPBACK (htonl(0x7f000001)) /* 127.0.0.1 */ 9 | #define IP_ADDR_BITS 32 /* IP address bits */ 10 | #define IP_ADDR_LEN 4 /* IP address length */ 11 | 12 | #define ARP_HRD_ETH 0x0001 /* ethernet hardware */ 13 | 14 | #define ETH_ADDR_BITS 48 15 | #define ETH_ADDR_LEN 6 16 | 17 | #define IP6_ADDR_BITS 128 18 | #define IP6_ADDR_LEN 16 19 | 20 | #define ETH_ADDR_BROADCAST "\xff\xff\xff\xff\xff\xff" 21 | 22 | typedef uint32_t ip_addr_t; 23 | 24 | typedef struct eth_addr { 25 | uint8_t data[ETH_ADDR_LEN]; 26 | } eth_addr_t; 27 | 28 | typedef struct ip6_addr { 29 | uint8_t data[IP6_ADDR_LEN]; 30 | } ip6_addr_t; 31 | 32 | #define IP_LOCAL_GROUP(i) (((uint32_t)(i) & htonl(0xffffff00)) == \ 33 | htonl(0xe0000000)) 34 | 35 | #endif /* DNET_MACROS_H */ 36 | -------------------------------------------------------------------------------- /xbuild/include/libdnet_win/dnet/route.h: -------------------------------------------------------------------------------- 1 | /* 2 | * route.c 3 | * 4 | * Kernel route table operations. 5 | * 6 | * Copyright (c) 2000 Dug Song 7 | * 8 | * $Id: route.h,v 1.6 2002/02/04 04:02:22 dugsong Exp $ 9 | */ 10 | 11 | #ifndef DNET_ROUTE_H 12 | #define DNET_ROUTE_H 13 | 14 | /* 15 | * Routing table entry 16 | */ 17 | struct route_entry { 18 | struct addr route_dst; /* destination address */ 19 | struct addr route_gw; /* gateway address */ 20 | }; 21 | 22 | typedef struct route_handle route_t; 23 | 24 | typedef int (*route_handler)(const struct route_entry *entry, void *arg); 25 | 26 | __BEGIN_DECLS 27 | route_t *route_open(void); 28 | int route_add(route_t *r, const struct route_entry *entry); 29 | int route_delete(route_t *r, const struct route_entry *entry); 30 | int route_get(route_t *r, struct route_entry *entry); 31 | int route_loop(route_t *r, route_handler callback, void *arg); 32 | route_t *route_close(route_t *r); 33 | __END_DECLS 34 | 35 | #endif /* DNET_ROUTE_H */ 36 | -------------------------------------------------------------------------------- /xbuild/include/libnet/gnuc.h: -------------------------------------------------------------------------------- 1 | /* @(#) $Header: /usr/local/CVS/libnet/include/gnuc.h,v 1.1.1.1 2003/06/26 21:55:10 route Exp $ (LBL) */ 2 | 3 | /* Define __P() macro, if necessary */ 4 | #ifndef __P 5 | #if __STDC__ 6 | #define __P(protos) protos 7 | #else 8 | #define __P(protos) () 9 | #endif 10 | #endif 11 | 12 | /* inline foo */ 13 | #ifdef __GNUC__ 14 | #define inline __inline 15 | #else 16 | #define inline 17 | #endif 18 | 19 | /* 20 | * Handle new and old "dead" routine prototypes 21 | * 22 | * For example: 23 | * 24 | * __dead void foo(void) __attribute__((volatile)); 25 | * 26 | */ 27 | #ifdef __GNUC__ 28 | #ifndef __dead 29 | #define __dead volatile 30 | #endif 31 | #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) 32 | #ifndef __attribute__ 33 | #define __attribute__(args) 34 | #endif 35 | #endif 36 | #else 37 | #ifndef __dead 38 | #define __dead 39 | #endif 40 | #ifndef __attribute__ 41 | #define __attribute__(args) 42 | #endif 43 | #endif 44 | -------------------------------------------------------------------------------- /xbuild/include/libnet/ifaddrlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1997 3 | * The Regents of the University of California. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that: (1) source code distributions 7 | * retain the above copyright notice and this paragraph in its entirety, (2) 8 | * distributions including binary code include the above copyright notice and 9 | * this paragraph in its entirety in the documentation or other materials 10 | * provided with the distribution, and (3) all advertising materials mentioning 11 | * features or use of this software display the following acknowledgement: 12 | * ``This product includes software developed by the University of California, 13 | * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14 | * the University nor the names of its contributors may be used to endorse 15 | * or promote products derived from this software without specific prior 16 | * written permission. 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18 | * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20 | * 21 | */ 22 | 23 | struct ifaddrlist 24 | { 25 | #if (HAVE_SOLARIS || HAVE_HPUX11) 26 | uint addr; 27 | #else 28 | uint32_t addr; 29 | #endif 30 | char *device; 31 | }; 32 | 33 | struct libnet_ifaddr_list 34 | { 35 | uint32_t addr; 36 | char *device; 37 | }; 38 | 39 | int 40 | ifaddrlist( 41 | struct ifaddrlist **, 42 | int8_t * 43 | ); 44 | 45 | 46 | int 47 | set_up_interface( 48 | struct sockaddr_in **, 49 | uint8_t ** 50 | ); 51 | 52 | /* EOF */ 53 | -------------------------------------------------------------------------------- /xbuild/include/libnet/libnet.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBNET_H 2 | #define __LIBNET_H 3 | #ifdef _WIN32 4 | #include "libnet_win.h" 5 | #else 6 | #include "libnet_posix.h" 7 | #endif 8 | #endif /* __LIBNET_H */ 9 | 10 | /* EOF */ 11 | -------------------------------------------------------------------------------- /xbuild/include/libnet/libnet/libnet-types.h: -------------------------------------------------------------------------------- 1 | /* 2 | * $Id: libnet-types.h,v 1.3 2004/01/03 20:31:00 mike Exp $ 3 | * 4 | * libnet-types.h - Network routine library macro header file 5 | * 6 | * Copyright (c) 1998 - 2004 Mike D. Schiffman 7 | * All rights reserved. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 1. Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 | * SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | #ifndef __LIBNET_TYPES_H 33 | #define __LIBNET_TYPES_H 34 | 35 | /* libnet should be using the standard type names, so mapping standard 36 | * to non-standard should not be necessary. */ 37 | 38 | #endif /* __LIBNET_TYPES_H */ 39 | 40 | /* EOF */ 41 | -------------------------------------------------------------------------------- /xbuild/include/libpcap/pcap-namedb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1994, 1996 3 | * The Regents of the University of California. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 3. All advertising materials mentioning features or use of this software 14 | * must display the following acknowledgement: 15 | * This product includes software developed by the Computer Systems 16 | * Engineering Group at Lawrence Berkeley Laboratory. 17 | * 4. Neither the name of the University nor of the Laboratory may be used 18 | * to endorse or promote products derived from this software without 19 | * specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | * SUCH DAMAGE. 32 | */ 33 | 34 | /* 35 | * For backwards compatibility. 36 | * 37 | * Note to OS vendors: do NOT get rid of this file! Some applications 38 | * might expect to be able to include . 39 | */ 40 | #include 41 | -------------------------------------------------------------------------------- /xbuild/include/libuv/android-ifaddrs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1995, 1999 3 | * Berkeley Software Design, Inc. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND 12 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 13 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 14 | * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE 15 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 16 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 17 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 18 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 19 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 20 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 21 | * SUCH DAMAGE. 22 | * 23 | * BSDI ifaddrs.h,v 2.5 2000/02/23 14:51:59 dab Exp 24 | */ 25 | 26 | #ifndef _IFADDRS_H_ 27 | #define _IFADDRS_H_ 28 | 29 | struct ifaddrs { 30 | struct ifaddrs *ifa_next; 31 | char *ifa_name; 32 | unsigned int ifa_flags; 33 | struct sockaddr *ifa_addr; 34 | struct sockaddr *ifa_netmask; 35 | struct sockaddr *ifa_dstaddr; 36 | void *ifa_data; 37 | }; 38 | 39 | /* 40 | * This may have been defined in . Note that if is 41 | * to be included it must be included before this header file. 42 | */ 43 | #ifndef ifa_broadaddr 44 | #define ifa_broadaddr ifa_dstaddr /* broadcast address interface */ 45 | #endif 46 | 47 | #include 48 | 49 | __BEGIN_DECLS 50 | extern int getifaddrs(struct ifaddrs **ifap); 51 | extern void freeifaddrs(struct ifaddrs *ifa); 52 | __END_DECLS 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /xbuild/include/libuv/uv-aix.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_AIX_H 23 | #define UV_AIX_H 24 | 25 | #define UV_PLATFORM_LOOP_FIELDS \ 26 | int fs_fd; \ 27 | 28 | #define UV_PLATFORM_FS_EVENT_FIELDS \ 29 | uv__io_t event_watcher; \ 30 | char *dir_filename; \ 31 | 32 | #endif /* UV_AIX_H */ 33 | -------------------------------------------------------------------------------- /xbuild/include/libuv/uv-bsd.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_BSD_H 23 | #define UV_BSD_H 24 | 25 | #define UV_PLATFORM_FS_EVENT_FIELDS \ 26 | uv__io_t event_watcher; \ 27 | 28 | #define UV_IO_PRIVATE_PLATFORM_FIELDS \ 29 | int rcount; \ 30 | int wcount; \ 31 | 32 | #define UV_HAVE_KQUEUE 1 33 | 34 | #endif /* UV_BSD_H */ 35 | -------------------------------------------------------------------------------- /xbuild/include/libuv/uv-linux.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_LINUX_H 23 | #define UV_LINUX_H 24 | 25 | #define UV_PLATFORM_LOOP_FIELDS \ 26 | uv__io_t inotify_read_watcher; \ 27 | void* inotify_watchers; \ 28 | int inotify_fd; \ 29 | 30 | #define UV_PLATFORM_FS_EVENT_FIELDS \ 31 | void* watchers[2]; \ 32 | int wd; \ 33 | 34 | #endif /* UV_LINUX_H */ 35 | -------------------------------------------------------------------------------- /xbuild/include/libuv/uv-os390.h: -------------------------------------------------------------------------------- 1 | /* Copyright libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_MVS_H 23 | #define UV_MVS_H 24 | 25 | #define UV_PLATFORM_SEM_T int 26 | 27 | #define UV_PLATFORM_LOOP_FIELDS \ 28 | void* ep; \ 29 | 30 | #endif /* UV_MVS_H */ 31 | -------------------------------------------------------------------------------- /xbuild/include/libuv/uv-posix.h: -------------------------------------------------------------------------------- 1 | /* Copyright libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_POSIX_H 23 | #define UV_POSIX_H 24 | 25 | #define UV_PLATFORM_LOOP_FIELDS \ 26 | struct pollfd* poll_fds; \ 27 | size_t poll_fds_used; \ 28 | size_t poll_fds_size; \ 29 | unsigned char poll_fds_iterating; \ 30 | 31 | #endif /* UV_POSIX_H */ 32 | -------------------------------------------------------------------------------- /xbuild/include/libuv/uv-sunos.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_SUNOS_H 23 | #define UV_SUNOS_H 24 | 25 | #include 26 | #include 27 | 28 | /* For the sake of convenience and reduced #ifdef-ery in src/unix/sunos.c, 29 | * add the fs_event fields even when this version of SunOS doesn't support 30 | * file watching. 31 | */ 32 | #define UV_PLATFORM_LOOP_FIELDS \ 33 | uv__io_t fs_event_watcher; \ 34 | int fs_fd; \ 35 | 36 | #if defined(PORT_SOURCE_FILE) 37 | 38 | # define UV_PLATFORM_FS_EVENT_FIELDS \ 39 | file_obj_t fo; \ 40 | int fd; \ 41 | 42 | #endif /* defined(PORT_SOURCE_FILE) */ 43 | 44 | #endif /* UV_SUNOS_H */ 45 | -------------------------------------------------------------------------------- /xbuild/include/libuv/uv-threadpool.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | /* 23 | * This file is private to libuv. It provides common functionality to both 24 | * Windows and Unix backends. 25 | */ 26 | 27 | #ifndef UV_THREADPOOL_H_ 28 | #define UV_THREADPOOL_H_ 29 | 30 | struct uv__work { 31 | void (*work)(struct uv__work *w); 32 | void (*done)(struct uv__work *w, int status); 33 | struct uv_loop_s* loop; 34 | void* wq[2]; 35 | }; 36 | 37 | #endif /* UV_THREADPOOL_H_ */ 38 | -------------------------------------------------------------------------------- /xbuild/include/libuv/uv-version.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_VERSION_H 23 | #define UV_VERSION_H 24 | 25 | /* 26 | * Versions with the same major number are ABI stable. API is allowed to 27 | * evolve between minor releases, but only in a backwards compatible way. 28 | * Make sure you update the -soname directives in configure.ac 29 | * and uv.gyp whenever you bump UV_VERSION_MAJOR or UV_VERSION_MINOR (but 30 | * not UV_VERSION_PATCH.) 31 | */ 32 | 33 | #define UV_VERSION_MAJOR 1 34 | #define UV_VERSION_MINOR 16 35 | #define UV_VERSION_PATCH 1 36 | #define UV_VERSION_IS_RELEASE 0 37 | #define UV_VERSION_SUFFIX "dev" 38 | 39 | #define UV_VERSION_HEX ((UV_VERSION_MAJOR << 16) | \ 40 | (UV_VERSION_MINOR << 8) | \ 41 | (UV_VERSION_PATCH)) 42 | 43 | #endif /* UV_VERSION_H */ 44 | -------------------------------------------------------------------------------- /xbuild/include/winpcap/pcap/bluetooth.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Paolo Abeni (Italy) 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 3. The name of the author may not be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | * bluetooth data struct 31 | * By Paolo Abeni 32 | * 33 | * @(#) $Header: /tcpdump/master/libpcap/pcap/bluetooth.h,v 1.1 2007/09/22 02:10:17 guy Exp $ 34 | */ 35 | 36 | #ifndef _PCAP_BLUETOOTH_STRUCTS_H__ 37 | #define _PCAP_BLUETOOTH_STRUCTS_H__ 38 | 39 | /* 40 | * Header prepended libpcap to each bluetooth h:4 frame. 41 | * fields are in network byte order 42 | */ 43 | typedef struct _pcap_bluetooth_h4_header { 44 | u_int32_t direction; /* if first bit is set direction is incoming */ 45 | } pcap_bluetooth_h4_header; 46 | 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /xbuild/lib/Darwin_x86_64/libdnet.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Darwin_x86_64/libdnet.a -------------------------------------------------------------------------------- /xbuild/lib/Darwin_x86_64/libnet.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Darwin_x86_64/libnet.a -------------------------------------------------------------------------------- /xbuild/lib/Darwin_x86_64/libpcap.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Darwin_x86_64/libpcap.a -------------------------------------------------------------------------------- /xbuild/lib/Darwin_x86_64/libuv.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Darwin_x86_64/libuv.a -------------------------------------------------------------------------------- /xbuild/lib/Linux_x86_64/libdnet.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Linux_x86_64/libdnet.a -------------------------------------------------------------------------------- /xbuild/lib/Linux_x86_64/libnet.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Linux_x86_64/libnet.a -------------------------------------------------------------------------------- /xbuild/lib/Linux_x86_64/libpcap.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Linux_x86_64/libpcap.a -------------------------------------------------------------------------------- /xbuild/lib/Linux_x86_64/libuv.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Linux_x86_64/libuv.a -------------------------------------------------------------------------------- /xbuild/lib/Windows_x86/dnet.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Windows_x86/dnet.dll -------------------------------------------------------------------------------- /xbuild/lib/Windows_x86/dnet.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Windows_x86/dnet.lib -------------------------------------------------------------------------------- /xbuild/lib/Windows_x86/libnet.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Windows_x86/libnet.dll -------------------------------------------------------------------------------- /xbuild/lib/Windows_x86/libuv.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Windows_x86/libuv.dll -------------------------------------------------------------------------------- /xbuild/lib/Windows_x86/net.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Windows_x86/net.lib -------------------------------------------------------------------------------- /xbuild/lib/Windows_x86/packet.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Windows_x86/packet.lib -------------------------------------------------------------------------------- /xbuild/lib/Windows_x86/ucrtbased.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Windows_x86/ucrtbased.dll -------------------------------------------------------------------------------- /xbuild/lib/Windows_x86/uv.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Windows_x86/uv.lib -------------------------------------------------------------------------------- /xbuild/lib/Windows_x86/vcruntime140d.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Windows_x86/vcruntime140d.dll -------------------------------------------------------------------------------- /xbuild/lib/Windows_x86/wpcap.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iceonsun/rsock/9f2f7248df11952a73f1ff6529083b56d76960a9/xbuild/lib/Windows_x86/wpcap.lib -------------------------------------------------------------------------------- /xbuild/sh/base_func.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | function get_os { 4 | local os=$(uname) 5 | 6 | if [[ "${os}" == *Darwin* ]]; then 7 | echo "using MacOs" > /dev/stderr 8 | return 1 9 | elif [[ "${os}" == *Linux* ]]; then 10 | echo "using Linux" > /dev/stderr 11 | return 2 12 | else 13 | echo "Don't know what system you are using." > /dev/stderr 14 | return 0 15 | fi 16 | return 0 17 | 18 | } 19 | 20 | function get_num_core { 21 | local num_core=1 22 | get_os 23 | local os=$? 24 | # get number of cores 25 | if [ ${os} -eq 1 ]; then # mac 26 | num_core=$(sysctl -n hw.ncpu) 27 | elif [ ${os} -eq 2 ]; then # linux 28 | num_core=$(grep -c '^processor' /proc/cpuinfo) 29 | fi 30 | return ${num_core} 31 | } 32 | 33 | function strindex { 34 | x="${1%%$2*}" 35 | [[ "$x" = "$1" ]] && echo -1 || echo "${#x}" 36 | } -------------------------------------------------------------------------------- /xbuild/sh/travis_install.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 4 | source "$DIR/base_func.sh" 5 | if [ $? -ne 0 ]; then 6 | echo "the script can only be called in root directory of project" 7 | return 1 8 | fi 9 | 10 | function build_on_travis { 11 | mkdir build 12 | cd build 13 | pwd 14 | OS=$(uname) 15 | 16 | ARCH=$(uname -m) 17 | if [ $ARCH != "x86_64" ]; then 18 | echo "only x86_64 supported" 19 | fi 20 | 21 | local core=$(get_num_core) 22 | cmake -DCMAKE_TOOLCHAIN_FILE="../xbuild/cmake/travis_${OS}_x86_64.cmake" .. 23 | make "-j$core" 24 | if [ $? -eq 0 ];then 25 | echo "build success" 26 | echo "check library dependency ..." 27 | for f in *_rsock_${OS}; do 28 | if which readelf; then # on travis, linux is statically linked 29 | readelf -d ${f} 30 | readelf -d ${f} |grep '++' 31 | if [ $? -eq 0 ]; then 32 | echo "Linux c++ library dependency not resolved" 33 | echo "build failed" 34 | exit 1 35 | fi 36 | fi 37 | 38 | done 39 | else 40 | echo "build failed" 41 | exit 1 42 | fi 43 | } 44 | 45 | build_on_travis 46 | 47 | 48 | --------------------------------------------------------------------------------