1 /* 2 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2003, 2006, 2008, 2009, 2010, 3 * 2011, 2012, 2013, 2014 4 * Inferno Nettverk A/S, Norway. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. The above copyright notice, this list of conditions and the following 10 * disclaimer must appear in all copies of the software, derivative works 11 * or modified versions, and any portions thereof, aswell as in all 12 * supporting documentation. 13 * 2. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by 16 * Inferno Nettverk A/S, Norway. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * Inferno Nettverk A/S requests users of this software to return to 32 * 33 * Software Distribution Coordinator or sdc@inet.no 34 * Inferno Nettverk A/S 35 * Oslo Research Park 36 * Gaustadall�en 21 37 * NO-0349 Oslo 38 * Norway 39 * 40 * any improvements or extensions that they make and grant Inferno Nettverk A/S 41 * the rights to redistribute these changes. 42 * 43 */ 44 45 /* $Id: tostring.h,v 1.99.4.5 2014/08/15 18:16:40 karls Exp $ */ 46 47 #ifndef _TOSTRING_H_ 48 #define _TOSTRING_H_ 49 50 #define QUOTE(a) a 51 #define QUOTE0() "" 52 53 char * 54 aihints2string(const struct addrinfo *hints, char *string, size_t len); 55 /* 56 * Returns a printable representation of "hints". 57 * 58 * If "string" and "len" is NULL and zero, the function returns a 59 * string that will be overwritten on the next call to this function. 60 */ 61 62 63 char * 64 fdset2string(const int nfds, const fd_set *set, const int docheck, 65 char *buf, size_t buflen); 66 /* 67 * Returns a printable representation of the fd_set "set", which may 68 * have up to "nfds" fds set. 69 * 70 * If "docheck" is true, will check each fd in the set for being a valid 71 * fd. 72 */ 73 74 int32_t 75 string2portnumber(const char *string, char *emsg, size_t emsglen); 76 /* 77 * Returns the integer corresponding to the portnumber represented by 78 * the string "string", or -1 on error. 79 * On error, "emsg" and "emsglen" contains the reason for error. 80 */ 81 82 enum operator_t 83 string2operator(const char *operator); 84 /* 85 * Returns the enum for the string representation of a operator. 86 * Can't fail. 87 */ 88 89 const char * 90 operator2string(enum operator_t operator); 91 /* 92 * Returns the string representation of the operator. 93 * Can't fail. 94 */ 95 96 /* 97 * Bitmask of info to include when doing 2string conversion, in addition 98 * to the IP-address or hostname. 99 */ 100 #define ADDRINFO_PORT (1) 101 #define ADDRINFO_ATYPE (2) 102 #define ADDRINFO_SCOPEID (4) 103 104 const char * 105 ruleaddr2string(const ruleaddr_t *rule, const size_t includeinfo, 106 char *string, size_t len) 107 __ATTRIBUTE__((__BOUNDED__(__string__, 3, 4))); 108 /* 109 * Writes "rule" out as a string. The string is written to "string", 110 * which is of size "len". 111 * 112 * If "includeinfo" is set, it specifies additional information, 113 * apart from the ipaddress/hostname, to include in the string. 114 * 115 * If "string" and "len" is NULL and zero, the function returns a 116 * string that will be overwritten on the next call to this function. 117 * 118 * Returns: "string". 119 */ 120 121 const char * 122 command2string(int command); 123 /* 124 * Returns a printable representation of the socks command "command". 125 * Can't fail. 126 */ 127 128 char * 129 commands2string(const command_t *command, char *str, size_t strsize) 130 __ATTRIBUTE__((__NONNULL__(2))) 131 __ATTRIBUTE__((__BOUNDED__(__string__, 2, 3))); 132 /* 133 * Returns a printable representation of "commands". 134 * "str" is the memory to write the printable representation into, 135 * "strsize" is the size of the memory. 136 * 137 * Returns: "str", NUL terminated. 138 */ 139 140 char * 141 methods2string(size_t methodc, const int *methodv, char *str, size_t strsize) 142 __ATTRIBUTE__((__BOUNDED__(__string__, 3, 4))); 143 /* 144 * Returns a printable representation of the methods "methodv", of 145 * length "methodc". 146 * "str" is the memory to write the printable representation into, 147 * "strsize" is the size of the memory. 148 * 149 * Returns: "str", NUL terminated. 150 */ 151 152 const char * 153 protocol2string(int protocol); 154 /* 155 * Returns a printable representation of "protocol". 156 * Can't fail. 157 */ 158 159 char * 160 protocols2string(const protocol_t *protocols, char *str, size_t strsize) 161 __ATTRIBUTE__((__NONNULL__(2))) 162 __ATTRIBUTE__((__BOUNDED__(__string__, 2, 3))); 163 /* 164 * Returns a printable representation of "protocols". 165 * "str" is the memory to write the printable representation into, 166 * "strsize" is the size of the memory. 167 * 168 * Returns: "str", NUL terminated. 169 */ 170 171 char * 172 proxyprotocols2string(const proxyprotocol_t *proxyprotocols, char *str, 173 size_t strsize) 174 __ATTRIBUTE__((__BOUNDED__(__string__, 2, 3))); 175 /* 176 * Returns a printable representation of "protocols". 177 * "str" is the memory to write the printable representation into, 178 * "strsize" is the size of the memory. 179 * 180 * If "str" and "strsize" is NULL and zero, the function returns a 181 * string that will be overwritten on the next call to this function. 182 * 183 * Returns: "str", NUL terminated. 184 */ 185 186 const char * 187 method2string(int method); 188 /* 189 * Returns a printable representation of the authmethod "method". 190 */ 191 192 const char * 193 gssapiprotection2string(const int protection); 194 /* 195 * Returns a printable representation of the gssapi protection "protection". 196 */ 197 198 int 199 string2method(const char *methodname); 200 /* 201 * If "methodname" is the name of a supported method, the protocol 202 * value of that method is returned. 203 * Otherwise, -1 is returned. 204 */ 205 206 char * 207 sockshost2string2(const sockshost_t *host, const size_t includeinfo, 208 char *string, size_t len) 209 __ATTRIBUTE__((__BOUNDED__(__string__, 3, 4))); 210 /* 211 * Writes "host" out as a string. The string is written to "string", 212 * which is of length "len", including NUL termination. 213 * 214 * If "includeinfo" is not zero, it indicates extra info to include. 215 * 216 * If "string" and "len" is NULL and zero, the function returns a 217 * string that will be overwritten on the next call to this function. 218 * 219 * Returns: "string". 220 */ 221 222 char * 223 sockshost2string(const sockshost_t *host, char *string, size_t len) 224 __ATTRIBUTE__((__BOUNDED__(__string__, 2, 3))); 225 /* 226 * Wrapper around sockshost2string2() that does not include any extra info. 227 */ 228 229 230 char * 231 sockaddr2string2(const struct sockaddr_storage *addr, const size_t includeinfo, 232 char *string, size_t len) 233 __ATTRIBUTE__((__BOUNDED__(__string__, 3, 4))); 234 /* 235 * Returns the IP addr and port in "addr" on string form. 236 * 237 * "addr" is assumed to be on network form and it will be 238 * converted to host form before written to "string". 239 * "len" gives length of the NUL terminated string. 240 * 241 * If "includeinfo" is set, it specifies additional information, 242 * apart from the ipaddress/hostname, to include in the string. 243 * 244 * If "string" and "len" is NULL and zero, the function returns a 245 * string that will be overwritten on the next call to this function. 246 * 247 * Returns: "string". 248 */ 249 250 char * 251 sockaddr2string(const struct sockaddr_storage *addr, char *string, size_t len) 252 __ATTRIBUTE__((__BOUNDED__(__string__, 2, 3))); 253 /* 254 * Like sockaddr2string2(), but prints what we normally want to print. 255 */ 256 257 258 char * 259 addr2hexstring(const void *addr, const sa_family_t safamily, 260 char *string, size_t len); 261 /* 262 * prints "addr" of type "safamily", in hex format, to the string "string", 263 * which should be of size "len". 264 * 265 * If "string" and "len" is zero, uses a statically allocated string. 266 * 267 * Returns "addr" on hex form, stored in "string". 268 */ 269 270 udpheader_t * 271 string2udpheader(const char *data, size_t len, udpheader_t *header) 272 __ATTRIBUTE__((__NONNULL__(1))) 273 __ATTRIBUTE__((__BOUNDED__(__string__, 1, 2))); 274 /* 275 * Converts "data" to udpheader_t representation. 276 * "len" is length of "data". 277 * "data" is assumed to be in network order. 278 * Returns: 279 * On success: pointer to a udpheader_t in static memory. 280 * On failure: NULL ("data" is not a valid udp packet). In this case, 281 * udpheader is bzero(3)-ed. 282 */ 283 284 const char * 285 socks_packet2string(const void *packet, int isrequest); 286 /* 287 * debug function; dumps socks packet content. 288 * "packet" is a socks packet, "isrequest" is set if it is a request 289 * packet, false otherwise. 290 * 291 * Returns: 292 * On success: 0 293 * On failure: -1 294 */ 295 296 char * 297 extensions2string(const extension_t *extensions, char *str, 298 size_t strsize) 299 __ATTRIBUTE__((__NONNULL__(2))) 300 __ATTRIBUTE__((__BOUNDED__(__string__, 2, 3))); 301 /* 302 * Returns a printable representation of "extensions". 303 * "str" is the memory to write the printable representation into, 304 * "strsize" is the size of the memory. 305 * 306 * Returns: "str", NUL terminated. 307 */ 308 309 const char * 310 resolveprotocol2string(int resolveprotocol); 311 /* 312 * Returns a printable representation of "resolveprotocol". 313 */ 314 315 char * 316 str2upper(char *string); 317 /* 318 * converts all characters in "string" to uppercase. 319 * returns "string". 320 */ 321 322 char * 323 sockname2string(const int s, char *buf, size_t buflen) 324 __ATTRIBUTE__((__BOUNDED__(__string__, 2, 3))); 325 /* 326 * Returns a printable representation of the local address of socket 327 * "s", or NULL on failure. 328 */ 329 330 char * 331 peername2string(const int s, char *buf, size_t buflen) 332 __ATTRIBUTE__((__BOUNDED__(__string__, 2, 3))); 333 /* 334 * Returns a printable representation of the remote address of socket 335 * "s", or NULL on failure. 336 */ 337 338 char * 339 socket2string(const int s, char *buf, size_t buflen) 340 __ATTRIBUTE__((__BOUNDED__(__string__, 2, 3))); 341 /* 342 * Prints out address info for the socket "s". 343 * "buf" gives the buffer to write the address info to, "buflen" the 344 * size of "buf". If buflen is zero, a statically allocated buffer will 345 * be used instead. 346 * 347 * Returns a pointer to buf. 348 */ 349 350 const char * 351 proxyprotocol2string(int version); 352 /* 353 * Returns a printable representation of the proxy protocol version "version". 354 */ 355 356 const char * 357 atype2string(const unsigned int atype); 358 /* 359 * Returns a printable representation of the atype "atype". 360 */ 361 362 const char * 363 safamily2string(const sa_family_t af); 364 /* 365 * Returns a printable representation of the socket address family "af". 366 */ 367 368 const char * 369 socktype2string(const int socktype); 370 /* 371 * Returns a printable representation of the socket type "socktype". 372 */ 373 374 char * 375 routeoptions2string(const routeoptions_t *options, char *str, size_t strsize); 376 /* 377 * Returns a printable representation of the atype "atype". 378 * If "str" and "strsize" is NULL and zero, the function returns a 379 * string that will be overwritten on the next call to this function. 380 */ 381 382 383 const char * 384 loglevel2string(const int loglevel); 385 /* 386 * returns a printable representation of the loglevel "loglevel". 387 */ 388 389 const char * 390 signal2string(const int sig); 391 /* 392 * returns a printable representation of the signalnumber "sig". 393 */ 394 395 396 char * 397 logtypes2string(const logtype_t *logtypes, char *str, size_t strsize) 398 __ATTRIBUTE__((__NONNULL__(2))) 399 __ATTRIBUTE__((__BOUNDED__(__string__, 2, 3))); 400 /* 401 * Returns a printable representation of "logtypes". 402 * "str" is the memory to write the printable representation into, 403 * "strsize" is the size of the memory. 404 * 405 * Returns: "str", NUL terminated. 406 */ 407 408 const char * 409 sockoptval2string(socketoptvalue_t value, socketoptvalue_type_t type, 410 char *str, size_t strsize); 411 /* 412 * Returns a printable representation of socket option value "value", 413 * type "type". 414 */ 415 416 const char * 417 sockoptlevel2string(int level); 418 /* 419 * Returns a printable representation of the socket option "level" value. 420 */ 421 422 const char * 423 sockoptvaltype2string(socketoptvalue_type_t type); 424 /* 425 * Returns a printable representation of the socket option "level" value. 426 */ 427 428 const char * 429 sockopt2string(const socketoption_t *opt, char *str, size_t strsize); 430 /* 431 * Returns a printable representation of the socketoption_t "opt". 432 */ 433 434 char * 435 ltoa(long l, char *buf, size_t buflen); 436 /* 437 * Returns the value of "l" as a string, stored in buf, which should 438 * be of at least 22 bytes. 439 * If "buf" is NULL, returns a pointer to a statically allocated string 440 * instead of "buf". 441 */ 442 443 const char * 444 socketsettime2string(const int whichtime); 445 /* 446 * Returns string representation of the socket option setting time "whichtime". 447 */ 448 449 450 451 #if !SOCKS_CLIENT 452 const char * 453 interfaceside2string(const interfaceside_t side); 454 /* 455 * Returns a printable representation of the interface side "side". 456 */ 457 458 char * 459 interfaceprotocol2string(const interfaceprotocol_t *ifproto, 460 char *str, size_t strsize); 461 /* 462 * Returns a printable representation of "if". 463 */ 464 465 char * 466 networktest2string(const networktest_t *test, char *str, size_t strlen); 467 /* 468 * Returns a printable representation of "test". 469 */ 470 471 const char * 472 addrscope2string(const ipv6_addrscope_t scope); 473 /* 474 * Returns string representation of the ipv6 address scope "scope". 475 */ 476 477 const char * 478 alarmside2string(const size_t alarmside); 479 /* 480 * Returns string representation of the alarmside "alarmside". 481 */ 482 483 const char * 484 clientinfo2string(const clientinfo_t *cinfo, char *str, size_t strsize); 485 /* 486 * Returns a printable representation of "cinfo". 487 */ 488 489 const char * 490 statekey2string(const statekey_t key); 491 /* 492 * Returns a printable representation of "key". 493 */ 494 495 statekey_t 496 string2statekey(const char *string); 497 /* 498 * Returns the statekey_t matching the string "string", or "unset" if 499 * it does not match any valid key (unset is not a valid keyvalue). 500 */ 501 502 const char * 503 objecttype2string(const objecttype_t objecttype); 504 /* 505 * returns a printable representation of "objecttype". 506 */ 507 508 char * 509 timeouts2string(const timeout_t *timeouts, const char *prefix, 510 char *str, size_t strsize) 511 __ATTRIBUTE__((__NONNULL__(3))) 512 __ATTRIBUTE__((__BOUNDED__(__string__, 3, 4))); 513 /* 514 * Returns a printable representation of "timeouts". 515 * "prefix" is prefixed to every line written to "str". 516 * "str" is the memory to write the printable representation into, 517 * "strsize" is the size of the memory. 518 * 519 * Returns: "str", NUL terminated. 520 */ 521 522 523 const char * 524 timeouttype2string(const timeouttype_t type); 525 /* 526 * Returns a printable representation of the timeout type "type". 527 */ 528 529 char * 530 logs2string(const log_t *logs, char *str, size_t strsize) 531 __ATTRIBUTE__((__BOUNDED__(__string__, 2, 3))); 532 /* 533 * Returns a printable representation of "logs". 534 * "str" is the memory to write the printable representation into, 535 * "strsize" is the size of the memory. 536 * 537 * Returns: "str", NUL terminated. 538 */ 539 540 #if !HAVE_PRIVILEGES 541 char * 542 userids2string(const userid_t *userids, const char *prefix, char *str, 543 size_t strsize) 544 __ATTRIBUTE__((__NONNULL__(3))) 545 __ATTRIBUTE__((__BOUNDED__(__string__, 3, 4))); 546 /* 547 * Returns a printable representation of "userids". 548 * "prefix" is prefixed to every line written to "str". 549 * "str" is the memory to write the printable representation into, 550 * "strsize" is the size of the memory. 551 * 552 * Returns: "str", NUL terminated. 553 */ 554 #endif /* !HAVE_PRIVILEGES */ 555 556 char * 557 options2string(const option_t *options, const char *prefix, 558 char *str, size_t strsize) 559 __ATTRIBUTE__((__NONNULL__(3))) 560 __ATTRIBUTE__((__BOUNDED__(__string__, 3, 4))); 561 /* 562 * Returns a printable representation of "options". 563 * "prefix" is prefixed to every line written to "str". 564 * "str" is the memory to write the printable representation into, 565 * "strsize" is the size of the memory. 566 * Returns: "str", NUL terminated. 567 */ 568 569 char * 570 compats2string(const compat_t *compats, char *str, size_t strsize) 571 __ATTRIBUTE__((__NONNULL__(2))) 572 __ATTRIBUTE__((__BOUNDED__(__string__, 2, 3))); 573 /* 574 * Returns a printable representation of "compats". 575 * "str" is the memory to write the printable representation into, 576 * "strsize" is the size of the memory. 577 * 578 * Returns: "str", NUL terminated. 579 */ 580 581 char * 582 list2string(const linkedname_t *list, char *str, size_t strsize) 583 __ATTRIBUTE__((__NONNULL__(2))) 584 __ATTRIBUTE__((__BOUNDED__(__string__, 2, 3))); 585 /* 586 * Returns a printable representation of "list". 587 * "str" is the memory to write the printable representation into, 588 * "strsize" is the size of the memory. 589 * 590 * Returns: "str", NUL terminated. 591 */ 592 593 const char * 594 childtype2string(int type); 595 /* 596 * returns the string representation of "type". 597 */ 598 599 const char * 600 verdict2string(int verdict); 601 /* 602 * returns the string representation of "verdict". 603 */ 604 605 char * 606 srchosts2string(const srchost_t *srchosts, const char *prefix, 607 char *str, size_t strsize) 608 __ATTRIBUTE__((__NONNULL__(3))) 609 __ATTRIBUTE__((__BOUNDED__(__string__, 3, 4))); 610 /* 611 * Returns a printable representation of "srchosts". 612 * "prefix" is prefixed to every line written to "str". 613 * "str" is the memory to write the printable representation into, 614 * "strsize" is the size of the memory. 615 * 616 * Returns: "str", NUL terminated. 617 */ 618 619 const char * 620 uid2name(uid_t uid); 621 /* 622 * If there is a mapping from "uid" to name, returns the name. 623 * Otherwise returns NULL. 624 */ 625 626 const char * 627 rotation2string(int rotation); 628 /* 629 * Returns a printable representation of "rotation". 630 */ 631 632 const char * 633 privop2string(const priv_op_t op); 634 /* 635 * Returns a printable representation of "op". 636 */ 637 638 #if HAVE_SCHED_SETAFFINITY 639 640 char * 641 cpuset2string(const cpu_set_t *set, char *str, size_t strsize) 642 __ATTRIBUTE__((__BOUNDED__(__string__, 2, 3))); 643 /* 644 * returns a printable representation of the cpu_set_t "set". 645 */ 646 647 #endif /* HAVE_SCHED_SETAFFINITY */ 648 649 #if COVENANT 650 const char *httpcode2string(const int version, const int code); 651 /* 652 * Returns a short printable representation of the http version 653 * "version" response code "code" 654 */ 655 656 #endif /* COVENANT */ 657 #endif /* !SOCKS_CLIENT */ 658 659 #endif /* !_TOSTRING_H_ */ 660