How to python raw socket ????

L

Leon

thanks Michel

I find socket module use socket.SOCK_RAW

but... the setsockopt(level,option,value) fuction,I don't know how to use it

like 'level' .....because I want to modify ip header....

^_^ libpap I can try it.....thanks you very much

"Michel Claveau - abstraction méta-galactique non triviale en fuite perpétuelle."
 
M

Michel Claveau - abstraction méta-galactique non t

Hi !

......because I want to modify ip header

Wouaaah ! You want to make a proxy/router, a firewall, or a backdoor in
Python ?
It is not my normal work ; but, by curiosity, I would like to read examples
of code.

*sorry for my bad english*

@-salutations
 
G

Grant Edwards

It's not good form to rely on the subject of the message to ask
your question. Repeat the question in the body of the message:

said:
have any relate module ??

Not that I'm aware of.
or write example ??

Here's a raw socket demo that sends and receives raw Ethernet
packets using protocol number 0x55aa on interface eth1. The
file is available at <ftp://ftp.visi.com/users/grante/python/rawDemo.py>.

Last time I looked, the socket module supported raw sockets
under Linux only.

---------------------------------8<---------------------------------
#!/usr/bin/python

import sys
import string
import struct
from socket import *

proto = 0x55aa

s = socket(AF_PACKET, SOCK_RAW, proto)
s.bind(("eth1",proto))

ifName,ifProto,pktType,hwType,hwAddr = s.getsockname()

srcAddr = hwAddr
dstAddr = "\x01\x02\x03\x04\x05\x06"
ethData = "here is some data for an ethernet packet"

txFrame = struct.pack("!6s6sh",dstAddr,srcAddr,proto) + ethData

print "Tx[%d]: "%len(ethData) + string.join(["%02x"%ord(b) for b in ethData]," ")

s.send(txFrame)

rxFrame = s.recv(2048)

dstAddr,srcAddr,proto = struct.unpack("!6s6sh",rxFrame[:14])
ethData = rxFrame[14:]

print "Rx[%d]: "%len(ethData) + string.join(["%02x"%ord(b) for b in ethData]," ")

s.close()
---------------------------------8<---------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,686
Latest member
scamivo

Latest Threads

Top