Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'none:': 0.05; 'socket': 0.05; 'sys': 0.05; '"__main__":': 0.07; '[],': 0.07; '__name__': 0.07; 'assign': 0.07; 'dst': 0.07; 'subject:skip:c 10': 0.07; 'try:': 0.07; 'subject:How': 0.09; 'dst)': 0.09; 'subject:set': 0.09; 'subject:using': 0.09; 'timeout': 0.09; 'timeout)': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; '"g"': 0.16; '1))': 0.16; 'data)': 0.16; 'header:': 0.16; 'indexerror:': 0.16; 'subject:type': 0.16; 'true:': 0.16; 'bytes': 0.17; 'import': 0.21; 'struct': 0.22; 'example': 0.23; 'work.': 0.23; "i've": 0.23; 'header': 0.24; 'id:': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'select': 0.26; 'cc:addr:gmail.com': 0.27; 'cc:2**2': 0.27; "doesn't": 0.28; 'skip:( 20': 0.28; '0.5': 0.29; 'src': 0.29; 'source': 0.29; 'error': 0.30; 'seconds': 0.30; 'code': 0.31; 'could': 0.32; 'print': 0.32; 'skip:s 30': 0.33; 'anyone': 0.33; 'code:': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'protocol': 0.35; 'sequence': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'except': 0.36; 'but': 0.36; 'cc:no real name:2**1': 0.36; 'does': 0.37; 'received:209': 0.37; 'received:209.85.216': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'build': 0.39; 'skip:" 10': 0.40; 'skip:u 10': 0.60; 'worth': 0.63; 'here': 0.65; 'address,': 0.79; 'received:209.85.216.184': 0.84 Newsgroups: comp.lang.python Date: Tue, 21 Aug 2012 10:00:28 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=85.226.61.187; posting-account=nArglgoAAAD1vIdoa_OKjmLHmRbfn9Os References: <4edc5ccb-9121-47b0-8ad4-2bf106735532@googlegroups.com> <50323dc7$0$6841$e4fe514c@news2.news.xs4all.nl> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 85.226.61.187 MIME-Version: 1.0 Subject: Re: How to set the socket type and the protocol of a socket using create_connection? From: Guillaume Comte To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org, Guillaume Comte , d@davea.name X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345568436 news.xs4all.nl 6919 [2001:888:2000:d::a6]:57735 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27574 Unfortunatly, my_socket.bind((src_addr, 1)) doesn't work. I get the error message: "socket.error: [Errno 49] Can't assign requested address"... I've tried to change the protocol to IPPROTO_RAW. Here is a simple example of the code: import socket import os import struct import time import select ICMP_ECHO_REQUEST = 8 PACKET_SIZE = 64 # Bytes TIMEOUT = 0.5 # Seconds def do_one(src_addr, dest_addr): my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW) if src_addr != None: src_addr = socket.gethostbyname(src_addr) my_socket.bind((src_addr, 1)) my_id = os.getpid() & 0xFFFF print "id: " + str(my_id) print "sending..." send_one(dest_addr, my_socket, my_id) print "receiving..." id = receive_one(my_socket) if id == None: print "nothing received !" else: print "received id: " + str(id) my_socket.close() def checksum(source_string): ... def send_one(addr, my_socket, id): # Header: type (8), code (8), checksum (16), id (16), sequence number (16) cs = 0 header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, cs, socket.htons(id), 0) data = PACKET_SIZE * "G" cs = checksum(header + data) header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(cs), socket.htons(id), 0) packet = header + data my_socket.sendto(packet, (socket.gethostbyname(addr), 1)) def receive_one(my_socket): while True: what_ready = select.select([my_socket], [], [], TIMEOUT) if what_ready[0] == []: return None received_packet = my_socket.recvfrom(1024)[0] header = received_packet[20:28] id = struct.unpack("bbHHh", header)[3] return socket.ntohs(id) if __name__ == "__main__": import sys dst = sys.argv[1] print "dst: " + dst try: src = sys.argv[2] print "src: " + src except IndexError: src = None do_one(src, dst) But when I try to set a source address, I still get the same error message... Does anyone know how I could build the IP header (I don't even know if it can be the solution but it's worth a try...)?