xref: /openbsd/usr.sbin/traceroute/traceroute.h (revision ce7279d8)
1 /*	$OpenBSD: traceroute.h,v 1.8 2024/05/21 05:00:48 jsg Exp $	*/
2 /*	$NetBSD: traceroute.c,v 1.10 1995/05/21 15:50:45 mycroft Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*-
34  * Copyright (c) 1990, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * This code is derived from software contributed to Berkeley by
38  * Van Jacobson.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  */
64 
65 #include <sys/types.h>
66 
67 #include <netinet/ip_var.h>
68 #include <netmpls/mpls.h>
69 
70 #define ICMP_CODE 0;
71 
72 #define DUMMY_PORT 10010
73 
74 #define MAX_LSRR		((MAX_IPOPTLEN - 4) / 4)
75 
76 #define MPLS_LABEL(m)		((m & MPLS_LABEL_MASK) >> MPLS_LABEL_OFFSET)
77 #define MPLS_EXP(m)		((m & MPLS_EXP_MASK) >> MPLS_EXP_OFFSET)
78 
79 /*
80  * Format of the data in a (udp) probe packet.
81  */
82 struct packetdata {
83 	u_char seq;		/* sequence number of this packet */
84 	u_int8_t ttl;		/* ttl packet left with */
85 	u_char pad[2];
86 	u_int32_t sec;		/* time packet left */
87 	u_int32_t usec;
88 } __packed;
89 
90 struct tr_conf {
91 	int		 first_ttl;	/* Set the first TTL or hop limit */
92 	u_char		 proto;		/* IP payload protocol to use */
93 	u_int8_t	 max_ttl;	/* Set the maximum TTL / hop limit */
94 	int		 nprobes;
95 	u_int16_t 	 port;		/* start udp dest port */
96 	int		 waittime;	/* time to wait for a response */
97 	int		 Aflag;		/* lookup ASN */
98 	int		 dflag;		/* set SO_DEBUG */
99 	int		 dump;
100 	int		 lsrr;		/* Loose Source Record Route */
101 	struct in_addr	 gateway[MAX_LSRR + 1];
102 	int		 lsrrlen;
103 	int		 protoset;
104 	int		 ttl_flag;	/* display ttl on returned packet */
105 	int		 nflag;		/* print addresses numerically */
106 	char		*source;
107 	int		 sump;
108 	int		 tos;
109 	int		 tflag;		/* tos value was set */
110 	int		 xflag;		/* show ICMP extension header */
111 	int		 verbose;
112 	u_int		 rtableid;	/* Set the routing table */
113 	u_short		 ident;
114 	int		 expected_responses;
115 };
116 
117 struct tr_result {
118 	int		 seq;
119 	int		 row;
120 	int		 dup;
121 	int		 timeout;
122 	uint8_t		 ttl;
123 	uint8_t		 resp_ttl;
124 	char		 hbuf[NI_MAXHOST];
125 	char		 inetname[NI_MAXHOST];
126 	char		*asn;
127 	char		*exthdr;
128 	char		 to[NI_MAXHOST];
129 	int		 cc;
130 	struct timeval	 t1;
131 	struct timeval	 t2;
132 	char		 icmp_code[sizeof("!<255>")];
133 	char		 tos[sizeof(" (TOS=255!)")];
134 	int		 got_there;
135 	int		 unreachable;
136 	int		 inetname_done;
137 	int		 asn_done;
138 };
139 
140 extern int		*waiting_ttls;
141 extern int32_t		 sec_perturb;
142 extern int32_t		 usec_perturb;
143 
144 extern u_char		 packet[512];
145 extern u_char		*outpacket;	/* last inbound (icmp) packet */
146 
147 void		 send_probe(struct tr_conf *, int, u_int8_t, struct sockaddr *);
148 int		 packet_ok(struct tr_conf *, int, struct msghdr *, int, int *);
149 void		 icmp_code(int, int, int *, int *, struct tr_result *);
150 void		 check_tos(struct ip*, int *, struct tr_result *);
151 int		 map_tos(char *, int *);
152 void		 print(struct tr_conf *, struct sockaddr *, int, const char *,
153 		     struct tr_result *);
154 void		 print_exthdr(u_char *, int, struct tr_result *);
155 void		 gettime(struct timeval *);
156 
157 void		 catchup_result_rows(struct tr_result *, struct tr_conf *);
158 
159 extern int		 sndsock;  /* send (udp) socket file descriptor */
160 
161 extern int		 rcvhlim;
162 extern struct in6_pktinfo *rcvpktinfo;
163 
164 extern int		 datalen;  /* How much data */
165 
166 extern char		*hostname;
167 
168 extern u_int16_t	 srcport;
169 
170 extern char *__progname;
171