2 * Copyright (C) 2010 Bernhard Reutner-Fischer <uclibc@uclibc.org>
4 * Licensed under LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
8 # protocol-name number [aliases ...]
9 ip 0 IP # internet protocol, pseudo protocol number
11 protocol-name: case sensitive friendly name of the IP protocol
12 number: decimal protocol number
13 aliases: case sensitive optional space or tab separated list of other names
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
24 #include "internal/parse_config.h"
26 #include <bits/uClibc_mutex.h>
27 __UCLIBC_MUTEX_STATIC(mylock
, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
);
30 #define MAXALIASES 8 /* will probably never be more than one */
31 #define MAXTOKENS (MINTOKENS + MAXALIASES + 1)
32 #define BUFSZ (255) /* one line */
33 #define SBUFSIZE (BUFSZ + 1 + (sizeof(char *) * MAXTOKENS))
35 static parser_t
*protop
= NULL
;
36 static struct protoent protoe
;
37 static char *protobuf
= NULL
;
38 static smallint proto_stayopen
;
40 void setprotoent(int stayopen
)
42 __UCLIBC_MUTEX_LOCK(mylock
);
45 protop
= config_open(_PATH_PROTOCOLS
);
48 __UCLIBC_MUTEX_UNLOCK(mylock
);
50 libc_hidden_def(setprotoent
)
52 void endprotoent(void)
54 __UCLIBC_MUTEX_LOCK(mylock
);
60 __UCLIBC_MUTEX_UNLOCK(mylock
);
62 libc_hidden_def(endprotoent
)
64 int getprotoent_r(struct protoent
*result_buf
,
65 char *buf
, size_t buflen
, struct protoent
**result
)
68 const size_t aliaslen
= sizeof(char *) * MAXTOKENS
;
73 || (buflen
- aliaslen
) < BUFSZ
+ 1)
76 __UCLIBC_MUTEX_LOCK(mylock
);
77 //tok = (char **) buf;
80 setprotoent(proto_stayopen
);
84 protop
->data_len
= aliaslen
;
85 protop
->line_len
= buflen
- aliaslen
;
86 /* <name>[[:space:]]<protonumber>[[:space:]][<aliases>] */
87 if (!config_read(protop
, &tok
, MAXTOKENS
- 1, MINTOKENS
, "# \t/", PARSE_NORMAL
)) {
90 result_buf
->p_name
= *(tok
++);
91 result_buf
->p_proto
= atoi(*(tok
++));
92 result_buf
->p_aliases
= tok
;
96 __UCLIBC_MUTEX_UNLOCK(mylock
);
101 libc_hidden_def(getprotoent_r
)
103 static void __initbuf(void)
106 protobuf
= malloc(SBUFSIZE
);
112 struct protoent
*getprotoent(void)
114 struct protoent
*result
;
117 getprotoent_r(&protoe
, protobuf
, SBUFSIZE
, &result
);
121 int getprotobyname_r(const char *name
,
122 struct protoent
*result_buf
, char *buf
, size_t buflen
,
123 struct protoent
**result
)
128 __UCLIBC_MUTEX_LOCK(mylock
);
129 setprotoent(proto_stayopen
);
130 while (!(ret
= getprotoent_r(result_buf
, buf
, buflen
, result
))) {
131 if (strcmp(name
, result_buf
->p_name
) == 0)
133 for (cp
= result_buf
->p_aliases
; *cp
; cp
++)
134 if (strcmp(name
, *cp
) == 0)
140 __UCLIBC_MUTEX_UNLOCK(mylock
);
141 return *result
? 0 : ret
;
143 libc_hidden_def(getprotobyname_r
)
145 struct protoent
*getprotobyname(const char *name
)
147 struct protoent
*result
;
150 getprotobyname_r(name
, &protoe
, protobuf
, SBUFSIZE
, &result
);
154 int getprotobynumber_r(int proto
,
155 struct protoent
*result_buf
, char *buf
,
156 size_t buflen
, struct protoent
**result
)
160 __UCLIBC_MUTEX_LOCK(mylock
);
161 setprotoent(proto_stayopen
);
162 while (!(ret
= getprotoent_r(result_buf
, buf
, buflen
, result
))) {
163 if (proto
== result_buf
->p_proto
)
168 __UCLIBC_MUTEX_UNLOCK(mylock
);
169 return *result
? 0 : ret
;
171 libc_hidden_def(getprotobynumber_r
)
173 struct protoent
*getprotobynumber(int proto
)
175 struct protoent
*result
;
178 getprotobynumber_r(proto
, &protoe
, protobuf
, SBUFSIZE
, &result
);