title: UDP Packet Analysis
body: <p>So, for a specific task I am asked to search and print the </p>
  
  <blockquote>
    <p>request and response (for protocol fields)</p>
  </blockquote>
  
  <p>for captured <strong>UDP DNS packets</strong>.</p>
  
  <p>Now I am very new to network programming and I tried to achieve this using the <a href="http://jnetpcap.com" rel="nofollow">JNetPcap Library</a>. However, please note that the question is <strong>not</strong> about <a href="http://jnetpcap.com" rel="nofollow">JNetPcap</a> functions.</p>
  
  <p>The sample outputs I am getting from my program (which can be modified) is viz.</p>
  
  <pre><code>Got UDP Packet
  Name of packet :Udp
  The Source :14786
  The Destination :53
  The Legnth of packet :8
  
  Got UDP Packet
  Name of packet :Udp
  The Source :58186
  The Destination :53
  The Legnth of packet :8
  </code></pre>
  
  <p>So <strong>what is the</strong> <code>request and response</code> <strong>here</strong>? If my output is incomplete, what fields do I need to print to get the request and response?</p>
  
  <p><strong>Edit:</strong> I am not sniffing any specific application. I am asked to sniff the network (as in, run in your software in our machine and that's all) as a whole and find the request and response.
  Thanks.</p>
  
  <p><strong>Extra Information That May Be Relevant:</strong></p>
  
  <p>So to filter UDP DNS packets I am using this code (<strong>the code may not be required for the question</strong>, but I am including it just in case I have done something wrong):</p>
  
  <pre><code>PcapBpfProgram program = new PcapBpfProgram();
  String expression = "udp dst port 53";
  int optimize = 0;         // 0 = false
  int netmask = 0xFFFFFF00; // 255.255.255.0
  if (pcap.compile(program, expression, optimize, netmask) != Pcap.OK) {
                  System.err.println(pcap.getErr());
                  return;
  }
  </code></pre>
  
  <p>Here I analyze the first 10 packets viz.</p>
  
  <pre><code>// Receive packet from the loop
  PcapPacketHandler&lt;String&gt; jpacketHandler = packet_handler();
  // capture first 10 packages
  pcap.loop(10, jpacketHandler, "");
  </code></pre>
  
  <p>And this is how I print the sample output given above:</p>
  
  <pre><code>public static PcapPacketHandler&lt;String&gt; packet_handler() {
  PcapPacketHandler&lt;String&gt; jpacketHandler = new PcapPacketHandler&lt;String&gt;() {
  Udp udp = new Udp();
  
  @Override
  public void nextPacket(PcapPacket packet, String user) {
  
  // Another check to determine if packet is UDP.
  if (!packet.hasHeader(udp)) {
     // not UDP? skip
  }
  // Yet another check to determine if packet is UDP.
  if (packet.hasHeader(udp)) {
      System.out.println("Got UDP Packet");
      System.out.println("Name of packet :" + packet.getHeader(udp).getName());
      System.out.println("The Source :" + packet.getHeader(udp).source());
      System.out.println("The Destination :" + packet.getHeader(udp).destination());
      System.out.println("The Check sum :" + packet.getHeader(udp).checksum());
      System.out.println("The Legnth of packet :" + packet.getHeader(udp).getHeaderLength());
  </code></pre>