libnl  3.2.21
utils.c
1 /*
2  * lib/utils.c Utility Functions
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation version 2.1
7  * of the License.
8  *
9  * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 /**
13  * @ingroup core
14  * @defgroup utils Utilities
15  *
16  * Collection of helper functions
17  *
18  * @{
19  *
20  * Header
21  * ------
22  * ~~~~{.c}
23  * #include <netlink/utils.h>
24  * ~~~~
25  */
26 
27 #include <netlink-private/netlink.h>
28 #include <netlink/netlink.h>
29 #include <netlink/utils.h>
30 #include <linux/socket.h>
31 #include <stdlib.h> /* exit() */
32 
33 /**
34  * Global variable indicating the desired level of debugging output.
35  *
36  * Level | Messages Printed
37  * ----- | ---------------------------------------------------------
38  * 0 | Debugging output disabled
39  * 1 | Warnings, important events and notifications
40  * 2 | More or less important debugging messages
41  * 3 | Repetitive events causing a flood of debugging messages
42  * 4 | Even less important messages
43  *
44  * If available, the variable will be initialized to the value of the
45  * environment variable `NLDBG`. The default value is 0 (disabled).
46  *
47  * For more information, see section @core_doc{_debugging, Debugging}.
48  */
49 int nl_debug = 0;
50 
51 /** @cond SKIP */
52 #ifdef NL_DEBUG
53 struct nl_dump_params nl_debug_dp = {
55 };
56 
57 static void __init nl_debug_init(void)
58 {
59  char *nldbg, *end;
60 
61  if ((nldbg = getenv("NLDBG"))) {
62  long level = strtol(nldbg, &end, 0);
63  if (nldbg != end)
64  nl_debug = level;
65  }
66 
67  nl_debug_dp.dp_fd = stderr;
68 }
69 #endif
70 
71 int __nl_read_num_str_file(const char *path, int (*cb)(long, const char *))
72 {
73  FILE *fd;
74  char buf[128];
75 
76  fd = fopen(path, "r");
77  if (fd == NULL)
78  return -nl_syserr2nlerr(errno);
79 
80  while (fgets(buf, sizeof(buf), fd)) {
81  int goodlen, err;
82  long num;
83  char *end;
84 
85  if (*buf == '#' || *buf == '\n' || *buf == '\r')
86  continue;
87 
88  num = strtol(buf, &end, 0);
89  if (end == buf) {
90  fclose(fd);
91  return -NLE_INVAL;
92  }
93 
94  if (num == LONG_MIN || num == LONG_MAX) {
95  fclose(fd);
96  return -NLE_RANGE;
97  }
98 
99  while (*end == ' ' || *end == '\t')
100  end++;
101 
102  goodlen = strcspn(end, "#\r\n\t ");
103  if (goodlen == 0) {
104  fclose(fd);
105  return -NLE_INVAL;
106  }
107 
108  end[goodlen] = '\0';
109 
110  err = cb(num, end);
111  if (err < 0) {
112  fclose(fd);
113  return err;
114  }
115  }
116 
117  fclose(fd);
118 
119  return 0;
120 }
121 /** @endcond */
122 
123 /**
124  * @name Pretty Printing of Numbers
125  * @{
126  */
127 
128 /**
129  * Cancel down a byte counter
130  * @arg l byte counter
131  * @arg unit destination unit pointer
132  *
133  * Cancels down a byte counter until it reaches a reasonable
134  * unit. The chosen unit is assigned to \a unit.
135  * This function assume 1024 bytes in one kilobyte
136  *
137  * @return The cancelled down byte counter in the new unit.
138  */
139 double nl_cancel_down_bytes(unsigned long long l, char **unit)
140 {
141  if (l >= 1099511627776LL) {
142  *unit = "TiB";
143  return ((double) l) / 1099511627776LL;
144  } else if (l >= 1073741824) {
145  *unit = "GiB";
146  return ((double) l) / 1073741824;
147  } else if (l >= 1048576) {
148  *unit = "MiB";
149  return ((double) l) / 1048576;
150  } else if (l >= 1024) {
151  *unit = "KiB";
152  return ((double) l) / 1024;
153  } else {
154  *unit = "B";
155  return (double) l;
156  }
157 }
158 
159 /**
160  * Cancel down a bit counter
161  * @arg l bit counter
162  * @arg unit destination unit pointer
163  *
164  * Cancels down bit counter until it reaches a reasonable
165  * unit. The chosen unit is assigned to \a unit.
166  * This function assume 1000 bits in one kilobit
167  *
168  * @return The cancelled down bit counter in the new unit.
169  */
170 double nl_cancel_down_bits(unsigned long long l, char **unit)
171 {
172  if (l >= 1000000000000ULL) {
173  *unit = "Tbit";
174  return ((double) l) / 1000000000000ULL;
175  }
176 
177  if (l >= 1000000000) {
178  *unit = "Gbit";
179  return ((double) l) / 1000000000;
180  }
181 
182  if (l >= 1000000) {
183  *unit = "Mbit";
184  return ((double) l) / 1000000;
185  }
186 
187  if (l >= 1000) {
188  *unit = "Kbit";
189  return ((double) l) / 1000;
190  }
191 
192  *unit = "bit";
193  return (double) l;
194 }
195 
196 int nl_rate2str(unsigned long long rate, int type, char *buf, size_t len)
197 {
198  char *unit;
199  double frac;
200 
201  switch (type) {
202  case NL_BYTE_RATE:
203  frac = nl_cancel_down_bytes(rate, &unit);
204  break;
205 
206  case NL_BIT_RATE:
207  frac = nl_cancel_down_bits(rate, &unit);
208  break;
209 
210  default:
211  BUG();
212  }
213 
214  return snprintf(buf, len, "%.2f%s/s", frac, unit);
215 }
216 
217 /**
218  * Cancel down a micro second value
219  * @arg l micro seconds
220  * @arg unit destination unit pointer
221  *
222  * Cancels down a microsecond counter until it reaches a
223  * reasonable unit. The chosen unit is assigned to \a unit.
224  *
225  * @return The cancelled down microsecond in the new unit
226  */
227 double nl_cancel_down_us(uint32_t l, char **unit)
228 {
229  if (l >= 1000000) {
230  *unit = "s";
231  return ((double) l) / 1000000;
232  } else if (l >= 1000) {
233  *unit = "ms";
234  return ((double) l) / 1000;
235  } else {
236  *unit = "us";
237  return (double) l;
238  }
239 }
240 
241 /** @} */
242 
243 /**
244  * @name Generic Unit Translations
245  * @{
246  */
247 
248 /**
249  * Convert a character string to a size
250  * @arg str size encoded as character string
251  *
252  * Converts the specified size as character to the corresponding
253  * number of bytes.
254  *
255  * Supported formats are:
256  * - b,kb/k,m/mb,gb/g for bytes
257  * - bit,kbit/mbit/gbit
258  *
259  * This function assume 1000 bits in one kilobit and
260  * 1024 bytes in one kilobyte
261  *
262  * @return The number of bytes or -1 if the string is unparseable
263  */
264 long nl_size2int(const char *str)
265 {
266  char *p;
267  long l = strtol(str, &p, 0);
268  if (p == str)
269  return -NLE_INVAL;
270 
271  if (*p) {
272  if (!strcasecmp(p, "kb") || !strcasecmp(p, "k"))
273  l *= 1024;
274  else if (!strcasecmp(p, "gb") || !strcasecmp(p, "g"))
275  l *= 1024*1024*1024;
276  else if (!strcasecmp(p, "gbit"))
277  l *= 1000000000L/8;
278  else if (!strcasecmp(p, "mb") || !strcasecmp(p, "m"))
279  l *= 1024*1024;
280  else if (!strcasecmp(p, "mbit"))
281  l *= 1000000/8;
282  else if (!strcasecmp(p, "kbit"))
283  l *= 1000/8;
284  else if (!strcasecmp(p, "bit"))
285  l /= 8;
286  else if (strcasecmp(p, "b") != 0)
287  return -NLE_INVAL;
288  }
289 
290  return l;
291 }
292 
293 static const struct {
294  double limit;
295  const char *unit;
296 } size_units[] = {
297  { 1024. * 1024. * 1024. * 1024. * 1024., "EiB" },
298  { 1024. * 1024. * 1024. * 1024., "TiB" },
299  { 1024. * 1024. * 1024., "GiB" },
300  { 1024. * 1024., "MiB" },
301  { 1024., "KiB" },
302  { 0., "B" },
303 };
304 
305 /**
306  * Convert a size toa character string
307  * @arg size Size in number of bytes
308  * @arg buf Buffer to write character string to
309  * @arg len Size of buf
310  *
311  * This function converts a value in bytes to a human readable representation
312  * of it. The function uses IEC prefixes:
313  *
314  * @code
315  * 1024 bytes => 1 KiB
316  * 1048576 bytes => 1 MiB
317  * @endcode
318  *
319  * The highest prefix is used which ensures a result of >= 1.0, the result
320  * is provided as floating point number with a maximum precision of 2 digits:
321  * @code
322  * 965176 bytes => 942.55 KiB
323  * @endcode
324  *
325  * @return pointer to buf
326  */
327 char *nl_size2str(const size_t size, char *buf, const size_t len)
328 {
329  size_t i;
330 
331  for (i = 0; i < ARRAY_SIZE(size_units); i++) {
332  if (size >= size_units[i].limit) {
333  snprintf(buf, len, "%.2g%s",
334  (double) size / size_units[i].limit,
335  size_units[i].unit);
336  return buf;
337  }
338  }
339 
340  BUG();
341 }
342 
343 /**
344  * Convert a character string to a probability
345  * @arg str probability encoded as character string
346  *
347  * Converts the specified probability as character to the
348  * corresponding probability number.
349  *
350  * Supported formats are:
351  * - 0.0-1.0
352  * - 0%-100%
353  *
354  * @return The probability relative to NL_PROB_MIN and NL_PROB_MAX
355  */
356 long nl_prob2int(const char *str)
357 {
358  char *p;
359  double d = strtod(str, &p);
360 
361  if (p == str)
362  return -NLE_INVAL;
363 
364  if (d > 1.0)
365  d /= 100.0f;
366 
367  if (d > 1.0f || d < 0.0f)
368  return -NLE_RANGE;
369 
370  if (*p && strcmp(p, "%") != 0)
371  return -NLE_INVAL;
372 
373  return rint(d * NL_PROB_MAX);
374 }
375 
376 /** @} */
377 
378 /**
379  * @name Time Translations
380  * @{
381  */
382 
383 #ifndef USER_HZ
384 #define USER_HZ 100
385 #endif
386 
387 static uint32_t user_hz = USER_HZ;
388 static uint32_t psched_hz = USER_HZ;
389 
390 static double ticks_per_usec = 1.0f;
391 
392 /* Retrieves the configured HZ and ticks/us value in the kernel.
393  * The value is cached. Supported ways of getting it:
394  *
395  * 1) environment variable
396  * 2) /proc/net/psched and sysconf
397  *
398  * Supports the environment variables:
399  * PROC_NET_PSCHED - may point to psched file in /proc
400  * PROC_ROOT - may point to /proc fs */
401 static void __init get_psched_settings(void)
402 {
403  char name[FILENAME_MAX];
404  FILE *fd;
405  int got_hz = 0;
406 
407  if (getenv("HZ")) {
408  long hz = strtol(getenv("HZ"), NULL, 0);
409 
410  if (LONG_MIN != hz && LONG_MAX != hz) {
411  user_hz = hz;
412  got_hz = 1;
413  }
414  }
415 
416  if (!got_hz)
417  user_hz = sysconf(_SC_CLK_TCK);
418 
419  psched_hz = user_hz;
420 
421  if (getenv("TICKS_PER_USEC")) {
422  double t = strtod(getenv("TICKS_PER_USEC"), NULL);
423  ticks_per_usec = t;
424  }
425  else {
426  if (getenv("PROC_NET_PSCHED"))
427  snprintf(name, sizeof(name), "%s", getenv("PROC_NET_PSCHED"));
428  else if (getenv("PROC_ROOT"))
429  snprintf(name, sizeof(name), "%s/net/psched",
430  getenv("PROC_ROOT"));
431  else
432  strncpy(name, "/proc/net/psched", sizeof(name) - 1);
433 
434  if ((fd = fopen(name, "r"))) {
435  unsigned int ns_per_usec, ns_per_tick, nom, denom;
436 
437  if (fscanf(fd, "%08x %08x %08x %08x",
438  &ns_per_usec, &ns_per_tick, &nom, &denom) != 4) {
439  NL_DBG(1, "Fatal error: can not read psched settings from \"%s\". " \
440  "Try to set TICKS_PER_USEC, PROC_NET_PSCHED or PROC_ROOT " \
441  "environment variables\n", name);
442  exit(1);
443  }
444 
445  ticks_per_usec = (double) ns_per_usec /
446  (double) ns_per_tick;
447 
448  if (nom == 1000000)
449  psched_hz = denom;
450 
451  fclose(fd);
452  }
453  }
454 }
455 
456 
457 /**
458  * Return the value of HZ
459  */
460 int nl_get_user_hz(void)
461 {
462  return user_hz;
463 }
464 
465 /**
466  * Return the value of packet scheduler HZ
467  */
469 {
470  return psched_hz;
471 }
472 
473 /**
474  * Convert micro seconds to ticks
475  * @arg us micro seconds
476  * @return number of ticks
477  */
478 uint32_t nl_us2ticks(uint32_t us)
479 {
480  return us * ticks_per_usec;
481 }
482 
483 
484 /**
485  * Convert ticks to micro seconds
486  * @arg ticks number of ticks
487  * @return microseconds
488  */
489 uint32_t nl_ticks2us(uint32_t ticks)
490 {
491  return ticks / ticks_per_usec;
492 }
493 
494 int nl_str2msec(const char *str, uint64_t *result)
495 {
496  uint64_t total = 0, l;
497  int plen;
498  char *p;
499 
500  do {
501  l = strtoul(str, &p, 0);
502  if (p == str)
503  return -NLE_INVAL;
504  else if (*p) {
505  plen = strcspn(p, " \t");
506 
507  if (!plen)
508  total += l;
509  else if (!strncasecmp(p, "sec", plen))
510  total += (l * 1000);
511  else if (!strncasecmp(p, "min", plen))
512  total += (l * 1000*60);
513  else if (!strncasecmp(p, "hour", plen))
514  total += (l * 1000*60*60);
515  else if (!strncasecmp(p, "day", plen))
516  total += (l * 1000*60*60*24);
517  else
518  return -NLE_INVAL;
519 
520  str = p + plen;
521  } else
522  total += l;
523  } while (*str && *p);
524 
525  *result = total;
526 
527  return 0;
528 }
529 
530 /**
531  * Convert milliseconds to a character string
532  * @arg msec number of milliseconds
533  * @arg buf destination buffer
534  * @arg len buffer length
535  *
536  * Converts milliseconds to a character string split up in days, hours,
537  * minutes, seconds, and milliseconds and stores it in the specified
538  * destination buffer.
539  *
540  * @return The destination buffer.
541  */
542 char * nl_msec2str(uint64_t msec, char *buf, size_t len)
543 {
544  uint64_t split[5];
545  size_t i;
546  static const char *units[5] = {"d", "h", "m", "s", "msec"};
547  char * const buf_orig = buf;
548 
549 #define _SPLIT(idx, unit) if ((split[idx] = msec / unit)) msec %= unit
550  _SPLIT(0, 86400000); /* days */
551  _SPLIT(1, 3600000); /* hours */
552  _SPLIT(2, 60000); /* minutes */
553  _SPLIT(3, 1000); /* seconds */
554 #undef _SPLIT
555  split[4] = msec;
556 
557  for (i = 0; i < ARRAY_SIZE(split) && len; i++) {
558  int l;
559  if (split[i] == 0)
560  continue;
561  l = snprintf(buf, len, "%s%" PRIu64 "%s",
562  (buf==buf_orig) ? "" : " ", split[i], units[i]);
563  buf += l;
564  len -= l;
565  }
566 
567  return buf_orig;
568 }
569 
570 /** @} */
571 
572 /**
573  * @name Netlink Family Translations
574  * @{
575  */
576 
577 static const struct trans_tbl nlfamilies[] = {
578  __ADD(NETLINK_ROUTE,route)
579  __ADD(NETLINK_USERSOCK,usersock)
580  __ADD(NETLINK_FIREWALL,firewall)
581  __ADD(NETLINK_INET_DIAG,inetdiag)
582  __ADD(NETLINK_NFLOG,nflog)
583  __ADD(NETLINK_XFRM,xfrm)
584  __ADD(NETLINK_SELINUX,selinux)
585  __ADD(NETLINK_ISCSI,iscsi)
586  __ADD(NETLINK_AUDIT,audit)
587  __ADD(NETLINK_FIB_LOOKUP,fib_lookup)
588  __ADD(NETLINK_CONNECTOR,connector)
589  __ADD(NETLINK_NETFILTER,netfilter)
590  __ADD(NETLINK_IP6_FW,ip6_fw)
591  __ADD(NETLINK_DNRTMSG,dnrtmsg)
592  __ADD(NETLINK_KOBJECT_UEVENT,kobject_uevent)
593  __ADD(NETLINK_GENERIC,generic)
594  __ADD(NETLINK_SCSITRANSPORT,scsitransport)
595  __ADD(NETLINK_ECRYPTFS,ecryptfs)
596 };
597 
598 char * nl_nlfamily2str(int family, char *buf, size_t size)
599 {
600  return __type2str(family, buf, size, nlfamilies,
601  ARRAY_SIZE(nlfamilies));
602 }
603 
604 int nl_str2nlfamily(const char *name)
605 {
606  return __str2type(name, nlfamilies, ARRAY_SIZE(nlfamilies));
607 }
608 
609 /**
610  * @}
611  */
612 
613 /**
614  * @name Link Layer Protocol Translations
615  * @{
616  */
617 
618 static const struct trans_tbl llprotos[] = {
619  {0, "generic"},
620  __ADD(ARPHRD_ETHER,ether)
621  __ADD(ARPHRD_EETHER,eether)
622  __ADD(ARPHRD_AX25,ax25)
623  __ADD(ARPHRD_PRONET,pronet)
624  __ADD(ARPHRD_CHAOS,chaos)
625  __ADD(ARPHRD_IEEE802,ieee802)
626  __ADD(ARPHRD_ARCNET,arcnet)
627  __ADD(ARPHRD_APPLETLK,atalk)
628  __ADD(ARPHRD_DLCI,dlci)
629  __ADD(ARPHRD_ATM,atm)
630  __ADD(ARPHRD_METRICOM,metricom)
631  __ADD(ARPHRD_IEEE1394,ieee1394)
632 #ifdef ARPHRD_EUI64
633  __ADD(ARPHRD_EUI64,eui64)
634 #endif
635  __ADD(ARPHRD_INFINIBAND,infiniband)
636  __ADD(ARPHRD_SLIP,slip)
637  __ADD(ARPHRD_CSLIP,cslip)
638  __ADD(ARPHRD_SLIP6,slip6)
639  __ADD(ARPHRD_CSLIP6,cslip6)
640  __ADD(ARPHRD_RSRVD,rsrvd)
641  __ADD(ARPHRD_ADAPT,adapt)
642  __ADD(ARPHRD_ROSE,rose)
643  __ADD(ARPHRD_X25,x25)
644 #ifdef ARPHRD_HWX25
645  __ADD(ARPHRD_HWX25,hwx25)
646 #endif
647  __ADD(ARPHRD_CAN,can)
648  __ADD(ARPHRD_PPP,ppp)
649  __ADD(ARPHRD_HDLC,hdlc)
650  __ADD(ARPHRD_LAPB,lapb)
651  __ADD(ARPHRD_DDCMP,ddcmp)
652  __ADD(ARPHRD_RAWHDLC,rawhdlc)
653  __ADD(ARPHRD_TUNNEL,ipip)
654  __ADD(ARPHRD_TUNNEL6,tunnel6)
655  __ADD(ARPHRD_FRAD,frad)
656  __ADD(ARPHRD_SKIP,skip)
657  __ADD(ARPHRD_LOOPBACK,loopback)
658  __ADD(ARPHRD_LOCALTLK,localtlk)
659  __ADD(ARPHRD_FDDI,fddi)
660  __ADD(ARPHRD_BIF,bif)
661  __ADD(ARPHRD_SIT,sit)
662  __ADD(ARPHRD_IPDDP,ip/ddp)
663  __ADD(ARPHRD_IPGRE,gre)
664  __ADD(ARPHRD_PIMREG,pimreg)
665  __ADD(ARPHRD_HIPPI,hippi)
666  __ADD(ARPHRD_ASH,ash)
667  __ADD(ARPHRD_ECONET,econet)
668  __ADD(ARPHRD_IRDA,irda)
669  __ADD(ARPHRD_FCPP,fcpp)
670  __ADD(ARPHRD_FCAL,fcal)
671  __ADD(ARPHRD_FCPL,fcpl)
672  __ADD(ARPHRD_FCFABRIC,fcfb_0)
673  __ADD(ARPHRD_FCFABRIC+1,fcfb_1)
674  __ADD(ARPHRD_FCFABRIC+2,fcfb_2)
675  __ADD(ARPHRD_FCFABRIC+3,fcfb_3)
676  __ADD(ARPHRD_FCFABRIC+4,fcfb_4)
677  __ADD(ARPHRD_FCFABRIC+5,fcfb_5)
678  __ADD(ARPHRD_FCFABRIC+6,fcfb_6)
679  __ADD(ARPHRD_FCFABRIC+7,fcfb_7)
680  __ADD(ARPHRD_FCFABRIC+8,fcfb_8)
681  __ADD(ARPHRD_FCFABRIC+9,fcfb_9)
682  __ADD(ARPHRD_FCFABRIC+10,fcfb_10)
683  __ADD(ARPHRD_FCFABRIC+11,fcfb_11)
684  __ADD(ARPHRD_FCFABRIC+12,fcfb_12)
685  __ADD(ARPHRD_IEEE802_TR,tr)
686  __ADD(ARPHRD_IEEE80211,ieee802.11)
687  __ADD(ARPHRD_PHONET,phonet)
688  __ADD(ARPHRD_CAIF, caif)
689 #ifdef ARPHRD_IEEE80211_PRISM
690  __ADD(ARPHRD_IEEE80211_PRISM, ieee802.11_prism)
691 #endif
692 #ifdef ARPHRD_VOID
693  __ADD(ARPHRD_VOID,void)
694 #endif
695 #ifdef ARPHRD_NONE
696  __ADD(ARPHRD_NONE,nohdr)
697 #endif
698 };
699 
700 char * nl_llproto2str(int llproto, char *buf, size_t len)
701 {
702  return __type2str(llproto, buf, len, llprotos, ARRAY_SIZE(llprotos));
703 }
704 
705 int nl_str2llproto(const char *name)
706 {
707  return __str2type(name, llprotos, ARRAY_SIZE(llprotos));
708 }
709 
710 /** @} */
711 
712 
713 /**
714  * @name Ethernet Protocol Translations
715  * @{
716  */
717 
718 static const struct trans_tbl ether_protos[] = {
719  __ADD(ETH_P_LOOP,loop)
720  __ADD(ETH_P_PUP,pup)
721  __ADD(ETH_P_PUPAT,pupat)
722  __ADD(ETH_P_IP,ip)
723  __ADD(ETH_P_X25,x25)
724  __ADD(ETH_P_ARP,arp)
725  __ADD(ETH_P_BPQ,bpq)
726  __ADD(ETH_P_IEEEPUP,ieeepup)
727  __ADD(ETH_P_IEEEPUPAT,ieeepupat)
728  __ADD(ETH_P_DEC,dec)
729  __ADD(ETH_P_DNA_DL,dna_dl)
730  __ADD(ETH_P_DNA_RC,dna_rc)
731  __ADD(ETH_P_DNA_RT,dna_rt)
732  __ADD(ETH_P_LAT,lat)
733  __ADD(ETH_P_DIAG,diag)
734  __ADD(ETH_P_CUST,cust)
735  __ADD(ETH_P_SCA,sca)
736  __ADD(ETH_P_TEB,teb)
737  __ADD(ETH_P_RARP,rarp)
738  __ADD(ETH_P_ATALK,atalk)
739  __ADD(ETH_P_AARP,aarp)
740 #ifdef ETH_P_8021Q
741  __ADD(ETH_P_8021Q,802.1q)
742 #endif
743  __ADD(ETH_P_IPX,ipx)
744  __ADD(ETH_P_IPV6,ipv6)
745  __ADD(ETH_P_PAUSE,pause)
746  __ADD(ETH_P_SLOW,slow)
747 #ifdef ETH_P_WCCP
748  __ADD(ETH_P_WCCP,wccp)
749 #endif
750  __ADD(ETH_P_PPP_DISC,ppp_disc)
751  __ADD(ETH_P_PPP_SES,ppp_ses)
752  __ADD(ETH_P_MPLS_UC,mpls_uc)
753  __ADD(ETH_P_MPLS_MC,mpls_mc)
754  __ADD(ETH_P_ATMMPOA,atmmpoa)
755  __ADD(ETH_P_LINK_CTL,link_ctl)
756  __ADD(ETH_P_ATMFATE,atmfate)
757  __ADD(ETH_P_PAE,pae)
758  __ADD(ETH_P_AOE,aoe)
759  __ADD(ETH_P_TIPC,tipc)
760  __ADD(ETH_P_1588,ieee1588)
761  __ADD(ETH_P_FCOE,fcoe)
762  __ADD(ETH_P_FIP,fip)
763  __ADD(ETH_P_EDSA,edsa)
764  __ADD(ETH_P_EDP2,edp2)
765  __ADD(ETH_P_802_3,802.3)
766  __ADD(ETH_P_AX25,ax25)
767  __ADD(ETH_P_ALL,all)
768  __ADD(ETH_P_802_2,802.2)
769  __ADD(ETH_P_SNAP,snap)
770  __ADD(ETH_P_DDCMP,ddcmp)
771  __ADD(ETH_P_WAN_PPP,wan_ppp)
772  __ADD(ETH_P_PPP_MP,ppp_mp)
773  __ADD(ETH_P_LOCALTALK,localtalk)
774  __ADD(ETH_P_CAN,can)
775  __ADD(ETH_P_PPPTALK,ppptalk)
776  __ADD(ETH_P_TR_802_2,tr_802.2)
777  __ADD(ETH_P_MOBITEX,mobitex)
778  __ADD(ETH_P_CONTROL,control)
779  __ADD(ETH_P_IRDA,irda)
780  __ADD(ETH_P_ECONET,econet)
781  __ADD(ETH_P_HDLC,hdlc)
782  __ADD(ETH_P_ARCNET,arcnet)
783  __ADD(ETH_P_DSA,dsa)
784  __ADD(ETH_P_TRAILER,trailer)
785  __ADD(ETH_P_PHONET,phonet)
786  __ADD(ETH_P_IEEE802154,ieee802154)
787  __ADD(ETH_P_CAIF,caif)
788 };
789 
790 char *nl_ether_proto2str(int eproto, char *buf, size_t len)
791 {
792  return __type2str(eproto, buf, len, ether_protos,
793  ARRAY_SIZE(ether_protos));
794 }
795 
796 int nl_str2ether_proto(const char *name)
797 {
798  return __str2type(name, ether_protos, ARRAY_SIZE(ether_protos));
799 }
800 
801 /** @} */
802 
803 /**
804  * @name IP Protocol Translations
805  * @{
806  */
807 
808 char *nl_ip_proto2str(int proto, char *buf, size_t len)
809 {
810  struct protoent *p = getprotobynumber(proto);
811 
812  if (p) {
813  snprintf(buf, len, "%s", p->p_name);
814  return buf;
815  }
816 
817  snprintf(buf, len, "0x%x", proto);
818  return buf;
819 }
820 
821 int nl_str2ip_proto(const char *name)
822 {
823  struct protoent *p = getprotobyname(name);
824  unsigned long l;
825  char *end;
826 
827  if (p)
828  return p->p_proto;
829 
830  l = strtoul(name, &end, 0);
831  if (l == ULONG_MAX || *end != '\0')
832  return -NLE_OBJ_NOTFOUND;
833 
834  return (int) l;
835 }
836 
837 /** @} */
838 
839 /**
840  * @name Dumping Helpers
841  * @{
842  */
843 
844 /**
845  * Handle a new line while dumping
846  * @arg params Dumping parameters
847  *
848  * This function must be called before dumping any onto a
849  * new line. It will ensure proper prefixing as specified
850  * by the dumping parameters.
851  *
852  * @note This function will NOT dump any newlines itself
853  */
854 void nl_new_line(struct nl_dump_params *params)
855 {
856  params->dp_line++;
857 
858  if (params->dp_prefix) {
859  int i;
860  for (i = 0; i < params->dp_prefix; i++) {
861  if (params->dp_fd)
862  fprintf(params->dp_fd, " ");
863  else if (params->dp_buf)
864  strncat(params->dp_buf, " ",
865  params->dp_buflen -
866  strlen(params->dp_buf) - 1);
867  }
868  }
869 
870  if (params->dp_nl_cb)
871  params->dp_nl_cb(params, params->dp_line);
872 }
873 
874 static void dump_one(struct nl_dump_params *parms, const char *fmt,
875  va_list args)
876 {
877  if (parms->dp_fd)
878  vfprintf(parms->dp_fd, fmt, args);
879  else if (parms->dp_buf || parms->dp_cb) {
880  char *buf = NULL;
881  if (vasprintf(&buf, fmt, args) >= 0) {
882  if (parms->dp_cb)
883  parms->dp_cb(parms, buf);
884  else
885  strncat(parms->dp_buf, buf,
886  parms->dp_buflen -
887  strlen(parms->dp_buf) - 1);
888  free(buf);
889  }
890  }
891 }
892 
893 
894 /**
895  * Dump a formatted character string
896  * @arg params Dumping parameters
897  * @arg fmt printf style formatting string
898  * @arg ... Arguments to formatting string
899  *
900  * Dumps a printf style formatting string to the output device
901  * as specified by the dumping parameters.
902  */
903 void nl_dump(struct nl_dump_params *params, const char *fmt, ...)
904 {
905  va_list args;
906 
907  va_start(args, fmt);
908  dump_one(params, fmt, args);
909  va_end(args);
910 }
911 
912 void nl_dump_line(struct nl_dump_params *parms, const char *fmt, ...)
913 {
914  va_list args;
915 
916  nl_new_line(parms);
917 
918  va_start(args, fmt);
919  dump_one(parms, fmt, args);
920  va_end(args);
921 }
922 
923 
924 /** @} */
925 
926 /** @cond SKIP */
927 
928 int __trans_list_add(int i, const char *a, struct nl_list_head *head)
929 {
930  struct trans_list *tl;
931 
932  tl = calloc(1, sizeof(*tl));
933  if (!tl)
934  return -NLE_NOMEM;
935 
936  tl->i = i;
937  tl->a = strdup(a);
938 
939  nl_list_add_tail(&tl->list, head);
940 
941  return 0;
942 }
943 
944 void __trans_list_clear(struct nl_list_head *head)
945 {
946  struct trans_list *tl, *next;
947 
948  nl_list_for_each_entry_safe(tl, next, head, list) {
949  free(tl->a);
950  free(tl);
951  }
952 
953  nl_init_list_head(head);
954 }
955 
956 char *__type2str(int type, char *buf, size_t len,
957  const struct trans_tbl *tbl, size_t tbl_len)
958 {
959  size_t i;
960  for (i = 0; i < tbl_len; i++) {
961  if (tbl[i].i == type) {
962  snprintf(buf, len, "%s", tbl[i].a);
963  return buf;
964  }
965  }
966 
967  snprintf(buf, len, "0x%x", type);
968  return buf;
969 }
970 
971 char *__list_type2str(int type, char *buf, size_t len,
972  struct nl_list_head *head)
973 {
974  struct trans_list *tl;
975 
976  nl_list_for_each_entry(tl, head, list) {
977  if (tl->i == type) {
978  snprintf(buf, len, "%s", tl->a);
979  return buf;
980  }
981  }
982 
983  snprintf(buf, len, "0x%x", type);
984  return buf;
985 }
986 
987 char *__flags2str(int flags, char *buf, size_t len,
988  const struct trans_tbl *tbl, size_t tbl_len)
989 {
990  size_t i;
991  int tmp = flags;
992 
993  memset(buf, 0, len);
994 
995  for (i = 0; i < tbl_len; i++) {
996  if (tbl[i].i & tmp) {
997  tmp &= ~tbl[i].i;
998  strncat(buf, tbl[i].a, len - strlen(buf) - 1);
999  if ((tmp & flags))
1000  strncat(buf, ",", len - strlen(buf) - 1);
1001  }
1002  }
1003 
1004  return buf;
1005 }
1006 
1007 int __str2type(const char *buf, const struct trans_tbl *tbl, size_t tbl_len)
1008 {
1009  unsigned long l;
1010  char *end;
1011  size_t i;
1012 
1013  if (*buf == '\0')
1014  return -NLE_INVAL;
1015 
1016  for (i = 0; i < tbl_len; i++)
1017  if (!strcasecmp(tbl[i].a, buf))
1018  return tbl[i].i;
1019 
1020  l = strtoul(buf, &end, 0);
1021  if (l == ULONG_MAX || *end != '\0')
1022  return -NLE_OBJ_NOTFOUND;
1023 
1024  return (int) l;
1025 }
1026 
1027 int __list_str2type(const char *buf, struct nl_list_head *head)
1028 {
1029  struct trans_list *tl;
1030  unsigned long l;
1031  char *end;
1032 
1033  if (*buf == '\0')
1034  return -NLE_INVAL;
1035 
1036  nl_list_for_each_entry(tl, head, list) {
1037  if (!strcasecmp(tl->a, buf))
1038  return tl->i;
1039  }
1040 
1041  l = strtoul(buf, &end, 0);
1042  if (l == ULONG_MAX || *end != '\0')
1043  return -NLE_OBJ_NOTFOUND;
1044 
1045  return (int) l;
1046 }
1047 
1048 int __str2flags(const char *buf, const struct trans_tbl *tbl, size_t tbl_len)
1049 {
1050  int flags = 0;
1051  size_t i;
1052  size_t len; /* ptrdiff_t ? */
1053  char *p = (char *) buf, *t;
1054 
1055  for (;;) {
1056  if (*p == ' ')
1057  p++;
1058 
1059  t = strchr(p, ',');
1060  len = t ? t - p : strlen(p);
1061  for (i = 0; i < tbl_len; i++)
1062  if (len == strlen(tbl[i].a) &&
1063  !strncasecmp(tbl[i].a, p, len))
1064  flags |= tbl[i].i;
1065 
1066  if (!t)
1067  return flags;
1068 
1069  p = ++t;
1070  }
1071 
1072  return 0;
1073 }
1074 
1075 void dump_from_ops(struct nl_object *obj, struct nl_dump_params *params)
1076 {
1077  int type = params->dp_type;
1078 
1079  if (type < 0 || type > NL_DUMP_MAX)
1080  BUG();
1081 
1082  params->dp_line = 0;
1083 
1084  if (params->dp_dump_msgtype) {
1085 #if 0
1086  /* XXX */
1087  char buf[64];
1088 
1089  dp_dump_line(params, 0, "%s ",
1090  nl_cache_mngt_type2name(obj->ce_ops,
1091  obj->ce_ops->co_protocol,
1092  obj->ce_msgtype,
1093  buf, sizeof(buf)));
1094 #endif
1095  params->dp_pre_dump = 1;
1096  }
1097 
1098  if (obj->ce_ops->oo_dump[type])
1099  obj->ce_ops->oo_dump[type](obj, params);
1100 }
1101 
1102 /** @endcond */
1103 
1104 /** @} */