python中recvfrom的问题

时间:2014-09-24 21:45:34

标签: python sockets networking tcp ip

我正在运行安装了apache服务器的虚拟机,并且我正在尝试解决如何在python中创建IP和TCP标头的问题。我遇到的问题是,虽然我知道(来自tcpdump)我的代码实际上在tcp标头中创建了带有SYN的正确标头并将它们发送到服务器,服务器以ACK回答我的代码并没有&# 39;似乎收到了那条消息。

此外,在ack之后,似乎有第二条消息从客户端计算机发送,其中窗口大小为0,我的代码不是为了发送此消息而设计的...无论如何,如果那是超级rambly(它是)你可以原谅我,它已经很晚了,我感到沮丧。无论如何,这里是代码

import socket, sys
from struct import *

# checksum functions needed for calculation checksum
def checksum(msg):
    s = 0

    # loop taking 2 characters at a time
    for i in range(0, len(msg), 2):
        w = ord(msg[i]) + (ord(msg[i+1]) << 8 )
        s = s + w

    s = (s>>16) + (s & 0xffff);
    s = s + (s >> 16);

    #complement and mask to 4 byte short
    s = ~s & 0xffff

    return s

def IPHeader(SourceIP, DestinationIP, ID):
    Version         = 4
    Header_Length   = 5
    Type_Of_Service = 0
    Total_Length    = 0 #Kernel will fill this in for us
    Fragment_Offset = 0 #If it's split at this point, sum tin wong
    Time_To_Live    = 255
    Protocol        = socket.IPPROTO_TCP
    Checksum        = 0 #Kernel will fill this in for us
    Source_IP       = socket.inet_aton(SourceIP)
    Destination_IP = socket.inet_aton(DestinationIP)

    #We can not pack 4 bit numbers, so we left shift Version and add Header to
    #make an 8 bit number
    Version_and_Header_Length = (Version << 4) + Header_Length

    return pack(
        '!BBHHHBBH4s4s'
        , Version_and_Header_Length
        , Type_Of_Service
        , Total_Length
        , ID
        , Fragment_Offset
        , Time_To_Live
        , Protocol
        , Checksum
        , Source_IP
        , Destination_IP
    )

def TCPHeader(SourceIP, DestinationIP, SourcePort, DestinationPort, SYN, ACK, SeqNr,         AckSeq, WindowSize, UserData):
    Source_Port       = SourcePort
    Destination_Port  = DestinationPort
    Sequence_Number   = SeqNr
    Ack_Number        = AckSeq
    Header_Length     = 5

    #Flags
    Urg               = 0
    Ack               = ACK
    Psh               = 0
    Rst               = 0
    Syn               = SYN
    Fin               = 0

    Window_Size       = WindowSize
    Checksum          = 0
    Urgent_Pointer    = 0

    Header_Length_and_Offset = (Header_Length << 4) + 0
    Flags                    = Fin + (Syn << 1) + (Rst << 2) + (Psh << 3) + (Ack << 4) +   (Urg << 5)

    TCP_Header = pack(
        '!HHLLBBHHH'
        , Source_Port
        , Destination_Port
        , Sequence_Number
        , Ack_Number
        , Header_Length_and_Offset
        , Flags
        , Window_Size
        , Checksum
        , Urgent_Pointer
    )

    Source_IP      = socket.inet_aton(SourceIP)
    Destination_IP = socket.inet_aton(DestinationIP)
    Placeholder    = 0
    Protocol       = socket.IPPROTO_TCP
    Length         = len(TCP_Header) + len(UserData)

    Pseudo_Header = pack(
        '!4s4sBBH'
        , Source_IP
        , Destination_IP
        , Placeholder
        , Protocol
        , Length
    )

    Pseudo_Header = Pseudo_Header + TCP_Header + UserData
    Checksum      = checksum(Pseudo_Header)

    return pack(
        '!HHLLBBH'
        , Source_Port
        , Destination_Port
        , Sequence_Number
        , Ack_Number
        , Header_Length_and_Offset
        , Flags
        , Window_Size
    ) + pack('H', Checksum) + pack ('!H', Urgent_Pointer)

message = IPHeader("192.168.0.100", "192.168.0.103", 0)+TCPHeader("192.168.0.100",  "192.168.0.103", 500, 80, 1, 0, 0, 0, 1000, "")

s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
s.sendto(message, ("192.168.0.103", 0))

response, addr = s.recvfrom(1000)
SourceIP = unpack('!4s', response[12:16])
print map(ord, (SourceIP[0]))

0 个答案:

没有答案