Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
IOT - Worksheet 3
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
jo2-holdsworth
IOT - Worksheet 3
Commits
484dd59a
Commit
484dd59a
authored
2 years ago
by
jo2-holdsworth
Browse files
Options
Downloads
Patches
Plain Diff
task 2 and 3
parent
e66586f8
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
udp.py
+46
-136
46 additions, 136 deletions
udp.py
with
46 additions
and
136 deletions
udp.py
+
46
−
136
View file @
484dd59a
...
...
@@ -12,9 +12,46 @@ async def main():
message
=
await
websocket
.
recv
()
udp
=
decodePacket
(
message
)
printUDPPacket
(
udp
)
compute_checksum
(
udp
[
'
Source Port
'
],
udp
[
'
Dest Port
'
],
udp
[
'
Server Sent
'
][
8
:(
len
(
udp
[
'
Server Sent
'
])
+
8
)],
udp
[
'
Data Length
'
])
compute_checksum
(
udp
[
'
Source Port
'
],
udp
[
'
Dest Port
'
],
udp
[
'
Server Sent
'
][
8
:(
len
(
udp
[
'
Server Sent
'
])
+
8
)])
#print(f"calculated checksum: {int(compute_checksum(2,1,b'AB'),2)}")
print
(
bin
(
udp
[
'
Checksum
'
]))
async
def
mainSend
():
uri
=
"
ws://localhost:5612
"
async
with
websockets
.
connect
(
uri
)
as
websocket
:
await
recv_and_decode_packet
(
websocket
)
while
True
:
await
send_packet
(
websocket
,
0
,
542
,
b
'
1111
'
)
await
recv_and_decode_packet
(
websocket
)
time
.
sleep
(
1
)
async
def
recv_and_decode_packet
(
websocket
):
message
=
await
websocket
.
recv
()
print
(
"
here
"
)
print
(
decodePacket
(
message
))
async
def
send_packet
(
websocket
,
sourcePort
,
destPort
,
payload
):
print
(
type
(
payload
))
checkSum
=
compute_checksum
(
sourcePort
,
destPort
,
payload
)
print
(
checkSum
)
destPort
=
destPort
.
to_bytes
(
2
,
'
little
'
)
sourcePort
=
sourcePort
.
to_bytes
(
2
,
'
little
'
)
length
=
8
+
len
(
payload
)
length
=
length
.
to_bytes
(
2
,
'
little
'
)
checkSum
=
checkSum
.
to_bytes
(
2
,
'
little
'
)
print
(
f
"
length :
{
length
}
"
)
packet
=
sourcePort
+
destPort
+
length
+
checkSum
+
payload
print
(
destPort
+
sourcePort
)
packet
=
base64
.
b64encode
(
packet
)
await
websocket
.
send
(
packet
)
def
decodePacket
(
response
):
decoded
=
{}
decoded
[
'
Base64
'
]
=
response
...
...
@@ -33,15 +70,15 @@ def printUDPPacket(dict):
for
key
,
value
in
dict
.
items
():
print
(
f
"
{
key
}
:
{
value
}
"
)
def
compute_checksum
(
sourcePort
,
destPort
,
payload
,
length
):
def
compute_checksum
(
sourcePort
,
destPort
,
payload
):
print
(
sourcePort
)
print
(
destPort
)
print
(
payload
)
print
(
length
)
sourcePort
=
sourcePort
.
to_bytes
(
2
,
'
little
'
)
destPort
=
destPort
.
to_bytes
(
2
,
'
little
'
)
length
=
len
gth
.
to_bytes
(
2
,
'
little
'
)
length
=
(
len
(
payload
)
+
8
)
.
to_bytes
(
2
,
'
little
'
)
calc
=
sourcePort
+
destPort
+
length
+
bytes
(
2
)
+
payload
...
...
@@ -57,147 +94,20 @@ def compute_checksum(sourcePort, destPort, payload, length):
tmp
=
(
calc
[
i
]
<<
8
)
+
calc
[
i
+
1
]
sum
+=
tmp
sum
=
(
sum
<<
16
)
+
(
sum
&
0xFF
)
sum
=
(
sum
>>
16
)
+
(
sum
&
0xFFFF
)
sum
=
~
sum
&
0xFFFF
sum
=
int
(
format
(
sum
,
'
b
'
),
2
)
return
sum
return
~
sum
# payload = bytearray(payload)
print
(
type
(
payload
))
# if (len(payload) % 2) != 0:
# print("0")
# payload.append(bytes())
x
=
0
i
=
0
print
(
len
(
payload
))
j
=
(
len
(
payload
)
//
2
)
*
2
print
(
j
)
while
i
<
j
:
x
=
format
(
payload
[
i
],
"
b
"
)
for
n
in
range
(
0
,
8
-
len
(
x
)):
#n note used
x
=
"
0
"
+
x
y
=
format
(
payload
[
i
+
1
],
"
b
"
)
for
n
in
range
(
0
,
8
-
len
(
y
)):
y
=
"
0
"
+
y
z
=
x
+
y
z
=
"
0b
"
+
z
print
(
f
"
x:
{
x
}
"
)
print
(
f
"
y:
{
y
}
"
)
print
(
f
"
z:
{
z
}
"
)
calc
=
calc
+
int
(
z
,
2
)
print
(
f
"
int of z:
{
int
(
z
,
2
)
}
"
)
print
(
f
"
calc after calc:
{
calc
}
"
)
i
+=
2
if
len
(
payload
)
%
2
!=
0
:
print
(
f
"
i:
{
i
}
"
)
x
=
format
(
payload
[
i
],
"
b
"
)
for
f
in
range
(
0
,
8
-
len
(
x
)):
x
=
"
0
"
+
x
print
(
f
"
payload:
{
payload
[
i
]
}
"
)
y
=
"
00000000
"
z
=
x
+
y
print
(
x
)
print
(
z
)
z
=
"
0b
"
+
z
calc
=
calc
+
int
(
z
,
2
)
# while i <= len(payload):
# x = format(payload[i],"b")
# for n in range(0,8 - len(x)): #n note used
# x = "0" + x
# y = format(payload[i+1],"b")
# for n in range(0,8 - len(z)):
# z = "0" + z
# z = x + y
# z = "0b" + z
# calc = calc + int(z,2)
# print(calc)
# i += 2
# else:
# print("heres")
# j = (len(payload) // 2)*2
# print(j)
# while i < j:
# x = format(payload[i],"b")
# for n in range(0,8 - len(x)): #n note used
# print("hereres")
# x = "0" + x
# y = format(payload[i+1],"b")
# print(f"y: {y}")
# for n in range(0,8 - len(y)):
# print("okkk")
# y = "0" + y
# z = x + y
# print(f"length of z: {len(z)}")
# z = "0b" + z
# calc = calc + int(z,2)
# print(calc)
# i += 2
# print(f"i: {i}")
# x = format(payload[i],"b")
# for f in range(0,8 - len(x)):
# x = "0" + x
# print(f"payload: {payload[i]}")
# y = "00000000"
# z = y + x
# print(x)
# print(z)
# z = "0b" + z
# calc = calc + int(z,2)
# for byte in payload:
# x = + 1
# print(type(byte))
# z = format(byte,"b")
# calcPayload += z
# calcPayload = "0b" + calcPayload
# if x % 2 != 0:
# calcPayload +="00000000"
# print("here")
print
(
f
"
before calc:
{
calc
}
"
)
print
(
format
(
calc
,
"
b
"
))
flipped
=
""
calc
=
calc
&
0xFFFF
print
(
type
(
calc
))
calc
=
format
(
calc
,
"
b
"
)
print
(
f
"
after calc1:
{
calc
}
"
)
for
f
in
range
(
0
,
16
-
len
(
calc
)):
calc
=
"
0
"
+
calc
print
(
f
"
after calc2:
{
calc
}
"
)
print
(
int
(
calc
,
2
))
# print(format(calc,"b"))
# calcPayload = bin(calc)
for
bit
in
calc
:
if
bit
==
"
1
"
:
flipped
+=
"
0
"
elif
bit
==
"
0
"
:
flipped
+=
"
1
"
# flipped += bit
print
(
flipped
)
print
(
int
(
flipped
,
2
))
#mod odd number then add 8 0's
async
def
recv_message
(
websocket
):
message
=
json
.
loads
(
await
websocket
.
recv
())
return
message
[
'
payload
'
]
# 0000 0000 0111 0010
# 1111 1101 1111 0111
# 0000 0010 0000 1000
# 0111 0110 0110 0101
# 0111 0110 0110 0101
# 1101 1111 0000 101
# 1101 1111 0000 101
# 0110 1111 1000 0101
#1001 0000 0111 1010
# 0111 0010 0000 0000
#0011 1011 1100 1011
# 1100 0100 0011 0100
if
__name__
==
"
__main__
"
:
print
(
"
Echo client
"
)
# while 1:
asyncio
.
run
(
main
())
asyncio
.
run
(
main
Send
())
time
.
sleep
(
10
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment