sourceroute.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* include inet_srcrt_init */
  2. #include "unp.h"
  3. #include <netinet/in_systm.h>
  4. #include <netinet/ip.h>
  5. static u_char *optr; /* pointer into options being formed */
  6. static u_char *lenptr; /* pointer to length byte in SRR option */
  7. static int ocnt; /* count of # addresses */
  8. u_char *
  9. inet_srcrt_init(int type)
  10. {
  11. optr = Malloc(44); /* NOP, code, len, ptr, up to 10 addresses */
  12. bzero(optr, 44); /* guarantees EOLs at end */
  13. ocnt = 0;
  14. *optr++ = IPOPT_NOP; /* NOP for alignment */
  15. *optr++ = type ? IPOPT_SSRR : IPOPT_LSRR;
  16. lenptr = optr++; /* we fill in length later */
  17. *optr++ = 4; /* offset to first address */
  18. return(optr - 4); /* pointer for setsockopt() */
  19. }
  20. /* end inet_srcrt_init */
  21. /* include inet_srcrt_add */
  22. int
  23. inet_srcrt_add(char *hostptr)
  24. {
  25. int len;
  26. struct addrinfo *ai;
  27. struct sockaddr_in *sin;
  28. if (ocnt > 9)
  29. err_quit("too many source routes with: %s", hostptr);
  30. ai = Host_serv(hostptr, NULL, AF_INET, 0);
  31. sin = (struct sockaddr_in *) ai->ai_addr;
  32. memcpy(optr, &sin->sin_addr, sizeof(struct in_addr));
  33. freeaddrinfo(ai);
  34. optr += sizeof(struct in_addr);
  35. ocnt++;
  36. len = 3 + (ocnt * sizeof(struct in_addr));
  37. *lenptr = len;
  38. return(len + 1); /* size for setsockopt() */
  39. }
  40. /* end inet_srcrt_add */
  41. /* include inet_srcrt_print */
  42. void
  43. inet_srcrt_print(u_char *ptr, int len)
  44. {
  45. u_char c;
  46. char str[INET_ADDRSTRLEN];
  47. struct in_addr hop1;
  48. memcpy(&hop1, ptr, sizeof(struct in_addr));
  49. ptr += sizeof(struct in_addr);
  50. while ( (c = *ptr++) == IPOPT_NOP)
  51. ; /* skip any leading NOPs */
  52. if (c == IPOPT_LSRR)
  53. printf("received LSRR: ");
  54. else if (c == IPOPT_SSRR)
  55. printf("received SSRR: ");
  56. else {
  57. printf("received option type %d\n", c);
  58. return;
  59. }
  60. printf("%s ", Inet_ntop(AF_INET, &hop1, str, sizeof(str)));
  61. len = *ptr++ - sizeof(struct in_addr); /* subtract dest IP addr */
  62. ptr++; /* skip over pointer */
  63. while (len > 0) {
  64. printf("%s ", Inet_ntop(AF_INET, ptr, str, sizeof(str)));
  65. ptr += sizeof(struct in_addr);
  66. len -= sizeof(struct in_addr);
  67. }
  68. printf("\n");
  69. }
  70. /* end inet_srcrt_print */