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

Function in6_getLinkScopedMcastAddr

scapy/utils6.py:305–373  ·  view source on GitHub ↗

Generate a Link-Scoped Multicast Address as described in RFC 4489. Returned value is in printable notation. 'addr' parameter specifies the link-local address to use for generating Link-scoped multicast address IID. By default, the function returns a ::/96 prefix (aka last 32 b

(addr, grpid=None, scope=2)

Source from the content-addressed store, hash-verified

303
304
305def in6_getLinkScopedMcastAddr(addr, grpid=None, scope=2):
306 # type: (str, Optional[Union[bytes, str, int]], int) -> Optional[str]
307 """
308 Generate a Link-Scoped Multicast Address as described in RFC 4489.
309 Returned value is in printable notation.
310
311 'addr' parameter specifies the link-local address to use for generating
312 Link-scoped multicast address IID.
313
314 By default, the function returns a ::/96 prefix (aka last 32 bits of
315 returned address are null). If a group id is provided through 'grpid'
316 parameter, last 32 bits of the address are set to that value (accepted
317 formats : b'\x12\x34\x56\x78' or '12345678' or 0x12345678 or 305419896).
318
319 By default, generated address scope is Link-Local (2). That value can
320 be modified by passing a specific 'scope' value as an argument of the
321 function. RFC 4489 only authorizes scope values <= 2. Enforcement
322 is performed by the function (None will be returned).
323
324 If no link-local address can be used to generate the Link-Scoped IPv6
325 Multicast address, or if another error occurs, None is returned.
326 """
327 if scope not in [0, 1, 2]:
328 return None
329 try:
330 if not in6_islladdr(addr):
331 return None
332 baddr = inet_pton(socket.AF_INET6, addr)
333 except Exception:
334 warning("in6_getLinkScopedMcastPrefix(): Invalid address provided")
335 return None
336
337 iid = baddr[8:]
338
339 if grpid is None:
340 b_grpid = b'\x00\x00\x00\x00'
341 else:
342 b_grpid = b''
343 # Is either bytes, str or int
344 if isinstance(grpid, (str, bytes)):
345 try:
346 if isinstance(grpid, str) and len(grpid) == 8:
347 i_grpid = int(grpid, 16) & 0xffffffff
348 elif isinstance(grpid, bytes) and len(grpid) == 4:
349 i_grpid = struct.unpack("!I", grpid)[0]
350 else:
351 raise ValueError
352 except Exception:
353 warning(
354 "in6_getLinkScopedMcastPrefix(): Invalid group id "
355 "provided"
356 )
357 return None
358 elif isinstance(grpid, int):
359 i_grpid = grpid
360 else:
361 warning(
362 "in6_getLinkScopedMcastPrefix(): Invalid group id "

Callers

nothing calls this directly

Calls 4

inet_ptonFunction · 0.90
warningFunction · 0.90
inet_ntopFunction · 0.90
in6_islladdrFunction · 0.85

Tested by

no test coverage detected