14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#!/usr/sbin/dtrace -s
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * tcpsnoop - snoop TCP network packets by process.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Written using DTrace tcp Provider.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * This analyses TCP network packets and prints the responsible PID plus
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * standard details such as IP address and port. This captures traffic
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * from existing and newly created TCP connections. It can help identify
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * which processes are causing TCP traffic.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * SEE ALSO: snoop -rS
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * CDDL HEADER START
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * The contents of this file are subject to the terms of the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Common Development and Distribution License (the "License").
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * You may not use this file except in compliance with the License.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * or http://www.opensolaris.org/os/licensing.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * See the License for the specific language governing permissions
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * and limitations under the License.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * When distributing Covered Code, include this CDDL HEADER in each
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If applicable, add the following below this CDDL HEADER, with the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * fields enclosed by brackets "[]" replaced with your own identifying
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * information: Portions Copyright [yyyy] [name of copyright owner]
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * CDDL HEADER END
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Portions Copyright 2010 Brendan Gregg
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#pragma D option quiet
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#pragma D option switchrate=10hz
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdtrace:::BEGIN
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync printf("%6s %6s %15s:%-5s %15s:%-5s %6s %s\n",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "TIME", "PID", "LADDR", "PORT", "RADDR", "PORT", "BYTES", "FLAGS");
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsynctcp:::send
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync this->length = args[2]->ip_plength - args[4]->tcp_offset;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync printf("%6d %6d %15s:%-5d -> %15s:%-5d %6d (",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync timestamp/1000, args[1]->cs_pid, args[2]->ip_saddr,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync args[4]->tcp_sport, args[2]->ip_daddr, args[4]->tcp_dport,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync this->length);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsynctcp:::receive
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync this->length = args[2]->ip_plength - args[4]->tcp_offset;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync printf("%6d %6d %15s:%-5d <- %15s:%-5d %6d (",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync timestamp/1000, args[1]->cs_pid, args[2]->ip_daddr,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync args[4]->tcp_dport, args[2]->ip_saddr, args[4]->tcp_sport,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync this->length);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsynctcp:::send,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsynctcp:::receive
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync printf("%s", args[4]->tcp_flags & TH_FIN ? "FIN|" : "");
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync printf("%s", args[4]->tcp_flags & TH_SYN ? "SYN|" : "");
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync printf("%s", args[4]->tcp_flags & TH_RST ? "RST|" : "");
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync printf("%s", args[4]->tcp_flags & TH_PUSH ? "PUSH|" : "");
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync printf("%s", args[4]->tcp_flags & TH_ACK ? "ACK|" : "");
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync printf("%s", args[4]->tcp_flags & TH_URG ? "URG|" : "");
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync printf("%s", args[4]->tcp_flags & TH_ECE ? "ECE|" : "");
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync printf("%s", args[4]->tcp_flags & TH_CWR ? "CWR|" : "");
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync printf("%s", args[4]->tcp_flags == 0 ? "null " : "");
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync printf("\b)\n");
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}