Use glibc_likely instead __builtin_expect.
[jlayton/glibc.git] / nis / nss_nisplus / nisplus-ethers.c
1 /* Copyright (C) 1997-2014 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <atomic.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <inttypes.h>
23 #include <netdb.h>
24 #include <nss.h>
25 #include <string.h>
26 #include <netinet/ether.h>
27 #include <netinet/if_ether.h>
28 #include <rpcsvc/nis.h>
29 #include <bits/libc-lock.h>
30
31 #include "nss-nisplus.h"
32
33 __libc_lock_define_initialized (static, lock)
34
35 static nis_result *result;
36 static nis_name tablename_val;
37 static u_long tablename_len;
38
39
40 #define NISENTRYVAL(idx, col, res) \
41         (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
42
43 #define NISENTRYLEN(idx, col, res) \
44         (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
45
46 static int
47 _nss_nisplus_parse_etherent (nis_result *result, struct etherent *ether,
48                              char *buffer, size_t buflen, int *errnop)
49 {
50   char *p = buffer;
51   size_t room_left = buflen;
52
53   if (result == NULL)
54     return 0;
55
56   if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
57       || NIS_RES_NUMOBJ (result) != 1
58       || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
59       || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type,
60                  "ethers_tbl") != 0
61       || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 2)
62     return 0;
63
64   /* Generate the ether entry format and use the normal parser */
65   if (NISENTRYLEN (0, 0, result) + 1 > room_left)
66     {
67       *errnop = ERANGE;
68       return -1;
69     }
70   char *cp = __stpncpy (p, NISENTRYVAL (0, 0, result),
71                         NISENTRYLEN (0, 0, result));
72   *cp = '\0';
73   room_left -= NISENTRYLEN (0, 0, result) + 1;
74   ether->e_name = p;
75
76   struct ether_addr *ea = ether_aton (NISENTRYVAL (0, 1, result));
77   if (ea == NULL)
78     {
79       *errnop = EINVAL;
80       return -2;
81     }
82
83   ether->e_addr = *ea;
84
85   return 1;
86 }
87
88 static enum nss_status
89 _nss_create_tablename (int *errnop)
90 {
91   if (tablename_val == NULL)
92     {
93       const char *local_dir = nis_local_directory ();
94       size_t local_dir_len = strlen (local_dir);
95       static const char prefix[] = "ethers.org_dir.";
96
97       char *p = malloc (sizeof (prefix) + local_dir_len);
98       if (p == NULL)
99         {
100           *errnop = errno;
101           return NSS_STATUS_TRYAGAIN;
102         }
103
104       memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
105
106       tablename_len = sizeof (prefix) - 1 + local_dir_len;
107
108       atomic_write_barrier ();
109
110       tablename_val = p;
111     }
112   return NSS_STATUS_SUCCESS;
113 }
114
115
116 enum nss_status
117 _nss_nisplus_setetherent (int stayopen)
118 {
119   enum nss_status status;
120   int err;
121
122   status = NSS_STATUS_SUCCESS;
123
124   __libc_lock_lock (lock);
125
126   if (result != NULL)
127     {
128       nis_freeresult (result);
129       result = NULL;
130     }
131
132   if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
133     status = NSS_STATUS_UNAVAIL;
134
135   __libc_lock_unlock (lock);
136
137   return status;
138 }
139
140 enum nss_status
141 _nss_nisplus_endetherent (void)
142 {
143   __libc_lock_lock (lock);
144
145   if (result != NULL)
146     {
147       nis_freeresult (result);
148       result = NULL;
149     }
150
151   __libc_lock_unlock (lock);
152
153   return NSS_STATUS_SUCCESS;
154 }
155
156 static enum nss_status
157 internal_nisplus_getetherent_r (struct etherent *ether, char *buffer,
158                                 size_t buflen, int *errnop)
159 {
160   if (tablename_val == NULL)
161     {
162       enum nss_status status = _nss_create_tablename (errnop);
163
164       if (status != NSS_STATUS_SUCCESS)
165         return status;
166     }
167
168   /* Get the next entry until we found a correct one. */
169   int parse_res;
170   do
171     {
172       nis_result *saved_result;
173
174       if (result == NULL)
175         {
176           saved_result = NULL;
177           result = nis_first_entry (tablename_val);
178           if (result == NULL)
179             {
180               *errnop = errno;
181               return NSS_STATUS_TRYAGAIN;
182             }
183           if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
184             return niserr2nss (result->status);
185         }
186       else
187         {
188           saved_result = result;
189           result = nis_next_entry (tablename_val, &result->cookie);
190           if (result == NULL)
191             {
192               *errnop = errno;
193               return NSS_STATUS_TRYAGAIN;
194             }
195           if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
196             {
197               nis_freeresult (saved_result);
198               return niserr2nss (result->status);
199             }
200         }
201
202       parse_res = _nss_nisplus_parse_etherent (result, ether, buffer,
203                                                buflen, errnop);
204       if (parse_res == -1)
205         {
206           nis_freeresult (result);
207           result = saved_result;
208           return NSS_STATUS_TRYAGAIN;
209         }
210
211       if (saved_result != NULL)
212         nis_freeresult (saved_result);
213
214     }
215   while (!parse_res);
216
217   return NSS_STATUS_SUCCESS;
218 }
219
220 enum nss_status
221 _nss_nisplus_getetherent_r (struct etherent *result, char *buffer,
222                             size_t buflen, int *errnop)
223 {
224   int status;
225
226   __libc_lock_lock (lock);
227
228   status = internal_nisplus_getetherent_r (result, buffer, buflen, errnop);
229
230   __libc_lock_unlock (lock);
231
232   return status;
233 }
234
235 enum nss_status
236 _nss_nisplus_gethostton_r (const char *name, struct etherent *eth,
237                            char *buffer, size_t buflen, int *errnop)
238 {
239   if (tablename_val == NULL)
240     {
241       enum nss_status status = _nss_create_tablename (errnop);
242
243       if (status != NSS_STATUS_SUCCESS)
244         return status;
245     }
246
247   if (name == NULL)
248     {
249       *errnop = EINVAL;
250       return NSS_STATUS_UNAVAIL;
251     }
252
253   char buf[strlen (name) + 9 + tablename_len];
254   int olderr = errno;
255
256   snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
257
258   nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM,
259                                  NULL, NULL);
260
261   if (result == NULL)
262     {
263       *errnop = ENOMEM;
264       return NSS_STATUS_TRYAGAIN;
265     }
266
267   if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS))
268     {
269       enum nss_status status = niserr2nss (result->status);
270       nis_freeresult (result);
271       return status;
272     }
273
274   int parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
275                                                buflen, errnop);
276
277   /* We do not need the lookup result anymore.  */
278   nis_freeresult (result);
279
280   if (__glibc_unlikely (parse_res < 1))
281     {
282       __set_errno (olderr);
283
284       if (parse_res == -1)
285         return NSS_STATUS_TRYAGAIN;
286
287       return NSS_STATUS_NOTFOUND;
288     }
289
290   return NSS_STATUS_SUCCESS;
291 }
292
293 enum nss_status
294 _nss_nisplus_getntohost_r (const struct ether_addr *addr, struct etherent *eth,
295                            char *buffer, size_t buflen, int *errnop)
296 {
297   if (tablename_val == NULL)
298     {
299       __libc_lock_lock (lock);
300
301       enum nss_status status = _nss_create_tablename (errnop);
302
303       __libc_lock_unlock (lock);
304
305       if (status != NSS_STATUS_SUCCESS)
306         return status;
307     }
308
309   if (addr == NULL)
310     {
311       *errnop = EINVAL;
312       return NSS_STATUS_UNAVAIL;
313     }
314
315   char buf[26 + tablename_len];
316
317   snprintf (buf, sizeof (buf),
318             "[addr=%" PRIx8 ":%" PRIx8 ":%" PRIx8 ":%" PRIx8 ":%" PRIx8
319             ":%" PRIx8 "],%s",
320             addr->ether_addr_octet[0], addr->ether_addr_octet[1],
321             addr->ether_addr_octet[2], addr->ether_addr_octet[3],
322             addr->ether_addr_octet[4], addr->ether_addr_octet[5],
323             tablename_val);
324
325   nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM,
326                                  NULL, NULL);
327
328   if (result == NULL)
329     {
330       *errnop = ENOMEM;
331       return NSS_STATUS_TRYAGAIN;
332     }
333
334   if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS))
335     {
336       enum nss_status status = niserr2nss (result->status);
337       nis_freeresult (result);
338       return status;
339     }
340
341   int parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
342                                                buflen, errnop);
343
344   /* We do not need the lookup result anymore.  */
345   nis_freeresult (result);
346
347   if (__glibc_unlikely (parse_res < 1))
348     {
349       if (parse_res == -1)
350         return NSS_STATUS_TRYAGAIN;
351
352       return NSS_STATUS_NOTFOUND;
353     }
354
355   return NSS_STATUS_SUCCESS;
356 }