├── .clang-format ├── .editorconfig ├── .github ├── FUNDING.yml └── workflows │ └── build.yml ├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── Dockerfile ├── LICENSE ├── README.md ├── cmake └── Modules │ └── FindLibJuice.cmake ├── example.conf ├── image.png ├── src ├── daemon.c ├── daemon.h ├── main.c ├── options.c ├── options.h ├── utils.c └── utils.h └── violet.service /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | BasedOnStyle: LLVM 3 | IndentWidth: 4 4 | TabWidth: 4 5 | UseTab: ForIndentation 6 | --- 7 | Language: Cpp 8 | Standard: Cpp11 9 | AccessModifierOffset: -4 10 | ColumnLimit: 100 11 | --- 12 | Language: JavaScript 13 | IndentWidth: 2 14 | UseTab: Never 15 | ColumnLimit: 100 16 | ... 17 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: https://EditorConfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | charset = utf-8 7 | end_of_line = lf 8 | insert_final_newline = true 9 | indent_style = tab 10 | indent_size = 4 11 | 12 | [*.js] 13 | insert_final_newline = true 14 | indent_style = space 15 | indent_size = 2 16 | 17 | [*.py] 18 | insert_final_newline = false 19 | indent_style = space 20 | indent_size = 4 21 | 22 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: ['paullouisageneau'] 2 | custom: ['https://paypal.me/paullouisageneau'] 3 | 4 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | on: 3 | push: 4 | branches: 5 | - master 6 | pull_request: 7 | branches: 8 | - master 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: submodules 15 | run: git submodule update --init --recursive 16 | - name: cmake 17 | run: cmake -B build -DWARNINGS_AS_ERRORS=1 18 | - name: make 19 | run: (cd build; make) 20 | 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | *.d 3 | *.o 4 | *.a 5 | *.so 6 | compile_commands.json 7 | 8 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "deps/libjuice"] 2 | path = deps/libjuice 3 | url = https://github.com/paullouisageneau/libjuice 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(violet 3 | VERSION 0.5.4 4 | LANGUAGES C) 5 | 6 | set(PROJECT_DESCRIPTION "Lightweight STUN/TURN server") 7 | 8 | # Options 9 | option(USE_SYSTEM_JUICE "Use system libjuice" OFF) 10 | option(WARNINGS_AS_ERRORS "Treat warnings as errors" OFF) 11 | 12 | set(CMAKE_C_STANDARD 11) 13 | list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules) 14 | 15 | if(WIN32) 16 | add_definitions(-DWIN32_LEAN_AND_MEAN) 17 | if(MSVC) 18 | add_definitions(-DNOMINMAX) 19 | add_definitions(-D_CRT_SECURE_NO_WARNINGS) 20 | add_definitions(-D_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING) 21 | endif() 22 | endif() 23 | 24 | set(VIOLET_SOURCES 25 | ${CMAKE_CURRENT_SOURCE_DIR}/src/daemon.c 26 | ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c 27 | ${CMAKE_CURRENT_SOURCE_DIR}/src/options.c 28 | ${CMAKE_CURRENT_SOURCE_DIR}/src/utils.c 29 | ) 30 | 31 | set(VIOLET_HEADERS 32 | ${CMAKE_CURRENT_SOURCE_DIR}/src/daemon.h 33 | ${CMAKE_CURRENT_SOURCE_DIR}/src/options.h 34 | ${CMAKE_CURRENT_SOURCE_DIR}/src/utils.h 35 | ) 36 | 37 | add_executable(violet ${VIOLET_HEADERS} ${VIOLET_SOURCES}) 38 | target_compile_definitions(violet PRIVATE VIOLET_VERSION="${PROJECT_VERSION}") 39 | 40 | if(USE_SYSTEM_JUICE) 41 | find_package(LibJuice REQUIRED) 42 | target_link_libraries(violet PRIVATE LibJuice::LibJuice) 43 | else() 44 | option(NO_TESTS "Disable tests for libjuice" ON) 45 | option(NO_EXAMPLES "Disable examples for libjuice" ON) 46 | add_subdirectory(deps/libjuice EXCLUDE_FROM_ALL) 47 | target_link_libraries(violet PRIVATE LibJuice::LibJuiceStatic) 48 | endif() 49 | 50 | install(TARGETS violet RUNTIME DESTINATION bin) 51 | 52 | target_compile_options(violet PRIVATE -Wall -Wextra) 53 | 54 | if(WARNINGS_AS_ERRORS) 55 | target_compile_options(violet PRIVATE -Werror) 56 | endif() 57 | 58 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:3.20 AS builder 2 | 3 | RUN apk update --no-cache; \ 4 | apk add --no-cache cmake make gcc musl-dev; 5 | 6 | WORKDIR /usr/src/violet 7 | COPY . . 8 | 9 | RUN cmake -B build -DCMAKE_BUILD_TYPE=Release; \ 10 | cd build; \ 11 | make -j2 12 | 13 | FROM alpine:3.20 AS app 14 | 15 | RUN apk update --no-cache; \ 16 | apk add --no-cache musl; 17 | 18 | COPY --from=builder /usr/src/violet/build/violet /usr/local/bin/ 19 | 20 | ENTRYPOINT [ "/usr/local/bin/violet" ] 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Violet - Lightweight STUN/TURN server 2 | 3 | [![License: GPL v2 or later](https://img.shields.io/badge/License-GPL_v2_or_later-blue.svg)](https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html) 4 | [![Build](https://github.com/paullouisageneau/violet/actions/workflows/build.yml/badge.svg)](https://github.com/paullouisageneau/violet/actions/workflows/build.yml) 5 | [![Docker](https://img.shields.io/docker/v/paullouisageneau/violet/latest?color=2497ed&label=Docker)](https://hub.docker.com/repository/docker/paullouisageneau/violet) 6 | [![Gitter](https://badges.gitter.im/libjuice/violet.svg)](https://gitter.im/libjuice/violet?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 7 | [![Discord](https://img.shields.io/discord/903257095539925006?logo=discord)](https://discord.gg/jXAP8jp3Nn) 8 | 9 | Violet is a lightweight STUN/TURN server ([RFC8489](https://www.rfc-editor.org/rfc/rfc8489.html) and [RFC8656](https://www.rfc-editor.org/rfc/rfc8656.html)) written in C without dependencies, based on [libjuice](https://github.com/paullouisageneau/libjuice). 10 | 11 | Violet is licensed under GPLv2 or later, see [LICENSE](https://github.com/paullouisageneau/violet/blob/master/LICENSE). 12 | 13 | ![Oompa-Loompas rolling Violet, from Charlie and the Chocolate Factory](https://github.com/paullouisageneau/violet/blob/master/image.png?raw=true) 14 | 15 | > "Mercy! Save us!" yelled Mrs Beauregarde. "[...] Violet, you’re **turn**ing violet, Violet!" [...] 16 | > 17 | > "Squeeze her," said Mr Wonka. "We've got to squeeze the **juice** out of her immediately." 18 | > 19 | > -- Charlie and the Chocolate Factory, Roald Dahl 20 | 21 | ## Dependencies 22 | 23 | No external dependencies! 24 | 25 | ## Running with Docker 26 | 27 | An image is available on [Docker Hub](https://hub.docker.com/repository/docker/paullouisageneau/violet), running the TURN server with default options is as simple as: 28 | ```bash 29 | docker run --network=host paullouisageneau/violet --credentials=USER:PASSWORD 30 | ``` 31 | Available options can be listed with the `--help` flag: 32 | ```bash 33 | docker run paullouisageneau/violet --help 34 | ``` 35 | 36 | ## Installing on Arch Linux 37 | 38 | Violet is available as a [package on AUR](https://aur.archlinux.org/packages/violet/): 39 | ```bash 40 | paru -S violet 41 | sudo systemctl enable --now violet 42 | ``` 43 | The configuration file is `/etc/violet/violet.conf` 44 | 45 | ## Building 46 | 47 | ### Clone repository and submodules 48 | 49 | ```bash 50 | git clone https://github.com/paullouisageneau/violet.git 51 | cd violet 52 | git submodule update --init --recursive 53 | ``` 54 | 55 | ### Build with CMake 56 | 57 | ```bash 58 | cmake -B build -DCMAKE_BUILD_TYPE=Release 59 | cd build 60 | make -j2 61 | ``` 62 | ```bash 63 | ./violet --credentials=USER:PASSWORD 64 | ``` 65 | You can list available options with the `--help` (or `-h`) flag. You can also load a configuration file: 66 | ```bash 67 | ./violet -f ../example.conf 68 | ``` 69 | 70 | ### Build with Docker 71 | 72 | ```bash 73 | docker build -t violet . 74 | ``` 75 | ```bash 76 | docker run --network=host violet --credentials=USER:PASSWORD 77 | ``` 78 | You can list available options with the `--help` flag. You can also load a configuration file: 79 | ```bash 80 | docker run \ 81 | --network=host \ 82 | --mount type=bind,source=$(pwd)/example.conf,target=/etc/violet.conf,readonly \ 83 | paullouisageneau/violet --file=/etc/violet.conf 84 | ``` 85 | 86 | -------------------------------------------------------------------------------- /cmake/Modules/FindLibJuice.cmake: -------------------------------------------------------------------------------- 1 | if (NOT TARGET LibJuice::LibJuice) 2 | find_path(JUICE_INCLUDE_DIR juice/juice.h) 3 | find_library(JUICE_LIBRARY NAMES juice) 4 | 5 | include(FindPackageHandleStandardArgs) 6 | find_package_handle_standard_args(LibJuice DEFAULT_MSG JUICE_LIBRARY JUICE_INCLUDE_DIR) 7 | 8 | if (LibJuice_FOUND) 9 | add_library(LibJuice::LibJuice UNKNOWN IMPORTED) 10 | set_target_properties(LibJuice::LibJuice PROPERTIES 11 | IMPORTED_LOCATION "${JUICE_LIBRARY}" 12 | INTERFACE_INCLUDE_DIRECTORIES "${JUICE_INCLUDE_DIRS}" 13 | INTERFACE_LINK_LIBRARIES "${JUICE_LIBRARIES}" 14 | IMPORTED_LINK_INTERFACE_LANGUAGES "C") 15 | endif () 16 | endif () 17 | 18 | -------------------------------------------------------------------------------- /example.conf: -------------------------------------------------------------------------------- 1 | # Violet - Example configuration file 2 | 3 | # Log level 4 | log-level=info 5 | 6 | # Log file (default stdout) 7 | #log=/var/log/violet.log 8 | 9 | # Port for STUN/TURN server 10 | #port=3478 11 | 12 | # Port range for TURN relay 13 | #range=49152:65535 14 | 15 | # Local address to bind (default any) 16 | #bind=0.0.0.0 17 | 18 | # External address to advertise for TURN relay (default automatic) 19 | #external=X.Y.Z.W 20 | 21 | # TURN credentials with optional quota (default none) 22 | #credentials=user1:pass1 23 | #quota=50 24 | #credentials=user2:pass2 25 | #quota=60 26 | 27 | # Global maximum number of allocations 28 | #max=1024 29 | 30 | -------------------------------------------------------------------------------- /image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paullouisageneau/violet/42b9457b8daee029ae0499580c4a9864899fc90e/image.png -------------------------------------------------------------------------------- /src/daemon.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Paul-Louis Ageneau 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | static void signal_handler(int sig) { (void)sig; } 24 | 25 | // Implements the double-fork technique to create daemon 26 | // Returns 0 for the daemon and the pid for the parent, or -1 on error 27 | int violet_fork_daemon() { 28 | int fd[2]; 29 | if (pipe(fd) != 0) 30 | return -1; 31 | 32 | int pipe_out = fd[0]; 33 | int pipe_in = fd[1]; 34 | 35 | // First fork 36 | pid_t pid = fork(); 37 | if (pid < 0) 38 | return -1; 39 | 40 | if (pid > 0) { // if parent 41 | close(pipe_in); 42 | 43 | // Get pid from pipe 44 | char buf[32]; 45 | int len = read(pipe_out, buf, 32); 46 | close(pipe_out); 47 | if (len <= 0) 48 | return -1; 49 | 50 | buf[len] = '\0'; 51 | if (sscanf(buf, "%d\n", &pid) != 1) 52 | return -1; 53 | 54 | return pid; 55 | } 56 | 57 | close(pipe_out); 58 | 59 | // Set child as session leader 60 | if (setsid() < 0) { 61 | close(pipe_in); 62 | exit(EXIT_FAILURE); 63 | } 64 | 65 | signal(SIGCHLD, signal_handler); 66 | signal(SIGHUP, signal_handler); 67 | 68 | // Second fork 69 | pid = fork(); 70 | if (pid < 0) { 71 | close(pipe_in); 72 | exit(EXIT_FAILURE); 73 | } 74 | 75 | if (pid > 0) { // if parent 76 | close(pipe_in); 77 | exit(EXIT_SUCCESS); 78 | } 79 | 80 | pid = getpid(); 81 | 82 | // Put pid to pipe 83 | char buf[32]; 84 | int len = snprintf(buf, 32, "%d\n", pid); 85 | if (len <= 0 || write(pipe_in, buf, len) <= 0) { 86 | close(pipe_in); 87 | exit(EXIT_FAILURE); 88 | } 89 | 90 | close(pipe_in); 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /src/daemon.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Paul-Louis Ageneau 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; If not, see . 16 | */ 17 | 18 | int violet_fork_daemon(); 19 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Paul-Louis Ageneau 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; If not, see . 16 | */ 17 | 18 | #include "daemon.h" 19 | #include "options.h" 20 | #include "utils.h" 21 | 22 | #include 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | static FILE *log_file = NULL; 32 | 33 | static void signal_handler(int sig) { (void)sig; } 34 | 35 | static void log_handler(juice_log_level_t level, const char *message) { 36 | FILE *file = log_file ? log_file : stdout; 37 | time_t t = time(NULL); 38 | struct tm lt; 39 | char buffer[32]; 40 | if (!localtime_r(&t, <) || strftime(buffer, 32, "%Y-%m-%d %H:%M:%S", <) == 0) 41 | buffer[0] = '\0'; 42 | fprintf(file, "%s %-7s %s\n", buffer, log_level_to_string(level), message); 43 | fflush(file); 44 | } 45 | 46 | int main(int argc, char *argv[]) { 47 | signal(SIGINT, signal_handler); 48 | 49 | violet_options_t vopts; 50 | violet_options_init(&vopts); 51 | 52 | if (violet_options_from_arg(argc, argv, &vopts) < 0) { 53 | goto error; 54 | } 55 | 56 | if (vopts.daemon) { 57 | int pid = violet_fork_daemon(); 58 | if (pid < 0) { 59 | fprintf(stderr, "Fork as daemon failed\n"); 60 | goto error; 61 | } 62 | 63 | if (pid > 0) { // parent 64 | printf("Daemon forked as pid %d\n", pid); 65 | violet_options_destroy(&vopts); 66 | return EXIT_SUCCESS; 67 | } 68 | } 69 | 70 | if (vopts.log_filename) { 71 | log_file = fopen(vopts.log_filename, "a"); 72 | if (!log_file) { 73 | fprintf(stderr, "Log file opening failed\n"); 74 | goto error; 75 | } 76 | } 77 | 78 | juice_set_log_handler(log_handler); 79 | juice_set_log_level(vopts.log_level); 80 | 81 | juice_server_t *server = juice_server_create(&vopts.config); 82 | if (!server) { 83 | fprintf(stderr, "Server initialization failed\n"); 84 | goto error; 85 | } 86 | 87 | pause(); 88 | 89 | juice_server_destroy(server); 90 | 91 | if (log_file) 92 | fclose(log_file); 93 | 94 | violet_options_destroy(&vopts); 95 | return EXIT_SUCCESS; 96 | 97 | error: 98 | if (log_file) 99 | fclose(log_file); 100 | 101 | violet_options_destroy(&vopts); 102 | return EXIT_FAILURE; 103 | } 104 | 105 | -------------------------------------------------------------------------------- /src/options.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Paul-Louis Ageneau 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; If not, see . 16 | */ 17 | 18 | #include "options.h" 19 | #include "utils.h" 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | static char *alloc_string_copy(const char *src, size_t max) { 30 | if (!src) 31 | return NULL; 32 | 33 | size_t len = strlen(src); 34 | if (len > max) 35 | len = max; 36 | 37 | char *copy = malloc(len + 1); 38 | if (!copy) 39 | return NULL; 40 | 41 | memcpy(copy, src, len); 42 | copy[len] = '\0'; 43 | return copy; 44 | } 45 | 46 | static char *trim_string(char *str) { 47 | while (*str != '\0' && isspace(*str)) 48 | ++str; 49 | 50 | if (*str == '\0') 51 | return str; 52 | 53 | char *last = str + strlen(str) - 1; 54 | while (last > str && isspace(*last)) 55 | --last; 56 | *(last + 1) = '\0'; 57 | 58 | return str; 59 | } 60 | 61 | void violet_options_init(violet_options_t *vopts) { 62 | memset(vopts, 0, sizeof(*vopts)); 63 | vopts->log_level = JUICE_LOG_LEVEL_INFO; 64 | vopts->log_filename = NULL; 65 | vopts->daemon = false; 66 | vopts->stun_only = false; 67 | vopts->config.port = 3478; 68 | } 69 | 70 | void violet_options_destroy(violet_options_t *vopts) { 71 | free((char *)vopts->log_filename); 72 | vopts->log_filename = NULL; 73 | 74 | for (int i = 0; i < vopts->config.credentials_count; ++i) { 75 | juice_server_credentials_t *credentials = vopts->config.credentials + i; 76 | free((char *)credentials->username); 77 | free((char *)credentials->password); 78 | } 79 | 80 | free((char *)vopts->config.bind_address); 81 | vopts->config.bind_address = NULL; 82 | 83 | free((char *)vopts->config.external_address); 84 | vopts->config.external_address = NULL; 85 | 86 | free(vopts->config.credentials); 87 | vopts->config.credentials = NULL; 88 | vopts->config.credentials_count = 0; 89 | } 90 | 91 | static int on_help(violet_options_t *vopts, const char *arg); 92 | static int on_version(violet_options_t *vopts, const char *arg); 93 | 94 | static int on_file(violet_options_t *vopts, const char *arg) { 95 | FILE *file = fopen(arg, "r"); 96 | if (!file) { 97 | fprintf(stderr, "Unable to open configuration file \"%s\"\n", arg); 98 | goto error; 99 | } 100 | 101 | if (violet_options_from_file(file, vopts) != 0) { 102 | goto error; 103 | } 104 | 105 | fclose(file); 106 | return 0; 107 | 108 | error: 109 | if (file) 110 | fclose(file); 111 | 112 | violet_options_destroy(vopts); 113 | exit(EXIT_FAILURE); 114 | } 115 | 116 | static int on_log(violet_options_t *vopts, const char *arg) { 117 | if (*arg == '\0') 118 | return -1; 119 | 120 | free((char *)vopts->log_filename); 121 | vopts->log_filename = alloc_string_copy(arg, SIZE_MAX); 122 | return 0; 123 | } 124 | 125 | static int on_log_level(violet_options_t *vopts, const char *arg) { 126 | juice_log_level_t level = string_to_log_level(arg); 127 | if(level == JUICE_LOG_LEVEL_NONE) 128 | return -1; 129 | 130 | vopts->log_level = level; 131 | return 0; 132 | } 133 | 134 | static int on_daemon(violet_options_t *vopts, const char *arg) { 135 | (void)arg; 136 | vopts->daemon = true; 137 | return 0; 138 | } 139 | 140 | static int on_port(violet_options_t *vopts, const char *arg) { 141 | int p = atoi(arg); 142 | if (p <= 0) 143 | return -1; 144 | 145 | vopts->config.port = (uint16_t)p; 146 | return 0; 147 | } 148 | 149 | static int on_range(violet_options_t *vopts, const char *arg) { 150 | uint16_t range_begin = 0; 151 | uint16_t range_end = 0; 152 | if (sscanf(arg, "%hu:%hu", &range_begin, &range_end) != 2) 153 | return -1; 154 | 155 | if (range_end < range_begin) 156 | return -1; 157 | 158 | vopts->config.relay_port_range_begin = range_begin; 159 | vopts->config.relay_port_range_end = range_end; 160 | return 0; 161 | } 162 | 163 | static int on_bind(violet_options_t *vopts, const char *arg) { 164 | if (*arg == '\0') 165 | return -1; 166 | 167 | vopts->config.bind_address = alloc_string_copy(arg, SIZE_MAX); 168 | return 0; 169 | } 170 | 171 | static int on_external(violet_options_t *vopts, const char *arg) { 172 | if (*arg == '\0') 173 | return -1; 174 | 175 | vopts->config.external_address = alloc_string_copy(arg, SIZE_MAX); 176 | return 0; 177 | } 178 | 179 | static int on_credentials(violet_options_t *vopts, const char *arg) { 180 | const char *s = strchr(arg, ':'); 181 | if (!s) 182 | return -1; 183 | 184 | if (vopts->stun_only) 185 | return 0; 186 | 187 | char *username = alloc_string_copy(arg, s - arg); 188 | char *password = alloc_string_copy(s + 1, SIZE_MAX); 189 | vopts->config.credentials = 190 | realloc(vopts->config.credentials, 191 | (vopts->config.credentials_count + 1) * sizeof(juice_server_credentials_t)); 192 | if (!username || !password || !vopts->config.credentials) { 193 | fprintf(stderr, "Memory allocation for credentials failed\n"); 194 | free(username); 195 | free(password); 196 | free(vopts->config.credentials); 197 | vopts->config.credentials_count = 0; 198 | return -1; 199 | } 200 | 201 | juice_server_credentials_t *credentials = 202 | vopts->config.credentials + vopts->config.credentials_count; 203 | memset(credentials, 0, sizeof(*credentials)); 204 | credentials->username = username; 205 | credentials->password = password; 206 | ++vopts->config.credentials_count; 207 | 208 | return 0; 209 | } 210 | 211 | static int on_quota(violet_options_t *vopts, const char *arg) { 212 | int n = atoi(arg); 213 | if (n <= 0) 214 | return -1; 215 | 216 | if (vopts->stun_only) 217 | return 0; 218 | 219 | if (vopts->config.credentials_count == 0) 220 | return -1; 221 | 222 | vopts->config.credentials[vopts->config.credentials_count - 1].allocations_quota = n; 223 | return 0; 224 | } 225 | 226 | static int on_max(violet_options_t *vopts, const char *arg) { 227 | int n = atoi(arg); 228 | if (n <= 0) 229 | return -1; 230 | 231 | if (vopts->stun_only) 232 | return 0; 233 | 234 | vopts->config.max_allocations = n; 235 | return 0; 236 | } 237 | 238 | static int on_stun_only(violet_options_t *vopts, const char *arg) { 239 | (void)arg; 240 | vopts->stun_only = true; 241 | 242 | free(vopts->config.credentials); 243 | vopts->config.credentials = NULL; 244 | vopts->config.credentials_count = 0; 245 | vopts->config.max_allocations = 0; 246 | return 0; 247 | } 248 | 249 | typedef struct violet_option_entry { 250 | char short_name; 251 | const char *long_name; 252 | const char *arg_name; 253 | const char *description; 254 | int (*callback)(violet_options_t *violet_options, const char *value); 255 | } violet_option_entry_t; 256 | 257 | #define VIOLET_OPTIONS_COUNT 14 258 | #define HELP_DESCRIPTION_OFFSET 24 259 | 260 | static const violet_option_entry_t violet_options_map[VIOLET_OPTIONS_COUNT] = { 261 | {'h', "help", NULL, "Display this message", on_help}, 262 | {'v', "version", NULL, "Display the version", on_version}, 263 | {'f', "file", "FILE", "Read configuration from FILE", on_file}, 264 | {'o', "log", "FILE", "Output log to FILE (default stdout)", on_log}, 265 | {'l', "log-level", "LEVEL", "Set log level to LEVEL: fatal, error, warn, info (default), debug, or verbose", on_log_level}, 266 | {'d', "daemon", NULL, "Detach from terminal and run as daemon", on_daemon}, 267 | {'p', "port", "PORT", "UDP port to listen on (default 3478)", on_port}, 268 | {'r', "range", "BEGIN:END", "UDP port range for relay (default automatic)", on_range}, 269 | {'b', "bind", "ADDRESS", "Bind only on ADDRESS (default any address)", on_bind}, 270 | {'e', "external", "ADDRESS", "Advertise relay on ADDRESS (default local address)", on_external}, 271 | {'c', "credentials", "USER:PASS", "Add TURN credentials (may be called multiple times)", 272 | on_credentials}, 273 | {'q', "quota", "ALLOCATIONS", "Set an allocations quota for the last credentials (default none)", on_quota}, 274 | {'m', "max", "ALLOCATIONS", "Set the maximum number of allocations (default 1000)", on_max}, 275 | {'s', "stun-only", NULL, "Disable TURN support", on_stun_only}}; 276 | 277 | static const char *program_name = NULL; 278 | 279 | static int on_help(violet_options_t *vopts, const char *arg) { 280 | (void)vopts; 281 | (void)arg; 282 | printf("Usage: %s [options]\n\n", program_name ? program_name : "violet"); 283 | 284 | for (int i = 0; i < VIOLET_OPTIONS_COUNT; ++i) { 285 | const violet_option_entry_t *entry = violet_options_map + i; 286 | int ret = printf(" %c%c%s%s%c%s", entry->short_name ? '-' : ' ', 287 | entry->short_name ? entry->short_name : ' ', 288 | entry->long_name ? ", --" : " ", entry->long_name ? entry->long_name : "", 289 | entry->long_name && entry->arg_name ? '=' : ' ', 290 | entry->arg_name ? entry->arg_name : ""); 291 | 292 | if (entry->description) { 293 | int offset = ret >= 0 ? HELP_DESCRIPTION_OFFSET - ret : 0; 294 | while (offset-- > 0) 295 | printf(" "); 296 | 297 | printf("\t%s\n", entry->description); 298 | } 299 | } 300 | 301 | printf("\n"); 302 | violet_options_destroy(vopts); 303 | exit(EXIT_SUCCESS); 304 | } 305 | 306 | static int on_version(violet_options_t *vopts, const char *arg) { 307 | (void)vopts; 308 | (void)arg; 309 | 310 | const char *version = VIOLET_VERSION; 311 | printf("violet version %s\n", version); 312 | 313 | violet_options_destroy(vopts); 314 | exit(EXIT_SUCCESS); 315 | } 316 | 317 | int violet_options_from_file(FILE *file, violet_options_t *vopts) { 318 | char *line = NULL; 319 | size_t size = 0; 320 | ssize_t len; 321 | while ((len = getline(&line, &size, file)) >= 0) { 322 | char *str = trim_string(line); 323 | if (str[0] == '\0') 324 | continue; // blank line 325 | if (str[0] == '#') 326 | continue; // comment 327 | 328 | const char *arg = NULL; 329 | char *sep = strchr(str, '='); 330 | if (sep) { 331 | *sep = '\0'; 332 | arg = trim_string(sep + 1); 333 | } 334 | const char *name = trim_string(str); 335 | 336 | int i = 0; 337 | while (i < VIOLET_OPTIONS_COUNT) { 338 | const violet_option_entry_t *entry = violet_options_map + i; 339 | if (strcmp(entry->long_name, name) == 0) { 340 | if (entry->arg_name && !arg) { 341 | fprintf(stderr, "Option \"%s\" in file requires an argument\n", name); 342 | return -1; 343 | } 344 | if (!entry->arg_name && arg) { 345 | fprintf(stderr, "Option \"%s\" in file does not expect an argument\n", name); 346 | return -1; 347 | } 348 | if (entry->callback && entry->callback(vopts, arg) < 0) { 349 | fprintf(stderr, "Option \"%s\" in file cannot be set%s%s\n", name, 350 | arg ? " to value " : "", arg ? arg : ""); 351 | return -1; 352 | } 353 | break; 354 | } 355 | ++i; 356 | } 357 | 358 | if (i == VIOLET_OPTIONS_COUNT) { 359 | fprintf(stderr, "Unknown option \"%s\" in file\n", name); 360 | return -1; 361 | } 362 | } 363 | 364 | free(line); 365 | return 0; 366 | } 367 | 368 | int violet_options_from_arg(int argc, char *argv[], violet_options_t *vopts) { 369 | program_name = argv[0]; 370 | 371 | char short_options[VIOLET_OPTIONS_COUNT * 2 + 2]; 372 | memset(short_options, 0, sizeof(short_options)); 373 | 374 | char *so = short_options; 375 | *so++ = ':'; 376 | for (int i = 0; i < VIOLET_OPTIONS_COUNT; ++i) { 377 | const violet_option_entry_t *entry = violet_options_map + i; 378 | if (entry->short_name) { 379 | *so++ = entry->short_name; 380 | if (entry->arg_name) 381 | *so++ = ':'; 382 | } 383 | } 384 | 385 | struct option long_options[VIOLET_OPTIONS_COUNT + 1]; 386 | memset(long_options, 0, sizeof(long_options)); 387 | 388 | struct option *lo = long_options; 389 | for (int i = 0; i < VIOLET_OPTIONS_COUNT; ++i) { 390 | const violet_option_entry_t *entry = violet_options_map + i; 391 | if (entry->long_name) { 392 | lo->name = entry->long_name; 393 | lo->has_arg = optional_argument; // we are going to check ourselves 394 | ++lo; 395 | } 396 | } 397 | 398 | opterr = false; 399 | while (true) { 400 | int index = 0; 401 | int c = getopt_long(argc, argv, short_options, long_options, &index); 402 | if (c == -1) 403 | break; 404 | 405 | if (c == 0) { 406 | lo = long_options + index; 407 | for (int i = 0; i < VIOLET_OPTIONS_COUNT; ++i) { 408 | const violet_option_entry_t *entry = violet_options_map + i; 409 | if (entry->long_name == lo->name) { 410 | if (entry->arg_name && !optarg) { 411 | fprintf(stderr, "Option --%s requires an argument\n", entry->long_name); 412 | return -1; 413 | } 414 | if (!entry->arg_name && optarg) { 415 | fprintf(stderr, "Option --%s does not expect an argument\n", 416 | entry->long_name); 417 | return -1; 418 | } 419 | if (entry->callback && entry->callback(vopts, optarg) < 0) { 420 | fprintf(stderr, "Option --%s cannot be set%s%s\n", entry->long_name, 421 | optarg ? " to value " : "", optarg ? optarg : ""); 422 | return -1; 423 | } 424 | break; 425 | } 426 | } 427 | continue; 428 | } 429 | 430 | switch (c) { 431 | case ':': 432 | fprintf(stderr, "Option -%c requires an argument\n", optopt); 433 | return -1; 434 | break; 435 | 436 | case '?': 437 | if (optopt == 0) 438 | fprintf(stderr, "Unknown option %s\n", argv[optind - 1]); 439 | else 440 | fprintf(stderr, "Unknown option -%c\n", optopt); 441 | 442 | return -1; 443 | break; 444 | 445 | default: 446 | for (int i = 0; i < VIOLET_OPTIONS_COUNT; ++i) { 447 | const violet_option_entry_t *entry = violet_options_map + i; 448 | if (entry->short_name == c) { 449 | if (entry->callback && entry->callback(vopts, optarg) < 0) { 450 | fprintf(stderr, "Option -%c cannot be set%s%s\n", entry->short_name, 451 | optarg ? " to value " : "", optarg ? optarg : ""); 452 | return -1; 453 | } 454 | break; 455 | } 456 | } 457 | break; 458 | } 459 | } 460 | 461 | if (optind < argc) { 462 | fprintf(stderr, "Unexpected non-option argument \"%s\"\n", argv[optind]); 463 | return -1; 464 | } 465 | 466 | return 0; 467 | } 468 | -------------------------------------------------------------------------------- /src/options.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Paul-Louis Ageneau 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; If not, see . 16 | */ 17 | 18 | #include 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | typedef struct violet_options { 25 | juice_log_level_t log_level; 26 | juice_server_config_t config; 27 | const char *log_filename; 28 | bool daemon; 29 | bool stun_only; 30 | } violet_options_t; 31 | 32 | void violet_options_init(violet_options_t *vopts); 33 | void violet_options_destroy(violet_options_t *vopts); 34 | int violet_options_from_file(FILE *file, violet_options_t *vopts); 35 | int violet_options_from_arg(int argc, char *argv[], violet_options_t *vopts); 36 | -------------------------------------------------------------------------------- /src/utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Paul-Louis Ageneau 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; If not, see . 16 | */ 17 | 18 | #include "utils.h" 19 | 20 | #include 21 | 22 | const char *log_level_to_string(juice_log_level_t level) { 23 | switch (level) { 24 | case JUICE_LOG_LEVEL_NONE: 25 | return "NONE"; 26 | case JUICE_LOG_LEVEL_FATAL: 27 | return "FATAL"; 28 | case JUICE_LOG_LEVEL_ERROR: 29 | return "ERROR"; 30 | case JUICE_LOG_LEVEL_WARN: 31 | return "WARN"; 32 | case JUICE_LOG_LEVEL_INFO: 33 | return "INFO"; 34 | case JUICE_LOG_LEVEL_DEBUG: 35 | return "DEBUG"; 36 | default: 37 | return "VERBOSE"; 38 | } 39 | } 40 | 41 | juice_log_level_t string_to_log_level(const char *str) { 42 | if(strcasecmp(str, "FATAL") == 0) 43 | return JUICE_LOG_LEVEL_FATAL; 44 | 45 | if(strcasecmp(str, "ERROR") == 0) 46 | return JUICE_LOG_LEVEL_ERROR; 47 | 48 | if(strcasecmp(str, "WARN") == 0 || strcasecmp(str, "WARNING") == 0) 49 | return JUICE_LOG_LEVEL_WARN; 50 | 51 | if(strcasecmp(str, "INFO") == 0) 52 | return JUICE_LOG_LEVEL_INFO; 53 | 54 | if(strcasecmp(str, "DEBUG") == 0) 55 | return JUICE_LOG_LEVEL_DEBUG; 56 | 57 | if(strcasecmp(str, "VERBOSE") == 0) 58 | return JUICE_LOG_LEVEL_VERBOSE; 59 | 60 | return JUICE_LOG_LEVEL_NONE; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /src/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Paul-Louis Ageneau 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; If not, see . 16 | */ 17 | 18 | #include 19 | 20 | const char *log_level_to_string(juice_log_level_t level); 21 | juice_log_level_t string_to_log_level(const char *str); 22 | -------------------------------------------------------------------------------- /violet.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Violet lightweight STUN/TURN server 3 | After=network-online.target 4 | 5 | [Service] 6 | Type=simple 7 | User=violet 8 | Group=violet 9 | UMask=002 10 | ExecStart=/usr/bin/violet -f /etc/violet/violet.conf 11 | Restart=on-failure 12 | TimeoutStopSec=30 13 | 14 | [Install] 15 | WantedBy=multi-user.target 16 | 17 | --------------------------------------------------------------------------------