[英]How do I read and write raw ip packets from java on a mac?
What would be the easiest way to be able to send and receive raw network packets. 什么是能够发送和接收原始网络数据包的最简单方法。 Do I have to write my own JNI wrapping of some c API, and in that case what API am I looking for? 我是否必须编写自己的一些c API的JNI包装,在这种情况下,我在寻找什么API?
EDIT: I want to be able to do what wireshark does, ie record all incomming packets on an interface, and in addition be able to send back my own created packets. 编辑:我希望能够做Wirehark做的事情,即记录接口上的所有incomming数据包,此外还能够发回我自己创建的数据包。 And I want to do it on a mac. 我想在Mac上做到这一点。
如果你开始想要你需要像数据包嗅探器这样的东西,你会想看看http://netresearch.ics.uci.edu/kfujii/jpcap/doc/ 。
Raw Socket for Java is a request for JDK for a looong long time. Raw Socket for Java是一个长期以来对JDK的请求。 See the request here . 请在此处查看请求。 There's a long discussion there where you can look for workarounds and solutions. 在那里有一个长期的讨论,你可以在那里寻找解决方法和解决方案。 I once needed this for a simple PING operation, but I can't remember how I resolved this. 我曾经需要这个用于简单的PING操作,但我不记得我是如何解决这个问题的。 Sorry :) 抱歉:)
到目前为止,我最好的选择似乎是BPF api并编写一个精简的JNI包装器
You can't access raw sockets from pure Java, so you will need some sort of layer between your Java code and the network interfaces. 您无法从纯Java访问原始套接字,因此您需要在Java代码和网络接口之间使用某种层。
Also note that access to raw sockets is normally only available to "root" processes, since otherwise any user could both a) sniff all traffic, and b) generate spoofed packets. 另请注意,对原始套接字的访问通常仅适用于“根”进程,因为否则任何用户都可以a)嗅探所有流量,以及b)生成欺骗性数据包。
Rather than write your whole program so that it needs to run as "root", you might consider having the packet capture and generation done in a standalone program with some sort of IPC (RMI, named pipe, TCP socket, etc) to exchange the data with your Java app. 您可以考虑在一个独立程序中使用某种IPC(RMI,命名管道,TCP套接字等)来完成数据包捕获和生成,而不是编写整个程序以便它需要以“root”身份运行。您的Java应用程序的数据。
TINI is a java ethernet controller, which may have libraries and classes for directly accessing data from ethernet frames to TCP streams. TINI是一个java以太网控制器,它可能有用于直接访问从以太网帧到TCP流的数据的库和类。 You may be able to find something in there that implements your needed classes. 您可以在那里找到实现所需课程的内容。 If not, there should be pointers or user groups that will give you a head start. 如果没有,应该有指针或用户组,将为您提供一个良好的开端。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.