getopt: fix null pointer arithmetic ub
[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         "\004ipencap\0"
13         "\005st\0"
14         "\006tcp\0"
15         "\010egp\0"
16         "\014pup\0"
17         "\021udp\0"
18         "\024hmp\0"
19         "\026xns-idp\0"
20         "\033rdp\0"
21         "\035iso-tp4\0"
22         "\044xtp\0"
23         "\045ddp\0"
24         "\046idpr-cmtp\0"
25         "\051ipv6\0"
26         "\053ipv6-route\0"
27         "\054ipv6-frag\0"
28         "\055idrp\0"
29         "\056rsvp\0"
30         "\057gre\0"
31         "\062esp\0"
32         "\063ah\0"
33         "\071skip\0"
34         "\072ipv6-icmp\0"
35         "\073ipv6-nonxt\0"
36         "\074ipv6-opts\0"
37         "\111rspf\0"
38         "\121vmtp\0"
39         "\131ospf\0"
40         "\136ipip\0"
41         "\142encap\0"
42         "\147pim\0"
43         "\377raw"
44 };
45
46 void endprotoent(void)
47 {
48         idx = 0;
49 }
50
51 void setprotoent(int stayopen)
52 {
53         idx = 0;
54 }
55
56 struct protoent *getprotoent(void)
57 {
58         static struct protoent p;
59         static const char *aliases;
60         if (idx >= sizeof protos) return NULL;
61         p.p_proto = protos[idx];
62         p.p_name = (char *)&protos[idx+1];
63         p.p_aliases = (char **)&aliases;
64         idx += strlen(p.p_name) + 2;
65         return &p;
66 }
67
68 struct protoent *getprotobyname(const char *name)
69 {
70         struct protoent *p;
71         endprotoent();
72         do p = getprotoent();
73         while (p && strcmp(name, p->p_name));
74         return p;
75 }
76
77 struct protoent *getprotobynumber(int num)
78 {
79         struct protoent *p;
80         endprotoent();
81         do p = getprotoent();
82         while (p && p->p_proto != num);
83         return p;
84 }