├── .github └── workflows │ └── ci.yml ├── .gitignore ├── AUTHORS ├── COPYING ├── ChangeLog ├── Doxyfile.in ├── INSTALL ├── Makefile.am ├── NEWS ├── README ├── README.md ├── configure.ac ├── davici.c ├── davici.h ├── examples ├── .gitignore ├── Makefile.am └── version.c └── tests ├── .gitignore ├── Makefile.am ├── badsock.c ├── cmd.c ├── cmdunknown.c ├── dump.c ├── event.c ├── eventunknown.c ├── many.c ├── recurse.c ├── stream.c ├── tester.c └── tester.h /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | ci: 7 | strategy: 8 | matrix: 9 | os: [ ubuntu-latest ] 10 | compiler: [ gcc, clang ] 11 | include: 12 | - os: macos-latest 13 | compiler: clang 14 | 15 | runs-on: ${{ matrix.os }} 16 | env: 17 | CC: ${{ matrix.compiler }} 18 | 19 | steps: 20 | - if: ${{ startsWith(matrix.os, 'macos') }} 21 | name: Install macOS build-deps 22 | run: brew install automake 23 | - uses: actions/checkout@v2 24 | - run: autoreconf -i 25 | - name: Configure 26 | run: CFLAGS="-g -O2 -Wall -Werror" ./configure 27 | - name: Build 28 | run: make 29 | - name: Tests 30 | run: make check 31 | - if: ${{ failure() }} 32 | name: Collect error logs 33 | run: find ./tests -name '*.log' -print -exec cat {} \; 34 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.lo 3 | *.la 4 | .deps/ 5 | .libs/ 6 | Makefile 7 | Makefile.in 8 | aclocal.m4 9 | autom4te.cache/ 10 | m4/ 11 | compile 12 | config.guess 13 | config.h 14 | config.h.in 15 | config.log 16 | config.status 17 | config.sub 18 | configure 19 | depcomp 20 | install-sh 21 | libtool 22 | ltmain.sh 23 | missing 24 | stamp-h1 25 | test-driver 26 | *.gcda 27 | *.gcno 28 | coverage 29 | Doxyfile 30 | api/ 31 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/strongswan/davici/fdd58792ffe7c8bb9a77cd05a7d7dcdb8f78f5cc/AUTHORS -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 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 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | 474 | Copyright (C) 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 489 | 490 | Also add information on how to contact you by electronic and paper mail. 491 | 492 | You should also get your employer (if you work as a programmer) or your 493 | school, if any, to sign a "copyright disclaimer" for the library, if 494 | necessary. Here is a sample; alter the names: 495 | 496 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 497 | library `Frob' (a library for tweaking knobs) written by James Random Hacker. 498 | 499 | , 1 April 1990 500 | Ty Coon, President of Vice 501 | 502 | That's all there is to it! 503 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/strongswan/davici/fdd58792ffe7c8bb9a77cd05a7d7dcdb8f78f5cc/ChangeLog -------------------------------------------------------------------------------- /Doxyfile.in: -------------------------------------------------------------------------------- 1 | # Doxyfile 1.8.9.1 2 | 3 | #--------------------------------------------------------------------------- 4 | # Project related configuration options 5 | #--------------------------------------------------------------------------- 6 | DOXYFILE_ENCODING = UTF-8 7 | PROJECT_NAME = "@PACKAGE_NAME@" 8 | PROJECT_NUMBER = "@PACKAGE_VERSION@" 9 | PROJECT_BRIEF = 10 | PROJECT_LOGO = 11 | OUTPUT_DIRECTORY = api 12 | CREATE_SUBDIRS = NO 13 | ALLOW_UNICODE_NAMES = NO 14 | OUTPUT_LANGUAGE = English 15 | BRIEF_MEMBER_DESC = YES 16 | REPEAT_BRIEF = YES 17 | ABBREVIATE_BRIEF = 18 | ALWAYS_DETAILED_SEC = NO 19 | INLINE_INHERITED_MEMB = NO 20 | FULL_PATH_NAMES = YES 21 | STRIP_FROM_PATH = 22 | STRIP_FROM_INC_PATH = 23 | SHORT_NAMES = NO 24 | JAVADOC_AUTOBRIEF = NO 25 | QT_AUTOBRIEF = NO 26 | MULTILINE_CPP_IS_BRIEF = NO 27 | INHERIT_DOCS = YES 28 | SEPARATE_MEMBER_PAGES = NO 29 | TAB_SIZE = 4 30 | ALIASES = 31 | TCL_SUBST = 32 | OPTIMIZE_OUTPUT_FOR_C = YES 33 | OPTIMIZE_OUTPUT_JAVA = NO 34 | OPTIMIZE_FOR_FORTRAN = NO 35 | OPTIMIZE_OUTPUT_VHDL = NO 36 | EXTENSION_MAPPING = 37 | MARKDOWN_SUPPORT = YES 38 | AUTOLINK_SUPPORT = YES 39 | BUILTIN_STL_SUPPORT = NO 40 | CPP_CLI_SUPPORT = NO 41 | SIP_SUPPORT = NO 42 | IDL_PROPERTY_SUPPORT = YES 43 | DISTRIBUTE_GROUP_DOC = NO 44 | SUBGROUPING = YES 45 | INLINE_GROUPED_CLASSES = NO 46 | INLINE_SIMPLE_STRUCTS = NO 47 | TYPEDEF_HIDES_STRUCT = NO 48 | LOOKUP_CACHE_SIZE = 0 49 | #--------------------------------------------------------------------------- 50 | # Build related configuration options 51 | #--------------------------------------------------------------------------- 52 | EXTRACT_ALL = NO 53 | EXTRACT_PRIVATE = NO 54 | EXTRACT_PACKAGE = NO 55 | EXTRACT_STATIC = NO 56 | EXTRACT_LOCAL_CLASSES = YES 57 | EXTRACT_LOCAL_METHODS = NO 58 | EXTRACT_ANON_NSPACES = NO 59 | HIDE_UNDOC_MEMBERS = NO 60 | HIDE_UNDOC_CLASSES = NO 61 | HIDE_FRIEND_COMPOUNDS = NO 62 | HIDE_IN_BODY_DOCS = NO 63 | INTERNAL_DOCS = NO 64 | CASE_SENSE_NAMES = YES 65 | HIDE_SCOPE_NAMES = NO 66 | HIDE_COMPOUND_REFERENCE= NO 67 | SHOW_INCLUDE_FILES = YES 68 | SHOW_GROUPED_MEMB_INC = NO 69 | FORCE_LOCAL_INCLUDES = NO 70 | INLINE_INFO = YES 71 | SORT_MEMBER_DOCS = YES 72 | SORT_BRIEF_DOCS = NO 73 | SORT_MEMBERS_CTORS_1ST = NO 74 | SORT_GROUP_NAMES = NO 75 | SORT_BY_SCOPE_NAME = NO 76 | STRICT_PROTO_MATCHING = NO 77 | GENERATE_TODOLIST = YES 78 | GENERATE_TESTLIST = YES 79 | GENERATE_BUGLIST = YES 80 | GENERATE_DEPRECATEDLIST= YES 81 | ENABLED_SECTIONS = 82 | MAX_INITIALIZER_LINES = 30 83 | SHOW_USED_FILES = YES 84 | SHOW_FILES = YES 85 | SHOW_NAMESPACES = YES 86 | FILE_VERSION_FILTER = 87 | LAYOUT_FILE = 88 | CITE_BIB_FILES = 89 | #--------------------------------------------------------------------------- 90 | # Configuration options related to warning and progress messages 91 | #--------------------------------------------------------------------------- 92 | QUIET = NO 93 | WARNINGS = YES 94 | WARN_IF_UNDOCUMENTED = YES 95 | WARN_IF_DOC_ERROR = YES 96 | WARN_NO_PARAMDOC = NO 97 | WARN_FORMAT = "$file:$line: $text" 98 | WARN_LOGFILE = 99 | #--------------------------------------------------------------------------- 100 | # Configuration options related to the input files 101 | #--------------------------------------------------------------------------- 102 | INPUT = @SRC_DIR@ 103 | INPUT_ENCODING = UTF-8 104 | FILE_PATTERNS = *.h *.md 105 | RECURSIVE = NO 106 | EXCLUDE = 107 | EXCLUDE_SYMLINKS = NO 108 | EXCLUDE_PATTERNS = 109 | EXCLUDE_SYMBOLS = 110 | EXAMPLE_PATH = 111 | EXAMPLE_PATTERNS = 112 | EXAMPLE_RECURSIVE = NO 113 | IMAGE_PATH = 114 | INPUT_FILTER = 115 | FILTER_PATTERNS = 116 | FILTER_SOURCE_FILES = NO 117 | FILTER_SOURCE_PATTERNS = 118 | USE_MDFILE_AS_MAINPAGE = README.md 119 | #--------------------------------------------------------------------------- 120 | # Configuration options related to source browsing 121 | #--------------------------------------------------------------------------- 122 | SOURCE_BROWSER = NO 123 | INLINE_SOURCES = NO 124 | STRIP_CODE_COMMENTS = YES 125 | REFERENCED_BY_RELATION = NO 126 | REFERENCES_RELATION = NO 127 | REFERENCES_LINK_SOURCE = YES 128 | SOURCE_TOOLTIPS = YES 129 | USE_HTAGS = NO 130 | VERBATIM_HEADERS = YES 131 | CLANG_ASSISTED_PARSING = NO 132 | CLANG_OPTIONS = 133 | #--------------------------------------------------------------------------- 134 | # Configuration options related to the alphabetical class index 135 | #--------------------------------------------------------------------------- 136 | ALPHABETICAL_INDEX = YES 137 | COLS_IN_ALPHA_INDEX = 5 138 | IGNORE_PREFIX = 139 | #--------------------------------------------------------------------------- 140 | # Configuration options related to the HTML output 141 | #--------------------------------------------------------------------------- 142 | GENERATE_HTML = YES 143 | HTML_OUTPUT = html 144 | HTML_FILE_EXTENSION = .html 145 | HTML_HEADER = 146 | HTML_FOOTER = 147 | HTML_STYLESHEET = 148 | HTML_EXTRA_STYLESHEET = 149 | HTML_EXTRA_FILES = 150 | HTML_COLORSTYLE_HUE = 220 151 | HTML_COLORSTYLE_SAT = 100 152 | HTML_COLORSTYLE_GAMMA = 80 153 | HTML_TIMESTAMP = YES 154 | HTML_DYNAMIC_SECTIONS = NO 155 | HTML_INDEX_NUM_ENTRIES = 100 156 | GENERATE_DOCSET = NO 157 | DOCSET_FEEDNAME = "Doxygen generated docs" 158 | DOCSET_BUNDLE_ID = org.doxygen.Project 159 | DOCSET_PUBLISHER_ID = org.doxygen.Publisher 160 | DOCSET_PUBLISHER_NAME = Publisher 161 | GENERATE_HTMLHELP = NO 162 | CHM_FILE = 163 | HHC_LOCATION = 164 | GENERATE_CHI = NO 165 | CHM_INDEX_ENCODING = 166 | BINARY_TOC = NO 167 | TOC_EXPAND = NO 168 | GENERATE_QHP = NO 169 | QCH_FILE = 170 | QHP_NAMESPACE = org.doxygen.Project 171 | QHP_VIRTUAL_FOLDER = doc 172 | QHP_CUST_FILTER_NAME = 173 | QHP_CUST_FILTER_ATTRS = 174 | QHP_SECT_FILTER_ATTRS = 175 | QHG_LOCATION = 176 | GENERATE_ECLIPSEHELP = NO 177 | ECLIPSE_DOC_ID = org.doxygen.Project 178 | DISABLE_INDEX = NO 179 | GENERATE_TREEVIEW = NO 180 | ENUM_VALUES_PER_LINE = 4 181 | TREEVIEW_WIDTH = 250 182 | EXT_LINKS_IN_WINDOW = NO 183 | FORMULA_FONTSIZE = 10 184 | FORMULA_TRANSPARENT = YES 185 | USE_MATHJAX = NO 186 | MATHJAX_FORMAT = HTML-CSS 187 | MATHJAX_RELPATH = http://cdn.mathjax.org/mathjax/latest 188 | MATHJAX_EXTENSIONS = 189 | MATHJAX_CODEFILE = 190 | SEARCHENGINE = YES 191 | SERVER_BASED_SEARCH = NO 192 | EXTERNAL_SEARCH = NO 193 | SEARCHENGINE_URL = 194 | SEARCHDATA_FILE = searchdata.xml 195 | EXTERNAL_SEARCH_ID = 196 | EXTRA_SEARCH_MAPPINGS = 197 | #--------------------------------------------------------------------------- 198 | # Configuration options related to the LaTeX output 199 | #--------------------------------------------------------------------------- 200 | GENERATE_LATEX = YES 201 | LATEX_OUTPUT = latex 202 | LATEX_CMD_NAME = latex 203 | MAKEINDEX_CMD_NAME = makeindex 204 | COMPACT_LATEX = NO 205 | PAPER_TYPE = a4 206 | EXTRA_PACKAGES = 207 | LATEX_HEADER = 208 | LATEX_FOOTER = 209 | LATEX_EXTRA_STYLESHEET = 210 | LATEX_EXTRA_FILES = 211 | PDF_HYPERLINKS = YES 212 | USE_PDFLATEX = YES 213 | LATEX_BATCHMODE = NO 214 | LATEX_HIDE_INDICES = NO 215 | LATEX_SOURCE_CODE = NO 216 | LATEX_BIB_STYLE = plain 217 | #--------------------------------------------------------------------------- 218 | # Configuration options related to the RTF output 219 | #--------------------------------------------------------------------------- 220 | GENERATE_RTF = NO 221 | RTF_OUTPUT = rtf 222 | COMPACT_RTF = NO 223 | RTF_HYPERLINKS = NO 224 | RTF_STYLESHEET_FILE = 225 | RTF_EXTENSIONS_FILE = 226 | RTF_SOURCE_CODE = NO 227 | #--------------------------------------------------------------------------- 228 | # Configuration options related to the man page output 229 | #--------------------------------------------------------------------------- 230 | GENERATE_MAN = NO 231 | MAN_OUTPUT = man 232 | MAN_EXTENSION = .3 233 | MAN_SUBDIR = 234 | MAN_LINKS = NO 235 | #--------------------------------------------------------------------------- 236 | # Configuration options related to the XML output 237 | #--------------------------------------------------------------------------- 238 | GENERATE_XML = NO 239 | XML_OUTPUT = xml 240 | XML_PROGRAMLISTING = YES 241 | #--------------------------------------------------------------------------- 242 | # Configuration options related to the DOCBOOK output 243 | #--------------------------------------------------------------------------- 244 | GENERATE_DOCBOOK = NO 245 | DOCBOOK_OUTPUT = docbook 246 | DOCBOOK_PROGRAMLISTING = NO 247 | #--------------------------------------------------------------------------- 248 | # Configuration options for the AutoGen Definitions output 249 | #--------------------------------------------------------------------------- 250 | GENERATE_AUTOGEN_DEF = NO 251 | #--------------------------------------------------------------------------- 252 | # Configuration options related to the Perl module output 253 | #--------------------------------------------------------------------------- 254 | GENERATE_PERLMOD = NO 255 | PERLMOD_LATEX = NO 256 | PERLMOD_PRETTY = YES 257 | PERLMOD_MAKEVAR_PREFIX = 258 | #--------------------------------------------------------------------------- 259 | # Configuration options related to the preprocessor 260 | #--------------------------------------------------------------------------- 261 | ENABLE_PREPROCESSING = YES 262 | MACRO_EXPANSION = NO 263 | EXPAND_ONLY_PREDEF = NO 264 | SEARCH_INCLUDES = YES 265 | INCLUDE_PATH = 266 | INCLUDE_FILE_PATTERNS = 267 | PREDEFINED = 268 | EXPAND_AS_DEFINED = 269 | SKIP_FUNCTION_MACROS = YES 270 | #--------------------------------------------------------------------------- 271 | # Configuration options related to external references 272 | #--------------------------------------------------------------------------- 273 | TAGFILES = 274 | GENERATE_TAGFILE = 275 | ALLEXTERNALS = NO 276 | EXTERNAL_GROUPS = YES 277 | EXTERNAL_PAGES = YES 278 | PERL_PATH = /usr/bin/perl 279 | #--------------------------------------------------------------------------- 280 | # Configuration options related to the dot tool 281 | #--------------------------------------------------------------------------- 282 | CLASS_DIAGRAMS = YES 283 | MSCGEN_PATH = 284 | DIA_PATH = 285 | HIDE_UNDOC_RELATIONS = YES 286 | HAVE_DOT = NO 287 | DOT_NUM_THREADS = 0 288 | DOT_FONTNAME = Helvetica 289 | DOT_FONTSIZE = 10 290 | DOT_FONTPATH = 291 | CLASS_GRAPH = YES 292 | COLLABORATION_GRAPH = YES 293 | GROUP_GRAPHS = YES 294 | UML_LOOK = NO 295 | UML_LIMIT_NUM_FIELDS = 10 296 | TEMPLATE_RELATIONS = NO 297 | INCLUDE_GRAPH = YES 298 | INCLUDED_BY_GRAPH = YES 299 | CALL_GRAPH = NO 300 | CALLER_GRAPH = NO 301 | GRAPHICAL_HIERARCHY = YES 302 | DIRECTORY_GRAPH = YES 303 | DOT_IMAGE_FORMAT = png 304 | INTERACTIVE_SVG = NO 305 | DOT_PATH = 306 | DOTFILE_DIRS = 307 | MSCFILE_DIRS = 308 | DIAFILE_DIRS = 309 | PLANTUML_JAR_PATH = 310 | PLANTUML_INCLUDE_PATH = 311 | DOT_GRAPH_MAX_NODES = 50 312 | MAX_DOT_GRAPH_DEPTH = 0 313 | DOT_TRANSPARENT = NO 314 | DOT_MULTI_TARGETS = NO 315 | GENERATE_LEGEND = YES 316 | DOT_CLEANUP = YES 317 | -------------------------------------------------------------------------------- /INSTALL: -------------------------------------------------------------------------------- 1 | Installation Instructions 2 | ************************* 3 | 4 | Copyright (C) 1994-1996, 1999-2002, 2004-2013 Free Software Foundation, 5 | Inc. 6 | 7 | Copying and distribution of this file, with or without modification, 8 | are permitted in any medium without royalty provided the copyright 9 | notice and this notice are preserved. This file is offered as-is, 10 | without warranty of any kind. 11 | 12 | Basic Installation 13 | ================== 14 | 15 | Briefly, the shell command `./configure && make && make install' 16 | should configure, build, and install this package. The following 17 | more-detailed instructions are generic; see the `README' file for 18 | instructions specific to this package. Some packages provide this 19 | `INSTALL' file but do not implement all of the features documented 20 | below. The lack of an optional feature in a given package is not 21 | necessarily a bug. More recommendations for GNU packages can be found 22 | in *note Makefile Conventions: (standards)Makefile Conventions. 23 | 24 | The `configure' shell script attempts to guess correct values for 25 | various system-dependent variables used during compilation. It uses 26 | those values to create a `Makefile' in each directory of the package. 27 | It may also create one or more `.h' files containing system-dependent 28 | definitions. Finally, it creates a shell script `config.status' that 29 | you can run in the future to recreate the current configuration, and a 30 | file `config.log' containing compiler output (useful mainly for 31 | debugging `configure'). 32 | 33 | It can also use an optional file (typically called `config.cache' 34 | and enabled with `--cache-file=config.cache' or simply `-C') that saves 35 | the results of its tests to speed up reconfiguring. Caching is 36 | disabled by default to prevent problems with accidental use of stale 37 | cache files. 38 | 39 | If you need to do unusual things to compile the package, please try 40 | to figure out how `configure' could check whether to do them, and mail 41 | diffs or instructions to the address given in the `README' so they can 42 | be considered for the next release. If you are using the cache, and at 43 | some point `config.cache' contains results you don't want to keep, you 44 | may remove or edit it. 45 | 46 | The file `configure.ac' (or `configure.in') is used to create 47 | `configure' by a program called `autoconf'. You need `configure.ac' if 48 | you want to change it or regenerate `configure' using a newer version 49 | of `autoconf'. 50 | 51 | The simplest way to compile this package is: 52 | 53 | 1. `cd' to the directory containing the package's source code and type 54 | `./configure' to configure the package for your system. 55 | 56 | Running `configure' might take a while. While running, it prints 57 | some messages telling which features it is checking for. 58 | 59 | 2. Type `make' to compile the package. 60 | 61 | 3. Optionally, type `make check' to run any self-tests that come with 62 | the package, generally using the just-built uninstalled binaries. 63 | 64 | 4. Type `make install' to install the programs and any data files and 65 | documentation. When installing into a prefix owned by root, it is 66 | recommended that the package be configured and built as a regular 67 | user, and only the `make install' phase executed with root 68 | privileges. 69 | 70 | 5. Optionally, type `make installcheck' to repeat any self-tests, but 71 | this time using the binaries in their final installed location. 72 | This target does not install anything. Running this target as a 73 | regular user, particularly if the prior `make install' required 74 | root privileges, verifies that the installation completed 75 | correctly. 76 | 77 | 6. You can remove the program binaries and object files from the 78 | source code directory by typing `make clean'. To also remove the 79 | files that `configure' created (so you can compile the package for 80 | a different kind of computer), type `make distclean'. There is 81 | also a `make maintainer-clean' target, but that is intended mainly 82 | for the package's developers. If you use it, you may have to get 83 | all sorts of other programs in order to regenerate files that came 84 | with the distribution. 85 | 86 | 7. Often, you can also type `make uninstall' to remove the installed 87 | files again. In practice, not all packages have tested that 88 | uninstallation works correctly, even though it is required by the 89 | GNU Coding Standards. 90 | 91 | 8. Some packages, particularly those that use Automake, provide `make 92 | distcheck', which can by used by developers to test that all other 93 | targets like `make install' and `make uninstall' work correctly. 94 | This target is generally not run by end users. 95 | 96 | Compilers and Options 97 | ===================== 98 | 99 | Some systems require unusual options for compilation or linking that 100 | the `configure' script does not know about. Run `./configure --help' 101 | for details on some of the pertinent environment variables. 102 | 103 | You can give `configure' initial values for configuration parameters 104 | by setting variables in the command line or in the environment. Here 105 | is an example: 106 | 107 | ./configure CC=c99 CFLAGS=-g LIBS=-lposix 108 | 109 | *Note Defining Variables::, for more details. 110 | 111 | Compiling For Multiple Architectures 112 | ==================================== 113 | 114 | You can compile the package for more than one kind of computer at the 115 | same time, by placing the object files for each architecture in their 116 | own directory. To do this, you can use GNU `make'. `cd' to the 117 | directory where you want the object files and executables to go and run 118 | the `configure' script. `configure' automatically checks for the 119 | source code in the directory that `configure' is in and in `..'. This 120 | is known as a "VPATH" build. 121 | 122 | With a non-GNU `make', it is safer to compile the package for one 123 | architecture at a time in the source code directory. After you have 124 | installed the package for one architecture, use `make distclean' before 125 | reconfiguring for another architecture. 126 | 127 | On MacOS X 10.5 and later systems, you can create libraries and 128 | executables that work on multiple system types--known as "fat" or 129 | "universal" binaries--by specifying multiple `-arch' options to the 130 | compiler but only a single `-arch' option to the preprocessor. Like 131 | this: 132 | 133 | ./configure CC="gcc -arch i386 -arch x86_64 -arch ppc -arch ppc64" \ 134 | CXX="g++ -arch i386 -arch x86_64 -arch ppc -arch ppc64" \ 135 | CPP="gcc -E" CXXCPP="g++ -E" 136 | 137 | This is not guaranteed to produce working output in all cases, you 138 | may have to build one architecture at a time and combine the results 139 | using the `lipo' tool if you have problems. 140 | 141 | Installation Names 142 | ================== 143 | 144 | By default, `make install' installs the package's commands under 145 | `/usr/local/bin', include files under `/usr/local/include', etc. You 146 | can specify an installation prefix other than `/usr/local' by giving 147 | `configure' the option `--prefix=PREFIX', where PREFIX must be an 148 | absolute file name. 149 | 150 | You can specify separate installation prefixes for 151 | architecture-specific files and architecture-independent files. If you 152 | pass the option `--exec-prefix=PREFIX' to `configure', the package uses 153 | PREFIX as the prefix for installing programs and libraries. 154 | Documentation and other data files still use the regular prefix. 155 | 156 | In addition, if you use an unusual directory layout you can give 157 | options like `--bindir=DIR' to specify different values for particular 158 | kinds of files. Run `configure --help' for a list of the directories 159 | you can set and what kinds of files go in them. In general, the 160 | default for these options is expressed in terms of `${prefix}', so that 161 | specifying just `--prefix' will affect all of the other directory 162 | specifications that were not explicitly provided. 163 | 164 | The most portable way to affect installation locations is to pass the 165 | correct locations to `configure'; however, many packages provide one or 166 | both of the following shortcuts of passing variable assignments to the 167 | `make install' command line to change installation locations without 168 | having to reconfigure or recompile. 169 | 170 | The first method involves providing an override variable for each 171 | affected directory. For example, `make install 172 | prefix=/alternate/directory' will choose an alternate location for all 173 | directory configuration variables that were expressed in terms of 174 | `${prefix}'. Any directories that were specified during `configure', 175 | but not in terms of `${prefix}', must each be overridden at install 176 | time for the entire installation to be relocated. The approach of 177 | makefile variable overrides for each directory variable is required by 178 | the GNU Coding Standards, and ideally causes no recompilation. 179 | However, some platforms have known limitations with the semantics of 180 | shared libraries that end up requiring recompilation when using this 181 | method, particularly noticeable in packages that use GNU Libtool. 182 | 183 | The second method involves providing the `DESTDIR' variable. For 184 | example, `make install DESTDIR=/alternate/directory' will prepend 185 | `/alternate/directory' before all installation names. The approach of 186 | `DESTDIR' overrides is not required by the GNU Coding Standards, and 187 | does not work on platforms that have drive letters. On the other hand, 188 | it does better at avoiding recompilation issues, and works well even 189 | when some directory options were not specified in terms of `${prefix}' 190 | at `configure' time. 191 | 192 | Optional Features 193 | ================= 194 | 195 | If the package supports it, you can cause programs to be installed 196 | with an extra prefix or suffix on their names by giving `configure' the 197 | option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. 198 | 199 | Some packages pay attention to `--enable-FEATURE' options to 200 | `configure', where FEATURE indicates an optional part of the package. 201 | They may also pay attention to `--with-PACKAGE' options, where PACKAGE 202 | is something like `gnu-as' or `x' (for the X Window System). The 203 | `README' should mention any `--enable-' and `--with-' options that the 204 | package recognizes. 205 | 206 | For packages that use the X Window System, `configure' can usually 207 | find the X include and library files automatically, but if it doesn't, 208 | you can use the `configure' options `--x-includes=DIR' and 209 | `--x-libraries=DIR' to specify their locations. 210 | 211 | Some packages offer the ability to configure how verbose the 212 | execution of `make' will be. For these packages, running `./configure 213 | --enable-silent-rules' sets the default to minimal output, which can be 214 | overridden with `make V=1'; while running `./configure 215 | --disable-silent-rules' sets the default to verbose, which can be 216 | overridden with `make V=0'. 217 | 218 | Particular systems 219 | ================== 220 | 221 | On HP-UX, the default C compiler is not ANSI C compatible. If GNU 222 | CC is not installed, it is recommended to use the following options in 223 | order to use an ANSI C compiler: 224 | 225 | ./configure CC="cc -Ae -D_XOPEN_SOURCE=500" 226 | 227 | and if that doesn't work, install pre-built binaries of GCC for HP-UX. 228 | 229 | HP-UX `make' updates targets which have the same time stamps as 230 | their prerequisites, which makes it generally unusable when shipped 231 | generated files such as `configure' are involved. Use GNU `make' 232 | instead. 233 | 234 | On OSF/1 a.k.a. Tru64, some versions of the default C compiler cannot 235 | parse its `' header file. The option `-nodtk' can be used as 236 | a workaround. If GNU CC is not installed, it is therefore recommended 237 | to try 238 | 239 | ./configure CC="cc" 240 | 241 | and if that doesn't work, try 242 | 243 | ./configure CC="cc -nodtk" 244 | 245 | On Solaris, don't put `/usr/ucb' early in your `PATH'. This 246 | directory contains several dysfunctional programs; working variants of 247 | these programs are available in `/usr/bin'. So, if you need `/usr/ucb' 248 | in your `PATH', put it _after_ `/usr/bin'. 249 | 250 | On Haiku, software installed for all users goes in `/boot/common', 251 | not `/usr/local'. It is recommended to use the following options: 252 | 253 | ./configure --prefix=/boot/common 254 | 255 | Specifying the System Type 256 | ========================== 257 | 258 | There may be some features `configure' cannot figure out 259 | automatically, but needs to determine by the type of machine the package 260 | will run on. Usually, assuming the package is built to be run on the 261 | _same_ architectures, `configure' can figure that out, but if it prints 262 | a message saying it cannot guess the machine type, give it the 263 | `--build=TYPE' option. TYPE can either be a short name for the system 264 | type, such as `sun4', or a canonical name which has the form: 265 | 266 | CPU-COMPANY-SYSTEM 267 | 268 | where SYSTEM can have one of these forms: 269 | 270 | OS 271 | KERNEL-OS 272 | 273 | See the file `config.sub' for the possible values of each field. If 274 | `config.sub' isn't included in this package, then this package doesn't 275 | need to know the machine type. 276 | 277 | If you are _building_ compiler tools for cross-compiling, you should 278 | use the option `--target=TYPE' to select the type of system they will 279 | produce code for. 280 | 281 | If you want to _use_ a cross compiler, that generates code for a 282 | platform different from the build platform, you should specify the 283 | "host" platform (i.e., that on which the generated programs will 284 | eventually be run) with `--host=TYPE'. 285 | 286 | Sharing Defaults 287 | ================ 288 | 289 | If you want to set default values for `configure' scripts to share, 290 | you can create a site shell script called `config.site' that gives 291 | default values for variables like `CC', `cache_file', and `prefix'. 292 | `configure' looks for `PREFIX/share/config.site' if it exists, then 293 | `PREFIX/etc/config.site' if it exists. Or, you can set the 294 | `CONFIG_SITE' environment variable to the location of the site script. 295 | A warning: not all `configure' scripts look for a site script. 296 | 297 | Defining Variables 298 | ================== 299 | 300 | Variables not defined in a site shell script can be set in the 301 | environment passed to `configure'. However, some packages may run 302 | configure again during the build, and the customized values of these 303 | variables may be lost. In order to avoid this problem, you should set 304 | them in the `configure' command line, using `VAR=value'. For example: 305 | 306 | ./configure CC=/usr/local2/bin/gcc 307 | 308 | causes the specified `gcc' to be used as the C compiler (unless it is 309 | overridden in the site shell script). 310 | 311 | Unfortunately, this technique does not work for `CONFIG_SHELL' due to 312 | an Autoconf limitation. Until the limitation is lifted, you can use 313 | this workaround: 314 | 315 | CONFIG_SHELL=/bin/bash ./configure CONFIG_SHELL=/bin/bash 316 | 317 | `configure' Invocation 318 | ====================== 319 | 320 | `configure' recognizes the following options to control how it 321 | operates. 322 | 323 | `--help' 324 | `-h' 325 | Print a summary of all of the options to `configure', and exit. 326 | 327 | `--help=short' 328 | `--help=recursive' 329 | Print a summary of the options unique to this package's 330 | `configure', and exit. The `short' variant lists options used 331 | only in the top level, while the `recursive' variant lists options 332 | also present in any nested packages. 333 | 334 | `--version' 335 | `-V' 336 | Print the version of Autoconf used to generate the `configure' 337 | script, and exit. 338 | 339 | `--cache-file=FILE' 340 | Enable the cache: use and save the results of the tests in FILE, 341 | traditionally `config.cache'. FILE defaults to `/dev/null' to 342 | disable caching. 343 | 344 | `--config-cache' 345 | `-C' 346 | Alias for `--cache-file=config.cache'. 347 | 348 | `--quiet' 349 | `--silent' 350 | `-q' 351 | Do not print messages saying which checks are being made. To 352 | suppress all normal output, redirect it to `/dev/null' (any error 353 | messages will still be shown). 354 | 355 | `--srcdir=DIR' 356 | Look for the package's source code in directory DIR. Usually 357 | `configure' can determine that directory automatically. 358 | 359 | `--prefix=DIR' 360 | Use DIR as the installation prefix. *note Installation Names:: 361 | for more details, including other options available for fine-tuning 362 | the installation locations. 363 | 364 | `--no-create' 365 | `-n' 366 | Run the configure checks, but stop before creating any output 367 | files. 368 | 369 | `configure' also accepts some other, not widely useful, options. Run 370 | `configure --help' for more details. 371 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = . tests examples 2 | 3 | ACLOCAL_AMFLAGS = -I m4 4 | AM_MAKEFLAGS = -s 5 | 6 | lib_LTLIBRARIES = libdavici.la 7 | 8 | # current:revision:age, do all of the following for each release: 9 | # source changes: revision++ 10 | # interface added, removed, or changed: current++, revision = 0 11 | # interfaces added: age++ 12 | # interfaces removed: age = 0 13 | libdavici_la_LDFLAGS = -version-info 1:0:1 14 | 15 | libdavici_la_SOURCES = \ 16 | davici.c 17 | 18 | nobase_include_HEADERS = \ 19 | davici.h 20 | 21 | AM_CFLAGS = -Wall 22 | 23 | clean-local: cov-reset 24 | @find $(top_builddir) -name "*.gcno" -delete 25 | rm -rf $(top_builddir)/api 26 | 27 | cov-reset: 28 | @rm -rf $(top_builddir)/coverage 29 | @find $(top_builddir) -name "*.gcda" -delete 30 | @lcov --directory $(top_builddir) --zerocounters || true 31 | 32 | cov-makereport: cov-reset check 33 | @mkdir $(top_builddir)/coverage 34 | lcov -c -o $(top_builddir)/coverage/coverage.info -d $(top_builddir) \ 35 | --rc lcov_branch_coverage=1 36 | lcov -r $(top_builddir)/coverage/coverage.info '*/tests/*' \ 37 | -r $(top_builddir)/coverage/coverage.info '/usr/include/*' \ 38 | -o $(top_builddir)/coverage/coverage.cleaned.info \ 39 | --rc lcov_branch_coverage=1 40 | genhtml --num-spaces 4 --legend --branch-coverage --ignore-errors source \ 41 | -t "$(PACKAGE_STRING)" -o $(top_builddir)/coverage/html \ 42 | -p $$(readlink -m $(abs_top_srcdir)) \ 43 | $(top_builddir)/coverage/coverage.cleaned.info 44 | 45 | cov-report: cov-makereport 46 | xdg-open $(top_builddir)/coverage/html/index.html 47 | 48 | doxygen: Doxyfile 49 | @test -d $(top_builddir)/api || doxygen 50 | @! find Doxyfile $(top_srcdir) \ 51 | \( -name '*.h' -o -name '*.md' \) -newer $(top_builddir)/api | \ 52 | grep -q '' || doxygen && touch $(top_builddir)/api 53 | 54 | Doxyfile: Doxyfile.in 55 | $(AM_V_GEN) \ 56 | sed \ 57 | -e "s:\@PACKAGE_VERSION\@:$(PACKAGE_VERSION):" \ 58 | -e "s:\@PACKAGE_NAME\@:$(PACKAGE_NAME):" \ 59 | -e "s:\@SRC_DIR\@:$(srcdir)/README.md $(srcdir)/davici.h:g" \ 60 | $(srcdir)/$@.in > $@ 61 | 62 | CLEANFILES = Doxyfile 63 | 64 | EXTRA_DIST = Doxyfile.in 65 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- 1 | # Release notes # 2 | 3 | ## Version 1.4 - 2021-09-07 ## 4 | 5 | - Add support for edge triggered file descriptor dispatching 6 | - Migrate CI from Travis to GitHub Actions 7 | - Add a first trivial usage example showing poll() integration 8 | 9 | 10 | ## Version 1.3 - 2017-01-20 ## 11 | 12 | - Introduce recursive message parsing using callbacks 13 | - Add scanf() based value conversion helper functions 14 | - Some tweaks for C99 compatibility 15 | 16 | 17 | ## Version 1.2 - 2017-01-05 ## 18 | 19 | - Add Travis CI support 20 | - Add support for macOS and other BSDs 21 | 22 | 23 | ## Version 1.1 - 2016-06-6 ## 24 | 25 | - Misc minor fixes 26 | - Wrap davici.h in extern C for C++ users 27 | 28 | 29 | ## Version 1.0 - 2015-12-15 ## 30 | 31 | - Initial public LGPLv2 licensed release 32 | 33 | 34 | ## Version 0.1 - 2015-11-06 ## 35 | 36 | - First release of davici asynchronous VICI client library 37 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | README.md -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # davici - Decoupled Asynchronous VICI library # 2 | 3 | The strongSwan VICI interface is an RPC-like interface to configure, monitor and 4 | control the IKE daemon charon. It is implemented in the vici plugin and used 5 | by the swanctl configuration backend. VICI stands for Versatile IKE 6 | Configuration Interface, details about the protocol are provided in the 7 | strongSwan documentation. 8 | 9 | strongSwan ships client libraries for the VICI protocol implemented in Ruby 10 | and Python, and the libvici library provides a client side implementation 11 | of the protocol in C. The libvici library, however, uses the libstrongswan 12 | base library, which is not well suited for integration in other software 13 | stacks: libstrongswan is GPLv2-licensed, so is libvici, and it makes use of 14 | threads for asynchronous event delivery. 15 | 16 | davici is an alternative implementation of the VICI client protocol, targeting 17 | better integration in other software stacks. It uses an asynchronous, 18 | non-blocking API and can be integrated in third-party main dispatching loops 19 | without the use of threads. davici is a very low-level library intended as a 20 | foundation to build higher level control interfaces for strongSwan. 21 | 22 | ## I/O dispatching and main loop integration ## 23 | 24 | davici requires integration in an asynchronous I/O dispatching framework 25 | with file descriptors, such as ``select()``, ``poll()`` or similar interfaces. 26 | Higher level main loop processing APIs can use davici as well, as long as they 27 | provide a mechanism to watch for file descriptor state changes. 28 | 29 | When creating a VICI connection with davici, the constructor takes a 30 | ``davici_fdcb`` callback function. Any davici function may invoke the passed 31 | callback function to perform I/O dispatching registration. The callback 32 | receives a file descriptor, and a request for a set of file descriptor 33 | operations to monitor. That information must be used to add or update file 34 | descriptor watch operations using ``select()``, ``poll()`` or similar calls. 35 | 36 | When dispatching the passed file descriptor, the user code shall call 37 | ``davici_read()`` if the file descriptor is read-ready, and ``davici_write()`` 38 | if the file descriptor is write-ready. Both calls are non-blocking, and on 39 | failure the VICI connection shall be closed using ``davici_disconnect()``. 40 | 41 | ## Use in multithreaded code ## 42 | 43 | davici is not thread safe in the sense that multiple threads may operate on 44 | a single connection concurrently. It does not perform any locking. Individual 45 | threads may operate on individual connections, though, and multiple threads 46 | may also use a single connection if any call to davici functions is 47 | synchronized to a single concurrent thread. 48 | 49 | ## Invoking commands ## 50 | 51 | Commands are client-initiated exchanges including a request message sent by 52 | the client and a response message sent by the server. The ``davici_new_cmd()`` 53 | function creates a new and empty request message for a specific command, which 54 | then may be populated using the different functions provided by davici. 55 | 56 | Once the request is complete (and balanced in regards to sections/lists), it may 57 | be queued for execution using ``davici_queue()``. The provided user callback is 58 | invoked asynchronously once the server response is received or the exchange 59 | failed. If a valid response has been received, a non-negative error value is 60 | passed to the callback together with the response message. An arbitrary number 61 | of requests may be queued, but they do get executed synchronized one by one on 62 | a single connection. 63 | 64 | Within the response callback the user may parse the response message using 65 | ``davici_parse()`` and associated functions. The function implements an 66 | iterative parser to process any kind of response message. 67 | 68 | ## Streaming command response ## 69 | 70 | Some commands in the VICI protocol use response streaming, that is, upon 71 | receiving a request the vici plugin starts sending related event messages. 72 | The end of the event stream is indicated by the response message matching 73 | the request. 74 | 75 | The ``davici_queue_streamed()`` function queues a request for streamed response 76 | processing. It takes an additional event callback that is invoked for each 77 | streamed response. The event callback is also invoked for the implicit event 78 | registration with a NULL response to indicate any registration error. 79 | 80 | As with the command response callback the user may use ``davici_parse()`` to 81 | process messages in the event callback. One may even use the same callback 82 | function for both usages, but care must be taken to properly handle the 83 | registration/deregistration invocations properly. 84 | 85 | ## Event handling ## 86 | 87 | To register for normal events, the ``davici_register()`` and 88 | ``davici_unregister()`` functions may be used. Both event registration and 89 | deregistration are confirmed (asynchronously) by invoking the event callback 90 | with a NULL response message. The error code indicates any failure, and should 91 | be checked in all invocations. 92 | 93 | To parse event messages, the callback may invoke ``davici_parse()`` and related 94 | functions. 95 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | AC_PREREQ([2.69]) 2 | AC_INIT([davici], [1.4]) 3 | AC_CONFIG_SRCDIR([tests]) 4 | AC_CONFIG_HEADERS([config.h]) 5 | AC_CONFIG_MACRO_DIR([m4]) 6 | AM_INIT_AUTOMAKE([tar-ustar subdir-objects]) 7 | AM_SILENT_RULES([yes]) 8 | 9 | LT_INIT 10 | AC_PROG_CC 11 | 12 | AC_CONFIG_FILES([ 13 | Makefile 14 | tests/Makefile 15 | examples/Makefile 16 | ]) 17 | AC_OUTPUT 18 | -------------------------------------------------------------------------------- /davici.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 onway ag 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library 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 GNU 12 | * Lesser General Public License for more details. 13 | */ 14 | 15 | #include "davici.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | /* buffer size for a name tag */ 32 | #define NAME_BUF_LEN (UCHAR_MAX + 1) 33 | 34 | enum davici_packet_type { 35 | DAVICI_CMD_REQUEST = 0, 36 | DAVICI_CMD_RESPONSE = 1, 37 | DAVICI_CMD_UNKNOWN = 2, 38 | DAVICI_EVENT_REGISTER = 3, 39 | DAVICI_EVENT_UNREGISTER = 4, 40 | DAVICI_EVENT_CONFIRM = 5, 41 | DAVICI_EVENT_UNKNOWN = 6, 42 | DAVICI_EVENT = 7, 43 | }; 44 | 45 | struct davici_request { 46 | struct davici_request *next; 47 | unsigned int allocated; 48 | unsigned int used; 49 | unsigned int sent; 50 | char *buf; 51 | int err; 52 | davici_cb cb; 53 | void *user; 54 | }; 55 | 56 | struct davici_packet { 57 | unsigned int received; 58 | char len[sizeof(uint32_t)]; 59 | char *buf; 60 | }; 61 | 62 | struct davici_response { 63 | struct davici_packet *pkt; 64 | unsigned int pos; 65 | unsigned int buflen; 66 | void *buf; 67 | char name[NAME_BUF_LEN]; 68 | unsigned int section; 69 | unsigned int list; 70 | }; 71 | 72 | struct davici_event { 73 | struct davici_event *next; 74 | davici_cb cb; 75 | void *user; 76 | char name[0]; 77 | }; 78 | 79 | struct davici_conn { 80 | int s; 81 | struct davici_request *reqs; 82 | struct davici_event *events; 83 | struct davici_packet pkt; 84 | davici_fdcb fdcb; 85 | void *user; 86 | enum davici_fdops ops; 87 | }; 88 | 89 | static int connect_and_fcntl(int fd, const char *path) 90 | { 91 | struct sockaddr_un addr; 92 | int len, flags; 93 | 94 | memset(&addr, 0, sizeof(addr)); 95 | addr.sun_family = AF_UNIX; 96 | snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path); 97 | len = offsetof(struct sockaddr_un, sun_path) + strlen(addr.sun_path); 98 | 99 | if (connect(fd, (struct sockaddr*)&addr, len) != 0) 100 | { 101 | return -errno; 102 | } 103 | flags = fcntl(fd, F_GETFL); 104 | if (flags == -1) 105 | { 106 | return -errno; 107 | } 108 | #ifdef O_CLOEXEC 109 | flags |= O_CLOEXEC; 110 | #endif 111 | if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) 112 | { 113 | return -errno; 114 | } 115 | return 0; 116 | } 117 | 118 | int davici_connect_unix(const char *path, davici_fdcb fdcb, void *user, 119 | struct davici_conn **cp) 120 | { 121 | struct davici_conn *c; 122 | int err; 123 | 124 | c = calloc(1, sizeof(*c)); 125 | if (!c) 126 | { 127 | return -errno; 128 | } 129 | c->fdcb = fdcb; 130 | c->user = user; 131 | 132 | c->s = socket(AF_UNIX, SOCK_STREAM, 0); 133 | if (c->s < 0) 134 | { 135 | err = -errno; 136 | free(c); 137 | return err; 138 | } 139 | err = connect_and_fcntl(c->s, path); 140 | if (err < 0) 141 | { 142 | close(c->s); 143 | free(c); 144 | return err; 145 | } 146 | 147 | *cp = c; 148 | return 0; 149 | } 150 | 151 | static unsigned int max(unsigned int a, unsigned int b) 152 | { 153 | return a > b ? a : b; 154 | } 155 | 156 | static int update_ops(struct davici_conn *c, enum davici_fdops ops) 157 | { 158 | int ret; 159 | 160 | if (ops == c->ops) 161 | { 162 | return 0; 163 | } 164 | ret = c->fdcb(c, c->s, ops, c->user); 165 | if (ret == 0) 166 | { 167 | c->ops = ops; 168 | } 169 | return -abs(ret); 170 | } 171 | 172 | static int copy_name(char *out, unsigned int outlen, 173 | const char *in, unsigned int inlen) 174 | { 175 | int i; 176 | 177 | if (inlen >= outlen) 178 | { 179 | return -ENOBUFS; 180 | } 181 | for (i = 0; i < inlen; i++) 182 | { 183 | if (!isprint((unsigned char)in[i])) 184 | { 185 | return -EINVAL; 186 | } 187 | } 188 | memcpy(out, in, inlen); 189 | out[inlen] = '\0'; 190 | return 0; 191 | } 192 | 193 | static struct davici_request* pop_request(struct davici_conn *c, 194 | enum davici_packet_type type, 195 | char *name, unsigned int namelen) 196 | { 197 | struct davici_request *req; 198 | 199 | req = c->reqs; 200 | if (!req || !req->cb || req->used < 2 || 201 | req->buf[0] != type || req->used - 2 < req->buf[1]) 202 | { 203 | return NULL; 204 | } 205 | if (copy_name(name, namelen, req->buf + 2, req->buf[1]) < 0) 206 | { 207 | return NULL; 208 | } 209 | c->reqs = req->next; 210 | return req; 211 | } 212 | 213 | static void destroy_request(struct davici_request *req) 214 | { 215 | free(req->buf); 216 | free(req); 217 | } 218 | 219 | static int handle_cmd_response(struct davici_conn *c, struct davici_packet *pkt) 220 | { 221 | struct davici_request *req; 222 | struct davici_response res = { 223 | .pkt = pkt, 224 | }; 225 | char name[NAME_BUF_LEN]; 226 | 227 | req = pop_request(c, DAVICI_CMD_REQUEST, name, sizeof(name)); 228 | if (!req) 229 | { 230 | return -EBADMSG; 231 | } 232 | 233 | req->cb(c, 0, name, &res, req->user); 234 | destroy_request(req); 235 | return 0; 236 | } 237 | 238 | static int handle_cmd_unknown(struct davici_conn *c) 239 | { 240 | struct davici_request *req; 241 | char name[NAME_BUF_LEN]; 242 | 243 | req = pop_request(c, DAVICI_CMD_REQUEST, name, sizeof(name)); 244 | if (!req) 245 | { 246 | return -EBADMSG; 247 | } 248 | 249 | req->cb(c, -ENOSYS, name, NULL, req->user); 250 | destroy_request(req); 251 | return 0; 252 | } 253 | 254 | static int handle_event_unknown(struct davici_conn *c) 255 | { 256 | struct davici_request *req; 257 | char name[NAME_BUF_LEN]; 258 | 259 | req = pop_request(c, DAVICI_EVENT_REGISTER, name, sizeof(name)); 260 | if (!req) 261 | { 262 | req = pop_request(c, DAVICI_EVENT_UNREGISTER, name, sizeof(name)); 263 | } 264 | if (!req) 265 | { 266 | return -EBADMSG; 267 | } 268 | 269 | req->cb(c, -ENOENT, name, NULL, req->user); 270 | destroy_request(req); 271 | return 0; 272 | } 273 | 274 | static int remove_event(struct davici_conn *c, const char *name, davici_cb cb) 275 | { 276 | struct davici_event *ev, *prev = NULL; 277 | 278 | ev = c->events; 279 | while (ev) 280 | { 281 | if (strcmp(ev->name, name) == 0 && ev->cb == cb) 282 | { 283 | if (prev) 284 | { 285 | prev->next = ev->next; 286 | } 287 | else 288 | { 289 | c->events = ev->next; 290 | } 291 | free(ev); 292 | return 0; 293 | } 294 | prev = ev; 295 | ev = ev->next; 296 | } 297 | return -ENOENT; 298 | } 299 | 300 | static int add_event(struct davici_conn *c, const char *name, 301 | davici_cb cb, void *user) 302 | { 303 | struct davici_event *ev; 304 | int len; 305 | 306 | len = strlen(name); 307 | ev = calloc(sizeof(*ev) + len + 1, 1); 308 | if (!ev) 309 | { 310 | return -errno; 311 | } 312 | memcpy(ev->name, name, len); 313 | ev->cb = cb; 314 | ev->user = user; 315 | ev->next = c->events; 316 | c->events = ev; 317 | return 0; 318 | } 319 | 320 | static int handle_event_confirm(struct davici_conn *c) 321 | { 322 | struct davici_request *req; 323 | char name[NAME_BUF_LEN]; 324 | int err; 325 | 326 | req = pop_request(c, DAVICI_EVENT_REGISTER, name, sizeof(name)); 327 | if (req) 328 | { 329 | err = add_event(c, name, req->cb, req->user); 330 | } 331 | else 332 | { 333 | req = pop_request(c, DAVICI_EVENT_UNREGISTER, name, sizeof(name)); 334 | if (req) 335 | { 336 | err = remove_event(c, name, req->cb); 337 | } 338 | else 339 | { 340 | return -EBADMSG; 341 | } 342 | } 343 | req->cb(c, err, name, NULL, req->user); 344 | destroy_request(req); 345 | return 0; 346 | } 347 | 348 | static int handle_event(struct davici_conn *c, struct davici_packet *pkt) 349 | { 350 | struct davici_packet inner = { 351 | .buf = pkt->buf + 1 + pkt->buf[0], 352 | .received = pkt->received - 1 - pkt->buf[0], 353 | }; 354 | struct davici_response res = { 355 | .pkt = &inner, 356 | }; 357 | struct davici_event *ev; 358 | char name[NAME_BUF_LEN]; 359 | int err; 360 | 361 | if (!pkt->received || pkt->buf[0] >= c->pkt.received - 1) 362 | { 363 | return -EBADMSG; 364 | } 365 | err = copy_name(name, sizeof(name), pkt->buf + 1, pkt->buf[0]); 366 | if (err < 0) 367 | { 368 | return err; 369 | } 370 | ev = c->events; 371 | while (ev) 372 | { 373 | if (strcmp(name, ev->name) == 0) 374 | { 375 | ev->cb(c, 0, ev->name, &res, ev->user); 376 | } 377 | ev = ev->next; 378 | } 379 | return 0; 380 | } 381 | 382 | static int handle_message(struct davici_conn *c) 383 | { 384 | struct davici_packet pkt = { 385 | .buf = c->pkt.buf + 1, 386 | .received = c->pkt.received - sizeof(c->pkt.len) - 1, 387 | }; 388 | 389 | switch (c->pkt.buf[0]) 390 | { 391 | case DAVICI_CMD_RESPONSE: 392 | return handle_cmd_response(c, &pkt); 393 | case DAVICI_CMD_UNKNOWN: 394 | return handle_cmd_unknown(c); 395 | case DAVICI_EVENT_UNKNOWN: 396 | return handle_event_unknown(c); 397 | case DAVICI_EVENT_CONFIRM: 398 | return handle_event_confirm(c); 399 | case DAVICI_EVENT: 400 | return handle_event(c, &pkt); 401 | default: 402 | return 0; 403 | } 404 | } 405 | 406 | int davici_read(struct davici_conn *c) 407 | { 408 | uint32_t size; 409 | int len, err = 0; 410 | 411 | while (!err) 412 | { 413 | while (c->pkt.received < sizeof(c->pkt.len)) 414 | { 415 | len = recv(c->s, c->pkt.len + c->pkt.received, 416 | sizeof(c->pkt.len) - c->pkt.received, 0); 417 | if (len == -1) 418 | { 419 | if (errno == EWOULDBLOCK || errno == EINTR) 420 | { 421 | return 0; 422 | } 423 | return -errno; 424 | } 425 | if (len == 0) 426 | { 427 | return -ECONNRESET; 428 | } 429 | c->pkt.received += len; 430 | } 431 | memcpy(&size, c->pkt.len, sizeof(size)); 432 | size = ntohl(size); 433 | if (!c->pkt.buf) 434 | { 435 | c->pkt.buf = malloc(size); 436 | if (!c->pkt.buf) 437 | { 438 | return -errno; 439 | } 440 | } 441 | while (c->pkt.received < size + sizeof(c->pkt.len)) 442 | { 443 | len = recv(c->s, c->pkt.buf + c->pkt.received - sizeof(c->pkt.len), 444 | size - (c->pkt.received - sizeof(c->pkt.len)), 0); 445 | if (len == -1) 446 | { 447 | if (errno == EWOULDBLOCK || errno == EINTR) 448 | { 449 | return 0; 450 | } 451 | return -errno; 452 | } 453 | if (len == 0) 454 | { 455 | return -ECONNRESET; 456 | } 457 | c->pkt.received += len; 458 | } 459 | if (size) 460 | { 461 | err = handle_message(c); 462 | } 463 | else 464 | { 465 | err = 0; 466 | } 467 | free(c->pkt.buf); 468 | c->pkt.buf = NULL; 469 | c->pkt.received = 0; 470 | } 471 | return err; 472 | } 473 | 474 | int davici_write(struct davici_conn *c) 475 | { 476 | struct davici_request *req; 477 | uint32_t size; 478 | int len, err; 479 | 480 | req = c->reqs; 481 | while (req) 482 | { 483 | while (req->sent < sizeof(req->used)) 484 | { 485 | size = htonl(req->used); 486 | len = send(c->s, (char*)&size + req->sent, 487 | sizeof(size) - req->sent, 0); 488 | if (len == -1) 489 | { 490 | if (errno == EWOULDBLOCK || errno == EINTR) 491 | { 492 | return 0; 493 | } 494 | return -errno; 495 | } 496 | req->sent += len; 497 | } 498 | while (req->sent < req->used + sizeof(size)) 499 | { 500 | len = send(c->s, req->buf + req->sent - sizeof(size), 501 | req->used - (req->sent - sizeof(size)), 0); 502 | if (len == -1) 503 | { 504 | if (errno == EWOULDBLOCK || errno == EINTR) 505 | { 506 | return 0; 507 | } 508 | return -errno; 509 | } 510 | req->sent += len; 511 | } 512 | err = update_ops(c, c->ops | DAVICI_READ); 513 | if (err) 514 | { 515 | return err; 516 | } 517 | req = req->next; 518 | } 519 | return update_ops(c, c->ops & ~DAVICI_WRITE); 520 | } 521 | 522 | void davici_disconnect(struct davici_conn *c) 523 | { 524 | struct davici_event *event; 525 | struct davici_request *req; 526 | void *next; 527 | 528 | update_ops(c, 0); 529 | 530 | event = c->events; 531 | while (event) 532 | { 533 | next = event->next; 534 | free(event); 535 | event = next; 536 | } 537 | req = c->reqs; 538 | while (req) 539 | { 540 | next = req->next; 541 | free(req->buf); 542 | free(req); 543 | req = next; 544 | } 545 | close(c->s); 546 | free(c); 547 | } 548 | 549 | static int create_request(enum davici_packet_type type, const char *name, 550 | struct davici_request **rp) 551 | { 552 | struct davici_request *req; 553 | int err; 554 | 555 | req = calloc(1, sizeof(*req)); 556 | if (!req) 557 | { 558 | return -errno; 559 | } 560 | req->used = 2; 561 | if (name) 562 | { 563 | req->used += strlen(name); 564 | } 565 | req->allocated = max(32, req->used); 566 | req->buf = malloc(req->allocated); 567 | if (!req->buf) 568 | { 569 | err = -errno; 570 | free(req); 571 | return err; 572 | } 573 | req->buf[0] = type; 574 | req->buf[1] = req->used - 2; 575 | if (name) 576 | { 577 | memcpy(req->buf + 2, name, req->used - 2); 578 | } 579 | *rp = req; 580 | return 0; 581 | } 582 | 583 | int davici_new_cmd(const char *cmd, struct davici_request **rp) 584 | { 585 | return create_request(DAVICI_CMD_REQUEST, cmd, rp); 586 | } 587 | 588 | static void* add_element(struct davici_request *r, enum davici_element type, 589 | unsigned int size) 590 | { 591 | unsigned int newlen; 592 | void *ret, *new; 593 | 594 | if (r->used + size + 1 > r->allocated) 595 | { 596 | newlen = r->allocated; 597 | while (newlen < r->used + size + 1) 598 | { 599 | newlen *= 2; 600 | } 601 | new = realloc(r->buf, newlen); 602 | if (!new) 603 | { 604 | r->err = -errno; 605 | return NULL; 606 | } 607 | r->buf = new; 608 | r->allocated = newlen; 609 | } 610 | r->buf[r->used++] = type; 611 | ret = r->buf + r->used; 612 | r->used += size; 613 | return ret; 614 | } 615 | 616 | void davici_section_start(struct davici_request *r, const char *name) 617 | { 618 | uint8_t nlen; 619 | char *pos; 620 | 621 | nlen = strlen(name); 622 | pos = add_element(r, DAVICI_SECTION_START, 1 + nlen); 623 | if (pos) 624 | { 625 | pos[0] = nlen; 626 | memcpy(pos + 1, name, nlen); 627 | } 628 | } 629 | 630 | void davici_section_end(struct davici_request *r) 631 | { 632 | add_element(r, DAVICI_SECTION_END, 0); 633 | } 634 | 635 | void davici_kv(struct davici_request *r, const char *name, 636 | const void *buf, unsigned int buflen) 637 | { 638 | uint8_t nlen; 639 | uint16_t vlen; 640 | char *pos; 641 | 642 | nlen = strlen(name); 643 | pos = add_element(r, DAVICI_KEY_VALUE, 1 + nlen + sizeof(vlen) + buflen); 644 | if (pos) 645 | { 646 | pos[0] = nlen; 647 | memcpy(pos + 1, name, nlen); 648 | vlen = htons(buflen); 649 | memcpy(pos + 1 + nlen, &vlen, sizeof(vlen)); 650 | memcpy(pos + 1 + nlen + sizeof(vlen), buf, buflen); 651 | } 652 | } 653 | 654 | void davici_kvf(struct davici_request *r, const char *name, 655 | const char *fmt, ...) 656 | { 657 | va_list args; 658 | 659 | va_start(args, fmt); 660 | davici_vkvf(r, name, fmt, args); 661 | va_end(args); 662 | } 663 | 664 | void davici_vkvf(struct davici_request *r, const char *name, 665 | const char *fmt, va_list args) 666 | { 667 | char buf[512], *m = buf; 668 | va_list copy; 669 | int len; 670 | 671 | va_copy(copy, args); 672 | len = vsnprintf(buf, sizeof(buf), fmt, copy); 673 | va_end(copy); 674 | if (len >= sizeof(buf)) 675 | { 676 | m = malloc(len + 1); 677 | if (m) 678 | { 679 | len = vsnprintf(m, len + 1, fmt, args); 680 | } 681 | else 682 | { 683 | len = -1; 684 | } 685 | } 686 | if (len < 0) 687 | { 688 | r->err = -errno; 689 | } 690 | else 691 | { 692 | davici_kv(r, name, m, len); 693 | } 694 | if (m != buf) 695 | { 696 | free(m); 697 | } 698 | } 699 | 700 | void davici_list_start(struct davici_request *r, const char *name) 701 | { 702 | uint8_t nlen; 703 | char *pos; 704 | 705 | nlen = strlen(name); 706 | pos = add_element(r, DAVICI_LIST_START, 1 + nlen); 707 | if (pos) 708 | { 709 | pos[0] = nlen; 710 | memcpy(pos + 1, name, nlen); 711 | } 712 | } 713 | 714 | void davici_list_item(struct davici_request *r, const void *buf, 715 | unsigned int buflen) 716 | { 717 | uint16_t vlen; 718 | char *pos; 719 | 720 | pos = add_element(r, DAVICI_LIST_ITEM, sizeof(vlen) + buflen); 721 | if (pos) 722 | { 723 | vlen = htons(buflen); 724 | memcpy(pos, &vlen, sizeof(vlen)); 725 | memcpy(pos + sizeof(vlen), buf, buflen); 726 | } 727 | } 728 | 729 | void davici_list_itemf(struct davici_request *r, const char *fmt, ...) 730 | { 731 | 732 | va_list args; 733 | 734 | va_start(args, fmt); 735 | davici_list_vitemf(r, fmt, args); 736 | va_end(args); 737 | } 738 | 739 | void davici_list_vitemf(struct davici_request *r, const char *fmt, va_list args) 740 | { 741 | char buf[512], *m = buf; 742 | va_list copy; 743 | int len; 744 | 745 | va_copy(copy, args); 746 | len = vsnprintf(buf, sizeof(buf), fmt, copy); 747 | va_end(copy); 748 | if (len >= sizeof(buf)) 749 | { 750 | m = malloc(len + 1); 751 | if (m) 752 | { 753 | len = vsnprintf(m, len + 1, fmt, args); 754 | } 755 | else 756 | { 757 | len = -1; 758 | } 759 | } 760 | if (len < 0) 761 | { 762 | r->err = -errno; 763 | } 764 | else 765 | { 766 | davici_list_item(r, m, len); 767 | } 768 | if (m != buf) 769 | { 770 | free(m); 771 | } 772 | } 773 | 774 | void davici_list_end(struct davici_request *r) 775 | { 776 | add_element(r, DAVICI_LIST_END, 0); 777 | } 778 | 779 | void davici_cancel(struct davici_request *r) 780 | { 781 | free(r->buf); 782 | free(r); 783 | } 784 | 785 | static void append_req(struct davici_conn *c, struct davici_request *r) 786 | { 787 | struct davici_request *cur; 788 | 789 | if (!c->reqs) 790 | { 791 | c->reqs = r; 792 | } 793 | else 794 | { 795 | cur = c->reqs; 796 | while (cur->next) 797 | { 798 | cur = cur->next; 799 | } 800 | cur->next = r; 801 | } 802 | } 803 | 804 | int davici_queue(struct davici_conn *c, struct davici_request *r, 805 | davici_cb cmd_cb, void *user) 806 | { 807 | int err; 808 | 809 | if (r->err) 810 | { 811 | err = r->err; 812 | davici_cancel(r); 813 | return err; 814 | } 815 | r->cb = cmd_cb; 816 | r->user = user; 817 | 818 | append_req(c, r); 819 | 820 | return update_ops(c, c->ops | DAVICI_WRITE); 821 | } 822 | 823 | int davici_queue_streamed(struct davici_conn *c, struct davici_request *r, 824 | davici_cb cmd_cb, const char *event, 825 | davici_cb event_cb, void *user) 826 | { 827 | int err; 828 | 829 | if (r->err) 830 | { 831 | err = r->err; 832 | davici_cancel(r); 833 | return err; 834 | } 835 | err = davici_register(c, event, event_cb, user); 836 | if (err) 837 | { 838 | return err; 839 | } 840 | err = davici_queue(c, r, cmd_cb, user); 841 | davici_unregister(c, event, event_cb, user); 842 | return err; 843 | } 844 | 845 | unsigned int davici_queue_len(struct davici_conn *c) 846 | { 847 | struct davici_request *req; 848 | unsigned int count = 0; 849 | 850 | req = c->reqs; 851 | while (req) 852 | { 853 | req = req->next; 854 | count++; 855 | } 856 | return count; 857 | } 858 | 859 | int davici_register(struct davici_conn *c, const char *event, 860 | davici_cb cb, void *user) 861 | { 862 | struct davici_request *req; 863 | int err; 864 | 865 | err = create_request(DAVICI_EVENT_REGISTER, event, &req); 866 | if (err) 867 | { 868 | return err; 869 | } 870 | req->cb = cb; 871 | req->user = user; 872 | append_req(c, req); 873 | 874 | return update_ops(c, c->ops | DAVICI_WRITE); 875 | } 876 | 877 | int davici_unregister(struct davici_conn *c, const char *event, 878 | davici_cb cb, void *user) 879 | { 880 | struct davici_request *req; 881 | int err; 882 | 883 | err = create_request(DAVICI_EVENT_UNREGISTER, event, &req); 884 | if (err) 885 | { 886 | return err; 887 | } 888 | req->cb = cb; 889 | req->user = user; 890 | append_req(c, req); 891 | 892 | return update_ops(c, c->ops | DAVICI_WRITE); 893 | } 894 | 895 | static int parse_name(struct davici_response *res) 896 | { 897 | unsigned char len; 898 | int err; 899 | 900 | if (res->pos > res->pkt->received - sizeof(len)) 901 | { 902 | return -EBADMSG; 903 | } 904 | len = res->pkt->buf[res->pos++]; 905 | if (len > res->pkt->received - res->pos) 906 | { 907 | return -EBADMSG; 908 | } 909 | err = copy_name(res->name, sizeof(res->name), 910 | res->pkt->buf + res->pos, len); 911 | if (err < 0) 912 | { 913 | return err; 914 | } 915 | res->pos += len; 916 | return 0; 917 | } 918 | 919 | static int parse_value(struct davici_response *res) 920 | { 921 | uint16_t len; 922 | 923 | if (res->pos > res->pkt->received - sizeof(len)) 924 | { 925 | return -EBADMSG; 926 | } 927 | memcpy(&len, res->pkt->buf + res->pos, sizeof(len)); 928 | len = ntohs(len); 929 | res->pos += sizeof(len); 930 | if (len > res->pkt->received - res->pos) 931 | { 932 | return -EBADMSG; 933 | } 934 | res->buf = res->pkt->buf + res->pos; 935 | res->buflen = len; 936 | res->pos += len; 937 | return 0; 938 | } 939 | 940 | int davici_parse(struct davici_response *res) 941 | { 942 | int type, err; 943 | 944 | if (res->pos == res->pkt->received) 945 | { 946 | if (res->list || res->section) 947 | { 948 | return -EBADMSG; 949 | } 950 | res->pos = 0; 951 | return DAVICI_END; 952 | } 953 | if (res->pos > res->pkt->received) 954 | { 955 | return -EINVAL; 956 | } 957 | type = res->pkt->buf[res->pos++]; 958 | switch (type) 959 | { 960 | case DAVICI_SECTION_START: 961 | if (res->list) 962 | { 963 | return -EBADMSG; 964 | } 965 | res->section++; 966 | err = parse_name(res); 967 | if (err < 0) 968 | { 969 | return err; 970 | } 971 | return type; 972 | case DAVICI_LIST_START: 973 | if (res->list) 974 | { 975 | return -EBADMSG; 976 | } 977 | err = parse_name(res); 978 | if (err < 0) 979 | { 980 | return err; 981 | } 982 | res->list++; 983 | return type; 984 | case DAVICI_LIST_ITEM: 985 | if (!res->list) 986 | { 987 | return -EBADMSG; 988 | } 989 | err = parse_value(res); 990 | if (err < 0) 991 | { 992 | return err; 993 | } 994 | return type; 995 | case DAVICI_KEY_VALUE: 996 | if (res->list) 997 | { 998 | return -EBADMSG; 999 | } 1000 | err = parse_name(res); 1001 | if (err < 0) 1002 | { 1003 | return err; 1004 | } 1005 | err = parse_value(res); 1006 | if (err < 0) 1007 | { 1008 | return err; 1009 | } 1010 | return type; 1011 | case DAVICI_SECTION_END: 1012 | if (!res->section || res->list) 1013 | { 1014 | return -EBADMSG; 1015 | } 1016 | res->section--; 1017 | return type; 1018 | case DAVICI_LIST_END: 1019 | if (!res->list) 1020 | { 1021 | return -EBADMSG; 1022 | } 1023 | res->list--; 1024 | return type; 1025 | default: 1026 | return -EBADMSG; 1027 | } 1028 | } 1029 | 1030 | int davici_recurse(struct davici_response *res, davici_recursecb section, 1031 | davici_recursecb li, davici_recursecb kv, void *user) 1032 | { 1033 | int type, err; 1034 | 1035 | while (1) 1036 | { 1037 | type = davici_parse(res); 1038 | switch (type) 1039 | { 1040 | case DAVICI_SECTION_START: 1041 | if (section) 1042 | { 1043 | err = section(res, user); 1044 | } 1045 | else 1046 | { 1047 | err = davici_recurse(res, NULL, NULL, NULL, NULL); 1048 | } 1049 | if (err < 0) 1050 | { 1051 | return err; 1052 | } 1053 | break; 1054 | case DAVICI_KEY_VALUE: 1055 | if (kv) 1056 | { 1057 | err = kv(res, user); 1058 | if (err < 0) 1059 | { 1060 | return err; 1061 | } 1062 | } 1063 | break; 1064 | case DAVICI_LIST_START: 1065 | while (1) 1066 | { 1067 | type = davici_parse(res); 1068 | switch (type) 1069 | { 1070 | case DAVICI_LIST_ITEM: 1071 | if (li) 1072 | { 1073 | err = li(res, user); 1074 | if (err < 0) 1075 | { 1076 | return err; 1077 | } 1078 | } 1079 | continue; 1080 | case DAVICI_LIST_END: 1081 | break; 1082 | default: 1083 | if (type < 0) 1084 | { 1085 | return type; 1086 | } 1087 | return -EBADMSG; 1088 | } 1089 | break; 1090 | } 1091 | break; 1092 | case DAVICI_SECTION_END: 1093 | case DAVICI_END: 1094 | return 0; 1095 | default: 1096 | if (type < 0) 1097 | { 1098 | return type; 1099 | } 1100 | return -EBADMSG; 1101 | } 1102 | } 1103 | } 1104 | 1105 | unsigned int davici_get_level(struct davici_response *res) 1106 | { 1107 | if (res->list) 1108 | { 1109 | return res->section + 1; 1110 | } 1111 | return res->section; 1112 | } 1113 | 1114 | const char* davici_get_name(struct davici_response *res) 1115 | { 1116 | return res->name; 1117 | } 1118 | 1119 | int davici_name_strcmp(struct davici_response *res, const char *str) 1120 | { 1121 | return strcmp(res->name, str); 1122 | } 1123 | 1124 | const void* davici_get_value(struct davici_response *res, unsigned int *len) 1125 | { 1126 | *len = res->buflen; 1127 | return res->buf; 1128 | } 1129 | 1130 | int davici_value_scanf(struct davici_response *res, const char *fmt, ...) 1131 | { 1132 | va_list args; 1133 | int ret; 1134 | 1135 | va_start(args, fmt); 1136 | ret = davici_value_vscanf(res, fmt, args); 1137 | va_end(args); 1138 | return ret; 1139 | } 1140 | 1141 | int davici_value_vscanf(struct davici_response *res, const char *fmt, 1142 | va_list args) 1143 | { 1144 | char buf[1024]; 1145 | int err; 1146 | 1147 | err = davici_get_value_str(res, buf, sizeof(buf)); 1148 | if (err < 0) 1149 | { 1150 | return err; 1151 | } 1152 | return vsscanf(buf, fmt, args); 1153 | } 1154 | 1155 | int davici_get_value_str(struct davici_response *res, 1156 | char *buf, unsigned int buflen) 1157 | { 1158 | const char *val = res->buf; 1159 | int i, len; 1160 | 1161 | for (i = 0; i < res->buflen; i++) 1162 | { 1163 | if (!isprint((unsigned char)val[i])) 1164 | { 1165 | return -EINVAL; 1166 | } 1167 | } 1168 | len = snprintf(buf, buflen, "%.*s", res->buflen, val); 1169 | if (len < 0) 1170 | { 1171 | return -errno; 1172 | } 1173 | if (len >= buflen) 1174 | { 1175 | return -ENOBUFS; 1176 | } 1177 | return len; 1178 | } 1179 | 1180 | int davici_value_strcmp(struct davici_response *res, const char *str) 1181 | { 1182 | int ret; 1183 | 1184 | ret = strncmp(res->buf, str, res->buflen); 1185 | if (ret) 1186 | { 1187 | return ret; 1188 | } 1189 | if (strlen(str) == res->buflen) 1190 | { 1191 | return 0; 1192 | } 1193 | return -1; 1194 | } 1195 | 1196 | int davici_dump(struct davici_response *res, const char *name, const char *sep, 1197 | unsigned int level, unsigned int indent, FILE *out) 1198 | { 1199 | ssize_t len, total = 0; 1200 | char buf[4096]; 1201 | int err; 1202 | 1203 | len = fprintf(out, "%*s%s {%s", level * indent, "", name, sep); 1204 | if (len < 0) 1205 | { 1206 | return -errno; 1207 | } 1208 | level++; 1209 | total += len; 1210 | while (1) 1211 | { 1212 | err = davici_parse(res); 1213 | switch (err) 1214 | { 1215 | case DAVICI_END: 1216 | level--; 1217 | len = fprintf(out, "%*s}", level * indent, ""); 1218 | if (len < 0) 1219 | { 1220 | return -errno; 1221 | } 1222 | return total + len; 1223 | case DAVICI_SECTION_START: 1224 | len = fprintf(out, "%*s%s {%s", level * indent, "", 1225 | res->name, sep); 1226 | level++; 1227 | break; 1228 | case DAVICI_SECTION_END: 1229 | level--; 1230 | len = fprintf(out, "%*s}%s", level * indent, "", sep); 1231 | break; 1232 | case DAVICI_KEY_VALUE: 1233 | err = davici_get_value_str(res, buf, sizeof(buf)); 1234 | if (err < 0) 1235 | { 1236 | return err; 1237 | } 1238 | len = fprintf(out, "%*s%s = %s%s", level * indent, "", 1239 | res->name, buf, sep); 1240 | break; 1241 | case DAVICI_LIST_START: 1242 | len = fprintf(out, "%*s%s [%s", level * indent, "", 1243 | res->name, sep); 1244 | level++; 1245 | break; 1246 | case DAVICI_LIST_ITEM: 1247 | err = davici_get_value_str(res, buf, sizeof(buf)); 1248 | if (err < 0) 1249 | { 1250 | return err; 1251 | } 1252 | len = fprintf(out, "%*s%s%s", level * indent, "", buf, sep); 1253 | break; 1254 | case DAVICI_LIST_END: 1255 | level--; 1256 | len = fprintf(out, "%*s]%s", level * indent, "", sep); 1257 | break; 1258 | default: 1259 | return err; 1260 | } 1261 | if (len < 0) 1262 | { 1263 | return -errno; 1264 | } 1265 | total += len; 1266 | } 1267 | } 1268 | -------------------------------------------------------------------------------- /davici.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 onway ag 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library 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 GNU 12 | * Lesser General Public License for more details. 13 | */ 14 | 15 | /** 16 | * @defgroup davici davici 17 | * @{ 18 | * 19 | * Decoupled Asynchronous strongSwan VICI client library. 20 | */ 21 | 22 | #ifndef _DAVICI_H_ 23 | #define _DAVICI_H_ 24 | 25 | #include 26 | #include 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | /** 33 | * Opaque connection context. 34 | */ 35 | struct davici_conn; 36 | 37 | /** 38 | * Opaque request message context. 39 | */ 40 | struct davici_request; 41 | 42 | /** 43 | * Opaque response message context. 44 | */ 45 | struct davici_response; 46 | 47 | /** 48 | * Parsed message element. 49 | */ 50 | enum davici_element { 51 | /** valid end of message */ 52 | DAVICI_END = 0, 53 | /** begin of a section */ 54 | DAVICI_SECTION_START, 55 | /** end of a section */ 56 | DAVICI_SECTION_END, 57 | /** key/value pair */ 58 | DAVICI_KEY_VALUE, 59 | /** begin of a list */ 60 | DAVICI_LIST_START, 61 | /** list item */ 62 | DAVICI_LIST_ITEM, 63 | /** end of a list */ 64 | DAVICI_LIST_END, 65 | }; 66 | 67 | /** 68 | * File descriptor watch operations requested. 69 | */ 70 | enum davici_fdops { 71 | /** request read-ready notifications */ 72 | DAVICI_READ = (1<<0), 73 | /** request write-ready notifications */ 74 | DAVICI_WRITE = (1<<1), 75 | }; 76 | 77 | /** 78 | * Prototype for a command response or event callback function. 79 | * 80 | * This kind of callback is invoked for command response and event callbacks. 81 | * The err parameter indicates any errors in issuing a command or registering 82 | * for an event. 83 | * 84 | * For event registration, also implicitly for streamed command messages using 85 | * events, the callback gets invoked with a NULL res parameter both after 86 | * registration and deregistration to indicate any error conditions. 87 | * 88 | * Command and event registration responses do not carry a name on the wire, 89 | * but the name parameter is populated with the name of the issued event 90 | * registration or command request. 91 | * 92 | * @param conn opaque connection context 93 | * @param err 0 on success, or a negative receive errno 94 | * @param name command or event name raising the callback 95 | * @param res event or response message 96 | * @param user user context passed to registration function 97 | */ 98 | typedef void (*davici_cb)(struct davici_conn *conn, int err, const char *name, 99 | struct davici_response *res, void *user); 100 | 101 | /** 102 | * Prototype for a file descriptor watch update callback. 103 | * 104 | * The watch update callback requests (or updates) a file descriptor watch from 105 | * the user. The fd parameter is a file descriptor the user shall monitor 106 | * with select(), poll() or similar functionality. The ops argument is an ORed 107 | * set of enum davici_fdops flags indicating for what kind of file descriptor 108 | * events monitoring should be set up. 109 | * 110 | * The callback may be invoked from any davici function multiple times, but 111 | * is guaranteed to get called with a zero ops parameter before returning 112 | * from davici_disconnect(). 113 | * 114 | * @param conn opaque connection context 115 | * @param fd file descriptor to watch 116 | * @param ops watch operations to register for, enum davici_fdops 117 | * @param user user context passed during connection 118 | * @return 0 if watch updated, or a negative errno 119 | */ 120 | typedef int (*davici_fdcb)(struct davici_conn *conn, int fd, int ops, 121 | void *user); 122 | 123 | /** 124 | * Prototype for a recursive parsing callback. 125 | * 126 | * This callback is used by davici_recurse(), a recursive VICI message parser. 127 | * The same kind of callback is used for section, list item and key/value 128 | * elements found in a message. 129 | * 130 | * If this callback returns a negative errno, parsing is aborted and the 131 | * same errno is returned from davici_recurse(). 132 | * 133 | * @param res response or event message parsing 134 | * @param user user context, as passed to davici_recurse() 135 | * @return a negative errno on error to stop parsing 136 | */ 137 | typedef int (*davici_recursecb)(struct davici_response *res, void *user); 138 | 139 | /** 140 | * Create a connection to a VICI Unix socket. 141 | * 142 | * Opens a Unix socket connection to a VICI service under path, using a 143 | * file descriptor monitoring callback function as discussed above. 144 | * 145 | * Please note that this function uses connect() on a blocking socket, which 146 | * in theory is a blocking call. 147 | * 148 | * @param path path to Unix socket 149 | * @param fdcb callback to register for file descriptor watching 150 | * @param user user context to pass to fdcb 151 | * @param connp pointer receiving connection context on success 152 | * @return 0 on success, or a negative errno 153 | */ 154 | int davici_connect_unix(const char *path, davici_fdcb fdcb, void *user, 155 | struct davici_conn **connp); 156 | 157 | /** 158 | * Read and process pending connection data. 159 | * 160 | * Performs a non-blocking read on the connection, and dispatches any 161 | * received response or event message. The call uses non-blocking reads 162 | * only, and returns with a successful result if the call would block. It is 163 | * usually an unrecoverable error if a negative errno is returned, and the 164 | * user should call davici_disconnect(). 165 | * 166 | * @param conn opaque connection context 167 | * @return 0 on success, or a negative errno 168 | */ 169 | int davici_read(struct davici_conn *conn); 170 | 171 | /** 172 | * Write queued request data to the connection. 173 | * 174 | * Performs a non-blocking write on the connection for any currently queued 175 | * message. The call uses non-blocking reads only, and returns with a 176 | * successful result if the call would block. It is usually an unrecoverable 177 | * error if a negative errno is returned, and the user should call 178 | * davici_disconnect(). 179 | * 180 | * @param conn opaque connection context 181 | * @return 0 on success, or a negative errno 182 | */ 183 | int davici_write(struct davici_conn *conn); 184 | 185 | /** 186 | * Close an open VICI connection and free associated resources. 187 | * 188 | * Frees any resources associated to a connection, and invokes the file 189 | * descriptor watch callback of the connection with zero-flags if a monitoring 190 | * registration is active. 191 | * 192 | * Any request messages created with davici_new_cmd() but not queued to 193 | * the connection using either davici_queue() or davici_queue_streamed() 194 | * is not freed by this call. Such requests must be freed explicitly by calling 195 | * davici_cancel() on them. 196 | * 197 | * @param conn opaque connection context 198 | */ 199 | void davici_disconnect(struct davici_conn *conn); 200 | 201 | /** 202 | * Allocate a new request command message. 203 | * 204 | * Creates a new but empty request message for a named command. The returned 205 | * handle must be passed to davici_queue() or davici_queue_streamed() after 206 | * adding request data. If the connection gets closed before the request 207 | * could be queued, the request must be freed using davici_cancel(). 208 | * 209 | * @param cmd command name 210 | * @param reqp receives allocated request context 211 | * @return 0 on success, or a negative errno 212 | */ 213 | int davici_new_cmd(const char *cmd, struct davici_request **reqp); 214 | 215 | /** 216 | * Begin a new section on a request message. 217 | * 218 | * Begin a new named section at the current request position. Any started 219 | * section must have a corresponding davici_section_end() added to form 220 | * a valid request message. Creating sections within lists is invalid. 221 | * 222 | * @param req request context 223 | * @param name name of section to open 224 | */ 225 | void davici_section_start(struct davici_request *req, const char *name); 226 | 227 | /** 228 | * End a section previously opened on a request message. 229 | * 230 | * Close a section previously started with davici_section_start(). The 231 | * call must be balanced with davici_section_start() to form a valid request 232 | * message. 233 | * 234 | * @param req request context 235 | */ 236 | void davici_section_end(struct davici_request *req); 237 | 238 | /** 239 | * Add a key/value element to the request message. 240 | * 241 | * Adds a new key/value pair at the current request position. Key/values 242 | * may be added to any explicit or the implicit root section, but not to 243 | * lists. 244 | * 245 | * @param req request context 246 | * @param name key name 247 | * @param buf value buffer 248 | * @param buflen size, in bytes, of buf 249 | */ 250 | void davici_kv(struct davici_request *req, const char *name, 251 | const void *buf, unsigned int buflen); 252 | 253 | /** 254 | * Add a key with a format string value to the request message. 255 | * 256 | * Similar to davici_kv(), but instead of a fixed buffer takes a printf() 257 | * format string and arguments to form the value of the key/value pair. 258 | * 259 | * @param req request context 260 | * @param name key name 261 | * @param fmt format string for value 262 | * @param ... arguments for fmt string 263 | */ 264 | void davici_kvf(struct davici_request *req, const char *name, 265 | const char *fmt, ...); 266 | 267 | /** 268 | * Add a key with a format string value and a va_list to the request message. 269 | * 270 | * Very similar to davici_kvf(), but takes a va_list argument list for the 271 | * format string instead of variable arguments. 272 | * 273 | * @param req request context 274 | * @param name key name 275 | * @param fmt format string for value 276 | * @param args argument list for fmt string 277 | */ 278 | void davici_vkvf(struct davici_request *req, const char *name, 279 | const char *fmt, va_list args); 280 | 281 | /** 282 | * Begin a list of unnamed items in a request message. 283 | * 284 | * Starts a new list at the current request message position. Lists may appear 285 | * in any section, but not in lists. Started lists must be closed by 286 | * davici_list_end(), and both calls must be balanced to form a valid request 287 | * message. 288 | * 289 | * @param req request context 290 | * @param name list name to open 291 | */ 292 | void davici_list_start(struct davici_request *req, const char *name); 293 | 294 | /** 295 | * Add a list item value to a previously opened list. 296 | * 297 | * List items may be added to lists only, and are invalid in any other context. 298 | * 299 | * @param req request context 300 | * @param buf value buffer 301 | * @param buflen size, in bytes, of buf 302 | */ 303 | void davici_list_item(struct davici_request *req, const void *buf, 304 | unsigned int buflen); 305 | 306 | /** 307 | * Add a list item with a format string value to a previously opened list. 308 | * 309 | * Similar to davici_list_item(), but instead of a fixed buffer takes a 310 | * printf() format string and variable arguments to form the list item value. 311 | * 312 | * @param req request context 313 | * @param fmt format string for value 314 | * @param ... arguments for fmt string 315 | */ 316 | void davici_list_itemf(struct davici_request *req, const char *fmt, ...); 317 | 318 | /** 319 | * Add a list item with a format string value and a va_list. 320 | * 321 | * Very similar to davici_list_itemf(), but instead of variable arguments 322 | * takes a va_list for the printf() format string. 323 | * 324 | * @param req request context 325 | * @param fmt format string for value 326 | * @param args argument list for fmt string 327 | */ 328 | void davici_list_vitemf(struct davici_request *req, const char *fmt, 329 | va_list args); 330 | 331 | /** 332 | * End a list previously opened for a request message. 333 | * 334 | * Closes the currently open list; valid only in a list context. 335 | * 336 | * @param req request context 337 | */ 338 | void davici_list_end(struct davici_request *req); 339 | 340 | /** 341 | * Clean up a request if it is not passed to davici_queue(). 342 | * 343 | * Messages get automatically cleaned up when passed to davici_queue() or 344 | * after it has been fed to callbacks. Use this call only if an allocated 345 | * message does not get queued(), but the request is aborted. 346 | * 347 | * @param req request context to free 348 | */ 349 | void davici_cancel(struct davici_request *req); 350 | 351 | /** 352 | * Queue a command request message for submission. 353 | * 354 | * The request gets queued, and is sent to the daemon with davici_write() 355 | * when the daemon accepts new data and all previously queued requests have 356 | * been processed. Once the daemon responds to the request, davici_read() 357 | * invokes the passed callback with the response message and the provided 358 | * user context. 359 | * 360 | * @param conn connection context 361 | * @param req request message to queue 362 | * @param cb callback to invoke for response message 363 | * @param user user context to pass to callback 364 | * @return 0 on success, or a negative errno 365 | */ 366 | int davici_queue(struct davici_conn *conn, struct davici_request *req, 367 | davici_cb cb, void *user); 368 | 369 | /** 370 | * Queue a command request using event based streaming. 371 | * 372 | * In addition to davici_queue(), this call registers for an event while 373 | * the command is active and invokes an event callback for each streamed 374 | * object. Upon completion of the call, it invokes the result callback. 375 | * 376 | * @param conn connection context 377 | * @param req request message to queue 378 | * @param res_cb callback to invoke for response message 379 | * @param event streamed event name to register for 380 | * @param event_cb event callback invoked for each streamed event message 381 | * @param user user context to pass to callbacks 382 | * @return 0 on success, or a negative errno 383 | */ 384 | int davici_queue_streamed(struct davici_conn *conn, struct davici_request *req, 385 | davici_cb res_cb, const char *event, 386 | davici_cb event_cb, void *user); 387 | 388 | /** 389 | * Get the count of all queued davici request messages. 390 | * 391 | * @param conn connection context 392 | * @return number of request messages in queue 393 | */ 394 | unsigned int davici_queue_len(struct davici_conn *conn); 395 | 396 | /** 397 | * Register for event messages. 398 | * 399 | * Event registration is asynchronous; once registration completed the 400 | * callback is invoked with a NULL event message. The error code indicates 401 | * the registration status. 402 | * 403 | * @param conn connection context 404 | * @param event event name to register 405 | * @param cb callback to invoke on events 406 | * @param user user context to pass to cb 407 | * @return 0 on success, or a negative errno 408 | */ 409 | int davici_register(struct davici_conn *conn, const char *event, 410 | davici_cb cb, void *user); 411 | 412 | /** 413 | * Unregister for event messages. 414 | * 415 | * Event deregistration is asynchronous; once deregistration completed the 416 | * callback is invoked with a NULL event message. The error code indicates 417 | * the deregistration status. 418 | * 419 | * @param conn connection context 420 | * @param event event name to unregister 421 | * @param cb callback to invoke on events 422 | * @param user user context to pass to cb 423 | * @return 0 on success, or a negative errno 424 | */ 425 | int davici_unregister(struct davici_conn *conn, const char *event, 426 | davici_cb cb, void *user); 427 | 428 | /** 429 | * Parse a response or event message. 430 | * 431 | * davici_parse() implements iterative parsing of response messages by 432 | * returning enum davici_element types as parsing status. For named types 433 | * davici_get_name() may be called to get the name of the just parsed element. 434 | * For types having a value, davici_get_value() returns the value of the 435 | * just parsed element. 436 | * 437 | * If parsing completes with DAVICI_END it may be parsed again using 438 | * davici_parse(). When davici_parse() returns an error, additional calls 439 | * to davici_parse() have undefined behavior. 440 | * 441 | * @param res response or event message 442 | * @return enum davici_element, or a negative errno 443 | */ 444 | int davici_parse(struct davici_response *res); 445 | 446 | /** 447 | * Recursive response or event message parser. 448 | * 449 | * Using davici_parse(), davici_recurse() parses a VICI message by invoking 450 | * callbacks for the found elements. Three callbacks can be passed, but all 451 | * are optional to not further handle an element or descent into a section. 452 | * 453 | * If a section callback is given, that callback must invoke davici_recurse() 454 | * to parse the inner context, or it may return a negative errno. The return 455 | * value from the recursive davici_recurse() invocation should be returned 456 | * from the section callback to properly propagate inner errors. 457 | * 458 | * For any callback, davici_get_name() or davici_name_strcmp() can be called 459 | * to get the name of the element for which the callback is called. For lists, 460 | * the li callback is invoked for items only; davici_get_name() in a list item 461 | * callback returns the name of the list. 462 | * 463 | * davici_get_value() can be used in list item and key/value callbacks to get 464 | * the value of the element the callback is invoked for. 465 | * 466 | * @param res response or event message 467 | * @param section section callback, NULL to skip sub-sections 468 | * @param li list item callback, NULL to ignore 469 | * @param kv key/value callback, NULL to ignore 470 | * @return a negative errno on error 471 | */ 472 | int davici_recurse(struct davici_response *res, davici_recursecb section, 473 | davici_recursecb li, davici_recursecb kv, void *ctx); 474 | 475 | /** 476 | * Get the section/list nesting level of the current parser position. 477 | * 478 | * The root section has a level of 0 and for each nested section the level is 479 | * incremented by one. When parsing a list, the level is incremented by one. 480 | * The level after parsing a section/list start is increased just after 481 | * davici_parse() is called for such an element, while for end elements the 482 | * level is decreased in davici_parse() for that element. 483 | * 484 | * @param res response or event message 485 | * @return section/list nesting level 486 | */ 487 | unsigned int davici_get_level(struct davici_response *res); 488 | 489 | /** 490 | * Get the element name previously parsed in davici_parse(). 491 | * 492 | * This call has defined behavior only if davici_parse() returned an element, 493 | * with a name, i.e. a section/list start or a key/value. 494 | * 495 | * @param res response or event message 496 | * @return element name 497 | */ 498 | const char* davici_get_name(struct davici_response *res); 499 | 500 | /** 501 | * Compare the previously parsed element name against a given string. 502 | * 503 | * This call has defined behavior only if davici_parse() returned an element, 504 | * with a name, i.e. a section/list start or a key/value. 505 | * 506 | * @param res response or event message 507 | * @param str string to compare against name 508 | * @return strcmp() result 509 | */ 510 | int davici_name_strcmp(struct davici_response *res, const char *str); 511 | 512 | /** 513 | * Get the element value previously parsed in davici_parse(). 514 | * 515 | * This call has defined behavior only if davici_parse() returned an element, 516 | * with a value, i.e. a section/list start or a key/value. 517 | * 518 | * @param res response or event message 519 | * @param len pointer receiving buffer length 520 | * @return buffer containing value 521 | */ 522 | const void* davici_get_value(struct davici_response *res, unsigned int *len); 523 | 524 | /** 525 | * Get the element value if it is a string. 526 | * 527 | * This is a convenience function to get the value of an element as string, 528 | * the same restrictions as to davici_get_value() apply. The function fails 529 | * if the value has any non-printable characters. 530 | * 531 | * @param res response or event message context 532 | * @param buf buffer to write string to 533 | * @param buflen size of the buffer 534 | * @return number of characters written, or a negative errno 535 | */ 536 | int davici_get_value_str(struct davici_response *res, 537 | char *buf, unsigned int buflen); 538 | 539 | /** 540 | * Convert value using a format specifier. 541 | * 542 | * This functions handles the value like a string, and then uses a scanf() 543 | * format string to convert the value to the passed argument pointers. 544 | * 545 | * @param res response or event message context 546 | * @param fmt scanf() format string 547 | * @param ... arguments of format string 548 | * @return number of input items matched, or a negative errno 549 | */ 550 | int davici_value_scanf(struct davici_response *res, const char *fmt, ...); 551 | 552 | /** 553 | * Convert value using a format specifier, va_list variant. 554 | * 555 | * Like davici_value_scanf(), but takes arguments in a va_list. 556 | * 557 | * @param res response or event message context 558 | * @param fmt scanf() format string 559 | * @param args arguments of format string 560 | * @return number of input items matched, or a negative errno 561 | */ 562 | int davici_value_vscanf(struct davici_response *res, const char *fmt, 563 | va_list args); 564 | 565 | /** 566 | * Convert value using a format specifier, error checking variant. 567 | * 568 | * Like davici_value_scanf(), but fails if either not the full value is 569 | * consumed or not all input items have been matched. 570 | * 571 | * Note that davici_value_escanf() is implemented as macro, and that 572 | * the arguments are evaluated more than once. fmt must be a string literal. 573 | * 574 | * @param res response or event message context 575 | * @param fmt scanf() format string 576 | * @param ... arguments of format string 577 | * @return a negative errno on error 578 | */ 579 | #define davici_value_escanf(res, fmt, ...) ({\ 580 | unsigned int _v; \ 581 | int _m, _e, _c; \ 582 | _e = sizeof((void*[]){ __VA_ARGS__ }) / sizeof(void*); \ 583 | _m = davici_value_scanf(res, fmt "%n", __VA_ARGS__, &_c); \ 584 | davici_get_value(res, &_v); \ 585 | (_m == _e && _c == _v) ? 0 : -EBADMSG; \ 586 | }) 587 | 588 | /** 589 | * Compare the element value to a given string. 590 | * 591 | * This is a convenience function to compare the value of an element as string 592 | * to a given string. The same restrictions as to davici_get_value() apply. 593 | * 594 | * @param res response or event message 595 | * @param str string to compare against value 596 | * @return strcmp() result 597 | */ 598 | int davici_value_strcmp(struct davici_response *res, const char *str); 599 | 600 | /** 601 | * Dump a response or event message to a FILE stream. 602 | * 603 | * The dump output is not considered stable, but solely for debugging 604 | * purposes. 605 | * 606 | * @param res response or event message context 607 | * @param name name of the message to dump 608 | * @param sep value separator, such as "\n" 609 | * @param level base indentation level 610 | * @param indent number of spaces to use for indentation 611 | * @param out FILE stream to write to 612 | * @return total bytes written, or a negative errno on error 613 | */ 614 | int davici_dump(struct davici_response *res, const char *name, const char *sep, 615 | unsigned int level, unsigned int indent, FILE *out); 616 | 617 | #ifdef __cplusplus 618 | } 619 | #endif 620 | 621 | #endif /* _DAVICI_H_ */ 622 | 623 | /** 624 | * @} 625 | */ 626 | -------------------------------------------------------------------------------- /examples/.gitignore: -------------------------------------------------------------------------------- 1 | version 2 | -------------------------------------------------------------------------------- /examples/Makefile.am: -------------------------------------------------------------------------------- 1 | AM_CPPFLAGS = \ 2 | -I$(top_srcdir) 3 | 4 | AM_CFLAGS = -Wall 5 | 6 | LDADD = \ 7 | $(top_builddir)/libdavici.la 8 | 9 | version_SOURCES = version.c 10 | 11 | noinst_PROGRAMS = \ 12 | version 13 | -------------------------------------------------------------------------------- /examples/version.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 onway ag 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library 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 GNU 12 | * Lesser General Public License for more details. 13 | */ 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include 22 | 23 | struct dispatch_data { 24 | struct pollfd pfd; 25 | struct davici_conn *c; 26 | int pending; 27 | }; 28 | 29 | static short ops2poll(int ops) 30 | { 31 | short events = 0; 32 | 33 | if (ops & DAVICI_READ) 34 | { 35 | events |= POLLIN; 36 | } 37 | if (ops & DAVICI_WRITE) 38 | { 39 | events |= POLLOUT; 40 | } 41 | return events; 42 | } 43 | 44 | static int fdcb(struct davici_conn *conn, int fd, int ops, void *user) 45 | { 46 | struct dispatch_data *dd = user; 47 | 48 | dd->pfd.events = ops2poll(ops); 49 | if (dd->pfd.events) 50 | { 51 | dd->pfd.fd = fd; 52 | } 53 | else 54 | { 55 | dd->pfd.fd = -1; 56 | } 57 | return 0; 58 | } 59 | 60 | static void die(int err, const char *msg) 61 | { 62 | fprintf(stderr, "%s: %s\n", msg, strerror(-err)); 63 | exit(-err); 64 | } 65 | 66 | static void version_cb(struct davici_conn *conn, int err, const char *name, 67 | struct davici_response *res, void *user) 68 | { 69 | struct dispatch_data *dd = user; 70 | 71 | if (err < 0) 72 | { 73 | die(err, "version"); 74 | } 75 | davici_dump(res, "version", "\n", 0, 2, stdout); 76 | dd->pending--; 77 | } 78 | 79 | static int query_version(struct dispatch_data *dd) 80 | { 81 | struct davici_request *r; 82 | int err; 83 | 84 | err = davici_new_cmd("version", &r); 85 | if (err < 0) 86 | { 87 | return err; 88 | } 89 | err = davici_queue(dd->c, r, version_cb, dd); 90 | if (err < 0) 91 | { 92 | return err; 93 | } 94 | dd->pending++; 95 | return 0; 96 | } 97 | 98 | static int dispatch(struct dispatch_data *dd) 99 | { 100 | int err, ret; 101 | 102 | while (dd->pending && dd->pfd.fd != -1) 103 | { 104 | ret = poll(&dd->pfd, 1, -1); 105 | if (ret < 0) 106 | { 107 | return -errno; 108 | } 109 | if (ret != 1) 110 | { 111 | return -EIO; 112 | } 113 | if (dd->pfd.revents & POLLIN) 114 | { 115 | err = davici_read(dd->c); 116 | if (err < 0) 117 | { 118 | return err; 119 | } 120 | } 121 | if (dd->pfd.revents & POLLOUT) 122 | { 123 | err = davici_write(dd->c); 124 | if (err < 0) 125 | { 126 | return err; 127 | } 128 | } 129 | } 130 | return 0; 131 | } 132 | 133 | int main(int argc, const char *argv[]) 134 | { 135 | struct dispatch_data dd = { 136 | .pfd = { 137 | .fd = -1, 138 | }, 139 | }; 140 | int err; 141 | 142 | err = davici_connect_unix("/var/run/charon.vici", fdcb, &dd, &dd.c); 143 | if (err < 0) 144 | { 145 | die(err, "connect failed"); 146 | } 147 | err = query_version(&dd); 148 | if (err < 0) 149 | { 150 | die(err, "query version failed"); 151 | } 152 | err = dispatch(&dd); 153 | if (err < 0) 154 | { 155 | die(err, "dispatch failed"); 156 | } 157 | davici_disconnect(dd.c); 158 | return 0; 159 | } 160 | -------------------------------------------------------------------------------- /tests/.gitignore: -------------------------------------------------------------------------------- 1 | *.tst 2 | *.log 3 | *.trs 4 | -------------------------------------------------------------------------------- /tests/Makefile.am: -------------------------------------------------------------------------------- 1 | AM_CPPFLAGS = \ 2 | -I$(top_srcdir) 3 | 4 | AM_CFLAGS = -Wall 5 | 6 | check_LTLIBRARIES = libtester.la 7 | 8 | libtester_la_SOURCES = \ 9 | tester.c tester.h 10 | 11 | LDADD = \ 12 | $(top_builddir)/libdavici.la \ 13 | libtester.la 14 | 15 | TESTS = \ 16 | cmd.tst \ 17 | dump.tst \ 18 | many.tst \ 19 | event.tst \ 20 | stream.tst \ 21 | recurse.tst \ 22 | badsock.tst \ 23 | cmdunknown.tst \ 24 | eventunknown.tst 25 | 26 | cmd_tst_SOURCES = cmd.c 27 | dump_tst_SOURCES = dump.c 28 | many_tst_SOURCES = many.c 29 | event_tst_SOURCES = event.c 30 | stream_tst_SOURCES = stream.c 31 | recurse_tst_SOURCES = recurse.c 32 | badsock_tst_SOURCES = badsock.c 33 | cmdunknown_tst_SOURCES = cmdunknown.c 34 | eventunknown_tst_SOURCES = eventunknown.c 35 | 36 | check_PROGRAMS = $(TESTS) 37 | -------------------------------------------------------------------------------- /tests/badsock.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 onway ag 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library 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 GNU 12 | * Lesser General Public License for more details. 13 | */ 14 | 15 | #include "tester.h" 16 | 17 | #include 18 | #include 19 | 20 | int iocb(struct davici_conn *c, int fd, int ops, void *user) 21 | { 22 | assert(0); 23 | } 24 | 25 | int main(int argc, char *argv[]) 26 | { 27 | struct davici_conn *c; 28 | int ret; 29 | 30 | ret = davici_connect_unix("/does/not/exist", iocb, NULL, &c); 31 | assert(ret == -ENOENT); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /tests/cmd.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 onway ag 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library 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 GNU 12 | * Lesser General Public License for more details. 13 | */ 14 | 15 | #include "tester.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | static char huge[4096]; 23 | 24 | static void echocb(struct tester *t, int fd) 25 | { 26 | char buf[sizeof(huge) * 2]; 27 | uint32_t len; 28 | 29 | len = tester_read_cmdreq(fd, "echoreq"); 30 | assert(len < sizeof(buf)); 31 | assert(read(fd, buf, len) == len); 32 | tester_write_cmdres(fd, buf, len); 33 | } 34 | 35 | static void reqcb(struct davici_conn *c, int err, const char *name, 36 | struct davici_response *res, void *user) 37 | { 38 | struct tester *t = user; 39 | char buf[64]; 40 | const char *h; 41 | unsigned int len; 42 | const void *v; 43 | int ret, i, j; 44 | 45 | assert(err >= 0); 46 | assert(davici_get_level(res) == 0); 47 | 48 | for (i = 0;; i++) 49 | { 50 | ret = davici_parse(res); 51 | switch (i) 52 | { 53 | case 0: 54 | assert(ret == DAVICI_SECTION_START); 55 | assert(strcmp(davici_get_name(res), "section") == 0); 56 | assert(davici_get_level(res) == 1); 57 | break; 58 | case 1: 59 | assert(ret == DAVICI_KEY_VALUE); 60 | assert(strcmp(davici_get_name(res), "key") == 0); 61 | v = davici_get_value(res, &len); 62 | assert(v); 63 | assert(len == strlen("value")); 64 | assert(memcmp(v, "value", len) == 0); 65 | assert(davici_get_level(res) == 1); 66 | break; 67 | case 2: 68 | assert(ret == DAVICI_LIST_START); 69 | assert(strcmp(davici_get_name(res), "list") == 0); 70 | assert(davici_name_strcmp(res, "list") == 0); 71 | assert(davici_name_strcmp(res, "a") > 0); 72 | assert(davici_name_strcmp(res, "x") < 0); 73 | assert(davici_get_level(res) == 2); 74 | break; 75 | case 3: 76 | assert(ret == DAVICI_LIST_ITEM); 77 | assert(davici_get_value_str(res, buf, 78 | sizeof(buf)) == strlen("item")); 79 | assert(strcmp(buf, "item") == 0); 80 | assert(davici_value_strcmp(res, "item") == 0); 81 | assert(davici_value_strcmp(res, "a") > 0); 82 | assert(davici_value_strcmp(res, "x") < 0); 83 | assert(davici_value_strcmp(res, "itemxx") < 0); 84 | assert(davici_get_level(res) == 2); 85 | break; 86 | case 4: 87 | assert(ret == DAVICI_LIST_END); 88 | assert(davici_get_level(res) == 1); 89 | break; 90 | case 5: 91 | assert(ret == DAVICI_KEY_VALUE); 92 | assert(davici_get_level(res) == 1); 93 | assert(davici_name_strcmp(res, "huge") == 0); 94 | h = davici_get_value(res, &len); 95 | assert(len == sizeof(huge)); 96 | for (j = 0; j < len; j++) 97 | { 98 | assert(h[i] == 'h'); 99 | } 100 | break; 101 | case 6: 102 | assert(ret == DAVICI_SECTION_END); 103 | assert(davici_get_level(res) == 0); 104 | break; 105 | case 7: 106 | assert(ret == DAVICI_END); 107 | assert(davici_get_level(res) == 0); 108 | return tester_complete(t); 109 | default: 110 | assert(0); 111 | break; 112 | } 113 | } 114 | } 115 | 116 | int main(int argc, char *argv[]) 117 | { 118 | struct tester *t; 119 | struct davici_conn *c; 120 | struct davici_request *r; 121 | 122 | t = tester_create(echocb); 123 | assert(davici_connect_unix(tester_getpath(t), 124 | tester_davici_iocb, t, &c) >= 0); 125 | assert(davici_new_cmd("tocancel", &r) >= 0); 126 | davici_cancel(r); 127 | assert(davici_new_cmd("one", &r) >= 0); 128 | assert(davici_queue(c, r, reqcb, t) >= 0); 129 | assert(davici_new_cmd("two", &r) >= 0); 130 | assert(davici_queue(c, r, reqcb, t) >= 0); 131 | assert(davici_new_cmd("three", &r) >= 0); 132 | assert(davici_queue(c, r, reqcb, t) >= 0); 133 | davici_disconnect(c); 134 | 135 | assert(davici_connect_unix(tester_getpath(t), 136 | tester_davici_iocb, t, &c) >= 0); 137 | assert(davici_new_cmd("echoreq", &r) >= 0); 138 | davici_section_start(r, "section"); 139 | davici_kvf(r, "key", "%s", "value"); 140 | davici_list_start(r, "list"); 141 | davici_list_itemf(r, "%s", "item"); 142 | davici_list_end(r); 143 | memset(huge, 'h', sizeof(huge)); 144 | davici_kv(r, "huge", huge, sizeof(huge)); 145 | davici_section_end(r); 146 | 147 | assert(davici_queue(c, r, reqcb, t) >= 0); 148 | 149 | tester_runio(t, c); 150 | davici_disconnect(c); 151 | tester_cleanup(t); 152 | return 0; 153 | } 154 | -------------------------------------------------------------------------------- /tests/cmdunknown.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 onway ag 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library 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 GNU 12 | * Lesser General Public License for more details. 13 | */ 14 | 15 | #include "tester.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | static void srvcb(struct tester *t, int fd) 24 | { 25 | tester_read_cmdreq(fd, "nosuchreq"); 26 | tester_write_cmdunknown(fd); 27 | } 28 | 29 | static void reqcb(struct davici_conn *c, int err, const char *name, 30 | struct davici_response *res, void *user) 31 | { 32 | struct tester *t = user; 33 | 34 | assert(strcmp(name, "nosuchreq") == 0); 35 | assert(err == -ENOSYS); 36 | assert(res == NULL); 37 | 38 | tester_complete(t); 39 | } 40 | 41 | int main(int argc, char *argv[]) 42 | { 43 | struct tester *t; 44 | struct davici_conn *c; 45 | struct davici_request *r; 46 | 47 | t = tester_create(srvcb); 48 | assert(davici_connect_unix(tester_getpath(t), 49 | tester_davici_iocb, t, &c) >= 0); 50 | assert(davici_new_cmd("nosuchreq", &r) >= 0); 51 | assert(davici_queue(c, r, reqcb, t) >= 0); 52 | 53 | tester_runio(t, c); 54 | davici_disconnect(c); 55 | tester_cleanup(t); 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /tests/dump.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Tobias Brunner 3 | * HSR Hochschule fuer Technik Rapperswil 4 | * 5 | * Copyright (C) 2015 onway ag 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | */ 17 | 18 | #include "config.h" 19 | #include "tester.h" 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | static void echocb(struct tester *t, int fd) 29 | { 30 | char buf[2048]; 31 | uint32_t len; 32 | 33 | len = tester_read_cmdreq(fd, "echoreq"); 34 | assert(len < sizeof(buf)); 35 | assert(read(fd, buf, len) == len); 36 | tester_write_cmdres(fd, buf, len); 37 | } 38 | 39 | static void verify(FILE *f, const char *exp) 40 | { 41 | char buf[512]; 42 | 43 | rewind(f); 44 | assert(fread(buf, strlen(exp), 1, f) == 1); 45 | assert(strncmp(buf, exp, strlen(exp)) == 0); 46 | assert(fread(buf, 1, 1, f) == 0); 47 | assert(feof(f)); 48 | } 49 | 50 | static void reqcb(struct davici_conn *c, int err, const char *name, 51 | struct davici_response *res, void *user) 52 | { 53 | struct tester *t = user; 54 | FILE *f; 55 | 56 | assert(err >= 0); 57 | 58 | f = tmpfile(); 59 | assert(f); 60 | assert(davici_dump(res, name, " ", 0, 0, f)); 61 | verify(f, "echoreq { section { key = value list [ item ] } }"); 62 | fclose(f); 63 | 64 | f = tmpfile(); 65 | assert(f); 66 | assert(davici_dump(res, name, "\n", 1, 2, f)); 67 | verify(f, 68 | " echoreq {\n" 69 | " section {\n" 70 | " key = value\n" 71 | " list [\n" 72 | " item\n" 73 | " ]\n" 74 | " }\n" 75 | " }"); 76 | fclose(f); 77 | 78 | tester_complete(t); 79 | } 80 | 81 | int main(int argc, char *argv[]) 82 | { 83 | struct tester *t; 84 | struct davici_conn *c; 85 | struct davici_request *r; 86 | 87 | t = tester_create(echocb); 88 | assert(davici_connect_unix(tester_getpath(t), 89 | tester_davici_iocb, t, &c) >= 0); 90 | assert(davici_new_cmd("echoreq", &r) >= 0); 91 | 92 | davici_section_start(r, "section"); 93 | davici_kvf(r, "key", "%s", "value"); 94 | davici_list_start(r, "list"); 95 | davici_list_itemf(r, "%s", "item"); 96 | davici_list_end(r); 97 | davici_section_end(r); 98 | 99 | assert(davici_queue(c, r, reqcb, t) >= 0); 100 | 101 | tester_runio(t, c); 102 | davici_disconnect(c); 103 | tester_cleanup(t); 104 | return 0; 105 | } 106 | -------------------------------------------------------------------------------- /tests/event.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 onway ag 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library 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 GNU 12 | * Lesser General Public License for more details. 13 | */ 14 | 15 | #include "tester.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | static unsigned int seen = 0; 23 | 24 | static void echocb(struct tester *t, int fd) 25 | { 26 | static int state = 0; 27 | 28 | switch (state++) 29 | { 30 | case 0: 31 | tester_read_eventreg(fd, "anevent"); 32 | tester_write_eventconfirm(fd); 33 | tester_write_event(fd, "anevent", NULL, 0); 34 | break; 35 | case 1: 36 | tester_read_eventreg(fd, "another"); 37 | tester_write_eventconfirm(fd); 38 | tester_write_event(fd, "another", NULL, 0); 39 | break; 40 | case 2: 41 | tester_read_eventunreg(fd, "another"); 42 | tester_write_eventconfirm(fd); 43 | break; 44 | case 3: 45 | tester_read_eventunreg(fd, "anevent"); 46 | tester_write_eventconfirm(fd); 47 | break; 48 | default: 49 | assert(0); 50 | break; 51 | } 52 | } 53 | 54 | static void eventcb(struct davici_conn *c, int err, const char *name, 55 | struct davici_response *res, void *user) 56 | { 57 | struct tester *t = user; 58 | 59 | assert(strcmp(name, "anevent") == 0); 60 | assert(err >= 0); 61 | if (res) 62 | { 63 | assert(davici_parse(res) == DAVICI_END); 64 | seen++; 65 | } 66 | else if (seen == 2) 67 | { 68 | tester_complete(t); 69 | } 70 | } 71 | 72 | static void anothercb(struct davici_conn *c, int err, const char *name, 73 | struct davici_response *res, void *user) 74 | { 75 | assert(strcmp(name, "another") == 0); 76 | assert(err >= 0); 77 | if (res) 78 | { 79 | assert(davici_parse(res) == DAVICI_END); 80 | seen++; 81 | } 82 | } 83 | 84 | int main(int argc, char *argv[]) 85 | { 86 | struct tester *t; 87 | struct davici_conn *c; 88 | 89 | t = tester_create(echocb); 90 | assert(davici_connect_unix(tester_getpath(t), 91 | tester_davici_iocb, t, &c) >= 0); 92 | assert(davici_register(c, "anevent", eventcb, t) >= 0); 93 | assert(davici_register(c, "another", anothercb, t) >= 0); 94 | assert(davici_unregister(c, "another", anothercb, t) >= 0); 95 | assert(davici_unregister(c, "anevent", eventcb, t) >= 0); 96 | 97 | tester_runio(t, c); 98 | assert(seen == 2); 99 | davici_disconnect(c); 100 | tester_cleanup(t); 101 | return 0; 102 | } 103 | -------------------------------------------------------------------------------- /tests/eventunknown.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 onway ag 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library 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 GNU 12 | * Lesser General Public License for more details. 13 | */ 14 | 15 | #include "tester.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | static void srvcb(struct tester *t, int fd) 24 | { 25 | tester_read_eventreg(fd, "nosuchevent"); 26 | tester_write_eventunknown(fd); 27 | } 28 | 29 | static void eventcb(struct davici_conn *c, int err, const char *name, 30 | struct davici_response *res, void *user) 31 | { 32 | struct tester *t = user; 33 | 34 | assert(strcmp(name, "nosuchevent") == 0); 35 | assert(err == -ENOENT); 36 | assert(res == NULL); 37 | 38 | tester_complete(t); 39 | } 40 | 41 | int main(int argc, char *argv[]) 42 | { 43 | struct tester *t; 44 | struct davici_conn *c; 45 | 46 | t = tester_create(srvcb); 47 | assert(davici_connect_unix(tester_getpath(t), 48 | tester_davici_iocb, t, &c) >= 0); 49 | assert(davici_register(c, "nosuchevent", eventcb, t) >= 0); 50 | 51 | tester_runio(t, c); 52 | davici_disconnect(c); 53 | tester_cleanup(t); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /tests/many.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 onway ag 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library 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 GNU 12 | * Lesser General Public License for more details. 13 | */ 14 | 15 | #include "tester.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | static const unsigned int request_count = 512; 23 | static unsigned int seen = 0; 24 | 25 | static void echocb(struct tester *t, int fd) 26 | { 27 | char buf[32768]; 28 | uint32_t len; 29 | 30 | len = tester_read_cmdreq(fd, "echoreq"); 31 | assert(len < sizeof(buf)); 32 | assert(read(fd, buf, len) == len); 33 | tester_write_cmdres(fd, buf, len); 34 | } 35 | 36 | static void reqcb(struct davici_conn *c, int err, const char *name, 37 | struct davici_response *res, void *user) 38 | { 39 | struct tester *t = user; 40 | int ret; 41 | 42 | assert(err >= 0); 43 | 44 | while (1) 45 | { 46 | ret = davici_parse(res); 47 | assert(ret >= 0); 48 | if (ret == 0) 49 | { 50 | break; 51 | } 52 | } 53 | if (++seen == request_count) 54 | { 55 | tester_complete(t); 56 | } 57 | assert(seen <= request_count); 58 | } 59 | 60 | int main(int argc, char *argv[]) 61 | { 62 | struct tester *t; 63 | struct davici_conn *c; 64 | struct davici_request *r; 65 | int i, j, k; 66 | 67 | t = tester_create(echocb); 68 | assert(davici_connect_unix(tester_getpath(t), 69 | tester_davici_iocb, t, &c) >= 0); 70 | 71 | assert(davici_queue_len(c) == 0); 72 | for (i = 0; i < request_count; i++) 73 | { 74 | assert(davici_new_cmd("echoreq", &r) >= 0); 75 | for (j = 0; j < 8; j++) 76 | { 77 | davici_section_start(r, "section"); 78 | for (k = 0; k < 16; k++) 79 | { 80 | davici_kvf(r, "key", "value-%d", k); 81 | } 82 | davici_list_start(r, "list"); 83 | for (k = 0; k < 32; k++) 84 | { 85 | davici_list_itemf(r, "item-%d", k); 86 | } 87 | davici_list_end(r); 88 | davici_section_end(r); 89 | } 90 | assert(davici_queue(c, r, reqcb, t) >= 0); 91 | assert(davici_queue_len(c) == i + 1); 92 | } 93 | 94 | tester_runio(t, c); 95 | assert(seen == request_count); 96 | assert(davici_queue_len(c) == 0); 97 | davici_disconnect(c); 98 | tester_cleanup(t); 99 | return 0; 100 | } 101 | -------------------------------------------------------------------------------- /tests/recurse.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 onway ag 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library 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 GNU 12 | * Lesser General Public License for more details. 13 | */ 14 | 15 | #include "config.h" 16 | #include "tester.h" 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | static void echocb(struct tester *t, int fd) 27 | { 28 | char buf[2048]; 29 | uint32_t len; 30 | 31 | len = tester_read_cmdreq(fd, "echoreq"); 32 | assert(len < sizeof(buf)); 33 | assert(read(fd, buf, len) == len); 34 | tester_write_cmdres(fd, buf, len); 35 | } 36 | 37 | static int kv_cb(struct davici_response *res, void *user) 38 | { 39 | int *i = user; 40 | char buf[64]; 41 | int err, v; 42 | 43 | err = davici_get_value_str(res, buf, sizeof(buf)); 44 | if (err < 0) 45 | { 46 | return err; 47 | } 48 | switch ((*i)++) 49 | { 50 | case 1: 51 | assert(davici_name_strcmp(res, "key") == 0); 52 | assert(strcmp(buf, "value") == 0); 53 | break; 54 | case 3: 55 | case 4: 56 | case 5: 57 | assert(davici_name_strcmp(res, "list") == 0); 58 | assert(davici_value_escanf(res, "item%d", &v) >= 0); 59 | assert(davici_value_escanf(res, "item%d%d", &v, &v) == -EBADMSG); 60 | assert(davici_value_escanf(res, "%4c%d", &buf, &v) >= 0); 61 | assert(davici_value_escanf(res, "%4c", &buf) == -EBADMSG); 62 | assert(v == (*i) - 3); 63 | break; 64 | case 7: 65 | assert(davici_name_strcmp(res, "v") == 0); 66 | assert(strcmp(buf, "w") == 0); 67 | break; 68 | case 8: 69 | assert(davici_name_strcmp(res, "x") == 0); 70 | assert(strcmp(buf, "y") == 0); 71 | break; 72 | default: 73 | assert(0); 74 | break; 75 | } 76 | return 0; 77 | } 78 | 79 | static int section_cb(struct davici_response *res, void *user) 80 | { 81 | int *i = user; 82 | int err; 83 | 84 | switch ((*i)++) 85 | { 86 | case 0: 87 | assert(davici_name_strcmp(res, "section") == 0); 88 | break; 89 | case 2: 90 | assert(davici_name_strcmp(res, "subsection") == 0); 91 | break; 92 | case 6: 93 | assert(davici_name_strcmp(res, "subsub") == 0); 94 | break; 95 | default: 96 | assert(0); 97 | break; 98 | } 99 | err = davici_recurse(res, section_cb, kv_cb, kv_cb, i); 100 | if (err < 0) 101 | { 102 | return err; 103 | } 104 | return 0; 105 | } 106 | 107 | static void reqcb(struct davici_conn *c, int err, const char *name, 108 | struct davici_response *res, void *user) 109 | { 110 | struct tester *t = user; 111 | int i = 0; 112 | 113 | assert(err >= 0); 114 | assert(davici_recurse(res, section_cb, kv_cb, kv_cb, &i) >= 0); 115 | 116 | tester_complete(t); 117 | } 118 | 119 | int main(int argc, char *argv[]) 120 | { 121 | struct tester *t; 122 | struct davici_conn *c; 123 | struct davici_request *r; 124 | 125 | t = tester_create(echocb); 126 | assert(davici_connect_unix(tester_getpath(t), 127 | tester_davici_iocb, t, &c) >= 0); 128 | assert(davici_new_cmd("echoreq", &r) >= 0); 129 | 130 | davici_section_start(r, "section"); 131 | davici_kvf(r, "key", "%s", "value"); 132 | davici_section_start(r, "subsection"); 133 | davici_list_start(r, "list"); 134 | davici_list_itemf(r, "%s", "item1"); 135 | davici_list_itemf(r, "%s", "item2"); 136 | davici_list_itemf(r, "%s", "item3"); 137 | davici_list_end(r); 138 | davici_section_start(r, "subsub"); 139 | davici_kvf(r, "v", "w"); 140 | davici_kvf(r, "x", "y"); 141 | davici_section_end(r); 142 | davici_section_end(r); 143 | davici_section_end(r); 144 | 145 | assert(davici_queue(c, r, reqcb, t) >= 0); 146 | 147 | tester_runio(t, c); 148 | davici_disconnect(c); 149 | tester_cleanup(t); 150 | return 0; 151 | } 152 | -------------------------------------------------------------------------------- /tests/stream.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 onway ag 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library 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 GNU 12 | * Lesser General Public License for more details. 13 | */ 14 | 15 | #include "tester.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | static unsigned int stream_count = 8; 23 | static unsigned int seen = 0; 24 | 25 | static void echocb(struct tester *t, int fd) 26 | { 27 | char ev[] = {0x03,0x05,'c','o','u','n','t',0x00,0x01,0x00}; 28 | static int state = 0; 29 | char buf[256]; 30 | uint32_t len; 31 | int i; 32 | 33 | switch (state++) 34 | { 35 | case 0: 36 | tester_read_eventreg(fd, "listevent"); 37 | tester_write_eventconfirm(fd); 38 | break; 39 | case 1: 40 | len = tester_read_cmdreq(fd, "streamreq"); 41 | assert(len < sizeof(buf)); 42 | assert(read(fd, buf, len) == len); 43 | for (i = 0; i < stream_count; i++) 44 | { 45 | ev[9] = '0' + i; 46 | tester_write_event(fd, "listevent", ev, sizeof(ev)); 47 | } 48 | tester_write_cmdres(fd, buf, len); 49 | break; 50 | case 2: 51 | tester_read_eventunreg(fd, "listevent"); 52 | tester_write_eventconfirm(fd); 53 | break; 54 | default: 55 | assert(0); 56 | break; 57 | } 58 | } 59 | 60 | static void reqcb(struct davici_conn *c, int err, const char *name, 61 | struct davici_response *res, void *user) 62 | { 63 | char buf[10]; 64 | int i, ret; 65 | 66 | assert(err >= 0); 67 | for (i = 0;; i++) 68 | { 69 | ret = davici_parse(res); 70 | switch (i) 71 | { 72 | case 0: 73 | assert(ret == DAVICI_KEY_VALUE); 74 | assert(strcmp(davici_get_name(res), "count") == 0); 75 | assert(davici_get_value_str(res, buf, sizeof(buf)) == 1); 76 | assert(buf[0] - '0' == stream_count); 77 | continue; 78 | case 1: 79 | assert(ret == DAVICI_END); 80 | break; 81 | default: 82 | assert(0); 83 | } 84 | break; 85 | } 86 | } 87 | 88 | static void streamcb(struct davici_conn *c, int err, const char *name, 89 | struct davici_response *res, void *user) 90 | { 91 | struct tester *t = user; 92 | char buf[10]; 93 | int i, ret; 94 | 95 | assert(err >= 0); 96 | if (res) 97 | { 98 | for (i = 0;; i++) 99 | { 100 | ret = davici_parse(res); 101 | switch (i) 102 | { 103 | case 0: 104 | assert(ret == DAVICI_KEY_VALUE); 105 | assert(strcmp(davici_get_name(res), "count") == 0); 106 | assert(davici_get_value_str(res, buf, sizeof(buf)) == 1); 107 | assert(buf[0] - '0' == seen++); 108 | continue; 109 | case 1: 110 | assert(ret == DAVICI_END); 111 | break; 112 | default: 113 | assert(0); 114 | } 115 | break; 116 | } 117 | } 118 | else if (seen == stream_count) 119 | { 120 | tester_complete(t); 121 | } 122 | } 123 | 124 | int main(int argc, char *argv[]) 125 | { 126 | struct tester *t; 127 | struct davici_conn *c; 128 | struct davici_request *r; 129 | 130 | t = tester_create(echocb); 131 | assert(davici_connect_unix(tester_getpath(t), 132 | tester_davici_iocb, t, &c) >= 0); 133 | assert(davici_new_cmd("streamreq", &r) >= 0); 134 | davici_kvf(r, "count", "%d", stream_count); 135 | assert(davici_queue_streamed(c, r, reqcb, "listevent", streamcb, t) >= 0); 136 | 137 | tester_runio(t, c); 138 | assert(seen == stream_count); 139 | davici_disconnect(c); 140 | tester_cleanup(t); 141 | return 0; 142 | } 143 | -------------------------------------------------------------------------------- /tests/tester.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 onway ag 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library 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 GNU 12 | * Lesser General Public License for more details. 13 | */ 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include "tester.h" 30 | 31 | enum tester_fd { 32 | FD_CLIENT = 0, 33 | FD_LISTEN = 1, 34 | FD_SERVER = 2, 35 | FD_COUNT 36 | }; 37 | 38 | enum tester_type { 39 | CMD_REQUEST = 0, 40 | CMD_RESPONSE = 1, 41 | CMD_UNKNOWN = 2, 42 | EVENT_REGISTER = 3, 43 | EVENT_UNREGISTER = 4, 44 | EVENT_CONFIRM = 5, 45 | EVENT_UNKNOWN = 6, 46 | EVENT = 7, 47 | }; 48 | 49 | struct tester { 50 | struct pollfd pfd[FD_COUNT]; 51 | const char *path; 52 | tester_srvcb srvcb; 53 | int complete; 54 | }; 55 | 56 | struct tester* tester_create(tester_srvcb srvcb) 57 | { 58 | struct sockaddr_un addr; 59 | struct tester *t; 60 | int len; 61 | 62 | t = calloc(1, sizeof(*t)); 63 | assert(t); 64 | t->path = "/tmp/test.vici"; 65 | t->srvcb = srvcb; 66 | 67 | memset(&addr, 0, sizeof(addr)); 68 | addr.sun_family = AF_UNIX; 69 | snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", t->path); 70 | len = offsetof(struct sockaddr_un, sun_path) + strlen(addr.sun_path); 71 | 72 | t->pfd[FD_LISTEN].fd = socket(AF_UNIX, SOCK_STREAM, 0); 73 | assert(t->pfd[FD_LISTEN].fd >= 0); 74 | t->pfd[FD_LISTEN].events = POLLIN; 75 | t->pfd[FD_SERVER].events = POLLIN; 76 | t->pfd[FD_SERVER].fd = -1; 77 | 78 | unlink(t->path); 79 | assert(bind(t->pfd[FD_LISTEN].fd, (struct sockaddr*)&addr, len) == 0); 80 | assert(listen(t->pfd[FD_LISTEN].fd, 2) == 0); 81 | 82 | return t; 83 | } 84 | 85 | int tester_davici_iocb(struct davici_conn *c, int fd, int ops, void *user) 86 | { 87 | struct tester *t = user; 88 | 89 | t->pfd[FD_CLIENT].fd = fd; 90 | t->pfd[FD_CLIENT].events = 0; 91 | if (ops & DAVICI_READ) 92 | { 93 | t->pfd[FD_CLIENT].events |= POLLIN; 94 | } 95 | if (ops & DAVICI_WRITE) 96 | { 97 | t->pfd[FD_CLIENT].events |= POLLOUT; 98 | } 99 | return 0; 100 | } 101 | 102 | void tester_runio(struct tester *t, struct davici_conn *c) 103 | { 104 | while (!t->complete) 105 | { 106 | int fd; 107 | 108 | assert(poll(t->pfd, sizeof(t->pfd) / sizeof(t->pfd[0]), -1) >= 0); 109 | if (t->pfd[FD_CLIENT].revents & POLLIN) 110 | { 111 | assert(davici_read(c) >= 0); 112 | } 113 | if (t->pfd[FD_CLIENT].revents & POLLOUT) 114 | { 115 | assert(davici_write(c) >= 0); 116 | } 117 | if (t->pfd[FD_LISTEN].revents & POLLIN) 118 | { 119 | fd = accept(t->pfd[FD_LISTEN].fd, NULL, NULL); 120 | assert(fd >= 0); 121 | t->pfd[FD_SERVER].fd = fd; 122 | } 123 | if (t->pfd[FD_SERVER].revents & POLLIN) 124 | { 125 | t->srvcb(t, t->pfd[FD_SERVER].fd); 126 | } 127 | } 128 | } 129 | 130 | void tester_complete(struct tester *t) 131 | { 132 | t->complete = 1; 133 | } 134 | 135 | const char *tester_getpath(struct tester *t) 136 | { 137 | return t->path; 138 | } 139 | 140 | void tester_cleanup(struct tester *t) 141 | { 142 | close(t->pfd[FD_LISTEN].fd); 143 | close(t->pfd[FD_SERVER].fd); 144 | free(t); 145 | } 146 | 147 | static unsigned int read_type(int fd, const char *name, uint8_t expected) 148 | { 149 | uint8_t type, namelen; 150 | uint32_t pktlen; 151 | char buf[257]; 152 | 153 | assert(read(fd, &pktlen, sizeof(pktlen)) == sizeof(pktlen)); 154 | assert(read(fd, &type, sizeof(type)) == sizeof(type)); 155 | assert(type == expected); 156 | assert(read(fd, &namelen, sizeof(namelen)) == sizeof(namelen)); 157 | assert(read(fd, buf, namelen) == namelen); 158 | buf[namelen] = 0; 159 | assert(strcmp(buf, name) == 0); 160 | 161 | return ntohl(pktlen) - sizeof(type) - sizeof(namelen) - namelen; 162 | } 163 | 164 | unsigned int tester_read_cmdreq(int fd, const char *name) 165 | { 166 | return read_type(fd, name, CMD_REQUEST); 167 | } 168 | 169 | void tester_write_cmdres(int fd, const char *buf, unsigned int buflen) 170 | { 171 | uint8_t type = CMD_RESPONSE; 172 | uint32_t len; 173 | 174 | len = htonl(sizeof(type) + buflen); 175 | assert(write(fd, &len, sizeof(len)) == sizeof(len)); 176 | assert(write(fd, &type, sizeof(type)) == sizeof(type)); 177 | assert(write(fd, buf, buflen) == buflen); 178 | } 179 | 180 | void tester_write_cmdunknown(int fd) 181 | { 182 | uint8_t type = CMD_UNKNOWN; 183 | uint32_t len; 184 | 185 | len = htonl(sizeof(type)); 186 | assert(write(fd, &len, sizeof(len)) == sizeof(len)); 187 | assert(write(fd, &type, sizeof(type)) == sizeof(type)); 188 | } 189 | 190 | unsigned int tester_read_eventreg(int fd, const char *name) 191 | { 192 | return read_type(fd, name, EVENT_REGISTER); 193 | } 194 | 195 | unsigned int tester_read_eventunreg(int fd, const char *name) 196 | { 197 | return read_type(fd, name, EVENT_UNREGISTER); 198 | } 199 | 200 | void tester_write_eventconfirm(int fd) 201 | { 202 | uint8_t type = EVENT_CONFIRM; 203 | uint32_t len; 204 | 205 | len = htonl(sizeof(type)); 206 | assert(write(fd, &len, sizeof(len)) == sizeof(len)); 207 | assert(write(fd, &type, sizeof(type)) == sizeof(type)); 208 | } 209 | 210 | void tester_write_eventunknown(int fd) 211 | { 212 | uint8_t type = EVENT_UNKNOWN; 213 | uint32_t len; 214 | 215 | len = htonl(sizeof(type)); 216 | assert(write(fd, &len, sizeof(len)) == sizeof(len)); 217 | assert(write(fd, &type, sizeof(type)) == sizeof(type)); 218 | } 219 | 220 | void tester_write_event(int fd, const char *name, 221 | const char *buf, unsigned int buflen) 222 | { 223 | uint8_t namelen, type = EVENT; 224 | uint32_t len; 225 | 226 | namelen = strlen(name); 227 | len = htonl(sizeof(type) + sizeof(namelen) + namelen + buflen); 228 | assert(write(fd, &len, sizeof(len)) == sizeof(len)); 229 | assert(write(fd, &type, sizeof(type)) == sizeof(type)); 230 | assert(write(fd, &namelen, sizeof(namelen)) == sizeof(namelen)); 231 | assert(write(fd, name, namelen) == namelen); 232 | assert(write(fd, buf, buflen) == buflen); 233 | } 234 | -------------------------------------------------------------------------------- /tests/tester.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 onway ag 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library 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 GNU 12 | * Lesser General Public License for more details. 13 | */ 14 | 15 | #include 16 | 17 | struct tester; 18 | 19 | typedef void (*tester_srvcb)(struct tester *tester, int fd); 20 | 21 | struct tester* tester_create(tester_srvcb srvcb); 22 | 23 | int tester_davici_iocb(struct davici_conn *c, int fd, int ops, void *user); 24 | 25 | void tester_runio(struct tester *tester, struct davici_conn *c); 26 | 27 | void tester_complete(struct tester *tester); 28 | 29 | const char *tester_getpath(struct tester *tester); 30 | 31 | void tester_cleanup(struct tester *tester); 32 | 33 | unsigned int tester_read_cmdreq(int fd, const char *name); 34 | 35 | void tester_write_cmdres(int fd, const char *buf, unsigned int buflen); 36 | 37 | void tester_write_cmdunknown(int fd); 38 | 39 | unsigned int tester_read_eventreg(int fd, const char *name); 40 | 41 | unsigned int tester_read_eventunreg(int fd, const char *name); 42 | 43 | void tester_write_eventconfirm(int fd); 44 | 45 | void tester_write_eventunknown(int fd); 46 | 47 | void tester_write_event(int fd, const char *name, 48 | const char *buf, unsigned int buflen); 49 | --------------------------------------------------------------------------------