/usr/share/doc/bird-doc/prog-7.html is in bird-doc 1.6.3-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="LinuxDoc-Tools 1.0.9">
<TITLE>BIRD Programmer's Documentation: Library functions</TITLE>
<LINK HREF="prog-8.html" REL=next>
<LINK HREF="prog-6.html" REL=previous>
<LINK HREF="prog.html#toc7" REL=contents>
</HEAD>
<BODY>
<A HREF="prog-8.html">Next</A>
<A HREF="prog-6.html">Previous</A>
<A HREF="prog.html#toc7">Contents</A>
<HR>
<H2><A NAME="s7">7.</A> <A HREF="prog.html#toc7">Library functions</A></H2>
<H2><A NAME="ss7.1">7.1</A> <A HREF="prog.html#toc7.1">IP addresses</A>
</H2>
<P>
<P>BIRD uses its own abstraction of IP address in order to share the same
code for both IPv4 and IPv6. IP addresses are represented as entities
of type <I>ip_addr</I> which are never to be treated as numbers and instead
they must be manipulated using the following functions and macros.
<P>
<P><HR><H3>Function</H3>
<P><I>char *</I>
<B>ip_scope_text</B>
(<I>uint</I> <B>scope</B>) -- get textual representation of address scope
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>uint</I> <B>scope</B><DD><P>scope (<I>SCOPE_xxx</I>)
</DL>
<H3>Description</H3>
<P>Returns a pointer to a textual name of the scope given.
<HR><H3>Function</H3>
<P><I>int</I>
<B>ipa_equal</B>
(<I>ip_addr</I> <B>x</B>, <I>ip_addr</I> <B>y</B>) -- compare two IP addresses for equality
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>ip_addr</I> <B>x</B><DD><P>IP address
<DT><I>ip_addr</I> <B>y</B><DD><P>IP address
</DL>
<H3>Description</H3>
<P><B>ipa_equal()</B> returns 1 if <B>x</B> and <B>y</B> represent the same IP address, else 0.
<HR><H3>Function</H3>
<P><I>int</I>
<B>ipa_nonzero</B>
(<I>ip_addr</I> <B>x</B>) -- test if an IP address is defined
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>ip_addr</I> <B>x</B><DD><P>IP address
</DL>
<H3>Description</H3>
<P>ipa_nonzero returns 1 if <B>x</B> is a defined IP address (not all bits are zero),
else 0.
<P>The undefined all-zero address is reachable as a <CODE>IPA_NONE</CODE> macro.
<HR><H3>Function</H3>
<P><I>ip_addr</I>
<B>ipa_and</B>
(<I>ip_addr</I> <B>x</B>, <I>ip_addr</I> <B>y</B>) -- compute bitwise and of two IP addresses
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>ip_addr</I> <B>x</B><DD><P>IP address
<DT><I>ip_addr</I> <B>y</B><DD><P>IP address
</DL>
<H3>Description</H3>
<P>This function returns a bitwise and of <B>x</B> and <B>y</B>. It's primarily
used for network masking.
<HR><H3>Function</H3>
<P><I>ip_addr</I>
<B>ipa_or</B>
(<I>ip_addr</I> <B>x</B>, <I>ip_addr</I> <B>y</B>) -- compute bitwise or of two IP addresses
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>ip_addr</I> <B>x</B><DD><P>IP address
<DT><I>ip_addr</I> <B>y</B><DD><P>IP address
</DL>
<H3>Description</H3>
<P>This function returns a bitwise or of <B>x</B> and <B>y</B>.
<HR><H3>Function</H3>
<P><I>ip_addr</I>
<B>ipa_xor</B>
(<I>ip_addr</I> <B>x</B>, <I>ip_addr</I> <B>y</B>) -- compute bitwise xor of two IP addresses
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>ip_addr</I> <B>x</B><DD><P>IP address
<DT><I>ip_addr</I> <B>y</B><DD><P>IP address
</DL>
<H3>Description</H3>
<P>This function returns a bitwise xor of <B>x</B> and <B>y</B>.
<HR><H3>Function</H3>
<P><I>ip_addr</I>
<B>ipa_not</B>
(<I>ip_addr</I> <B>x</B>) -- compute bitwise negation of two IP addresses
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>ip_addr</I> <B>x</B><DD><P>IP address
</DL>
<H3>Description</H3>
<P>This function returns a bitwise negation of <B>x</B>.
<HR><H3>Function</H3>
<P><I>ip_addr</I>
<B>ipa_mkmask</B>
(<I>int</I> <B>x</B>) -- create a netmask
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>int</I> <B>x</B><DD><P>prefix length
</DL>
<H3>Description</H3>
<P>This function returns an <I>ip_addr</I> corresponding of a netmask
of an address prefix of size <B>x</B>.
<HR><H3>Function</H3>
<P><I>int</I>
<B>ipa_masklen</B>
(<I>ip_addr</I> <B>x</B>) -- calculate netmask length
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>ip_addr</I> <B>x</B><DD><P>IP address
</DL>
<H3>Description</H3>
<P>This function checks whether <B>x</B> represents a valid netmask and
returns the size of the associate network prefix or -1 for invalid
mask.
<HR><H3>Function</H3>
<P><I>int</I>
<B>ipa_hash</B>
(<I>ip_addr</I> <B>x</B>) -- hash IP addresses
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>ip_addr</I> <B>x</B><DD><P>IP address
</DL>
<H3>Description</H3>
<P><B>ipa_hash()</B> returns a 16-bit hash value of the IP address <B>x</B>.
<HR><H3>Function</H3>
<P><I>void</I>
<B>ipa_hton</B>
(<I>ip_addr</I> <B>x</B>) -- convert IP address to network order
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>ip_addr</I> <B>x</B><DD><P>IP address
</DL>
<H3>Description</H3>
<P>Converts the IP address <B>x</B> to the network byte order.
<P>Beware, this is a macro and it alters the argument!
<HR><H3>Function</H3>
<P><I>void</I>
<B>ipa_ntoh</B>
(<I>ip_addr</I> <B>x</B>) -- convert IP address to host order
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>ip_addr</I> <B>x</B><DD><P>IP address
</DL>
<H3>Description</H3>
<P>Converts the IP address <B>x</B> from the network byte order.
<P>Beware, this is a macro and it alters the argument!
<HR><H3>Function</H3>
<P><I>int</I>
<B>ipa_classify</B>
(<I>ip_addr</I> <B>x</B>) -- classify an IP address
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>ip_addr</I> <B>x</B><DD><P>IP address
</DL>
<H3>Description</H3>
<P><B>ipa_classify()</B> returns an address class of <B>x</B>, that is a bitwise or
of address type (<I>IADDR_INVALID</I>, <I>IADDR_HOST</I>, <I>IADDR_BROADCAST</I>, <I>IADDR_MULTICAST</I>)
with address scope (<I>SCOPE_HOST</I> to <I>SCOPE_UNIVERSE</I>) or -1 (<I>IADDR_INVALID</I>)
for an invalid address.
<HR><H3>Function</H3>
<P><I>ip4_addr</I>
<B>ip4_class_mask</B>
(<I>ip4_addr</I> <B>x</B>) -- guess netmask according to address class
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>ip4_addr</I> <B>x</B><DD><P>IPv4 address
</DL>
<H3>Description</H3>
<P>This function (available in IPv4 version only) returns a
network mask according to the address class of <B>x</B>. Although
classful addressing is nowadays obsolete, there still live
routing protocols transferring no prefix lengths nor netmasks
and this function could be useful to them.
<HR><H3>Function</H3>
<P><I>u32</I>
<B>ipa_from_u32</B>
(<I>ip_addr</I> <B>x</B>) -- convert IPv4 address to an integer
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>ip_addr</I> <B>x</B><DD><P>IP address
</DL>
<H3>Description</H3>
<P>This function takes an IPv4 address and returns its numeric
representation.
<HR><H3>Function</H3>
<P><I>ip_addr</I>
<B>ipa_to_u32</B>
(<I>u32</I> <B>x</B>) -- convert integer to IPv4 address
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>u32</I> <B>x</B><DD><P>a 32-bit integer
</DL>
<H3>Description</H3>
<P><B>ipa_to_u32()</B> takes a numeric representation of an IPv4 address
and converts it to the corresponding <I>ip_addr</I>.
<HR><H3>Function</H3>
<P><I>int</I>
<B>ipa_compare</B>
(<I>ip_addr</I> <B>x</B>, <I>ip_addr</I> <B>y</B>) -- compare two IP addresses for order
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>ip_addr</I> <B>x</B><DD><P>IP address
<DT><I>ip_addr</I> <B>y</B><DD><P>IP address
</DL>
<H3>Description</H3>
<P>The <B>ipa_compare()</B> function takes two IP addresses and returns
-1 if <B>x</B> is less than <B>y</B> in canonical ordering (lexicographical
order of the bit strings), 1 if <B>x</B> is greater than <B>y</B> and 0
if they are the same.
<HR><H3>Function</H3>
<P><I>ip_addr</I>
<B>ipa_build6</B>
(<I>u32</I> <B>a1</B>, <I>u32</I> <B>a2</B>, <I>u32</I> <B>a3</B>, <I>u32</I> <B>a4</B>) -- build an IPv6 address from parts
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>u32</I> <B>a1</B><DD><P>part #1
<DT><I>u32</I> <B>a2</B><DD><P>part #2
<DT><I>u32</I> <B>a3</B><DD><P>part #3
<DT><I>u32</I> <B>a4</B><DD><P>part #4
</DL>
<H3>Description</H3>
<P><B>ipa_build()</B> takes <B>a1</B> to <B>a4</B> and assembles them to a single IPv6
address. It's used for example when a protocol wants to bind its
socket to a hard-wired multicast address.
<HR><H3>Function</H3>
<P><I>char *</I>
<B>ip_ntop</B>
(<I>ip_addr</I> <B>a</B>, <I>char *</I> <B>buf</B>) -- convert IP address to textual representation
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>ip_addr</I> <B>a</B><DD><P>IP address
<DT><I>char *</I> <B>buf</B><DD><P>buffer of size at least <I>STD_ADDRESS_P_LENGTH</I>
</DL>
<H3>Description</H3>
<P>This function takes an IP address and creates its textual
representation for presenting to the user.
<HR><H3>Function</H3>
<P><I>char *</I>
<B>ip_ntox</B>
(<I>ip_addr</I> <B>a</B>, <I>char *</I> <B>buf</B>) -- convert IP address to hexadecimal representation
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>ip_addr</I> <B>a</B><DD><P>IP address
<DT><I>char *</I> <B>buf</B><DD><P>buffer of size at least <I>STD_ADDRESS_P_LENGTH</I>
</DL>
<H3>Description</H3>
<P>This function takes an IP address and creates its hexadecimal
textual representation. Primary use: debugging dumps.
<HR><H3>Function</H3>
<P><I>int</I>
<B>ip_pton</B>
(<I>char *</I> <B>a</B>, <I>ip_addr *</I> <B>o</B>) -- parse textual representation of IP address
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>char *</I> <B>a</B><DD><P>textual representation
<DT><I>ip_addr *</I> <B>o</B><DD><P>where to put the resulting address
</DL>
<H3>Description</H3>
<P>This function parses a textual IP address representation and
stores the decoded address to a variable pointed to by <B>o</B>.
Returns 0 if a parse error has occurred, else 0.
<H2><A NAME="ss7.2">7.2</A> <A HREF="prog.html#toc7.2">Linked lists</A>
</H2>
<P>
<P>The BIRD library provides a set of functions for operating on linked
lists. The lists are internally represented as standard doubly linked
lists with synthetic head and tail which makes all the basic operations
run in constant time and contain no extra end-of-list checks. Each list
is described by a <I>list</I> structure, nodes can have any format as long
as they start with a <I>node</I> structure. If you want your nodes to belong
to multiple lists at once, you can embed multiple <I>node</I> structures in them
and use the <B>SKIP_BACK()</B> macro to calculate a pointer to the start of the
structure from a <I>node</I> pointer, but beware of obscurity.
<P>There also exist safe linked lists (<I>slist</I>, <I>snode</I> and all functions
being prefixed with <CODE>s_</CODE>) which support asynchronous walking very
similar to that used in the <I>fib</I> structure.
<P>
<P><HR><H3>Function</H3>
<P><I>LIST_INLINE void</I>
<B>add_tail</B>
(<I>list *</I> <B>l</B>, <I>node *</I> <B>n</B>) -- append a node to a list
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>list *</I> <B>l</B><DD><P>linked list
<DT><I>node *</I> <B>n</B><DD><P>list node
</DL>
<H3>Description</H3>
<P><B>add_tail()</B> takes a node <B>n</B> and appends it at the end of the list <B>l</B>.
<HR><H3>Function</H3>
<P><I>LIST_INLINE void</I>
<B>add_head</B>
(<I>list *</I> <B>l</B>, <I>node *</I> <B>n</B>) -- prepend a node to a list
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>list *</I> <B>l</B><DD><P>linked list
<DT><I>node *</I> <B>n</B><DD><P>list node
</DL>
<H3>Description</H3>
<P><B>add_head()</B> takes a node <B>n</B> and prepends it at the start of the list <B>l</B>.
<HR><H3>Function</H3>
<P><I>LIST_INLINE void</I>
<B>insert_node</B>
(<I>node *</I> <B>n</B>, <I>node *</I> <B>after</B>) -- insert a node to a list
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>node *</I> <B>n</B><DD><P>a new list node
<DT><I>node *</I> <B>after</B><DD><P>a node of a list
</DL>
<H3>Description</H3>
<P>Inserts a node <B>n</B> to a linked list after an already inserted
node <B>after</B>.
<HR><H3>Function</H3>
<P><I>LIST_INLINE void</I>
<B>rem_node</B>
(<I>node *</I> <B>n</B>) -- remove a node from a list
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>node *</I> <B>n</B><DD><P>node to be removed
</DL>
<H3>Description</H3>
<P>Removes a node <B>n</B> from the list it's linked in. Afterwards, node <B>n</B> is cleared.
<HR><H3>Function</H3>
<P><I>LIST_INLINE void</I>
<B>replace_node</B>
(<I>node *</I> <B>old</B>, <I>node *</I> <B>new</B>) -- replace a node in a list with another one
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>node *</I> <B>old</B><DD><P>node to be removed
<DT><I>node *</I> <B>new</B><DD><P>node to be inserted
</DL>
<H3>Description</H3>
<P>Replaces node <B>old</B> in the list it's linked in with node <B>new</B>. Node
<B>old</B> may be a copy of the original node, which is not accessed
through the list. The function could be called with <B>old</B> == <B>new</B>,
which just fixes neighbors' pointers in the case that the node
was reallocated.
<HR><H3>Function</H3>
<P><I>LIST_INLINE void</I>
<B>init_list</B>
(<I>list *</I> <B>l</B>) -- create an empty list
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>list *</I> <B>l</B><DD><P>list
</DL>
<H3>Description</H3>
<P><B>init_list()</B> takes a <I>list</I> structure and initializes its
fields, so that it represents an empty list.
<HR><H3>Function</H3>
<P><I>LIST_INLINE void</I>
<B>add_tail_list</B>
(<I>list *</I> <B>to</B>, <I>list *</I> <B>l</B>) -- concatenate two lists
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>list *</I> <B>to</B><DD><P>destination list
<DT><I>list *</I> <B>l</B><DD><P>source list
</DL>
<H3>Description</H3>
<P>This function appends all elements of the list <B>l</B> to
the list <B>to</B> in constant time.
<H2><A NAME="ss7.3">7.3</A> <A HREF="prog.html#toc7.3">Miscellaneous functions.</A>
</H2>
<P>
<P>
<P><HR><H3>Function</H3>
<P><I>int</I>
<B>ipsum_verify</B>
(<I>void *</I> <B>frag</B>, <I>uint</I> <B>len</B>, <I>...</I> <B>...</B>) -- verify an IP checksum
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>void *</I> <B>frag</B><DD><P>first packet fragment
<DT><I>uint</I> <B>len</B><DD><P>length in bytes
<DT><I>...</I> <B>...</B><DD><P>variable arguments
</DL>
<H3>Description</H3>
<P>This function verifies whether a given fragmented packet
has correct one's complement checksum as used by the IP
protocol.
<P>It uses all the clever tricks described in RFC 1071 to speed
up checksum calculation as much as possible.
<H3>Result</H3>
<P>1 if the checksum is correct, 0 else.
<HR><H3>Function</H3>
<P><I>u16</I>
<B>ipsum_calculate</B>
(<I>void *</I> <B>frag</B>, <I>uint</I> <B>len</B>, <I>...</I> <B>...</B>) -- compute an IP checksum
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>void *</I> <B>frag</B><DD><P>first packet fragment
<DT><I>uint</I> <B>len</B><DD><P>length in bytes
<DT><I>...</I> <B>...</B><DD><P>variable arguments
</DL>
<H3>Description</H3>
<P>This function calculates a one's complement checksum of a given fragmented
packet.
<P>It uses all the clever tricks described in RFC 1071 to speed
up checksum calculation as much as possible.
<HR><H3>Function</H3>
<P><I>u32</I>
<B>u32_mkmask</B>
(<I>uint</I> <B>n</B>) -- create a bit mask
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>uint</I> <B>n</B><DD><P>number of bits
</DL>
<H3>Description</H3>
<P><B>u32_mkmask()</B> returns an unsigned 32-bit integer which binary
representation consists of <B>n</B> ones followed by zeroes.
<HR><H3>Function</H3>
<P><I>int</I>
<B>u32_masklen</B>
(<I>u32</I> <B>x</B>) -- calculate length of a bit mask
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>u32</I> <B>x</B><DD><P>bit mask
</DL>
<H3>Description</H3>
<P>This function checks whether the given integer <B>x</B> represents
a valid bit mask (binary representation contains first ones, then
zeroes) and returns the number of ones or -1 if the mask is invalid.
<HR><H3>Function</H3>
<P><I>u32</I>
<B>u32_log2</B>
(<I>u32</I> <B>v</B>) -- compute a binary logarithm.
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>u32</I> <B>v</B><DD><P>number
</DL>
<H3>Description</H3>
<P>This function computes a integral part of binary logarithm of given
integer <B>v</B> and returns it. The computed value is also an index of the
most significant non-zero bit position.
<HR><H3>Function</H3>
<P><I>int</I>
<B>patmatch</B>
(<I>byte *</I> <B>p</B>, <I>byte *</I> <B>s</B>) -- match shell-like patterns
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>byte *</I> <B>p</B><DD><P>pattern
<DT><I>byte *</I> <B>s</B><DD><P>string
</DL>
<H3>Description</H3>
<P><B>patmatch()</B> returns whether given string <B>s</B> matches the given shell-like
pattern <B>p</B>. The patterns consist of characters (which are matched literally),
question marks which match any single character, asterisks which match any
(possibly empty) string of characters and backslashes which are used to
escape any special characters and force them to be treated literally.
<P>The matching process is not optimized with respect to time, so please
avoid using this function for complex patterns.
<HR><H3>Function</H3>
<P><I>int</I>
<B>bvsnprintf</B>
(<I>char *</I> <B>buf</B>, <I>int</I> <B>size</B>, <I>const char *</I> <B>fmt</B>, <I>va_list</I> <B>args</B>) -- BIRD's <B>vsnprintf()</B>
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>char *</I> <B>buf</B><DD><P>destination buffer
<DT><I>int</I> <B>size</B><DD><P>size of the buffer
<DT><I>const char *</I> <B>fmt</B><DD><P>format string
<DT><I>va_list</I> <B>args</B><DD><P>a list of arguments to be formatted
</DL>
<H3>Description</H3>
<P>This functions acts like ordinary <B>sprintf()</B> except that it checks
available space to avoid buffer overflows and it allows some more
<H3>format specifiers</H3>
<P><CODE><I>I</I></CODE> for formatting of IP addresses (any non-zero
width is automatically replaced by standard IP address width which
depends on whether we use IPv4 or IPv6; <CODE>%#I</CODE> gives hexadecimal format),
<CODE><I>R</I></CODE> for Router / Network ID (u32 value printed as IPv4 address)
<CODE><I>lR</I></CODE> for 64bit Router / Network ID (u64 value printed as eight :-separated octets)
and <CODE><I>m</I></CODE> resp. <CODE><I>M</I></CODE> for error messages (uses <B>strerror()</B> to translate <B>errno</B> code to
message text). On the other hand, it doesn't support floating
point numbers.
<H3>Result</H3>
<P>number of characters of the output string or -1 if
the buffer space was insufficient.
<HR><H3>Function</H3>
<P><I>int</I>
<B>bvsprintf</B>
(<I>char *</I> <B>buf</B>, <I>const char *</I> <B>fmt</B>, <I>va_list</I> <B>args</B>) -- BIRD's <B>vsprintf()</B>
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>char *</I> <B>buf</B><DD><P>buffer
<DT><I>const char *</I> <B>fmt</B><DD><P>format string
<DT><I>va_list</I> <B>args</B><DD><P>a list of arguments to be formatted
</DL>
<H3>Description</H3>
<P>This function is equivalent to <B>bvsnprintf()</B> with an infinite
buffer size. Please use carefully only when you are absolutely
sure the buffer won't overflow.
<HR><H3>Function</H3>
<P><I>int</I>
<B>bsprintf</B>
(<I>char *</I> <B>buf</B>, <I>const char *</I> <B>fmt</B>, <I>...</I> <B>...</B>) -- BIRD's <B>sprintf()</B>
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>char *</I> <B>buf</B><DD><P>buffer
<DT><I>const char *</I> <B>fmt</B><DD><P>format string
<DT><I>...</I> <B>...</B><DD><P>variable arguments
</DL>
<H3>Description</H3>
<P>This function is equivalent to <B>bvsnprintf()</B> with an infinite
buffer size and variable arguments instead of a <I>va_list</I>.
Please use carefully only when you are absolutely
sure the buffer won't overflow.
<HR><H3>Function</H3>
<P><I>int</I>
<B>bsnprintf</B>
(<I>char *</I> <B>buf</B>, <I>int</I> <B>size</B>, <I>const char *</I> <B>fmt</B>, <I>...</I> <B>...</B>) -- BIRD's <B>snprintf()</B>
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>char *</I> <B>buf</B><DD><P>buffer
<DT><I>int</I> <B>size</B><DD><P>buffer size
<DT><I>const char *</I> <B>fmt</B><DD><P>format string
<DT><I>...</I> <B>...</B><DD><P>variable arguments
</DL>
<H3>Description</H3>
<P>This function is equivalent to <B>bsnprintf()</B> with variable arguments instead of a <I>va_list</I>.
<HR><H3>Function</H3>
<P><I>void *</I>
<B>xmalloc</B>
(<I>uint</I> <B>size</B>) -- malloc with checking
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>uint</I> <B>size</B><DD><P>block size
</DL>
<H3>Description</H3>
<P>This function is equivalent to <B>malloc()</B> except that in case of
failure it calls <B>die()</B> to quit the program instead of returning
a <I>NULL</I> pointer.
<P>Wherever possible, please use the memory resources instead.
<HR><H3>Function</H3>
<P><I>void *</I>
<B>xrealloc</B>
(<I>void *</I> <B>ptr</B>, <I>uint</I> <B>size</B>) -- realloc with checking
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>void *</I> <B>ptr</B><DD><P>original memory block
<DT><I>uint</I> <B>size</B><DD><P>block size
</DL>
<H3>Description</H3>
<P>This function is equivalent to <B>realloc()</B> except that in case of
failure it calls <B>die()</B> to quit the program instead of returning
a <I>NULL</I> pointer.
<P>Wherever possible, please use the memory resources instead.
<H2><A NAME="ss7.4">7.4</A> <A HREF="prog.html#toc7.4">Message authentication codes</A>
</H2>
<P>
<P>MAC algorithms are simple cryptographic tools for message authentication.
They use shared a secret key a and message text to generate authentication
code, which is then passed with the message to the other side, where the code
is verified. There are multiple families of MAC algorithms based on different
cryptographic primitives, BIRD implements two MAC families which use hash
functions.
<P>The first family is simply a cryptographic hash camouflaged as MAC algorithm.
Originally supposed to be (m|k)-hash (message is concatenated with key, and
that is hashed), but later it turned out that a raw hash is more practical.
This is used for cryptographic authentication in OSPFv2, RIP and BFD.
<P>The second family is the standard HMAC (RFC 2104), using inner and outer hash
to process key and message. HMAC (with SHA) is used in advanced OSPF and RIP
authentication (RFC 5709, RFC 4822).
<P>
<P><HR><H3>Function</H3>
<P><I>void</I>
<B>mac_init</B>
(<I>struct mac_context *</I> <B>ctx</B>, <I>uint</I> <B>id</B>, <I>const byte *</I> <B>key</B>, <I>uint</I> <B>keylen</B>) -- initialize MAC algorithm
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>struct mac_context *</I> <B>ctx</B><DD><P>context to initialize
<DT><I>uint</I> <B>id</B><DD><P>MAC algorithm ID
<DT><I>const byte *</I> <B>key</B><DD><P>MAC key
<DT><I>uint</I> <B>keylen</B><DD><P>MAC key length
</DL>
<H3>Description</H3>
<P>Initialize MAC context <B>ctx</B> for algorithm <B>id</B> (e.g., <I>ALG_HMAC_SHA1</I>), with
key <B>key</B> of length <B>keylen</B>. After that, message data could be added using
<B>mac_update()</B> function.
<HR><H3>Function</H3>
<P><I>void</I>
<B>mac_update</B>
(<I>struct mac_context *</I> <B>ctx</B>, <I>const byte *</I> <B>data</B>, <I>uint</I> <B>datalen</B>) -- add more data to MAC algorithm
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>struct mac_context *</I> <B>ctx</B><DD><P>MAC context
<DT><I>const byte *</I> <B>data</B><DD><P>data to add
<DT><I>uint</I> <B>datalen</B><DD><P>length of data
</DL>
<H3>Description</H3>
<P>Push another <B>datalen</B> bytes of data pointed to by <B>data</B> into the MAC
algorithm currently in <B>ctx</B>. Can be called multiple times for the same MAC
context. It has the same effect as concatenating all the data together and
passing them at once.
<HR><H3>Function</H3>
<P><I>byte *</I>
<B>mac_final</B>
(<I>struct mac_context *</I> <B>ctx</B>) -- finalize MAC algorithm
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>struct mac_context *</I> <B>ctx</B><DD><P>MAC context
</DL>
<H3>Description</H3>
<P>Finish MAC computation and return a pointer to the result. No more
<B>mac_update</B>() calls could be done, but the context may be reinitialized
later.
<P>Note that the returned pointer points into data in the <B>ctx</B> context. If it
ceases to exist, the pointer becomes invalid.
<HR><H3>Function</H3>
<P><I>void</I>
<B>mac_cleanup</B>
(<I>struct mac_context *</I> <B>ctx</B>) -- cleanup MAC context
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>struct mac_context *</I> <B>ctx</B><DD><P>MAC context
</DL>
<H3>Description</H3>
<P>Cleanup MAC context after computation (by filling with zeros). Not strictly
necessary, just to erase sensitive data from stack. This also invalidates the
pointer returned by <B>mac_final</B>().
<HR><H3>Function</H3>
<P><I>void</I>
<B>mac_fill</B>
(<I>uint</I> <B>id</B>, <I>const byte *</I> <B>key</B>, <I>uint</I> <B>keylen</B>, <I>const byte *</I> <B>data</B>, <I>uint</I> <B>datalen</B>, <I>byte *</I> <B>mac</B>) -- compute and fill MAC
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>uint</I> <B>id</B><DD><P>MAC algorithm ID
<DT><I>const byte *</I> <B>key</B><DD><P>secret key
<DT><I>uint</I> <B>keylen</B><DD><P>key length
<DT><I>const byte *</I> <B>data</B><DD><P>message data
<DT><I>uint</I> <B>datalen</B><DD><P>message length
<DT><I>byte *</I> <B>mac</B><DD><P>place to fill MAC
</DL>
<H3>Description</H3>
<P>Compute MAC for specified key <B>key</B> and message <B>data</B> using algorithm <B>id</B> and
copy it to buffer <B>mac</B>. <B>mac_fill()</B> is a shortcut function doing all usual
steps for transmitted messages.
<HR><H3>Function</H3>
<P><I>int</I>
<B>mac_verify</B>
(<I>uint</I> <B>id</B>, <I>const byte *</I> <B>key</B>, <I>uint</I> <B>keylen</B>, <I>const byte *</I> <B>data</B>, <I>uint</I> <B>datalen</B>, <I>const byte *</I> <B>mac</B>) -- compute and verify MAC
<P>
<H3>Arguments</H3>
<P>
<DL>
<DT><I>uint</I> <B>id</B><DD><P>MAC algorithm ID
<DT><I>const byte *</I> <B>key</B><DD><P>secret key
<DT><I>uint</I> <B>keylen</B><DD><P>key length
<DT><I>const byte *</I> <B>data</B><DD><P>message data
<DT><I>uint</I> <B>datalen</B><DD><P>message length
<DT><I>const byte *</I> <B>mac</B><DD><P>received MAC
</DL>
<H3>Description</H3>
<P>Compute MAC for specified key <B>key</B> and message <B>data</B> using algorithm <B>id</B> and
compare it with received <B>mac</B>, return whether they are the same. <B>mac_verify()</B>
is a shortcut function doing all usual steps for received messages.
<P>
<HR>
<A HREF="prog-8.html">Next</A>
<A HREF="prog-6.html">Previous</A>
<A HREF="prog.html#toc7">Contents</A>
</BODY>
</HTML>
|