tags:

views:

218

answers:

2

HI

I want to capture the UDP packets by joining to the Multicast group. after the receving the packet i want to check for the TTL value from that UDP packet. How can i achieve this by using python ?

The Sammple code as mentioned below: here rec_port is any port which i had used to bind; eg: 9180 rec_hostname is any multicast group which i had joined;eg:239.2.2.2

 #! /usr/bin/env python
    .........
    ............
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind(("", rec_port))
    mreq = struct.pack("4sl", socket.inet_aton(rec_hostname), socket.INADDR_ANY)
    sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

    total_length = 0
    while True:
            print "Waiting for the packets, if no packets recevied by 10 seconds, than i will exit"
            sock.settimeout(10)
            data , address = sock.recvfrom(2048)
            if len(data) > 1:
                                    total_length=total_length + len(data)
                                    print "Data is:", data
                                    print "Length of data received is:", len(data)
                                    print "Packet recevied from :", address[0]
                                    print "Total Packet size:", total_length
            else:
                    break
    sock.close()
    print "Total Packet size:", total_length
+1  A: 

You probably want to use this python wrapper. If it doesn't satisfy you can wrap libpcap yourself.

In response to unwind: You don't have to act "promiscious" with libpcap, you can inject and snoop valid traffic -- i.e., meant for your NIC.

Edit: Read this tutorial on pcap to figure out how to isolate the ttl field.

Hassan Syed
I'm using python 2.3 version, I guess in this version i cant use the pcap or libpcap module/lib . This is not supported. I'm able to bind to the multicast group. But how can i read the TTL value from the packet recevied?
Sanjay Jain
The TTL value is in the IP header part of the packet, this is generally not available to user space programs so joining the multicast group won't help -- you do need libpcap or something similar. The pycap page says they do support 2.3 though?
Wim
If it really won't work with pycap, another (but messier) option is to call `tcpdump` using `os.popen` and parse its output...
Wim
I'm trying to use python to automate my manual verfication steps used in testing of my component. If at all i want to use the libpcap or something like that than how can i implement these libpcap in my script?
Sanjay Jain
you need to wrap the c code and make it callable from python. see http://docs.python.org/extending/extending.html
Hassan Syed
A: 

in a comment, you told us that you are able to join the multicast group.

now you can use getsockopt(), specify IPPROTO_IP for the level parameter and IP_MULTICAST_TTL for the option, to get the TTL value which is written to outgoing packets.

Adrien Plisson
I think he wants to read the TTL of the *incoming* packets.
Wim
that's why i specified "outgoing packets" in my reply.
Adrien Plisson
Yes i want for incoming packet. i'd this similar problem while sending the data to a multicast group. i had resolved it by the way Plisson have detailed.
Sanjay Jain