Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #27731

Re: How to set the socket type and the protocol of a socket using create_connection?

From Grant Edwards <invalid@invalid.invalid>
Newsgroups comp.lang.python
Subject Re: How to set the socket type and the protocol of a socket using create_connection?
Date 2012-08-23 13:53 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <k15cl7$820$1@reader1.panix.com> (permalink)
References <cbafe578-0a1a-4d0b-82a5-54c9f84fcd67@googlegroups.com> <32176aa4-4b9d-44a6-8729-958e3c251955@googlegroups.com> <mailman.3681.1345663132.4697.python-list@python.org>

Show all headers | View raw


On 2012-08-22, Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:
> On Wed, 22 Aug 2012 01:43:19 -0700 (PDT), Guillaume Comte
><guillaume.comte10@gmail.com> declaimed the following in
> gmane.comp.python.general:
>
>> I've managed to build the IP header. I've put the source and destination addresses in this header but it doesn't change the real source address...
>
> 	For all I know (I've done very little network programming, and that
> was years ago using plain TCP and UDP -- worse, on a VMS system so it
> wasn't the "UNIX style" socket interface), your network stack may still
> be overriding the packet at some lower level and inserting the IP
> associated with the interface the packet went out on...

I've only been intermittently following this thread, but back when I
added Python's raw packet support for Unix, the socket module was a
_very_ thin wrapper for the underlying OS network socket API.  The
behavior of various types of sockets was defined entirely by the
underlying OS.

So, if you're trying to do something obscure (which it seems you are),
asking people who know how to do it in C on the relevent OS is
probably the best approach.

Below are examples of sending and receiving a completely raw packet on
Linux (where you provide _all_ the bytes: the MAC addreses, the
Ethernet type, everything).

------------------------------send------------------------------
#!/usr/bin/python
import sys,os,socket,struct
from optparse import OptionParser

p = OptionParser()
p.add_option("-i","--interface",dest="interface",metavar="<name>",type='str',default="eth0")
options,args = p.parse_args()

if len(args) != 1:
    sys.stderr.write("you must provide a destination MAC address\n")
    sys.exit(1)
    
def toHex(s):
    return " ".join([("%02x" % ord(c)) for c in s])

ethProto = 0x5678
dstMacStr = args[0]
dstMacAddr = "".join(map(chr,[int(x,16) for x in dstMacStr.split(":")]))

s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, ethProto)
s.bind((options.interface,ethProto))

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

ethHeader = struct.pack("!6s6sh",dstMacAddr,srcMacAddr,ethProto)
packet = ethHeader + "some ASCII data here"

sys.stdout.write("tx: %s\n" % toHex(packet))
s.send(packet)
s.close()
-----------------------------------------------------------------

------------------------------recv------------------------------
#!/usr/bin/python
import sys,os,socket,struct
from optparse import OptionParser

p = OptionParser()
p.add_option("-i","--interface",dest="interface",metavar="<name>",type='str',default="eth0")
options,args = p.parse_args()

if len(args) != 0:
    sys.stderr.write("no arguments accepted\n")
    sys.exit(1)
    
def toHex(s):
    return " ".join([("%02x" % ord(c)) for c in s])

ethProto = 0x5678

s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, ethProto)
s.bind((options.interface,ethProto))

packet = s.recv(4096)
sys.stdout.write("rx: %s\n" % toHex(packet))
s.close()
-----------------------------------------------------------------

-- 
Grant Edwards               grant.b.edwards        Yow! I'm changing the
                                  at               CHANNEL ... But all I get
                              gmail.com            is commercials for "RONCO
                                                   MIRACLE BAMBOO STEAMERS"!

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-20 02:14 -0700
  Re: How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-20 05:36 -0700
    Re: How to set the socket type and the protocol of a socket using create_connection? Hans Mulder <hansmu@xs4all.nl> - 2012-08-20 15:38 +0200
      Re: How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-20 08:04 -0700
        Re: How to set the socket type and the protocol of a socket using create_connection? Dave Angel <d@davea.name> - 2012-08-20 13:27 -0400
          Re: How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-21 10:00 -0700
          Re: How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-21 10:00 -0700
            Re: How to set the socket type and the protocol of a socket using create_connection? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-21 15:28 -0400
            Re: How to set the socket type and the protocol of a socket using create_connection? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-21 22:10 -0400
              Re: How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-22 00:29 -0700
              Re: How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-22 00:29 -0700
                Re: How to set the socket type and the protocol of a socket using create_connection? Hans Mulder <hansmu@xs4all.nl> - 2012-08-22 11:03 +0200
                Re: How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-22 02:21 -0700
      Re: How to set the socket type and the protocol of a socket using create_connection? Chris Angelico <rosuav@gmail.com> - 2012-08-21 01:38 +1000
  Re: How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-22 01:43 -0700
    Re: How to set the socket type and the protocol of a socket using create_connection? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-22 15:18 -0400
      Re: How to set the socket type and the protocol of a socket using create_connection? Grant Edwards <invalid@invalid.invalid> - 2012-08-23 13:53 +0000

csiph-web