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