hello everyone.
i have a problem in sending the packet that i have overwritten.
the code is below
pcap.dispatch(1000, new PcapHandler() { public void nextPacket(String user, long seconds, int useconds, int caplen, int len, ByteBuffer buffer) { buffer.put(getByteMAC(MAC_NAS));//newdes buffer.put(getByteMAC(MAC_LAPTOP_WIRE));//new source for(int jj=0 ;jj<5;jj++){ System.out.println("jj is "+jj); pcap.sendPacket(buffer.array()); } } }
the console output is like this
jj is 0 (there is no loop in there)
but when i didnt send the packet, the loop worked well
jj is 0, jj is 1 ....
note: i have checked that i could send using pcap.sendPacket(buffer.array()), with arp packet that i've created and overwritten it.
anyone know why i couldnt send the packet?
Best Regards
Timmy
The problem is with the statement buffer.array() on line 10. This ByteBuffer is not array backed, it is a direct ByteBuffer. Before you can use array() you should check if its array backed with ByteBuffer.hasArray() first.
Anyway, to fix it just pass in the entire byte buffer to Pcap.sendPacket()
Cheers,
mark...
Mark thanks for the reply.
Actually,i have used Bytebuffer to send the packet,
so line 10 is pcap.sendPacket(buffer);
but wireshark couldnt capture the packet, so i think it is not beeing sended.
i dont know why??
rite now, i want to make program to capture packets from two devices and make my computer as a mediator. i just change MAC adresss from the packet and send it again. and it couldnt work.
please help me
Best Regards
Timmy
Did you reset the position property? When you use relative puts, it advances the position of the buffer. Pcap.sendPacket will send the portion of the buffer from position to limit.
Other than that it should be sent, even if its mangled or not from the start of where it should. Unless it too small to send (with the incorrect start position) and its being rejected by your ethernet card.
Thanks Mark for the reply.
i have resetted the buffer. and the problem as i mentioned you for the 1st time occured again.it seems the program didnt loop again.
btw. is it bad to write code like this
while(true){
pcap.dispatch(1, new PcapHandler() {
code here ....
}
}
coz, i want to loop it continually
Best Regards
Timmy
Use Pcap.loop instead and do this:
pcap.loop(Pcap.LOOP_INFINATE, ....)
Can you post your entire code where you open the device for capture and sending and your loop.
Thanks Mark
this is my entire code, to capture and sending
i still dont know why?help me
Best Regards
Timmy
public class CapAndSend implements MacAndIp{
public final static int MODE_ETH = 1; //0 is wireless, 1 is wir
/**
* @param args
*/
@SuppressWarnings("deprecation")
public static void main(String[] args) {
List alldevs = new ArrayList(); // Will be filled with NICs
StringBuilder errbuf = new StringBuilder(); // For any error msgs
/********************************************
* First get a list of devices on this system
********************************************/
int r = Pcap.findAllDevs(alldevs, errbuf);
if (r == Pcap.NOT_OK || alldevs.isEmpty()) {
System.err.printf("Can't read list of devices, error is %s", errbuf
.toString());
return;
}
System.out.println("Network devices found:");
int i = 0;
for (PcapIf device : alldevs) {
System.out.printf("#%d: %s [%s]\n", i++, device.getName(), device
.getDescription());
}
PcapIf device = alldevs.get(MODE_ETH); // We know we have atleast 1 device
System.out.printf("\nChoosing '%s' on your behalf:\n", device
.getDescription());
/***************************************
* Second we open up the selected device
***************************************/
int snaplen = 64 * 1024; // Capture all packets, no trucation
int flags = Pcap.MODE_PROMISCUOUS; // capture all packets
int timeout = 10 * 1000; // 10 seconds in millis
final Pcap pcap = Pcap
.openLive(device.getName(), snaplen, flags, timeout, errbuf);
if (pcap == null) {
System.err.printf("Error while opening device for capture: "
+ errbuf.toString());
return;
}
/**********************************************************************
* Third we create a packet hander which will be dispatched to from the
* libpcap loop.
**********************************************************************/
// while(true){
pcap.loop(Pcap.LOOP_INFINATE, new PcapHandler() {
String srcMAC = "";
String desMAC = "";
String srcIP = "";
String desIP = "";
private Ethernet eth = new Ethernet();
private Ip4 ip = new Ip4();
private Icmp icmp = new Icmp();
public void nextPacket(String user, long seconds, int useconds,
int caplen, int len, ByteBuffer buffer) {
System.out.println("packet comes ");
JScanner scanner = JScanner.getThreadLocal(); // Our scanner
PcapPacket packet = new PcapPacket(JMemory.Type.POINTER);
packet.peer(buffer);
scanner.scan(packet, Ethernet.ID); // Assume DLT is ethernet
if (packet.hasHeader(Ethernet.ID) ){
eth = packet.getHeader(eth);
srcMAC = byte2strMac(eth.source());
desMAC = byte2strMac(eth.destination());
if (packet.hasHeader(ip)) {
try{
srcIP = java.net.InetAddress.getByAddress(ip.source()).getHostAddress();
desIP = java.net.InetAddress.getByAddress(ip.destination()).getHostAddress();;
if((srcIP.indexOf("192.168.12") == 0)&&(desIP.indexOf("192.168.12") == 0)){
// if(packet.hasHeader(icmp)){
// System.out.println("OK");
// }
if(desIP.equals(IP_NAS)){
System.out.println("--------------------------before start-----------------------------");
System.out.println("mac source is "+srcMAC);
System.out.println("mac destination is "+desMAC);
System.out.println("IP source is "+srcIP);
System.out.println("IP destination is "+desIP);
System.out.println("size is "+packet.size());
System.out.println("---------------------------before end------------------------------");
System.out.println("position is "+buffer.position());
buffer.put(getByteMAC(MAC_NAS)); //new des
buffer.put(getByteMAC(MAC_LAPTOP_WIRE));//new source
buffer.reset();
if(packet.hasHeader(icmp)){
System.out.println("OK after buffer being changed");
}
for(int jj=0 ;jj<5;jj++){
System.out.println("buffer capacity is "+buffer.capacity());
// pcap.breakloop();
// ByteBuffer b = ByteBuffer.allocate(caplen);
pcap.sendPacket(buffer);
System.out.println("jj is 2");
pcap.sendPacket(buffer);
System.out.println("jj is 3");
// ByteBuffer buffer2 = ByteBuffer.wrap(getARPPacket(getByteMAC(MAC_LAPTOP_WIRE), getByteMAC(MAC_NAS), java.net.InetAddress.getByName("192.168.12.140").getAddress(), java.net.InetAddress.getByName("192.168.12.4").getAddress()));
// buffer.put(capPacket.getByteMAC(MAC_NAS)); //new des
// buffer.put(capPacket.getByteMAC(MAC_LAPTOP_WIRE));//new source
// buffer.position(28);
// buffer.put(java.net.InetAddress.getByName("192.168.12.100").getAddress());
// buffer.clear();
// pcap.sendPacket(buffer.array());//send as ByteBuffer
pcap.sendPacket(getARPPacket(getByteMAC(MAC_LAPTOP_WIRE), getByteMAC(MAC_NAS), java.net.InetAddress.getByName("192.168.12.140").getAddress(), java.net.InetAddress.getByName("192.168.12.4").getAddress()));
}
if (packet.hasHeader(Ethernet.ID) ){
eth = packet.getHeader(eth);
srcMAC = byte2strMac(eth.source());
desMAC = byte2strMac(eth.destination());
// System.out.println("mac destination is "+desMAC);
}
if (packet.hasHeader(ip)) {
try{
srcIP = java.net.InetAddress.getByAddress(ip.source()).getHostAddress();
desIP = java.net.InetAddress.getByAddress(ip.destination()).getHostAddress();
System.out.println("--------------------------after start-----------------------------");
System.out.println("mac source is "+srcMAC);
System.out.println("mac destination is "+desMAC);
System.out.println("IP source is "+srcIP);
System.out.println("IP destination is "+desIP);
System.out.println("size is "+packet.size());
System.out.println("--------------------------after end-----------------------------");
}
catch(Exception e){
System.out.println("no IP");
}
}
}
if(desIP.equals(IP_DESKTOP)){
// System.out.println("go to desktop");
// packet.setByteArray(0, getByteMAC(MAC_DESKTOP));
// packet.setByteArray(6, getByteMAC(MAC_LAPTOP_WIRE));
// pcap.sendPacket((JBuffer)packet);
System.out.println("position is "+buffer.position());
buffer.put(getByteMAC(MAC_DESKTOP)); //new des
buffer.put(getByteMAC(MAC_LAPTOP_WIRE));//new source
for(int jj=0 ;jj<5;jj++){
// pcap.sendPacket(buffer.array());
}
}
}
}
catch(Exception e){
System.out.println("no IP");
}
}
}
}
}, "Timmy");
/*
* Last thing to do is close the pcap handle
*/
pcap.close();
// }
}
Noticed you are not checking for error code. If you can check for errors from sendPacket and report what the error is, that might provide a clue as to what's libpcap not liking about your packet:
if (pcap.sendPacket(...) != Pcap.OK) {
System.out.println(pcap.getErr()); // Message directly from libpcap
}
Try that and lets see if we get anything useful out of the error message.
Thanks Mark for the reply
it still didnt work. even in this below code, the error didnt come, and the output just displayed message "packet comes" for one time. and seems the program is looping infinitely.
Best Regards
Timmy
pcap.loop(Pcap.LOOP_INFINATE, new PcapHandler
public void nextPacket(String user, long seconds, int useconds,
int caplen, int len, ByteBuffer buffer) {
System.out.println("packet comes ");
buffer.put(getByteMAC(MAC_NAS)); //new des
buffer.put(getByteMAC(MAC_LAPTOP_WIRE));//new source
buffer.reset();
if (pcap.sendPacket(buffer) != Pcap.OK) {
System.out.println(pcap.getErr()); // Message directly from libpcap
}
else {System.out.println("it works");} }
}
}, "Timmy");
/*
* Last thing to do is close the pcap handle
*/
pcap.close();
}