summaryrefslogtreecommitdiffstats
path: root/epan/services.c
blob: 0c00817d0da374e2b20e29e1d8aaa893a6396a6f (plain)
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
/* services.c
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "services.h"
#include <stdlib.h>

#include "services-data.c"


static int
compare_entry(const void *a, const void *b)
{
    return *(const uint16_t *)a - ((const ws_services_entry_t *)b)->port;
}

static inline
size_t global_tcp_udp_services_table_count(void)
{
    return G_N_ELEMENTS(global_tcp_udp_services_table);
}

static inline
size_t global_tcp_services_table_count(void)
{
    return G_N_ELEMENTS(global_tcp_services_table);
}

static inline
size_t global_udp_services_table_count(void)
{
    return G_N_ELEMENTS(global_udp_services_table);
}

static inline
size_t global_sctp_services_table_count(void)
{
    return G_N_ELEMENTS(global_sctp_services_table);
}

static inline
size_t global_dccp_services_table_count(void)
{
    return G_N_ELEMENTS(global_dccp_services_table);
}

ws_services_entry_t *
global_services_lookup(uint16_t value, ws_services_proto_t proto)
{
    ws_services_entry_t *list1 = NULL, *list2 = NULL;
    size_t list1_size, list2_size;
    ws_services_entry_t *found;

    switch (proto) {
        case ws_tcp:
            list1 = global_tcp_udp_services_table;
            list1_size = global_tcp_udp_services_table_count();
            list2 = global_tcp_services_table;
            list2_size = global_tcp_services_table_count();
            break;
        case ws_udp:
            list1 = global_tcp_udp_services_table;
            list1_size = global_tcp_udp_services_table_count();
            list2 = global_udp_services_table;
            list2_size = global_udp_services_table_count();
            break;
        case ws_sctp:
            list1 = global_sctp_services_table;
            list1_size = global_sctp_services_table_count();
            break;
        case ws_dccp:
            list1 = global_dccp_services_table;
            list1_size = global_dccp_services_table_count();
            break;
        default:
            ws_assert_not_reached();
    }

    if (list1) {
        found = bsearch(&value, list1, list1_size, sizeof(ws_services_entry_t), compare_entry);
        if (found) {
            return found;
        }
    }

    if (list2) {
        found = bsearch(&value, list2, list2_size, sizeof(ws_services_entry_t), compare_entry);
        if (found) {
            return found;
        }
    }

    return NULL;
}

void
global_services_dump(FILE *fp)
{
    ws_services_entry_t *ptr;

    /* Brute-force approach... */
    for (uint16_t num = 0; num <= _services_max_port && num < UINT16_MAX; num++) {
        /* TCP */
        ptr = global_services_lookup(num, ws_tcp);
        if (ptr != NULL) {
            fprintf(fp, "%s\t%"PRIu16"\ttcp\t%s\n", ptr->name, num, ptr->description);
        }
        /* UDP */
        ptr = global_services_lookup(num, ws_udp);
        if (ptr != NULL) {
            fprintf(fp, "%s\t%"PRIu16"\tudp\t%s\n", ptr->name, num, ptr->description);
        }
        /* SCTP */
        ptr = global_services_lookup(num, ws_sctp);
        if (ptr != NULL) {
            fprintf(fp, "%s\t%"PRIu16"\tsctp\t%s\n", ptr->name, num, ptr->description);
        }
        /* DCCP */
        ptr = global_services_lookup(num, ws_dccp);
        if (ptr != NULL) {
            fprintf(fp, "%s\t%"PRIu16"\tdccp\t%s\n", ptr->name, num, ptr->description);
        }
    }
}