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

Class Field

scapy/fields.py:145–306  ·  view source on GitHub ↗

For more information on how this works, please refer to the 'Adding new protocols' chapter in the online documentation: https://scapy.readthedocs.io/en/stable/build_dissect.html

Source from the content-addressed store, hash-verified

143
144
145class Field(Generic[I, M], metaclass=Field_metaclass):
146 """
147 For more information on how this works, please refer to the
148 'Adding new protocols' chapter in the online documentation:
149
150 https://scapy.readthedocs.io/en/stable/build_dissect.html
151 """
152 __slots__ = [
153 "name",
154 "fmt",
155 "default",
156 "sz",
157 "owners",
158 "struct"
159 ]
160 islist = 0
161 ismutable = False
162 holds_packets = 0
163
164 def __init__(self, name, default, fmt="H"):
165 # type: (str, Any, str) -> None
166 if not isinstance(name, str):
167 raise ValueError("name should be a string")
168 self.name = name
169 if fmt[0] in "@=<>!":
170 self.fmt = fmt
171 else:
172 self.fmt = "!" + fmt
173 self.struct = struct.Struct(self.fmt)
174 self.default = self.any2i(None, default)
175 self.sz = struct.calcsize(self.fmt) # type: int
176 self.owners = [] # type: List[Type[Packet]]
177
178 def register_owner(self, cls):
179 # type: (Type[Packet]) -> None
180 self.owners.append(cls)
181
182 def i2len(self,
183 pkt, # type: Packet
184 x, # type: Any
185 ):
186 # type: (...) -> int
187 """Convert internal value to a length usable by a FieldLenField"""
188 if isinstance(x, RawVal):
189 return len(x)
190 return self.sz
191
192 def i2count(self, pkt, x):
193 # type: (Optional[Packet], I) -> int
194 """Convert internal value to a number of elements usable by a FieldLenField.
195 Always 1 except for list fields"""
196 return 1
197
198 def h2i(self, pkt, x):
199 # type: (Optional[Packet], Any) -> I
200 """Convert human value to internal value"""
201 return cast(I, x)
202

Callers 15

rtmsghdrClass · 0.90
nlmsgerrClass · 0.90
ifinfomsgClass · 0.90
ifaddrmsgClass · 0.90
rtmsg_rtattrClass · 0.90
pfmsghdrClass · 0.90
sockaddrClass · 0.90
if_dataClass · 0.90
if_msghdrClass · 0.90
ifa_msghdrClass · 0.90
if_announcemsghdrClass · 0.90
rt_metricsClass · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected