General discussion

Locked

Help packet sending with Jpcap(netresearch)

By aniketsharma190 ·
I am a beginner in Jpcap.
My problem is as follows:-
i am trying to send a TCP packet but unable to receive it.
The code for sending Tcp packet is as follows.
I am working on Windows XP.
hoping for reply.


import jpcap.*;
import jpcap.packet.*;
import java.net.InetAddress;
import java.lang.Object;


class SendPacket
{

public static void main(String args[]) throws Exception
{
int index=0;

//Obtain the list of network interfaces
NetworkInterface[] devices = JpcapCaptor.getDeviceList();

//open a network interface to send a packet to
JpcapSender sender=JpcapSender.openDevice(devices[index]);

//create a TCP packet with specified port numbers, flags, and other parameters
TCPPacket p=new TCPPacket(12,34,56,78,false,false,false,false,true,true,true,true,10,10);

//specify IPv4 header parameters

p.setIPv4Parameter(0,false,false,false,0,false,false,false,0,65,128,IPPacket.IPPROTO_TCP,InetAddress.getByName("192.168.15.16"),InetAddress.getByName("192.168.15.15"));

//set the data field of the packet
p.data=("ABCD").getBytes();

//create an Ethernet packet (frame)
EthernetPacket ether=new EthernetPacket();
//set frame type as IP
ether.frametype=EthernetPacket.ETHERTYPE_IP;
//set source and destination MAC addresses



String strdst = new String("00:21:9B:D7:98:54");
ether.dst_mac = strdst.getBytes();
String strsrc = new String("00:0C:F1:DE:00:D0");
ether.src_mac = strsrc.getBytes();

//set the datalink frame of the packet p as ether
p.datalink=ether;

//send the packet p
for(int k=0;k>=0;k++)
{
sender.sendPacket(p);
}
sender.close();
}

}

This conversation is currently closed to new comments.

5 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Couple of thoughts

by robo_dev In reply to Help packet sending with ...

When you say you are 'trying to send but cannot receive'

Do you mean that your send program is not working or your receive program is not working?

Your example seems to indicate that your send program is not right.

Do other WinPcap programs, such as WireShark or Nmap work on your PC?
Do other JPCAP programs work on your PC?
Do you have admin privileges on the PC?

Collapse -

Reply to Couple of thoughts

by aniketsharma190 In reply to Couple of thoughts

The problem is when i execute the send program on my pc, packets don't reach the destination ,instead they are captured on the same machine(i am using following receive program to capture).I have also verified the IP/MAC address.

I have administrator privileges on my pc.
and i tried wireshark it works on my pc properly.
Jpcap also works.


import jpcap.*;
import jpcap.packet.*;
import java.io.*;
import java.net.InetAddress;

class PacketLengthPrinter implements PacketReceiver
{
static int i=0;
public void receivePacket(Packet pack)
{

// ports
/*
TCPPacket p=(TCPPacket)pack;
int src_port=p.src_port;
int dst_port=p.dst_port;
System.out.println("Source port:t"+src_port);
System.out.println("Dest PORT:t"+dst_port);

// ip addr
InetAddress src_ip=p.src_ip;
InetAddress dst_ip=p.dst_ip;
System.out.println("Source ip:t"+src_ip);
System.out.println("Dest IP:t"+dst_ip);

//identification
int ident=p.ident;
System.out.println("IDENTIFICATION::"+ident);

i++;
*/
System.out.println(""+pack);
}
}

class ReceivePacket
{
public static void main(String args[])
{
NetworkInterface devices[]=new NetworkInterface[10];
devices=JpcapCaptor.getDeviceList();
byte macaddr[]=new byte[10];

int i;
System.out.println("NAMES:");
for(i=0;i<devices.length;i++)
{
System.out.println((i+1)+"."+devices.name);
}
System.out.println("DATA LINK NAMES:");
for(i=0;i<devices.length;i++)
{
System.out.println((i+1)+"."+devices.datalink_name);
}
System.out.println("MAC ADDRESSES:");
for(i=0;i<devices.length;i++)
{
System.out.println((i+1)+".");
for(byte b:devices.mac_address)
{
System.out.print(Integer.toHexString(b&0xff)+":");
}
}
System.out.println("IP ADDRESS:SUBNET MASK");
for(i=0;i<devices.length;i++)
{
System.out.println((i+1)+".");
for(NetworkInterfaceAddress a:devices.addresses)
{
System.out.print("\t"+a.address+":"+a.subnet);
}
}
System.out.println();
int index;
index=1;
JpcapCaptor captor;
try
{
captor=JpcapCaptor.openDevice(devices[index],65535,false,20);
System.out.println("NAME:t"+devices[index].name+"\topened successfuly");

//print out its MAC address
System.out.print(" MAC address:");
for (byte b : devices[index].mac_address)
System.out.print(Integer.toHexString(b&0xff) + ":");
System.out.println();


System.out.println("USING LOOPPACKET:n\n");
//captor.setFilter("ip and tcp", true);
captor.loopPacket(-1,new PacketLengthPrinter());
captor.close();
}
catch(IOException ioe)
{
System.out.println("Unable to open network Device::"+devices[index].name);
}
}
}

Collapse -

Problem solved

by aniketsharma190 In reply to Couple of thoughts

Problem solved.
Problem was with receiving code !

Collapse -

receive function problem

by shruthimelu In reply to Problem solved

i've the same problem..Could you please specify what changes you made in the receiving code to make it work?

Collapse -

Help needed in receiving packets

by noonz In reply to Help packet sending with ...

Hi everyone,

I am trying to capture packets on a windows XP machine installed on a VMware workstation.

Following is the code that works fine on my windows 7 laptop but does not print anything on the current machine. I do not get any compilation or run time errors. I would really apprecicate any help. thanks


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import jpcap.packet.*;
import jpcap.*;

class RecvHttp implements PacketReceiver {
jpcap.NetworkInterface[] devices = JpcapCaptor.getDeviceList();
JpcapSender sender;
int index=0;
public void rcvPacket(String[] args) throws java.io.IOException {

if (args.length < 1) {
System.out.println("Usage: java RecvHttp <device index (e.g., 0, 1..)>");
for (int i = 0; i < devices.length; i++)
System.out.println(i + ":" + devices.name + "("
+ devices.description + ")");
System.exit(0);
}
index = Integer.parseInt(args[0]);
JpcapCaptor jpcap = JpcapCaptor.openDevice(devices[index], 1028, false, 1);
jpcap.loopPacket(-1, new RecvHttp());
System.out.println("Test");

}

@Override
public void receivePacket(Packet p) {
if (p instanceof TCPPacket){
TCPPacket tcp = (TCPPacket) p;
if (tcp.src_port==80 || tcp.dst_port == 80)
try {

System.out.println(new String(tcp.data,"ASCII"));


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}



}
public static void main(String args[]) throws IOException{
RecvHttp rcv = new RecvHttp();
rcv.rcvPacket(args);
}

}

Back to Software Forum
5 total posts (Page 1 of 1)  

Related Discussions

Related Forums