Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

print_ifindex.c 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2001-2018 The strace developers.
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: LGPL-2.1-or-later
  6. */
  7. #include "defs.h"
  8. #include <net/if.h>
  9. #ifdef HAVE_IF_INDEXTONAME
  10. # include "xstring.h"
  11. # define INI_PFX "if_nametoindex(\""
  12. # define INI_SFX "\")"
  13. # define IFNAME_QUOTED_SZ (sizeof(IFNAMSIZ) * 4 + 3)
  14. const char *
  15. get_ifname(const unsigned int ifindex)
  16. {
  17. static char name_quoted_buf[IFNAME_QUOTED_SZ];
  18. char name_buf[IFNAMSIZ];
  19. if (!if_indextoname(ifindex, name_buf))
  20. return NULL;
  21. if (string_quote(name_buf, name_quoted_buf, sizeof(name_buf),
  22. QUOTE_0_TERMINATED | QUOTE_OMIT_LEADING_TRAILING_QUOTES,
  23. NULL))
  24. return NULL;
  25. return name_quoted_buf;
  26. }
  27. static const char *
  28. sprint_ifname(const unsigned int ifindex)
  29. {
  30. static char res[IFNAME_QUOTED_SZ + sizeof(INI_PFX INI_SFX)];
  31. const char *name_quoted = get_ifname(ifindex);
  32. if (!name_quoted)
  33. return NULL;
  34. xsprintf(res, INI_PFX "%s" INI_SFX, name_quoted);
  35. return res;
  36. }
  37. #else /* !HAVE_IF_INDEXTONAME */
  38. const char *get_ifname(const unsigned int ifindex) { return NULL; }
  39. #endif /* HAVE_IF_INDEXTONAME */
  40. void
  41. print_ifindex(const unsigned int ifindex)
  42. {
  43. #ifdef HAVE_IF_INDEXTONAME
  44. print_xlat_ex(ifindex, sprint_ifname(ifindex), XLAT_STYLE_FMT_U);
  45. #else
  46. tprintf("%u", ifindex);
  47. #endif
  48. }