MCPcopy Index your code
hub / github.com/USArmyResearchLab/Dshell / packet_handler

Method packet_handler

dshell/plugins/tftp/tftp.py:114–234  ·  view source on GitHub ↗

Handles each UDP packet. It checks the TFTP opcode and parses accordingly.

(self, pkt)

Source from the content-addressed store, hash-verified

112 self.__closeStream(k, "POSSIBLY INCOMPLETE")
113
114 def packet_handler(self, pkt):
115 """
116 Handles each UDP packet. It checks the TFTP opcode and parses
117 accordingly.
118 """
119 udpp = pkt.pkt.upper_layer
120 while not isinstance(udpp, udp.UDP):
121 try:
122 udpp = udpp.upper_layer
123 except AttributeError:
124 # There doesn't appear to be a UDP layer, for some reason
125 return
126
127 data = udpp.body_bytes
128
129 try:
130 flag = struct.unpack("!H", data[:2])[0]
131 except struct.error:
132 return # awful small packet
133 data = data[2:]
134 if flag == self.RRQ:
135 # this packet is requesting to read a file from the server
136 try:
137 filename, mode = data.split(b"\x00")[0:2]
138 except ValueError:
139 return # probably not TFTP
140 clientIP, clientPort, serverIP, serverPort = pkt.sip, udpp.sport, pkt.dip, udpp.dport
141 self.unset_read_streams[(clientIP, clientPort, serverIP)] = {
142 'filename': filename,
143 'mode': mode,
144 'readwrite': 'read',
145 'closed_connection': False,
146 'filedata': {},
147 'timestamp': pkt.ts
148 }
149
150 elif flag == self.WRQ:
151 # this packet is requesting to write a file to the server
152 try:
153 filename, mode = data.split(b"\x00")[0:2]
154 except ValueError:
155 return # probably not TFTP
156 # in this case, we are writing to the "server"
157 clientIP, clientPort, serverIP, serverPort = pkt.sip, udpp.sport, pkt.dip, udpp.dport
158 self.unset_write_streams[(clientIP, clientPort, serverIP)] = {
159 'filename': filename,
160 'mode': mode,
161 'readwrite': 'write',
162 'closed_connection': False,
163 'filedata': {},
164 'timestamp': pkt.ts
165 }
166
167 elif flag == self.DATA:
168 # this packet is sending a chunk of data
169 clientIP, clientPort, serverIP, serverPort = pkt.sip, udpp.sport, pkt.dip, udpp.dport
170 key = (clientIP, clientPort, serverIP, serverPort)
171 if key not in self.open_streams:

Callers

nothing calls this directly

Calls 1

__closeStreamMethod · 0.95

Tested by

no test coverage detected