xref: /freebsd/cddl/usr.sbin/dwatch/libexec/udp (revision e2340276fc734a1f0bd0d2cf16fcfba7936c9462)
1# -*- tab-width: 4 -*- ;; Emacs
2# vi: set filetype=sh tabstop=8 shiftwidth=8 noexpandtab :: Vi/ViM
3############################################################ IDENT(1)
4#
5# $Title: dwatch(8) module for dtrace_udp(4) $
6# $Copyright: 2014-2018 Devin Teske. All rights reserved. $
7#
8############################################################ DESCRIPTION
9#
10# Display local/remote UDP addresses/ports and bytes sent/received for UDP I/O
11#
12############################################################ PROBE
13
14case "$PROFILE" in
15udp) : ${PROBE:=udp:::send, udp:::receive} ;;
16  *) : ${PROBE:=udp:::${PROFILE#udp-}}
17esac
18
19############################################################ ACTIONS
20
21exec 9<<EOF
22this string	flow;
23this string	local;
24this string	remote;
25this u_char	local6;
26this u_char	recv;
27this u_char	remote6;
28this uint16_t	length;
29this uint16_t	lport;
30this uint16_t	rport;
31
32$PROBE /* probe ID $ID */
33{${TRACE:+
34	printf("<$ID>");
35}
36	/*
37	 * dtrace_udp(4)
38	 */
39	this->recv = probename == "receive" ? 1 : 0;
40	this->flow = this->recv ? "<-" : "->";
41
42	/*
43	 * ipinfo_t *
44	 */
45	this->local  = this->recv ? args[2]->ip_daddr : args[2]->ip_saddr;
46	this->remote = this->recv ? args[2]->ip_saddr : args[2]->ip_daddr;
47
48	/*
49	 * udpinfo_t *
50	 */
51	this->length = (uint16_t)args[4]->udp_length;
52	this->lport  = this->recv ? args[4]->udp_dport : args[4]->udp_sport;
53	this->rport  = this->recv ? args[4]->udp_sport : args[4]->udp_dport;
54
55	/*
56	 * IPv6 support
57	 */
58	this->local6 = strstr(this->local, ":") != NULL ? 1 : 0;
59	this->remote6 = strstr(this->remote, ":") != NULL ? 1 : 0;
60	this->local = strjoin(strjoin(this->local6 ? "[" : "",
61		this->local), this->local6 ? "]" : "");
62	this->remote = strjoin(strjoin(this->remote6 ? "[" : "",
63		this->remote), this->remote6 ? "]" : "");
64}
65EOF
66ACTIONS=$( cat <&9 )
67ID=$(( $ID + 1 ))
68
69############################################################ EVENT DETAILS
70
71if [ ! "$CUSTOM_DETAILS" ]; then
72exec 9<<EOF
73	/*
74	 * Print network I/O details
75	 */
76	printf("%s:%u %s %s:%u %d byte%s",
77		this->local, this->lport,
78		this->flow,
79		this->remote, this->rport,
80		this->length,
81		this->length == 1 ? "" : "s");
82EOF
83EVENT_DETAILS=$( cat <&9 )
84fi
85
86################################################################################
87# END
88################################################################################
89