create a set_rand_bitmask function and use instead of open-coding.
[trinity.git] / net / protocols.c
blob63d03dd7e2a5ebe1f0dd21b957ab23e46a4eca61
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
6 #include "utils.h"
7 #include "constants.h"
8 #include "protocols.h"
9 #include "params.h"
10 #include "net.h"
11 #include "log.h"
13 struct protocol {
14 const char *name;
15 const unsigned int proto;
18 static const struct protocol protocols[] = {
19 { "PF_UNSPEC", 0 },
20 { "PF_LOCAL", 1 },
21 { "PF_UNIX", PF_LOCAL },
22 { "PF_FILE", PF_LOCAL },
23 { "PF_INET", 2 },
24 { "PF_AX25", 3 },
25 { "PF_IPX", 4 },
26 { "PF_APPLETALK", 5 },
27 { "PF_NETROM", 6 },
28 { "PF_BRIDGE", 7 },
29 { "PF_ATMPVC", 8 },
30 { "PF_X25", 9 },
31 { "PF_INET6", 10 },
32 { "PF_ROSE", 11 },
33 { "PF_DECnet", 12 },
34 { "PF_NETBEUI", 13 },
35 { "PF_SECURITY", 14 },
36 { "PF_KEY", 15 },
37 { "PF_NETLINK", 16 },
38 { "PF_ROUTE", PF_NETLINK },
39 { "PF_PACKET", 17 },
40 { "PF_ASH", 18 },
41 { "PF_ECONET", 19 },
42 { "PF_ATMSVC", 20 },
43 { "PF_RDS", 21 },
44 { "PF_SNA", 22 },
45 { "PF_IRDA", 23 },
46 { "PF_PPPOX", 24 },
47 { "PF_WANPIPE", 25 },
48 { "PF_LLC", 26 },
49 { "PF_CAN", 29 },
50 { "PF_TIPC", 30 },
51 { "PF_BLUETOOTH", 31 },
52 { "PF_IUCV", 32 },
53 { "PF_RXRPC", 33 },
54 { "PF_ISDN", 34 },
55 { "PF_PHONET", 35 },
56 { "PF_IEEE802154", 36 },
57 { "PF_CAIF", 37 },
58 { "PF_ALG", 38 },
59 { "PF_NFC", 39 },
60 { "PF_VSOCK", 40 },
63 static const struct protocol *lookup_proto(const char *name, unsigned int proto)
65 unsigned int i;
67 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
68 if ((name && strcmp(name, protocols[i].name) == 0) ||
69 (proto != -1u && protocols[i].proto == proto))
70 return &protocols[i];
73 return NULL;
76 const char * get_proto_name(unsigned int proto)
78 unsigned int i;
80 for (i = 0; i < ARRAY_SIZE(protocols); i++)
81 if (protocols[i].proto == proto)
82 return protocols[i].name;
83 return NULL;
86 void find_specific_proto(const char *protoarg)
88 const struct protocol *p;
89 unsigned int i;
91 p = lookup_proto(protoarg, specific_proto ? : -1u);
92 if (p) {
93 specific_proto = p->proto;
94 output(2, "Using protocol %s (%u) for all sockets\n", p->name, p->proto);
95 return;
98 outputerr("Protocol unknown. Pass a numeric value [0-%d] or one of ", TRINITY_PF_MAX);
99 for (i = 0; i < ARRAY_SIZE(protocols); i++)
100 outputerr("%s ", protocols[i].name);
101 outputerr("\n");
103 exit(EXIT_FAILURE);
106 unsigned int find_next_enabled_proto(unsigned int from)
108 unsigned int i;
110 from %= ARRAY_SIZE(no_protos);
112 for (i = from; i < ARRAY_SIZE(no_protos); i++) {
113 if (no_protos[i] == FALSE)
114 return no_protos[i];
117 for (i = 0; i < from; i++) {
118 if (no_protos[i] == FALSE)
119 return no_protos[i];
122 return -1u;
125 void parse_exclude_protos(const char *arg)
127 char *_arg = strdup(arg);
128 const struct protocol *p;
129 char *tok;
131 if (!_arg) {
132 outputerr("No free memory\n");
133 exit(EXIT_FAILURE);
136 for (tok = strtok(_arg, ","); tok; tok = strtok(NULL, ",")) {
137 p = lookup_proto(tok, (unsigned int)atoi(tok));
138 if (p) {
139 BUG_ON(p->proto >= ARRAY_SIZE(no_protos));
140 no_protos[p->proto] = TRUE;
141 } else
142 goto err;
145 free(_arg);
146 return;
148 err:
149 free(_arg);
150 outputerr("Protocol unknown in argument %s\n", arg);
151 exit(EXIT_FAILURE);