File: res_query_dump.c

package info (click to toggle)
libstrophe 0.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 908 kB
  • ctags: 1,015
  • sloc: ansic: 9,849; cpp: 205; makefile: 102; sh: 30
file content (90 lines) | stat: -rw-r--r-- 2,327 bytes parent folder | download | duplicates (2)
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
/* res_query_dump.c
 * Simple program to dump res_query(3) response
 *
 * Copyright (C) 2014 Dmitry Podgorny <pasis.ua@gmail.com>
 *
 *  This software is provided AS-IS with no warranty, either express
 *  or implied.
 *
 *  This program is dual licensed under the MIT and GPLv3 licenses.
 */

/* Linux and OSX:
 *   gcc -o res_query_dump tests/res_query_dump.c -lresolv
 * *BSD:
 *   gcc -o res_query_dump tests/res_query_dump.c
 * QNX:
 *   gcc -o res_query_dump tests/res_query_dump.c -lsocket
 * Solaris:
 *   gcc -o res_query_dump tests/res_query_dump.c -lresolv -lsocket -lnsl
 */

#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>

#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

#ifndef T_SRV
#define T_SRV 33
#endif /* T_SRV */
#ifndef C_IN
#define C_IN 1
#endif /* C_IN */

#define STEP 10

int main(int argc, char **argv)
{
    unsigned char buf[65536];
    char fulldomain[2048];
    char *service = "xmpp-client";
    char *proto = "tcp";
    char *domain = NULL;
    int len;
    int i;
    int j;

    if (argc < 2) {
        fprintf(stderr, "%s: argument missed\n", argc > 0 ? argv[0] : "$0");
        fprintf(stderr, "Usage: %s <domain>\n", argc > 0 ? argv[0] : "$0");
        return 1;
    }

    domain = argv[1];
    snprintf(fulldomain, sizeof(fulldomain), "_%s._%s.%s",
             service, proto, domain);
    errno = 0;
    len = res_query(fulldomain, C_IN, T_SRV, buf, sizeof(buf));

    if (len < 0) {
        fprintf(stderr, "res_query(): Error occurred (errno=%d)\n", errno);
    }
    if (len == 0) {
        fprintf(stderr, "res_query(): Empty result\n");
    }
    if (len > 0) {
        printf("/* res_query(\"%s\", C_IN, T_SRV, ...) */\n", fulldomain);
        printf("static const unsigned char data[] = {\n");
        for (i = 0; i < len; i += STEP) {
            printf("   ");
            for (j = i; j < len && j < i + STEP; ++j) {
                printf(" 0x%02x,", buf[j]);
            }
            for (j = len; j < i + STEP; ++j) {
                printf("      ");
            }
            printf("    // ");
            for (j = i; j < len && j < i + STEP; ++j) {
                printf("%c", isprint(buf[j]) ? buf[j] : '.');
            }
            printf("\n");
        }
        printf("};\n");
    }

    return len <= 0;
}