├── .clang-format ├── .gitignore ├── .travis.yml ├── CMakeLists.txt ├── COPYING ├── INSTALL.md ├── README.md ├── default.nix ├── flake.lock ├── flake.nix ├── release.nix ├── res └── bootstrap.json ├── scripts ├── CMakeLists.txt ├── jsoncpp.sh └── libsodium.sh └── src ├── control.cpp ├── control.h ├── epoll_target.h ├── interface.cpp ├── interface.h ├── interface_linux.cpp ├── interface_mac.cpp ├── interface_windows.cpp ├── listener.cpp ├── listener.h ├── main.cpp ├── main.h ├── route.h ├── route_linux.cpp ├── route_mac.cpp ├── route_windows.cpp ├── toxvpn-remote.cpp └── update-bootstrap /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: LLVM 2 | AccessModifierOffset: -4 3 | AlignAfterOpenBracket: true 4 | AlignEscapedNewlinesLeft: false 5 | AlignOperands: true 6 | AlignTrailingComments: true 7 | AllowAllParametersOfDeclarationOnNextLine: false 8 | AllowShortBlocksOnASingleLine: true 9 | AllowShortCaseLabelsOnASingleLine: true 10 | AllowShortFunctionsOnASingleLine: All 11 | AllowShortIfStatementsOnASingleLine: false 12 | AllowShortLoopsOnASingleLine: false 13 | AlwaysBreakAfterDefinitionReturnType: false 14 | AlwaysBreakBeforeMultilineStrings: false 15 | AlwaysBreakTemplateDeclarations: true 16 | BinPackArguments: true 17 | BinPackParameters: false 18 | BreakBeforeBinaryOperators: None 19 | BreakBeforeBraces: Attach 20 | BreakBeforeTernaryOperators: true 21 | BreakConstructorInitializersBeforeComma: false 22 | ColumnLimit: 80 23 | CommentPragmas: '^ IWYU pragma:' 24 | ConstructorInitializerAllOnOneLineOrOnePerLine: false 25 | ConstructorInitializerIndentWidth: 4 26 | ContinuationIndentWidth: 4 27 | Cpp11BracedListStyle: true 28 | DerivePointerAlignment: false 29 | DisableFormat: false 30 | ExperimentalAutoDetectBinPacking: false 31 | IndentCaseLabels: false 32 | IndentWidth: 4 33 | IndentWrappedFunctionNames: false 34 | KeepEmptyLinesAtTheStartOfBlocks: false 35 | Language: Cpp 36 | MaxEmptyLinesToKeep: 1 37 | PenaltyBreakBeforeFirstCallParameter: 19 38 | PenaltyBreakComment: 300 39 | PenaltyBreakFirstLessLess: 120 40 | PenaltyBreakString: 1000 41 | PenaltyExcessCharacter: 1000000 42 | PenaltyReturnTypeOnItsOwnLine: 60 43 | PointerAlignment: Left 44 | SpaceAfterCStyleCast: true 45 | SpaceBeforeAssignmentOperators: true 46 | SpaceBeforeParens: Never 47 | SpaceInEmptyParentheses: false 48 | SpacesBeforeTrailingComments: 1 49 | SpacesInAngles: false 50 | SpacesInCStyleCastParentheses: false 51 | SpacesInContainerLiterals: true 52 | SpacesInParentheses: false 53 | SpacesInSquareBrackets: false 54 | Standard: Cpp11 55 | TabWidth: 4 56 | UseTab: Never 57 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: c 3 | sudo: false 4 | compiler: 5 | - gcc 6 | os: 7 | - linux 8 | - osx 9 | 10 | addons: 11 | apt: 12 | packages: 13 | - libjsoncpp-dev 14 | - libcap-dev 15 | cache: 16 | directories: 17 | - $HOME/libsodium 18 | 19 | before_script: 20 | # installing libsodium, needed for Core 21 | - ./scripts/libsodium.sh 22 | - ./scripts/jsoncpp.sh 23 | # creating libraries links and updating cache 24 | # - sudo ldconfig > /dev/null 25 | # and toxcore 26 | - git clone git://github.com/TokTok/toxcore.git > /dev/null 27 | - cd toxcore 28 | - autoreconf -i 29 | - CFLAGS="-Ofast -Wall -Wextra" ./configure --enable-daemon --enable-ntox --with-libsodium-headers=${HOME}/libsodium/include/ --with-libsodium-libs=${HOME}/libsodium/lib/ --prefix=${HOME}/toxcore/ 30 | - make -j3 31 | - make check 32 | - make install 33 | - cd .. 34 | 35 | script: 36 | - mkdir build 37 | - cd build 38 | - cmake ../ -DSTATIC=1 -DTOX_PREFIX=${HOME}/toxcore/ -DSODIUM_PREFIX=${HOME}/libsodium/ 39 | - make 40 | 41 | notification: 42 | email: false 43 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | project(toxvpn) 3 | 4 | set(BOOTSTRAP_PATH "${CMAKE_INSTALL_PREFIX}/share/toxvpn/bootstrap.json") 5 | 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBOOTSTRAP_FILE=\\\"${BOOTSTRAP_PATH}\\\"") 7 | 8 | if(WIN32) 9 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DWIN32") 10 | else(WIN32) 11 | endif(WIN32) 12 | 13 | if(STATIC) 14 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSTATIC") 15 | set(LIBMODE STATIC) 16 | endif(STATIC) 17 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wconversion -Wshadow -Wextra -std=c++14") 18 | 19 | if(SYSTEMD) 20 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSYSTEMD") 21 | set(SYSTEMD_LIBRARIES "systemd") 22 | endif(SYSTEMD) 23 | 24 | if(ZMQ) 25 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DZMQ") 26 | set(ZMQ_LIBS "zmq") 27 | endif(ZMQ) 28 | 29 | find_package(nlohmann_json) 30 | 31 | find_library(TOXCORE_LIBRARIES toxcore REQUIRED HINTS "${TOX_PREFIX}/lib") 32 | find_path(TOXCORE_INCLUDE_DIRS tox/tox.h REQUIRED HINTS "${TOX_PREFIX}/include") 33 | 34 | find_library(SODIUM_LIBRARIES sodium REQUIRED "${SODIUM_PREFIX}/lib") 35 | find_path(SODIUM_INCLUDE_DIRS sodium.h REQUIRED "${SODIUM_PREFIX}/include") 36 | 37 | if(WIN32) 38 | set(extra_files src/interface_windows.cpp src/route_windows.cpp) 39 | elseif(CYGWIN) 40 | set(extra_files src/interface_windows.cpp src/route_windows.cpp) 41 | elseif(APPLE) 42 | set(extra_files src/interface_mac.cpp src/route_mac.cpp) 43 | else() 44 | set(extra_files src/interface_linux.cpp src/route_linux.cpp) 45 | endif() 46 | 47 | add_executable(toxvpn src/main.cpp src/control.cpp src/interface.cpp src/listener.cpp ${extra_files}) 48 | target_link_libraries(toxvpn pthread ${JSONCPP_LIBRARIES} ${TOXCORE_LIBRARIES}) 49 | include_directories(${JSONCPP_INCLUDE_DIRS} ${TOXCORE_INCLUDE_DIRS}) 50 | 51 | add_executable(toxvpn-remote src/toxvpn-remote.cpp) 52 | target_link_libraries(toxvpn-remote ${ZMQ_LIBS}) 53 | 54 | if(WIN32) 55 | target_link_libraries(toxvpn ${TOXCORE_LIBRARIES} ws2_32 ${SODIUM_LIBRARIES} pthread iphlpapi ${JSONCPP_LIBRARIES}) 56 | elseif(CYGWIN) 57 | target_link_libraries(toxvpn ${TOXCORE_LIBRARIES} ws2_32 ${SODIUM_LIBRARIES} pthread iphlpapi) 58 | elseif(APPLE) 59 | target_link_libraries(toxvpn ${ZMQ_LIBS}) 60 | else() 61 | if(STATIC) 62 | target_link_libraries(toxvpn cap ${ZMQ_LIBS} pthread ${SODIUM_LIBRARIES}) 63 | else(STATIC) 64 | target_link_libraries(toxvpn cap ${ZMQ_LIBS} ${SYSTEMD_LIBRARIES}) 65 | endif(STATIC) 66 | endif() 67 | 68 | install(TARGETS toxvpn toxvpn-remote DESTINATION bin) 69 | install(FILES res/bootstrap.json DESTINATION share/toxvpn) 70 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /INSTALL.md: -------------------------------------------------------------------------------- 1 | # Install instructions 2 | 3 | ## Dependencies 4 | 5 | | Name | Version | 6 | |-----------|----------| 7 | | CMake | >= 2.6 | 8 | | JsonCpp | >= 0.5.0 | 9 | | GCC | >= 4.7 | 10 | | toxcore | latest | 11 | 12 | ## Linux 13 | 14 | ### Simple install 15 | 16 | #### Gentoo 17 | If you are using Gentoo, there is ebuild available in [Tox Gentoo overlay](https://github.com/Tox/gentoo-overlay-tox). 18 | 19 | If you don't run Gentoo, you can always compile manually. 20 | 21 | ### Compiling manually 22 | 23 | Make sure to have dependencies installed. 24 | 25 | After you install dependencies, run ``cmake`` to generate config: 26 | ``` 27 | $ cmake . 28 | ``` 29 | 30 | Compile: 31 | ``` 32 | $ make 33 | ``` 34 | 35 | Now you have **toxvpn** compiled. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | toxvpn 2 | ====== 3 | 4 | [![Build Status](https://travis-ci.org/cleverca22/toxvpn.svg?branch=master)](https://travis-ci.org/cleverca22/toxvpn) 5 | 6 | **toxvpn** is a powerful tool that allows one to make tunneled point to point connections over [Tox](https://github.com/irungentoo/toxcore). 7 | 8 | Using Tox for transport allows fast, efficient and reliable encrypted tunneling. 9 | 10 | Currently only Linux has full support. 11 | 12 | 13 | ## Documentation: 14 | * [Installation](INSTALL.md) 15 | 16 | 17 | To run **toxvpn** after you compile / install it, you will need to load ``tun`` module: 18 | ``` 19 | # modprobe tun 20 | ``` 21 | 22 | After that, you can run **toxvpn**: 23 | ``` 24 | # ./toxvpn -i 192.168.127.1 25 | ``` 26 | 27 | After that type ``help`` to get list of commands. 28 | 29 | 30 | Note that **toxvpn** instances that connect to each other need to have different IPs in order to work properly. 31 | 32 | 33 | ## License 34 | **toxvpn** is licensed under GPLv3. For details, look in [COPYING](COPYING). 35 | -------------------------------------------------------------------------------- /default.nix: -------------------------------------------------------------------------------- 1 | { stdenv, clangStdenv, lib, fetchFromGitHub 2 | , cmake, libsodium, systemd, nlohmann_json, libtoxcore, libcap, zeromq 3 | }: 4 | 5 | with rec { 6 | enableDebugging = true; 7 | 8 | libtoxcoreLocked = (libtoxcore.override { libconfig = null; }).overrideAttrs(old: { 9 | name = "libtoxcore-20250101"; 10 | 11 | src = fetchFromGitHub { 12 | owner = "cleverca22"; 13 | repo = "toxcore"; 14 | rev = "e5a5c75eb889be932d6c14f3edcfaf2077fba231"; 15 | hash = "sha256-WLHRW+2Phxv1U3qxb9lQSJhGQ/573O+QDkTPUyjivnc="; 16 | fetchSubmodules = true; 17 | }; 18 | 19 | dontStrip = enableDebugging; 20 | cmakeFlags = [ 21 | "-DDHT_BOOTSTRAP=ON" 22 | "-DBOOTSTRAP_DAEMON=OFF" 23 | "-DENABLE_SHARED=ON" 24 | "-DENABLE_STATIC=ON" 25 | ]; 26 | }); 27 | 28 | 29 | systemdOrNull = if stdenv.system == "x86_64-darwin" then null else systemd; 30 | 31 | if_systemd = lib.optional (systemdOrNull != null); 32 | }; 33 | 34 | stdenv.mkDerivation { 35 | name = "toxvpn-git"; 36 | 37 | src = ./.; 38 | 39 | dontStrip = enableDebugging; 40 | 41 | NIX_CFLAGS_COMPILE = if enableDebugging then [ "-ggdb -Og" ] else []; 42 | 43 | buildInputs = lib.concatLists [ 44 | [ cmake libtoxcoreLocked nlohmann_json libsodium ] 45 | (if_systemd systemd) 46 | (lib.optional (stdenv.system != "x86_64-darwin") libcap) 47 | (lib.optional (zeromq != null) zeromq) 48 | ]; 49 | 50 | cmakeFlags = (if_systemd [ "-DSYSTEMD=1" ]) ++ (lib.optional (zeromq != null) "-DZMQ=1"); 51 | 52 | meta = with lib; { 53 | description = "A tool for making tunneled connections over Tox"; 54 | homepage = "https://github.com/cleverca22/toxvpn"; 55 | license = licenses.gpl3; 56 | maintainers = with maintainers; [ cleverca22 obadz ]; 57 | platforms = platforms.linux ++ platforms.darwin; 58 | }; 59 | } 60 | -------------------------------------------------------------------------------- /flake.lock: -------------------------------------------------------------------------------- 1 | { 2 | "nodes": { 3 | "nixpkgs": { 4 | "locked": { 5 | "lastModified": 1736042175, 6 | "narHash": "sha256-jdd5UWtLVrNEW8K6u5sy5upNAFmF3S4Y+OIeToqJ1X8=", 7 | "owner": "NixOS", 8 | "repo": "nixpkgs", 9 | "rev": "bf689c40d035239a489de5997a4da5352434632e", 10 | "type": "github" 11 | }, 12 | "original": { 13 | "id": "nixpkgs", 14 | "type": "indirect" 15 | } 16 | }, 17 | "root": { 18 | "inputs": { 19 | "nixpkgs": "nixpkgs" 20 | } 21 | } 22 | }, 23 | "root": "root", 24 | "version": 7 25 | } 26 | -------------------------------------------------------------------------------- /flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | outputs = { self, nixpkgs }: 3 | let 4 | pkgs = nixpkgs.legacyPackages.x86_64-linux; 5 | in 6 | { 7 | packages.x86_64-linux.default = pkgs.callPackage ./. {}; 8 | }; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /release.nix: -------------------------------------------------------------------------------- 1 | { nixpkgs ? }: 2 | 3 | let 4 | pkgsFromSystem = system: (import nixpkgs { config = {}; inherit system; }); 5 | makeJob = (s: { ${s} = (pkgsFromSystem s).callPackage ./default.nix {}; }); 6 | nativePkgs = import nixpkgs {}; 7 | merge = a: b: a // b; 8 | mergeList = builtins.foldl' merge {}; 9 | makeJobs = systems: mergeList (map makeJob systems); 10 | makeRPM = system: diskImageFun: extraPackages: with import nixpkgs { inherit system; }; 11 | releaseTools.rpmBuild rec { 12 | name = "toxvpn-rpm"; 13 | src = ./.; 14 | diskImage = (diskImageFun vmTools.diskImageFuns) { inherit extraPackages; }; 15 | memSize = 1024; 16 | }; 17 | in { toxvpn = makeJobs [ "x86_64-linux" /*"x86_64-darwin"*/ ]; } 18 | -------------------------------------------------------------------------------- /res/bootstrap.json: -------------------------------------------------------------------------------- 1 | {"last_scan":1724191272,"last_refresh":1724191153,"nodes":[{"ipv4":"144.217.167.73","ipv6":"-","port":33445,"tcp_ports":[33445,3389],"public_key":"7E5668E0EE09E19F320AD47902419331FFEE147BB3606769CFBE921A2A2FD34C","maintainer":"velusip","location":"CA","status_udp":true,"status_tcp":true,"version":"1000002019","motd":"Jera","last_ping":1724191272},{"ipv4":"tox.abilinski.com","ipv6":"-","port":33445,"tcp_ports":[33445],"public_key":"10C00EB250C3233E343E2AEBA07115A5C28920E9C8D29492F6D00B29049EDC7E","maintainer":"AnthonyBilinski","location":"CA","status_udp":true,"status_tcp":true,"version":"1000002019","motd":"Running https://github.com/toktok/c-toxcore v0.2.13. qTox best Tox! Contact: AC18841E56CCDEE16E93E10E6AB2765BE54277D67F1372921B5B418A6B330D3D3FAFA60B0931","last_ping":1724191272},{"ipv4":"tox.kurnevsky.net","ipv6":"tox.kurnevsky.net","port":33445,"tcp_ports":[],"public_key":"82EF82BA33445A1F91A7DB27189ECFC0C013E06E3DA71F588ED692BED625EC23","maintainer":"kurnevsky","location":"NL","status_udp":true,"status_tcp":false,"version":"3000002000","motd":"Hi from tox-rs!","last_ping":1724191272},{"ipv4":"205.185.115.131","ipv6":"-","port":53,"tcp_ports":[53,443,33445,3389],"public_key":"3091C6BEB2A993F1C6300C16549FABA67098FF3D62C6D253828B531470B53D68","maintainer":"GDR!","location":"US","status_udp":true,"status_tcp":true,"version":"1000002018","motd":"https://gdr.name/tuntox/","last_ping":1724191272},{"ipv4":"tox2.abilinski.com","ipv6":"tox2.abilinski.com","port":33445,"tcp_ports":[33445],"public_key":"7A6098B590BDC73F9723FC59F82B3F9085A64D1B213AAF8E610FD351930D052D","maintainer":"AnthonyBilinski","location":"US","status_udp":true,"status_tcp":true,"version":"1000002019","motd":"Running https://github.com/toktok/c-toxcore v0.2.13. qTox best Tox! Contact: AC18841E56CCDEE16E93E10E6AB2765BE54277D67F1372921B5B418A6B330D3D3FAFA60B0931","last_ping":1724191272},{"ipv4":"tox1.mf-net.eu","ipv6":"tox1.mf-net.eu","port":33445,"tcp_ports":[33445,3389],"public_key":"B3E5FA80DC8EBD1149AD2AB35ED8B85BD546DEDE261CA593234C619249419506","maintainer":"2mf","location":"DE","status_udp":true,"status_tcp":true,"version":"1000002019","motd":"tox-bootstrapd","last_ping":1724191274},{"ipv4":"tox4.plastiras.org","ipv6":"-","port":33445,"tcp_ports":[3389,443,33445],"public_key":"836D1DA2BE12FE0E669334E437BE3FB02806F1528C2B2782113E0910C7711409","maintainer":"Tha_14","location":"MD","status_udp":true,"status_tcp":true,"version":"1000002019","motd":"Add me on Tox: F0AA7C8C55552E8593B2B77AC6FCA598A40D1F5F52A26C2322690A4BF1DFCB0DD8AEDD2822FF","last_ping":1724191272},{"ipv4":"188.225.9.167","ipv6":"209:dead:ded:4991:49f3:b6c0:9869:3019","port":33445,"tcp_ports":[33445,3389],"public_key":"1911341A83E02503AB1FD6561BD64AF3A9D6C3F12B5FBB656976B2E678644A67","maintainer":"Nikat","location":"RU","status_udp":true,"status_tcp":true,"version":"1000002013","motd":"First yggdrasil tox bootstrapd!!!\nYou can read about it here: https://yggdrasil-network.github.io/","last_ping":1724191273},{"ipv4":"3.0.24.15","ipv6":"-","port":33445,"tcp_ports":[33445],"public_key":"E20ABCF38CDBFFD7D04B29C956B33F7B27A3BB7AF0618101617B036E4AEA402D","maintainer":"Hardy","location":"SG","status_udp":true,"status_tcp":true,"version":"1000002013","motd":"tox-bootstrapd","last_ping":1724191274},{"ipv4":"tox3.plastiras.org","ipv6":"tox3.plastiras.org","port":33445,"tcp_ports":[33445],"public_key":"4B031C96673B6FF123269FF18F2847E1909A8A04642BBECD0189AC8AEEADAF64","maintainer":"Tha_14","location":"DE","status_udp":true,"status_tcp":true,"version":"1000002019","motd":"Add me on Tox: F0AA7C8C55552E8593B2B77AC6FCA598A40D1F5F52A26C2322690A4BF1DFCB0DD8AEDD2822FF","last_ping":1724191274},{"ipv4":"104.225.141.59","ipv6":"-","port":43334,"tcp_ports":[33445,3389],"public_key":"933BA20B2E258B4C0D475B6DECE90C7E827FE83EFA9655414E7841251B19A72C","maintainer":"Gabe","location":"US","status_udp":true,"status_tcp":true,"version":"1000002018","motd":"True peace is in Jesus Matt 11:28-30 Tox ID: CD9E37503A5B2DFB41947B9A0E4B921381340B49FC318FEB07250789C715DA3470885905869F matt2446.us","last_ping":1724191274},{"ipv4":"139.162.110.188","ipv6":"2400:8902::f03c:93ff:fe69:bf77","port":33445,"tcp_ports":[33445,3389,443],"public_key":"F76A11284547163889DDC89A7738CF271797BF5E5E220643E97AD3C7E7903D55","maintainer":"ToxTom","location":"CA","status_udp":true,"status_tcp":true,"version":"1000002013","motd":"ToxTom","last_ping":1724191272},{"ipv4":"tox2.mf-net.eu","ipv6":"tox2.mf-net.eu","port":33445,"tcp_ports":[3389,33445],"public_key":"70EA214FDE161E7432530605213F18F7427DC773E276B3E317A07531F548545F","maintainer":"2mf","location":"DE","status_udp":true,"status_tcp":true,"version":"1000002019","motd":"tox-bootstrapd","last_ping":1724191274},{"ipv4":"172.105.109.31","ipv6":"2600:3c04::f03c:92ff:fe30:5df","port":33445,"tcp_ports":[33445],"public_key":"D46E97CF995DC1820B92B7D899E152A217D36ABE22730FEA4B6BF1BFC06C617C","maintainer":"amr","location":"CA","status_udp":true,"status_tcp":true,"version":"1000002019","motd":"FrozenDev Node: tox-bootstrapd Add me on tox: A625D9E9EAAA7B40C399F50BA8B255836EE5A09B6DD0C54CF0E190E24544DC39237D6389FAED","last_ping":1724191274},{"ipv4":"91.146.66.26","ipv6":"-","port":33445,"tcp_ports":[],"public_key":"B5E7DAC610DBDE55F359C7F8690B294C8E4FCEC4385DE9525DBFA5523EAD9D53","maintainer":"Toxdaemon","location":"EE","status_udp":true,"status_tcp":false,"version":"1000002013","motd":"tox-bootstrapd 91.146.66.26","last_ping":1724191272},{"ipv4":"tox2.plastiras.org","ipv6":"tox2.plastiras.org","port":33445,"tcp_ports":[33445,3389],"public_key":"B6626D386BE7E3ACA107B46F48A5C4D522D29281750D44A0CBA6A2721E79C951","maintainer":"Tha_14","location":"DE","status_udp":true,"status_tcp":true,"version":"1000002019","motd":"Add me on Tox: F0AA7C8C55552E8593B2B77AC6FCA598A40D1F5F52A26C2322690A4BF1DFCB0DD8AEDD2822FF","last_ping":1724191272},{"ipv4":"172.104.215.182","ipv6":"2600:3c03::f03c:93ff:fe7f:6096","port":33445,"tcp_ports":[33445,3389,443],"public_key":"DA2BD927E01CD05EBCC2574EBE5BEBB10FF59AE0B2105A7D1E2B40E49BB20239","maintainer":"zero-one","location":"US","status_udp":true,"status_tcp":true,"version":"1000002018","motd":"tox-bootstrapd","last_ping":1724191272},{"ipv4":"tox.initramfs.io","ipv6":"tox.initramfs.io","port":33445,"tcp_ports":[3389,33445],"public_key":"3F0A45A268367C1BEA652F258C85F4A66DA76BCAA667A49E770BCC4917AB6A25","maintainer":"initramfs","location":"TW","status_udp":true,"status_tcp":true,"version":"1000002018","motd":"initramfs' tox bootstrap node","last_ping":1724191272},{"ipv4":"tox.plastiras.org","ipv6":"tox.plastiras.org","port":33445,"tcp_ports":[443,33445],"public_key":"8E8B63299B3D520FB377FE5100E65E3322F7AE5B20A0ACED2981769FC5B43725","maintainer":"Tha_14","location":"LU","status_udp":true,"status_tcp":true,"version":"1000002018","motd":"Add me on Tox: F0AA7C8C55552E8593B2B77AC6FCA598A40D1F5F52A26C2322690A4BF1DFCB0DD8AEDD2822FF","last_ping":1724191272},{"ipv4":"188.214.122.30","ipv6":"-","port":33445,"tcp_ports":[3389,33445],"public_key":"2A9F7A620581D5D1B09B004624559211C5ED3D1D712E8066ACDB0896A7335705","maintainer":"turambar","location":"EG","status_udp":true,"status_tcp":true,"version":"1000002018","motd":"tox-bootstrapd","last_ping":1724191272},{"ipv4":"62.183.96.32","ipv6":"-","port":33445,"tcp_ports":[33445],"public_key":"52BD37D53357701CB9C69ABA81E7741C5F14105523C89153A770D73F434AC473","maintainer":"Alexsandr","location":"RU","status_udp":true,"status_tcp":true,"version":"1000002018","motd":"New Adugeya tox boostrap node","last_ping":1724191272},{"ipv4":"141.11.229.155","ipv6":"-","port":33445,"tcp_ports":[3389,33445],"public_key":"1FD96DF8DCAC4A95C117B460F23EB740C8FBA60DE89BE7B45136790B8E3D4B63","maintainer":"lzk","location":"US","status_udp":true,"status_tcp":true,"version":"1000002013","motd":"tox-bootstrapd","last_ping":1724191272},{"ipv4":"43.198.227.166","ipv6":"-","port":33445,"tcp_ports":[33445,3389],"public_key":"AD13AB0D434BCE6C83FE2649237183964AE3341D0AFB3BE1694B18505E4E135E","maintainer":"Hardy","location":"CN","status_udp":true,"status_tcp":true,"version":"1000002013","motd":"tox-bootstrapd","last_ping":1724191272},{"ipv4":"95.181.230.108","ipv6":"2a03:c980:db:5d::","port":33445,"tcp_ports":[33445],"public_key":"B5FFECB4E4C26409EBB88DB35793E7B39BFA3BA12AC04C096950CB842E3E130A","maintainer":"wdwp","location":"RU","status_udp":true,"status_tcp":true,"version":"1000002019","motd":"tox-bootstrapd","last_ping":1724191274},{"ipv4":"5.19.249.240","ipv6":"-","port":38296,"tcp_ports":[3389,38296],"public_key":"DA98A4C0CD7473A133E115FEA2EBDAEEA2EF4F79FD69325FC070DA4DE4BA3238","maintainer":"Toxdaemon","location":"RU","status_udp":false,"status_tcp":true,"version":"","motd":"","last_ping":1724191272},{"ipv4":"198.199.98.108","ipv6":"2604:a880:1:20::32f:1001","port":33445,"tcp_ports":[],"public_key":"BEF0CFB37AF874BD17B9A8F9FE64C75521DB95A37D33C5BDB00E9CF58659C04F","maintainer":"Cody","location":"US","status_udp":false,"status_tcp":false,"version":"1000002015","motd":"Cody's Tox node!","last_ping":1692090783},{"ipv4":"46.101.197.175","ipv6":"2a03:b0c0:3:d0::ac:5001","port":33445,"tcp_ports":[],"public_key":"CD133B521159541FB1D326DE9850F5E56A6C724B5B8E5EB5CD8D950408E95707","maintainer":"kotelnik","location":"DE","status_udp":false,"status_tcp":false,"version":"1000002018","motd":"Power to Ukraine!","last_ping":1716531423},{"ipv4":"tox01.ky0uraku.xyz","ipv6":"tox01.ky0uraku.xyz","port":33445,"tcp_ports":[],"public_key":"FD04EB03ABC5FC5266A93D37B4D6D6171C9931176DC68736629552D8EF0DE174","maintainer":"ky0uraku","location":"NL","status_udp":false,"status_tcp":false,"version":"1000002013","motd":"ky0uraku tox01 node","last_ping":1691512685},{"ipv4":"122.116.39.151","ipv6":"2001:b011:8:2f22:1957:7f9d:e31f:96dd","port":33445,"tcp_ports":[],"public_key":"5716530A10D362867C8E87EE1CD5362A233BAFBBA4CF47FA73B7CAD368BD5E6E","maintainer":"miaoski","location":"TW","status_udp":false,"status_tcp":false,"version":"1000002018","motd":"tox-bootstrapd","last_ping":1681799519},{"ipv4":"173.232.195.131","ipv6":"-","port":33445,"tcp_ports":[],"public_key":"3F7D1765E54FADEE08DEDDFECCF8ACF38C52580D4DCA77B30CC3E478F2C50A34","maintainer":"DEADBEEF","location":"PL","status_udp":false,"status_tcp":false,"version":"1000002018","motd":"Maintained by DEADBEEF; ID: AFF6CAA16FFEDE7F458A08D2B19D5DABA6E39A3B26319CC516178DEFBC652154EE393B6C2008","last_ping":1692574743},{"ipv4":"NONE","ipv6":"2607:f130:0:f8::4c85:a645","port":33445,"tcp_ports":[],"public_key":"8AFE1FC6426E5B77AB80318ED64F5F76341695B9FB47AB8AC9537BF5EE9E9D29","maintainer":"Busindre","location":"US","status_udp":false,"status_tcp":false,"version":"","motd":"","last_ping":1718916783},{"ipv4":"198.98.49.206","ipv6":"2605:6400:10:caa:1:be:a:7001","port":33445,"tcp_ports":[],"public_key":"28DB44A3CEEE69146469855DFFE5F54DA567F5D65E03EFB1D38BBAEFF2553255","maintainer":"Cüber","location":"US","status_udp":false,"status_tcp":false,"version":"1000002013","motd":"Tox","last_ping":1685639465},{"ipv4":"tox02.ky0uraku.xyz","ipv6":"tox02.ky0uraku.xyz","port":33445,"tcp_ports":[],"public_key":"D3D6D7C0C7009FC75406B0A49E475996C8C4F8BCE1E6FC5967DE427F8F600527","maintainer":"ky0uraku","location":"FR","status_udp":false,"status_tcp":false,"version":"1000002016","motd":"ky0uraku tox02 node","last_ping":1713682503},{"ipv4":"kusoneko.moe","ipv6":"kusoneko.moe","port":33445,"tcp_ports":[],"public_key":"BE7ED53CD924813507BA711FD40386062E6DC6F790EFA122C78F7CDEEE4B6D1B","maintainer":"Kusoneko","location":"CA","status_udp":false,"status_tcp":false,"version":"1000002018","motd":"Managed by kusoneko (ID:D8E4A5E926A4E7A85FA40F8CA55D47554F043D3C5CDB457187726F19CE20E52C0D7C3FCE9466)","last_ping":1711445823},{"ipv4":"NONE","ipv6":"200:832f:2e56:91a6:678e:aaaf:80bf:4a8a","port":33445,"tcp_ports":[],"public_key":"444361B1717AD5E10D9C03EA1C714A846C9D3B16A875186D0034DC516A49F013","maintainer":"Dima(Yggdrasil)","location":"RU","status_udp":false,"status_tcp":false,"version":"","motd":"","last_ping":0},{"ipv4":"193.168.141.224","ipv6":"-","port":33445,"tcp_ports":[],"public_key":"8E82CF0D7CC42B63748C01DD61EAA490BC35DBDB177942D423DC96D40762C01D","maintainer":"DEADBEEF","location":"RO","status_udp":false,"status_tcp":false,"version":"1000002018","motd":"Maintained by DEADBEEF; ID: AFF6CAA16FFEDE7F458A08D2B19D5DABA6E39A3B26319CC516178DEFBC652154EE393B6C2008","last_ping":1689605883},{"ipv4":"194.36.190.71","ipv6":"-","port":33445,"tcp_ports":[],"public_key":"99E8460035E45C0A6B6DC2C02B14440F7F876518E9D054D028209B5669827645","maintainer":"UR1229SWL","location":"NL","status_udp":false,"status_tcp":false,"version":"3000002000","motd":"Welcome to https://rx-tx.info and t.me/rx_tx_info","last_ping":1722501794}]} -------------------------------------------------------------------------------- /scripts/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 2.6) 2 | project (toxcore) 3 | 4 | find_library(SODIUM_LIBRARIES sodium REQUIRED "${SODIUM_PREFIX}/lib") 5 | find_path(SODIUM_INCLUDE_DIRS sodium.h REQUIRED "${SODIUM_PREFIX}/include") 6 | 7 | include_directories(${SODIUM_INCLUDE_DIRS}) 8 | add_library(toxcore SHARED toxcore/tox.c toxcore/Messenger.c toxcore/group.c 9 | toxcore/crypto_core.c toxcore/friend_requests.c toxcore/logger.c 10 | toxcore/DHT.c toxcore/network.c toxcore/net_crypto.c toxcore/TCP_server.c 11 | toxcore/onion.c toxcore/onion_client.c toxcore/util.c toxcore/friend_connection.c 12 | toxcore/onion_announce.c toxcore/LAN_discovery.c toxcore/ping.c toxcore/ping_array.c 13 | toxcore/list.c toxcore/TCP_connection.c toxcore/TCP_client.c) 14 | target_link_libraries(toxcore ${SODIUM_LIBRARIES} ws2_32 iphlpapi) 15 | -------------------------------------------------------------------------------- /scripts/jsoncpp.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ $TRAVIS_OS_NAME == osx ]; then 4 | brew tap cuber/homebrew-jsoncpp 5 | brew unlink json-c 6 | brew install jsoncpp 7 | fi 8 | -------------------------------------------------------------------------------- /scripts/libsodium.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | if [ ! -d "$HOME/libsodium/lib" ]; then 3 | mkdir build 4 | pushd build 5 | git clone git://github.com/jedisct1/libsodium.git > /dev/null 6 | cd libsodium 7 | git checkout tags/1.0.0 > /dev/null 8 | ./autogen.sh > /dev/null 9 | ./configure --prefix=${HOME}/libsodium/ 10 | make check -j3 > /dev/null 11 | make install 12 | popd 13 | rm -rf build 14 | else 15 | echo 'Using cached directory.'; 16 | fi 17 | -------------------------------------------------------------------------------- /src/control.cpp: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include "control.h" 3 | 4 | using namespace std; 5 | using namespace ToxVPN; 6 | 7 | Control::Control(NetworkInterface* iface) : interfarce(iface) { 8 | this->handle = STDIN_FILENO; 9 | input = stdin; 10 | output = stdout; 11 | #ifdef USE_EPOLL 12 | memset(&this->event, 0, sizeof(this->event)); 13 | this->event.events = EPOLLIN | EPOLLPRI | EPOLLERR; 14 | this->event.data.ptr = this; 15 | if(epoll_ctl(epoll_handle, EPOLL_CTL_ADD, this->handle, &this->event) != 0) 16 | puts(strerror(errno)); 17 | #endif 18 | } 19 | 20 | Control::Control(NetworkInterface* iface, int socket) : interfarce(iface) { 21 | this->handle = socket; 22 | input = fdopen(handle, "r"); 23 | output = fdopen(handle, "w"); 24 | } 25 | 26 | ssize_t Control::handleReadData(Tox* tox, ToxVPNCore* toxvpn) { 27 | ssize_t size; 28 | #ifdef WIN32 29 | std::string cmd; 30 | getline(cin, cmd); 31 | size = cmd.length(); 32 | #else 33 | char* line = nullptr; 34 | size_t linelen = 0; 35 | size = getline(&line, &linelen, input); 36 | if(size == -1) 37 | return -1; 38 | std::string cmd(line, size); 39 | #endif 40 | std::string buf; 41 | std::stringstream ss(cmd); 42 | ss >> buf; 43 | Tox_Err_Friend_Query fqerror; 44 | if(buf == "list") { 45 | fputs("listing friends\n", output); 46 | size_t friendCount = tox_self_get_friend_list_size(tox); 47 | uint32_t* friends = new uint32_t[friendCount]; 48 | tox_self_get_friend_list(tox, friends); 49 | for(unsigned int i = 0; i < friendCount; i++) { 50 | int friendid = friends[i]; 51 | Tox_Connection conn_status = 52 | tox_friend_get_connection_status(tox, friendid, nullptr); 53 | string statusString; 54 | switch(conn_status) { 55 | case TOX_CONNECTION_NONE: statusString = "offline"; break; 56 | case TOX_CONNECTION_TCP: statusString = "tcp"; break; 57 | case TOX_CONNECTION_UDP: statusString = "udp"; break; 58 | } 59 | uint64_t lastonline = 60 | tox_friend_get_last_online(tox, friendid, nullptr); 61 | size_t namesize = tox_friend_get_name_size(tox, friendid, &fqerror); 62 | uint8_t* friendname = new uint8_t[namesize + 1]; 63 | tox_friend_get_name(tox, friendid, friendname, nullptr); 64 | friendname[namesize] = 0; 65 | size_t statusSize = 66 | tox_friend_get_status_message_size(tox, friendid, nullptr); 67 | uint8_t* status = new uint8_t[statusSize + 1]; 68 | tox_friend_get_status_message(tox, friendid, status, nullptr); 69 | status[statusSize] = 0; 70 | time_t t = lastonline; 71 | char *last_online_str = ctime(&t); 72 | last_online_str[strlen(last_online_str)-1] = 0; 73 | fprintf(output, 74 | "friend#%2d name:%15s status:%10s %30s lastonline: %s\n", 75 | friendid, friendname, statusString.c_str(), status, 76 | last_online_str); 77 | delete[] friendname; 78 | delete[] status; 79 | } 80 | delete[] friends; 81 | } else if(buf == "remove") { 82 | int friendid; 83 | ss >> friendid; 84 | fprintf(output, "going to kick %d\n", friendid); 85 | tox_friend_delete(tox, friendid, nullptr); 86 | interfarce->removePeer(friendid); 87 | } else if(buf == "add") { 88 | ss >> buf; 89 | fprintf(output, "going to connect to %s\n", buf.c_str()); 90 | const char* msg = "toxvpn"; 91 | uint8_t peerbinary[TOX_ADDRESS_SIZE]; 92 | Tox_Err_Friend_Add error; 93 | hex_string_to_bin(buf.c_str(), peerbinary); 94 | tox_friend_add(tox, (const uint8_t*) peerbinary, (const uint8_t*) msg, strlen(msg), 95 | &error); 96 | switch(error) { 97 | case TOX_ERR_FRIEND_ADD_OK: saveState(tox); break; 98 | case TOX_ERR_FRIEND_ADD_ALREADY_SENT: 99 | fputs("already sent\n", output); 100 | break; 101 | case TOX_ERR_FRIEND_ADD_BAD_CHECKSUM: puts("crc error"); break; 102 | default: fprintf(output, "err code %d\n", error); 103 | } 104 | } else if(buf == "whitelist") { 105 | ss >> buf; 106 | uint8_t peerbinary[TOX_PUBLIC_KEY_SIZE]; 107 | Tox_Err_Friend_Add error; 108 | hex_string_to_bin(buf.c_str(), peerbinary); 109 | tox_friend_add_norequest(tox, peerbinary, &error); 110 | switch(error) { 111 | case TOX_ERR_FRIEND_ADD_OK: break; 112 | case TOX_ERR_FRIEND_ADD_ALREADY_SENT: 113 | fputs("already sent\n", output); 114 | break; 115 | case TOX_ERR_FRIEND_ADD_BAD_CHECKSUM: 116 | fputs("crc error\n", output); 117 | break; 118 | default: fprintf(output, "err code %d\n", error); 119 | } 120 | saveState(tox); 121 | } else if(buf == "status") { 122 | uint8_t toxid[TOX_ADDRESS_SIZE]; 123 | tox_self_get_address(tox, toxid); 124 | char tox_printable_id[TOX_ADDRESS_SIZE * 2 + 1]; 125 | memset(tox_printable_id, 0, sizeof(tox_printable_id)); 126 | to_hex(tox_printable_id, toxid, TOX_ADDRESS_SIZE); 127 | fprintf(output, "my id is %s and IP is %s\n", tox_printable_id, 128 | myip.c_str()); 129 | } else if(buf == "help") { 130 | fputs("list - lists tox friends\n", output); 131 | fputs( 132 | "remove - removes a friend, get the number from list\n", 133 | output); 134 | fputs("add - adds a friend\n", output); 135 | fputs("whitelist - add/accept a friend\n", output); 136 | fputs("status - shows your own id&ip\n", output); 137 | fputs("bootstrap - attempt to reconnect\n", output); 138 | } else if(buf == "bootstrap") { 139 | do_bootstrap(tox, toxvpn); 140 | } else if(buf == "route") { 141 | ss >> buf; 142 | if(buf == "show") { 143 | std::list::const_iterator i; 144 | for(i = interfarce->routes.begin(); i != interfarce->routes.end(); 145 | ++i) { 146 | Route r = *i; 147 | fprintf(output, "%s/%d via friend#%d\n", inet_ntoa(r.network), 148 | r.maskbits, r.friend_number); 149 | } 150 | } 151 | } 152 | fflush(output); 153 | return size; 154 | } 155 | 156 | int Control::populate_fdset(fd_set* readset) { 157 | FD_SET(this->handle, readset); 158 | return this->handle; 159 | } 160 | -------------------------------------------------------------------------------- /src/control.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "main.h" 4 | #include "interface.h" 5 | 6 | namespace ToxVPN { 7 | 8 | class Control { 9 | public: 10 | Control(NetworkInterface* interfarce); 11 | Control(NetworkInterface* interfarce, int socket); 12 | ssize_t handleReadData(Tox* tox, ToxVPNCore* toxvpn); 13 | int populate_fdset(fd_set* readset); 14 | 15 | int handle; 16 | 17 | private: 18 | NetworkInterface* interfarce; 19 | FILE *input, *output; 20 | }; 21 | } 22 | -------------------------------------------------------------------------------- /src/epoll_target.h: -------------------------------------------------------------------------------- 1 | extern int epoll_handle; 2 | 3 | class EpollTarget { 4 | public: 5 | virtual void handleReadData(Tox* tox) = 0; 6 | #ifdef USE_EPOLL 7 | struct epoll_event event; 8 | #endif 9 | int handle; 10 | }; 11 | -------------------------------------------------------------------------------- /src/interface.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "interface.h" 11 | #include "main.h" 12 | #include "route.h" 13 | 14 | using namespace std; 15 | using namespace ToxVPN; 16 | 17 | typedef struct { 18 | uint16_t hardware_type; 19 | uint16_t protocol_type; 20 | uint8_t hw_size; 21 | uint8_t protocol_size; 22 | uint16_t opcode; 23 | uint8_t src_mac[6]; 24 | struct in_addr src_ip; 25 | uint8_t dst_mac[6]; 26 | struct in_addr dst_ip; 27 | } __attribute__((__packed__)) arp_header; 28 | 29 | typedef struct { 30 | uint8_t dest[6]; 31 | uint8_t src[6]; 32 | uint16_t type; 33 | uint8_t next[0]; 34 | } __attribute__((__packed__)) ethernet_header; 35 | 36 | typedef struct { 37 | struct tun_pi pi; 38 | ethernet_header eth_hdr; 39 | arp_header arp_hdr; 40 | } arp_reply_packet; 41 | 42 | void NetworkInterface::send_arp_reply(const uint8_t *macsrc, struct in_addr src, struct in_addr dst, const uint8_t *pubkey) { 43 | arp_reply_packet pkt; 44 | pkt.pi.flags = 0; 45 | pkt.pi.proto = htons(0x0806); 46 | pubkey_to_mac(pubkey, pkt.eth_hdr.src); 47 | memcpy(pkt.eth_hdr.dest, macsrc, 6); 48 | pkt.eth_hdr.type = htons(0x0806); 49 | pkt.arp_hdr.hardware_type = htons(1); 50 | pkt.arp_hdr.protocol_type = htons(0x800); 51 | pkt.arp_hdr.hw_size = 6; 52 | pkt.arp_hdr.protocol_size = 4; 53 | pkt.arp_hdr.opcode = htons(2); 54 | pubkey_to_mac(pubkey, pkt.arp_hdr.src_mac); 55 | pkt.arp_hdr.src_ip = dst; 56 | memcpy(pkt.arp_hdr.dst_mac, macsrc, 6); 57 | pkt.arp_hdr.dst_ip = src; 58 | send_pi_packet_to_kernel((uint8_t*)&pkt, sizeof(pkt)); 59 | } 60 | 61 | void NetworkInterface::process_arp_request(const uint8_t *macsrc, struct in_addr src, struct in_addr dst) { 62 | Route route; 63 | if (findRoute(&route, dst)) { 64 | if (route.netmode == MODE_TUN) { 65 | // remote peer is in TUN mode, generate an ARP reply locally 66 | send_arp_reply(macsrc, src, dst, route.pubkey); 67 | } else { 68 | fprintf(stderr, "peer isnt in TUN mode\n"); 69 | } 70 | } else { 71 | fprintf(stderr, "no route for arp %s\n", inet_ntoa(dst)); 72 | } 73 | } 74 | 75 | void* NetworkInterface::loop() { 76 | fd_set readset; 77 | struct timeval timeout; 78 | int r; 79 | while(true) { 80 | FD_ZERO(&readset); 81 | FD_SET(fd, &readset); 82 | timeout.tv_sec = 60; 83 | timeout.tv_usec = 0; 84 | r = select(fd + 1, &readset, nullptr, nullptr, &timeout); 85 | if(r > 0) { 86 | if(FD_ISSET(fd, &readset)) 87 | handleReadData(); 88 | } else if(r == 0) { 89 | } else { 90 | printf("select == %d\n", r); 91 | printf("select error fd:%d r:%d errno:%d %s\n", fd, r, errno, 92 | strerror(errno)); 93 | } 94 | } 95 | return nullptr; 96 | } 97 | #ifndef __APPLE__ 98 | static const uint8_t required[] = {0x00, 0x00, 0x08, 0x00, 0x45}; 99 | #endif 100 | void dump_packet(uint8_t* buffer, ssize_t size) { 101 | for(int i = 0; i < size; i++) { 102 | printf("%02x ", buffer[i]); 103 | } 104 | printf("\n"); 105 | } 106 | void NetworkInterface::handleReadData() { 107 | uint8_t readbuffer[1500]; 108 | ssize_t size_ = read(fd, readbuffer, 1500); 109 | if(size_ < 0) { 110 | printf("unable to read from tun %d, %s\n", fd, strerror(errno)); 111 | exit(-2); 112 | return; 113 | } 114 | uint32_t size = (uint32_t)size_; 115 | 116 | if (netmode == MODE_TAP) { 117 | struct tun_pi *pi = (struct tun_pi*)readbuffer; 118 | ethernet_header *eth_header = (ethernet_header*)(readbuffer + 4); 119 | uint8_t *ip_header = ð_header->next[0]; 120 | if (ntohs(pi->proto) == 0x86dd) { // IPv6, TODO 121 | return; 122 | } else if (ntohs(pi->proto) == 0x800) { // IPv4 123 | //printf("flags: 0x%x, proto: 0x%x\n", pi->flags, pi->proto); 124 | //dump_packet(ip_header, size - 4 - 14); 125 | struct in_addr *src = (struct in_addr*) (ip_header + 12); 126 | struct in_addr *dest = (struct in_addr*) (ip_header + 16); 127 | char src_str[16], dst_str[16]; 128 | strncpy(src_str, inet_ntoa(*src), 16); 129 | strncpy(dst_str, inet_ntoa(*dest), 16); 130 | //printf("%ld bytes for %s -> %s\n", size, src_str, dst_str); 131 | if (mac_is_multicast(eth_header->dest)) { 132 | //printf("mcast to %s\n", dst_str); 133 | broadcastPacket(readbuffer, size); 134 | } else { 135 | Route route; 136 | if (findRoute(&route, *dest)) { 137 | forwardPacket(route, readbuffer, size); 138 | } else { 139 | printf("no route found for %s\n", dst_str); 140 | } 141 | } 142 | } else if (ntohs(pi->proto) == 0x0806) { // ARP 143 | //dump_packet(ip_header, size - 4 - 14); 144 | const arp_header *arp = (const arp_header*)ip_header; 145 | if (arp->hw_size != 6) { 146 | fprintf(stderr, "hw size wrong\n"); 147 | return; 148 | } 149 | if (arp->protocol_size != 4) { 150 | fprintf(stderr, "proto size wrong\n"); 151 | return; 152 | } 153 | if (ntohs(arp->hardware_type) != 1) { 154 | fprintf(stderr, "hw type wrong\n"); 155 | return; 156 | } 157 | if (ntohs(arp->protocol_type) != 0x800) { 158 | fprintf(stderr, "protocol type wrong\n"); 159 | return; 160 | } 161 | switch (ntohs(arp->opcode)) { 162 | case 1: // request, what is the mac behind dst_ip 163 | process_arp_request(&arp->src_mac[0], arp->src_ip, arp->dst_ip); 164 | break; 165 | default: 166 | printf("ARP op %d\n", ntohs(arp->opcode)); 167 | } 168 | } else { 169 | printf("UNK flags: 0x%x, proto: 0x%x\n", pi->flags, pi->proto); 170 | } 171 | } else { 172 | struct tun_pi *pi = (struct tun_pi*)readbuffer; 173 | for(unsigned int i = 0; i < sizeof(required); i++) { 174 | if(readbuffer[i] != required[i]) { 175 | puts("unsupported packet, dropping"); 176 | dump_packet(readbuffer, size); 177 | return; 178 | } 179 | } 180 | struct in_addr* dest = (struct in_addr*) (readbuffer + 20); 181 | 182 | //printf("read %d bytes on master interface for %s\n", size, inet_ntoa(*dest)); 183 | //dump_packet(readbuffer,size); 184 | 185 | Route route; 186 | if (findRoute(&route, *dest)) { 187 | struct { 188 | struct tun_pi pi; 189 | ethernet_header eth; 190 | uint8_t rest[1500]; 191 | } newpacket; 192 | newpacket.pi = *pi; 193 | pubkey_to_mac(route.pubkey, newpacket.eth.dest); 194 | memcpy(newpacket.eth.src, mymac, 6); 195 | newpacket.eth.type = newpacket.pi.proto; 196 | memcpy(newpacket.rest, readbuffer+4, size-4); 197 | uint32_t newsize = sizeof(ethernet_header) + size; 198 | forwardPacket(route, (uint8_t*)&newpacket, newsize); 199 | } else { 200 | printf("no route found for %s\n", inet_ntoa(*dest)); 201 | } 202 | } 203 | } 204 | 205 | // gets a packet with PI, eth, ip .... 206 | // TUN based targets want just PI, ip ... 207 | // TAP based targets want the whole packet 208 | // the 200 prefix is tox specific 209 | void NetworkInterface::forwardPacket(Route route, const uint8_t* readbuffer, ssize_t size) { 210 | uint8_t buffer[1600]; 211 | if (route.netmode == MODE_TUN) { 212 | buffer[0] = 200; 213 | memcpy(buffer + 1, readbuffer, sizeof(tun_pi)); 214 | int offset = sizeof(tun_pi) + sizeof(ethernet_header); 215 | size -= offset; 216 | memcpy(buffer + 1 + sizeof(tun_pi), readbuffer + offset, size); 217 | size += sizeof(tun_pi); 218 | } else { 219 | // TODO, sending to TAP 220 | } 221 | Tox_Err_Friend_Custom_Packet error; 222 | tox_friend_send_lossy_packet(my_tox, route.friend_number, buffer, 223 | size + 1, &error); 224 | switch(error) { 225 | case TOX_ERR_FRIEND_CUSTOM_PACKET_OK: break; 226 | case TOX_ERR_FRIEND_CUSTOM_PACKET_FRIEND_NOT_CONNECTED: 227 | cout << size << "byte packet dropped, friend#" << route.friend_number 228 | << "not online" << endl; 229 | break; 230 | case TOX_ERR_FRIEND_CUSTOM_PACKET_SENDQ: 231 | cout << size << "byte packet dropped, sendq for friend#" 232 | << route.friend_number << "full" << endl; 233 | break; 234 | default: cout << "TX error code " << error << endl; 235 | } 236 | } 237 | void NetworkInterface::addPeerRoute(struct in_addr peer, int friend_number, int peer_netmode, uint8_t *pubkey) { 238 | Route x; 239 | x.network = peer; 240 | inet_pton(AF_INET, "255.255.255.255", &x.mask); 241 | x.maskbits = 32; 242 | x.friend_number = friend_number; 243 | x.netmode = peer_netmode; 244 | memcpy(x.pubkey, pubkey, TOX_PUBLIC_KEY_SIZE); 245 | routes.push_back(x); 246 | //systemRouteSingle(interfaceIndex, peer, "10.123.123.123"); 247 | systemRouteDirect(interfaceIndex, peer); 248 | } 249 | void NetworkInterface::setPeerIp(struct in_addr peer, int friend_number, int peer_netmode, uint8_t *pubkey) { 250 | // TODO, flag as online, remove previous ip route 251 | addPeerRoute(peer, friend_number, peer_netmode, pubkey); 252 | } 253 | void NetworkInterface::removePeer(int friend_number) { 254 | // TODO, remove routes in-app and in-kernel 255 | } 256 | bool NetworkInterface::findRoute(Route* route, struct in_addr peer) { 257 | std::list::const_iterator i; 258 | for(i = routes.begin(); i != routes.end(); ++i) { 259 | Route r = *i; 260 | string network1(inet_ntoa(r.network)); 261 | string mask1(inet_ntoa(r.mask)); 262 | uint32_t network = (uint32_t) r.network.s_addr; 263 | uint32_t mask = (uint32_t) r.mask.s_addr; 264 | // printf("test %08x\n",(network & mask)); 265 | // printf("%s %s %d\n",network1.c_str(),mask1.c_str(),r.friend_number); 266 | if((network & mask) == (peer.s_addr & mask)) { 267 | *route = r; 268 | return true; 269 | } 270 | } 271 | return false; 272 | } 273 | 274 | void NetworkInterface::broadcastPacket(const uint8_t* readbuffer, ssize_t size) { 275 | std::list::const_iterator i; 276 | for (i = routes.begin(); i != routes.end(); ++i) { 277 | Route r = *i; 278 | if (r.netmode == MODE_TAP) { 279 | forwardPacket(r, readbuffer, size); 280 | } 281 | } 282 | } 283 | 284 | void NetworkInterface::processPacket(const uint8_t* data, size_t size, int friend_number, int source_mode, const uint8_t *pubkey) { 285 | ssize_t ret = 0; 286 | 287 | friend_number; 288 | 289 | if (fd) { 290 | if (source_mode == MODE_TUN) { 291 | // received packet starts with PI + IP header, insert a ethernet header 292 | struct { 293 | struct tun_pi pi; 294 | ethernet_header eth; 295 | uint8_t rest[1500]; 296 | } __attribute__((__packed__)) newpacket; 297 | newpacket.pi.flags = 0; 298 | newpacket.pi.proto = htons(0x800); 299 | memcpy(newpacket.eth.dest, mymac, 6); 300 | pubkey_to_mac(pubkey, newpacket.eth.src); 301 | newpacket.eth.type = htons(0x800); 302 | memcpy(newpacket.rest, data+4, size-4); 303 | uint32_t newsize = sizeof(struct tun_pi) + sizeof(ethernet_header) + size - 4; 304 | send_pi_packet_to_kernel((uint8_t*)&newpacket, newsize); 305 | } else { 306 | ret = write(fd, data, size); 307 | if ((size_t)ret != size) 308 | cerr << "partial packet write to tun\n"; 309 | } 310 | } 311 | } 312 | 313 | // incoming packet is always in the form of PI+ETH+IP+... 314 | void NetworkInterface::send_pi_packet_to_kernel(const uint8_t* data, uint32_t size) { 315 | if(fd) { 316 | uint8_t newpacket[1600]; 317 | if (netmode == MODE_TUN) { 318 | // need to strip ethernet header 319 | memcpy(newpacket, data, sizeof(struct tun_pi)); 320 | memcpy(newpacket + sizeof(struct tun_pi), data + sizeof(struct tun_pi) + sizeof(ethernet_header), size - sizeof(struct tun_pi) + sizeof(ethernet_header)); 321 | size = size - sizeof(ethernet_header); 322 | data = newpacket; 323 | } 324 | ssize_t ret = write(fd, data, size); 325 | if (ret != size) { 326 | fprintf(stderr, "partial packet write to tun, %d attempted vs %ld successful\n", size, ret); 327 | } 328 | } else { 329 | fprintf(stderr, "tun fd not open\n"); 330 | } 331 | } 332 | 333 | void NetworkInterface::pubkey_to_mac(const uint8_t *pubkey, uint8_t *mac) { 334 | memcpy(mac, pubkey, 6); 335 | mac[0] |= 2; 336 | mac[0] &= 254; 337 | } 338 | -------------------------------------------------------------------------------- /src/interface.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is libre software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * (at your option) any later version. 6 | * This program is distributed in the hope that it will be useful, 7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 9 | * 10 | * See the COPYING file for more details. 11 | */ 12 | #pragma once 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | void dump_packet(uint8_t* buffer, ssize_t size); 19 | 20 | namespace ToxVPN { 21 | 22 | class Route { 23 | public: 24 | struct in_addr network; 25 | struct in_addr mask; 26 | int maskbits; 27 | int friend_number; 28 | int netmode; 29 | uint8_t pubkey[TOX_PUBLIC_KEY_SIZE]; 30 | }; 31 | 32 | class NetworkInterface { 33 | public: 34 | NetworkInterface(); 35 | ~NetworkInterface(); 36 | void* loop(); 37 | void setPeerIp(struct in_addr peer, int friend_number, int peer_netmode, uint8_t *pubkey); 38 | void removePeer(int friend_number); 39 | void addPeerRoute(struct in_addr peer, int friend_number, int peer_netmode, uint8_t *pubkey); 40 | void processPacket(const uint8_t* data, size_t bytes, int friend_number, int source_mode, const uint8_t *pubkey); 41 | void configure(std::string myip, Tox* my_tox); 42 | void send_arp_reply(const uint8_t *macsrc, struct in_addr src, struct in_addr dst, const uint8_t *dstmac); 43 | void process_arp_request(const uint8_t *macsrc, struct in_addr src, struct in_addr dst); 44 | void send_pi_packet_to_kernel(const uint8_t *data, uint32_t size); 45 | static void pubkey_to_mac(const uint8_t *pubkey, uint8_t *mac); 46 | 47 | std::list routes; 48 | 49 | private: 50 | void handleReadData(); 51 | bool findRoute(Route* route, struct in_addr peer); 52 | void forwardPacket(Route route, const uint8_t* buffer, ssize_t bytes); 53 | // accepts a packet in the form of PI + ETH + IP + ..., and sends to all TAP peers 54 | void broadcastPacket(const uint8_t* buffer, ssize_t bytes); 55 | 56 | pthread_t reader; 57 | int fd; 58 | Tox* my_tox; 59 | int interfaceIndex; 60 | uint8_t mymac[6]; 61 | }; 62 | 63 | static inline bool mac_is_multicast(const uint8_t *mac) { 64 | return (mac[0] & 1); 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/interface_linux.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "main.h" 5 | #include "interface.h" 6 | 7 | using namespace std; 8 | using namespace ToxVPN; 9 | 10 | static void* start_routine(void* x) { 11 | NetworkInterface* nic = (NetworkInterface*) x; 12 | return nic->loop(); 13 | } 14 | 15 | NetworkInterface::NetworkInterface() : my_tox(nullptr) { 16 | fd = 0; 17 | if((fd = open("/dev/net/tun", O_RDWR)) < 0) { 18 | cerr << "unable to open /dev/net/tun" << endl; 19 | } 20 | } 21 | 22 | void NetworkInterface::configure(string ip_in, Tox* tox_in) { 23 | int err; 24 | uint8_t pubkey[TOX_PUBLIC_KEY_SIZE]; 25 | struct ifreq ifr; 26 | 27 | my_tox = tox_in; 28 | 29 | memset(&ifr, 0, sizeof(ifr)); 30 | if (netmode == MODE_TAP) { 31 | ifr.ifr_flags = IFF_TAP; 32 | } else { 33 | ifr.ifr_flags = IFF_TUN; 34 | } 35 | strncpy(ifr.ifr_name, "tox_master0", IFNAMSIZ); 36 | 37 | if((err = ioctl(fd, TUNSETIFF, (void*) &ifr)) < 0) { 38 | if(errno == EPERM) { 39 | cerr << "no permission to create tun device" << endl; 40 | exit(-1); 41 | } 42 | if (errno == EINVAL) { 43 | fprintf(stderr, "EINVAL creating network device, is tun/tap in the right mode?\n"); 44 | exit(-1); 45 | } 46 | cerr << strerror(errno) << err << endl; 47 | close(fd); 48 | } 49 | 50 | tox_self_get_public_key(my_tox, pubkey); 51 | 52 | memset(&ifr, 0, sizeof(ifr)); 53 | pubkey_to_mac(pubkey, (uint8_t*)ifr.ifr_hwaddr.sa_data); 54 | pubkey_to_mac(pubkey, mymac); 55 | 56 | strcpy(ifr.ifr_name, "tox_master0"); 57 | 58 | if (netmode == MODE_TAP) { 59 | ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; 60 | if ((err = ioctl(fd, SIOCSIFHWADDR, &ifr)) < 0) { 61 | perror("unable to set mac"); 62 | exit(-1); 63 | } 64 | } 65 | 66 | // and set MTU params 67 | int tun_sock = socket(AF_INET, SOCK_DGRAM, 0); 68 | if(tun_sock < 0) { 69 | printf("error while setting MTU: %s", strerror(errno)); 70 | return; 71 | } 72 | ifr.ifr_mtu = 1200; 73 | err = ioctl(tun_sock, SIOCSIFMTU, &ifr); 74 | if(err) { 75 | perror("error setting mtu"); 76 | } 77 | 78 | printf("setting ip to %s\n", ip_in.c_str()); 79 | struct sockaddr_in address; 80 | address.sin_family = AF_INET; 81 | inet_aton(ip_in.c_str(), &address.sin_addr); 82 | memcpy(&ifr.ifr_addr, &address, sizeof(address)); 83 | err = ioctl(tun_sock, SIOCSIFADDR, &ifr); 84 | if(err) 85 | printf("error %d %s setting ip\n", errno, strerror(errno)); 86 | 87 | inet_aton("10.123.123.123", &address.sin_addr); 88 | memcpy(&ifr.ifr_dstaddr, &address, sizeof(address)); 89 | err = ioctl(tun_sock, SIOCSIFDSTADDR, &ifr); 90 | if(err) 91 | printf("error setting dest ip: %s\n", strerror(errno)); 92 | 93 | ifr.ifr_flags |= IFF_UP | IFF_RUNNING; 94 | ioctl(tun_sock, SIOCSIFFLAGS, &ifr); 95 | 96 | close(tun_sock); 97 | 98 | interfaceIndex = if_nametoindex(ifr.ifr_name); 99 | pthread_attr_t attr; 100 | pthread_attr_init(&attr); 101 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 102 | pthread_create(&reader, &attr, &start_routine, this); 103 | pthread_attr_destroy(&attr); 104 | } 105 | -------------------------------------------------------------------------------- /src/interface_mac.cpp: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include "interface.h" 3 | 4 | using namespace std; 5 | using namespace ToxVPN; 6 | 7 | static void* start_routine(void* x) { 8 | NetworkInterface* nic = (NetworkInterface*) x; 9 | return nic->loop(); 10 | } 11 | NetworkInterface::NetworkInterface() : fd(0), my_tox(0) { 12 | if((fd = open("/dev/tun0", O_RDWR)) < 0) { 13 | cerr << "unable to open /dev/tun0" << endl; 14 | } 15 | } 16 | void NetworkInterface::configure(string myip, Tox* my_tox) { 17 | int err; 18 | struct ifreq ifr; 19 | memset(&ifr, 0, sizeof(ifr)); 20 | strncpy(ifr.ifr_name, "tun0", IFNAMSIZ); 21 | int tun_sock = socket(AF_INET, SOCK_DGRAM, 0); 22 | if(tun_sock < 0) { 23 | printf("error while setting MTU: %s", strerror(errno)); 24 | return; 25 | } 26 | ifr.ifr_mtu = 1200; 27 | err = ioctl(tun_sock, SIOCSIFMTU, &ifr); 28 | if(err) 29 | printf("error %d setting mtu\n", err); 30 | 31 | printf("setting ip to %s\n", myip.c_str()); 32 | struct sockaddr_in address; 33 | address.sin_family = AF_INET; 34 | inet_aton(myip.c_str(), &address.sin_addr); 35 | memcpy(&ifr.ifr_addr, &address, sizeof(address)); 36 | err = ioctl(tun_sock, SIOCSIFADDR, &ifr); 37 | if(err) 38 | printf("error %d %s setting ip\n", errno, strerror(errno)); 39 | 40 | inet_aton("10.123.123.123", &address.sin_addr); 41 | memcpy(&ifr.ifr_dstaddr, &address, sizeof(address)); 42 | err = ioctl(tun_sock, SIOCSIFDSTADDR, &ifr); 43 | if(err) 44 | printf("error setting dest ip: %s\n", strerror(errno)); 45 | 46 | ifr.ifr_flags |= IFF_UP | IFF_RUNNING; 47 | ioctl(tun_sock, SIOCSIFFLAGS, &ifr); 48 | 49 | close(tun_sock); 50 | 51 | interfaceIndex = if_nametoindex(ifr.ifr_name); 52 | this->my_tox = my_tox; 53 | pthread_attr_t attr; 54 | pthread_attr_init(&attr); 55 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 56 | pthread_create(&reader, &attr, &start_routine, this); 57 | pthread_attr_destroy(&attr); 58 | } 59 | -------------------------------------------------------------------------------- /src/interface_windows.cpp: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | using namespace std; 4 | using namespace ToxVPN; 5 | 6 | NetworkInterface::NetworkInterface() { fd = 0; } 7 | void NetworkInterface::configure(string ip_in, Tox* tox_in) { my_tox = tox_in; } 8 | -------------------------------------------------------------------------------- /src/listener.cpp: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include "listener.h" 3 | #ifdef ZMQ 4 | #include 5 | #endif 6 | 7 | using namespace ToxVPN; 8 | 9 | SocketListener::SocketListener(NetworkInterface* iface) : interfarce(iface) { 10 | socket = dup(0); 11 | } 12 | 13 | #ifndef WIN32 14 | SocketListener::SocketListener(NetworkInterface* iface 15 | ,std::string unixSocket 16 | #ifdef ZMQ 17 | ,void* zmq 18 | #endif 19 | ) 20 | : interfarce(iface) { 21 | socket = ::socket(AF_UNIX, SOCK_STREAM, 0); 22 | struct sockaddr_un addr; 23 | bzero(&addr, sizeof(addr)); 24 | addr.sun_family = AF_UNIX; 25 | strncpy(addr.sun_path, unixSocket.c_str(), sizeof(addr.sun_path) - 1); 26 | unlink(unixSocket.c_str()); 27 | if(bind(socket, (struct sockaddr*) &addr, sizeof(addr))) { 28 | printf("unable to bind control socket: %s\n", strerror(errno)); 29 | } 30 | chmod(unixSocket.c_str(), 0777); 31 | listen(socket, 5); 32 | 33 | #ifdef ZMQ 34 | zmq_broadcast = zmq_socket(zmq, ZMQ_PUB); 35 | #ifndef NDEBUG 36 | int rc = 37 | #endif 38 | zmq_bind(zmq_broadcast, 39 | (std::string("ipc://") + unixSocket + "broadcast").c_str()); 40 | assert(rc == 0); 41 | #endif 42 | } 43 | #endif 44 | 45 | int SocketListener::populate_fdset(fd_set* readset) { 46 | std::list::const_iterator i; 47 | int max = socket; 48 | FD_SET(socket, readset); 49 | for(i = connections.begin(); i != connections.end(); ++i) { 50 | Control* c = *i; 51 | max = std::max(max, c->populate_fdset(readset)); 52 | } 53 | return max; 54 | } 55 | 56 | void SocketListener::doAccept() { 57 | int newsocket = accept(socket, nullptr, nullptr); 58 | Control* c = new Control(interfarce, newsocket); 59 | connections.push_back(c); 60 | } 61 | 62 | void SocketListener::checkFds(fd_set* readset, 63 | Tox* my_tox, 64 | ToxVPNCore* toxvpn) { 65 | std::list::iterator i; 66 | for(i = connections.begin(); i != connections.end(); ++i) { 67 | Control* c = *i; 68 | if(FD_ISSET(c->handle, readset)) { 69 | ssize_t x = c->handleReadData(my_tox, toxvpn); 70 | if(x == -1) { 71 | connections.erase(i); 72 | return; // FIXME 73 | } 74 | } 75 | } 76 | } 77 | 78 | void SocketListener::broadcast(const char* msg) { 79 | printf("in broadcast with '%s'\n", msg); 80 | #ifdef ZMQ 81 | zmq_msg_t header; 82 | char* hack = new char[4]; 83 | strcpy(hack, "all"); 84 | hack[3] = 0; 85 | #ifndef NDEBUG 86 | int rc = 87 | #endif 88 | zmq_msg_init_data(&header, hack, 3, nullptr, nullptr); 89 | assert(rc == 0); 90 | zmq_msg_send(&header, zmq_broadcast, ZMQ_SNDMORE); 91 | 92 | char* copy = new char[strlen(msg)]; 93 | strncpy(copy, msg, strlen(msg)); 94 | 95 | zmq_msg_t msg_out; 96 | #ifndef NDEBUG 97 | rc = 98 | #endif 99 | zmq_msg_init_data(&msg_out, (void*) copy, strlen(msg), nullptr, nullptr); 100 | assert(rc == 0); 101 | zmq_msg_send(&msg_out, zmq_broadcast, 0); 102 | #endif 103 | } 104 | -------------------------------------------------------------------------------- /src/listener.h: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include "control.h" 3 | 4 | namespace ToxVPN { 5 | 6 | class SocketListener { 7 | public: 8 | SocketListener(NetworkInterface* interfarce); 9 | #ifndef WIN32 10 | SocketListener(NetworkInterface* interfarce 11 | ,std::string unixSocket 12 | #ifdef ZMQ 13 | ,void* zmq 14 | #endif 15 | ); 16 | #endif 17 | int populate_fdset(fd_set* readset); 18 | void checkFds(fd_set* readset, Tox* my_tox, ToxVPNCore* toxvpn); 19 | void doAccept(); 20 | void broadcast(const char* msg); 21 | 22 | int socket; 23 | 24 | private: 25 | std::list connections; 26 | NetworkInterface* interfarce; 27 | #ifdef ZMQ 28 | void* zmq_broadcast; 29 | #endif 30 | }; 31 | } 32 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include "control.h" 3 | #include "listener.h" 4 | #include "interface.h" 5 | #include "route.h" 6 | #ifdef ZMQ 7 | #include 8 | #endif 9 | #include 10 | 11 | using namespace std; 12 | using namespace ToxVPN; 13 | using namespace std::chrono; 14 | 15 | using json = nlohmann::json; 16 | 17 | NetworkInterface* mynic; 18 | volatile bool keep_running = true; 19 | std::string myip; 20 | int epoll_handle; 21 | 22 | void hex_string_to_bin(const char* hex_string, uint8_t* ret) { 23 | // byte is represented by exactly 2 hex digits, so lenth of binary string 24 | // is half of that of the hex one. only hex string with even length 25 | // valid. the more proper implementation would be to check if 26 | // strlen(hex_string) 27 | // is odd and return error code if it is. we assume strlen is even. if it's 28 | // not 29 | // then the last byte just won't be written in 'ret'. 30 | size_t i, len = strlen(hex_string) / 2; 31 | const char* pos = hex_string; 32 | 33 | for(i = 0; i < len; ++i, pos += 2) 34 | sscanf(pos, "%2hhx", &ret[i]); 35 | } 36 | 37 | void to_hex(char* a, const uint8_t* p, int size) { 38 | char buffer[3]; 39 | for(int i = 0; i < size; i++) { 40 | snprintf(buffer, 3, "%02x", p[i]); 41 | a[i * 2] = buffer[0]; 42 | a[i * 2 + 1] = buffer[1]; 43 | } 44 | } 45 | namespace ToxVPN { 46 | 47 | int netmode = MODE_TUN; 48 | 49 | bool saveState(Tox* tox) { 50 | size_t size = tox_get_savedata_size(tox); 51 | uint8_t* savedata = new uint8_t[size]; 52 | tox_get_savedata(tox, savedata); 53 | int fd = open("savedata", O_TRUNC | O_WRONLY | O_CREAT, 0644); 54 | assert(fd); 55 | ssize_t written = write(fd, savedata, size); 56 | assert(written > 0); // FIXME: check even if NDEBUG is disabled 57 | close(fd); 58 | delete[] savedata; 59 | return written > 0; 60 | } 61 | 62 | void do_bootstrap(Tox* tox, ToxVPNCore* toxvpn) { 63 | assert(toxvpn->nodes.size() > 0); 64 | size_t i = rand() % toxvpn->nodes.size(); 65 | printf("%lu / %lu\n", i, toxvpn->nodes.size()); 66 | uint8_t* bootstrap_pub_key = new uint8_t[TOX_PUBLIC_KEY_SIZE]; 67 | hex_string_to_bin(toxvpn->nodes[i].pubkey.c_str(), bootstrap_pub_key); 68 | tox_bootstrap(tox, toxvpn->nodes[i].ipv4.c_str(), toxvpn->nodes[i].port, 69 | bootstrap_pub_key, nullptr); 70 | delete[] bootstrap_pub_key; 71 | toxvpn->last_boostrap = steady_clock::now(); 72 | fflush(stdout); 73 | } 74 | 75 | ToxVPNCore::ToxVPNCore() : listener(nullptr){} 76 | } 77 | 78 | void MyFriendRequestCallback(Tox* tox, 79 | const uint8_t* public_key, 80 | const uint8_t* message, 81 | size_t length, 82 | void* user_data) { 83 | ToxVPNCore* toxvpn = static_cast(user_data); 84 | char tox_printable_id[TOX_PUBLIC_KEY_SIZE * 2 + 1]; 85 | string msg((const char*) message, length); 86 | 87 | memset(tox_printable_id, 0, sizeof(tox_printable_id)); 88 | to_hex(tox_printable_id, public_key, TOX_PUBLIC_KEY_SIZE); 89 | 90 | char formated[512]; 91 | snprintf(formated, 511, "Friend request: %s\nto accept, run 'whitelist %s'", 92 | message, tox_printable_id); 93 | 94 | printf("%s\n", formated); 95 | fflush(stdout); 96 | 97 | toxvpn->listener->broadcast(formated); 98 | saveState(tox); 99 | } 100 | 101 | #ifdef SYSTEMD 102 | static void notify(const char* message) { sd_notify(0, message); } 103 | #endif 104 | 105 | bool did_ready = false; 106 | 107 | void do_ready() { 108 | if(did_ready) 109 | return; 110 | did_ready = true; 111 | #ifdef SYSTEMD 112 | notify("READY=1"); 113 | #endif 114 | } 115 | 116 | void FriendConnectionUpdate(Tox* tox, 117 | uint32_t friend_number, 118 | Tox_Connection connection_status, 119 | void* user_data) { 120 | ToxVPNCore* toxvpn = static_cast(user_data); 121 | size_t namesize = tox_friend_get_name_size(tox, friend_number, nullptr); 122 | uint8_t* friendname = new uint8_t[namesize + 1]; 123 | tox_friend_get_name(tox, friend_number, friendname, nullptr); 124 | friendname[namesize] = 0; 125 | 126 | char formated[512]; 127 | 128 | switch(connection_status) { 129 | case TOX_CONNECTION_NONE: 130 | snprintf(formated, 511, "friend %d(%s) went offline", friend_number, 131 | friendname); 132 | mynic->removePeer(friend_number); 133 | break; 134 | case TOX_CONNECTION_TCP: 135 | snprintf(formated, 511, "friend %d(%s) connected via tcp", 136 | friend_number, friendname); 137 | break; 138 | case TOX_CONNECTION_UDP: 139 | snprintf(formated, 511, "friend %d(%s) connected via udp", 140 | friend_number, friendname); 141 | break; 142 | } 143 | delete[] friendname; 144 | 145 | if(toxvpn->listener) 146 | toxvpn->listener->broadcast(formated); 147 | 148 | printf("%s\n", formated); 149 | fflush(stdout); 150 | } 151 | 152 | void MyFriendMessageCallback(Tox*, 153 | uint32_t friend_number, 154 | Tox_Message_Type type, 155 | const uint8_t* message, 156 | size_t length, 157 | void*) { 158 | string msg((const char*) message, length); 159 | cout << "message" << friend_number << msg << type << endl; 160 | } 161 | 162 | #ifdef WIN32 163 | void inet_pton(int type, const char* input, struct in_addr* output) { 164 | unsigned long result = inet_addr(input); 165 | output->S_un.S_addr = result; 166 | } 167 | #endif 168 | 169 | void MyFriendStatusCallback(Tox* tox, 170 | uint32_t friend_number, 171 | const uint8_t* message, 172 | size_t length, 173 | void*) { 174 | uint8_t pubkey[TOX_PUBLIC_KEY_SIZE]; 175 | tox_friend_get_public_key(tox, friend_number, &pubkey[0], NULL); 176 | printf("status msg #%d %s\n", friend_number, message); 177 | try { 178 | json root = json::parse(std::string((const char*) message, length)); 179 | json ip = root["ownip"]; 180 | int peer_netmode = MODE_TUN; 181 | if (root["mode"] == "tap") peer_netmode = MODE_TAP; 182 | if(ip.is_string()) { 183 | std::string peerip = ip; 184 | struct in_addr peerBinary; 185 | inet_pton(AF_INET, peerip.c_str(), &peerBinary); 186 | printf("setting friend#%d ip to %s\n", friend_number, 187 | peerip.c_str()); 188 | mynic->setPeerIp(peerBinary, friend_number, peer_netmode, pubkey); 189 | } else { 190 | // FIXME: handle error condition instead of silently failing 191 | } 192 | } catch(...) { printf("unable to parse status, ignoring\n"); } 193 | saveState(tox); 194 | fflush(stdout); 195 | } 196 | 197 | void MyFriendLossyPacket(Tox *tox, uint32_t friend_number, const uint8_t* data, size_t length, void*) { 198 | if(data[0] == 200) { 199 | uint8_t pubkey[TOX_PUBLIC_KEY_SIZE]; 200 | tox_friend_get_public_key(tox, friend_number, &pubkey[0], NULL); 201 | mynic->processPacket(data + 1, length - 1, friend_number, MODE_TUN, pubkey); 202 | } 203 | } 204 | 205 | void handle_int(int something) { 206 | printf("int %d!", something); 207 | keep_running = false; 208 | } 209 | 210 | void add_auto_friends(Tox* tox, ToxVPNCore* toxvpn) { 211 | uint8_t peerbinary[TOX_ADDRESS_SIZE]; 212 | Tox_Err_Friend_Add error; 213 | const char* msg = "auto-toxvpn"; 214 | bool need_save = false; 215 | 216 | for(std::vector::iterator it = toxvpn->auto_friends.begin(); 217 | it != toxvpn->auto_friends.end(); ++it) { 218 | string toxid = *it; 219 | hex_string_to_bin(toxid.c_str(), peerbinary); 220 | tox_friend_add(tox, (const uint8_t*) peerbinary, (const uint8_t*) msg, strlen(msg), 221 | &error); 222 | switch(error) { 223 | case TOX_ERR_FRIEND_ADD_OK: 224 | need_save = true; 225 | cout << "added " << toxid << "\n"; 226 | break; 227 | case TOX_ERR_FRIEND_ADD_ALREADY_SENT: break; 228 | case TOX_ERR_FRIEND_ADD_BAD_CHECKSUM: 229 | cerr << "crc error when handling auto-friend" << toxid << "\n"; 230 | break; 231 | default: printf("err code %d\n", error); 232 | } 233 | } 234 | if(need_save) 235 | saveState(tox); 236 | } 237 | 238 | void connection_status(Tox* tox, 239 | Tox_Connection connection_status, 240 | void* user_data) { 241 | ToxVPNCore* toxvpn = static_cast(user_data); 242 | uint8_t toxid[TOX_ADDRESS_SIZE]; 243 | tox_self_get_address(tox, toxid); 244 | char tox_printable_id[TOX_ADDRESS_SIZE * 2 + 1]; 245 | memset(tox_printable_id, 0, sizeof(tox_printable_id)); 246 | to_hex(tox_printable_id, toxid, TOX_ADDRESS_SIZE); 247 | 248 | char buffer[128]; 249 | const char* msg = nullptr; 250 | 251 | switch(connection_status) { 252 | case TOX_CONNECTION_NONE: 253 | msg = "offline"; 254 | puts("connection lost"); 255 | break; 256 | case TOX_CONNECTION_TCP: 257 | msg = "connected via tcp"; 258 | puts("tcp connection established"); 259 | do_ready(); 260 | add_auto_friends(tox, toxvpn); 261 | break; 262 | case TOX_CONNECTION_UDP: 263 | msg = "connected via udp"; 264 | puts("udp connection established"); 265 | do_ready(); 266 | add_auto_friends(tox, toxvpn); 267 | break; 268 | } 269 | if(msg) { 270 | snprintf(buffer, 120, "STATUS=%s, id=%s", msg, tox_printable_id); 271 | #ifdef SYSTEMD 272 | notify(buffer); 273 | #endif 274 | } 275 | saveState(tox); 276 | fflush(stdout); 277 | } 278 | 279 | std::string readFile(std::string path) { 280 | std::string output; 281 | FILE* handle = fopen(path.c_str(), "r"); 282 | if(!handle) 283 | return ""; 284 | char buffer[100]; 285 | while(size_t bytes = fread(buffer, 1, 99, handle)) { 286 | std::string part(buffer, bytes); 287 | output += part; 288 | } 289 | fclose(handle); 290 | return output; 291 | } 292 | 293 | void saveConfig(json root) { 294 | std::string json_str = root.dump(); 295 | FILE* handle = fopen("config.json", "w"); 296 | if(!handle) { 297 | cerr << "unable to open config file for writting" << endl; 298 | exit(-1); 299 | } 300 | const char* data = json_str.c_str(); 301 | fwrite(data, json_str.length(), 1, handle); 302 | fclose(handle); 303 | } 304 | 305 | #ifdef ZMQ 306 | struct zmq_ctx_deleter { 307 | void operator()(void *zmq) const { zmq_ctx_term(zmq); } 308 | }; 309 | 310 | using zmq_ptr = std::unique_ptr; 311 | #endif 312 | 313 | struct tox_options_deleter { 314 | void operator()(Tox_Options *opts) const { tox_options_free(opts); } 315 | }; 316 | 317 | using tox_options_ptr = std::unique_ptr; 318 | 319 | int main(int argc, char** argv) { 320 | #ifdef USE_EPOLL 321 | epoll_handle = epoll_create(20); 322 | assert(epoll_handle >= 0); 323 | #endif 324 | 325 | #ifdef ZMQ 326 | zmq_ptr zmq(zmq_ctx_new()); 327 | #endif 328 | ToxVPNCore toxvpn; 329 | 330 | assert(strlen(BOOTSTRAP_FILE) > 5); 331 | 332 | json bootstrapRoot; 333 | 334 | try { 335 | if (strcmp(BOOTSTRAP_FILE, "") == 0) { 336 | cerr << "bootstrap file path is invalid\n"; 337 | return -2; 338 | } 339 | bootstrapRoot = json::parse(readFile(BOOTSTRAP_FILE)); 340 | json nodes = bootstrapRoot["nodes"]; 341 | assert(nodes.is_array()); 342 | for(size_t i = 0; i < nodes.size(); i++) { 343 | json e = nodes[i]; 344 | // printf("node %d\n",i); 345 | std::string ipv4 = e["ipv4"]; 346 | uint16_t port = e["port"]; 347 | std::string pubkey = e["public_key"]; 348 | // printf("%s %d %s\n", ipv4.c_str(), port, pubkey.c_str()); 349 | toxvpn.nodes.push_back(bootstrap_node(ipv4, port, pubkey)); 350 | } 351 | } catch(...) { 352 | cerr << "exception while trying to load bootstrap nodes"; 353 | return -2; 354 | } 355 | 356 | toxvpn.nodes.shrink_to_fit(); 357 | 358 | route_init(); 359 | 360 | #ifndef WIN32 361 | struct sigaction interupt; 362 | memset(&interupt, 0, sizeof(interupt)); 363 | interupt.sa_handler = &handle_int; 364 | sigaction(SIGINT, &interupt, nullptr); 365 | #endif 366 | 367 | json configRoot; 368 | 369 | int opt; 370 | Tox_Err_New new_error; 371 | bool stdin_is_socket = false; 372 | string changeIp; 373 | string unixSocket; 374 | tox_options_ptr opts(tox_options_new(nullptr)); 375 | tox_options_set_start_port(opts.get(), 33445); 376 | tox_options_set_end_port(opts.get(), 33445 + 100); 377 | struct passwd* target_user = nullptr; 378 | while((opt = getopt(argc, argv, "m:shi:l:u:p:a:")) != -1) { 379 | switch(opt) { 380 | case 's': stdin_is_socket = true; break; 381 | case 'h': 382 | case '?': 383 | cout << "-s\t\ttreat stdin as a unix socket server" << endl; 384 | cout << "-i \t\tuse this IP on the vpn" << endl; 385 | cout << "-l \tlisten on a unix socket at this path" << endl; 386 | cout << "-u \tswitch to this user once root is no longer " 387 | "required" 388 | << endl; 389 | cout << "-p \tbind on a given port" << endl; 390 | cout << "-h\t\tprint this help" << endl; 391 | return 0; 392 | case 'i': changeIp = optarg; break; 393 | case 'l': unixSocket = optarg; break; 394 | case 'u': 395 | #if defined(WIN32) || defined(__CYGWIN__) 396 | puts("-u not currently supported on windows"); 397 | #else 398 | target_user = getpwnam(optarg); 399 | assert(target_user); 400 | #endif 401 | break; 402 | case 'p': { 403 | const uint16_t port = (uint16_t) strtol(optarg, nullptr, 10); 404 | tox_options_set_start_port(opts.get(), port); 405 | tox_options_set_end_port(opts.get(), port); 406 | break; 407 | } 408 | case 'a': 409 | toxvpn.auto_friends.push_back(string(optarg)); 410 | break; 411 | case 'm': 412 | if (strcmp(optarg, "tun") == 0) { 413 | netmode = MODE_TUN; 414 | } else if (strcmp(optarg, "tap") == 0) { 415 | netmode = MODE_TAP; 416 | } else { 417 | fprintf(stderr, "invalid mode: %s\n", optarg); 418 | exit(-1); 419 | } 420 | break; 421 | } 422 | } 423 | toxvpn.auto_friends.shrink_to_fit(); 424 | 425 | 426 | puts("creating interface"); 427 | mynic = new NetworkInterface(); 428 | #if defined(WIN32) || defined(__CYGWIN__) 429 | puts("no drop root support yet"); 430 | if(0) { // TODO, cd into %AppData% 431 | #else 432 | if(target_user) { 433 | puts("setting uid"); 434 | #if !defined(WIN32) && !defined(__APPLE__) && !defined(__CYGWIN__) 435 | cap_value_t cap_values[] = {CAP_NET_ADMIN}; 436 | cap_t caps; 437 | 438 | caps = cap_get_proc(); 439 | cap_set_flag(caps, CAP_PERMITTED, 1, cap_values, CAP_SET); 440 | cap_set_proc(caps); 441 | prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0); 442 | cap_free(caps); 443 | #endif 444 | 445 | if(setgid(target_user->pw_gid)) { 446 | cerr << "unable to setgid()" << endl; 447 | return -2; 448 | } 449 | if(setuid(target_user->pw_uid)) { 450 | cerr << "unable to setuid()" << endl; 451 | return -2; 452 | } 453 | 454 | #if !defined(WIN32) && !defined(__APPLE__) && !defined(__CYGWIN__) 455 | caps = cap_get_proc(); 456 | cap_clear(caps); 457 | cap_set_flag(caps, CAP_PERMITTED, 1, cap_values, CAP_SET); 458 | cap_set_flag(caps, CAP_EFFECTIVE, 1, cap_values, CAP_SET); 459 | cap_set_proc(caps); 460 | cap_free(caps); 461 | #endif 462 | } else 463 | target_user = getpwnam("root"); 464 | if(chdir(target_user->pw_dir)) { 465 | #endif 466 | printf("unable to cd into $HOME(%s): %s\n", target_user->pw_dir, strerror(errno)); 467 | return -1; 468 | } 469 | if(chdir(".toxvpn")) { 470 | #ifdef WIN32 471 | mkdir(".toxvpn"); 472 | #else 473 | mkdir(".toxvpn", 0755); 474 | #endif 475 | if (chdir(".toxvpn")) { 476 | perror("chdir .toxvpn still fails"); 477 | return -1; 478 | } 479 | } 480 | 481 | try { 482 | std::string config = readFile("config.json"); 483 | configRoot = json::parse(config); 484 | if(changeIp.length() > 0) { 485 | configRoot["myip"] = changeIp; 486 | saveConfig(configRoot); 487 | } 488 | json ip = configRoot["myip"]; 489 | if(ip.is_string()) { 490 | myip = ip; 491 | } 492 | } catch(...) { 493 | if(changeIp.length() > 0) { 494 | configRoot["myip"] = myip = changeIp; 495 | } else { 496 | cout << "what is the VPN ip of this computer?" << endl; 497 | cin >> myip; 498 | configRoot["myip"] = myip; 499 | } 500 | saveConfig(configRoot); 501 | } 502 | 503 | json root{{"ownip", configRoot["myip"]}}; 504 | if (netmode == MODE_TAP) { 505 | root["mode"] = "tap"; 506 | } else { 507 | root["mode"] = "tun"; 508 | } 509 | 510 | Tox* my_tox; 511 | bool want_bootstrap = false; 512 | int oldstate = open("savedata", O_RDONLY); 513 | std::vector temp; 514 | if(oldstate >= 0) { 515 | struct stat info; 516 | fstat(oldstate, &info); 517 | temp.resize(info.st_size); 518 | ssize_t size = read(oldstate, temp.data(), info.st_size); 519 | close(oldstate); 520 | assert(size == info.st_size); 521 | tox_options_set_savedata_type(opts.get(), TOX_SAVEDATA_TYPE_TOX_SAVE); 522 | tox_options_set_savedata_data(opts.get(), temp.data(), size); 523 | } 524 | 525 | want_bootstrap = true; 526 | my_tox = tox_new(opts.get(), &new_error); 527 | if(!my_tox) { 528 | tox_options_set_ipv6_enabled(opts.get(), false); 529 | my_tox = tox_new(opts.get(), &new_error); 530 | } 531 | switch(new_error) { 532 | case TOX_ERR_NEW_OK: break; 533 | case TOX_ERR_NEW_PORT_ALLOC: 534 | cerr << "unable to bind to a port between " << tox_options_get_start_port(opts.get()) 535 | << " and " << tox_options_get_end_port(opts.get()) << endl; 536 | return 1; 537 | default: 538 | cerr << "unhandled error code on tox_new: " << new_error << endl; 539 | return 2; 540 | } 541 | assert(my_tox); 542 | opts = nullptr; 543 | 544 | uint8_t toxid[TOX_ADDRESS_SIZE]; 545 | tox_self_get_address(my_tox, toxid); 546 | char tox_printable_id[TOX_ADDRESS_SIZE * 2 + 1]; 547 | memset(tox_printable_id, 0, sizeof(tox_printable_id)); 548 | to_hex(tox_printable_id, toxid, TOX_ADDRESS_SIZE); 549 | printf("my id is %s and IP is %s\n", tox_printable_id, myip.c_str()); 550 | 551 | /* Register the callbacks */ 552 | tox_callback_friend_request(my_tox, MyFriendRequestCallback); 553 | tox_callback_friend_message(my_tox, MyFriendMessageCallback); 554 | tox_callback_friend_status_message(my_tox, MyFriendStatusCallback); 555 | tox_callback_friend_connection_status(my_tox, FriendConnectionUpdate); 556 | tox_callback_friend_lossy_packet(my_tox, MyFriendLossyPacket); 557 | tox_callback_self_connection_status(my_tox, &connection_status); 558 | 559 | /* Define or load some user details for the sake of it */ 560 | #ifndef WIN32 561 | struct utsname hostinfo; 562 | uname(&hostinfo); 563 | tox_self_set_name(my_tox, (const uint8_t*) hostinfo.nodename, 564 | strlen(hostinfo.nodename), nullptr); // Sets the username 565 | #else 566 | const char* hostname = "windows"; 567 | tox_self_set_name(my_tox, (const uint8_t*) hostname, strlen(hostname), 568 | nullptr); 569 | #endif 570 | std::string json_str = root.dump(); 571 | if(json_str[json_str.length() - 1] == '\n') { 572 | json_str.erase(json_str.length() - 1, 1); 573 | } 574 | tox_self_set_status_message(my_tox, (const uint8_t*) json_str.data(), 575 | json_str.length(), 576 | nullptr); // Sets the status message 577 | 578 | /* Set the user status to TOX_USER_STATUS_NONE. Other possible values: 579 | * TOX_USER_STATUS_AWAY and TOX_USER_STATUS_BUSY */ 580 | tox_self_set_status(my_tox, TOX_USER_STATUS_NONE); 581 | 582 | /* Bootstrap from the node defined above */ 583 | if(want_bootstrap) 584 | do_bootstrap(my_tox, &toxvpn); 585 | 586 | #ifdef USE_SELECT 587 | fd_set readset; 588 | #endif 589 | mynic->configure(myip, my_tox); 590 | Control* control = nullptr; 591 | 592 | if(unixSocket.length()) { 593 | #ifdef WIN32 594 | puts("error, -l is linux only"); 595 | return -1; 596 | #elif defined(ZMQ) 597 | toxvpn.listener = new SocketListener(mynic, unixSocket, zmq.get()); 598 | #else 599 | toxvpn.listener = new SocketListener(mynic, unixSocket); 600 | #endif 601 | } else if(stdin_is_socket) { 602 | toxvpn.listener = new SocketListener(mynic); 603 | } else { 604 | control = new Control(mynic); 605 | } 606 | fflush(stdout); 607 | while(keep_running) { 608 | int interval = tox_iteration_interval(my_tox); 609 | #ifdef USE_SELECT 610 | FD_ZERO(&readset); 611 | struct timeval timeout; 612 | int maxfd = 0; 613 | #if 0 614 | maxfd = tox_populate_fdset(my_tox,&readset); 615 | #endif 616 | #ifndef WIN32 617 | if(control) 618 | maxfd = std::max(maxfd, control->populate_fdset(&readset)); 619 | if(toxvpn.listener) 620 | maxfd = std::max(maxfd, toxvpn.listener->populate_fdset(&readset)); 621 | { 622 | int udp_sock = tox_get_udp_socket(my_tox); 623 | FD_SET(udp_sock, &readset); 624 | maxfd = std::max(maxfd, udp_sock); 625 | interval = 1000; 626 | } 627 | #endif 628 | 629 | #endif 630 | #ifdef USE_SELECT 631 | timeout.tv_sec = 0; 632 | timeout.tv_usec = interval * 1000; 633 | int r; 634 | #ifdef WIN32 635 | if(maxfd == 0) { 636 | Sleep(interval); 637 | r = -2; 638 | } else 639 | #endif 640 | r = select(maxfd + 1, &readset, nullptr, nullptr, &timeout); 641 | if(r > 0) { 642 | if(control && FD_ISSET(control->handle, &readset)) 643 | control->handleReadData(my_tox, &toxvpn); 644 | if(toxvpn.listener && FD_ISSET(toxvpn.listener->socket, &readset)) 645 | toxvpn.listener->doAccept(); 646 | if(toxvpn.listener) 647 | toxvpn.listener->checkFds(&readset, my_tox, &toxvpn); 648 | } else if(r == 0) { 649 | } else { 650 | if(r != -2) { 651 | #ifdef WIN32 652 | int error = WSAGetLastError(); 653 | printf("winsock error %d %d\n", error, r); 654 | #endif 655 | printf("select error %d %d %s\n", r, errno, strerror(errno)); 656 | } 657 | } 658 | #endif 659 | 660 | tox_iterate( 661 | my_tox, 662 | &toxvpn); // will call the callback functions defined and registered 663 | 664 | #ifdef USE_EPOLL 665 | struct epoll_event events[10]; 666 | int count = epoll_wait(epoll_handle, events, 10, interval); 667 | if(count == -1) 668 | std::cout << "epoll error " << strerror(errno) << std::endl; 669 | else { 670 | for(int i = 0; i < count; i++) { 671 | EpollTarget* t = (EpollTarget*) events[i].data.ptr; 672 | t->handleReadData(my_tox); 673 | } 674 | } 675 | #endif 676 | Tox_Connection conn_status = tox_self_get_connection_status(my_tox); 677 | if(conn_status == TOX_CONNECTION_NONE) { 678 | steady_clock::time_point now = steady_clock::now(); 679 | duration time_span = 680 | duration_cast>(now - toxvpn.last_boostrap); 681 | if(time_span.count() > 10) { 682 | do_bootstrap(my_tox, &toxvpn); 683 | } 684 | } 685 | } // while(keep_running) 686 | #ifdef SYSTEMD 687 | notify("STOPPING=1"); 688 | #endif 689 | puts("shutting down"); 690 | if (!saveState(my_tox)) { 691 | cerr << "unable to save state" << endl; 692 | } 693 | tox_kill(my_tox); 694 | if(control) 695 | delete control; 696 | return 0; 697 | } 698 | -------------------------------------------------------------------------------- /src/main.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | /* 3 | * This program is libre software: you can redistribute it and/or modify 4 | * it under the terms of the GNU General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or 6 | * (at your option) any later version. 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 10 | * 11 | * See the COPYING file for more details. 12 | */ 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | #include 34 | 35 | #if defined(__CYGWIN__) 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #elif defined(WIN32) 42 | #include 43 | #include 44 | #else 45 | // linux+mac includes 46 | #include 47 | #include 48 | #include 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | #include 56 | #include 57 | // linux-only includes 58 | #ifndef __APPLE__ 59 | #include 60 | #include 61 | #include 62 | #include 63 | #include 64 | #include 65 | #ifdef SYSTEMD 66 | #include 67 | #endif 68 | #endif 69 | #endif 70 | 71 | #include 72 | 73 | #define USE_SELECT 74 | 75 | #ifdef USE_EPOLL 76 | #include 77 | #endif 78 | 79 | #include "epoll_target.h" 80 | 81 | namespace ToxVPN { 82 | class SocketListener; 83 | 84 | enum { 85 | MODE_TUN, MODE_TAP 86 | }; 87 | 88 | extern int netmode; 89 | 90 | class bootstrap_node { 91 | public: 92 | bootstrap_node(std::string ipv4_in, uint16_t port_in, std::string pubkey_in) 93 | : ipv4(ipv4_in), pubkey(pubkey_in), port(port_in) {} 94 | std::string ipv4, pubkey; 95 | uint16_t port; 96 | }; 97 | 98 | class ToxVPNCore { 99 | public: 100 | ToxVPNCore(); 101 | SocketListener* listener; 102 | std::vector auto_friends; 103 | std::vector nodes; 104 | std::chrono::steady_clock::time_point last_boostrap; 105 | }; 106 | 107 | bool saveState(Tox* tox); 108 | void do_bootstrap(Tox* tox, ToxVPNCore* toxvpn); 109 | } 110 | 111 | void to_hex(char* a, const uint8_t* p, int size); 112 | void hex_string_to_bin(const char* hex_string, uint8_t* ret); 113 | #ifdef WIN32 114 | void inet_pton(int type, const char* input, struct in_addr* output); 115 | #endif 116 | 117 | extern std::string myip; 118 | -------------------------------------------------------------------------------- /src/route.h: -------------------------------------------------------------------------------- 1 | void route_init(); 2 | void systemRouteSingle(int ifindex, struct in_addr, const char* gateway); 3 | void systemRouteDirect(int ifindex, struct in_addr); 4 | -------------------------------------------------------------------------------- /src/route_linux.cpp: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | #include 4 | #include 5 | 6 | int netlink_socket; 7 | void route_init() { 8 | netlink_socket = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); 9 | } 10 | static struct { 11 | struct nlmsghdr nl; 12 | struct rtmsg rt; 13 | char buf[8192]; 14 | } req; 15 | void send_request(); 16 | 17 | void systemRouteSingle(int ifindex, struct in_addr peer, const char* gateway) { 18 | // http://www.linuxjournal.com/article/8498?page=0,2 19 | struct rtattr* rtap; 20 | 21 | // char *dest = "192.168.123.2"; 22 | unsigned char pn = 32; 23 | 24 | // initialize RTNETLINK request buffer 25 | bzero(&req, sizeof(req)); 26 | 27 | // compute the initial length of the service request 28 | int rtl = sizeof(struct rtmsg); 29 | 30 | // add first attrib 31 | // set destination ip addr and increment the netlink buf size 32 | rtap = (struct rtattr*) req.buf; 33 | rtap->rta_type = RTA_DST; 34 | rtap->rta_len = (unsigned short) (sizeof(struct rtattr) + 4); 35 | memcpy(((char*) rtap) + sizeof(struct rtattr), &peer, 4); 36 | // inet_pton(AF_INET,dest,((char *)rtap) + sizeof(struct rtattr)); 37 | rtl += rtap->rta_len; 38 | 39 | // add second attrib 40 | // set gateway 41 | rtap = (struct rtattr*) (((char*) rtap) + rtap->rta_len); 42 | rtap->rta_type = RTA_GATEWAY; 43 | rtap->rta_len = (unsigned short) (sizeof(struct rtattr) + 4); 44 | inet_pton(AF_INET, gateway, ((char*) rtap) + sizeof(struct rtattr)); 45 | rtl += rtap->rta_len; 46 | 47 | // add third attrib 48 | // set ifc index andincrement the netlink size 49 | rtap = (struct rtattr*) (((char*) rtap) + rtap->rta_len); 50 | rtap->rta_type = RTA_OIF; 51 | rtap->rta_len = (unsigned short) (sizeof(struct rtattr) + 4); 52 | memcpy(((char*) rtap) + sizeof(struct rtattr), &ifindex, 4); 53 | rtl += rtap->rta_len; 54 | 55 | // setup netlink header 56 | req.nl.nlmsg_len = NLMSG_LENGTH(rtl); 57 | req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE; 58 | req.nl.nlmsg_type = RTM_NEWROUTE; 59 | 60 | // setup service header 61 | req.rt.rtm_family = AF_INET; 62 | req.rt.rtm_table = RT_TABLE_MAIN; 63 | req.rt.rtm_protocol = RTPROT_STATIC; 64 | req.rt.rtm_scope = RT_SCOPE_UNIVERSE; 65 | req.rt.rtm_type = RTN_UNICAST; 66 | req.rt.rtm_dst_len = pn; 67 | 68 | send_request(); 69 | } 70 | 71 | void systemRouteDirect(int ifindex, struct in_addr peer) { 72 | struct rtattr* rtap; 73 | unsigned char pn = 32; 74 | 75 | // initialize RTNETLINK request buffer 76 | bzero(&req, sizeof(req)); 77 | 78 | // compute the initial length of the service request 79 | int rtl = sizeof(struct rtmsg); 80 | 81 | // add first attrib 82 | // set destination ip addr and increment the netlink buf size 83 | rtap = (struct rtattr*) req.buf; 84 | rtap->rta_type = RTA_DST; 85 | rtap->rta_len = (unsigned short) (sizeof(struct rtattr) + 4); 86 | memcpy(((char*) rtap) + sizeof(struct rtattr), &peer, 4); 87 | rtl += rtap->rta_len; 88 | 89 | // add second attrib 90 | // set ifc index andincrement the netlink size 91 | rtap = (struct rtattr*) (((char*) rtap) + rtap->rta_len); 92 | rtap->rta_type = RTA_OIF; 93 | rtap->rta_len = (unsigned short) (sizeof(struct rtattr) + 4); 94 | memcpy(((char*) rtap) + sizeof(struct rtattr), &ifindex, 4); 95 | rtl += rtap->rta_len; 96 | 97 | // setup netlink header 98 | req.nl.nlmsg_len = NLMSG_LENGTH(rtl); 99 | req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE; 100 | req.nl.nlmsg_type = RTM_NEWROUTE; 101 | 102 | // setup service header 103 | req.rt.rtm_family = AF_INET; 104 | req.rt.rtm_table = RT_TABLE_MAIN; 105 | req.rt.rtm_protocol = RTPROT_STATIC; 106 | req.rt.rtm_scope = RT_SCOPE_UNIVERSE; 107 | req.rt.rtm_type = RTN_UNICAST; 108 | req.rt.rtm_dst_len = pn; 109 | 110 | send_request(); 111 | } 112 | 113 | void send_request() { 114 | struct sockaddr_nl pa; 115 | bzero(&pa, sizeof(pa)); 116 | pa.nl_family = AF_NETLINK; 117 | 118 | // initialize and create the msghdr 119 | struct msghdr msg; 120 | bzero(&msg, sizeof(msg)); 121 | msg.msg_name = &pa; 122 | msg.msg_namelen = sizeof(pa); 123 | 124 | // place the pointer and size in it 125 | struct iovec iov; 126 | iov.iov_base = (void*) &req.nl; 127 | iov.iov_len = req.nl.nlmsg_len; 128 | msg.msg_iov = &iov; 129 | msg.msg_iovlen = 1; 130 | 131 | ssize_t res = sendmsg(netlink_socket, &msg, 0); 132 | if(res < 0) { 133 | printf("route error: %s\n", strerror(errno)); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /src/route_mac.cpp: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | void route_init() {} 4 | void systemRouteSingle(int ifindex, struct in_addr dest, const char* gateway) { 5 | char buffer[512]; 6 | char network[16]; 7 | const char* netmask = "255.255.255.255"; 8 | strncpy(network, inet_ntoa(dest), 16); 9 | printf("adding route for %s\n", network); 10 | snprintf(buffer, 500, "route add -net %s 10.123.123.123 %s -ifp tun0", 11 | network, netmask); 12 | system(buffer); 13 | } 14 | -------------------------------------------------------------------------------- /src/route_windows.cpp: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | void route_init() {} 4 | void systemRouteSingle(int ifindex, struct in_addr peer, const char* gateway) {} 5 | -------------------------------------------------------------------------------- /src/toxvpn-remote.cpp: -------------------------------------------------------------------------------- 1 | #ifdef ZMQ 2 | #include 3 | #endif 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | bool keep_running; 15 | 16 | #ifdef ZMQ 17 | void read_sub_socket(void* subscriber) { 18 | int more; 19 | size_t more_size = sizeof(more); 20 | zmq_msg_t header, msg; 21 | zmq_msg_init(&header); 22 | char buffer[512]; 23 | 24 | int rc = zmq_msg_recv(&header, subscriber, ZMQ_DONTWAIT); 25 | if((rc == -1) && (errno == EAGAIN)) { 26 | return; 27 | } 28 | assert(rc == 0); 29 | 30 | char* msg_contents = (char*) zmq_msg_data(&header); 31 | size_t msg_size = zmq_msg_size(&header); 32 | strncpy(buffer, msg_contents, msg_size); 33 | buffer[msg_size] = 0; 34 | puts(buffer); 35 | 36 | zmq_getsockopt(subscriber, ZMQ_RCVMORE, &more, &more_size); 37 | for(int i = 0; i < more; i++) { 38 | zmq_msg_init(&msg); 39 | zmq_msg_recv(&msg, subscriber, 0); 40 | msg_contents = (char*) zmq_msg_data(&msg); 41 | msg_size = zmq_msg_size(&msg); 42 | strncpy(buffer, msg_contents, msg_size); 43 | buffer[msg_size] = 0; 44 | printf("%s\n", buffer); 45 | zmq_msg_close(&msg); 46 | } 47 | zmq_msg_close(&header); 48 | } 49 | #endif 50 | 51 | void read_stdin(int socket) { 52 | char buffer[512]; 53 | ssize_t count = read(STDIN_FILENO, buffer, 512); 54 | if(strncmp(buffer, "quit", 4) == 0) { 55 | keep_running = false; 56 | return; 57 | } 58 | write(socket, buffer, count); 59 | } 60 | 61 | void read_socket(int socket) { 62 | char buffer[512]; 63 | ssize_t count = read(socket, buffer, 512); 64 | write(STDOUT_FILENO, buffer, count); 65 | } 66 | 67 | int main(int, char**) { 68 | #ifdef ZMQ 69 | void* zmq = zmq_ctx_new(); 70 | void* subscriber = zmq_socket(zmq, ZMQ_SUB); 71 | zmq_connect(subscriber, "ipc:///run/toxvpn/controlbroadcast"); 72 | zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, "all", 3); 73 | #endif 74 | 75 | std::string unixSocket = "/run/toxvpn/control"; 76 | 77 | int socket = ::socket(AF_UNIX, SOCK_STREAM, 0); 78 | struct sockaddr_un addr; 79 | bzero(&addr, sizeof(addr)); 80 | addr.sun_family = AF_UNIX; 81 | strncpy(addr.sun_path, unixSocket.c_str(), sizeof(addr.sun_path) - 1); 82 | connect(socket, (const struct sockaddr*) &addr, sizeof(struct sockaddr_un)); 83 | 84 | fd_set readset; 85 | keep_running = true; 86 | while(keep_running) { 87 | FD_ZERO(&readset); 88 | struct timeval timeout; 89 | timeout.tv_sec = 0; 90 | timeout.tv_usec = 1000 * 1000; // todo, lower to 100 91 | int maxfd = 0; 92 | 93 | FD_SET(STDIN_FILENO, &readset); 94 | maxfd = std::max(maxfd, STDIN_FILENO); 95 | 96 | FD_SET(socket, &readset); 97 | maxfd = std::max(maxfd, socket); 98 | 99 | int r = select(maxfd + 1, &readset, nullptr, nullptr, &timeout); 100 | #ifdef ZMQ 101 | read_sub_socket(subscriber); 102 | #endif 103 | if(r > 0) { 104 | if(FD_ISSET(STDIN_FILENO, &readset)) 105 | read_stdin(socket); 106 | if(FD_ISSET(socket, &readset)) 107 | read_socket(socket); 108 | } else if(r == 0) { 109 | } else { 110 | printf("select error %d %d %s\n", r, errno, strerror(errno)); 111 | } 112 | } 113 | 114 | #ifdef ZMQ 115 | zmq_close(subscriber); 116 | zmq_ctx_term(zmq); 117 | #endif 118 | } 119 | -------------------------------------------------------------------------------- /src/update-bootstrap: -------------------------------------------------------------------------------- 1 | curl https://nodes.tox.chat/json -o res/bootstrap.json 2 | --------------------------------------------------------------------------------