utils.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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-2006 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @defgroup utils Utilities
  13. * @{
  14. */
  15. #include <netlink-local.h>
  16. #include <netlink/netlink.h>
  17. #include <netlink/utils.h>
  18. #include <linux/socket.h>
  19. /**
  20. * Debug level
  21. */
  22. int nl_debug = 0;
  23. struct nl_dump_params nl_debug_dp = {
  24. .dp_type = NL_DUMP_FULL,
  25. };
  26. static void __init nl_debug_init(void)
  27. {
  28. char *nldbg, *end;
  29. if ((nldbg = getenv("NLDBG"))) {
  30. long level = strtol(nldbg, &end, 0);
  31. if (nldbg != end)
  32. nl_debug = level;
  33. }
  34. nl_debug_dp.dp_fd = stderr;
  35. }
  36. /**
  37. * @name Error Code Helpers
  38. * @{
  39. */
  40. static __thread char *errbuf;
  41. static __thread int nlerrno;
  42. /** @cond SKIP */
  43. int __nl_error(int err, const char *file, unsigned int line, const char *func,
  44. const char *fmt, ...)
  45. {
  46. char *user_err;
  47. va_list args;
  48. if (errbuf) {
  49. free(errbuf);
  50. errbuf = NULL;
  51. }
  52. nlerrno = err;
  53. if (fmt) {
  54. va_start(args, fmt);
  55. vasprintf(&user_err, fmt, args);
  56. va_end(args);
  57. }
  58. #ifdef VERBOSE_ERRORS
  59. asprintf(&errbuf, "%s:%u:%s: %s (errno = %s)",
  60. file, line, func, fmt ? user_err : "", strerror(err));
  61. #else
  62. asprintf(&errbuf, "%s (errno = %s)",
  63. fmt ? user_err : "", strerror(err));
  64. #endif
  65. if (fmt)
  66. free(user_err);
  67. return -err;
  68. }
  69. int __nl_read_num_str_file(const char *path, int (*cb)(long, const char *))
  70. {
  71. FILE *fd;
  72. char buf[128];
  73. fd = fopen(path, "r");
  74. if (fd == NULL)
  75. return nl_error(errno, "Unable to open file %s for reading",
  76. path);
  77. while (fgets(buf, sizeof(buf), fd)) {
  78. int goodlen, err;
  79. long num;
  80. char *end;
  81. if (*buf == '#' || *buf == '\n' || *buf == '\r')
  82. continue;
  83. num = strtol(buf, &end, 0);
  84. if (end == buf)
  85. return nl_error(EINVAL, "Parsing error");
  86. if (num == LONG_MIN || num == LONG_MAX)
  87. return nl_error(errno, "Number of out range");
  88. while (*end == ' ' || *end == '\t')
  89. end++;
  90. goodlen = strcspn(end, "#\r\n\t ");
  91. if (goodlen == 0)
  92. return nl_error(EINVAL, "Empty string");
  93. end[goodlen] = '\0';
  94. err = cb(num, end);
  95. if (err < 0)
  96. return err;
  97. }
  98. fclose(fd);
  99. return 0;
  100. }
  101. /** @endcond */
  102. int nl_get_errno(void)
  103. {
  104. return nlerrno;
  105. }
  106. /**
  107. * Return error message for an error code
  108. * @return error message
  109. */
  110. char *nl_geterror(void)
  111. {
  112. if (errbuf)
  113. return errbuf;
  114. if (nlerrno)
  115. return strerror(nlerrno);
  116. return "Sucess\n";
  117. }
  118. /**
  119. * Print a libnl error message
  120. * @arg s error message prefix
  121. *
  122. * Prints the error message of the call that failed last.
  123. *
  124. * If s is not NULL and *s is not a null byte the argument
  125. * string is printed, followed by a colon and a blank. Then
  126. * the error message and a new-line.
  127. */
  128. void nl_perror(const char *s)
  129. {
  130. if (s && *s)
  131. fprintf(stderr, "%s: %s\n", s, nl_geterror());
  132. else
  133. fprintf(stderr, "%s\n", nl_geterror());
  134. }
  135. /** @} */
  136. /**
  137. * @name Unit Pretty-Printing
  138. * @{
  139. */
  140. /**
  141. * Cancel down a byte counter
  142. * @arg l byte counter
  143. * @arg unit destination unit pointer
  144. *
  145. * Cancels down a byte counter until it reaches a reasonable
  146. * unit. The chosen unit is assigned to \a unit.
  147. * This function assume 1024 bytes in one kilobyte
  148. *
  149. * @return The cancelled down byte counter in the new unit.
  150. */
  151. double nl_cancel_down_bytes(unsigned long long l, char **unit)
  152. {
  153. if (l >= 1099511627776LL) {
  154. *unit = "TiB";
  155. return ((double) l) / 1099511627776LL;
  156. } else if (l >= 1073741824) {
  157. *unit = "GiB";
  158. return ((double) l) / 1073741824;
  159. } else if (l >= 1048576) {
  160. *unit = "MiB";
  161. return ((double) l) / 1048576;
  162. } else if (l >= 1024) {
  163. *unit = "KiB";
  164. return ((double) l) / 1024;
  165. } else {
  166. *unit = "B";
  167. return (double) l;
  168. }
  169. }
  170. /**
  171. * Cancel down a bit counter
  172. * @arg l bit counter
  173. * @arg unit destination unit pointer
  174. *
  175. * Cancels down bit counter until it reaches a reasonable
  176. * unit. The chosen unit is assigned to \a unit.
  177. * This function assume 1000 bits in one kilobit
  178. *
  179. * @return The cancelled down bit counter in the new unit.
  180. */
  181. double nl_cancel_down_bits(unsigned long long l, char **unit)
  182. {
  183. if (l >= 1000000000000ULL) {
  184. *unit = "Tbit";
  185. return ((double) l) / 1000000000000ULL;
  186. }
  187. if (l >= 1000000000) {
  188. *unit = "Gbit";
  189. return ((double) l) / 1000000000;
  190. }
  191. if (l >= 1000000) {
  192. *unit = "Mbit";
  193. return ((double) l) / 1000000;
  194. }
  195. if (l >= 1000) {
  196. *unit = "Kbit";
  197. return ((double) l) / 1000;
  198. }
  199. *unit = "bit";
  200. return (double) l;
  201. }
  202. /**
  203. * Cancel down a micro second value
  204. * @arg l micro seconds
  205. * @arg unit destination unit pointer
  206. *
  207. * Cancels down a microsecond counter until it reaches a
  208. * reasonable unit. The chosen unit is assigned to \a unit.
  209. *
  210. * @return The cancelled down microsecond in the new unit
  211. */
  212. double nl_cancel_down_us(uint32_t l, char **unit)
  213. {
  214. if (l >= 1000000) {
  215. *unit = "s";
  216. return ((double) l) / 1000000;
  217. } else if (l >= 1000) {
  218. *unit = "ms";
  219. return ((double) l) / 1000;
  220. } else {
  221. *unit = "us";
  222. return (double) l;
  223. }
  224. }
  225. /** @} */
  226. /**
  227. * @name Generic Unit Translations
  228. * @{
  229. */
  230. /**
  231. * Convert a character string to a size
  232. * @arg str size encoded as character string
  233. *
  234. * Converts the specified size as character to the corresponding
  235. * number of bytes.
  236. *
  237. * Supported formats are:
  238. * - b,kb/k,m/mb,gb/g for bytes
  239. * - bit,kbit/mbit/gbit
  240. *
  241. * This function assume 1000 bits in one kilobit and
  242. * 1024 bytes in one kilobyte
  243. *
  244. * @return The number of bytes or -1 if the string is unparseable
  245. */
  246. long nl_size2int(const char *str)
  247. {
  248. char *p;
  249. long l = strtol(str, &p, 0);
  250. if (p == str)
  251. return -1;
  252. if (*p) {
  253. if (!strcasecmp(p, "kb") || !strcasecmp(p, "k"))
  254. l *= 1024;
  255. else if (!strcasecmp(p, "gb") || !strcasecmp(p, "g"))
  256. l *= 1024*1024*1024;
  257. else if (!strcasecmp(p, "gbit"))
  258. l *= 1000000000L/8;
  259. else if (!strcasecmp(p, "mb") || !strcasecmp(p, "m"))
  260. l *= 1024*1024;
  261. else if (!strcasecmp(p, "mbit"))
  262. l *= 1000000/8;
  263. else if (!strcasecmp(p, "kbit"))
  264. l *= 1000/8;
  265. else if (!strcasecmp(p, "bit"))
  266. l /= 8;
  267. else if (strcasecmp(p, "b") != 0)
  268. return -1;
  269. }
  270. return l;
  271. }
  272. /**
  273. * Convert a character string to a probability
  274. * @arg str probability encoded as character string
  275. *
  276. * Converts the specified probability as character to the
  277. * corresponding probability number.
  278. *
  279. * Supported formats are:
  280. * - 0.0-1.0
  281. * - 0%-100%
  282. *
  283. * @return The probability relative to NL_PROB_MIN and NL_PROB_MAX
  284. */
  285. long nl_prob2int(const char *str)
  286. {
  287. char *p;
  288. double d = strtod(str, &p);
  289. if (p == str)
  290. return -1;
  291. if (d > 1.0)
  292. d /= 100.0f;
  293. if (d > 1.0f || d < 0.0f)
  294. return -1;
  295. if (*p && strcmp(p, "%") != 0)
  296. return -1;
  297. return rint(d * NL_PROB_MAX);
  298. }
  299. /** @} */
  300. /**
  301. * @name Time Translations
  302. * @{
  303. */
  304. #ifdef USER_HZ
  305. static uint32_t user_hz = USER_HZ;
  306. #else
  307. static uint32_t user_hz = 100;
  308. #endif
  309. static double ticks_per_usec = 1.0f;
  310. /* Retrieves the configured HZ and ticks/us value in the kernel.
  311. * The value is cached. Supported ways of getting it:
  312. *
  313. * 1) environment variable
  314. * 2) /proc/net/psched and sysconf
  315. *
  316. * Supports the environment variables:
  317. * PROC_NET_PSCHED - may point to psched file in /proc
  318. * PROC_ROOT - may point to /proc fs */
  319. static void __init get_psched_settings(void)
  320. {
  321. char name[FILENAME_MAX];
  322. FILE *fd;
  323. int got_hz = 0, got_tick = 0;
  324. if (getenv("HZ")) {
  325. long hz = strtol(getenv("HZ"), NULL, 0);
  326. if (LONG_MIN != hz && LONG_MAX != hz) {
  327. user_hz = hz;
  328. got_hz = 1;
  329. }
  330. }
  331. if (!got_hz)
  332. user_hz = sysconf(_SC_CLK_TCK);
  333. if (getenv("TICKS_PER_USEC")) {
  334. double t = strtod(getenv("TICKS_PER_USEC"), NULL);
  335. ticks_per_usec = t;
  336. got_tick = 1;
  337. }
  338. if (getenv("PROC_NET_PSCHED"))
  339. snprintf(name, sizeof(name), "%s", getenv("PROC_NET_PSCHED"));
  340. else if (getenv("PROC_ROOT"))
  341. snprintf(name, sizeof(name), "%s/net/psched",
  342. getenv("PROC_ROOT"));
  343. else
  344. strncpy(name, "/proc/net/psched", sizeof(name) - 1);
  345. if ((fd = fopen(name, "r"))) {
  346. uint32_t tick, us, nom;
  347. int r = fscanf(fd, "%08x%08x%08x%*08x", &tick, &us, &nom);
  348. if (4 == r && nom == 1000000 && !got_tick)
  349. ticks_per_usec = (double)tick/(double)us;
  350. fclose(fd);
  351. }
  352. }
  353. /**
  354. * Return the value of HZ
  355. */
  356. int nl_get_hz(void)
  357. {
  358. return user_hz;
  359. }
  360. /**
  361. * Convert micro seconds to ticks
  362. * @arg us micro seconds
  363. * @return number of ticks
  364. */
  365. uint32_t nl_us2ticks(uint32_t us)
  366. {
  367. return us * ticks_per_usec;
  368. }
  369. /**
  370. * Convert ticks to micro seconds
  371. * @arg ticks number of ticks
  372. * @return microseconds
  373. */
  374. uint32_t nl_ticks2us(uint32_t ticks)
  375. {
  376. return ticks / ticks_per_usec;
  377. }
  378. long nl_time2int(const char *str)
  379. {
  380. char *p;
  381. long l = strtol(str, &p, 0);
  382. if (p == str)
  383. return -1;
  384. if (*p) {
  385. if (!strcasecmp(p, "min") == 0 || !strcasecmp(p, "m"))
  386. l *= 60;
  387. else if (!strcasecmp(p, "hour") || !strcasecmp(p, "h"))
  388. l *= 60*60;
  389. else if (!strcasecmp(p, "day") || !strcasecmp(p, "d"))
  390. l *= 60*60*24;
  391. else if (strcasecmp(p, "s") != 0)
  392. return -1;
  393. }
  394. return l;
  395. }
  396. /**
  397. * Convert milliseconds to a character string
  398. * @arg msec number of milliseconds
  399. * @arg buf destination buffer
  400. * @arg len buffer length
  401. *
  402. * Converts milliseconds to a character string split up in days, hours,
  403. * minutes, seconds, and milliseconds and stores it in the specified
  404. * destination buffer.
  405. *
  406. * @return The destination buffer.
  407. */
  408. char * nl_msec2str(uint64_t msec, char *buf, size_t len)
  409. {
  410. int i, split[5];
  411. char *units[] = {"d", "h", "m", "s", "msec"};
  412. #define _SPLIT(idx, unit) if ((split[idx] = msec / unit) > 0) msec %= unit
  413. _SPLIT(0, 86400000); /* days */
  414. _SPLIT(1, 3600000); /* hours */
  415. _SPLIT(2, 60000); /* minutes */
  416. _SPLIT(3, 1000); /* seconds */
  417. #undef _SPLIT
  418. split[4] = msec;
  419. memset(buf, 0, len);
  420. for (i = 0; i < ARRAY_SIZE(split); i++) {
  421. if (split[i] > 0) {
  422. char t[64];
  423. snprintf(t, sizeof(t), "%s%d%s",
  424. strlen(buf) ? " " : "", split[i], units[i]);
  425. strncat(buf, t, len - strlen(buf) - 1);
  426. }
  427. }
  428. return buf;
  429. }
  430. /** @} */
  431. /**
  432. * @name Link Layer Protocol Translations
  433. * @{
  434. */
  435. static struct trans_tbl llprotos[] = {
  436. {0, "generic"},
  437. __ADD(ARPHRD_ETHER,ether)
  438. __ADD(ARPHRD_EETHER,eether)
  439. __ADD(ARPHRD_AX25,ax25)
  440. __ADD(ARPHRD_PRONET,pronet)
  441. __ADD(ARPHRD_CHAOS,chaos)
  442. __ADD(ARPHRD_IEEE802,ieee802)
  443. __ADD(ARPHRD_ARCNET,arcnet)
  444. __ADD(ARPHRD_APPLETLK,atalk)
  445. __ADD(ARPHRD_DLCI,dlci)
  446. __ADD(ARPHRD_ATM,atm)
  447. __ADD(ARPHRD_METRICOM,metricom)
  448. __ADD(ARPHRD_IEEE1394,ieee1394)
  449. #ifdef ARPHRD_EUI64
  450. __ADD(ARPHRD_EUI64,eui64)
  451. #endif
  452. __ADD(ARPHRD_INFINIBAND,infiniband)
  453. __ADD(ARPHRD_SLIP,slip)
  454. __ADD(ARPHRD_CSLIP,cslip)
  455. __ADD(ARPHRD_SLIP6,slip6)
  456. __ADD(ARPHRD_CSLIP6,cslip6)
  457. __ADD(ARPHRD_RSRVD,rsrvd)
  458. __ADD(ARPHRD_ADAPT,adapt)
  459. __ADD(ARPHRD_ROSE,rose)
  460. __ADD(ARPHRD_X25,x25)
  461. #ifdef ARPHRD_HWX25
  462. __ADD(ARPHRD_HWX25,hwx25)
  463. #endif
  464. __ADD(ARPHRD_PPP,ppp)
  465. __ADD(ARPHRD_HDLC,hdlc)
  466. __ADD(ARPHRD_LAPB,lapb)
  467. __ADD(ARPHRD_DDCMP,ddcmp)
  468. __ADD(ARPHRD_RAWHDLC,rawhdlc)
  469. __ADD(ARPHRD_TUNNEL,ipip)
  470. __ADD(ARPHRD_TUNNEL6,tunnel6)
  471. __ADD(ARPHRD_FRAD,frad)
  472. __ADD(ARPHRD_SKIP,skip)
  473. __ADD(ARPHRD_LOOPBACK,loopback)
  474. __ADD(ARPHRD_LOCALTLK,localtlk)
  475. __ADD(ARPHRD_FDDI,fddi)
  476. __ADD(ARPHRD_BIF,bif)
  477. __ADD(ARPHRD_SIT,sit)
  478. __ADD(ARPHRD_IPDDP,ip/ddp)
  479. __ADD(ARPHRD_IPGRE,gre)
  480. __ADD(ARPHRD_PIMREG,pimreg)
  481. __ADD(ARPHRD_HIPPI,hippi)
  482. __ADD(ARPHRD_ASH,ash)
  483. __ADD(ARPHRD_ECONET,econet)
  484. __ADD(ARPHRD_IRDA,irda)
  485. __ADD(ARPHRD_FCPP,fcpp)
  486. __ADD(ARPHRD_FCAL,fcal)
  487. __ADD(ARPHRD_FCPL,fcpl)
  488. __ADD(ARPHRD_FCFABRIC,fcfb_0)
  489. __ADD(ARPHRD_FCFABRIC+1,fcfb_1)
  490. __ADD(ARPHRD_FCFABRIC+2,fcfb_2)
  491. __ADD(ARPHRD_FCFABRIC+3,fcfb_3)
  492. __ADD(ARPHRD_FCFABRIC+4,fcfb_4)
  493. __ADD(ARPHRD_FCFABRIC+5,fcfb_5)
  494. __ADD(ARPHRD_FCFABRIC+6,fcfb_6)
  495. __ADD(ARPHRD_FCFABRIC+7,fcfb_7)
  496. __ADD(ARPHRD_FCFABRIC+8,fcfb_8)
  497. __ADD(ARPHRD_FCFABRIC+9,fcfb_9)
  498. __ADD(ARPHRD_FCFABRIC+10,fcfb_10)
  499. __ADD(ARPHRD_FCFABRIC+11,fcfb_11)
  500. __ADD(ARPHRD_FCFABRIC+12,fcfb_12)
  501. __ADD(ARPHRD_IEEE802_TR,tr)
  502. __ADD(ARPHRD_IEEE80211,ieee802.11)
  503. #ifdef ARPHRD_IEEE80211_PRISM
  504. __ADD(ARPHRD_IEEE80211_PRISM, ieee802.11_prism)
  505. #endif
  506. #ifdef ARPHRD_VOID
  507. __ADD(ARPHRD_VOID,void)
  508. #endif
  509. };
  510. char * nl_llproto2str(int llproto, char *buf, size_t len)
  511. {
  512. return __type2str(llproto, buf, len, llprotos, ARRAY_SIZE(llprotos));
  513. }
  514. int nl_str2llproto(const char *name)
  515. {
  516. return __str2type(name, llprotos, ARRAY_SIZE(llprotos));
  517. }
  518. /** @} */
  519. /**
  520. * @name Ethernet Protocol Translations
  521. * @{
  522. */
  523. static struct trans_tbl ether_protos[] = {
  524. __ADD(ETH_P_LOOP,loop)
  525. __ADD(ETH_P_PUP,pup)
  526. __ADD(ETH_P_PUPAT,pupat)
  527. __ADD(ETH_P_IP,ip)
  528. __ADD(ETH_P_X25,x25)
  529. __ADD(ETH_P_ARP,arp)
  530. __ADD(ETH_P_BPQ,bpq)
  531. __ADD(ETH_P_IEEEPUP,ieeepup)
  532. __ADD(ETH_P_IEEEPUPAT,ieeepupat)
  533. __ADD(ETH_P_DEC,dec)
  534. __ADD(ETH_P_DNA_DL,dna_dl)
  535. __ADD(ETH_P_DNA_RC,dna_rc)
  536. __ADD(ETH_P_DNA_RT,dna_rt)
  537. __ADD(ETH_P_LAT,lat)
  538. __ADD(ETH_P_DIAG,diag)
  539. __ADD(ETH_P_CUST,cust)
  540. __ADD(ETH_P_SCA,sca)
  541. __ADD(ETH_P_RARP,rarp)
  542. __ADD(ETH_P_ATALK,atalk)
  543. __ADD(ETH_P_AARP,aarp)
  544. #ifdef ETH_P_8021Q
  545. __ADD(ETH_P_8021Q,802.1q)
  546. #endif
  547. __ADD(ETH_P_IPX,ipx)
  548. __ADD(ETH_P_IPV6,ipv6)
  549. #ifdef ETH_P_WCCP
  550. __ADD(ETH_P_WCCP,wccp)
  551. #endif
  552. __ADD(ETH_P_PPP_DISC,ppp_disc)
  553. __ADD(ETH_P_PPP_SES,ppp_ses)
  554. __ADD(ETH_P_MPLS_UC,mpls_uc)
  555. __ADD(ETH_P_MPLS_MC,mpls_mc)
  556. __ADD(ETH_P_ATMMPOA,atmmpoa)
  557. __ADD(ETH_P_ATMFATE,atmfate)
  558. __ADD(ETH_P_EDP2,edp2)
  559. __ADD(ETH_P_802_3,802.3)
  560. __ADD(ETH_P_AX25,ax25)
  561. __ADD(ETH_P_ALL,all)
  562. __ADD(ETH_P_802_2,802.2)
  563. __ADD(ETH_P_SNAP,snap)
  564. __ADD(ETH_P_DDCMP,ddcmp)
  565. __ADD(ETH_P_WAN_PPP,wan_ppp)
  566. __ADD(ETH_P_PPP_MP,ppp_mp)
  567. __ADD(ETH_P_LOCALTALK,localtalk)
  568. __ADD(ETH_P_PPPTALK,ppptalk)
  569. __ADD(ETH_P_TR_802_2,tr_802.2)
  570. __ADD(ETH_P_MOBITEX,mobitex)
  571. __ADD(ETH_P_CONTROL,control)
  572. __ADD(ETH_P_IRDA,irda)
  573. __ADD(ETH_P_ECONET,econet)
  574. __ADD(ETH_P_HDLC,hdlc)
  575. };
  576. char *nl_ether_proto2str(int eproto, char *buf, size_t len)
  577. {
  578. return __type2str(eproto, buf, len, ether_protos,
  579. ARRAY_SIZE(ether_protos));
  580. }
  581. int nl_str2ether_proto(const char *name)
  582. {
  583. return __str2type(name, ether_protos, ARRAY_SIZE(ether_protos));
  584. }
  585. /** @} */
  586. /**
  587. * @name IP Protocol Translations
  588. * @{
  589. */
  590. char *nl_ip_proto2str(int proto, char *buf, size_t len)
  591. {
  592. struct protoent *p = getprotobynumber(proto);
  593. if (p) {
  594. snprintf(buf, len, "%s", p->p_name);
  595. return buf;
  596. }
  597. snprintf(buf, len, "0x%x", proto);
  598. return buf;
  599. }
  600. int nl_str2ip_proto(const char *name)
  601. {
  602. struct protoent *p = getprotobyname(name);
  603. unsigned long l;
  604. char *end;
  605. if (p)
  606. return p->p_proto;
  607. l = strtoul(name, &end, 0);
  608. if (l == ULONG_MAX || *end != '\0')
  609. return -1;
  610. return (int) l;
  611. }
  612. /** @} */
  613. /**
  614. * @name Dumping Helpers
  615. * @{
  616. */
  617. /**
  618. * Handle a new line while dumping
  619. * @arg params Dumping parameters
  620. * @arg line Number of lines dumped already.
  621. *
  622. * This function must be called before dumping any onto a
  623. * new line. It will ensure proper prefixing as specified
  624. * by the dumping parameters.
  625. *
  626. * @note This function will NOT dump any newlines itself
  627. */
  628. void nl_new_line(struct nl_dump_params *params, int line)
  629. {
  630. if (params->dp_prefix) {
  631. int i;
  632. for (i = 0; i < params->dp_prefix; i++) {
  633. if (params->dp_fd)
  634. fprintf(params->dp_fd, " ");
  635. else if (params->dp_buf)
  636. strncat(params->dp_buf, " ",
  637. params->dp_buflen -
  638. sizeof(params->dp_buf) - 1);
  639. }
  640. }
  641. if (params->dp_nl_cb)
  642. params->dp_nl_cb(params, line);
  643. }
  644. /**
  645. * Dump a formatted character string
  646. * @arg params Dumping parameters
  647. * @arg fmt printf style formatting string
  648. * @arg ... Arguments to formatting string
  649. *
  650. * Dumps a printf style formatting string to the output device
  651. * as specified by the dumping parameters.
  652. */
  653. void nl_dump(struct nl_dump_params *params, const char *fmt, ...)
  654. {
  655. va_list args;
  656. va_start(args, fmt);
  657. __dp_dump(params, fmt, args);
  658. va_end(args);
  659. }
  660. /** @} */
  661. /** @} */