make protocol table zero byte separated and add ipv6 protocols
[musl] / src / network / proto.c
1 #include <netdb.h>
2 #include <string.h>
3
4 /* do we really need all these?? */
5
6 static int idx;
7 static const unsigned char protos[] = {
8         "\000ip\0"
9         "\001icmp\0"
10         "\002igmp\0"
11         "\003ggp\0"
12         "\006tcp\0"
13         "\014pup\0"
14         "\021udp\0"
15         "\026idp\0"
16         "\051ipv6\0"
17         "\053ipv6-route\0"
18         "\054ipv6-frag\0"
19         "\057gre\0"
20         "\062esp\0"
21         "\063ah\0"
22         "\072ipv6-icmp\0"
23         "\073ipv6-nonxt\0"
24         "\074ipv6-opts\0"
25         "\131ospf\0"
26         "\136ipip\0"
27         "\147pim\0"
28         "\377raw"
29 };
30
31 void endprotoent(void)
32 {
33         idx = 0;
34 }
35
36 void setprotoent(int stayopen)
37 {
38         idx = 0;
39 }
40
41 struct protoent *getprotoent(void)
42 {
43         static struct protoent p;
44         static const char *aliases;
45         if (idx >= sizeof protos) return NULL;
46         p.p_proto = protos[idx];
47         p.p_name = (char *)&protos[idx+1];
48         p.p_aliases = (char **)&aliases;
49         idx += strlen(p.p_name) + 2;
50         return &p;
51 }
52
53 struct protoent *getprotobyname(const char *name)
54 {
55         struct protoent *p;
56         endprotoent();
57         do p = getprotoent();
58         while (p && strcmp(name, p->p_name));
59         return p;
60 }
61
62 struct protoent *getprotobynumber(int num)
63 {
64         struct protoent *p;
65         endprotoent();
66         do p = getprotoent();
67         while (p && p->p_proto != num);
68         return p;
69 }