Skip to content

UDP part TODO and TCP header length edit #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion raw_python/lib/Tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def create_tcp_feilds(self):
self.tcp_ack_seq = self.ack

# ---- [ Header Length ]
self.tcp_hdr_len = 80
self.tcp_hdr_len = 20

# ---- [ TCP Flags ]
f = self.flags
Expand Down Expand Up @@ -184,3 +184,4 @@ def create_tcp_feilds(self):
self.tcp_urg_ptr = 0

return

106 changes: 106 additions & 0 deletions raw_python/lib/Udp.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,109 @@


# TODO: complete this

import socket
import struct

from ..samples import utils

UDP_STRUCTURE_FMT = '!HHHH'

class UDPPacket:
def __init__(self,
dport=80,
sport=65535,
dst='127.0.0.1',
src=utils.get_ip(), # '192.168.1.101',
data=''
):
self.dport = dport
self.sport = sport
self.src_ip = src
self.dst_ip = dst
self.data = data
self.raw = None
self.create_udp_feilds()
self.assemble_udp_feilds()
# self.calculate_chksum()
# self.reassemble_udp_feilds()

def assemble_udp_feilds(self):
self.raw = struct.pack('!HHHH', # Data Structure Representation
self.sport, # Source Port
self.dport, # Destination Port
self.udp_len, # Total Length
self.udp_chksum, # UDP
)

self.calculate_chksum() # Call Calculate CheckSum
return

def reassemble_udp_feilds(self):
self.raw = struct.pack(UDP_STRUCTURE_FMT,
self.udp_src,
self.udp_dst,
self.udp_len,
socket.htons(self.udp_chksum)
)
return

def calculate_chksum(self):
src_addr = socket.inet_aton(self.src_ip)
dest_addr = socket.inet_aton(self.dst_ip)
placeholder = 0
protocol = socket.IPPROTO_UDP
self.udp_len = len(self.raw) + len(self.data)

psh = struct.pack('!4s4sBBH',
src_addr,
dest_addr,
placeholder,
protocol,
self.udp_len
)

psh = ''.join([psh, self.raw, self.data])

self.udp_chksum = self.chksum(psh)

self.reassemble_udp_feilds()

return

def chksum(self, msg):
s = 0 # Binary Sum

# loop taking 2 characters at a time
for i in range(0, len(msg), 2):
if (i + 1) < len(msg):
a = ord(msg[i])
b = ord(msg[i + 1])
s = s + (a + (b << 8))
elif (i + 1) == len(msg):
s += ord(msg[i])
else:
raise Exception("Something Wrong here")

s = (s >> 16) + (s & 0xffff)
# One's Complement
s = s + (s >> 16)
s = ~s & 0xffff

return s

def create_udp_feilds(self):

# ---- [ Source Port ]
self.udp_src = self.sport

# ---- [ Destination Port ]
self.udp_dst = self.dport

# ---- [ Header Length ]
self.udp_len = 16

# ---- [ UDP CheckSum ]
self.udp_chksum = 0

return
60 changes: 60 additions & 0 deletions traceroute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import ping
import socket
import time

def main():
ttl = 1
tries = 3
arrived = False
ipfound = False

# create socket
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)

# take Input
addr = input("Enter Domain Name : ") or "www.sustc.edu.cn"
print('traceroute {0} ({1})'.format(addr, socket.gethostbyname(addr)))
print("No. Address rtts")


while not arrived:
s.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)

for i in range(0, tries):
# Request sent
ID = ping.single_ping_request(s, addr)

rtt, reply, icmp_reply = ping.catch_ping_reply(s, ID, time.time())
#print(rtt)

if reply:
reply['length'] = reply['Total Length'] - 20 # sub header

if i == 0:
print(str(ttl) , end=" ")

if reply == None:
print('* ', end=" ")
continue

if socket.gethostbyname(addr) == reply["Source Address"]:
#print("arrived")
arrived = True

if i == 0 or ipfound == False:
print(str(reply["Source Address"]), end=" ")
ipfound = True

print(('{0:.2f} ms '.format(rtt*1000)), end=" ")

print("")
ttl += 1
ipfound = False

# close socket
s.close()
return


if __name__ == '__main__':
main()