[转]Getting a Packet Trace
src:https://developer.apple.com/library/mac/qa/qa1176/_index.html
Getting a Packet Trace
Q: I'm trying to debug a network problem. How do I get a packet trace?
A: This depends on your platform:
There are a number of programs for OS X that let you gather and analyze packet traces. See OS X Programs for details.
iOS does not support packet tracing directly. However, if you connect your iOS device to a Mac via USB, you can use an OS X packet trace program to gather and analyze traces using the remote virtual interface feature. See iOS Packet Tracing for details.
Finally, Packet Trace Notes offers some hints and tips that you might find useful when dealing with packet traces.
OS X Programs
OS X supports a wide range of packet trace programs, as described in the following sections.
Charles HTTP Proxy
Charles is an HTTP proxy that allows you to view all of the HTTP and HTTPS traffic between your machine and the Internet.
CPA
Cocoa Packet Analyzer is a native OS X implementation of a network protocol analyzer and packet sniffer.
Debookee
Debookee is a OS X application which allows you to see what your devices are sending over the network.
FrameSeer
FrameSeer is an inexpensive network packet capture application for OS X.
HTTP Scoop
HTTP Scoop is an HTTP protocol analyzer for OS X. It reconstructs complete HTTP conversations (rather than just showing the packets that make them up) and presents them in a user-friendly manner.
IPNetMonitorX
IPNetMonitorX is a network troubleshooting toolkit for debugging Internet service problems and optimizing performance.
tcpdump
This command line tool is built in to all versions of OS X, and is also available on many other Unix platforms. For a quick summary of how to use tcpdump, see Getting Started With tcpdump.
tcpflow
If you're debugging a high-level protocol, it's nice to see the various TCP connections as streams of data rather than individual packets. The tcpflow tool can do that for you. If you've not used tcpflowbefore, there's a quick introduction in Getting Started With tcpflow.
The tcpflow tool is not built-in to OS X, but you can get it in a variety of ways.
tcptrace
tcptrace is an open source tool for analyzing the TCP connections in a packet trace.
Wireshark
Wireshark is an open source packet analyzer that has been ported to OS X. It requires X11.
Wireless Diagnostics
Wireless Diagnostics is an application built in to OS X that lets you capture a Wi-Fi level packet trace. Such traces contain more information than a standard packet trace (for example, they show Wi-Fi's link-layer retransmissions).
You can find Wireless Diagnostics in the /System/Library/CoreServices directory; on later systems it might be in the Applications subdirectory within that directory. On OS X 10.7 the application was called Wi-Fi Diagnostics.
See Wi-Fi Capture for more information about using this tool.
iOS Packet Tracing
iOS does not support packet tracing directly. However, if you're developing for iOS you can take a packet trace of your app in a number of different ways:
If the problem you're trying to debug occurs on Wi-Fi, you can put your iOS device on a test Wi-Fi network. See Wi-Fi Capture for details.
If your app uses HTTP, you can configure your iOS device to use a debugging HTTP proxy (such as Charles HTTP Proxy).
In iOS 5 and later you can use the remote virtual interface facility.
Remote Virtual Interface
iOS 5 added a remote virtual interface (RVI) facility that lets you use OS X packet trace programs to capture traces from an iOS device. The basic strategy is:
Connect your iOS device to your Mac via USB.
Set up an RVI for that device. This creates a virtual network interface on your Mac that represents the iOS device's networking stack.
Run your OS X packet trace program, and point it at the RVI created in the previous step.
To set up an RVI, you should run the rvictl tool as shown below.
$ # First get the current list of interfaces. |
$ ifconfig -l |
lo0 gif0 stf0 en0 en1 p2p0 fw0 ppp0 utun0 |
$ # Then run the tool with the UDID of the device. |
$ rvictl -s 74bd53c647548234ddcef0ee3abee616005051ed |
Starting device 74bd53c647548234ddcef0ee3abee616005051ed [SUCCEEDED] |
$ # Get the list of interfaces again, and you can see the new virtual |
$ # network interface, rvi0, added by the previous command. |
$ ifconfig -l |
lo0 gif0 stf0 en0 en1 p2p0 fw0 ppp0 utun0 rvi0 |
Now that you know the name of the RVI, you can point your packet trace tool at it. For example, he's how you might run tcpdump to take a packet trace from the RVI.
$ sudo tcpdump -i rvi0 -n |
tcpdump: WARNING: rvi0: That device doesn't support promiscuous mode |
(BIOCPROMISC: Operation not supported on socket) |
tcpdump: WARNING: rvi0: no IPv4 address assigned |
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode |
listening on rvi0, link-type RAW (Raw IP), capture size 65535 bytes |
… |
When you're done you can remove the RVI with the following command.
$ rvictl -x 74bd53c647548234ddcef0ee3abee616005051ed |
Stopping device 74bd53c647548234ddcef0ee3abee616005051ed [SUCCEEDED] |
Remote Virtual Interface Troubleshooting
This section explains how to resolve some common issues with RVI.
If your Mac doesn't have the rvictl tool, make sure you install Xcode 4.2 or later.
If the device is running iOS 7 or later, you must use the RVI support installed by Xcode 5.0 or later.
The RVI support installed by Xcode 5.0 works best on OS X 10.9 and later. Specifically, if you run tcpdump on 10.8.x and see the message "unknown ip 0", you will need to update to 10.9 to access those packets via RVI.
If rvictl fails with the message:
bootstrap_look_up(): 1102 |
make sure that that the com.apple.rpmuxd launchd job is loaded correctly. The following command should print information about the job.
$ sudo launchctl list com.apple.rpmuxd |
{
|
"Label" = "com.apple.rpmuxd"; |
… |
}; |
If it fails, it could be because the job is unloaded. You can force it to load with the following command.
$ sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.rpmuxd.plist |
Packet Trace Notes
Getting Started With tcpdump
To get started with tcpdump, try the following command.
sudo tcpdump -i en0 -w DumpFile.pcap |
The elements of this command line are:
The
sudocommand causestcpdumpto run with privileges, which is necessary in order to capture network traffic.The
-i en0option tellstcpdumpto capture packets on the first Ethernet interface. By default,tcpdumpwill use the first non-loopback interface it can find (usuallyen0). To specify a different interface, just changeen0to the BSD name of that interface. For example, the AirPort interface is typicallyen1.To get a list of network interfaces and their user-visible names, run the networksetup tool with the
-listallhardwareportsargument.The
-w DumpFile.pcapparameter tellstcpdumpto write the packets to a file calledDumpFile.pcap.
If you're running on a system prior to OS X 10.6 you should also supply the -s 0 option, which tells tcpdump to capture the full packet rather than just the first 68 bytes. This option is the default on OS X 10.6 and later.
In response to this command, tcpdump will begin to capture packets and put them in the DumpFile.pcap file. When you want to stop capturing, interrupt tcpdump by typing ^C. You can then display the contents of the packets as text using the following command.
tcpdump -n -e -x -vvv -r DumpFile.pcap |
New elements of the command line are:
The
-noption means that addresses are not converted to domain names, which speeds things up considerably.The
-eoption causestcpdumpto display the link-level header for each packet.The
-xoption causes the contents of the packet to also be displayed in hex.The
-vvvoption makestcpdump's output as verbose as possible.By specifying
-r DumpFile.pcapoption you telltcpdumpto read packets from the fileDumpFile.pcaprather than from a network interface. Note that you don't need privileges to do this, so runningtcpdumpusingsudois not required.
You can also combine these steps, as shown below, but if you do this you don't get a high-fidelity record of the packets that you captured.
sudo tcpdump -i en0 -n -e -x -vvv |
You can learn about tcpdump from the online manual and from the book TCP/IP Illustrated, Volume 1: The Protocols, W. Richard Stevens, Addison-Wesley, 1994, ISBN 0-201-63346-9. That book is also an excellent introduction to TCP/IP protocols in general.
Getting Started With tcpflow
The tcpflow command makes it much easier to debug high-level protocols. For example, if you're debugging an HTTP client, you can run the following command.
sudo tcpflow -i en0 port 80 |
tcpflow will create a bunch of files in the current directory, each of which contains the reassembled contents of a single TCP stream. So, if you run tcpflow as shown above and then fetch the URLhttp://apple.com, you can see how the HTTP redirect works.
$ sudo tcpflow -i en0 port 80 |
tcpflow[953]: listening on en0 |
^C |
tcpflow[953]: terminating |
$ ls -lh |
total 16 |
-rw-r--r-- 1 root quinn [...] 010.000.040.015.50232-017.149.160.049.00080 |
-rw-r--r-- 1 root quinn [...] 017.149.160.049.00080-010.000.040.015.50232 |
$ # This is the request. |
$ cat 010.000.040.015.50232-017.149.160.049.00080 |
GET / HTTP/1.1 |
User-Agent: curl/7.19.4 (universal-apple-darwin10.0) libcurl/7.19.4 OpenSSL/0.9.8k zlib/1.2.3 |
Host: apple.com |
Accept: */* |
$ # And this is the response. |
$ cat 017.149.160.049.00080-010.000.040.015.50232 |
HTTP/1.1 302 Object Moved |
Location: http://www.apple.com/ |
Content-Type: text/html |
Cache-Control: private |
Connection: close |
<head><body> This object may be found <a HREF="http://www.apple.com/">here</a> </body> |
Loopback Issues
Some packet trace programs have problems with packets being transferred to or from the trace machine (the machine running the packet trace program). To avoid these problems, separate your trace machine from the machines whose network traffic you're tracing.
As an example of this, on OS X tcpdump may display the TCP checksum of packets sent by the trace machine as bad. This is because of TCP checksum offloading; packets sent by the trace machine are captured before being handed to the network adapter, so they don't have the TCP checksum set correctly. This is not a fatal problem; if the bad checksums bother you, you can turn off the check by passing the -K option to tcpdump.
Dropped Packets
If you capture all the bytes of each packet, it's very easy to overrun the kernel's packet capture buffer. The symptoms of this overrun are that your packet trace program will report that it dropped packets.
In the case of tcpdump, it prints a summary of how many packets were captured, filtered, and dropped when you stop the capture. For example:
$ sudo tcpdump -i en0 -w DumpFile.pcap |
tcpdump: listening on en0, link-type EN10MB (Ethernet), capture size 65535 bytes |
^C |
94 packets captured |
177 packets received by filter |
0 packets dropped by kernel |
If the dropped count is non-zero, you need to increase the packet capture buffer size by passing the -B option to tcpdump, as discussed earlier.
Switches And Hubs
If you use a separate trace machine, you have to make sure that the trace machine can see the packets of interest. There are two ways to do this:
Use a hub rather than a switch — These days it is hard to find real hubs. Most 10/100 hubs are actually switches in disguise. However, it is possible to find a 10/100 hub that only switches between the different speed segments (for example, the SMC-EZ58xxDS range).
Enable port mirroring — On most advanced switches it is possible to configure the switch so that all traffic is mirrored to a specific port. To learn more about this, consult the documentation for your switch.
Capture Hints From The Wireshark Wiki
The Wireshark wiki has some really useful information about how to setup your packet tracing environment.
The Ethernet Capture Setup Document contains good background information for setting up your network for monitoring.
The Hub Reference Document contains information on various types of hubs.
The Switch Reference Document contains information on analysis features, such as port mirroring, found on various models of switches, including links to online documentation for those switches.
Wi-Fi Capture
Capturing packets on Wi-Fi can be tricky because conversations between one Wi-Fi client and the access point are not necessarily visible to other Wi-Fi clients. There are two easy ways to ensure that you see the relevant Wi-Fi traffic:
bridge mode — If your Wi-Fi access point supports bridge mode (for example, all Apple base stations do), you can bridge the Wi-Fi on to an Ethernet and then use standard Ethernet techniques to capture a packet trace. You may not be able to see Wi-Fi to Wi-Fi traffic, but in many situations that's not a problem.
Internet Sharing — If you enable Internet Sharing on your Mac, and have your Wi-Fi clients join the shared network, you can run your packet trace program on the Mac and see all the Wi-Fi traffic. If you target the Mac's Wi-Fi interface, you will see all traffic including Wi-Fi to Wi-Fi traffic. If you target the Ethernet interface, you will only see traffic entering or leaving the Wi-Fi network.
Alternatively, you can use the Wireless Diagnostics application to take a Wi-Fi level packet trace. This shows all traffic visible to your Mac, including low-level Wi-Fi traffic that's not visible with other tools. When using this tool, keep in mind the following:
After running the application, you can access the packet trace feature by choosing Utilities from the Window menu and then selecting the Frame Capture tab.
Your Mac can't use the Wi-Fi interface for normal network traffic while tracing.
You must choose a channel to trace on. It simplifies things if you configure your access point to use a specific channel rather than let it choose one automatically.
If the Wi-Fi network has a password, Wi-Fi encryption will make it much harder to examine the trace. To get around this, either temporarily turn off the Wi-Fi password on your network or use a separate test network that has no password.
Submitting A Trace With A Bug Report
If you're submitting a bug report related to networking, you should consider submitting a packet trace along with your bug report. The Bug Reporting page has details on how to provide additional information with your bug report.
Submitting A Trace To DTS
If you send a packet trace to DTS, please include the following:
The system type and OS version of the trace machine.
The name and version of the program you used to capture the packet trace.
If you've used a program whose native file format is the libpcap file format (these include
tcpdump, Wireshark, and various others), you can send us the packet trace file in that format. Otherwise, please include a copy of the packet trace in both its native format and, if that native format isn't text, a text export of the trace as well. That way we're guaranteed to be able to read your packet trace.For each relevant machine shown in the trace, please describe the following:
the machine's role in the network conversation
the system type and OS version
the machine's IP address
the machine's hardware address (also known as the Ethernet address or MAC address)
[转]Getting a Packet Trace的更多相关文章
- Linux 网络子系统
今天记录一下Linux网络子系统相关的东西. 因为感觉对这一块还是有一个很大的空白,这件事情太可怕了. 摘抄多份博客进行总结一下Linux网络子系统的相关东西. 一. Linux网络子系统体系结构 L ...
- [工具]Mac平台开发几个网络抓包工具(sniffer)
Cocoa Packet Analyzer http://www.tastycocoabytes.com/cpa/ Cocoa Packet Analyzer is a native Mac OS X ...
- ios抓包官方文档
OS X Programs OS X supports a wide range of packet trace programs, as described in the following sec ...
- Mini2440 DM9000 驱动分析(一)
Mini2440 DM9000 驱动分析(一) 硬件特性 Mini2440开发板上DM9000的电气连接和Mach-mini2440.c文件的关系: PW_RST 连接到复位按键,复位按键按下,低电平 ...
- wireshark filter manualpage
NAME wireshark-filter - Wireshark filter syntax and reference SYNOPSIS wireshark [other options] [ - ...
- Linux 网络子系统之网络协议接口层(一)
Linux 网络设备驱动之网络协议接口层介绍. 网络协议接口层最主要的功能是给上层协议提供透明的数据包发送和接收接口. 当上层ARP或IP需要发送数据包时,它将调用网络协议接口层的dev_queue_ ...
- NetScaler + Wireshark = A Perfect Combination!
NetScaler + Wireshark = A Perfect Combination! https://www.citrix.com/blogs/2014/05/03/netscaler-wir ...
- Mac OS X上使用Wireshark抓包
Wireshark针对UNIX Like系统的GUI发行版界面采用的是X Window(1987年更改X版本到X11).Mac OS X在Mountain Lion之后放弃X11,取而代之的是开源的X ...
- 《linux设备驱动开发详解》笔记——14 linux网络设备驱动
14.1 网络设备驱动结构 网络协议接口层:硬件无关,标准收发函数dev_queue_xmit()和netif_rx(); 注意,netif_rx是将接收到的数据给上层,有时也在驱动收到数据以后调用 ...
随机推荐
- 大数据系列修炼-Scala课程05
Scala多重继承.构造器的执行顺序.AOP实现 多重继承的trait实现:Scala中接口可以继承具体的类,trait接口可以实现多重继承,并且某个类也可以继承特定的类,在继承后面可以混入,接口的实 ...
- hdu Color the ball
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 树状数组的 update的应用,逆序更新 代码: #include <stdio.h&g ...
- javascript的语法作用域你真的懂了吗
原文:javascript的语法作用域你真的懂了吗 有段时间没有更新了,思绪一下子有点转不过来.正应了一句古话“一天不读书,无人看得出:一周不读书,开始会爆粗:一月不读书,智商输给猪.”.再加上周五晚 ...
- Python开发环境的搭建(win7)
一个.安装和配置Python 事实上,在开发python最好ubuntu环境.简单和易于扩展每个package. 在谈到如何win7建筑物Python开发环境. 因为python十字-platform ...
- Java数据结构与算法(4) - ch04队列(Queue和PriorityQ)
队列: 先进先出(FIFO). 优先级队列: 在优先级队列中,数据项按照关键字的值有序,关键字最小的数据项总在对头,数据项插入的时候会按照顺序插入到合适的位置以确保队列的顺序,从后往前将小于插入项的数 ...
- Unix命令操作
基本命令 [ man 查看 ]--万能命令 1.ls 列出文件 (-al) 2.cd 转换目录 3.mkdir 建立新目录 4.cp 拷贝文件 (-R) 5.rm 删除文件 (-rf) 6.mv 移动 ...
- 常用批处理命令总结3之Find和FindStr
原文:常用批处理命令总结3之Find和FindStr find 作用:从文件中收索字符串 格式:find 参数 "字符串" 路径\文件名 参数: /V 显示所有未包含指定字符串的行 ...
- 菜鸟进阶Android Touch事件传递(四)
尊重他人劳动成果,转载请说明出处:http://blog.csdn.net/bingospunky/article/details/44343477 在该系列文章第四篇.我准备介绍一下viewpage ...
- animation渐进实现点点点等待效果实例页面
CSS代码: .ani_dot { font-family: simsun; } :root .ani_dot { display: inline-block; width: 1.5em; verti ...
- Android分析应用程序的构建过程
为了方便Android应用开发要求我们Androidproject编制和包装有了更深入的了解,例如,我们知道这是做什么的每一步,境和工具.输入和输出是什么.等等. 在前文<命令行下Android ...