Erkin Alp Güney
Erkin Alp Güney

Reputation: 216

Invalid port in python sockets

I am getting error while trying to send bare ICMP message using following snippet:

Windows says this port is invalid for given IP range.

def send_ping(source_ip,target_ip,data_function=construct_icmp_echo):
    fail = 0
    skt_send = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_ICMP)
    skt_recv = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_ICMP)
    (src_binary,) = struct.unpack (">L",socket.inet_aton(source_ip))
    (tgt_binary,) = struct.unpack (">L",socket.inet_aton(source_ip))

    skt_send.setsockopt(socket.SOL_IP, socket.IP_TTL, 16)
    ipheader = struct.pack("BBHHHBBHLL",0x54,0xdc,48,50,8,16,1,0,src_binary,tgt_binary)
    cksum = icmpcksum(ipheader)
    ipheader = ipheader = struct.pack("BBHHHBBHLL",0x54,0xdc,48,50,8,16,1,cksum,src_binary,tgt_binary)
    skt_send.sendto(data_function(),(target_ip,22433))
    skt_recv.bind((target_ip,22433))
    skt_recv.settimeout(5)
    thetime = datetime.datetime.now()
    try: 
        s = datetime.datetime.now()
        while time.time - s < 60:
            a = skt_recv.recvfrom(1024)[0]
            hdr_reply = a[20:]
            icmp_type = a[20]
            if icmp_type == 0 :
                print ("got a ping")
                fl = True
                return target_ip
            if fl: break
    except socket.timeout as e:
        raise e
    return

here: skt.recv_bind((target_ip,22433)) IP address I have fed are B- and C-class IP addresses.

I get this traceback:

File "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensio
ns\Microsoft\Python Tools for Visual Studio\2.2\visualstudio_py_launcher.py", li
ne 78, in <module>
    vspd.debug(filename, port_num, debug_id, debug_options, run_as)
  File "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensio
ns\Microsoft\Python Tools for Visual Studio\2.2\visualstudio_py_debugger.py", li
ne 2465, in debug
    exec_file(file, globals_obj)
  File "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensio
ns\Microsoft\Python Tools for Visual Studio\2.2\visualstudio_py_util.py", line 1
11, in exec_file
    exec_code(code, file, global_variables)
  File "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensio
ns\Microsoft\Python Tools for Visual Studio\2.2\visualstudio_py_util.py", line 8
7, in exec_code
    exec(code_obj, global_variables)
  File "C:\Users\user\Documents\Visual Studio 2015\Projects\python-ipscan\python
-ipscan\python_ipscan.py", line 86, in <module>
    if __name__=="__main__": main ()
  File "C:\Users\user\Documents\Visual Studio 2015\Projects\python-ipscan\python
-ipscan\python_ipscan.py", line 83, in main
    send_ping (myaddr,"""155.223.197.1""",construct_icmp_echo)
  File "C:\Users\user\Documents\Visual Studio 2015\Projects\python-ipscan\python
-ipscan\python_ipscan.py", line 70, in send_ping
    traceback.print_stack()
Traceback (most recent call last):
  File "C:\Users\user\Documents\Visual Studio 2015\Projects\python-ipscan\python
-ipscan\python_ipscan.py", line 86, in <module>
    if __name__=="__main__": main ()
  File "C:\Users\user\Documents\Visual Studio 2015\Projects\python-ipscan\python
-ipscan\python_ipscan.py", line 84, in main
    traceback.extract_tb(sys.exc_info())
  File "C:\Program Files\Python34\lib\traceback.py", line 106, in extract_tb
    return list(_extract_tb_iter(tb, limit=limit))
  File "C:\Program Files\Python34\lib\traceback.py", line 59, in _extract_tb_or_
stack_iter
    f, lineno, next_item = extractor(curr)
AttributeError: 'tuple' object has no attribute 'tb_frame'

Exception when not handled by user code: OSError was unhandled by user code Message: [WinError 10049] Invalid request for given address

Upvotes: 1

Views: 655

Answers (1)

holdenweb
holdenweb

Reputation: 37033

You are trying to send to port 22433 before the receiving socket has bound the port. The skt_send.sendto() call is therefore trying to send to a non-existent endpoint - at the time of that call there's nothing listening on port 22433. Try putting the skt_recv.bind() call before it.

It would be helpful to see the traceback given by the interpreter, as with only a vague description of the error we have to guess what went wrong.

Upvotes: 2

Related Questions