MCPcopy Index your code
hub / github.com/secdev/scapy / _CoAPOptsField

Class _CoAPOptsField

scapy/contrib/coap.py:148–191  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

146
147
148class _CoAPOptsField(StrField):
149 islist = 1
150
151 def i2h(self, pkt, x):
152 return [(coap_options[0][o[0]], o[1]) if o[0] in coap_options[0] else o for o in x] # noqa: E501
153
154 # consume only the coap layer from the wire string
155 def getfield(self, pkt, s):
156 opts = self.m2i(pkt, s)
157 used = 0
158 for o in opts:
159 used += o[0]
160 return s[used:], [(o[1], o[2]) for o in opts]
161
162 def m2i(self, pkt, x):
163 opts = []
164 o = _CoAPOpt(x)
165 cur_delta = 0
166 while isinstance(o, _CoAPOpt):
167 cur_delta += _get_abs_val(o.delta, o.delta_ext)
168 # size of this option in bytes
169 u = 1 + len(o.opt_val) + len(o.delta_ext) + len(o.len_ext)
170 opts.append((u, cur_delta, o.opt_val))
171 o = o.payload
172 return opts
173
174 def i2m(self, pkt, x):
175 if not x:
176 return b""
177 opt_lst = []
178 for o in x:
179 if isinstance(o[0], str):
180 opt_lst.append((coap_options[1][o[0]], o[1]))
181 else:
182 opt_lst.append(o)
183 opt_lst.sort(key=lambda o: o[0])
184
185 opts = _CoAPOpt(delta=opt_lst[0][0], opt_val=opt_lst[0][1])
186 high_opt = opt_lst[0][0]
187 for o in opt_lst[1:]:
188 opts = opts / _CoAPOpt(delta=o[0] - high_opt, opt_val=o[1])
189 high_opt = o[0]
190
191 return raw(opts)
192
193
194class _CoAPPaymark(StrField):

Callers 1

CoAPClass · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected