Wednesday, May 21, 2014

RTP packet crafting using dpkt-1.80 Python library

Installing dpkt-1.80.tar.gz was trickier than i thought. Python is not easy to master. I always learn something new when i start to work on it. The problem with dpkt is the poor documentation right from the start. Installation was quite an issue for me but later in the afternoon, i figured out the correct way to do it. Before that i was trying to import modules from the dpkt directory but something in the code would keep on breaking. Dpkt project suffers from poor documentation of the source code as well and lack of examples to use the library.I had to try out a couple of things before i was able to create the RTP packets. Part of this effort was googling looking for code samples using dpkt. Unfortunately, there were not many.
Anyway, here is the RTP code that i have coded


#trying to send rtp packet
#by Tariq B. Ahmad
# 5/21/2014

import dpkt
import socket

rtp_pkt = dpkt.rtp.RTP()
rtp_pkt.data = '''HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  HelloWorldHelloWorldHelloWorldHelloWorld
                  '''

udp_pkt = dpkt.udp.UDP(sport=11111,dport=11112)
udp_pkt.data = rtp_pkt.data
udp_pkt.ulen += len(udp_pkt.data)

ip_pkt = dpkt.ip.IP(id=0, src='\x01\x02\x03\x04', dst='\xc0\xa8\x7a\x01', p=17) # p=17 for udp
ip_pkt.data = udp_pkt.data
ip_pkt.len += len(ip_pkt.data)

s = socket.socket(socket.AF_INET, socket.SOCK_RAW, dpkt.ip.IP_PROTO_UDP)
s.connect(('192.168.122.1', 11112))
#print str(ip_pkt)
sent = s.send(str(ip_pkt))

print 'sent %d bytes' % sent



Here is the output when you run the script. You have to be the root user in Linux to use socket functionality.

sent 1554 bytes


I tried to capture this using Wireshark by listening to the interface on 192.168.122.1 but Wireshark could not capture this. I have googled this and other people have reported the same issue.

If i have some time tomorrow, i shall use Scapy library to see if i can do the same or better with Scapy.
Cheerz and tomorrow is MyHDL day



I also received my Xilinx Atlys board today !!!

3 comments:

  1. Have you tried using wireshark to capture packets between two working RTP systems? (IE Two gstreamer pipelines would be a good test.)

    ReplyDelete
  2. Tim i did something shown below at the start of the week when i was learning Gstreamer and trying to create RTP Packets. I interacted with Carl for this

    One one shell i execute

    $gst-launch-1.0 -v videotestsrc is-live=true pattern=18 ! rtpvrawpay ! udpsink host=127.0.0.1

    On the other shell i execute

    gst-launch-1.0 -v udpsrc uri=udp://127.0.0.1:5004 caps="application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)RAW, sampling=(string)YCbCr-4:2:0, depth=(string)8, width=(string)320, height=(string)240, colorimetry=(string)BT601-5, payload=(int)96, ssrc=(uint)3768970899, timestamp-offset=(uint)1463426976, seqnum-offset=(uint)28808" ! rtpvrawdepay ! videoconvert ! videoscale ! autovideosink


    Wireshark is already running in the background. It captures RTP packets but for some reason, it shows RTP packets as UDP packets.

    Secondly, it is great that Gstreamer can generate RTP Packets. But i am more interested in saving these packets as a text file so i can use it as RTP packet traffic in HDL code. From my interaction with Carl, i have learnt this cannot be done. I also googled a lot and my day1 and day2 show the myriad of tools that i explored to generate and save RTP traffic. Ultimately, this blog (Day3) show some code that generates RTP traffic which can be saved. But Wireshark is not able to capture this.

    I will deal with this as time goes on

    ReplyDelete
    Replies
    1. RTP is *application layer* and hence are encapsulated inside UDP packets which is *transport layer* (the fact you are using udpsrc should have given you a hint). Wireshark is showing you the correct result. You will need to know how to use Wireshark as it will be a very useful tool in capturing the raw data the FPGA is outputting (and hence what is wrong with it).

      Delete