Home | History | Annotate | Download | only in avahi-compat-libdns_sd
      1 /***
      2   This file is part of avahi.
      3 
      4   avahi is free software; you can redistribute it and/or modify it
      5   under the terms of the GNU Lesser General Public License as
      6   published by the Free Software Foundation; either version 2.1 of the
      7   License, or (at your option) any later version.
      8 
      9   avahi is distributed in the hope that it will be useful, but WITHOUT
     10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
     12   Public License for more details.
     13 
     14   You should have received a copy of the GNU Lesser General Public
     15   License along with avahi; if not, write to the Free Software
     16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
     17   USA.
     18 ***/
     19 
     20 #ifdef HAVE_CONFIG_H
     21 #include <config.h>
     22 #endif
     23 
     24 #include <sys/types.h>
     25 #include <assert.h>
     26 #include <stdio.h>
     27 
     28 #include <avahi-common/gccmacro.h>
     29 #include <dns_sd.h>
     30 
     31 static void hexdump(const void* p, size_t size) {
     32     const uint8_t *c = p;
     33     assert(p);
     34 
     35     printf("Dumping %zu bytes from %p:\n", size, p);
     36 
     37     while (size > 0) {
     38         unsigned i;
     39 
     40         for (i = 0; i < 16; i++) {
     41             if (i < size)
     42                 printf("%02x ", c[i]);
     43             else
     44                 printf("   ");
     45         }
     46 
     47         for (i = 0; i < 16; i++) {
     48             if (i < size)
     49                 printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
     50             else
     51                 printf(" ");
     52         }
     53 
     54         printf("\n");
     55 
     56         c += 16;
     57 
     58         if (size <= 16)
     59             break;
     60 
     61         size -= 16;
     62     }
     63 }
     64 
     65 
     66 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
     67     const char *r;
     68     TXTRecordRef ref;
     69     uint8_t l;
     70     const void *p;
     71     char k[256];
     72 
     73     TXTRecordCreate(&ref, 0, NULL);
     74 
     75     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
     76 
     77     TXTRecordSetValue(&ref, "foo", 7, "lennart");
     78     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
     79 
     80     TXTRecordSetValue(&ref, "waldo", 5, "rocks");
     81     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
     82 
     83     TXTRecordSetValue(&ref, "quux", 9, "the_house");
     84     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
     85 
     86     TXTRecordSetValue(&ref, "yeah", 0, NULL);
     87     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
     88 
     89     TXTRecordSetValue(&ref, "waldo", 6, "rocked");
     90     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
     91 
     92     TXTRecordRemoveValue(&ref, "foo");
     93     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
     94 
     95     TXTRecordRemoveValue(&ref, "waldo");
     96     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
     97 
     98     TXTRecordSetValue(&ref, "kawumm", 6, "bloerb");
     99     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
    100 
    101     TXTRecordSetValue(&ref, "one", 1, "1");
    102     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
    103 
    104     TXTRecordSetValue(&ref, "two", 1, "2");
    105     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
    106 
    107     TXTRecordSetValue(&ref, "three", 1, "3");
    108     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
    109 
    110     assert(TXTRecordContainsKey(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref), "two"));
    111     assert(!TXTRecordContainsKey(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref), "four"));
    112 
    113     r = TXTRecordGetValuePtr(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref), "kawumm", &l);
    114 
    115     hexdump(r, l);
    116 
    117     assert(TXTRecordGetCount(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref)) == 6);
    118 
    119     TXTRecordGetItemAtIndex(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref), 2, sizeof(k), k, &l, &p);
    120 
    121     fprintf(stderr, "key=<%s>\n", k);
    122 
    123     hexdump(p, l);
    124 
    125     assert(TXTRecordGetItemAtIndex(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref), 20, sizeof(k), k, &l, &p) == kDNSServiceErr_Invalid);
    126 
    127     TXTRecordDeallocate(&ref);
    128 }
    129