{"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\noverloading to restrict the acceptable x values to None.", "response": "def h2i(self, pkt, x):\n # type: (Optional[packet.Packet], int) -> int\n \"\"\" h2i is overloaded to restrict the acceptable x values (not None)\n\n @param packet.Packet|None pkt: the packet instance containing this field instance; probably unused. # noqa: E501\n @param int x: the value to convert.\n @return int: the converted value.\n @raise AssertionError\n \"\"\"\n ret = super(UVarIntField, self).h2i(pkt, x)\n assert(not isinstance(ret, type(None)) and ret >= 0)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef i2repr(self, pkt, x):\n # type: (Optional[packet.Packet], int) -> str\n \"\"\" i2repr is overloaded to restrict the acceptable x values (not None)\n\n @param packet.Packet|None pkt: the packet instance containing this field instance; probably unused. # noqa: E501\n @param int x: the value to convert.\n @return str: the converted value.\n \"\"\"\n return super(UVarIntField, self).i2repr(pkt, x)", "response": "Overloaded to restrict the acceptable x values to None."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _compute_value(self, pkt):\n # type: (packet.Packet) -> int\n \"\"\" Computes the value of this field based on the provided packet and\n the length_of field and the adjust callback\n\n @param packet.Packet pkt: the packet from which is computed this field value. # noqa: E501\n @return int: the computed value for this field.\n @raise KeyError: the packet nor its payload do not contain an attribute\n with the length_of name.\n @raise AssertionError\n @raise KeyError if _length_of is not one of pkt fields\n \"\"\"\n fld, fval = pkt.getfield_and_val(self._length_of)\n val = fld.i2len(pkt, fval)\n ret = self._adjust(val)\n assert(ret >= 0)\n return ret", "response": "Compute the value of this field based on the provided packet and the length_of attribute and the adjust callback\n ."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef huffman_encode(cls, s):\n # type: (str) -> Tuple[int, int]\n \"\"\" huffman_encode returns the bitstring and the bitlength of the\n bitstring representing the string provided as a parameter\n\n @param str s: the string to encode\n @return (int, int): the bitstring of s and its bitlength\n @raise AssertionError\n \"\"\"\n i = 0\n ibl = 0\n for c in s:\n val, bl = cls._huffman_encode_char(c)\n i = (i << bl) + val\n ibl += bl\n\n padlen = 8 - (ibl % 8)\n if padlen != 8:\n val, bl = cls._huffman_encode_char(EOS())\n i = (i << padlen) + (val >> (bl - padlen))\n ibl += padlen\n\n ret = i, ibl\n assert(ret[0] >= 0)\n assert (ret[1] >= 0)\n return ret", "response": "huffman_encode returns the bitstring and the bitlength of the the\n class s parameter as a parameter\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef huffman_compute_decode_tree(cls):\n # type: () -> None\n \"\"\" huffman_compute_decode_tree initializes/builds the static_huffman_tree\n\n @return None\n @raise InvalidEncodingException if there is an encoding problem\n \"\"\"\n cls.static_huffman_tree = HuffmanNode(None, None)\n i = 0\n for entry in cls.static_huffman_code:\n parent = cls.static_huffman_tree\n for idx in range(entry[1] - 1, -1, -1):\n b = (entry[0] >> idx) & 1\n if isinstance(parent[b], bytes):\n raise InvalidEncodingException('Huffman unique prefix violation :/') # noqa: E501\n if idx == 0:\n parent[b] = chb(i) if i < 256 else EOS()\n elif parent[b] is None:\n parent[b] = HuffmanNode(None, None)\n parent = parent[b]\n i += 1", "response": "Compute the decode tree for the static_huffman_tree."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef dispatch_hook(cls, s=None, *_args, **_kwds):\n # type: (Optional[str], *Any, **Any) -> base_classes.Packet_metaclass\n \"\"\"dispatch_hook returns the subclass of HPackHeaders that must be used\n to dissect the string.\n \"\"\"\n if s is None:\n return config.conf.raw_layer\n fb = orb(s[0])\n if fb & 0x80 != 0:\n return HPackIndexedHdr\n if fb & 0x40 != 0:\n return HPackLitHdrFldWithIncrIndexing\n if fb & 0x20 != 0:\n return HPackDynamicSizeUpdate\n return HPackLitHdrFldWithoutIndexing", "response": "This is a private method that returns the subclass of base_classes. Packet_metaclass_base. HPackHeaderBase that must be used\n to dissect the string."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_hdrs_len(self):\n # type: () -> int\n \"\"\" get_hdrs_len computes the length of the hdrs field\n\n To do this computation, the length of the padlen field, the priority\n information fields and the actual padding is subtracted to the string\n that was provided to the pre_dissect fun of the pkt parameter.\n @return int: the length of the hdrs field\n @raise AssertionError\n \"\"\"\n\n padding_len = self.getfieldval('padlen')\n fld, fval = self.getfield_and_val('padlen')\n padding_len_len = fld.i2len(self, fval)\n bit_cnt = self.get_field('exclusive').size\n bit_cnt += self.get_field('stream_dependency').size\n fld, fval = self.getfield_and_val('weight')\n weight_len = fld.i2len(self, fval)\n ret = int(self.s_len -\n padding_len_len -\n padding_len -\n (bit_cnt / 8) -\n weight_len\n )\n assert(ret >= 0)\n return ret", "response": "This method computes the length of the hdrs field\n ."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_hdrs_len(self):\n # type: () -> int\n \"\"\" get_hdrs_len computes the length of the hdrs field\n\n To do this computation, the length of the padlen field, reserved,\n stream_id and the actual padding is subtracted to the string that was\n provided to the pre_dissect fun of the pkt parameter.\n @return int: the length of the hdrs field\n @raise AssertionError\n \"\"\"\n fld, padding_len = self.getfield_and_val('padlen')\n padding_len_len = fld.i2len(self, padding_len)\n bit_len = self.get_field('reserved').size\n bit_len += self.get_field('stream_id').size\n\n ret = int(self.s_len -\n padding_len_len -\n padding_len -\n (bit_len / 8)\n )\n assert(ret >= 0)\n return ret", "response": "This method computes the length of the hdrs field\n ."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef guess_payload_class(self, payload):\n # type: (str) -> base_classes.Packet_metaclass\n \"\"\" guess_payload_class returns the Class object to use for parsing a payload\n This function uses the H2Frame.type field value to decide which payload to parse. The implement cannot be # noqa: E501\n performed using the simple bind_layers helper because sometimes the selection of which Class object to return # noqa: E501\n also depends on the H2Frame.flags value.\n\n @param payload:\n @return:\n \"\"\"\n if len(payload) == 0:\n return packet.NoPayload\n\n t = self.getfieldval('type')\n if t == H2DataFrame.type_id:\n if H2DataFrame.flags[H2DataFrame.PADDED_FLAG].short in self.getfieldval('flags'): # noqa: E501\n return H2PaddedDataFrame\n return H2DataFrame\n\n if t == H2HeadersFrame.type_id:\n if H2HeadersFrame.flags[H2HeadersFrame.PADDED_FLAG].short in self.getfieldval('flags'): # noqa: E501\n if H2HeadersFrame.flags[H2HeadersFrame.PRIORITY_FLAG].short in self.getfieldval('flags'): # noqa: E501\n return H2PaddedPriorityHeadersFrame\n else:\n return H2PaddedHeadersFrame\n elif H2HeadersFrame.flags[H2HeadersFrame.PRIORITY_FLAG].short in self.getfieldval('flags'): # noqa: E501\n return H2PriorityHeadersFrame\n return H2HeadersFrame\n\n if t == H2PriorityFrame.type_id:\n return H2PriorityFrame\n\n if t == H2ResetFrame.type_id:\n return H2ResetFrame\n\n if t == H2SettingsFrame.type_id:\n return H2SettingsFrame\n\n if t == H2PushPromiseFrame.type_id:\n if H2PushPromiseFrame.flags[H2PushPromiseFrame.PADDED_FLAG].short in self.getfieldval('flags'): # noqa: E501\n return H2PaddedPushPromiseFrame\n return H2PushPromiseFrame\n\n if t == H2PingFrame.type_id:\n return H2PingFrame\n\n if t == H2GoAwayFrame.type_id:\n return H2GoAwayFrame\n\n if t == H2WindowUpdateFrame.type_id:\n return H2WindowUpdateFrame\n\n if t == H2ContinuationFrame.type_id:\n return H2ContinuationFrame\n\n return config.conf.padding_layer", "response": "This function returns the Class object to use for parsing a payload."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef recap(self, nc):\n # type: (int) -> None\n \"\"\"recap changes the maximum size limit of the dynamic table. It also\n proceeds to a resize(), if the new size is lower than the previous one.\n @param int nc: the new cap of the dynamic table (that is the maximum-maximum size) # noqa: E501\n @raise AssertionError\n \"\"\"\n assert(nc >= 0)\n t = self._dynamic_table_cap_size > nc\n self._dynamic_table_cap_size = nc\n\n if t:\n # The RFC is not clear about whether this resize should happen;\n # we do it anyway\n self.resize(nc)", "response": "This method changes the maximum size limit of the dynamic table. It also updates the dynamic table cap size to match the new size of the dynamic table."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _reduce_dynamic_table(self, new_entry_size=0):\n # type: (int) -> None\n \"\"\"_reduce_dynamic_table evicts entries from the dynamic table until it\n fits in less than the current size limit. The optional parameter,\n new_entry_size, allows the resize to happen so that a new entry of this\n size fits in.\n @param int new_entry_size: if called before adding a new entry, the size of the new entry in bytes (following # noqa: E501\n the RFC7541 definition of the size of an entry)\n @raise AssertionError\n \"\"\"\n assert(new_entry_size >= 0)\n cur_sz = len(self)\n dyn_tbl_sz = len(self._dynamic_table)\n while dyn_tbl_sz > 0 and cur_sz + new_entry_size > self._dynamic_table_max_size: # noqa: E501\n last_elmt_sz = len(self._dynamic_table[-1])\n self._dynamic_table.pop()\n dyn_tbl_sz -= 1\n cur_sz -= last_elmt_sz", "response": "_reduce_dynamic_table evicts entries from the dynamic table until the current size limit is reached."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_idx_by_name(self, name):\n # type: (str) -> Optional[int]\n \"\"\" get_idx_by_name returns the index of a matching registered header\n\n This implementation will prefer returning a static entry index whenever\n possible. If multiple matching header name are found in the static\n table, there is insurance that the first entry (lowest index number)\n will be returned.\n If no matching header is found, this method returns None.\n \"\"\"\n name = name.lower()\n for key, val in six.iteritems(type(self)._static_entries):\n if val.name() == name:\n return key\n for idx, val in enumerate(self._dynamic_table):\n if val.name() == name:\n return type(self)._static_entries_last_idx + idx + 1\n return None", "response": "This method returns the index of a matching registered header in the static entry table that matches the given name."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef gen_txt_repr(self, hdrs, register=True):\n # type: (Union[H2Frame, List[HPackHeaders]], Optional[bool]) -> str\n \"\"\" gen_txt_repr returns a \"textual\" representation of the provided\n headers.\n\n The output of this function is compatible with the input of\n parse_txt_hdrs.\n @param H2Frame|list of HPackHeaders hdrs: the list of headers to convert to textual representation # noqa: E501\n @param bool: whether incremental headers should be added to the dynamic table as we generate the text # noqa: E501\n representation\n @return str: the textual representation of the provided headers\n @raise AssertionError\n \"\"\"\n lst = []\n if isinstance(hdrs, H2Frame):\n hdrs = hdrs.payload.hdrs\n\n for hdr in hdrs:\n try:\n if isinstance(hdr, HPackIndexedHdr):\n lst.append('{}'.format(self[hdr.index]))\n elif isinstance(hdr, (\n HPackLitHdrFldWithIncrIndexing,\n HPackLitHdrFldWithoutIndexing\n )):\n if hdr.index != 0:\n name = self[hdr.index].name()\n else:\n name = hdr.hdr_name.getfieldval('data').origin()\n if name.startswith(':'):\n lst.append(\n '{} {}'.format(\n name,\n hdr.hdr_value.getfieldval('data').origin()\n )\n )\n else:\n lst.append(\n '{}: {}'.format(\n name,\n hdr.hdr_value.getfieldval('data').origin()\n )\n )\n if register and isinstance(hdr, HPackLitHdrFldWithIncrIndexing): # noqa: E501\n self.register(hdr)\n except KeyError as e: # raised when an index is out-of-bound\n print(e)\n continue\n return '\\n'.join(lst)", "response": "This function returns a textual representation of the provided headers."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _convert_a_header_to_a_h2_header(self, hdr_name, hdr_value, is_sensitive, should_index): # noqa: E501\n # type: (str, str, Callable[[str, str], bool], Callable[[str], bool]) -> Tuple[HPackHeaders, int] # noqa: E501\n \"\"\" _convert_a_header_to_a_h2_header builds a HPackHeaders from a header\n name and a value. It returns a HPackIndexedHdr whenever possible. If not, # noqa: E501\n it returns a HPackLitHdrFldWithoutIndexing or a\n HPackLitHdrFldWithIncrIndexing, based on the should_index callback.\n HPackLitHdrFldWithoutIndexing is forced if the is_sensitive callback\n returns True and its never_index bit is set.\n \"\"\"\n\n # If both name and value are already indexed\n idx = self.get_idx_by_name_and_value(hdr_name, hdr_value)\n if idx is not None:\n return HPackIndexedHdr(index=idx), len(self[idx])\n\n # The value is not indexed for this headers\n\n hdr_value = self._optimize_header_length_and_packetify(hdr_value)\n\n # Searching if the header name is indexed\n idx = self.get_idx_by_name(hdr_name)\n if idx is not None:\n if is_sensitive(\n hdr_name,\n hdr_value.getfieldval('data').origin()\n ):\n return HPackLitHdrFldWithoutIndexing(\n never_index=1,\n index=idx,\n hdr_value=hdr_value\n ), len(\n HPackHdrEntry(\n self[idx].name(),\n hdr_value.getfieldval('data').origin()\n )\n )\n if should_index(hdr_name):\n return HPackLitHdrFldWithIncrIndexing(\n index=idx,\n hdr_value=hdr_value\n ), len(\n HPackHdrEntry(\n self[idx].name(),\n hdr_value.getfieldval('data').origin()\n )\n )\n return HPackLitHdrFldWithoutIndexing(\n index=idx,\n hdr_value=hdr_value\n ), len(\n HPackHdrEntry(\n self[idx].name(),\n hdr_value.getfieldval('data').origin()\n )\n )\n\n hdr_name = self._optimize_header_length_and_packetify(hdr_name)\n\n if is_sensitive(\n hdr_name.getfieldval('data').origin(),\n hdr_value.getfieldval('data').origin()\n ):\n return HPackLitHdrFldWithoutIndexing(\n never_index=1,\n index=0,\n hdr_name=hdr_name,\n hdr_value=hdr_value\n ), len(\n HPackHdrEntry(\n hdr_name.getfieldval('data').origin(),\n hdr_value.getfieldval('data').origin()\n )\n )\n if should_index(hdr_name.getfieldval('data').origin()):\n return HPackLitHdrFldWithIncrIndexing(\n index=0,\n hdr_name=hdr_name,\n hdr_value=hdr_value\n ), len(\n HPackHdrEntry(\n hdr_name.getfieldval('data').origin(),\n hdr_value.getfieldval('data').origin()\n )\n )\n return HPackLitHdrFldWithoutIndexing(\n index=0,\n hdr_name=hdr_name,\n hdr_value=hdr_value\n ), len(\n HPackHdrEntry(\n hdr_name.getfieldval('data').origin(),\n hdr_value.getfieldval('data').origin()\n )\n )", "response": "This function converts a header name and value to a HPackHeader. It returns a HPackIndexedHdr if possible and a HPackLitHdrFldWithIncrIndexing if possible."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nparse the given string s into a series of H2Frames with the correct flags.", "response": "def parse_txt_hdrs(self,\n s, # type: str\n stream_id=1, # type: int\n body=None, # type: Optional[str]\n max_frm_sz=4096, # type: int\n max_hdr_lst_sz=0, # type: int\n is_sensitive=lambda n, v: False, # type: Callable[[str, str], bool] # noqa: E501\n should_index=lambda x: False, # type: Callable[[str], bool] # noqa: E501\n register=True, # type: bool\n ):\n # type: (...) -> H2Seq\n \"\"\" parse_txt_hdrs parses headers expressed in text and converts them\n into a series of H2Frames with the \"correct\" flags. A body can be provided # noqa: E501\n in which case, the data frames are added, bearing the End Stream flag,\n instead of the H2HeadersFrame/H2ContinuationFrame. The generated frames\n may respect max_frm_sz (SETTINGS_MAX_FRAME_SIZE) and\n max_hdr_lst_sz (SETTINGS_MAX_HEADER_LIST_SIZE) if provided. The headers\n are split into multiple headers fragment (and H2Frames) to respect these # noqa: E501\n limits. Also, a callback can be provided to tell if a header should be\n never indexed (sensitive headers, such as cookies), and another callback # noqa: E501\n say if the header should be registered into the index table at all.\n For an header to be registered, the is_sensitive callback must return\n False AND the should_index callback should return True. This is the\n default behavior.\n\n @param str s: the string to parse for headers\n @param int stream_id: the stream id to use in the generated H2Frames\n @param str|None body: the eventual body of the request, that is added to the generated frames # noqa: E501\n @param int max_frm_sz: the maximum frame size. This is used to split the headers and data frames according to # noqa: E501\n the maximum frame size negotiated for this connection\n @param int max_hdr_lst_sz: the maximum size of a \"header fragment\" as defined in RFC7540 # noqa: E501\n @param callable is_sensitive: callback that returns True if the provided header is sensible and must be stored # noqa: E501\n in a header packet requesting this header never to be indexed\n @param callable should_index: callback that returns True if the provided header should be stored in a header # noqa: E501\n packet requesting indexation in the dynamic header table.\n @param bool register: whether to register new headers with incremental indexing as we parse them # noqa: E501\n @raise Exception\n \"\"\"\n\n sio = BytesIO(s)\n\n base_frm_len = len(raw(H2Frame()))\n\n ret = H2Seq()\n cur_frm = H2HeadersFrame() # type: Union[H2HeadersFrame, H2ContinuationFrame] # noqa: E501\n cur_hdr_sz = 0\n\n # For each line in the headers str to parse\n for hdr_line in sio:\n hdr_name, hdr_value = self._parse_header_line(hdr_line)\n if hdr_name is None:\n continue\n\n new_hdr, new_hdr_len = self._convert_a_header_to_a_h2_header(\n hdr_name, hdr_value, is_sensitive, should_index\n )\n new_hdr_bin_len = len(raw(new_hdr))\n\n if register and isinstance(new_hdr, HPackLitHdrFldWithIncrIndexing): # noqa: E501\n self.register(new_hdr)\n\n # The new header binary length (+ base frame size) must not exceed\n # the maximum frame size or it will just never fit. Also, the\n # header entry length (as specified in RFC7540 par6.5.2) must not\n # exceed the maximum length of a header fragment or it will just\n # never fit\n if (new_hdr_bin_len + base_frm_len > max_frm_sz or\n (max_hdr_lst_sz != 0 and new_hdr_len > max_hdr_lst_sz)):\n raise Exception('Header too long: {}'.format(hdr_name))\n\n if (max_frm_sz < len(raw(cur_frm)) + base_frm_len + new_hdr_len or\n (\n max_hdr_lst_sz != 0 and\n max_hdr_lst_sz < cur_hdr_sz + new_hdr_len\n )\n ):\n flags = set()\n if isinstance(cur_frm, H2HeadersFrame) and not body:\n flags.add('ES')\n ret.frames.append(H2Frame(stream_id=stream_id, flags=flags) / cur_frm) # noqa: E501\n cur_frm = H2ContinuationFrame()\n cur_hdr_sz = 0\n\n hdr_list = cur_frm.hdrs\n hdr_list += new_hdr\n cur_hdr_sz += new_hdr_len\n\n flags = {'EH'}\n if isinstance(cur_frm, H2HeadersFrame) and not body:\n flags.add('ES')\n ret.frames.append(H2Frame(stream_id=stream_id, flags=flags) / cur_frm)\n\n if body:\n base_data_frm_len = len(raw(H2DataFrame()))\n sio = BytesIO(body)\n frgmt = sio.read(max_frm_sz - base_data_frm_len - base_frm_len)\n while frgmt:\n nxt_frgmt = sio.read(max_frm_sz - base_data_frm_len - base_frm_len) # noqa: E501\n flags = set()\n if len(nxt_frgmt) == 0:\n flags.add('ES')\n ret.frames.append(\n H2Frame(stream_id=stream_id, flags=flags) / H2DataFrame(data=frgmt) # noqa: E501\n )\n frgmt = nxt_frgmt\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef AVP(avpId, **fields):\n val = None\n classType = AVP_Unknown\n if isinstance(avpId, str):\n try:\n for vnd in AvpDefDict:\n for code in AvpDefDict[vnd]:\n val = AvpDefDict[vnd][code]\n if val[0][:len(\n avpId)] == avpId: # A prefix of the full name is considered valid # noqa: E501\n raise\n found = False\n except BaseException:\n found = True\n else:\n if isinstance(avpId, list):\n code = avpId[0]\n vnd = avpId[1]\n else: # Assume this is an int\n code = avpId\n vnd = 0\n try:\n val = AvpDefDict[vnd][code]\n found = True\n except BaseException:\n found = False\n if not found:\n warning('The AVP identifier %s has not been found.' % str(avpId))\n if isinstance(avpId, str): # The string input is not valid\n return None\n # At this point code, vnd are provisionned val may be set (if found is True) # noqa: E501\n # Set/override AVP code\n fields['avpCode'] = code\n # Set vendor if not already defined and relevant\n if 'avpVnd' not in fields and vnd:\n fields['avpVnd'] = vnd\n # Set flags if not already defined and possible ...\n if 'avpFlags' not in fields:\n if val:\n fields['avpFlags'] = val[2]\n else:\n fields['avpFlags'] = vnd and 128 or 0\n # Finally, set the name and class if possible\n if val:\n classType = val[1]\n _ret = classType(**fields)\n if val:\n _ret.name = 'AVP ' + val[0]\n return _ret", "response": "Craft an AVP based on its id and optional parameter fields"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef getCmdParams(cmd, request, **fields):\n drCode = None\n params = None\n drAppId = None\n # Fetch the parameters if cmd is found in dict\n if isinstance(cmd, int):\n drCode = cmd # Enable to craft commands with non standard code\n if cmd in DR_cmd_def:\n params = DR_cmd_def[drCode]\n else:\n params = ('Unknown', 'UK', {0: (128, 0)})\n warning(\n 'No Diameter command with code %d found in DR_cmd_def dictionary' % # noqa: E501\n cmd)\n else: # Assume command is a string\n if len(cmd) > 3: # Assume full command name given\n fpos = 0\n else: # Assume abbreviated name is given and take only the first two letters # noqa: E501\n cmd = cmd[:2]\n fpos = 1\n for k, f in DR_cmd_def.items():\n if f[fpos][:len(\n cmd)] == cmd: # Accept only a prefix of the full name\n drCode = k\n params = f\n break\n if not drCode:\n warning(\n 'Diameter command with name %s not found in DR_cmd_def dictionary.' % # noqa: E501\n cmd)\n return (fields, 'Unknown')\n # The drCode is set/overridden in any case\n fields['drCode'] = drCode\n # Processing of drAppId\n if 'drAppId' in fields:\n val = fields['drAppId']\n if isinstance(val, str): # Translate into application Id code\n found = False\n for k, v in six.iteritems(AppIDsEnum):\n if v.find(val) != -1:\n drAppId = k\n fields['drAppId'] = drAppId\n found = True\n break\n if not found:\n del(fields['drAppId'])\n warning(\n 'Application ID with name %s not found in AppIDsEnum dictionary.' % # noqa: E501\n val)\n return (fields, 'Unknown')\n else: # Assume type is int\n drAppId = val\n else: # Application Id shall be taken from the params found based on cmd\n drAppId = next(iter(params[2])) # The first record is taken\n fields['drAppId'] = drAppId\n # Set the command name\n name = request and params[0] + '-Request' or params[0] + '-Answer'\n # Processing of flags (only if not provided manually)\n if 'drFlags' not in fields:\n if drAppId in params[2]:\n flags = params[2][drAppId]\n fields['drFlags'] = request and flags[0] or flags[1]\n return (fields, name)", "response": "Update or fill the fields with the parameters depending on the command code."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncrafting Diameter request commands", "response": "def DiamReq(cmd, **fields):\n \"\"\"Craft Diameter request commands\"\"\"\n upfields, name = getCmdParams(cmd, True, **fields)\n p = DiamG(**upfields)\n p.name = name\n return p"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef DiamAns(cmd, **fields):\n upfields, name = getCmdParams(cmd, False, **fields)\n p = DiamG(**upfields)\n p.name = name\n return p", "response": "Craft Diameter answer commands"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef i2m(self, pkt, x):\n if x is None:\n x = 0\n elif isinstance(x, str):\n return bytes_encode(x)\n return x", "response": "Convert internal value to machine value"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _find_fld_pkt_val(self, pkt, val):\n fld = self._iterate_fields_cond(pkt, val, True)\n # Default ? (in this case, let's make sure it's up-do-date)\n dflts_pkt = pkt.default_fields\n if val == dflts_pkt[self.name] and self.name not in pkt.fields:\n dflts_pkt[self.name] = fld.default\n val = fld.default\n return fld, val", "response": "Given a Packet instance pkt and the value val to be set returns the Field subclass to be used and the updated val."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the Field subclass to be used depending on the Packet instance.", "response": "def _find_fld(self):\n \"\"\"Returns the Field subclass to be used, depending on the Packet\ninstance, or the default subclass.\n\nDEV: since the Packet instance is not provided, we have to use a hack\nto guess it. It should only be used if you cannot provide the current\nPacket instance (for example, because of the current Scapy API).\n\nIf you have the current Packet instance, use ._find_fld_pkt_val() (if\nthe value to set is also known) of ._find_fld_pkt() instead.\n\n \"\"\"\n # Hack to preserve current Scapy API\n # See https://stackoverflow.com/a/7272464/3223422\n frame = inspect.currentframe().f_back.f_back\n while frame is not None:\n try:\n pkt = frame.f_locals['self']\n except KeyError:\n pass\n else:\n if isinstance(pkt, tuple(self.dflt.owners)):\n return self._find_fld_pkt(pkt)\n frame = frame.f_back\n return self.dflt"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _fixup_val(self, x):\n if isinstance(x, (list, tuple)):\n return type(x)(\n v if v is None or isinstance(v, FlagValue)\n else FlagValue(v, self.names)\n for v in x\n )\n return x if x is None or isinstance(x, FlagValue) else FlagValue(x, self.names)", "response": "Returns a FlagValue instance when needed. Internal method to be\nused in i2i and i2o methods."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _check_uuid_fmt(self):\n if self.uuid_fmt not in UUIDField.FORMATS:\n raise FieldValueRangeException(\n \"Unsupported uuid_fmt ({})\".format(self.uuid_fmt))", "response": "Checks. uuid_fmt and raises an exception if it is not valid."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef pre_dissect(self, s):\n if len(s) < 1:\n raise Exception(\"Invalid InnerPlaintext (too short).\")\n\n tmp_len = len(s) - 1\n if s[-1] != b\"\\x00\":\n msg_len = tmp_len\n else:\n n = 1\n while s[-n] != b\"\\x00\" and n < tmp_len:\n n += 1\n msg_len = tmp_len - n\n self.fields_desc[0].length_from = lambda pkt: msg_len\n\n self.type = struct.unpack(\"B\", s[msg_len:msg_len + 1])[0]\n\n return s", "response": "Parse the message list and set the type field."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _tls_auth_decrypt(self, s):\n rcs = self.tls_session.rcs\n read_seq_num = struct.pack(\"!Q\", rcs.seq_num)\n rcs.seq_num += 1\n try:\n return rcs.cipher.auth_decrypt(b\"\", s, read_seq_num)\n except CipherError as e:\n return e.args\n except AEADTagError as e:\n pkt_info = self.firstlayer().summary()\n log_runtime.info(\"TLS: record integrity check failed [%s]\", pkt_info) # noqa: E501\n return e.args", "response": "Decrypt the input string s with AEAD - ciphered data and return the decrypted AEAD - encrypted data."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef pre_dissect(self, s):\n if len(s) < 5:\n raise Exception(\"Invalid record: header is too short.\")\n\n if isinstance(self.tls_session.rcs.cipher, Cipher_NULL):\n self.deciphered_len = None\n return s\n else:\n msglen = struct.unpack('!H', s[3:5])[0]\n hdr, efrag, r = s[:5], s[5:5 + msglen], s[msglen + 5:]\n frag, auth_tag = self._tls_auth_decrypt(efrag)\n self.deciphered_len = len(frag)\n return hdr + frag + auth_tag + r", "response": "Decrypt verify and decompress the message."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef post_dissect(self, s):\n if self.tls_session.triggered_prcs_commit:\n if self.tls_session.prcs is not None:\n self.tls_session.rcs = self.tls_session.prcs\n self.tls_session.prcs = None\n self.tls_session.triggered_prcs_commit = False\n return s", "response": "Update the pending read state if the prcs has been set."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _tls_auth_encrypt(self, s):\n wcs = self.tls_session.wcs\n write_seq_num = struct.pack(\"!Q\", wcs.seq_num)\n wcs.seq_num += 1\n return wcs.cipher.auth_encrypt(s, b\"\", write_seq_num)", "response": "Encrypt a string with the AEAD ciphertext."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\napply the previous methods according to the writing cipher type.", "response": "def post_build(self, pkt, pay):\n \"\"\"\n Apply the previous methods according to the writing cipher type.\n \"\"\"\n # Compute the length of TLSPlaintext fragment\n hdr, frag = pkt[:5], pkt[5:]\n if not isinstance(self.tls_session.rcs.cipher, Cipher_NULL):\n frag = self._tls_auth_encrypt(frag)\n\n if self.len is not None:\n # The user gave us a 'len', let's respect this ultimately\n hdr = hdr[:3] + struct.pack(\"!H\", self.len)\n else:\n # Update header with the length of TLSCiphertext.inner\n hdr = hdr[:3] + struct.pack(\"!H\", len(frag))\n\n # Now we commit the pending write state if it has been triggered. We\n # update nothing if the pwcs was not set. This probably means that\n # we're working out-of-context (and we need to keep the default wcs).\n if self.tls_session.triggered_pwcs_commit:\n if self.tls_session.pwcs is not None:\n self.tls_session.wcs = self.tls_session.pwcs\n self.tls_session.pwcs = None\n self.tls_session.triggered_pwcs_commit = False\n\n return hdr + frag + pay"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nencrypt data with key key.", "response": "def ARC4_encrypt(key, data, skip=0):\n \"\"\"Encrypt data @data with key @key, skipping @skip first bytes of the\n keystream\"\"\"\n\n algorithm = algorithms.ARC4(key)\n cipher = Cipher(algorithm, mode=None, backend=default_backend())\n encryptor = cipher.encryptor()\n if skip:\n encryptor.update(b\"\\x00\" * skip)\n return encryptor.update(data)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nimplementing TKIP WEPSeed generation", "response": "def gen_TKIP_RC4_key(TSC, TA, TK):\n \"\"\"Implement TKIP WEPSeed generation\n TSC: packet IV\n TA: target addr bytes\n TK: temporal key\n \"\"\"\n\n assert len(TSC) == 6\n assert len(TA) == 6\n assert len(TK) == 16\n assert all(isinstance(x, six.integer_types) for x in TSC + TA + TK)\n\n # Phase 1\n # 802.11i p.54\n\n # Phase 1 - Step 1\n TTAK = []\n TTAK.append(_MK16(TSC[3], TSC[2]))\n TTAK.append(_MK16(TSC[5], TSC[4]))\n TTAK.append(_MK16(TA[1], TA[0]))\n TTAK.append(_MK16(TA[3], TA[2]))\n TTAK.append(_MK16(TA[5], TA[4]))\n\n # Phase 1 - Step 2\n for i in range(PHASE1_LOOP_CNT):\n j = 2 * (i & 1)\n TTAK[0] = _CAST16(TTAK[0] + _SBOX16(TTAK[4] ^ _MK16(TK[1 + j], TK[0 + j]))) # noqa: E501\n TTAK[1] = _CAST16(TTAK[1] + _SBOX16(TTAK[0] ^ _MK16(TK[5 + j], TK[4 + j]))) # noqa: E501\n TTAK[2] = _CAST16(TTAK[2] + _SBOX16(TTAK[1] ^ _MK16(TK[9 + j], TK[8 + j]))) # noqa: E501\n TTAK[3] = _CAST16(TTAK[3] + _SBOX16(TTAK[2] ^ _MK16(TK[13 + j], TK[12 + j]))) # noqa: E501\n TTAK[4] = _CAST16(TTAK[4] + _SBOX16(TTAK[3] ^ _MK16(TK[1 + j], TK[0 + j])) + i) # noqa: E501\n\n # Phase 2\n # 802.11i p.56\n\n # Phase 2 - Step 1\n PPK = list(TTAK)\n PPK.append(_CAST16(TTAK[4] + _MK16(TSC[1], TSC[0])))\n\n # Phase 2 - Step 2\n PPK[0] = _CAST16(PPK[0] + _SBOX16(PPK[5] ^ _MK16(TK[1], TK[0])))\n PPK[1] = _CAST16(PPK[1] + _SBOX16(PPK[0] ^ _MK16(TK[3], TK[2])))\n PPK[2] = _CAST16(PPK[2] + _SBOX16(PPK[1] ^ _MK16(TK[5], TK[4])))\n PPK[3] = _CAST16(PPK[3] + _SBOX16(PPK[2] ^ _MK16(TK[7], TK[6])))\n PPK[4] = _CAST16(PPK[4] + _SBOX16(PPK[3] ^ _MK16(TK[9], TK[8])))\n PPK[5] = _CAST16(PPK[5] + _SBOX16(PPK[4] ^ _MK16(TK[11], TK[10])))\n\n PPK[0] = _CAST16(PPK[0] + _RotR1(PPK[5] ^ _MK16(TK[13], TK[12])))\n PPK[1] = _CAST16(PPK[1] + _RotR1(PPK[0] ^ _MK16(TK[15], TK[14])))\n PPK[2] = _CAST16(PPK[2] + _RotR1(PPK[1]))\n PPK[3] = _CAST16(PPK[3] + _RotR1(PPK[2]))\n PPK[4] = _CAST16(PPK[4] + _RotR1(PPK[3]))\n PPK[5] = _CAST16(PPK[5] + _RotR1(PPK[4]))\n\n # Phase 2 - Step 3\n WEPSeed = []\n WEPSeed.append(TSC[1])\n WEPSeed.append((TSC[1] | 0x20) & 0x7f)\n WEPSeed.append(TSC[0])\n WEPSeed.append(((PPK[5] ^ _MK16(TK[1], TK[0])) >> 1) & 0xFF)\n for i in range(6):\n WEPSeed.append(PPK[i] & 0xFF)\n WEPSeed.append(PPK[i] >> 8)\n\n assert len(WEPSeed) == 16\n\n return b\"\".join(chb(x) for x in WEPSeed)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _michael_b(m_l, m_r):\n m_r = m_r ^ _rotate_left32(m_l, 17)\n m_l = (m_l + m_r) % 2**32\n m_r = m_r ^ _XSWAP(m_l)\n m_l = (m_l + m_r) % 2**32\n m_r = m_r ^ _rotate_left32(m_l, 3)\n m_l = (m_l + m_r) % 2**32\n m_r = m_r ^ _rotate_right32(m_l, 2)\n m_l = (m_l + m_r) % 2**32\n return m_l, m_r", "response": "Defined in 802. 11i p. 49"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef michael(key, to_hash):\n\n # Block size: 4\n nb_block, nb_extra_bytes = divmod(len(to_hash), 4)\n # Add padding\n data = to_hash + chb(0x5a) + b\"\\x00\" * (7 - nb_extra_bytes)\n\n # Hash\n m_l, m_r = unpack('> 40) & 0xFF,\n (iv >> 32) & 0xFF,\n (iv >> 24) & 0xFF,\n (iv >> 16) & 0xFF,\n (iv >> 8) & 0xFF,\n iv & 0xFF\n )\n bitfield = 1 << 5 # Extended IV\n TKIP_hdr = chb(TSC1) + chb((TSC1 | 0x20) & 0x7f) + chb(TSC0) + chb(bitfield) # noqa: E501\n TKIP_hdr += chb(TSC2) + chb(TSC3) + chb(TSC4) + chb(TSC5)\n\n TA = [orb(e) for e in mac2str(mac)]\n TSC = [TSC0, TSC1, TSC2, TSC3, TSC4, TSC5]\n TK = [orb(x) for x in tk]\n\n rc4_key = gen_TKIP_RC4_key(TSC, TA, TK)\n return TKIP_hdr + ARC4_encrypt(rc4_key, data)", "response": "Build a TKIP payload for IV and MAC and encrypt it based on temporal key tk."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef parse_data_pkt(pkt, tk):\n TSC, TA, data = parse_TKIP_hdr(pkt)\n TK = [orb(x) for x in tk]\n\n rc4_key = gen_TKIP_RC4_key(TSC, TA, TK)\n return ARC4_decrypt(rc4_key, data)", "response": "Extract data from a WPA packet with temporal key tk"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nchecking MIC and ICV & return the data from a decrypted TKIP packet", "response": "def check_MIC_ICV(data, mic_key, source, dest):\n \"\"\"Check MIC, ICV & return the data from a decrypted TKIP packet\"\"\"\n assert len(data) > 12\n\n # DATA - MIC(DA - SA - Priority=0 - 0 - 0 - 0 - DATA) - ICV\n # 802.11i p.47\n\n ICV = data[-4:]\n MIC = data[-12:-4]\n data_clear = data[:-12]\n\n expected_ICV = pack(\" 0 or lastp.flags.MF: # first or last fragment missing\n missfrag.append(lst)\n return\n p = p.copy()\n if conf.padding_layer in p:\n del(p[conf.padding_layer].underlayer.payload)\n ip = p[IP]\n if ip.len is None or ip.ihl is None:\n clen = len(ip.payload)\n else:\n clen = ip.len - (ip.ihl << 2)\n txt = conf.raw_layer()\n for q in lst[1:]:\n if clen != q.frag << 3: # Wrong fragmentation offset\n if clen > q.frag << 3:\n warning(\"Fragment overlap (%i > %i) %r || %r || %r\" % (clen, q.frag << 3, p, txt, q)) # noqa: E501\n missfrag.append(lst)\n break\n if q[IP].len is None or q[IP].ihl is None:\n clen += len(q[IP].payload)\n else:\n clen += q[IP].len - (q[IP].ihl << 2)\n if conf.padding_layer in q:\n del(q[conf.padding_layer].underlayer.payload)\n txt.add_payload(q[IP].payload.copy())\n if q.time > p.time:\n p.time = q.time\n else:\n ip.flags.MF = False\n del(ip.chksum)\n del(ip.len)\n p = p / txt\n p._defrag_pos = max(x._defrag_pos for x in lst)\n defrag.append(p)", "response": "Internal usage only. Part of the _defrag_logic"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _defrag_logic(plist, complete=False):\n frags = defaultdict(lambda: [])\n final = []\n pos = 0\n for p in plist:\n p._defrag_pos = pos\n pos += 1\n if IP in p:\n ip = p[IP]\n if ip.frag != 0 or ip.flags.MF:\n uniq = (ip.id, ip.src, ip.dst, ip.proto)\n frags[uniq].append(p)\n continue\n final.append(p)\n\n defrag = []\n missfrag = []\n for lst in six.itervalues(frags):\n lst.sort(key=lambda x: x.frag)\n _defrag_list(lst, defrag, missfrag)\n defrag2 = []\n for p in defrag:\n q = p.__class__(raw(p))\n q._defrag_pos = p._defrag_pos\n q.time = p.time\n defrag2.append(q)\n if complete:\n final.extend(defrag2)\n final.extend(missfrag)\n final.sort(key=lambda x: x._defrag_pos)\n if hasattr(plist, \"listname\"):\n name = \"Defragmented %s\" % plist.listname\n else:\n name = \"Defragmented\"\n return PacketList(final, name=name)\n else:\n return PacketList(final), PacketList(defrag2), PacketList(missfrag)", "response": "Internal function used to defragment a list of packets."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _packetlist_timeskew_graph(self, ip, **kargs):\n\n # Filter TCP segments which source address is 'ip'\n tmp = (self._elt2pkt(x) for x in self.res)\n b = (x for x in tmp if IP in x and x[IP].src == ip and TCP in x)\n\n # Build a list of tuples (creation_time, replied_timestamp)\n c = []\n tsf = ICMPTimeStampField(\"\", None)\n for p in b:\n opts = p.getlayer(TCP).options\n for o in opts:\n if o[0] == \"Timestamp\":\n c.append((p.time, tsf.any2i(\"\", o[1][0])))\n\n # Stop if the list is empty\n if not c:\n warning(\"No timestamps found in packet list\")\n return []\n\n # Prepare the data that will be plotted\n first_creation_time = c[0][0]\n first_replied_timestamp = c[0][1]\n\n def _wrap_data(ts_tuple, wrap_seconds=2000):\n \"\"\"Wrap the list of tuples.\"\"\"\n\n ct, rt = ts_tuple # (creation_time, replied_timestamp)\n X = ct % wrap_seconds\n Y = ((ct - first_creation_time) - ((rt - first_replied_timestamp) / 1000.0)) # noqa: E501\n\n return X, Y\n\n data = [_wrap_data(e) for e in c]\n\n # Mimic the default gnuplot output\n if kargs == {}:\n kargs = MATPLOTLIB_DEFAULT_PLOT_KARGS\n lines = plt.plot(data, **kargs)\n\n # Call show() if matplotlib is not inlined\n if not MATPLOTLIB_INLINED:\n plt.show()\n\n return lines", "response": "Tries to plot the timeskew between the timestamps and real time for a given ip"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef traceroute(target, dport=80, minttl=1, maxttl=30, sport=RandShort(), l4=None, filter=None, timeout=2, verbose=None, **kargs): # noqa: E501\n if verbose is None:\n verbose = conf.verb\n if filter is None:\n # we only consider ICMP error packets and TCP packets with at\n # least the ACK flag set *and* either the SYN or the RST flag\n # set\n filter = \"(icmp and (icmp[0]=3 or icmp[0]=4 or icmp[0]=5 or icmp[0]=11 or icmp[0]=12)) or (tcp and (tcp[13] & 0x16 > 0x10))\" # noqa: E501\n if l4 is None:\n a, b = sr(IP(dst=target, id=RandShort(), ttl=(minttl, maxttl)) / TCP(seq=RandInt(), sport=sport, dport=dport), # noqa: E501\n timeout=timeout, filter=filter, verbose=verbose, **kargs)\n else:\n # this should always work\n filter = \"ip\"\n a, b = sr(IP(dst=target, id=RandShort(), ttl=(minttl, maxttl)) / l4,\n timeout=timeout, filter=filter, verbose=verbose, **kargs)\n\n a = TracerouteResult(a.res)\n if verbose:\n a.show()\n return a, b", "response": "Instant TCP traceroute\ntraceroute(target, [maxttl=30,] [dport=80,] [sport=80,] [verbose=conf.verb]) -> None # noqa: E501"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef report_ports(target, ports):\n ans, unans = sr(IP(dst=target) / TCP(dport=ports), timeout=5)\n rep = \"\\\\begin{tabular}{|r|l|l|}\\n\\\\hline\\n\"\n for s, r in ans:\n if not r.haslayer(ICMP):\n if r.payload.flags == 0x12:\n rep += r.sprintf(\"%TCP.sport% & open & SA \\\\\\\\\\n\")\n rep += \"\\\\hline\\n\"\n for s, r in ans:\n if r.haslayer(ICMP):\n rep += r.sprintf(\"%TCPerror.dport% & closed & ICMP type %ICMP.type%/%ICMP.code% from %IP.src% \\\\\\\\\\n\") # noqa: E501\n elif r.payload.flags != 0x12:\n rep += r.sprintf(\"%TCP.sport% & closed & TCP %TCP.flags% \\\\\\\\\\n\")\n rep += \"\\\\hline\\n\"\n for i in unans:\n rep += i.sprintf(\"%TCP.dport% & ? & unanswered \\\\\\\\\\n\")\n rep += \"\\\\hline\\n\\\\end{tabular}\\n\"\n return rep", "response": "portscan a target and output a LaTeX table\nreport_ports"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nidentifying IP id values classes in a list of packets lst a list of packets funcID a function that returns IP id values classes a list of packets lst a list of packets funcpres a function that returns IP id values classes a list of packets that return IP id values", "response": "def IPID_count(lst, funcID=lambda x: x[1].id, funcpres=lambda x: x[1].summary()): # noqa: E501\n \"\"\"Identify IP id values classes in a list of packets\n\nlst: a list of packets\nfuncID: a function that returns IP id values\nfuncpres: a function used to summarize packets\"\"\"\n idlst = [funcID(e) for e in lst]\n idlst.sort()\n classes = [idlst[0]]\n classes += [t[1] for t in zip(idlst[:-1], idlst[1:]) if abs(t[0] - t[1]) > 50] # noqa: E501\n lst = [(funcID(x), funcpres(x)) for x in lst]\n lst.sort()\n print(\"Probably %i classes:\" % len(classes), classes)\n for id, pr in lst:\n print(\"%5i\" % id, pr)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn ttl or hlim depending on the IP version", "response": "def _ttl(self):\n \"\"\"Returns ttl or hlim, depending on the IP version\"\"\"\n return self.hlim if isinstance(self, scapy.layers.inet6.IPv6) else self.ttl"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef trace3D(self, join=True):\n # When not ran from a notebook, vpython pooly closes itself\n # using os._exit once finished. We pack it into a Process\n import multiprocessing\n p = multiprocessing.Process(target=self.trace3D_notebook)\n p.start()\n if join:\n p.join()", "response": "Give a 3D representation of the traceroute."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef trace3D_notebook(self):\n trace = self.get_trace()\n import vpython\n\n class IPsphere(vpython.sphere):\n def __init__(self, ip, **kargs):\n vpython.sphere.__init__(self, **kargs)\n self.ip = ip\n self.label = None\n self.setlabel(self.ip)\n self.last_clicked = None\n self.full = False\n self.savcolor = vpython.vec(*self.color.value)\n\n def fullinfos(self):\n self.full = True\n self.color = vpython.vec(1, 0, 0)\n a, b = sr(IP(dst=self.ip) / TCP(dport=[21, 22, 23, 25, 80, 443], flags=\"S\"), timeout=2, verbose=0) # noqa: E501\n if len(a) == 0:\n txt = \"%s:\\nno results\" % self.ip\n else:\n txt = \"%s:\\n\" % self.ip\n for s, r in a:\n txt += r.sprintf(\"{TCP:%IP.src%:%TCP.sport% %TCP.flags%}{TCPerror:%IPerror.dst%:%TCPerror.dport% %IP.src% %ir,ICMP.type%}\\n\") # noqa: E501\n self.setlabel(txt, visible=1)\n\n def unfull(self):\n self.color = self.savcolor\n self.full = False\n self.setlabel(self.ip)\n\n def setlabel(self, txt, visible=None):\n if self.label is not None:\n if visible is None:\n visible = self.label.visible\n self.label.visible = 0\n elif visible is None:\n visible = 0\n self.label = vpython.label(text=txt, pos=self.pos, space=self.radius, xoffset=10, yoffset=20, visible=visible) # noqa: E501\n\n def check_double_click(self):\n try:\n if self.full or not self.label.visible:\n return False\n if self.last_clicked is not None:\n return (time.time() - self.last_clicked) < 0.5\n return False\n finally:\n self.last_clicked = time.time()\n\n def action(self):\n self.label.visible ^= 1\n if self.full:\n self.unfull()\n\n vpython.scene = vpython.canvas()\n vpython.scene.title = \"
%s
\" % self.listname # noqa: E501\n vpython.scene.append_to_caption(\n re.sub(\n r'\\%(.*)\\%',\n r'\\1',\n re.sub(\n r'\\`(.*)\\`',\n r'\\1',\n \"\"\"Commands:\n%Click% to toggle information about a node.\n%Double click% to perform a quick web scan on this node.\nCamera usage:\n`Right button drag or Ctrl-drag` to rotate \"camera\" to view scene.\n`Shift-drag` to move the object around.\n`Middle button or Alt-drag` to drag up or down to zoom in or out.\n On a two-button mouse, `middle is wheel or left + right`.\nTouch screen: pinch/extend to zoom, swipe or two-finger rotate.\"\"\"\n )\n )\n )\n vpython.scene.exit = True\n start = vpython.box()\n rings = {}\n tr3d = {}\n for i in trace:\n tr = trace[i]\n tr3d[i] = []\n for t in range(1, max(tr) + 1):\n if t not in rings:\n rings[t] = []\n if t in tr:\n if tr[t] not in rings[t]:\n rings[t].append(tr[t])\n tr3d[i].append(rings[t].index(tr[t]))\n else:\n rings[t].append((\"unk\", -1))\n tr3d[i].append(len(rings[t]) - 1)\n\n for t in rings:\n r = rings[t]\n tmp_len = len(r)\n for i in range(tmp_len):\n if r[i][1] == -1:\n col = vpython.vec(0.75, 0.75, 0.75)\n elif r[i][1]:\n col = vpython.color.green\n else:\n col = vpython.color.blue\n\n s = IPsphere(pos=vpython.vec((tmp_len - 1) * vpython.cos(2 * i * vpython.pi / tmp_len), (tmp_len - 1) * vpython.sin(2 * i * vpython.pi / tmp_len), 2 * t), # noqa: E501\n ip=r[i][0],\n color=col)\n for trlst in six.itervalues(tr3d):\n if t <= len(trlst):\n if trlst[t - 1] == i:\n trlst[t - 1] = s\n forecol = colgen(0.625, 0.4375, 0.25, 0.125)\n for trlst in six.itervalues(tr3d):\n col = vpython.vec(*next(forecol))\n start = vpython.vec(0, 0, 0)\n for ip in trlst:\n vpython.cylinder(pos=start, axis=ip.pos - start, color=col, radius=0.2) # noqa: E501\n start = ip.pos\n\n vpython.rate(50)\n\n # Keys handling\n # TODO: there is currently no way of closing vpython correctly\n # https://github.com/BruceSherwood/vpython-jupyter/issues/36\n # def keyboard_press(ev):\n # k = ev.key\n # if k == \"esc\" or k == \"q\":\n # pass # TODO: close\n #\n # vpython.scene.bind('keydown', keyboard_press)\n\n # Mouse handling\n def mouse_click(ev):\n if ev.press == \"left\":\n o = vpython.scene.mouse.pick\n if o and isinstance(o, IPsphere):\n if o.check_double_click():\n if o.ip == \"unk\":\n return\n o.fullinfos()\n else:\n o.action()\n\n vpython.scene.bind('mousedown', mouse_click)", "response": "Same than trace3D_notebook but used when ran from Jupyther notebooks"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ndisplays traceroute results on a world map.", "response": "def world_trace(self):\n \"\"\"Display traceroute results on a world map.\"\"\"\n\n # Check that the geoip2 module can be imported\n # Doc: http://geoip2.readthedocs.io/en/latest/\n try:\n # GeoIP2 modules need to be imported as below\n import geoip2.database\n import geoip2.errors\n except ImportError:\n warning(\"Cannot import geoip2. Won't be able to plot the world.\")\n return []\n # Check availability of database\n if not conf.geoip_city:\n warning(\"Cannot import the geolite2 CITY database.\\n\"\n \"Download it from http://dev.maxmind.com/geoip/geoip2/geolite2/\" # noqa: E501\n \" then set its path to conf.geoip_city\")\n return []\n # Check availability of plotting devices\n try:\n import cartopy.crs as ccrs\n except ImportError:\n warning(\"Cannot import cartopy.\\n\"\n \"More infos on http://scitools.org.uk/cartopy/docs/latest/installing.html\") # noqa: E501\n return []\n if not MATPLOTLIB:\n warning(\"Matplotlib is not installed. Won't be able to plot the world.\") # noqa: E501\n return []\n\n # Open & read the GeoListIP2 database\n try:\n db = geoip2.database.Reader(conf.geoip_city)\n except Exception:\n warning(\"Cannot open geoip2 database at %s\", conf.geoip_city)\n return []\n\n # Regroup results per trace\n ips = {}\n rt = {}\n ports_done = {}\n for s, r in self.res:\n ips[r.src] = None\n if s.haslayer(TCP) or s.haslayer(UDP):\n trace_id = (s.src, s.dst, s.proto, s.dport)\n elif s.haslayer(ICMP):\n trace_id = (s.src, s.dst, s.proto, s.type)\n else:\n trace_id = (s.src, s.dst, s.proto, 0)\n trace = rt.get(trace_id, {})\n if not r.haslayer(ICMP) or r.type != 11:\n if trace_id in ports_done:\n continue\n ports_done[trace_id] = None\n trace[s.ttl] = r.src\n rt[trace_id] = trace\n\n # Get the addresses locations\n trt = {}\n for trace_id in rt:\n trace = rt[trace_id]\n loctrace = []\n for i in range(max(trace)):\n ip = trace.get(i, None)\n if ip is None:\n continue\n # Fetch database\n try:\n sresult = db.city(ip)\n except geoip2.errors.AddressNotFoundError:\n continue\n loctrace.append((sresult.location.longitude, sresult.location.latitude)) # noqa: E501\n if loctrace:\n trt[trace_id] = loctrace\n\n # Load the map renderer\n plt.figure(num='Scapy')\n ax = plt.axes(projection=ccrs.PlateCarree())\n # Draw countries\n ax.coastlines()\n ax.stock_img()\n # Set normal size\n ax.set_global()\n # Add title\n plt.title(\"Scapy traceroute results\")\n\n from matplotlib.collections import LineCollection\n from matplotlib import colors as mcolors\n colors_cycle = iter(mcolors.BASE_COLORS)\n lines = []\n\n # Split traceroute measurement\n for key, trc in six.iteritems(trt):\n # Get next color\n color = next(colors_cycle)\n # Gather mesurments data\n data_lines = [(trc[i], trc[i + 1]) for i in range(len(trc) - 1)]\n # Create line collection\n line_col = LineCollection(data_lines, linewidths=2,\n label=key[1],\n color=color)\n lines.append(line_col)\n ax.add_collection(line_col)\n # Create map points\n lines.extend([ax.plot(*x, marker='.', color=color) for x in trc])\n\n # Generate legend\n ax.legend()\n\n # Call show() if matplotlib is not inlined\n if not MATPLOTLIB_INLINED:\n plt.show()\n\n # Clean\n ax.remove()\n\n # Return the drawn lines\n return lines"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef m2i(self, pkt, s):\n diff_tag, s = BER_tagging_dec(s, hidden_tag=self.ASN1_tag,\n implicit_tag=self.implicit_tag,\n explicit_tag=self.explicit_tag,\n safe=self.flexible_tag)\n if diff_tag is not None:\n # this implies that flexible_tag was True\n if self.implicit_tag is not None:\n self.implicit_tag = diff_tag\n elif self.explicit_tag is not None:\n self.explicit_tag = diff_tag\n codec = self.ASN1_tag.get_codec(pkt.ASN1_codec)\n if self.flexible_tag:\n return codec.safedec(s, context=self.context)\n else:\n return codec.dec(s, context=self.context)", "response": "Decode a string into a single object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef m2i(self, pkt, s):\n if len(s) == 0:\n raise ASN1_Error(\"ASN1F_CHOICE: got empty string\")\n _, s = BER_tagging_dec(s, hidden_tag=self.ASN1_tag,\n explicit_tag=self.explicit_tag)\n tag, _ = BER_id_dec(s)\n if tag not in self.choices:\n if self.flexible_tag:\n choice = ASN1F_field\n else:\n raise ASN1_Error(\"ASN1F_CHOICE: unexpected field\")\n else:\n choice = self.choices[tag]\n if hasattr(choice, \"ASN1_root\"):\n # we don't want to import ASN1_Packet in this module...\n return self.extract_packet(choice, s)\n elif isinstance(choice, type):\n # XXX find a way not to instantiate the ASN1F_field\n return choice(self.name, b\"\").m2i(pkt, s)\n else:\n # XXX check properly if this is an ASN1F_PACKET\n return choice.m2i(pkt, s)", "response": "This method will take a string and return the corresponding field or packet."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef igmpize(self):\n gaddr = self.gaddr if hasattr(self, \"gaddr\") and self.gaddr else \"0.0.0.0\" # noqa: E501\n underlayer = self.underlayer\n if self.type not in [0x11, 0x30]: # General Rule 1 # noqa: E501\n self.mrcode = 0\n if isinstance(underlayer, IP):\n if (self.type == 0x11):\n if (gaddr == \"0.0.0.0\"):\n underlayer.dst = \"224.0.0.1\" # IP rule 1 # noqa: E501\n elif isValidMCAddr(gaddr):\n underlayer.dst = gaddr # IP rule 3a # noqa: E501\n else:\n warning(\"Invalid IGMP Group Address detected !\")\n return False\n elif ((self.type == 0x17) and isValidMCAddr(gaddr)):\n underlayer.dst = \"224.0.0.2\" # IP rule 2 # noqa: E501\n elif ((self.type == 0x12) or (self.type == 0x16)) and (isValidMCAddr(gaddr)): # noqa: E501\n underlayer.dst = gaddr # IP rule 3b # noqa: E501\n else:\n warning(\"Invalid IGMP Type detected !\")\n return False\n if not any(isinstance(x, IPOption_Router_Alert) for x in underlayer.options): # noqa: E501\n underlayer.options.append(IPOption_Router_Alert())\n underlayer.ttl = 1 # IP rule 4\n _root = self.firstlayer()\n if _root.haslayer(Ether):\n # Force recalculate Ether dst\n _root[Ether].dst = getmacbyip(underlayer.dst) # Ether rule 1 # noqa: E501\n from scapy.contrib.igmpv3 import IGMPv3\n if isinstance(self, IGMPv3):\n self.encode_maxrespcode()\n return True", "response": "This function will attempt to fixup the IGMP message according to the RFC 2236 section 2. 1."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _bgp_dispatcher(payload):\n\n cls = conf.raw_layer\n\n # By default, calling BGP() will build a BGPHeader.\n if payload is None:\n cls = _get_cls(\"BGPHeader\", conf.raw_layer)\n\n else:\n if len(payload) >= _BGP_HEADER_SIZE and\\\n payload[:16] == _BGP_HEADER_MARKER:\n\n # Get BGP message type\n message_type = orb(payload[18])\n if message_type == 4:\n cls = _get_cls(\"BGPKeepAlive\")\n else:\n cls = _get_cls(\"BGPHeader\")\n\n return cls", "response": "Returns the right class for a given BGP message."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _bgp_capability_dispatcher(payload):\n\n cls = _capabilities_registry[\"BGPCapGeneric\"]\n\n # By default, calling BGPCapability() will build a \"generic\" capability.\n if payload is None:\n cls = _capabilities_registry[\"BGPCapGeneric\"]\n\n else:\n length = len(payload)\n if length >= _BGP_CAPABILITY_MIN_SIZE:\n code = orb(payload[0])\n cls = _get_cls(_capabilities_objects.get(code, \"BGPCapGeneric\"))\n\n return cls", "response": "Returns the right class for a given BGP capability."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef i2m(self, pkt, i):\n mask, ip = i\n ip = socket.inet_aton(ip)\n return struct.pack(\">B\", mask) + ip[:self.mask2iplen(mask)]", "response": "Internal IP as bytes mask as int to machine name representation."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef i2m(self, pkt, i):\n mask, ip = i\n ip = pton_ntop.inet_pton(socket.AF_INET6, ip)\n return struct.pack(\">B\", mask) + ip[:self.mask2iplen(mask)]", "response": "Internal representation of the internal entry."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncheck that the payload is long enough.", "response": "def pre_dissect(self, s):\n \"\"\"\n Check that the payload is long enough (at least 2 bytes).\n \"\"\"\n length = len(s)\n if length < _BGP_CAPABILITY_MIN_SIZE:\n err = \" ({}\".format(length) + \" is < _BGP_CAPABILITY_MIN_SIZE \"\n err += \"({})).\".format(_BGP_CAPABILITY_MIN_SIZE)\n raise _BGPInvalidDataException(err)\n return s"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef post_build(self, pkt, pay):\n if self.length is None:\n pkt = pkt[:4] + chb(len(pay)) + pkt[5:]\n return pkt + pay", "response": "This method is called when the packet is built and the pay is added to the packet."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef guess_payload_class(self, payload):\n t = (orb(payload[0]) & 0xf0) >> 4\n if t == 0:\n return ISOTP_SF\n elif t == 1:\n return ISOTP_FF\n elif t == 2:\n return ISOTP_CF\n else:\n return ISOTP_FC", "response": "Guesses the ISOTP class from the payload."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef feed(self, can):\n if not isinstance(can, CAN):\n raise Scapy_Exception(\"argument is not a CAN frame\")\n identifier = can.identifier\n data = bytes(can.data)\n\n if len(data) > 1 and self.use_ext_addr is not True:\n self._try_feed(identifier, None, data)\n if len(data) > 2 and self.use_ext_addr is not False:\n ea = six.indexbytes(data, 0)\n self._try_feed(identifier, ea, data[1:])", "response": "Attempt to feed an incoming CAN frame into the state machine."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef pop(self, identifier=None, ext_addr=None, basecls=ISOTP):\n\n if identifier is not None:\n for i in range(len(self.ready)):\n b = self.ready[i]\n identifier = b[0]\n ea = b[1]\n if identifier == identifier and ext_addr == ea:\n return ISOTPMessageBuilder._build(self.ready.pop(i))\n return None\n\n if len(self.ready) > 0:\n return ISOTPMessageBuilder._build(self.ready.pop(0))\n return None", "response": "Returns an ISOTP message with the specified identifier and extended address."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef begin_send(self, p):\n if hasattr(p, \"sent_time\"):\n p.sent_time = time.time()\n\n return self.outs.begin_send(bytes(p))", "response": "Begin the transmission of a message p. This method returns after the message has been sent."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef recv_with_timeout(self, timeout=1):\n msg = self.ins.recv(timeout)\n t = time.time()\n if msg is None:\n raise Scapy_Exception(\"Timeout\")\n return self.basecls, msg, t", "response": "Receive a complete ISOTP message blocking until a message is received or the specified timeout is reached."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreceiving a complete ISOTP message.", "response": "def recv_raw(self, x=0xffff):\n \"\"\"Receive a complete ISOTP message, blocking until a message is\n received or the specified timeout is reached.\n If self.timeout is 0, then this function doesn't block and returns the\n first frame in the receive buffer or None if there isn't any.\"\"\"\n msg = self.ins.recv()\n t = time.time()\n return self.basecls, msg, t"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef select(sockets, remain=None):\n blocking = remain is None or remain > 0\n\n def find_ready_sockets():\n return list(filter(lambda x: not x.ins.rx_queue.empty(), sockets))\n\n ready_sockets = find_ready_sockets()\n if len(ready_sockets) > 0 or not blocking:\n return ready_sockets, None\n\n exit_select = Event()\n\n def my_cb(msg):\n exit_select.set()\n\n try:\n for s in sockets:\n s.ins.rx_callbacks.append(my_cb)\n\n exit_select.wait(remain)\n\n finally:\n for s in sockets:\n try:\n s.ins.rx_callbacks.remove(my_cb)\n except ValueError:\n pass\n\n ready_sockets = find_ready_sockets()\n return ready_sockets, None", "response": "This function is called during sendrecv routine to wait for a set of sockets to be ready to receive a new entry."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef set_timeout(self, timeout, callback):\n if not self._ready_sem.acquire(False):\n raise Scapy_Exception(\"Timer was already started\")\n\n self._callback = callback\n self._timeout = timeout\n self._cancelled.clear()\n self._busy_sem.release()", "response": "Call callback in timeout seconds unless cancelled."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nstop the timer without executing the callback.", "response": "def cancel(self):\n \"\"\"Stop the timer without executing the callback.\"\"\"\n self._cancelled.set()\n if not self._dead:\n self._ready_sem.acquire()\n self._ready_sem.release()"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef stop(self):\n if not self._dead:\n self._killed = True\n self._cancelled.set()\n self._busy_sem.release()\n self.join()\n if not self._ready_sem.acquire(False):\n warning(\"ISOTP Timer thread may not have stopped \"\n \"correctly\")", "response": "Stop the thread making this object unusable."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nfunctions that must be called every time a CAN frame is received. to is called every time a CAN frame is received. to is advance the state machine.", "response": "def on_recv(self, cf):\n \"\"\"Function that must be called every time a CAN frame is received, to\n advance the state machine.\"\"\"\n\n data = bytes(cf.data)\n\n if len(data) < 2:\n return\n\n ae = 0\n if self.extended_rx_addr is not None:\n ae = 1\n if len(data) < 3:\n return\n if six.indexbytes(data, 0) != self.extended_rx_addr:\n return\n\n n_pci = six.indexbytes(data, ae) & 0xf0\n\n if n_pci == N_PCI_FC:\n with self.tx_mutex:\n self._recv_fc(data[ae:])\n elif n_pci == N_PCI_SF:\n with self.rx_mutex:\n self._recv_sf(data[ae:])\n elif n_pci == N_PCI_FF:\n with self.rx_mutex:\n self._recv_ff(data[ae:])\n elif n_pci == N_PCI_CF:\n with self.rx_mutex:\n self._recv_cf(data[ae:])"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _recv_fc(self, data):\n if (self.tx_state != ISOTP_WAIT_FC and\n self.tx_state != ISOTP_WAIT_FIRST_FC):\n return 0\n\n self.tx_timer.cancel()\n\n if len(data) < 3:\n self.tx_state = ISOTP_IDLE\n self.tx_exception = \"CF frame discarded because it was too short\"\n self.tx_done.set()\n raise Scapy_Exception(self.tx_exception)\n\n # get communication parameters only from the first FC frame\n if self.tx_state == ISOTP_WAIT_FIRST_FC:\n self.txfc_bs = six.indexbytes(data, 1)\n self.txfc_stmin = six.indexbytes(data, 2)\n\n if ((self.txfc_stmin > 0x7F) and\n ((self.txfc_stmin < 0xF1) or (self.txfc_stmin > 0xF9))):\n self.txfc_stmin = 0x7F\n\n if six.indexbytes(data, 2) <= 127:\n tx_gap = six.indexbytes(data, 2) / 1000.0\n elif 0xf1 <= six.indexbytes(data, 2) <= 0xf9:\n tx_gap = (six.indexbytes(data, 2) & 0x0f) / 10000.0\n else:\n tx_gap = 0\n self.tx_gap = tx_gap\n\n self.tx_state = ISOTP_WAIT_FC\n\n isotp_fc = six.indexbytes(data, 0) & 0x0f\n\n if isotp_fc == ISOTP_FC_CTS:\n self.tx_bs = 0\n self.tx_state = ISOTP_SENDING\n # start cyclic timer for sending CF frame\n self.tx_timer.set_timeout(self.tx_gap, self._tx_timer_handler)\n elif isotp_fc == ISOTP_FC_WT:\n # start timer to wait for next FC frame\n self.tx_state = ISOTP_WAIT_FC\n self.tx_timer.set_timeout(self.fc_timeout, self._tx_timer_handler)\n elif isotp_fc == ISOTP_FC_OVFLW:\n # overflow in receiver side\n self.tx_state = ISOTP_IDLE\n self.tx_exception = \"Overflow happened at the receiver side\"\n self.tx_done.set()\n raise Scapy_Exception(self.tx_exception)\n else:\n self.tx_state = ISOTP_IDLE\n self.tx_exception = \"Unknown FC frame type\"\n self.tx_done.set()\n raise Scapy_Exception(self.tx_exception)\n\n return 0", "response": "Process a received Flow Control frame and return the corresponding ISO - 8601 version of the data."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _recv_sf(self, data):\n self.rx_timer.cancel()\n if self.rx_state != ISOTP_IDLE:\n warning(\"RX state was reset because single frame was received\")\n self.rx_state = ISOTP_IDLE\n\n length = six.indexbytes(data, 0) & 0xf\n if len(data) - 1 < length:\n return 1\n\n msg = data[1:1 + length]\n self.rx_queue.put(msg)\n for cb in self.rx_callbacks:\n cb(msg)\n self.call_release()\n return 0", "response": "Process a single frame from the broker."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _recv_ff(self, data):\n self.rx_timer.cancel()\n if self.rx_state != ISOTP_IDLE:\n warning(\"RX state was reset because first frame was received\")\n self.rx_state = ISOTP_IDLE\n\n if len(data) < 7:\n return 1\n self.rx_ll_dl = len(data)\n\n # get the FF_DL\n self.rx_len = (six.indexbytes(data, 0) & 0x0f) * 256 + six.indexbytes(\n data, 1)\n ff_pci_sz = 2\n\n # Check for FF_DL escape sequence supporting 32 bit PDU length\n if self.rx_len == 0:\n # FF_DL = 0 => get real length from next 4 bytes\n self.rx_len = six.indexbytes(data, 2) << 24\n self.rx_len += six.indexbytes(data, 3) << 16\n self.rx_len += six.indexbytes(data, 4) << 8\n self.rx_len += six.indexbytes(data, 5)\n ff_pci_sz = 6\n\n # copy the first received data bytes\n data_bytes = data[ff_pci_sz:]\n self.rx_idx = len(data_bytes)\n self.rx_buf = data_bytes\n\n # initial setup for this pdu reception\n self.rx_sn = 1\n self.rx_state = ISOTP_WAIT_DATA\n\n # no creation of flow control frames\n if not self.listen_mode:\n # send our first FC frame\n load = self.ea_hdr\n load += struct.pack(\"BBB\", N_PCI_FC, self.rxfc_bs, self.rxfc_stmin)\n self.can_send(load)\n\n # wait for a CF\n self.rx_bs = 0\n self.rx_timer.set_timeout(self.cf_timeout, self._rx_timer_handler)\n\n return 0", "response": "Process a first Frame frame from the packet."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nprocess a received Consecutive Frame frame and return the next CF.", "response": "def _recv_cf(self, data):\n \"\"\"Process a received 'Consecutive Frame' frame\"\"\"\n if self.rx_state != ISOTP_WAIT_DATA:\n return 0\n\n self.rx_timer.cancel()\n\n # CFs are never longer than the FF\n if len(data) > self.rx_ll_dl:\n return 1\n\n # CFs have usually the LL_DL length\n if len(data) < self.rx_ll_dl:\n # this is only allowed for the last CF\n if self.rx_len - self.rx_idx > self.rx_ll_dl:\n warning(\"Received a CF with insuffifient length\")\n return 1\n\n if six.indexbytes(data, 0) & 0x0f != self.rx_sn:\n # Wrong sequence number\n warning(\"RX state was reset because wrong sequence number was \"\n \"received\")\n self.rx_state = ISOTP_IDLE\n return 1\n\n self.rx_sn = (self.rx_sn + 1) % 16\n self.rx_buf += data[1:]\n self.rx_idx = len(self.rx_buf)\n\n if self.rx_idx >= self.rx_len:\n # we are done\n self.rx_buf = self.rx_buf[0:self.rx_len]\n self.rx_state = ISOTP_IDLE\n self.rx_queue.put(self.rx_buf)\n for cb in self.rx_callbacks:\n cb(self.rx_buf)\n self.call_release()\n self.rx_buf = None\n return 0\n\n # perform blocksize handling, if enabled\n if self.rxfc_bs != 0:\n self.rx_bs += 1\n\n # check if we reached the end of the block\n if self.rx_bs >= self.rxfc_bs and not self.listen_mode:\n # send our FC frame\n load = self.ea_hdr\n load += struct.pack(\"BBB\", N_PCI_FC, self.rxfc_bs,\n self.rxfc_stmin)\n self.can_send(load)\n\n # wait for another CF\n self.rx_timer.set_timeout(self.cf_timeout, self._rx_timer_handler)\n return 0"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef begin_send(self, x):\n with self.tx_mutex:\n if self.tx_state != ISOTP_IDLE:\n raise Scapy_Exception(\"Socket is already sending, retry later\")\n\n self.tx_done.clear()\n self.tx_exception = None\n self.tx_state = ISOTP_SENDING\n\n length = len(x)\n if length > ISOTP_MAX_DLEN_2015:\n raise Scapy_Exception(\"Too much data for ISOTP message\")\n\n if len(self.ea_hdr) + length <= 7:\n # send a single frame\n data = self.ea_hdr\n data += struct.pack(\"B\", length)\n data += x\n self.tx_state = ISOTP_IDLE\n self.can_send(data)\n self.tx_done.set()\n for cb in self.tx_callbacks:\n cb()\n return\n\n # send the first frame\n data = self.ea_hdr\n if length > ISOTP_MAX_DLEN:\n data += struct.pack(\">HI\", 0x1000, length)\n else:\n data += struct.pack(\">H\", 0x1000 | length)\n load = x[0:8 - len(data)]\n data += load\n self.can_send(data)\n\n self.tx_buf = x\n self.tx_sn = 1\n self.tx_bs = 0\n self.tx_idx = len(load)\n\n self.tx_state = ISOTP_WAIT_FIRST_FC\n self.tx_timer.set_timeout(self.fc_timeout, self._tx_timer_handler)", "response": "Starts sending an ISOTP message. This method does not block."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef send(self, p):\n with self.send_mutex:\n self.begin_send(p)\n\n # Wait until the tx callback is called\n self.tx_done.wait()\n if self.tx_exception is not None:\n raise Scapy_Exception(self.tx_exception)\n return", "response": "Send an ISOTP frame and block until the message is sent or an error occurs."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef recv(self, timeout=None):\n\n try:\n return self.rx_queue.get(timeout is None or timeout > 0, timeout)\n except queue.Empty:\n return None", "response": "Receive an ISOTP frame blocking if none is available in the buffer\n for at most timeout seconds."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef should_add_ClientCertificate(self):\n hs_msg = [type(m) for m in self.cur_session.handshake_messages_parsed]\n if TLSCertificateRequest not in hs_msg:\n return\n certs = []\n if self.mycert:\n certs = [self.mycert]\n self.add_msg(TLSCertificate(certs=certs))\n raise self.ADDED_CLIENTCERTIFICATE()", "response": "Check if we should add a client certificate."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef should_add_ClientVerify(self):\n hs_msg = [type(m) for m in self.cur_session.handshake_messages_parsed]\n if (TLSCertificateRequest not in hs_msg or\n self.mycert is None or\n self.mykey is None):\n return\n self.add_msg(TLSCertificateVerify())\n raise self.ADDED_CERTIFICATEVERIFY()", "response": "This method is used to add a CertificateVerify message to the current session."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_ClientData(self):\n if not self.data_to_send:\n data = six.moves.input().replace('\\\\r', '\\r').replace('\\\\n', '\\n').encode() # noqa: E501\n else:\n data = self.data_to_send.pop()\n if data == b\"quit\":\n return\n if self.linebreak:\n data += b\"\\n\"\n self.add_record()\n self.add_msg(TLSApplicationData(data=data))\n raise self.ADDED_CLIENTDATA()", "response": "Add client data to the message buffer."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef dispatch_hook(cls, _pkt=None, *args, **kargs):\n\n cls = conf.raw_layer\n if _pkt is not None:\n ptype = orb(_pkt[0])\n return globals().get(_param_set_cls.get(ptype), conf.raw_layer)\n\n return cls", "response": "Dispatches the class to get the right parameter set class."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef source_addr_mode2(pkt):\n if pkt.sac == 0x0:\n if pkt.sam == 0x0:\n return 16\n elif pkt.sam == 0x1:\n return 8\n elif pkt.sam == 0x2:\n return 2\n elif pkt.sam == 0x3:\n return 0\n else:\n if pkt.sam == 0x0:\n return 0\n elif pkt.sam == 0x1:\n return 8\n elif pkt.sam == 0x2:\n return 2\n elif pkt.sam == 0x3:\n return 0", "response": "source_addr_mode2 - Returns the amount of bits that are used by the source address."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef sixlowpan_fragment(packet, datagram_tag=1):\n if not packet.haslayer(IPv6):\n raise Exception(\"SixLoWPAN only fragments IPv6 packets !\")\n\n str_packet = raw(packet[IPv6])\n\n if len(str_packet) <= MAX_SIZE:\n return [packet]\n\n def chunks(l, n):\n return [l[i:i + n] for i in range(0, len(l), n)]\n\n new_packet = chunks(str_packet, MAX_SIZE)\n\n new_packet[0] = LoWPANFragmentationFirst(datagramTag=datagram_tag, datagramSize=len(str_packet)) / new_packet[0] # noqa: E501\n i = 1\n while i < len(new_packet):\n new_packet[i] = LoWPANFragmentationSubsequent(datagramTag=datagram_tag, datagramSize=len(str_packet), datagramOffset=MAX_SIZE // 8 * i) / new_packet[i] # noqa: E501\n i += 1\n\n return new_packet", "response": "Split a packet into different links to transmit as 6lowpan packets."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nadd an internal value to a string", "response": "def addfield(self, pkt, s, val):\n \"\"\"Add an internal value to a string\"\"\"\n tmp_len = self.length_of(pkt)\n if tmp_len == 0:\n return s\n internal = self.i2m(pkt, val)[-tmp_len:]\n return s + struct.pack(\"!%ds\" % tmp_len, internal)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd an internal value to a string", "response": "def addfield(self, pkt, s, val):\n \"\"\"Add an internal value to a string\"\"\"\n if self.length_of(pkt) == 8:\n return s + struct.pack(self.fmt[0] + \"B\", val)\n if self.length_of(pkt) == 16:\n return s + struct.pack(self.fmt[0] + \"H\", val)\n if self.length_of(pkt) == 32:\n return s + struct.pack(self.fmt[0] + \"2H\", val) # TODO: fix!\n if self.length_of(pkt) == 48:\n return s + struct.pack(self.fmt[0] + \"3H\", val) # TODO: fix!\n elif self.length_of(pkt) == 64:\n return s + struct.pack(self.fmt[0] + \"Q\", val)\n elif self.length_of(pkt) == 128:\n # TODO: FIX THE PACKING!!\n return s + struct.pack(self.fmt[0] + \"16s\", raw(val))\n else:\n return s"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef post_dissect(self, data):\n\n # uncompress payload\n packet = IPv6()\n packet.version = IPHC_DEFAULT_VERSION\n packet.tc, packet.fl = self._getTrafficClassAndFlowLabel()\n if not self.nh:\n packet.nh = self._nhField\n # HLIM: Hop Limit\n if self.hlim == 0:\n packet.hlim = self._hopLimit\n elif self.hlim == 0x1:\n packet.hlim = 1\n elif self.hlim == 0x2:\n packet.hlim = 64\n else:\n packet.hlim = 255\n # TODO: Payload length can be inferred from lower layers from either the # noqa: E501\n # 6LoWPAN Fragmentation header or the IEEE802.15.4 header\n\n packet.src = self.decompressSourceAddr(packet)\n packet.dst = self.decompressDestinyAddr(packet)\n\n if self.nh == 1:\n # The Next Header field is compressed and the next header is\n # encoded using LOWPAN_NHC\n\n packet.nh = 0x11 # UDP\n udp = UDP()\n if self.header_compression and \\\n self.header_compression & 0x4 == 0x0:\n udp.chksum = self.udpChecksum\n\n s, d = nhc_port(self)\n if s == 16:\n udp.sport = self.udpSourcePort\n elif s == 8:\n udp.sport = 0xF000 + s\n elif s == 4:\n udp.sport = 0xF0B0 + s\n if d == 16:\n udp.dport = self.udpDestinyPort\n elif d == 8:\n udp.dport = 0xF000 + d\n elif d == 4:\n udp.dport = 0xF0B0 + d\n\n packet.payload = udp / data\n data = raw(packet)\n # else self.nh == 0 not necessary\n elif self._nhField & 0xE0 == 0xE0: # IPv6 Extension Header Decompression # noqa: E501\n warning('Unimplemented: IPv6 Extension Header decompression') # noqa: E501\n packet.payload = conf.raw_layer(data)\n data = raw(packet)\n else:\n packet.payload = conf.raw_layer(data)\n data = raw(packet)\n\n return Packet.post_dissect(self, data)", "response": "This function is called by IPHC to dissect the IPv6 packet. It is called after the packet is decompressed and the payload needs to be decompressed and depending on the arguments."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\npage 6 draft feb 2011", "response": "def _getTrafficClassAndFlowLabel(self):\n \"\"\"Page 6, draft feb 2011 \"\"\"\n if self.tf == 0x0:\n return (self.tc_ecn << 6) + self.tc_dscp, self.flowlabel\n elif self.tf == 0x1:\n return (self.tc_ecn << 6), self.flowlabel\n elif self.tf == 0x2:\n return (self.tc_ecn << 6) + self.tc_dscp, 0\n else:\n return 0, 0"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ndepending on the payload content, the frame type we should interpretate", "response": "def dispatch_hook(cls, _pkt=b\"\", *args, **kargs):\n \"\"\"Depending on the payload content, the frame type we should interpretate\"\"\" # noqa: E501\n if _pkt and len(_pkt) >= 1:\n if orb(_pkt[0]) == 0x41:\n return LoWPANUncompressedIPv6\n if orb(_pkt[0]) == 0x42:\n return LoWPAN_HC1\n if orb(_pkt[0]) >> 3 == 0x18:\n return LoWPANFragmentationFirst\n elif orb(_pkt[0]) >> 3 == 0x1C:\n return LoWPANFragmentationSubsequent\n elif orb(_pkt[0]) >> 6 == 0x02:\n return LoWPANMesh\n elif orb(_pkt[0]) >> 6 == 0x01:\n return LoWPAN_IPHC\n return cls"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_issuer_str(self):\n name_str = \"\"\n attrsDict = self.get_issuer()\n for attrType, attrSymbol in _attrName_mapping:\n if attrType in attrsDict:\n name_str += \"/\" + attrSymbol + \"=\"\n name_str += attrsDict[attrType]\n for attrType in sorted(attrsDict):\n if attrType not in _attrName_specials:\n name_str += \"/\" + attrType + \"=\"\n name_str += attrsDict[attrType]\n return name_str", "response": "Returns a one - line string containing every type and value of the user s issuer."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef on_packet_received(self, pkt):\n if not pkt:\n return\n if self.store:\n self.lst.append(pkt)\n if self.prn:\n result = self.prn(pkt)\n if result is not None:\n print(result)", "response": "Called by the filter handler when a packet is received."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nbinds 2 layers for dissection.", "response": "def bind_bottom_up(lower, upper, __fval=None, **fval):\n \"\"\"Bind 2 layers for dissection.\n The upper layer will be chosen for dissection on top of the lower layer, if\n ALL the passed arguments are validated. If multiple calls are made with the same # noqa: E501\n layers, the last one will be used as default.\n\n ex:\n >>> bind_bottom_up(Ether, SNAP, type=0x1234)\n >>> Ether(b'\\xff\\xff\\xff\\xff\\xff\\xff\\xd0P\\x99V\\xdd\\xf9\\x124\\x00\\x00\\x00\\x00\\x00') # noqa: E501\n > # noqa: E501\n \"\"\"\n if __fval is not None:\n fval.update(__fval)\n lower.payload_guess = lower.payload_guess[:]\n lower.payload_guess.append((fval, upper))"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef bind_top_down(lower, upper, __fval=None, **fval):\n if __fval is not None:\n fval.update(__fval)\n upper._overload_fields = upper._overload_fields.copy()\n upper._overload_fields[lower] = fval", "response": "Bind 2 layers for building."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef bind_layers(lower, upper, __fval=None, **fval):\n if __fval is not None:\n fval.update(__fval)\n bind_top_down(lower, upper, **fval)\n bind_bottom_up(lower, upper, **fval)", "response": "Bind 2 layers on some specific fields values. It makes the packet being built when the arguments are present."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef split_layers(lower, upper, __fval=None, **fval):\n if __fval is not None:\n fval.update(__fval)\n split_bottom_up(lower, upper, **fval)\n split_top_down(lower, upper, **fval)", "response": "Split 2 layers previously bound."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfunctioning used to explore the Scapy layers and protocols.", "response": "def explore(layer=None):\n \"\"\"Function used to discover the Scapy layers and protocols.\n It helps to see which packets exists in contrib or layer files.\n\n params:\n - layer: If specified, the function will explore the layer. If not,\n the GUI mode will be activated, to browse the available layers\n\n examples:\n >>> explore() # Launches the GUI\n >>> explore(\"dns\") # Explore scapy.layers.dns\n >>> explore(\"http2\") # Explore scapy.contrib.http2\n >>> explore(scapy.layers.bluetooth4LE)\n\n Note: to search a packet by name, use ls(\"name\") rather than explore.\n \"\"\"\n if layer is None: # GUI MODE\n if not conf.interactive:\n raise Scapy_Exception(\"explore() GUI-mode cannot be run in \"\n \"interactive mode. Please provide a \"\n \"'layer' parameter !\")\n # 0 - Imports\n try:\n import prompt_toolkit\n except ImportError:\n raise ImportError(\"prompt_toolkit is not installed ! \"\n \"You may install IPython, which contains it, via\"\n \" `pip install ipython`\")\n if not _version_checker(prompt_toolkit, (2, 0)):\n raise ImportError(\"prompt_toolkit >= 2.0.0 is required !\")\n # Only available with prompt_toolkit > 2.0, not released on PyPi yet\n from prompt_toolkit.shortcuts.dialogs import radiolist_dialog, \\\n button_dialog\n from prompt_toolkit.formatted_text import HTML\n # 1 - Ask for layer or contrib\n action = button_dialog(\n title=\"Scapy v%s\" % conf.version,\n text=HTML(\n six.text_type(\n ''\n )\n ),\n buttons=[\n (six.text_type(\"Layers\"), \"layers\"),\n (six.text_type(\"Contribs\"), \"contribs\"),\n (six.text_type(\"Cancel\"), \"cancel\")\n ])\n # 2 - Retrieve list of Packets\n if action == \"layers\":\n # Get all loaded layers\n _radio_values = conf.layers.layers()\n # Restrict to layers-only (not contribs) + packet.py and asn1*.py\n _radio_values = [x for x in _radio_values if (\"layers\" in x[0] or\n \"packet\" in x[0] or\n \"asn1\" in x[0])]\n elif action == \"contribs\":\n # Get all existing contribs\n from scapy.main import list_contrib\n _radio_values = list_contrib(ret=True)\n _radio_values = [(x['name'], x['description'])\n for x in _radio_values]\n # Remove very specific modules\n _radio_values = [x for x in _radio_values if not (\"can\" in x[0])]\n else:\n # Escape/Cancel was pressed\n return\n # Python 2 compat\n if six.PY2:\n _radio_values = [(six.text_type(x), six.text_type(y))\n for x, y in _radio_values]\n # 3 - Ask for the layer/contrib module to explore\n result = radiolist_dialog(\n values=_radio_values,\n title=\"Scapy v%s\" % conf.version,\n text=HTML(\n six.text_type(\n ''\n )\n ))\n if result is None:\n return # User pressed \"Cancel\"\n # 4 - (Contrib only): load contrib\n if action == \"contribs\":\n from scapy.main import load_contrib\n load_contrib(result)\n result = \"scapy.contrib.\" + result\n else: # NON-GUI MODE\n # We handle layer as a short layer name, full layer name\n # or the module itself\n if isinstance(layer, types.ModuleType):\n layer = layer.__name__\n if isinstance(layer, str):\n if layer.startswith(\"scapy.layers.\"):\n result = layer\n else:\n if layer.startswith(\"scapy.contrib.\"):\n layer = layer.replace(\"scapy.contrib.\", \"\")\n from scapy.main import load_contrib\n load_contrib(layer)\n result_layer, result_contrib = ((\"scapy.layers.%s\" % layer),\n (\"scapy.contrib.%s\" % layer))\n if result_layer in conf.layers.ldict:\n result = result_layer\n elif result_contrib in conf.layers.ldict:\n result = result_contrib\n else:\n raise Scapy_Exception(\"Unknown scapy module '%s'\" % layer)\n else:\n warning(\"Wrong usage ! Check out help(explore)\")\n return\n\n # COMMON PART\n # Get the list of all Packets contained in that module\n try:\n all_layers = conf.layers.ldict[result]\n except KeyError:\n raise Scapy_Exception(\"Unknown scapy module '%s'\" % layer)\n # Print\n print(conf.color_theme.layer_name(\"Packets contained in %s:\" % result))\n rtlst = [(lay.__name__ or \"\", lay._name or \"\") for lay in all_layers]\n print(pretty_list(rtlst, [(\"Class\", \"Name\")], borders=True))"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nlists available layers or infos on a given Packet or name.", "response": "def ls(obj=None, case_sensitive=False, verbose=False):\n \"\"\"List available layers, or infos on a given layer class or name.\n params:\n - obj: Packet / packet name to use\n - case_sensitive: if obj is a string, is it case sensitive?\n - verbose\n \"\"\"\n is_string = isinstance(obj, six.string_types)\n\n if obj is None or is_string:\n tip = False\n if obj is None:\n tip = True\n all_layers = sorted(conf.layers, key=lambda x: x.__name__)\n else:\n pattern = re.compile(obj, 0 if case_sensitive else re.I)\n # We first order by accuracy, then length\n if case_sensitive:\n sorter = lambda x: (x.__name__.index(obj), len(x.__name__))\n else:\n obj = obj.lower()\n sorter = lambda x: (x.__name__.lower().index(obj),\n len(x.__name__))\n all_layers = sorted((layer for layer in conf.layers\n if (isinstance(layer.__name__, str) and\n pattern.search(layer.__name__)) or\n (isinstance(layer.name, str) and\n pattern.search(layer.name))),\n key=sorter)\n for layer in all_layers:\n print(\"%-10s : %s\" % (layer.__name__, layer._name))\n if tip and conf.interactive:\n print(\"\\nTIP: You may use explore() to navigate through all \"\n \"layers using a clear GUI\")\n else:\n is_pkt = isinstance(obj, Packet)\n if issubtype(obj, Packet) or is_pkt:\n for f in obj.fields_desc:\n cur_fld = f\n attrs = []\n long_attrs = []\n while isinstance(cur_fld, (Emph, ConditionalField)):\n if isinstance(cur_fld, ConditionalField):\n attrs.append(cur_fld.__class__.__name__[:4])\n cur_fld = cur_fld.fld\n if verbose and isinstance(cur_fld, EnumField) \\\n and hasattr(cur_fld, \"i2s\"):\n if len(cur_fld.i2s) < 50:\n long_attrs.extend(\n \"%s: %d\" % (strval, numval)\n for numval, strval in\n sorted(six.iteritems(cur_fld.i2s))\n )\n elif isinstance(cur_fld, MultiEnumField):\n fld_depend = cur_fld.depends_on(obj.__class__\n if is_pkt else obj)\n attrs.append(\"Depends on %s\" % fld_depend.name)\n if verbose:\n cur_i2s = cur_fld.i2s_multi.get(\n cur_fld.depends_on(obj if is_pkt else obj()), {}\n )\n if len(cur_i2s) < 50:\n long_attrs.extend(\n \"%s: %d\" % (strval, numval)\n for numval, strval in\n sorted(six.iteritems(cur_i2s))\n )\n elif verbose and isinstance(cur_fld, FlagsField):\n names = cur_fld.names\n long_attrs.append(\", \".join(names))\n class_name = \"%s (%s)\" % (\n cur_fld.__class__.__name__,\n \", \".join(attrs)) if attrs else cur_fld.__class__.__name__\n if isinstance(cur_fld, BitField):\n class_name += \" (%d bit%s)\" % (cur_fld.size,\n \"s\" if cur_fld.size > 1\n else \"\")\n print(\"%-10s : %-35s =\" % (f.name, class_name), end=' ')\n if is_pkt:\n print(\"%-15r\" % (getattr(obj, f.name),), end=' ')\n print(\"(%r)\" % (f.default,))\n for attr in long_attrs:\n print(\"%-15s%s\" % (\"\", attr))\n if is_pkt and not isinstance(obj.payload, NoPayload):\n print(\"--\")\n ls(obj.payload)\n\n else:\n print(\"Not a packet class or name. Type 'ls()' to list packet classes.\")"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef fuzz(p, _inplace=0):\n if not _inplace:\n p = p.copy()\n q = p\n while not isinstance(q, NoPayload):\n for f in q.fields_desc:\n if isinstance(f, PacketListField):\n for r in getattr(q, f.name):\n print(\"fuzzing\", repr(r))\n fuzz(r, _inplace=1)\n elif f.default is not None:\n if not isinstance(f, ConditionalField) or f._evalcond(q):\n rnd = f.randval()\n if rnd is not None:\n q.default_fields[f.name] = rnd\n q = q.payload\n return p", "response": "Transform a layer into a fuzzy layer by replacing some default values by random objects"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ninitialize each field of the fields_desc dict.", "response": "def init_fields(self):\n \"\"\"\n Initialize each fields of the fields_desc dict\n \"\"\"\n\n if self.class_dont_cache.get(self.__class__, False):\n self.do_init_fields(self.fields_desc)\n else:\n self.do_init_cached_fields()"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef do_init_fields(self, flist):\n for f in flist:\n self.default_fields[f.name] = copy.deepcopy(f.default)\n self.fieldtype[f.name] = f\n if f.holds_packets:\n self.packetfields.append(f)", "response": "Initialize each field of the fields_desc dict"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef do_init_cached_fields(self):\n\n cls_name = self.__class__\n\n # Build the fields information\n if Packet.class_default_fields.get(cls_name, None) is None:\n self.prepare_cached_fields(self.fields_desc)\n\n # Use fields information from cache\n if not Packet.class_default_fields.get(cls_name, None) is None:\n self.default_fields = Packet.class_default_fields[cls_name]\n self.fieldtype = Packet.class_fieldtype[cls_name]\n self.packetfields = Packet.class_packetfields[cls_name]\n\n # Deepcopy default references\n for fname in Packet.class_default_fields_ref[cls_name]:\n value = copy.deepcopy(self.default_fields[fname])\n setattr(self, fname, value)", "response": "Initialize each fields of the fields_desc dict or use the cached\n fields information"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef prepare_cached_fields(self, flist):\n\n cls_name = self.__class__\n\n # Fields cache initialization\n if flist:\n Packet.class_default_fields[cls_name] = dict()\n Packet.class_default_fields_ref[cls_name] = list()\n Packet.class_fieldtype[cls_name] = dict()\n Packet.class_packetfields[cls_name] = list()\n\n # Fields initialization\n for f in flist:\n if isinstance(f, MultipleTypeField):\n del Packet.class_default_fields[cls_name]\n del Packet.class_default_fields_ref[cls_name]\n del Packet.class_fieldtype[cls_name]\n del Packet.class_packetfields[cls_name]\n self.class_dont_cache[cls_name] = True\n self.do_init_fields(self.fields_desc)\n break\n\n tmp_copy = copy.deepcopy(f.default)\n Packet.class_default_fields[cls_name][f.name] = tmp_copy\n Packet.class_fieldtype[cls_name][f.name] = f\n if f.holds_packets:\n Packet.class_packetfields[cls_name].append(f)\n\n # Remember references\n if isinstance(f.default, (list, dict, set, RandField, Packet)):\n Packet.class_default_fields_ref[cls_name].append(f.name)", "response": "Prepare the cached fields of the fields_desc object."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncall when a dissection is complete.", "response": "def dissection_done(self, pkt):\n \"\"\"DEV: will be called after a dissection is completed\"\"\"\n self.post_dissection(pkt)\n self.payload.dissection_done(pkt)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a deep copy of the instance.", "response": "def copy(self):\n \"\"\"Returns a deep copy of the instance.\"\"\"\n clone = self.__class__()\n clone.fields = self.copy_fields_dict(self.fields)\n clone.default_fields = self.copy_fields_dict(self.default_fields)\n clone.overloaded_fields = self.overloaded_fields.copy()\n clone.underlayer = self.underlayer\n clone.explicit = self.explicit\n clone.raw_packet_cache = self.raw_packet_cache\n clone.raw_packet_cache_fields = self.copy_fields_dict(\n self.raw_packet_cache_fields\n )\n clone.wirelen = self.wirelen\n clone.post_transforms = self.post_transforms[:]\n clone.payload = self.payload.copy()\n clone.payload.add_underlayer(clone)\n clone.time = self.time\n return clone"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _superdir(self):\n attrs = set()\n cls = self.__class__\n if hasattr(cls, '__all_slots__'):\n attrs.update(cls.__all_slots__)\n for bcls in cls.__mro__:\n if hasattr(bcls, '__dict__'):\n attrs.update(bcls.__dict__)\n return attrs", "response": "Return a list of slots and methods including those from subclasses."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nclearing the raw packet cache for the field and all its subfields", "response": "def clear_cache(self):\n \"\"\"Clear the raw packet cache for the field and all its subfields\"\"\"\n self.raw_packet_cache = None\n for _, fval in six.iteritems(self.fields):\n if isinstance(fval, Packet):\n fval.clear_cache()\n self.payload.clear_cache()"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef self_build(self, field_pos_list=None):\n if self.raw_packet_cache is not None:\n for fname, fval in six.iteritems(self.raw_packet_cache_fields):\n if self.getfieldval(fname) != fval:\n self.raw_packet_cache = None\n self.raw_packet_cache_fields = None\n self.wirelen = None\n break\n if self.raw_packet_cache is not None:\n return self.raw_packet_cache\n p = b\"\"\n for f in self.fields_desc:\n val = self.getfieldval(f.name)\n if isinstance(val, RawVal):\n sval = raw(val)\n p += sval\n if field_pos_list is not None:\n field_pos_list.append((f.name, sval.encode(\"string_escape\"), len(p), len(sval))) # noqa: E501\n else:\n p = f.addfield(self, p, val)\n return p", "response": "Create the default layer regarding fields_desc dict\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef do_build(self):\n if not self.explicit:\n self = next(iter(self))\n pkt = self.self_build()\n for t in self.post_transforms:\n pkt = t(pkt)\n pay = self.do_build_payload()\n if self.raw_packet_cache is None:\n return self.post_build(pkt, pay)\n else:\n return pkt + pay", "response": "Create the default version of the layer and return the payload"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncreates the current layer and return the string with the payload", "response": "def build(self):\n \"\"\"\n Create the current layer\n\n :return: string of the packet with the payload\n \"\"\"\n p = self.do_build()\n p += self.build_padding()\n p = self.build_done(p)\n return p"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef do_dissect_payload(self, s):\n if s:\n cls = self.guess_payload_class(s)\n try:\n p = cls(s, _internal=1, _underlayer=self)\n except KeyboardInterrupt:\n raise\n except Exception:\n if conf.debug_dissector:\n if issubtype(cls, Packet):\n log_runtime.error(\"%s dissector failed\" % cls.__name__)\n else:\n log_runtime.error(\"%s.guess_payload_class() returned [%s]\" % (self.__class__.__name__, repr(cls))) # noqa: E501\n if cls is not None:\n raise\n p = conf.raw_layer(s, _internal=1, _underlayer=self)\n self.add_payload(p)", "response": "Perform the dissection of the payload of the layer."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nguessing the next payload class from the layer bonds.", "response": "def guess_payload_class(self, payload):\n \"\"\"\n DEV: Guesses the next payload class from layer bonds.\n Can be overloaded to use a different mechanism.\n\n :param str payload: the layer's payload\n :return: the payload class\n \"\"\"\n for t in self.aliastypes:\n for fval, cls in t.payload_guess:\n if all(hasattr(self, k) and v == self.getfieldval(k)\n for k, v in six.iteritems(fval)):\n return cls\n return self.default_payload_class(payload)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nremove fields values that are the same as default values.", "response": "def hide_defaults(self):\n \"\"\"Removes fields' values that are the same as default values.\"\"\"\n # use list(): self.fields is modified in the loop\n for k, v in list(six.iteritems(self.fields)):\n v = self.fields[k]\n if k in self.default_fields:\n if self.default_fields[k] == v:\n del self.fields[k]\n self.payload.hide_defaults()"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef layers(self):\n layers = []\n lyr = self\n while lyr:\n layers.append(lyr.__class__)\n lyr = lyr.payload.getlayer(0, _subclass=True)\n return layers", "response": "returns a list of layer classes including subclasses in this packet"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef getlayer(self, cls, nb=1, _track=None, _subclass=None, **flt):\n if _subclass is None:\n _subclass = self.match_subclass or None\n if _subclass:\n match = lambda cls1, cls2: issubclass(cls1, cls2)\n else:\n match = lambda cls1, cls2: cls1 == cls2\n if isinstance(cls, int):\n nb = cls + 1\n cls = None\n if isinstance(cls, str) and \".\" in cls:\n ccls, fld = cls.split(\".\", 1)\n else:\n ccls, fld = cls, None\n if cls is None or match(self.__class__, cls) \\\n or ccls in [self.__class__.__name__, self._name]:\n if all(self.getfieldval(fldname) == fldvalue\n for fldname, fldvalue in six.iteritems(flt)):\n if nb == 1:\n if fld is None:\n return self\n else:\n return self.getfieldval(fld)\n else:\n nb -= 1\n for f in self.packetfields:\n fvalue_gen = self.getfieldval(f.name)\n if fvalue_gen is None:\n continue\n if not f.islist:\n fvalue_gen = SetGen(fvalue_gen, _iterpacket=0)\n for fvalue in fvalue_gen:\n if isinstance(fvalue, Packet):\n track = []\n ret = fvalue.getlayer(cls, nb=nb, _track=track,\n _subclass=_subclass, **flt)\n if ret is not None:\n return ret\n nb = track[0]\n return self.payload.getlayer(cls, nb=nb, _track=_track,\n _subclass=_subclass, **flt)", "response": "Return the nb^th layer that is an instance of cls matching flt\nvalues."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef show(self, dump=False, indent=3, lvl=\"\", label_lvl=\"\"):\n return self._show_or_dump(dump, indent, lvl, label_lvl)", "response": "Print the value of the key - value layer."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef show2(self, dump=False, indent=3, lvl=\"\", label_lvl=\"\"):\n return self.__class__(raw(self)).show(dump, indent, lvl, label_lvl)", "response": "Print the packet as a hierarchical view of the packet."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef sprintf(self, fmt, relax=1):\n\n escape = {\"%\": \"%\",\n \"(\": \"{\",\n \")\": \"}\"}\n\n # Evaluate conditions\n while \"{\" in fmt:\n i = fmt.rindex(\"{\")\n j = fmt[i + 1:].index(\"}\")\n cond = fmt[i + 1:i + j + 1]\n k = cond.find(\":\")\n if k < 0:\n raise Scapy_Exception(\"Bad condition in format string: [%s] (read sprintf doc!)\" % cond) # noqa: E501\n cond, format_ = cond[:k], cond[k + 1:]\n res = False\n if cond[0] == \"!\":\n res = True\n cond = cond[1:]\n if self.haslayer(cond):\n res = not res\n if not res:\n format_ = \"\"\n fmt = fmt[:i] + format_ + fmt[i + j + 2:]\n\n # Evaluate directives\n s = \"\"\n while \"%\" in fmt:\n i = fmt.index(\"%\")\n s += fmt[:i]\n fmt = fmt[i + 1:]\n if fmt and fmt[0] in escape:\n s += escape[fmt[0]]\n fmt = fmt[1:]\n continue\n try:\n i = fmt.index(\"%\")\n sfclsfld = fmt[:i]\n fclsfld = sfclsfld.split(\",\")\n if len(fclsfld) == 1:\n f = \"s\"\n clsfld = fclsfld[0]\n elif len(fclsfld) == 2:\n f, clsfld = fclsfld\n else:\n raise Scapy_Exception\n if \".\" in clsfld:\n cls, fld = clsfld.split(\".\")\n else:\n cls = self.__class__.__name__\n fld = clsfld\n num = 1\n if \":\" in cls:\n cls, num = cls.split(\":\")\n num = int(num)\n fmt = fmt[i + 1:]\n except Exception:\n raise Scapy_Exception(\"Bad format string [%%%s%s]\" % (fmt[:25], fmt[25:] and \"...\")) # noqa: E501\n else:\n if fld == \"time\":\n val = time.strftime(\"%H:%M:%S.%%06i\", time.localtime(self.time)) % int((self.time - int(self.time)) * 1000000) # noqa: E501\n elif cls == self.__class__.__name__ and hasattr(self, fld):\n if num > 1:\n val = self.payload.sprintf(\"%%%s,%s:%s.%s%%\" % (f, cls, num - 1, fld), relax) # noqa: E501\n f = \"s\"\n elif f[-1] == \"r\": # Raw field value\n val = getattr(self, fld)\n f = f[:-1]\n if not f:\n f = \"s\"\n else:\n val = getattr(self, fld)\n if fld in self.fieldtype:\n val = self.fieldtype[fld].i2repr(self, val)\n else:\n val = self.payload.sprintf(\"%%%s%%\" % sfclsfld, relax)\n f = \"s\"\n s += (\"%\" + f) % val\n\n s += fmt\n return s", "response": "Return a string that can be used as a sprintf statement in a log file."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a string representing the command you have to type to obtain the same packet", "response": "def command(self):\n \"\"\"\n Returns a string representing the command you have to type to\n obtain the same packet\n \"\"\"\n f = []\n for fn, fv in six.iteritems(self.fields):\n fld = self.get_field(fn)\n if isinstance(fv, (list, dict, set)) and len(fv) == 0:\n continue\n if isinstance(fv, Packet):\n fv = fv.command()\n elif fld.islist and fld.holds_packets and isinstance(fv, list):\n fv = \"[%s]\" % \",\".join(map(Packet.command, fv))\n elif isinstance(fld, FlagsField):\n fv = int(fv)\n else:\n fv = repr(fv)\n f.append(\"%s=%s\" % (fn, fv))\n c = \"%s(%s)\" % (self.__class__.__name__, \", \".join(f))\n pc = self.payload.command()\n if pc:\n c += \"/\" + pc\n return c"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ninitializes the TLS session.", "response": "def INIT_TLS_SESSION(self):\n \"\"\"\n XXX We should offer the right key according to the client's suites. For\n now server_rsa_key is only used for RSAkx, but we should try to replace\n every server_key with both server_rsa_key and server_ecdsa_key.\n \"\"\"\n self.cur_session = tlsSession(connection_end=\"server\")\n self.cur_session.server_certs = [self.mycert]\n self.cur_session.server_key = self.mykey\n if isinstance(self.mykey, PrivKeyRSA):\n self.cur_session.server_rsa_key = self.mykey\n # elif isinstance(self.mykey, PrivKeyECDSA):\n # self.cur_session.server_ecdsa_key = self.mykey\n raise self.WAITING_CLIENTFLIGHT1()"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef should_check_ciphersuites(self):\n if isinstance(self.mykey, PrivKeyRSA):\n kx = \"RSA\"\n elif isinstance(self.mykey, PrivKeyECDSA):\n kx = \"ECDSA\"\n if get_usable_ciphersuites(self.cur_pkt.ciphers, kx):\n return\n raise self.NO_USABLE_CIPHERSUITE()", "response": "Check if cipher suites are usable."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef should_add_ServerHello(self):\n if isinstance(self.mykey, PrivKeyRSA):\n kx = \"RSA\"\n elif isinstance(self.mykey, PrivKeyECDSA):\n kx = \"ECDSA\"\n usable_suites = get_usable_ciphersuites(self.cur_pkt.ciphers, kx)\n c = usable_suites[0]\n if self.preferred_ciphersuite in usable_suites:\n c = self.preferred_ciphersuite\n self.add_msg(TLSServerHello(cipher=c))\n raise self.ADDED_SERVERHELLO()", "response": "Add a server hello message to the message queue."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nparsing the arguments for the KrackAP object.", "response": "def parse_args(self, ap_mac, ssid, passphrase,\n channel=None,\n # KRACK attack options\n double_3handshake=True,\n encrypt_3handshake=True,\n wait_3handshake=0,\n double_gtk_refresh=True,\n arp_target_ip=None,\n arp_source_ip=None,\n wait_gtk=10,\n **kwargs):\n \"\"\"\n Mandatory arguments:\n @iface: interface to use (must be in monitor mode)\n @ap_mac: AP's MAC\n @ssid: AP's SSID\n @passphrase: AP's Passphrase (min 8 char.)\n\n Optional arguments:\n @channel: used by the interface. Default 6, autodetected on windows\n\n Krack attacks options:\n\n - Msg 3/4 handshake replay:\n double_3handshake: double the 3/4 handshake message\n encrypt_3handshake: encrypt the second 3/4 handshake message\n wait_3handshake: time to wait (in sec.) before sending the second 3/4\n - double GTK rekeying:\n double_gtk_refresh: double the 1/2 GTK rekeying message\n wait_gtk: time to wait (in sec.) before sending the GTK rekeying\n arp_target_ip: Client IP to use in ARP req. (to detect attack success)\n If None, use a DHCP server\n arp_source_ip: Server IP to use in ARP req. (to detect attack success)\n If None, use the DHCP server gateway address\n \"\"\"\n super(KrackAP, self).parse_args(**kwargs)\n\n # Main AP options\n self.mac = ap_mac\n self.ssid = ssid\n self.passphrase = passphrase\n if channel is None:\n if WINDOWS:\n try:\n channel = kwargs.get(\"iface\", conf.iface).channel()\n except (Scapy_Exception, AttributeError):\n channel = 6\n else:\n channel = 6\n self.channel = channel\n\n # Internal structures\n self.last_iv = None\n self.client = None\n self.seq_num = count()\n self.replay_counter = count()\n self.time_handshake_end = None\n self.dhcp_server = DHCPOverWPA(send_func=self.send_ether_over_wpa,\n pool=Net(\"192.168.42.128/25\"),\n network=\"192.168.42.0/24\",\n gw=\"192.168.42.1\")\n self.arp_sent = []\n self.arp_to_send = 0\n self.arp_retry = 0\n\n # Bit 0: 3way handshake sent\n # Bit 1: GTK rekeying sent\n # Bit 2: ARP response obtained\n self.krack_state = 0\n\n # Krack options\n self.double_3handshake = double_3handshake\n self.encrypt_3handshake = encrypt_3handshake\n self.wait_3handshake = wait_3handshake\n self.double_gtk_refresh = double_gtk_refresh\n self.arp_target_ip = arp_target_ip\n if arp_source_ip is None:\n # Use the DHCP server Gateway address\n arp_source_ip = self.dhcp_server.gw\n self.arp_source_ip = arp_source_ip\n self.wait_gtk = wait_gtk\n\n # May take several seconds\n self.install_PMK()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef install_PMK(self):\n self.pmk = PBKDF2HMAC(\n algorithm=hashes.SHA1(),\n length=32,\n salt=self.ssid.encode(),\n iterations=4096,\n backend=default_backend(),\n ).derive(self.passphrase.encode())", "response": "Compute and install the PMK"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nuse the client nonce to compute and install the unicast keys for the current client.", "response": "def install_unicast_keys(self, client_nonce):\n \"\"\"Use the client nonce @client_nonce to compute and install\n PTK, KCK, KEK, TK, MIC (AP -> STA), MIC (STA -> AP)\n \"\"\"\n pmk = self.pmk\n anonce = self.anonce\n snonce = client_nonce\n amac = mac2str(self.mac)\n smac = mac2str(self.client)\n\n # Compute PTK\n self.ptk = customPRF512(pmk, amac, smac, anonce, snonce)\n\n # Extract derivated keys\n self.kck = self.ptk[:16]\n self.kek = self.ptk[16:32]\n self.tk = self.ptk[32:48]\n self.mic_ap_to_sta = self.ptk[48:56]\n self.mic_sta_to_ap = self.ptk[56:64]\n\n # Reset IV\n self.client_iv = count()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef install_GTK(self):\n\n # Compute GTK\n self.gtk_full = self.gen_nonce(32)\n self.gtk = self.gtk_full[:16]\n\n # Extract derivated keys\n self.mic_ap_to_group = self.gtk_full[16:24]\n\n # Reset IV\n self.group_iv = count()", "response": "Compute a new GTK and install it alongs\n MIC"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef build_ap_info_pkt(self, layer_cls, dest):\n return RadioTap() \\\n / Dot11(addr1=dest, addr2=self.mac, addr3=self.mac) \\\n / layer_cls(timestamp=0, beacon_interval=100,\n cap='ESS+privacy') \\\n / Dot11Elt(ID=\"SSID\", info=self.ssid) \\\n / Dot11EltRates(rates=[130, 132, 139, 150, 12, 18, 24, 36]) \\\n / Dot11Elt(ID=\"DSset\", info=chb(self.channel)) \\\n / Dot11EltRSN(group_cipher_suite=RSNCipherSuite(cipher=0x2),\n pairwise_cipher_suites=[RSNCipherSuite(cipher=0x2)],\n akm_suites=[AKMSuite(suite=0x2)])", "response": "Build a packet with info describing the current AP."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nbuilds the Key Data Encapsulation for GTK.", "response": "def build_GTK_KDE(self):\n \"\"\"Build the Key Data Encapsulation for GTK\n KeyID: 0\n Ref: 802.11i p81\n \"\"\"\n return b''.join([\n b'\\xdd', # Type KDE\n chb(len(self.gtk_full) + 6),\n b'\\x00\\x0f\\xac', # OUI\n b'\\x01', # GTK KDE\n b'\\x00\\x00', # KeyID - Tx - Reserved x2\n self.gtk_full,\n ])"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef send_wpa_enc(self, data, iv, seqnum, dest, mic_key,\n key_idx=0, additionnal_flag=[\"from-DS\"],\n encrypt_key=None):\n \"\"\"Send an encrypted packet with content @data, using IV @iv,\n sequence number @seqnum, MIC key @mic_key\n \"\"\"\n\n if encrypt_key is None:\n encrypt_key = self.tk\n\n rep = RadioTap()\n rep /= Dot11(\n addr1=dest,\n addr2=self.mac,\n addr3=self.mac,\n FCfield=\"+\".join(['protected'] + additionnal_flag),\n SC=(next(self.seq_num) << 4),\n subtype=0,\n type=\"Data\",\n )\n\n # Assume packet is send by our AP -> use self.mac as source\n\n # Encapsule in TKIP with MIC Michael and ICV\n data_to_enc = build_MIC_ICV(raw(data), mic_key, self.mac, dest)\n\n # Header TKIP + payload\n rep /= Raw(build_TKIP_payload(data_to_enc, iv, self.mac, encrypt_key))\n\n self.send(rep)\n return rep", "response": "Send an encrypted packet with content data using IV seqnum MIC key mic_key and key_idx."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsends an Ethernet packet over the WPA channel.", "response": "def send_ether_over_wpa(self, pkt, **kwargs):\n \"\"\"Send an Ethernet packet using the WPA channel\n Extra arguments will be ignored, and are just left for compatibility\n \"\"\"\n\n payload = LLC() / SNAP() / pkt[Ether].payload\n dest = pkt.dst\n if dest == \"ff:ff:ff:ff:ff:ff\":\n self.send_wpa_to_group(payload, dest)\n else:\n assert dest == self.client\n self.send_wpa_to_client(payload)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nraises state on the next packet.", "response": "def raise_on_packet(self, pkt_cls, state, get_next_msg=True):\n \"\"\"\n If the next message to be processed has type 'pkt_cls', raise 'state'.\n If there is no message waiting to be processed, we try to get one with\n the default 'get_next_msg' parameters.\n \"\"\"\n # Maybe we already parsed the expected packet, maybe not.\n if get_next_msg:\n self.get_next_msg()\n if (not self.buffer_in or\n not isinstance(self.buffer_in[0], pkt_cls)):\n return\n self.cur_pkt = self.buffer_in[0]\n self.buffer_in = self.buffer_in[1:]\n raise state()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_record(self, is_sslv2=None, is_tls13=None):\n if is_sslv2 is None and is_tls13 is None:\n v = (self.cur_session.tls_version or\n self.cur_session.advertised_tls_version)\n if v in [0x0200, 0x0002]:\n is_sslv2 = True\n elif v >= 0x0304:\n is_tls13 = True\n if is_sslv2:\n self.buffer_out.append(SSLv2(tls_session=self.cur_session))\n elif is_tls13:\n self.buffer_out.append(TLS13(tls_session=self.cur_session))\n else:\n self.buffer_out.append(TLS(tls_session=self.cur_session))", "response": "Add a new TLS or SSLv2 or TLS 1. 3 record to the packets buffered out."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nadding a TLS message to the message buffer.", "response": "def add_msg(self, pkt):\n \"\"\"\n Add a TLS message (e.g. TLSClientHello or TLSApplicationData)\n inside the latest record to be sent through the socket.\n We believe a good automaton should not use the first test.\n \"\"\"\n if not self.buffer_out:\n self.add_record()\n r = self.buffer_out[-1]\n if isinstance(r, TLS13):\n self.buffer_out[-1].inner.msg.append(pkt)\n else:\n self.buffer_out[-1].msg.append(pkt)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef flush_records(self):\n s = b\"\".join(p.raw_stateful() for p in self.buffer_out)\n self.socket.send(s)\n self.buffer_out = []", "response": "Send all buffered records and update the session accordingly."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef util_srcpanid_present(pkt):\n '''A source PAN ID is included if and only if both src addr mode != 0 and PAN ID Compression in FCF == 0''' # noqa: E501\n if (pkt.underlayer.getfieldval(\"fcf_srcaddrmode\") != 0) and (pkt.underlayer.getfieldval(\"fcf_panidcompress\") == 0): # noqa: E501\n return True\n else:\n return False", "response": "A source PAN ID is included if and only if both src addr mode and PAN ID Compression in FCF == 0"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nconverting internal value to a nice representation", "response": "def i2repr(self, pkt, x):\n \"\"\"Convert internal value to a nice representation\"\"\"\n if len(hex(self.i2m(pkt, x))) < 7: # short address\n return hex(self.i2m(pkt, x))\n else: # long address\n x = \"%016x\" % self.i2m(pkt, x)\n return \":\".join([\"%s%s\" % (x[i], x[i + 1]) for i in range(0, len(x), 2)])"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nadd an internal value to a string", "response": "def addfield(self, pkt, s, val):\n \"\"\"Add an internal value to a string\"\"\"\n if self.adjust(pkt, self.length_of) == 2:\n return s + struct.pack(self.fmt[0] + \"H\", val)\n elif self.adjust(pkt, self.length_of) == 8:\n return s + struct.pack(self.fmt[0] + \"Q\", val)\n else:\n return s"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nprovide the implementation of P_hash function defined in section 5 of RFC 4346 (and section 5 of RFC 5246). Two parameters have been added (hm and req_len): - secret : the key to be used. If RFC 4868 is to be believed, the length must match hm.key_len. Actually, python hmac takes care of formatting every key. - seed : the seed to be used. - req_len : the length of data to be generated by iterating the specific HMAC function (hm). This prevents multiple calls to the function. - hm : the hmac function class to use for iteration (either Hmac_MD5 or Hmac_SHA1 in TLS <= 1.1 or Hmac_SHA256 or Hmac_SHA384 in TLS 1.2)", "response": "def _tls_P_hash(secret, seed, req_len, hm):\n \"\"\"\n Provides the implementation of P_hash function defined in\n section 5 of RFC 4346 (and section 5 of RFC 5246). Two\n parameters have been added (hm and req_len):\n\n - secret : the key to be used. If RFC 4868 is to be believed,\n the length must match hm.key_len. Actually,\n python hmac takes care of formatting every key.\n - seed : the seed to be used.\n - req_len : the length of data to be generated by iterating\n the specific HMAC function (hm). This prevents\n multiple calls to the function.\n - hm : the hmac function class to use for iteration (either\n Hmac_MD5 or Hmac_SHA1 in TLS <= 1.1 or\n Hmac_SHA256 or Hmac_SHA384 in TLS 1.2)\n \"\"\"\n hash_len = hm.hash_alg.hash_len\n n = (req_len + hash_len - 1) // hash_len\n seed = bytes_encode(seed)\n\n res = b\"\"\n a = hm(secret).digest(seed) # A(1)\n\n while n > 0:\n res += hm(secret).digest(a + seed)\n a = hm(secret).digest(a)\n n -= 1\n\n return res[:req_len]"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _ssl_PRF(secret, seed, req_len):\n if req_len > 416:\n warning(\"_ssl_PRF() is not expected to provide more than 416 bytes\")\n return \"\"\n\n d = [b\"A\", b\"B\", b\"C\", b\"D\", b\"E\", b\"F\", b\"G\", b\"H\", b\"I\", b\"J\", b\"K\", b\"L\", # noqa: E501\n b\"M\", b\"N\", b\"O\", b\"P\", b\"Q\", b\"R\", b\"S\", b\"T\", b\"U\", b\"V\", b\"W\", b\"X\", # noqa: E501\n b\"Y\", b\"Z\"]\n res = b\"\"\n hash_sha1 = _tls_hash_algs[\"SHA\"]()\n hash_md5 = _tls_hash_algs[\"MD5\"]()\n rounds = (req_len + hash_md5.hash_len - 1) // hash_md5.hash_len\n\n for i in range(rounds):\n label = d[i] * (i + 1)\n tmp = hash_sha1.digest(label + secret + seed)\n res += hash_md5.digest(secret + tmp)\n\n return res[:req_len]", "response": "Returns the SSLv3 PRF function for the given secret and seed."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _tls_PRF(secret, label, seed, req_len):\n tmp_len = (len(secret) + 1) // 2\n S1 = secret[:tmp_len]\n S2 = secret[-tmp_len:]\n\n a1 = _tls_P_MD5(S1, label + seed, req_len)\n a2 = _tls_P_SHA1(S2, label + seed, req_len)\n\n return strxor(a1, a2)", "response": "Returns the PRF function for the given secret label and seed."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning the implementation of TLS 1. 2 PRF function as", "response": "def _tls12_SHA256PRF(secret, label, seed, req_len):\n \"\"\"\n Provides the implementation of TLS 1.2 PRF function as\n defined in section 5 of RFC 5246:\n\n PRF(secret, label, seed) = P_SHA256(secret, label + seed)\n\n Parameters are:\n\n - secret: the secret used by the HMAC in the 2 expansion\n functions (S1 and S2 are the halves of this secret).\n - label: specific label as defined in various sections of the RFC\n depending on the use of the generated PRF keystream\n - seed: the seed used by the expansion functions.\n - req_len: amount of keystream to be generated\n \"\"\"\n return _tls_P_SHA256(secret, label + seed, req_len)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncomputes the master secret from the pre_master_secret client_random and server_random.", "response": "def compute_master_secret(self, pre_master_secret,\n client_random, server_random):\n \"\"\"\n Return the 48-byte master_secret, computed from pre_master_secret,\n client_random and server_random. See RFC 5246, section 6.3.\n \"\"\"\n seed = client_random + server_random\n if self.tls_version < 0x0300:\n return None\n elif self.tls_version == 0x0300:\n return self.prf(pre_master_secret, seed, 48)\n else:\n return self.prf(pre_master_secret, b\"master secret\", seed, 48)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nderives a key_block from a master_secret.", "response": "def derive_key_block(self, master_secret, server_random,\n client_random, req_len):\n \"\"\"\n Perform the derivation of master_secret into a key_block of req_len\n requested length. See RFC 5246, section 6.3.\n \"\"\"\n seed = server_random + client_random\n if self.tls_version <= 0x0300:\n return self.prf(master_secret, seed, req_len)\n else:\n return self.prf(master_secret, b\"key expansion\", seed, req_len)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef compute_verify_data(self, con_end, read_or_write,\n handshake_msg, master_secret):\n \"\"\"\n Return verify_data based on handshake messages, connection end,\n master secret, and read_or_write position. See RFC 5246, section 7.4.9.\n\n Every TLS 1.2 cipher suite has a verify_data of length 12. Note also:\n \"This PRF with the SHA-256 hash function is used for all cipher\n suites defined in this document and in TLS documents published\n prior to this document when TLS 1.2 is negotiated.\"\n Cipher suites using SHA-384 were defined later on.\n \"\"\"\n if self.tls_version < 0x0300:\n return None\n elif self.tls_version == 0x0300:\n\n if read_or_write == \"write\":\n d = {\"client\": b\"CLNT\", \"server\": b\"SRVR\"}\n else:\n d = {\"client\": b\"SRVR\", \"server\": b\"CLNT\"}\n label = d[con_end]\n\n sslv3_md5_pad1 = b\"\\x36\" * 48\n sslv3_md5_pad2 = b\"\\x5c\" * 48\n sslv3_sha1_pad1 = b\"\\x36\" * 40\n sslv3_sha1_pad2 = b\"\\x5c\" * 40\n\n md5 = _tls_hash_algs[\"MD5\"]()\n sha1 = _tls_hash_algs[\"SHA\"]()\n\n md5_hash = md5.digest(master_secret + sslv3_md5_pad2 +\n md5.digest(handshake_msg + label +\n master_secret + sslv3_md5_pad1))\n sha1_hash = sha1.digest(master_secret + sslv3_sha1_pad2 +\n sha1.digest(handshake_msg + label +\n master_secret + sslv3_sha1_pad1)) # noqa: E501\n verify_data = md5_hash + sha1_hash\n\n else:\n\n if read_or_write == \"write\":\n d = {\"client\": \"client\", \"server\": \"server\"}\n else:\n d = {\"client\": \"server\", \"server\": \"client\"}\n label = (\"%s finished\" % d[con_end]).encode()\n\n if self.tls_version <= 0x0302:\n s1 = _tls_hash_algs[\"MD5\"]().digest(handshake_msg)\n s2 = _tls_hash_algs[\"SHA\"]().digest(handshake_msg)\n verify_data = self.prf(master_secret, label, s1 + s2, 12)\n else:\n if self.hash_name in [\"MD5\", \"SHA\"]:\n h = _tls_hash_algs[\"SHA256\"]()\n else:\n h = _tls_hash_algs[self.hash_name]()\n s = h.digest(handshake_msg)\n verify_data = self.prf(master_secret, label, s, 12)\n\n return verify_data", "response": "Compute verify_data based on handshake messages master_secret and read_or_write."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef postprocess_key_for_export(self, key, client_random, server_random,\n con_end, read_or_write, req_len):\n \"\"\"\n Postprocess cipher key for EXPORT ciphersuite, i.e. weakens it.\n An export key generation example is given in section 6.3.1 of RFC 2246.\n See also page 86 of EKR's book.\n \"\"\"\n s = con_end + read_or_write\n s = (s == \"clientwrite\" or s == \"serverread\")\n\n if self.tls_version < 0x0300:\n return None\n elif self.tls_version == 0x0300:\n if s:\n tbh = key + client_random + server_random\n else:\n tbh = key + server_random + client_random\n export_key = _tls_hash_algs[\"MD5\"]().digest(tbh)[:req_len]\n else:\n if s:\n tag = b\"client write key\"\n else:\n tag = b\"server write key\"\n export_key = self.prf(key,\n tag,\n client_random + server_random,\n req_len)\n return export_key", "response": "Postprocess cipher key for EXPORT cipher suite."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngenerates an export IV for the specified ciphersuite.", "response": "def generate_iv_for_export(self, client_random, server_random,\n con_end, read_or_write, req_len):\n \"\"\"\n Generate IV for EXPORT ciphersuite, i.e. weakens it.\n An export IV generation example is given in section 6.3.1 of RFC 2246.\n See also page 86 of EKR's book.\n \"\"\"\n s = con_end + read_or_write\n s = (s == \"clientwrite\" or s == \"serverread\")\n\n if self.tls_version < 0x0300:\n return None\n elif self.tls_version == 0x0300:\n if s:\n tbh = client_random + server_random\n else:\n tbh = server_random + client_random\n iv = _tls_hash_algs[\"MD5\"]().digest(tbh)[:req_len]\n else:\n iv_block = self.prf(\"\",\n b\"IV block\",\n client_random + server_random,\n 2 * req_len)\n if s:\n iv = iv_block[:req_len]\n else:\n iv = iv_block[req_len:]\n return iv"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef load_protocols(filename, _integer_base=10):\n spaces = re.compile(b\"[ \\t]+|\\n\")\n dct = DADict(_name=filename)\n try:\n with open(filename, \"rb\") as fdesc:\n for line in fdesc:\n try:\n shrp = line.find(b\"#\")\n if shrp >= 0:\n line = line[:shrp]\n line = line.strip()\n if not line:\n continue\n lt = tuple(re.split(spaces, line))\n if len(lt) < 2 or not lt[0]:\n continue\n dct[lt[0]] = int(lt[1], _integer_base)\n except Exception as e:\n log_loading.info(\n \"Couldn't parse file [%s]: line [%r] (%s)\",\n filename,\n line,\n e,\n )\n except IOError:\n log_loading.info(\"Can't open %s file\", filename)\n return dct", "response": "Parse / etc / protocols and return values as a dictionary."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef load_manuf(filename):\n manufdb = ManufDA(_name=filename)\n with open(filename, \"rb\") as fdesc:\n for line in fdesc:\n try:\n line = line.strip()\n if not line or line.startswith(b\"#\"):\n continue\n parts = line.split(None, 2)\n oui, shrt = parts[:2]\n lng = parts[2].lstrip(b\"#\").strip() if len(parts) > 2 else \"\"\n lng = lng or shrt\n manufdb[oui] = plain_str(shrt), plain_str(lng)\n except Exception:\n log_loading.warning(\"Couldn't parse one line from [%s] [%r]\",\n filename, line, exc_info=True)\n return manufdb", "response": "Load the manuf file from Wireshark."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nfinding the OUI name matching to a MAC", "response": "def lookup(self, mac):\n \"\"\"Find OUI name matching to a MAC\"\"\"\n oui = \":\".join(mac.split(\":\")[:3]).upper()\n return self[oui]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a dict of all MACs registered to a OUI.", "response": "def reverse_lookup(self, name, case_sensitive=False):\n \"\"\"Find all MACs registered to a OUI\n params:\n - name: the OUI name\n - case_sensitive: default to False\n returns: a dict of mac:tuples (Name, Extended Name)\n \"\"\"\n if case_sensitive:\n filtr = lambda x, l: any(x == z for z in l)\n else:\n name = name.lower()\n filtr = lambda x, l: any(x == z.lower() for z in l)\n return {k: v for k, v in six.iteritems(self.__dict__)\n if filtr(name, v)}"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef answers(self, other):\n if not hasattr(other, \"ctr\"):\n return 0\n if self.ctr != other.ctr:\n return 0\n if not hasattr(other, \"cmd\"):\n return 0\n\n new_pl_cls = self.get_dto_cls(other.cmd)\n if self.payload_cls != new_pl_cls and \\\n self.payload_cls == DEFAULT_DTO:\n data = bytes(self.load)\n self.remove_payload()\n self.add_payload(new_pl_cls(data))\n self.payload_cls = new_pl_cls\n return 1", "response": "This function is used to determine the class of the payload of another DTO packet."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nread the version from git describe.", "response": "def _version_from_git_describe():\n \"\"\"\n Read the version from ``git describe``. It returns the latest tag with an\n optional suffix if the current directory is not exactly on the tag.\n\n Example::\n\n $ git describe --always\n v2.3.2-346-g164a52c075c8\n\n The tag prefix (``v``) and the git commit sha1 (``-g164a52c075c8``) are\n removed if present.\n\n If the current directory is not exactly on the tag, a ``.devN`` suffix is\n appended where N is the number of commits made after the last tag.\n\n Example::\n\n >>> _version_from_git_describe()\n '2.3.2.dev346'\n \"\"\"\n if not os.path.isdir(os.path.join(os.path.dirname(_SCAPY_PKG_DIR), '.git')): # noqa: E501\n raise ValueError('not in scapy git repo')\n\n process = subprocess.Popen(['git', 'describe', '--always'],\n cwd=_SCAPY_PKG_DIR,\n stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n\n out, err = process.communicate()\n\n if process.returncode == 0:\n tag = out.decode().strip()\n match = re.match('^v?(.+?)-(\\\\d+)-g[a-f0-9]+$', tag)\n if match:\n # remove the 'v' prefix and add a '.devN' suffix\n return '%s.dev%s' % (match.group(1), match.group(2))\n else:\n # just remove the 'v' prefix\n return re.sub('^v', '', tag)\n else:\n raise subprocess.CalledProcessError(process.returncode, err)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a string representation of the object.", "response": "def _show_or_dump(self, dump=False, indent=3,\n lvl=\"\", label_lvl=\"\", first_call=True):\n \"\"\" Reproduced from packet.py \"\"\"\n ct = AnsiColorTheme() if dump else conf.color_theme\n s = \"%s%s %s %s \\n\" % (label_lvl, ct.punct(\"###[\"),\n ct.layer_name(self.name), ct.punct(\"]###\"))\n for f in self.fields_desc[:-1]:\n ncol = ct.field_name\n vcol = ct.field_value\n fvalue = self.getfieldval(f.name)\n begn = \"%s %-10s%s \" % (label_lvl + lvl, ncol(f.name),\n ct.punct(\"=\"),)\n reprval = f.i2repr(self, fvalue)\n if isinstance(reprval, str):\n reprval = reprval.replace(\"\\n\", \"\\n\" + \" \" * (len(label_lvl) +\n len(lvl) +\n len(f.name) +\n 4))\n s += \"%s%s\\n\" % (begn, vcol(reprval))\n f = self.fields_desc[-1]\n ncol = ct.field_name\n vcol = ct.field_value\n fvalue = self.getfieldval(f.name)\n begn = \"%s %-10s%s \" % (label_lvl + lvl, ncol(f.name), ct.punct(\"=\"),)\n reprval = f.i2repr(self, fvalue)\n if isinstance(reprval, str):\n reprval = reprval.replace(\"\\n\", \"\\n\" + \" \" * (len(label_lvl) +\n len(lvl) +\n len(f.name) +\n 4))\n s += \"%s%s\\n\" % (begn, vcol(reprval))\n if self.payload:\n s += self.payload._show_or_dump(dump=dump, indent=indent,\n lvl=lvl + (\" \" * indent * self.show_indent), # noqa: E501\n label_lvl=label_lvl, first_call=False) # noqa: E501\n\n if first_call and not dump:\n print(s)\n else:\n return s"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef getfield(self, pkt, s):\n ext = pkt.get_field(self.length_of)\n tmp_len = ext.length_from(pkt)\n if tmp_len is None or tmp_len <= 0:\n v = pkt.tls_session.tls_version\n if v is None or v < 0x0304:\n return s, None\n return super(_ExtensionsLenField, self).getfield(pkt, s)", "response": "Return the length of the record from the extension field."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nadd a field to the internal buffer.", "response": "def addfield(self, pkt, s, i):\n \"\"\"\n There is a hack with the _ExtensionsField.i2len. It works only because\n we expect _ExtensionsField.i2m to return a string of the same size (if\n not of the same value) upon successive calls (e.g. through i2len here,\n then i2m when directly building the _ExtensionsField).\n\n XXX A proper way to do this would be to keep the extensions built from\n the i2len call here, instead of rebuilding them later on.\n \"\"\"\n if i is None:\n if self.length_of is not None:\n fld, fval = pkt.getfield_and_val(self.length_of)\n\n tmp = pkt.tls_session.frozen\n pkt.tls_session.frozen = True\n f = fld.i2len(pkt, fval)\n pkt.tls_session.frozen = tmp\n\n i = self.adjust(pkt, f)\n if i == 0: # for correct build if no ext and not explicitly 0\n return s\n return s + struct.pack(self.fmt, i)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _GenNetflowRecordV9(cls, lengths_list):\n _fields_desc = []\n for j, k in lengths_list:\n _f_data = NetflowV9TemplateFieldDecoders.get(k, None)\n _f_type, _f_args = (\n _f_data if isinstance(_f_data, tuple) else (_f_data, [])\n )\n _f_kwargs = {}\n if _f_type:\n if issubclass(_f_type, _AdjustableNetflowField):\n _f_kwargs[\"length\"] = j\n _fields_desc.append(\n _f_type(\n NetflowV910TemplateFieldTypes.get(k, \"unknown_data\"),\n 0, *_f_args, **_f_kwargs\n )\n )\n else:\n _fields_desc.append(\n _CustomStrFixedLenField(\n NetflowV910TemplateFieldTypes.get(k, \"unknown_data\"),\n b\"\", length=j\n )\n )\n\n # This will act exactly like a NetflowRecordV9, but has custom fields\n class NetflowRecordV9I(cls):\n fields_desc = _fields_desc\n match_subclass = True\n NetflowRecordV9I.name = cls.name\n NetflowRecordV9I.__name__ = cls.__name__\n return NetflowRecordV9I", "response": "Internal function used to generate the Records from the given class and the given list of lengths."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nuse internally to process a single packet during defragmenting.", "response": "def _netflowv9_defragment_packet(pkt, definitions, definitions_opts, ignored):\n \"\"\"Used internally to process a single packet during defragmenting\"\"\"\n # Dataflowset definitions\n if NetflowFlowsetV9 in pkt:\n current = pkt\n while NetflowFlowsetV9 in current:\n current = current[NetflowFlowsetV9]\n for ntv9 in current.templates:\n llist = []\n for tmpl in ntv9.template_fields:\n llist.append((tmpl.fieldLength, tmpl.fieldType))\n if llist:\n tot_len = sum(x[0] for x in llist)\n cls = _GenNetflowRecordV9(NetflowRecordV9, llist)\n definitions[ntv9.templateID] = (tot_len, cls)\n current = current.payload\n # Options definitions\n if NetflowOptionsFlowsetV9 in pkt:\n current = pkt\n while NetflowOptionsFlowsetV9 in current:\n current = current[NetflowOptionsFlowsetV9]\n # Load scopes\n llist = []\n for scope in current.scopes:\n llist.append((\n scope.scopeFieldlength,\n scope.scopeFieldType\n ))\n scope_tot_len = sum(x[0] for x in llist)\n scope_cls = _GenNetflowRecordV9(\n NetflowOptionsRecordScopeV9,\n llist\n )\n # Load options\n llist = []\n for opt in current.options:\n llist.append((\n opt.optionFieldlength,\n opt.optionFieldType\n ))\n option_tot_len = sum(x[0] for x in llist)\n option_cls = _GenNetflowRecordV9(\n NetflowOptionsRecordOptionV9,\n llist\n )\n # Storage\n definitions_opts[current.templateID] = (\n scope_tot_len, scope_cls,\n option_tot_len, option_cls\n )\n current = current.payload\n # Dissect flowsets\n if NetflowDataflowsetV9 in pkt:\n datafl = pkt[NetflowDataflowsetV9]\n tid = datafl.templateID\n if tid not in definitions and tid not in definitions_opts:\n ignored.add(tid)\n return\n # All data is stored in one record, awaiting to be split\n # If fieldValue is available, the record has not been\n # defragmented: pop it\n try:\n data = datafl.records[0].fieldValue\n datafl.records.pop(0)\n except (IndexError, AttributeError):\n return\n res = []\n # Flowset record\n # Now, according to the flow/option data,\n # let's re-dissect NetflowDataflowsetV9\n if tid in definitions:\n tot_len, cls = definitions[tid]\n while len(data) >= tot_len:\n res.append(cls(data[:tot_len]))\n data = data[tot_len:]\n # Inject dissected data\n datafl.records = res\n datafl.do_dissect_payload(data)\n # Options\n elif tid in definitions_opts:\n (scope_len, scope_cls,\n option_len, option_cls) = definitions_opts[tid]\n # Dissect scopes\n if scope_len:\n res.append(scope_cls(data[:scope_len]))\n if option_len:\n res.append(\n option_cls(data[scope_len:scope_len + option_len])\n )\n if len(data) > scope_len + option_len:\n res.append(\n conf.padding_layer(data[scope_len + option_len:])\n )\n # Inject dissected data\n datafl.records = res\n datafl.name = \"Netflow DataFlowSet V9/10 - OPTIONS\""} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nprocess all NetflowV9 and NetflowOptionsFlowsetV9 packets to match IDs of the Headers of the DataFlowsets with the Headers of the DataFlowsets .", "response": "def netflowv9_defragment(plist, verb=1):\n \"\"\"Process all NetflowV9/10 Packets to match IDs of the DataFlowsets\n with the Headers\n\n params:\n - plist: the list of mixed NetflowV9/10 packets.\n - verb: verbose print (0/1)\n \"\"\"\n if not isinstance(plist, (PacketList, list)):\n plist = [plist]\n # We need the whole packet to be dissected to access field def in\n # NetflowFlowsetV9 or NetflowOptionsFlowsetV9/10\n definitions = {}\n definitions_opts = {}\n ignored = set()\n # Iterate through initial list\n for pkt in plist:\n _netflowv9_defragment_packet(pkt,\n definitions,\n definitions_opts,\n ignored)\n if conf.verb >= 1 and ignored:\n warning(\"Ignored templateIDs (missing): %s\" % list(ignored))\n return plist"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nconverting a DER octet string to PEM format.", "response": "def der2pem(der_string, obj=\"UNKNOWN\"):\n \"\"\"Convert DER octet string to PEM format (with optional header)\"\"\"\n # Encode a byte string in PEM format. Header advertizes type.\n pem_string = (\"-----BEGIN %s-----\\n\" % obj).encode()\n base64_string = base64.b64encode(der_string)\n chunks = [base64_string[i:i + 64] for i in range(0, len(base64_string), 64)] # noqa: E501\n pem_string += b'\\n'.join(chunks)\n pem_string += (\"\\n-----END %s-----\\n\" % obj).encode()\n return pem_string"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nconverts PEM string to DER format", "response": "def pem2der(pem_string):\n \"\"\"Convert PEM string to DER format\"\"\"\n # Encode all lines between the first '-----\\n' and the 2nd-to-last '-----'.\n pem_string = pem_string.replace(b\"\\r\", b\"\")\n first_idx = pem_string.find(b\"-----\\n\") + 6\n if pem_string.find(b\"-----BEGIN\", first_idx) != -1:\n raise Exception(\"pem2der() expects only one PEM-encoded object\")\n last_idx = pem_string.rfind(b\"-----\", 0, pem_string.rfind(b\"-----\"))\n base64_string = pem_string[first_idx:last_idx]\n base64_string.replace(b\"\\n\", b\"\")\n der_string = base64.b64decode(base64_string)\n return der_string"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef split_pem(s):\n pem_strings = []\n while s != b\"\":\n start_idx = s.find(b\"-----BEGIN\")\n if start_idx == -1:\n break\n end_idx = s.find(b\"-----END\")\n end_idx = s.find(b\"\\n\", end_idx) + 1\n pem_strings.append(s[start_idx:end_idx])\n s = s[end_idx:]\n return pem_strings", "response": "Split PEM objects. Useful to process concatenated certificates."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating a CAfile from the list of anchor_list and write the result to filename.", "response": "def _create_ca_file(anchor_list, filename):\n \"\"\"\n Concatenate all the certificates (PEM format for the export) in\n 'anchor_list' and write the result to file 'filename'. On success\n 'filename' is returned, None otherwise.\n\n If you are used to OpenSSL tools, this function builds a CAfile\n that can be used for certificate and CRL check.\n \"\"\"\n try:\n with open(filename, \"w\") as f:\n for a in anchor_list:\n s = a.output(fmt=\"PEM\")\n f.write(s)\n except IOError:\n return None\n return filename"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef verifyCert(self, cert):\n tbsCert = cert.tbsCertificate\n sigAlg = tbsCert.signature\n h = hash_by_oid[sigAlg.algorithm.val]\n sigVal = raw(cert.signatureValue)\n return self.verify(raw(tbsCert), sigVal, h=h, t='pkcs')", "response": "Verifies either a Cert or X509_Cert."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef signTBSCert(self, tbsCert, h=\"sha256\"):\n sigAlg = tbsCert.signature\n h = h or hash_by_oid[sigAlg.algorithm.val]\n sigVal = self.sign(raw(tbsCert), h=h, t='pkcs')\n c = X509_Cert()\n c.tbsCertificate = tbsCert\n c.signatureAlgorithm = sigAlg\n c.signatureValue = _Raw_ASN1_BIT_STRING(sigVal, readable=True)\n return c", "response": "Signs a TBSCert and returns a X509_Cert object with the signature field set to the signatureValue of the TBSCert."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn True if the certificate is self - signed.", "response": "def isSelfSigned(self):\n \"\"\"\n Return True if the certificate is self-signed:\n - issuer and subject are the same\n - the signature of the certificate is valid.\n \"\"\"\n if self.issuer_hash == self.subject_hash:\n return self.isIssuerCert(self)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef isRevoked(self, crl_list):\n for c in crl_list:\n if (self.authorityKeyID is not None and\n c.authorityKeyID is not None and\n self.authorityKeyID == c.authorityKeyID):\n return self.serial in (x[0] for x in c.revoked_cert_serials)\n elif self.issuer == c.issuer:\n return self.serial in (x[0] for x in c.revoked_cert_serials)\n return False", "response": "This function returns True if the certificate is marked as revoked by one of the trusted CRLs in crl_list."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nexporting certificate in fmt format to file filename", "response": "def export(self, filename, fmt=\"DER\"):\n \"\"\"\n Export certificate in 'fmt' format (DER or PEM) to file 'filename'\n \"\"\"\n f = open(filename, \"wb\")\n if fmt == \"DER\":\n f.write(self.der)\n elif fmt == \"PEM\":\n f.write(self.pem)\n f.close()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nverifying the chain for this certificate.", "response": "def verifyChain(self, anchors, untrusted=None):\n \"\"\"\n Perform verification of certificate chains for that certificate.\n A list of anchors is required. The certificates in the optional\n untrusted list may be used as additional elements to the final chain.\n On par with chain instantiation, only one chain constructed with the\n untrusted candidates will be retained. Eventually, dates are checked.\n \"\"\"\n untrusted = untrusted or []\n for a in anchors:\n chain = Chain(self + untrusted, a)\n if len(chain) == 1: # anchor only\n continue\n # check that the chain does not exclusively rely on untrusted\n if any(c in chain[1:] for c in self):\n for c in chain:\n if c.remainingDays() < 0:\n break\n if c is chain[-1]: # we got to the end of the chain\n return chain\n return None"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nverifying the certificates in the cafile and return a list of anchors and untrusted certificates.", "response": "def verifyChainFromCAFile(self, cafile, untrusted_file=None):\n \"\"\"\n Does the same job as .verifyChain() but using the list of anchors\n from the cafile. As for .verifyChain(), a list of untrusted\n certificates can be passed (as a file, this time).\n \"\"\"\n try:\n f = open(cafile, \"rb\")\n ca_certs = f.read()\n f.close()\n except Exception:\n raise Exception(\"Could not read from cafile\")\n\n anchors = [Cert(c) for c in split_pem(ca_certs)]\n\n untrusted = None\n if untrusted_file:\n try:\n f = open(untrusted_file, \"rb\")\n untrusted_certs = f.read()\n f.close()\n except Exception:\n raise Exception(\"Could not read from untrusted_file\")\n untrusted = [Cert(c) for c in split_pem(untrusted_certs)]\n\n return self.verifyChain(anchors, untrusted)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef verifyChainFromCAPath(self, capath, untrusted_file=None):\n try:\n anchors = []\n for cafile in os.listdir(capath):\n anchors.append(Cert(open(os.path.join(capath, cafile), \"rb\").read())) # noqa: E501\n except Exception:\n raise Exception(\"capath provided is not a valid cert path\")\n\n untrusted = None\n if untrusted_file:\n try:\n f = open(untrusted_file, \"rb\")\n untrusted_certs = f.read()\n f.close()\n except Exception:\n raise Exception(\"Could not read from untrusted_file\")\n untrusted = [Cert(c) for c in split_pem(untrusted_certs)]\n\n return self.verifyChain(anchors, untrusted)", "response": "Verify the chain of certificates in a directory."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef answers(self, other):\n if other.__class__ == self.__class__:\n return (other.service + 0x40) == self.service or \\\n (self.service == 0x7f and\n self.request_service_id == other.service)\n return False", "response": "Returns True if self is an answer from other."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _sndrcv_snd(pks, timeout, inter, verbose, tobesent, hsent, timessent, stopevent): # noqa: E501\n try:\n i = 0\n rec_time = timessent is not None\n if verbose:\n print(\"Begin emission:\")\n for p in tobesent:\n # Populate the dictionary of _sndrcv_rcv\n # _sndrcv_rcv won't miss the answer of a packet that has not been sent # noqa: E501\n hsent.setdefault(p.hashret(), []).append(p)\n if stopevent.is_set():\n break\n # Send packet\n pks.send(p)\n if rec_time:\n timessent[i] = p.sent_time\n i += 1\n time.sleep(inter)\n if verbose:\n print(\"Finished sending %i packets.\" % i)\n except SystemExit:\n pass\n except KeyboardInterrupt:\n pass\n except Exception:\n log_runtime.exception(\"--- Error sending packets\")\n if timeout is not None:\n def _timeout(stopevent):\n stopevent.wait(timeout)\n stopevent.set()\n thread = threading.Thread(\n target=_timeout, args=(stopevent,)\n )\n thread.setDaemon(True)\n thread.start()", "response": "Function used in the sending thread of sndrcv"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _sndrcv_rcv(pks, hsent, stopevent, nbrecv, notans, verbose, chainCC,\n multi, _storage_policy=None):\n \"\"\"Function used to receive packets and check their hashret\"\"\"\n if not _storage_policy:\n _storage_policy = lambda x, y: (x, y)\n ans = []\n\n def _get_pkt():\n # SuperSocket.select() returns, according to each socket type,\n # the selected sockets + the function to recv() the packets (or None)\n # (when sockets aren't selectable, should be nonblock_recv)\n selected, read_func = pks.select([pks])\n read_func = read_func or pks.__class__.recv\n if selected:\n return read_func(selected[0])\n\n try:\n while True:\n r = _get_pkt()\n if stopevent.is_set():\n break\n if r is None:\n continue\n ok = False\n h = r.hashret()\n if h in hsent:\n hlst = hsent[h]\n for i, sentpkt in enumerate(hlst):\n if r.answers(sentpkt):\n ans.append(_storage_policy(sentpkt, r))\n if verbose > 1:\n os.write(1, b\"*\")\n ok = True\n if not multi:\n del hlst[i]\n notans -= 1\n else:\n if not hasattr(sentpkt, '_answered'):\n notans -= 1\n sentpkt._answered = 1\n break\n if notans == 0 and not multi:\n del r\n break\n if not ok:\n if verbose > 1:\n os.write(1, b\".\")\n nbrecv += 1\n if conf.debug_match:\n debug.recv.append(r)\n del r\n except KeyboardInterrupt:\n if chainCC:\n raise\n finally:\n stopevent.set()\n return (hsent, ans, nbrecv, notans)", "response": "Function used to receive packets and check their hashret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef send(x, inter=0, loop=0, count=None,\n verbose=None, realtime=None,\n return_packets=False, socket=None, *args, **kargs):\n \"\"\"Send packets at layer 3\nsend(packets, [inter=0], [loop=0], [count=None], [verbose=conf.verb], [realtime=None], [return_packets=False], # noqa: E501\n [socket=None]) -> None\"\"\"\n need_closing = socket is None\n socket = socket or conf.L3socket(*args, **kargs)\n results = __gen_send(socket, x, inter=inter, loop=loop,\n count=count, verbose=verbose,\n realtime=realtime, return_packets=return_packets)\n if need_closing:\n socket.close()\n return results", "response": "Send a packet at layer 3."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nsending packets at layer 2.", "response": "def sendp(x, inter=0, loop=0, iface=None, iface_hint=None, count=None,\n verbose=None, realtime=None,\n return_packets=False, socket=None, *args, **kargs):\n \"\"\"Send packets at layer 2\nsendp(packets, [inter=0], [loop=0], [iface=None], [iface_hint=None], [count=None], [verbose=conf.verb], # noqa: E501\n [realtime=None], [return_packets=False], [socket=None]) -> None\"\"\"\n if iface is None and iface_hint is not None and socket is None:\n iface = conf.route.route(iface_hint)[0]\n need_closing = socket is None\n socket = socket or conf.L2socket(iface=iface, *args, **kargs)\n results = __gen_send(socket, x, inter=inter, loop=loop,\n count=count, verbose=verbose,\n realtime=realtime, return_packets=return_packets)\n if need_closing:\n socket.close()\n return results"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nsend packets at layer 2 using tcpreplay for performance", "response": "def sendpfast(x, pps=None, mbps=None, realtime=None, loop=0, file_cache=False, iface=None, replay_args=None, # noqa: E501\n parse_results=False):\n \"\"\"Send packets at layer 2 using tcpreplay for performance\n pps: packets per second\n mpbs: MBits per second\n realtime: use packet's timestamp, bending time with real-time value\n loop: number of times to process the packet list\n file_cache: cache packets in RAM instead of reading from disk at each iteration # noqa: E501\n iface: output interface\n replay_args: List of additional tcpreplay args (List[str])\n parse_results: Return a dictionary of information outputted by tcpreplay (default=False) # noqa: E501\n :returns stdout, stderr, command used\"\"\"\n if iface is None:\n iface = conf.iface\n argv = [conf.prog.tcpreplay, \"--intf1=%s\" % iface]\n if pps is not None:\n argv.append(\"--pps=%i\" % pps)\n elif mbps is not None:\n argv.append(\"--mbps=%f\" % mbps)\n elif realtime is not None:\n argv.append(\"--multiplier=%f\" % realtime)\n else:\n argv.append(\"--topspeed\")\n\n if loop:\n argv.append(\"--loop=%i\" % loop)\n if file_cache:\n argv.append(\"--preload-pcap\")\n\n # Check for any additional args we didn't cover.\n if replay_args is not None:\n argv.extend(replay_args)\n\n f = get_temp_file()\n argv.append(f)\n wrpcap(f, x)\n results = None\n with ContextManagerSubprocess(\"sendpfast()\", conf.prog.tcpreplay):\n try:\n cmd = subprocess.Popen(argv, stdout=subprocess.PIPE,\n stderr=subprocess.PIPE)\n except KeyboardInterrupt:\n log_interactive.info(\"Interrupted by user\")\n except Exception:\n os.unlink(f)\n raise\n else:\n stdout, stderr = cmd.communicate()\n if stderr:\n log_runtime.warning(stderr.decode())\n if parse_results:\n results = _parse_tcpreplay_result(stdout, stderr, argv)\n elif conf.verb > 2:\n log_runtime.info(stdout.decode())\n os.unlink(f)\n return results"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nparse the output of tcpreplay and modify the results_dict to populate the output information.", "response": "def _parse_tcpreplay_result(stdout, stderr, argv):\n \"\"\"\n Parse the output of tcpreplay and modify the results_dict to populate output information. # noqa: E501\n Tested with tcpreplay v3.4.4\n Tested with tcpreplay v4.1.2\n :param stdout: stdout of tcpreplay subprocess call\n :param stderr: stderr of tcpreplay subprocess call\n :param argv: the command used in the subprocess call\n :return: dictionary containing the results\n \"\"\"\n try:\n results = {}\n stdout = plain_str(stdout).lower()\n stderr = plain_str(stderr).strip().split(\"\\n\")\n elements = {\n \"actual\": (int, int, float),\n \"rated\": (float, float, float),\n \"flows\": (int, float, int, int),\n \"attempted\": (int,),\n \"successful\": (int,),\n \"failed\": (int,),\n \"truncated\": (int,),\n \"retried packets (eno\": (int,),\n \"retried packets (eag\": (int,),\n }\n multi = {\n \"actual\": (\"packets\", \"bytes\", \"time\"),\n \"rated\": (\"bps\", \"mbps\", \"pps\"),\n \"flows\": (\"flows\", \"fps\", \"flow_packets\", \"non_flow\"),\n \"retried packets (eno\": (\"retried_enobufs\",),\n \"retried packets (eag\": (\"retried_eagain\",),\n }\n float_reg = r\"([0-9]*\\.[0-9]+|[0-9]+)\"\n int_reg = r\"([0-9]+)\"\n any_reg = r\"[^0-9]*\"\n r_types = {int: int_reg, float: float_reg}\n for line in stdout.split(\"\\n\"):\n line = line.strip()\n for elt, _types in elements.items():\n if line.startswith(elt):\n regex = any_reg.join([r_types[x] for x in _types])\n matches = re.search(regex, line)\n for i, typ in enumerate(_types):\n name = multi.get(elt, [elt])[i]\n results[name] = typ(matches.group(i + 1))\n results[\"command\"] = \" \".join(argv)\n results[\"warnings\"] = stderr[:-1]\n return results\n except Exception as parse_exception:\n if not conf.interactive:\n raise\n log_runtime.error(\"Error parsing output: \" + str(parse_exception))\n return {}"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef sr(x, promisc=None, filter=None, iface=None, nofilter=0, *args, **kargs):\n s = conf.L3socket(promisc=promisc, filter=filter,\n iface=iface, nofilter=nofilter)\n result = sndrcv(s, x, *args, **kargs)\n s.close()\n return result", "response": "Send and receive packets at layer 3"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nsend packets at layer 3 and return only the first answer", "response": "def sr1(x, promisc=None, filter=None, iface=None, nofilter=0, *args, **kargs):\n \"\"\"Send packets at layer 3 and return only the first answer\"\"\"\n s = conf.L3socket(promisc=promisc, filter=filter,\n nofilter=nofilter, iface=iface)\n ans, _ = sndrcv(s, x, *args, **kargs)\n s.close()\n if len(ans) > 0:\n return ans[0][1]\n else:\n return None"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef srp(x, promisc=None, iface=None, iface_hint=None, filter=None,\n nofilter=0, type=ETH_P_ALL, *args, **kargs):\n \"\"\"Send and receive packets at layer 2\"\"\"\n if iface is None and iface_hint is not None:\n iface = conf.route.route(iface_hint)[0]\n s = conf.L2socket(promisc=promisc, iface=iface,\n filter=filter, nofilter=nofilter, type=type)\n result = sndrcv(s, x, *args, **kargs)\n s.close()\n return result", "response": "Send and receive packets at layer 2"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nsend and receive packets at layer 2 and return only the first answer", "response": "def srp1(*args, **kargs):\n \"\"\"Send and receive packets at layer 2 and return only the first answer\"\"\"\n ans, _ = srp(*args, **kargs)\n if len(ans) > 0:\n return ans[0][1]\n else:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nflood and receive packets at layer 3 prn", "response": "def srflood(x, promisc=None, filter=None, iface=None, nofilter=None, *args, **kargs): # noqa: E501\n \"\"\"Flood and receive packets at layer 3\nprn: function applied to packets received\nunique: only consider packets whose print\nnofilter: put 1 to avoid use of BPF filters\nfilter: provide a BPF filter\niface: listen answers only on the given interface\"\"\"\n s = conf.L3socket(promisc=promisc, filter=filter, iface=iface, nofilter=nofilter) # noqa: E501\n r = sndrcvflood(s, x, *args, **kargs)\n s.close()\n return r"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef sr1flood(x, promisc=None, filter=None, iface=None, nofilter=0, *args, **kargs): # noqa: E501\n s = conf.L3socket(promisc=promisc, filter=filter, nofilter=nofilter, iface=iface) # noqa: E501\n ans, _ = sndrcvflood(s, x, *args, **kargs)\n s.close()\n if len(ans) > 0:\n return ans[0][1]\n else:\n return None", "response": "Flood and receive packets at layer 3 and return only the first answer\nprn : function applied to packets received\nnverbose : put 1 to avoid use of BPF filter\niface : give a BPF interface"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef srpflood(x, promisc=None, filter=None, iface=None, iface_hint=None, nofilter=None, *args, **kargs): # noqa: E501\n if iface is None and iface_hint is not None:\n iface = conf.route.route(iface_hint)[0]\n s = conf.L2socket(promisc=promisc, filter=filter, iface=iface, nofilter=nofilter) # noqa: E501\n r = sndrcvflood(s, x, *args, **kargs)\n s.close()\n return r", "response": "Flood and receive packets at layer 2\nprn"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nflooding and receive packets at layer 2 and return only the first answer prn : function applied to packets received nverbose levelofilter nofilter iface provide a BPF filter iface provide a BPF filter iface provide a BPF filter iface provide a BPF iface", "response": "def srp1flood(x, promisc=None, filter=None, iface=None, nofilter=0, *args, **kargs): # noqa: E501\n \"\"\"Flood and receive packets at layer 2 and return only the first answer\nprn: function applied to packets received\nverbose: set verbosity level\nnofilter: put 1 to avoid use of BPF filters\nfilter: provide a BPF filter\niface: listen answers only on the given interface\"\"\"\n s = conf.L2socket(promisc=promisc, filter=filter, nofilter=nofilter, iface=iface) # noqa: E501\n ans, _ = sndrcvflood(s, x, *args, **kargs)\n s.close()\n if len(ans) > 0:\n return ans[0][1]\n else:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef sniff(count=0, store=True, offline=None, prn=None, lfilter=None,\n L2socket=None, timeout=None, opened_socket=None,\n stop_filter=None, iface=None, started_callback=None,\n session=None, *arg, **karg):\n \"\"\"Sniff packets and return a list of packets.\n\n Args:\n count: number of packets to capture. 0 means infinity.\n store: whether to store sniffed packets or discard them\n prn: function to apply to each packet. If something is returned, it\n is displayed.\n --Ex: prn = lambda x: x.summary()\n session: a session = a flow decoder used to handle stream of packets.\n e.g: IPSession (to defragment on-the-flow) or NetflowSession\n filter: BPF filter to apply.\n lfilter: Python function applied to each packet to determine if\n further action may be done.\n --Ex: lfilter = lambda x: x.haslayer(Padding)\n offline: PCAP file (or list of PCAP files) to read packets from,\n instead of sniffing them\n timeout: stop sniffing after a given time (default: None).\n L2socket: use the provided L2socket (default: use conf.L2listen).\n opened_socket: provide an object (or a list of objects) ready to use\n .recv() on.\n stop_filter: Python function applied to each packet to determine if\n we have to stop the capture after this packet.\n --Ex: stop_filter = lambda x: x.haslayer(TCP)\n iface: interface or list of interfaces (default: None for sniffing\n on all interfaces).\n monitor: use monitor mode. May not be available on all OS\n started_callback: called as soon as the sniffer starts sniffing\n (default: None).\n\n The iface, offline and opened_socket parameters can be either an\n element, a list of elements, or a dict object mapping an element to a\n label (see examples below).\n\n Examples:\n >>> sniff(filter=\"arp\")\n >>> sniff(filter=\"tcp\",\n ... session=IPSession, # defragment on-the-flow\n ... prn=lambda x: x.summary())\n >>> sniff(lfilter=lambda pkt: ARP in pkt)\n >>> sniff(iface=\"eth0\", prn=Packet.summary)\n >>> sniff(iface=[\"eth0\", \"mon0\"],\n ... prn=lambda pkt: \"%s: %s\" % (pkt.sniffed_on,\n ... pkt.summary()))\n >>> sniff(iface={\"eth0\": \"Ethernet\", \"mon0\": \"Wifi\"},\n ... prn=lambda pkt: \"%s: %s\" % (pkt.sniffed_on,\n ... pkt.summary()))\n \"\"\"\n c = 0\n session = session or DefaultSession\n session = session(prn, store) # instantiate session\n sniff_sockets = {} # socket: label dict\n if opened_socket is not None:\n if isinstance(opened_socket, list):\n sniff_sockets.update((s, \"socket%d\" % i)\n for i, s in enumerate(opened_socket))\n elif isinstance(opened_socket, dict):\n sniff_sockets.update((s, label)\n for s, label in six.iteritems(opened_socket))\n else:\n sniff_sockets[opened_socket] = \"socket0\"\n if offline is not None:\n flt = karg.get('filter')\n if isinstance(offline, list):\n sniff_sockets.update((PcapReader(\n fname if flt is None else\n tcpdump(fname, args=[\"-w\", \"-\", flt], getfd=True)\n ), fname) for fname in offline)\n elif isinstance(offline, dict):\n sniff_sockets.update((PcapReader(\n fname if flt is None else\n tcpdump(fname, args=[\"-w\", \"-\", flt], getfd=True)\n ), label) for fname, label in six.iteritems(offline))\n else:\n sniff_sockets[PcapReader(\n offline if flt is None else\n tcpdump(offline, args=[\"-w\", \"-\", flt], getfd=True)\n )] = offline\n if not sniff_sockets or iface is not None:\n if L2socket is None:\n L2socket = conf.L2listen\n if isinstance(iface, list):\n sniff_sockets.update(\n (L2socket(type=ETH_P_ALL, iface=ifname, *arg, **karg), ifname)\n for ifname in iface\n )\n elif isinstance(iface, dict):\n sniff_sockets.update(\n (L2socket(type=ETH_P_ALL, iface=ifname, *arg, **karg), iflabel)\n for ifname, iflabel in six.iteritems(iface)\n )\n else:\n sniff_sockets[L2socket(type=ETH_P_ALL, iface=iface,\n *arg, **karg)] = iface\n if timeout is not None:\n stoptime = time.time() + timeout\n remain = None\n\n # Get select information from the sockets\n _main_socket = next(iter(sniff_sockets))\n read_allowed_exceptions = _main_socket.read_allowed_exceptions\n select_func = _main_socket.select\n # We check that all sockets use the same select(), or raise a warning\n if not all(select_func == sock.select for sock in sniff_sockets):\n warning(\"Warning: inconsistent socket types ! The used select function\"\n \"will be the one of the first socket\")\n # Now let's build the select function, used later on\n _select = lambda sockets, remain: select_func(sockets, remain)[0]\n\n try:\n if started_callback:\n started_callback()\n continue_sniff = True\n while sniff_sockets and continue_sniff:\n if timeout is not None:\n remain = stoptime - time.time()\n if remain <= 0:\n break\n for s in _select(sniff_sockets, remain):\n try:\n p = s.recv()\n except socket.error as ex:\n warning(\"Socket %s failed with '%s' and thus\"\n \" will be ignored\" % (s, ex))\n del sniff_sockets[s]\n continue\n except read_allowed_exceptions:\n continue\n if p is None:\n try:\n if s.promisc:\n continue\n except AttributeError:\n pass\n del sniff_sockets[s]\n break\n if lfilter and not lfilter(p):\n continue\n p.sniffed_on = sniff_sockets[s]\n c += 1\n # on_packet_received handles the prn/storage\n session.on_packet_received(p)\n if stop_filter and stop_filter(p):\n continue_sniff = False\n break\n if 0 < count <= c:\n continue_sniff = False\n break\n except KeyboardInterrupt:\n pass\n if opened_socket is None:\n for s in sniff_sockets:\n s.close()\n return session.toPacketList()", "response": "Sniff packets and return a list of packets."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nbridges and sniff packets between interfaces if1 and if2.", "response": "def bridge_and_sniff(if1, if2, xfrm12=None, xfrm21=None, prn=None, L2socket=None, # noqa: E501\n *args, **kargs):\n \"\"\"Forward traffic between interfaces if1 and if2, sniff and return\nthe exchanged packets.\n\nArguments:\n\n if1, if2: the interfaces to use (interface names or opened sockets).\n\n xfrm12: a function to call when forwarding a packet from if1 to\n if2. If it returns True, the packet is forwarded as it. If it\n returns False or None, the packet is discarded. If it returns a\n packet, this packet is forwarded instead of the original packet\n one.\n\n xfrm21: same as xfrm12 for packets forwarded from if2 to if1.\n\n The other arguments are the same than for the function sniff(),\n except for offline, opened_socket and iface that are ignored.\n See help(sniff) for more.\n\n \"\"\"\n for arg in ['opened_socket', 'offline', 'iface']:\n if arg in kargs:\n log_runtime.warning(\"Argument %s cannot be used in \"\n \"bridge_and_sniff() -- ignoring it.\", arg)\n del kargs[arg]\n\n def _init_socket(iface, count):\n if isinstance(iface, SuperSocket):\n return iface, \"iface%d\" % count\n else:\n return (L2socket or conf.L2socket)(iface=iface), iface\n sckt1, if1 = _init_socket(if1, 1)\n sckt2, if2 = _init_socket(if2, 2)\n peers = {if1: sckt2, if2: sckt1}\n xfrms = {}\n if xfrm12 is not None:\n xfrms[if1] = xfrm12\n if xfrm21 is not None:\n xfrms[if2] = xfrm21\n\n def prn_send(pkt):\n try:\n sendsock = peers[pkt.sniffed_on]\n except KeyError:\n return\n if pkt.sniffed_on in xfrms:\n try:\n newpkt = xfrms[pkt.sniffed_on](pkt)\n except Exception:\n log_runtime.warning(\n 'Exception in transformation function for packet [%s] '\n 'received on %s -- dropping',\n pkt.summary(), pkt.sniffed_on, exc_info=True\n )\n return\n else:\n if newpkt is True:\n newpkt = pkt.original\n elif not newpkt:\n return\n else:\n newpkt = pkt.original\n try:\n sendsock.send(newpkt)\n except Exception:\n log_runtime.warning('Cannot forward packet [%s] received on %s',\n pkt.summary(), pkt.sniffed_on, exc_info=True)\n if prn is None:\n prn = prn_send\n else:\n prn_orig = prn\n\n def prn(pkt):\n prn_send(pkt)\n return prn_orig(pkt)\n\n return sniff(opened_socket={sckt1: if1, sckt2: if2}, prn=prn,\n *args, **kargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsniffing packets and print them calling pkt. summary", "response": "def tshark(*args, **kargs):\n \"\"\"Sniff packets and print them calling pkt.summary(), a bit like text wireshark\"\"\" # noqa: E501\n print(\"Capturing on '\" + str(kargs.get('iface') if 'iface' in kargs else conf.iface) + \"'\") # noqa: E501\n i = [0] # This should be a nonlocal variable, using a mutable object for Python 2 compatibility # noqa: E501\n\n def _cb(pkt):\n print(\"%5d\\t%s\" % (i[0], pkt.summary()))\n i[0] += 1\n sniff(prn=_cb, store=False, *args, **kargs)\n print(\"\\n%d packet%s captured\" % (i[0], 's' if i[0] > 1 else ''))"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef ikev2scan(ip, **kwargs):\n return sr(IP(dst=ip) / UDP() / IKEv2(init_SPI=RandString(8),\n exch_type=34) / IKEv2_payload_SA(prop=IKEv2_payload_Proposal()), **kwargs)", "response": "Send a IKEv2 SA to an IP and wait for answers."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _mib_register(ident, value, the_mib, unresolved):\n if ident in the_mib or ident in unresolved:\n return ident in the_mib\n resval = []\n not_resolved = 0\n for v in value:\n if _mib_re_integer.match(v):\n resval.append(v)\n else:\n v = fixname(plain_str(v))\n if v not in the_mib:\n not_resolved = 1\n if v in the_mib:\n v = the_mib[v]\n elif v in unresolved:\n v = unresolved[v]\n if isinstance(v, list):\n resval += v\n else:\n resval.append(v)\n if not_resolved:\n unresolved[ident] = resval\n return False\n else:\n the_mib[ident] = resval\n keys = list(unresolved)\n i = 0\n while i < len(keys):\n k = keys[i]\n if _mib_register(k, unresolved[k], the_mib, {}):\n del(unresolved[k])\n del(keys[i])\n i = 0\n else:\n i += 1\n\n return True", "response": "Internal function used to register an OID and its name in a MIBDict"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef load_mib(filenames):\n the_mib = {'iso': ['1']}\n unresolved = {}\n for k in six.iterkeys(conf.mib):\n _mib_register(conf.mib[k], k.split(\".\"), the_mib, unresolved)\n\n if isinstance(filenames, (str, bytes)):\n filenames = [filenames]\n for fnames in filenames:\n for fname in glob(fnames):\n f = open(fname)\n text = f.read()\n cleantext = \" \".join(_mib_re_strings.split(\" \".join(_mib_re_comments.split(text)))) # noqa: E501\n for m in _mib_re_oiddecl.finditer(cleantext):\n gr = m.groups()\n ident, oid = gr[0], gr[-1]\n ident = fixname(ident)\n oid = oid.split()\n for i, elt in enumerate(oid):\n m = _mib_re_both.match(elt)\n if m:\n oid[i] = m.groups()[1]\n _mib_register(ident, oid, the_mib, unresolved)\n\n newmib = MIBDict(_name=\"MIB\")\n for oid, key in six.iteritems(the_mib):\n newmib[\".\".join(key)] = oid\n for oid, key in six.iteritems(unresolved):\n newmib[\".\".join(key)] = oid\n\n conf.mib = newmib", "response": "Load the conf. mib dict from a list of filenames"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nparsing the OID id or OID generator and return real OID", "response": "def _oid(self, x):\n \"\"\"Parse the OID id/OID generator, and return real OID\"\"\"\n xl = x.strip(\".\").split(\".\")\n p = len(xl) - 1\n while p >= 0 and _mib_re_integer.match(xl[p]):\n p -= 1\n if p != 0 or xl[p] not in six.itervalues(self.__dict__):\n return x\n xl[p] = next(k for k, v in six.iteritems(self.__dict__) if v == xl[p])\n return \".\".join(xl[p:])"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting the total size of all under layers", "response": "def _get_underlayers_size(self):\n \"\"\"\n get the total size of all under layers\n :return: number of bytes\n \"\"\"\n\n under_layer = self.underlayer\n\n under_layers_size = 0\n\n while under_layer and isinstance(under_layer, Dot1Q):\n under_layers_size += 4\n under_layer = under_layer.underlayer\n\n if under_layer and isinstance(under_layer, Ether):\n # ether header len + FCS len\n under_layers_size += 14 + 4\n\n return under_layers_size"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nadd padding to the frame if required.", "response": "def post_build(self, pkt, pay):\n \"\"\"\n add padding to the frame if required.\n\n note that padding is only added if pay is None/empty. this allows us to add # noqa: E501\n any payload after the MACControl* PDU if needed (piggybacking).\n \"\"\"\n\n if not pay:\n under_layers_size = self._get_underlayers_size()\n\n frame_size = (len(pkt) + under_layers_size)\n\n if frame_size < 64:\n return pkt + b'\\x00' * (64 - frame_size)\n\n return pkt + pay"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting pause time for given link speed in seconds", "response": "def get_pause_time(self, speed=ETHER_SPEED_MBIT_1000):\n \"\"\"\n get pause time for given link speed in seconds\n\n :param speed: select link speed to get the pause time for, must be ETHER_SPEED_MBIT_[10,100,1000] # noqa: E501\n :return: pause time in seconds\n :raises MACControlInvalidSpeedException: on invalid speed selector\n \"\"\"\n\n try:\n return self.pause_time * {\n ETHER_SPEED_MBIT_10: (0.0000001 * 512),\n ETHER_SPEED_MBIT_100: (0.00000001 * 512),\n ETHER_SPEED_MBIT_1000: (0.000000001 * 512 * 2)\n }[speed]\n except KeyError:\n raise MACControlInvalidSpeedException('Invalid speed selector given. ' # noqa: E501\n 'Must be one of ETHER_SPEED_MBIT_[10,100,1000]')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreading a candump log file and return a list of CAN packets.", "response": "def rdcandump(filename, count=None,\n is_not_log_file_format=False,\n interface=None):\n \"\"\"Read a candump log file and return a packet list\n\ncount: read only packets\nis_not_log_file_format: read input with candumps stdout format\ninterfaces: return only packets from a specified interface\n\n \"\"\"\n try:\n if isinstance(filename, six.string_types):\n file = open(filename, \"rb\")\n else:\n file = filename\n\n pkts = list()\n ifilter = None\n if interface is not None:\n if isinstance(interface, six.string_types):\n ifilter = [interface]\n else:\n ifilter = interface\n\n for l in file.readlines():\n if is_not_log_file_format:\n h, data = l.split(b']')\n intf, idn, le = h.split()\n t = None\n else:\n t, intf, f = l.split()\n idn, data = f.split(b'#')\n le = None\n t = float(t[1:-1])\n\n if ifilter is not None and intf.decode('ASCII') not in ifilter:\n continue\n\n data = data.replace(b' ', b'')\n data = data.strip()\n\n pkt = CAN(identifier=int(idn, 16), data=binascii.unhexlify(data))\n if le is not None:\n pkt.length = int(le[1:])\n else:\n pkt.length = len(pkt.data)\n\n if len(idn) > 3:\n pkt.flags = 0b100\n\n if t is not None:\n pkt.time = t\n\n pkts.append(pkt)\n if count is not None and len(pkts) >= count:\n break\n\n finally:\n file.close()\n\n return pkts"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef inv_endianness(pkt):\n len_partial = len(pkt) - 4 # len of the packet, CAN ID excluded\n return struct.pack('I{}s'.format(len_partial), pkt))", "response": "Invert the order of the first four bytes of a CAN packet"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nimplements the swap-bytes functionality when building this is based on a copy of the Packet.self_build default method. The goal is to affect only the CAN layer data and keep under layers (e.g LinuxCooked) unchanged", "response": "def post_build(self, pkt, pay):\n \"\"\" Implements the swap-bytes functionality when building\n\n this is based on a copy of the Packet.self_build default method.\n The goal is to affect only the CAN layer data and keep\n under layers (e.g LinuxCooked) unchanged\n \"\"\"\n if conf.contribs['CAN']['swap-bytes']:\n return CAN.inv_endianness(pkt) + pay\n return pkt + pay"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef pkt2uptime(pkt, HZ=100):\n if not isinstance(pkt, Packet):\n raise TypeError(\"Not a TCP packet\")\n if isinstance(pkt, NoPayload):\n raise TypeError(\"Not a TCP packet\")\n if not isinstance(pkt, TCP):\n return pkt2uptime(pkt.payload)\n for opt in pkt.options:\n if opt[0] == \"Timestamp\":\n # t = pkt.time - opt[1][0] * 1.0/HZ\n # return time.ctime(t)\n t = opt[1][0] / HZ\n return t\n raise TypeError(\"No timestamp option\")", "response": "Calculate the date the machine which emitted the packet booted using TCP timestamp"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p0f_impersonate(pkt, osgenre=None, osdetails=None, signature=None,\n extrahops=0, mtu=1500, uptime=None):\n \"\"\"Modifies pkt so that p0f will think it has been sent by a\nspecific OS. If osdetails is None, then we randomly pick up a\npersonality matching osgenre. If osgenre and signature are also None,\nwe use a local signature (using p0f_getlocalsigs). If signature is\nspecified (as a tuple), we use the signature.\n\nFor now, only TCP Syn packets are supported.\nSome specifications of the p0f.fp file are not (yet) implemented.\"\"\"\n pkt = pkt.copy()\n # pkt = pkt.__class__(raw(pkt))\n while pkt.haslayer(IP) and pkt.haslayer(TCP):\n pkt = pkt.getlayer(IP)\n if isinstance(pkt.payload, TCP):\n break\n pkt = pkt.payload\n\n if not isinstance(pkt, IP) or not isinstance(pkt.payload, TCP):\n raise TypeError(\"Not a TCP/IP packet\")\n\n db = p0f_selectdb(pkt.payload.flags)\n if osgenre:\n pb = db.get_base()\n if pb is None:\n pb = []\n pb = [x for x in pb if x[6] == osgenre]\n if osdetails:\n pb = [x for x in pb if x[7] == osdetails]\n elif signature:\n pb = [signature]\n else:\n pb = p0f_getlocalsigs()[db]\n if db == p0fr_kdb:\n # 'K' quirk <=> RST+ACK\n if pkt.payload.flags & 0x4 == 0x4:\n pb = [x for x in pb if 'K' in x[5]]\n else:\n pb = [x for x in pb if 'K' not in x[5]]\n if not pb:\n raise Scapy_Exception(\"No match in the p0f database\")\n pers = pb[random.randint(0, len(pb) - 1)]\n\n # options (we start with options because of MSS)\n # Take the options already set as \"hints\" to use in the new packet if we\n # can. MSS, WScale and Timestamp can all be wildcarded in a signature, so\n # we'll use the already-set values if they're valid integers.\n orig_opts = dict(pkt.payload.options)\n int_only = lambda val: val if isinstance(val, six.integer_types) else None\n mss_hint = int_only(orig_opts.get('MSS'))\n wscale_hint = int_only(orig_opts.get('WScale'))\n ts_hint = [int_only(o) for o in orig_opts.get('Timestamp', (None, None))]\n\n options = []\n if pers[4] != '.':\n for opt in pers[4].split(','):\n if opt[0] == 'M':\n # MSS might have a maximum size because of window size\n # specification\n if pers[0][0] == 'S':\n maxmss = (2**16 - 1) // int(pers[0][1:])\n else:\n maxmss = (2**16 - 1)\n # disregard hint if out of range\n if mss_hint and not 0 <= mss_hint <= maxmss:\n mss_hint = None\n # If we have to randomly pick up a value, we cannot use\n # scapy RandXXX() functions, because the value has to be\n # set in case we need it for the window size value. That's\n # why we use random.randint()\n if opt[1:] == '*':\n if mss_hint is not None:\n options.append(('MSS', mss_hint))\n else:\n options.append(('MSS', random.randint(1, maxmss)))\n elif opt[1] == '%':\n coef = int(opt[2:])\n if mss_hint is not None and mss_hint % coef == 0:\n options.append(('MSS', mss_hint))\n else:\n options.append((\n 'MSS', coef * random.randint(1, maxmss // coef)))\n else:\n options.append(('MSS', int(opt[1:])))\n elif opt[0] == 'W':\n if wscale_hint and not 0 <= wscale_hint < 2**8:\n wscale_hint = None\n if opt[1:] == '*':\n if wscale_hint is not None:\n options.append(('WScale', wscale_hint))\n else:\n options.append(('WScale', RandByte()))\n elif opt[1] == '%':\n coef = int(opt[2:])\n if wscale_hint is not None and wscale_hint % coef == 0:\n options.append(('WScale', wscale_hint))\n else:\n options.append((\n 'WScale', coef * RandNum(min=1, max=(2**8 - 1) // coef))) # noqa: E501\n else:\n options.append(('WScale', int(opt[1:])))\n elif opt == 'T0':\n options.append(('Timestamp', (0, 0)))\n elif opt == 'T':\n # Determine first timestamp.\n if uptime is not None:\n ts_a = uptime\n elif ts_hint[0] and 0 < ts_hint[0] < 2**32:\n # Note: if first ts is 0, p0f registers it as \"T0\" not \"T\",\n # hence we don't want to use the hint if it was 0.\n ts_a = ts_hint[0]\n else:\n ts_a = random.randint(120, 100 * 60 * 60 * 24 * 365)\n # Determine second timestamp.\n if 'T' not in pers[5]:\n ts_b = 0\n elif ts_hint[1] and 0 < ts_hint[1] < 2**32:\n ts_b = ts_hint[1]\n else:\n # FIXME: RandInt() here does not work (bug (?) in\n # TCPOptionsField.m2i often raises \"OverflowError:\n # long int too large to convert to int\" in:\n # oval = struct.pack(ofmt, *oval)\"\n # Actually, this is enough to often raise the error:\n # struct.pack('I', RandInt())\n ts_b = random.randint(1, 2**32 - 1)\n options.append(('Timestamp', (ts_a, ts_b)))\n elif opt == 'S':\n options.append(('SAckOK', ''))\n elif opt == 'N':\n options.append(('NOP', None))\n elif opt == 'E':\n options.append(('EOL', None))\n elif opt[0] == '?':\n if int(opt[1:]) in TCPOptions[0]:\n optname = TCPOptions[0][int(opt[1:])][0]\n optstruct = TCPOptions[0][int(opt[1:])][1]\n options.append((optname,\n struct.unpack(optstruct,\n RandString(struct.calcsize(optstruct))._fix()))) # noqa: E501\n else:\n options.append((int(opt[1:]), ''))\n # FIXME: qqP not handled\n else:\n warning(\"unhandled TCP option \" + opt)\n pkt.payload.options = options\n\n # window size\n if pers[0] == '*':\n pkt.payload.window = RandShort()\n elif pers[0].isdigit():\n pkt.payload.window = int(pers[0])\n elif pers[0][0] == '%':\n coef = int(pers[0][1:])\n pkt.payload.window = coef * RandNum(min=1, max=(2**16 - 1) // coef)\n elif pers[0][0] == 'T':\n pkt.payload.window = mtu * int(pers[0][1:])\n elif pers[0][0] == 'S':\n # needs MSS set\n mss = [x for x in options if x[0] == 'MSS']\n if not mss:\n raise Scapy_Exception(\"TCP window value requires MSS, and MSS option not set\") # noqa: E501\n pkt.payload.window = mss[0][1] * int(pers[0][1:])\n else:\n raise Scapy_Exception('Unhandled window size specification')\n\n # ttl\n pkt.ttl = pers[1] - extrahops\n # DF flag\n pkt.flags |= (2 * pers[2])\n # FIXME: ss (packet size) not handled (how ? may be with D quirk\n # if present)\n # Quirks\n if pers[5] != '.':\n for qq in pers[5]:\n # FIXME: not handled: P, I, X, !\n # T handled with the Timestamp option\n if qq == 'Z':\n pkt.id = 0\n elif qq == 'U':\n pkt.payload.urgptr = RandShort()\n elif qq == 'A':\n pkt.payload.ack = RandInt()\n elif qq == 'F':\n if db == p0fo_kdb:\n pkt.payload.flags |= 0x20 # U\n else:\n pkt.payload.flags |= random.choice([8, 32, 40]) # P/U/PU\n elif qq == 'D' and db != p0fo_kdb:\n pkt /= conf.raw_layer(load=RandString(random.randint(1, 10))) # XXX p0fo.fp # noqa: E501\n elif qq == 'Q':\n pkt.payload.seq = pkt.payload.ack\n # elif qq == '0': pkt.payload.seq = 0\n # if db == p0fr_kdb:\n # '0' quirk is actually not only for p0fr.fp (see\n # packet2p0f())\n if '0' in pers[5]:\n pkt.payload.seq = 0\n elif pkt.payload.seq == 0:\n pkt.payload.seq = RandInt()\n\n while pkt.underlayer:\n pkt = pkt.underlayer\n return pkt", "response": "Modifies a packet so that it has been sent by a specific OS."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngenerates the response block of this request.", "response": "def get_response(self):\n \"\"\"Generate the response block of this request.\n Careful: it only sets the fields which can be set from the request\n \"\"\"\n res = IODControlRes()\n for field in [\"ARUUID\", \"SessionKey\", \"AlarmSequenceNumber\"]:\n res.setfieldval(field, self.getfieldval(field))\n\n res.block_type = self.block_type + 0x8000\n return res"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_response(self):\n res = IODWriteRes()\n for field in [\"seqNum\", \"ARUUID\", \"API\", \"slotNumber\",\n \"subslotNumber\", \"index\"]:\n res.setfieldval(field, self.getfieldval(field))\n return res", "response": "Generate the response block of this request."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef i2len(self, pkt, val):\n fld_len = self._fld.i2len(pkt, val)\n return fld_len + self.padlen(fld_len)", "response": "get the length of the field including the padding length"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ngenerate the response block of this request.", "response": "def get_response(self):\n \"\"\"Generate the response block of this request.\n Careful: it only sets the fields which can be set from the request\n \"\"\"\n res = IODWriteMultipleRes()\n for field in [\"seqNum\", \"ARUUID\", \"API\", \"slotNumber\",\n \"subslotNumber\", \"index\"]:\n res.setfieldval(field, self.getfieldval(field))\n\n # append all block response\n res_blocks = []\n for block in self.getfieldval(\"blocks\"):\n res_blocks.append(block.get_response())\n res.setfieldval(\"blocks\", res_blocks)\n return res"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngenerates the response block of this request.", "response": "def get_response(self):\n \"\"\"Generate the response block of this request.\n Careful: it only sets the fields which can be set from the request\n \"\"\"\n res = ARBlockRes()\n for field in [\"ARType\", \"ARUUID\", \"SessionKey\"]:\n res.setfieldval(field, self.getfieldval(field))\n return res"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ngenerate the response block of this request.", "response": "def get_response(self):\n \"\"\"Generate the response block of this request.\n Careful: it only sets the fields which can be set from the request\n \"\"\"\n res = IOCRBlockRes()\n for field in [\"IOCRType\", \"IOCRReference\", \"FrameID\"]:\n res.setfieldval(field, self.getfieldval(field))\n return res"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nchecking if the class can handle this item", "response": "def can_handle(cls, pkt, rpc):\n \"\"\"heuristical guess_payload_class\"\"\"\n # type = 0 => request\n if rpc.getfieldval(\"type\") == 0 and \\\n str(rpc.object_uuid).startswith(\"dea00000-6c97-11d1-8271-\"):\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngenerates an IV for the packet", "response": "def make_iv(self, pkt):\n \"\"\"generate an IV for the packet\"\"\"\n if self.xpn_en:\n tmp_pn = (self.pn & 0xFFFFFFFF00000000) | (pkt[MACsec].pn & 0xFFFFFFFF) # noqa: E501\n tmp_iv = self.ssci + struct.pack('!Q', tmp_pn)\n return bytes(bytearray([a ^ b for a, b in zip(bytearray(tmp_iv), bytearray(self.salt))])) # noqa: E501\n else:\n return self.sci + struct.pack('!I', pkt[MACsec].pn)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef split_pkt(pkt, assoclen, icvlen=0):\n data = raw(pkt)\n assoc = data[:assoclen]\n if icvlen:\n icv = data[-icvlen:]\n enc = data[assoclen:-icvlen]\n else:\n icv = b''\n enc = data[assoclen:]\n return assoc, enc, icv", "response": "split the packet into associated data plaintext or ciphertext and ICV"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nencapsulate a frame using this Secure Association", "response": "def encap(self, pkt):\n \"\"\"encapsulate a frame using this Secure Association\"\"\"\n if pkt.name != Ether().name:\n raise TypeError('cannot encapsulate packet in MACsec, must be Ethernet') # noqa: E501\n hdr = copy.deepcopy(pkt)\n payload = hdr.payload\n del hdr.payload\n tag = MACsec(sci=self.sci, an=self.an,\n SC=self.send_sci,\n E=self.e_bit(), C=self.c_bit(),\n shortlen=MACsecSA.shortlen(pkt),\n pn=(self.pn & 0xFFFFFFFF), type=pkt.type)\n hdr.type = ETH_P_MACSEC\n return hdr / tag / payload"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef decap(self, orig_pkt):\n if orig_pkt.name != Ether().name or orig_pkt.payload.name != MACsec().name: # noqa: E501\n raise TypeError('cannot decapsulate MACsec packet, must be Ethernet/MACsec') # noqa: E501\n packet = copy.deepcopy(orig_pkt)\n prev_layer = packet[MACsec].underlayer\n prev_layer.type = packet[MACsec].type\n next_layer = packet[MACsec].payload\n del prev_layer.payload\n if prev_layer.name == Ether().name:\n return Ether(raw(prev_layer / next_layer))\n return prev_layer / next_layer", "response": "decapsulate a MACsec frame"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef encrypt(self, orig_pkt, assoclen=None):\n hdr = copy.deepcopy(orig_pkt)\n del hdr[MACsec].payload\n del hdr[MACsec].type\n pktlen = len(orig_pkt)\n if self.send_sci:\n hdrlen = NOSCI_LEN + SCI_LEN\n else:\n hdrlen = NOSCI_LEN\n if assoclen is None or not self.do_encrypt:\n if self.do_encrypt:\n assoclen = hdrlen\n else:\n assoclen = pktlen\n iv = self.make_iv(orig_pkt)\n assoc, pt, _ = MACsecSA.split_pkt(orig_pkt, assoclen)\n encryptor = Cipher(\n algorithms.AES(self.key),\n modes.GCM(iv),\n backend=default_backend()\n ).encryptor()\n encryptor.authenticate_additional_data(assoc)\n ct = encryptor.update(pt) + encryptor.finalize()\n hdr[MACsec].payload = Raw(assoc[hdrlen:assoclen] + ct + encryptor.tag)\n return hdr", "response": "encrypt a MACsec frame for this Secure Association"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef decrypt(self, orig_pkt, assoclen=None):\n hdr = copy.deepcopy(orig_pkt)\n del hdr[MACsec].payload\n pktlen = len(orig_pkt)\n if self.send_sci:\n hdrlen = NOSCI_LEN + SCI_LEN\n else:\n hdrlen = NOSCI_LEN\n if assoclen is None or not self.do_encrypt:\n if self.do_encrypt:\n assoclen = hdrlen\n else:\n assoclen = pktlen - self.icvlen\n iv = self.make_iv(hdr)\n assoc, ct, icv = MACsecSA.split_pkt(orig_pkt, assoclen, self.icvlen)\n decryptor = Cipher(\n algorithms.AES(self.key),\n modes.GCM(iv, icv),\n backend=default_backend()\n ).decryptor()\n decryptor.authenticate_additional_data(assoc)\n pt = assoc[hdrlen:assoclen]\n pt += decryptor.update(ct)\n pt += decryptor.finalize()\n hdr[MACsec].type = struct.unpack('!H', pt[0:2])[0]\n hdr[MACsec].payload = Raw(pt[2:])\n return hdr", "response": "decrypts a MACsec frame for this Secure Association"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef srbt(bt_address, pkts, inter=0.1, *args, **kargs):\n if \"port\" in kargs:\n s = conf.BTsocket(bt_address=bt_address, port=kargs.pop(\"port\"))\n else:\n s = conf.BTsocket(bt_address=bt_address)\n a, b = sndrcv(s, pkts, inter=inter, *args, **kargs)\n s.close()\n return a, b", "response": "send and receive using a bluetooth socket"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nsend and receive 1 packet using bluetooth socket", "response": "def srbt1(bt_address, pkts, *args, **kargs):\n \"\"\"send and receive 1 packet using a bluetooth socket\"\"\"\n a, b = srbt(bt_address, pkts, *args, **kargs)\n if len(a) > 0:\n return a[0][1]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef register_magic_payload(cls, payload_cls, magic_check=None):\n if magic_check is None:\n if hasattr(payload_cls, \"magic_check\"):\n magic_check = payload_cls.magic_check\n else:\n raise TypeError(\"magic_check not specified, and {} has no \"\n \"attribute magic_check\".format(payload_cls))\n\n cls.registered_magic_payloads[payload_cls] = magic_check", "response": "Registers a payload type that uses magic data."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn true if pkt is using SOMEIP - TP else returns false.", "response": "def _is_tp(pkt):\n \"\"\"Returns true if pkt is using SOMEIP-TP, else returns false.\"\"\"\n\n tp = [SOMEIP.TYPE_TP_REQUEST, SOMEIP.TYPE_TP_REQUEST_NO_RET,\n SOMEIP.TYPE_TP_NOTIFICATION, SOMEIP.TYPE_TP_RESPONSE,\n SOMEIP.TYPE_TP_ERROR]\n if isinstance(pkt, Packet):\n return pkt.msg_type in tp\n else:\n return pkt[15] in tp"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef fragment(self, fragsize=1392):\n fnb = 0\n fl = self\n lst = list()\n while fl.underlayer is not None:\n fnb += 1\n fl = fl.underlayer\n\n for p in fl:\n s = raw(p[fnb].payload)\n nb = (len(s) + fragsize) // fragsize\n for i in range(nb):\n q = p.copy()\n del q[fnb].payload\n q[fnb].len = SOMEIP.LEN_OFFSET_TP + \\\n len(s[i * fragsize:(i + 1) * fragsize])\n q[fnb].more_seg = 1\n if i == nb - 1:\n q[fnb].more_seg = 0\n q[fnb].offset += i * fragsize // 16\n r = conf.raw_layer(load=s[i * fragsize:(i + 1) * fragsize])\n r.overload_fields = p[fnb].payload.overload_fields.copy()\n q.add_payload(r)\n lst.append(q)\n\n return lst", "response": "Fragment SOME IP - TP"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngenerating the next RadioTapExtendedPresenceMask", "response": "def _next_radiotap_extpm(pkt, lst, cur, s):\n \"\"\"Generates the next RadioTapExtendedPresenceMask\"\"\"\n if cur is None or (cur.present and cur.present.Ext):\n st = len(lst) + (cur is not None)\n return lambda *args: RadioTapExtendedPresenceMask(*args, index=st)\n return None"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a dictionary containing a summary of the Dot11 - related elements fields containing the Dot11 - related information.", "response": "def network_stats(self):\n \"\"\"Return a dictionary containing a summary of the Dot11\n elements fields\n \"\"\"\n summary = {}\n crypto = set()\n akmsuite_types = {\n 0x00: \"Reserved\",\n 0x01: \"802.1X\",\n 0x02: \"PSK\"\n }\n p = self.payload\n while isinstance(p, Dot11Elt):\n if p.ID == 0:\n summary[\"ssid\"] = plain_str(p.info)\n elif p.ID == 3:\n summary[\"channel\"] = ord(p.info)\n elif isinstance(p, Dot11EltCountry):\n summary[\"country\"] = plain_str(p.country_string[:2])\n country_descriptor_types = {\n b\"I\": \"Indoor\",\n b\"O\": \"Outdoor\",\n b\"X\": \"Non-country\",\n b\"\\xff\": \"Ignored\"\n }\n summary[\"country_desc_type\"] = country_descriptor_types.get(\n p.country_string[-1:]\n )\n elif isinstance(p, Dot11EltRates):\n summary[\"rates\"] = p.rates\n elif isinstance(p, Dot11EltRSN):\n if p.akm_suites:\n auth = akmsuite_types.get(p.akm_suites[0].suite)\n crypto.add(\"WPA2/%s\" % auth)\n else:\n crypto.add(\"WPA2\")\n elif p.ID == 221:\n if isinstance(p, Dot11EltMicrosoftWPA) or \\\n p.info.startswith(b'\\x00P\\xf2\\x01\\x01\\x00'):\n if p.akm_suites:\n auth = akmsuite_types.get(p.akm_suites[0].suite)\n crypto.add(\"WPA/%s\" % auth)\n else:\n crypto.add(\"WPA\")\n p = p.payload\n if not crypto:\n if self.cap.privacy:\n crypto.add(\"WEP\")\n else:\n crypto.add(\"OPN\")\n summary[\"crypto\"] = crypto\n return summary"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_alias_address(iface_name, ip_mask, gw_str, metric):\n\n # Detect the architecture\n if scapy.consts.IS_64BITS:\n offset, name_len = 16, 40\n else:\n offset, name_len = 32, 32\n\n # Retrieve interfaces structures\n sck = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n names = array.array('B', b'\\0' * 4096)\n ifreq = ioctl(sck.fileno(), SIOCGIFCONF,\n struct.pack(\"iL\", len(names), names.buffer_info()[0]))\n\n # Extract interfaces names\n out = struct.unpack(\"iL\", ifreq)[0]\n names = names.tobytes() if six.PY3 else names.tostring()\n names = [names[i:i + offset].split(b'\\0', 1)[0] for i in range(0, out, name_len)] # noqa: E501\n\n # Look for the IP address\n for ifname in names:\n # Only look for a matching interface name\n if not ifname.decode(\"utf8\").startswith(iface_name):\n continue\n\n # Retrieve and convert addresses\n ifreq = ioctl(sck, SIOCGIFADDR, struct.pack(\"16s16x\", ifname))\n ifaddr = struct.unpack(\">I\", ifreq[20:24])[0]\n ifreq = ioctl(sck, SIOCGIFNETMASK, struct.pack(\"16s16x\", ifname))\n msk = struct.unpack(\">I\", ifreq[20:24])[0]\n\n # Get the full interface name\n ifname = plain_str(ifname)\n if ':' in ifname:\n ifname = ifname[:ifname.index(':')]\n else:\n continue\n\n # Check if the source address is included in the network\n if (ifaddr & msk) == ip_mask:\n sck.close()\n return (ifaddr & msk, msk, gw_str, ifname,\n scapy.utils.ltoa(ifaddr), metric)\n\n sck.close()\n return", "response": "Get the correct source IP address of an interface alias"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef in6_getifaddr():\n ret = []\n try:\n fdesc = open(\"/proc/net/if_inet6\", \"rb\")\n except IOError:\n return ret\n for line in fdesc:\n # addr, index, plen, scope, flags, ifname\n tmp = plain_str(line).split()\n addr = scapy.utils6.in6_ptop(\n b':'.join(\n struct.unpack('4s4s4s4s4s4s4s4s', tmp[0].encode())\n ).decode()\n )\n # (addr, scope, iface)\n ret.append((addr, int(tmp[3], 16), tmp[5]))\n fdesc.close()\n return ret", "response": "Returns a list of 3 - tuples of the form addr scope iface"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_iface_mode(iface):\n p = subprocess.Popen([\"iwconfig\", iface], stdout=subprocess.PIPE,\n stderr=subprocess.STDOUT)\n output, err = p.communicate()\n match = re.search(br\"mode:([a-zA-Z]*)\", output.lower())\n if match:\n return plain_str(match.group(1))\n return \"unknown\"", "response": "Get the interface mode."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsets the monitor mode of an interface.", "response": "def set_iface_monitor(iface, monitor):\n \"\"\"Sets the monitor mode (or remove it) from an interface.\n params:\n - iface: the iwconfig interface\n - monitor: True if the interface should be set in monitor mode,\n False if it should be in managed mode\n \"\"\"\n mode = get_iface_mode(iface)\n if mode == \"unknown\":\n warning(\"Could not parse iwconfig !\")\n current_monitor = mode == \"monitor\"\n if monitor == current_monitor:\n # Already correct\n return True\n s_mode = \"monitor\" if monitor else \"managed\"\n\n def _check_call(commands):\n p = subprocess.Popen(commands,\n stderr=subprocess.PIPE,\n stdout=subprocess.PIPE)\n stdout, stderr = p.communicate()\n if p.returncode != 0:\n warning(\"%s failed !\" % \" \".join(commands))\n return False\n return True\n try:\n assert _check_call([\"ifconfig\", iface, \"down\"])\n assert _check_call([\"iwconfig\", iface, \"mode\", s_mode])\n assert _check_call([\"ifconfig\", iface, \"up\"])\n return True\n except AssertionError:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef recv_raw(self, x=MTU):\n pkt, sa_ll = self.ins.recvfrom(x)\n if self.outs and sa_ll[2] == socket.PACKET_OUTGOING:\n return None, None, None\n ts = get_last_packet_timestamp(self.ins)\n return self.LL, pkt, ts", "response": "Receives a packet then returns a tuple containing the class and the packet data and the time."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns True is obj is a BPF Super Socket", "response": "def isBPFSocket(obj):\n \"\"\"Return True is obj is a BPF Super Socket\"\"\"\n return isinstance(obj, L2bpfListenSocket) or isinstance(obj, L2bpfListenSocket) or isinstance(obj, L3bpfSocket)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef bpf_select(fds_list, timeout=None):\n\n # Check file descriptors types\n bpf_scks_buffered = list()\n select_fds = list()\n\n for tmp_fd in fds_list:\n\n # Specific BPF sockets: get buffers status\n if isBPFSocket(tmp_fd) and tmp_fd.buffered_frames():\n bpf_scks_buffered.append(tmp_fd)\n continue\n\n # Regular file descriptors or empty BPF buffer\n select_fds.append(tmp_fd)\n\n if select_fds:\n # Call select for sockets with empty buffers\n if timeout is None:\n timeout = 0.05\n ready_list, _, _ = select(select_fds, [], [], timeout)\n return bpf_scks_buffered + ready_list\n else:\n return bpf_scks_buffered", "response": "A call to recv can return several frames. This function returns several frames. This function returns several frames. This function returns several frames. This function returns several frames. This function returns several frames."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset the interface in promiscuous mode", "response": "def set_promisc(self, value):\n \"\"\"Set the interface in promiscuous mode\"\"\"\n\n try:\n fcntl.ioctl(self.ins, BIOCPROMISC, struct.pack('i', value))\n except IOError:\n raise Scapy_Exception(\"Cannot set promiscuous mode on interface \"\n \"(%s)!\" % self.iface)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nguess the packet class that must be used on the interface", "response": "def guess_cls(self):\n \"\"\"Guess the packet class that must be used on the interface\"\"\"\n\n # Get the data link type\n try:\n ret = fcntl.ioctl(self.ins, BIOCGDLT, struct.pack('I', 0))\n ret = struct.unpack('I', ret)[0]\n except IOError:\n cls = conf.default_l2\n warning(\"BIOCGDLT failed: unable to guess type. Using %s !\",\n cls.name)\n return cls\n\n # Retrieve the corresponding class\n try:\n return conf.l2types[ret]\n except KeyError:\n cls = conf.default_l2\n warning(\"Unable to guess type (type %i). Using %s\", ret, cls.name)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nset the non blocking flag on the socket", "response": "def set_nonblock(self, set_flag=True):\n \"\"\"Set the non blocking flag on the socket\"\"\"\n\n # Get the current flags\n if self.fd_flags is None:\n try:\n self.fd_flags = fcntl.fcntl(self.ins, fcntl.F_GETFL)\n except IOError:\n warning(\"Cannot get flags on this file descriptor !\")\n return\n\n # Set the non blocking flag\n if set_flag:\n new_fd_flags = self.fd_flags | os.O_NONBLOCK\n else:\n new_fd_flags = self.fd_flags & ~os.O_NONBLOCK\n\n try:\n fcntl.fcntl(self.ins, fcntl.F_SETFL, new_fd_flags)\n self.fd_flags = new_fd_flags\n except Exception:\n warning(\"Can't set flags on this file descriptor !\")"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_stats(self):\n\n try:\n ret = fcntl.ioctl(self.ins, BIOCGSTATS, struct.pack(\"2I\", 0, 0))\n return struct.unpack(\"2I\", ret)\n except IOError:\n warning(\"Unable to get stats from BPF !\")\n return (None, None)", "response": "Get received and dropped statistics from the BPF."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget the length of the BPF buffer", "response": "def get_blen(self):\n \"\"\"Get the BPF buffer length\"\"\"\n\n try:\n ret = fcntl.ioctl(self.ins, BIOCGBLEN, struct.pack(\"I\", 0))\n return struct.unpack(\"I\", ret)[0]\n except IOError:\n warning(\"Unable to get the BPF buffer length\")\n return"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nclosing the Super Socket", "response": "def close(self):\n \"\"\"Close the Super Socket\"\"\"\n\n if not self.closed and self.ins is not None:\n os.close(self.ins)\n self.closed = True\n self.ins = None"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nextracting all frames from the buffer and store them in the received_frames list.", "response": "def extract_frames(self, bpf_buffer):\n \"\"\"Extract all frames from the buffer and stored them in the received list.\"\"\" # noqa: E501\n\n # Ensure that the BPF buffer contains at least the header\n len_bb = len(bpf_buffer)\n if len_bb < 20: # Note: 20 == sizeof(struct bfp_hdr)\n return\n\n # Extract useful information from the BPF header\n if FREEBSD or NETBSD:\n # struct bpf_xhdr or struct bpf_hdr32\n bh_tstamp_offset = 16\n else:\n # struct bpf_hdr\n bh_tstamp_offset = 8\n\n # Parse the BPF header\n bh_caplen = struct.unpack('I', bpf_buffer[bh_tstamp_offset:bh_tstamp_offset + 4])[0] # noqa: E501\n next_offset = bh_tstamp_offset + 4\n bh_datalen = struct.unpack('I', bpf_buffer[next_offset:next_offset + 4])[0] # noqa: E501\n next_offset += 4\n bh_hdrlen = struct.unpack('H', bpf_buffer[next_offset:next_offset + 2])[0] # noqa: E501\n if bh_datalen == 0:\n return\n\n # Get and store the Scapy object\n frame_str = bpf_buffer[bh_hdrlen:bh_hdrlen + bh_caplen]\n try:\n pkt = self.guessed_cls(frame_str)\n except Exception:\n if conf.debug_dissector:\n raise\n pkt = conf.raw_layer(frame_str)\n self.received_frames.append(pkt)\n\n # Extract the next frame\n end = self.bpf_align(bh_hdrlen, bh_caplen)\n if (len_bb - end) >= 20:\n self.extract_frames(bpf_buffer[end:])"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreceiving a single frame from the network", "response": "def recv(self, x=BPF_BUFFER_LENGTH):\n \"\"\"Receive a frame from the network\"\"\"\n\n if self.buffered_frames():\n # Get a frame from the buffer\n return self.get_frame()\n\n # Get data from BPF\n try:\n bpf_buffer = os.read(self.ins, x)\n except EnvironmentError as exc:\n if exc.errno != errno.EAGAIN:\n warning(\"BPF recv()\", exc_info=True)\n return\n\n # Extract all frames from the BPF buffer\n self.extract_frames(bpf_buffer)\n return self.get_frame()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting a frame or packet from the received list", "response": "def get_frame(self):\n \"\"\"Get a frame or packet from the received list\"\"\"\n pkt = super(L3bpfSocket, self).get_frame()\n if pkt is not None:\n return pkt.payload"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nsend a packet to the BPF", "response": "def send(self, pkt):\n \"\"\"Send a packet\"\"\"\n\n # Use the routing table to find the output interface\n iff = pkt.route()[0]\n if iff is None:\n iff = conf.iface\n\n # Assign the network interface to the BPF handle\n if self.assigned_interface != iff:\n try:\n fcntl.ioctl(self.outs, BIOCSETIF, struct.pack(\"16s16x\", iff.encode())) # noqa: E501\n except IOError:\n raise Scapy_Exception(\"BIOCSETIF failed on %s\" % iff)\n self.assigned_interface = iff\n\n # Build the frame\n frame = raw(self.guessed_cls() / pkt)\n pkt.sent_time = time.time()\n\n # Send the frame\n L2bpfSocket.send(self, frame)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef tls_session_update(self, msg_str):\n self.tls_session.handshake_messages.append(msg_str)\n self.tls_session.handshake_messages_parsed.append(self)", "response": "Add a message to the list of TLS session messages."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef tls_session_update(self, msg_str):\n super(SSLv2ServerHello, self).tls_session_update(msg_str)\n\n s = self.tls_session\n client_cs = s.sslv2_common_cs\n css = [cs for cs in client_cs if cs in self.ciphers]\n s.sslv2_common_cs = css\n s.sslv2_connection_id = self.connection_id\n s.tls_version = self.version\n if self.cert is not None:\n s.server_certs = [self.cert]", "response": "Update the TLS session with the new message."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ngenerate a range object from start stop tuple and or tuple.", "response": "def _get_values(value):\n \"\"\"Generate a range object from (start, stop[, step]) tuples, or\n return value.\n\n \"\"\"\n if (isinstance(value, tuple) and (2 <= len(value) <= 3) and\n all(hasattr(i, \"__int__\") for i in value)):\n # We use values[1] + 1 as stop value for (x)range to maintain\n # the behavior of using tuples as field `values`\n return range(*((int(value[0]), int(value[1]) + 1) +\n tuple(int(v) for v in value[2:])))\n return value"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ndumps the current state of the current state of the EPS file.", "response": "def psdump(self, filename=None, **kargs):\n \"\"\"\n psdump(filename=None, layer_shift=0, rebuild=1)\n\n Creates an EPS file describing a packet. If filename is not provided a\n temporary file is created and gs is called.\n\n :param filename: the file's filename\n \"\"\"\n from scapy.config import conf\n from scapy.utils import get_temp_file, ContextManagerSubprocess\n canvas = self.canvas_dump(**kargs)\n if filename is None:\n fname = get_temp_file(autoext=kargs.get(\"suffix\", \".eps\"))\n canvas.writeEPSfile(fname)\n if WINDOWS and conf.prog.psreader is None:\n os.startfile(fname)\n else:\n with ContextManagerSubprocess(\"psdump()\", conf.prog.psreader):\n subprocess.Popen([conf.prog.psreader, fname])\n else:\n canvas.writeEPSfile(filename)\n print()"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef pdfdump(self, filename=None, **kargs):\n from scapy.config import conf\n from scapy.utils import get_temp_file, ContextManagerSubprocess\n canvas = self.canvas_dump(**kargs)\n if filename is None:\n fname = get_temp_file(autoext=kargs.get(\"suffix\", \".pdf\"))\n canvas.writePDFfile(fname)\n if WINDOWS and conf.prog.pdfreader is None:\n os.startfile(fname)\n else:\n with ContextManagerSubprocess(\"pdfdump()\",\n conf.prog.pdfreader):\n subprocess.Popen([conf.prog.pdfreader, fname])\n else:\n canvas.writePDFfile(filename)\n print()", "response": "Create a PDF file describing a packet."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nextracts description from README. md for PyPI s usage", "response": "def get_long_description():\n \"\"\"Extract description from README.md, for PyPI's usage\"\"\"\n try:\n fpath = os.path.join(os.path.dirname(__file__), \"README.md\")\n with io.open(fpath, encoding=\"utf-8\") as f:\n readme = f.read()\n desc = readme.partition(\"\")[2]\n desc = desc.partition(\"\")[0]\n return desc.strip()\n except IOError:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nchecks for odd packet length and pad according to Cisco spec.", "response": "def _check_len(self, pkt):\n \"\"\"Check for odd packet length and pad according to Cisco spec.\n This padding is only used for checksum computation. The original\n packet should not be altered.\"\"\"\n if len(pkt) % 2:\n last_chr = pkt[-1]\n if last_chr <= b'\\x80':\n return pkt[:-1] + b'\\x00' + last_chr\n else:\n return pkt[:-1] + b'\\xff' + chb(orb(last_chr) - 1)\n else:\n return pkt"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _legacy_pkcs1_v1_5_encode_md5_sha1(M, emLen):\n M = bytes_encode(M)\n md5_hash = hashes.Hash(_get_hash(\"md5\"), backend=default_backend())\n md5_hash.update(M)\n sha1_hash = hashes.Hash(_get_hash(\"sha1\"), backend=default_backend())\n sha1_hash.update(M)\n H = md5_hash.finalize() + sha1_hash.finalize()\n if emLen < 36 + 11:\n warning(\"pkcs_emsa_pkcs1_v1_5_encode: \"\n \"intended encoded message length too short\")\n return None\n PS = b'\\xff' * (emLen - 36 - 3)\n return b'\\x00' + b'\\x01' + PS + b'\\x00' + H", "response": "Legacy method for PKCS1 v1. 5 encoding with MD5 - SHA1 hash."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsending a DHCP discover request and return the answer", "response": "def dhcp_request(iface=None, **kargs):\n \"\"\"Send a DHCP discover request and return the answer\"\"\"\n if conf.checkIPaddr != 0:\n warning(\"conf.checkIPaddr is not 0, I may not be able to match the answer\") # noqa: E501\n if iface is None:\n iface = conf.iface\n fam, hw = get_if_raw_hwaddr(iface)\n return srp1(Ether(dst=\"ff:ff:ff:ff:ff:ff\") / IP(src=\"0.0.0.0\", dst=\"255.255.255.255\") / UDP(sport=68, dport=67) / # noqa: E501\n BOOTP(chaddr=hw) / DHCP(options=[(\"message-type\", \"discover\"), \"end\"]), iface=iface, **kargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef apply_ipython_style(shell):\n try:\n from IPython.terminal.prompts import Prompts, Token\n except Exception:\n from scapy.error import log_loading\n log_loading.warning(\n \"IPython too old. Shell color won't be handled.\"\n )\n return\n from scapy.config import conf\n scapy_style = {}\n # Overwrite colors\n if isinstance(conf.color_theme, NoTheme):\n shell.colors = 'nocolor'\n elif isinstance(conf.color_theme, BrightTheme):\n # lightbg is optimized for light backgrounds\n shell.colors = 'lightbg'\n elif isinstance(conf.color_theme, ColorOnBlackTheme):\n # linux is optimised for dark backgrounds\n shell.colors = 'linux'\n else:\n # default\n shell.colors = 'neutral'\n try:\n get_ipython()\n # This function actually contains tons of hacks\n color_magic = shell.magics_manager.magics[\"line\"][\"colors\"]\n color_magic(shell.colors)\n except NameError:\n pass\n # Prompt Style\n if isinstance(conf.prompt, Prompts):\n # Set custom prompt style\n shell.prompts_class = conf.prompt\n else:\n if isinstance(conf.color_theme, (FormatTheme, NoTheme)):\n # Formatable\n if isinstance(conf.color_theme, HTMLTheme):\n prompt = cgi.escape(conf.prompt)\n elif isinstance(conf.color_theme, LatexTheme):\n from scapy.utils import tex_escape\n prompt = tex_escape(conf.prompt)\n else:\n prompt = conf.prompt\n prompt = conf.color_theme.prompt(prompt)\n else:\n # Needs to be manually set\n prompt = str(conf.prompt)\n scapy_style[Token.Prompt] = Color.ansi_to_pygments(\n conf.color_theme.style_prompt\n )\n\n class ClassicPrompt(Prompts):\n def in_prompt_tokens(self, cli=None):\n return [(Token.Prompt, prompt), ]\n\n def out_prompt_tokens(self):\n return [(Token.OutPrompt, ''), ]\n # Apply classic prompt style\n shell.prompts_class = ClassicPrompt\n sys.ps1 = prompt\n # Register scapy color style\n shell.highlighting_style_overrides = scapy_style\n # Apply if Live\n try:\n get_ipython().refresh_style()\n except NameError:\n pass", "response": "Updates the specified IPython console shell with the specified conf. color_theme scapy theme."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _wait_non_ressources(self, callback):\n self.trigger = threading.Lock()\n self.was_ended = False\n self.trigger.acquire()\n self.trigger.acquire()\n if not self.was_ended:\n callback(self)", "response": "This method is used to wait for non -ressources to be released and advertise itself to the SelectableSelector using the callback"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreleasing the lock of the object.", "response": "def call_release(self, arborted=False):\n \"\"\"DEV: Must be call when the object becomes ready to read.\n Relesases the lock of _wait_non_ressources\"\"\"\n self.was_ended = arborted\n try:\n self.trigger.release()\n except (threading.ThreadError, AttributeError):\n pass"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nrelease all locks to kill all threads", "response": "def _release_all(self):\n \"\"\"Releases all locks to kill all threads\"\"\"\n for i in self.inputs:\n i.call_release(True)\n self.available_lock.release()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _exit_door(self, _input):\n self.results.append(_input)\n if self._ended:\n return\n self._ended = True\n self._release_all()", "response": "This function is called when the SelectableObject is ready to be used to exit the door."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nprocesses the select command.", "response": "def process(self):\n \"\"\"Entry point of SelectableSelector\"\"\"\n if WINDOWS:\n select_inputs = []\n for i in self.inputs:\n if not isinstance(i, SelectableObject):\n warning(\"Unknown ignored object type: %s\", type(i))\n elif i.__selectable_force_select__:\n # Then use select.select\n select_inputs.append(i)\n elif not self.remain and i.check_recv():\n self.results.append(i)\n else:\n i.wait_return(self._exit_door)\n if select_inputs:\n # Use default select function\n self.results.extend(select(select_inputs, [], [], self.remain)[0]) # noqa: E501\n if not self.remain:\n return self.results\n\n threading.Thread(target=self._timeout_thread, args=(self.remain,)).start() # noqa: E501\n if not self._ended:\n self.available_lock.acquire()\n return self.results\n else:\n r, _, _ = select(self.inputs, [], [], self.remain)\n return r"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nchecking that a module has a higher version that minver.", "response": "def _version_checker(module, minver):\n \"\"\"Checks that module has a higher version that minver.\n\n params:\n - module: a module to test\n - minver: a tuple of versions\n \"\"\"\n # We could use LooseVersion, but distutils imports imp which is deprecated\n version_regexp = r'[a-z]?((?:\\d|\\.)+\\d+)(?:\\.dev[0-9]+)?'\n version_tags = re.match(version_regexp, module.__version__)\n if not version_tags:\n return False\n version_tags = version_tags.group(1).split(\".\")\n version_tags = tuple(int(x) for x in version_tags)\n return version_tags >= minver"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nchecking if the cryptography library is present and supports X25519.", "response": "def isCryptographyAdvanced():\n \"\"\"\n Check if the cryptography library is present, and if it supports X25519,\n ChaCha20Poly1305 and such (v2.0 or later).\n \"\"\"\n try:\n from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey # noqa: E501\n X25519PrivateKey.generate()\n except Exception:\n return False\n else:\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _prompt_changer(attr, val):\n try:\n sys.ps1 = conf.color_theme.prompt(conf.prompt)\n except Exception:\n pass\n try:\n apply_ipython_style(get_ipython())\n except NameError:\n pass", "response": "Change the current prompt theme"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _set_conf_sockets():\n if conf.use_bpf and not BSD:\n Interceptor.set_from_hook(conf, \"use_bpf\", False)\n raise ScapyInvalidPlatformException(\"BSD-like (OSX, *BSD...) only !\")\n if conf.use_winpcapy and not WINDOWS:\n Interceptor.set_from_hook(conf, \"use_winpcapy\", False)\n raise ScapyInvalidPlatformException(\"Windows only !\")\n # we are already in an Interceptor hook, use Interceptor.set_from_hook\n if conf.use_pcap or conf.use_dnet or conf.use_winpcapy:\n try:\n from scapy.arch.pcapdnet import L2pcapListenSocket, L2pcapSocket, \\\n L3pcapSocket\n except ImportError:\n warning(\"No pcap provider available ! pcap won't be used\")\n Interceptor.set_from_hook(conf, \"use_winpcapy\", False)\n Interceptor.set_from_hook(conf, \"use_pcap\", False)\n else:\n conf.L3socket = L3pcapSocket\n conf.L3socket6 = functools.partial(L3pcapSocket, filter=\"ip6\")\n conf.L2socket = L2pcapSocket\n conf.L2listen = L2pcapListenSocket\n return\n if conf.use_bpf:\n from scapy.arch.bpf.supersocket import L2bpfListenSocket, \\\n L2bpfSocket, L3bpfSocket\n conf.L3socket = L3bpfSocket\n conf.L3socket6 = functools.partial(L3bpfSocket, filter=\"ip6\")\n conf.L2socket = L2bpfSocket\n conf.L2listen = L2bpfListenSocket\n return\n if LINUX:\n from scapy.arch.linux import L3PacketSocket, L2Socket, L2ListenSocket\n conf.L3socket = L3PacketSocket\n conf.L3socket6 = functools.partial(L3PacketSocket, filter=\"ip6\")\n conf.L2socket = L2Socket\n conf.L2listen = L2ListenSocket\n return\n if WINDOWS:\n from scapy.arch.windows import _NotAvailableSocket\n from scapy.arch.windows.native import L3WinSocket, L3WinSocket6\n conf.L3socket = L3WinSocket\n conf.L3socket6 = L3WinSocket6\n conf.L2socket = _NotAvailableSocket\n conf.L2listen = _NotAvailableSocket\n return\n from scapy.supersocket import L3RawSocket\n from scapy.layers.inet6 import L3RawSocket6\n conf.L3socket = L3RawSocket\n conf.L3socket6 = L3RawSocket6", "response": "Populate the conf. L2Socket and conf. L3Socket according to the various use_* parameters\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef filter(self, func):\n return self.__class__([x for x in self.res if func(x)],\n name=\"filtered %s\" % self.listname)", "response": "Returns a new packet list filtered by a truth function. This truth\n function has to take a packet as the only argument and return a boolean value."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef plot(self, f, lfilter=None, plot_xy=False, **kargs):\n\n # Python 2 backward compatibility\n f = lambda_tuple_converter(f)\n lfilter = lambda_tuple_converter(lfilter)\n\n # Get the list of packets\n if lfilter is None:\n lst_pkts = [f(*e) for e in self.res]\n else:\n lst_pkts = [f(*e) for e in self.res if lfilter(*e)]\n\n # Mimic the default gnuplot output\n if kargs == {}:\n kargs = MATPLOTLIB_DEFAULT_PLOT_KARGS\n if plot_xy:\n lines = plt.plot(*zip(*lst_pkts), **kargs)\n else:\n lines = plt.plot(lst_pkts, **kargs)\n\n # Call show() if matplotlib is not inlined\n if not MATPLOTLIB_INLINED:\n plt.show()\n\n return lines", "response": "Applies a function to each packet to get a value that will be plotted\n with matplotlib. A list of matplotlib. lines. Line2D is returned."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfunction f applies a function to couples and returns a list of matplotlib. lines. Line2D is returned.", "response": "def diffplot(self, f, delay=1, lfilter=None, **kargs):\n \"\"\"diffplot(f, delay=1, lfilter=None)\n Applies a function to couples (l[i],l[i+delay])\n\n A list of matplotlib.lines.Line2D is returned.\n \"\"\"\n\n # Get the list of packets\n if lfilter is None:\n lst_pkts = [f(self.res[i], self.res[i + 1])\n for i in range(len(self.res) - delay)]\n else:\n lst_pkts = [f(self.res[i], self.res[i + 1])\n for i in range(len(self.res) - delay)\n if lfilter(self.res[i])]\n\n # Mimic the default gnuplot output\n if kargs == {}:\n kargs = MATPLOTLIB_DEFAULT_PLOT_KARGS\n lines = plt.plot(lst_pkts, **kargs)\n\n # Call show() if matplotlib is not inlined\n if not MATPLOTLIB_INLINED:\n plt.show()\n\n return lines"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nuse a function that returns a label and a value for this label then a list of matplotlib. lines. Line2D is returned.", "response": "def multiplot(self, f, lfilter=None, plot_xy=False, **kargs):\n \"\"\"Uses a function that returns a label and a value for this label, then\n plots all the values label by label.\n\n A list of matplotlib.lines.Line2D is returned.\n \"\"\"\n\n # Python 2 backward compatibility\n f = lambda_tuple_converter(f)\n lfilter = lambda_tuple_converter(lfilter)\n\n # Get the list of packets\n if lfilter is None:\n lst_pkts = (f(*e) for e in self.res)\n else:\n lst_pkts = (f(*e) for e in self.res if lfilter(*e))\n\n # Apply the function f to the packets\n d = {}\n for k, v in lst_pkts:\n d.setdefault(k, []).append(v)\n\n # Mimic the default gnuplot output\n if not kargs:\n kargs = MATPLOTLIB_DEFAULT_PLOT_KARGS\n\n if plot_xy:\n lines = [plt.plot(*zip(*pl), **dict(kargs, label=k))\n for k, pl in six.iteritems(d)]\n else:\n lines = [plt.plot(pl, **dict(kargs, label=k))\n for k, pl in six.iteritems(d)]\n plt.legend(loc=\"center right\", bbox_to_anchor=(1.5, 0.5))\n\n # Call show() if matplotlib is not inlined\n if not MATPLOTLIB_INLINED:\n plt.show()\n\n return lines"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef hexraw(self, lfilter=None):\n for i, res in enumerate(self.res):\n p = self._elt2pkt(res)\n if lfilter is not None and not lfilter(p):\n continue\n print(\"%s %s %s\" % (conf.color_theme.id(i, fmt=\"%04i\"),\n p.sprintf(\"%.time%\"),\n self._elt2sum(res)))\n if p.haslayer(conf.raw_layer):\n hexdump(p.getlayer(conf.raw_layer).load)", "response": "Print the hexdump of all the related objects in the packet."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nprinting the hexdump of the contents of the object.", "response": "def hexdump(self, lfilter=None):\n \"\"\"Same as nsummary(), except that packets are also hexdumped\n lfilter: a truth function that decides whether a packet must be displayed\"\"\" # noqa: E501\n for i, res in enumerate(self.res):\n p = self._elt2pkt(res)\n if lfilter is not None and not lfilter(p):\n continue\n print(\"%s %s %s\" % (conf.color_theme.id(i, fmt=\"%04i\"),\n p.sprintf(\"%.time%\"),\n self._elt2sum(res)))\n hexdump(p)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ngraph a conversations between source and destination IP and ARP layers and displays them as a graphviz graph.", "response": "def conversations(self, getsrcdst=None, **kargs):\n \"\"\"Graphes a conversations between sources and destinations and display it\n (using graphviz and imagemagick)\n getsrcdst: a function that takes an element of the list and\n returns the source, the destination and optionally\n a label. By default, returns the IP source and\n destination from IP and ARP layers\n type: output type (svg, ps, gif, jpg, etc.), passed to dot's \"-T\" option # noqa: E501\n target: filename or redirect. Defaults pipe to Imagemagick's display program # noqa: E501\n prog: which graphviz program to use\"\"\"\n if getsrcdst is None:\n def getsrcdst(pkt):\n \"\"\"Extract src and dst addresses\"\"\"\n if 'IP' in pkt:\n return (pkt['IP'].src, pkt['IP'].dst)\n if 'IPv6' in pkt:\n return (pkt['IPv6'].src, pkt['IPv6'].dst)\n if 'ARP' in pkt:\n return (pkt['ARP'].psrc, pkt['ARP'].pdst)\n raise TypeError()\n conv = {}\n for p in self.res:\n p = self._elt2pkt(p)\n try:\n c = getsrcdst(p)\n except Exception:\n # No warning here: it's OK that getsrcdst() raises an\n # exception, since it might be, for example, a\n # function that expects a specific layer in each\n # packet. The try/except approach is faster and\n # considered more Pythonic than adding tests.\n continue\n if len(c) == 3:\n conv.setdefault(c[:2], set()).add(c[2])\n else:\n conv[c] = conv.get(c, 0) + 1\n gr = 'digraph \"conv\" {\\n'\n for (s, d), l in six.iteritems(conv):\n gr += '\\t \"%s\" -> \"%s\" [label=\"%s\"]\\n' % (\n s, d, ', '.join(str(x) for x in l) if isinstance(l, set) else l\n )\n gr += \"}\\n\"\n return do_graph(gr, **kargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef replace(self, *args, **kargs):\n delete_checksums = kargs.get(\"delete_checksums\", False)\n x = PacketList(name=\"Replaced %s\" % self.listname)\n if not isinstance(args[0], tuple):\n args = (args,)\n for p in self.res:\n p = self._elt2pkt(p)\n copied = False\n for scheme in args:\n fld = scheme[0]\n old = scheme[1] # not used if len(scheme) == 2\n new = scheme[-1]\n for o in fld.owners:\n if o in p:\n if len(scheme) == 2 or p[o].getfieldval(fld.name) == old: # noqa: E501\n if not copied:\n p = p.copy()\n if delete_checksums:\n p.delete_checksums()\n copied = True\n setattr(p[o], fld.name, new)\n x.append(p)\n return x", "response": "Replace the entries in the list with the ones in args."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _check_tcpdump():\n with open(os.devnull, 'wb') as devnull:\n try:\n proc = subprocess.Popen([conf.prog.tcpdump, \"--version\"],\n stdout=devnull, stderr=subprocess.STDOUT)\n except OSError:\n return False\n\n if OPENBSD:\n # 'tcpdump --version' returns 1 on OpenBSD 6.4\n return proc.wait() == 1\n else:\n return proc.wait() == 0", "response": "Check if tcpdump command can be started"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_if(iff, cmd):\n\n sck = socket.socket()\n ifreq = ioctl(sck, cmd, struct.pack(\"16s16x\", iff.encode(\"utf8\")))\n sck.close()\n return ifreq", "response": "Ease SIOCGIF * ioctl calls"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _legacy_bpf_pointer(tcpdump_lines):\n X86_64 = os.uname()[4] in ['x86_64', 'aarch64']\n size = int(tcpdump_lines[0])\n bpf = b\"\"\n for l in tcpdump_lines[1:]:\n if six.PY2:\n int_type = long # noqa: F821\n else:\n int_type = int\n bpf += struct.pack(\"HBBI\", *map(int_type, l.split()))\n\n # Thanks to http://www.netprojects.de/scapy-with-pypy-solved/ for the pypy trick # noqa: E501\n if conf.use_pypy:\n str_buffer = ctypes.create_string_buffer(bpf)\n return struct.pack('HL', size, ctypes.addressof(str_buffer))\n else:\n # XXX. Argl! We need to give the kernel a pointer on the BPF,\n # Python object header seems to be 20 bytes. 36 bytes for x86 64bits arch. # noqa: E501\n if X86_64:\n return struct.pack(\"HL\", size, id(bpf) + 36)\n else:\n return struct.pack(\"HI\", size, id(bpf) + 20)", "response": "Get old - format BPF Pointer. Deprecated."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_bpf_pointer(tcpdump_lines):\n if conf.use_pypy:\n return _legacy_bpf_pointer(tcpdump_lines)\n\n # Allocate BPF instructions\n size = int(tcpdump_lines[0])\n bpf_insn_a = bpf_insn * size\n bip = bpf_insn_a()\n\n # Fill the BPF instruction structures with the byte code\n tcpdump_lines = tcpdump_lines[1:]\n i = 0\n for line in tcpdump_lines:\n values = [int(v) for v in line.split()]\n bip[i].code = c_ushort(values[0])\n bip[i].jt = c_ubyte(values[1])\n bip[i].jf = c_ubyte(values[2])\n bip[i].k = c_uint(values[3])\n i += 1\n\n # Create the BPF program\n return bpf_program(size, bip)", "response": "Create a BPF Pointer for TCPDump filter"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef compile_filter(bpf_filter, iface=None):\n if not TCPDUMP:\n raise Scapy_Exception(\"tcpdump is not available. Cannot use filter !\")\n try:\n process = subprocess.Popen([\n conf.prog.tcpdump,\n \"-p\",\n \"-i\", (conf.iface if iface is None else iface),\n \"-ddd\",\n \"-s\", str(MTU),\n bpf_filter],\n stdout=subprocess.PIPE,\n stderr=subprocess.PIPE\n )\n except OSError as ex:\n raise Scapy_Exception(\"Failed to attach filter: %s\" % ex)\n lines, err = process.communicate()\n ret = process.returncode\n if ret:\n raise Scapy_Exception(\n \"Failed to attach filter: tcpdump returned: %s\" % err\n )\n lines = lines.strip().split(b\"\\n\")\n return get_bpf_pointer(lines)", "response": "Asks Tcpdump to parse the filter then build the matching BPF bytecode using get_bpf_pointer."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _ldp_id_adjustlen(pkt, x):\n f, v = pkt.getfield_and_val('id')\n return len(_LLDPidField.i2m(f, pkt, v)) + 1", "response": "Return the length of the id field according to its real encoded type"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncalculating size of lower dot1q headers in a layer", "response": "def _dot1q_headers_size(layer):\n \"\"\"\n calculate size of lower dot1q layers (if present)\n :param layer: the layer to start at\n :return: size of vlan headers, layer below lowest vlan header\n \"\"\"\n\n vlan_headers_size = 0\n under_layer = layer\n\n while under_layer and isinstance(under_layer, Dot1Q):\n vlan_headers_size += LLDPDU.DOT1Q_HEADER_LEN\n under_layer = under_layer.underlayer\n\n return vlan_headers_size, under_layer"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _frame_structure_check(structure_description):\n\n standard_frame_structure = [LLDPDUChassisID.__name__,\n LLDPDUPortID.__name__,\n LLDPDUTimeToLive.__name__,\n '<...>',\n LLDPDUEndOfLLDPDU.__name__]\n\n if len(structure_description) < 4:\n raise LLDPInvalidFrameStructure(\n 'Invalid frame structure.\\ngot: {}\\nexpected: '\n '{}'.format(' '.join(structure_description),\n ' '.join(standard_frame_structure)))\n\n for idx, layer_name in enumerate(standard_frame_structure):\n\n if layer_name == '<...>':\n break\n if layer_name != structure_description[idx]:\n raise LLDPInvalidFrameStructure(\n 'Invalid frame structure.\\ngot: {}\\nexpected: '\n '{}'.format(' '.join(structure_description),\n ' '.join(standard_frame_structure)))\n\n if structure_description[-1] != standard_frame_structure[-1]:\n raise LLDPInvalidFrameStructure(\n 'Invalid frame structure.\\ngot: {}\\nexpected: '\n '{}'.format(' '.join(structure_description),\n ' '.join(standard_frame_structure)))", "response": "check if the structure of the frame is conform to the basic LLDP - msg structure"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nchecks if multiplicity of present TLVs conforms to the standard.", "response": "def _tlv_multiplicities_check(tlv_type_count):\n \"\"\"\n check if multiplicity of present TLVs conforms to the standard\n :param tlv_type_count: dict containing counte-per-TLV\n \"\"\"\n\n # * : 0..n, 1 : one and only one.\n standard_multiplicities = {\n LLDPDUEndOfLLDPDU.__name__: 1,\n LLDPDUChassisID.__name__: 1,\n LLDPDUPortID.__name__: 1,\n LLDPDUTimeToLive.__name__: 1,\n LLDPDUPortDescription: '*',\n LLDPDUSystemName: '*',\n LLDPDUSystemDescription: '*',\n LLDPDUSystemCapabilities: '*',\n LLDPDUManagementAddress: '*'\n }\n\n for tlv_type_name in standard_multiplicities:\n\n standard_tlv_multiplicity = \\\n standard_multiplicities[tlv_type_name]\n if standard_tlv_multiplicity == '*':\n continue\n\n try:\n if tlv_type_count[tlv_type_name] != standard_tlv_multiplicity:\n raise LLDPInvalidTLVCount(\n 'Invalid number of entries for TLV type '\n '{} - expected {} entries, got '\n '{}'.format(tlv_type_name,\n standard_tlv_multiplicity,\n tlv_type_count[tlv_type_name]))\n\n except KeyError:\n raise LLDPInvalidTLVCount('Missing TLV layer of type '\n '{}.'.format(tlv_type_name))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _check(self):\n if conf.contribs['LLDP'].strict_mode() and self._length != 2:\n raise LLDPInvalidLengthField('length must be 2 - got '\n '{}'.format(self._length))", "response": "run layer specific checks\nxn"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _check(self):\n if conf.contribs['LLDP'].strict_mode():\n management_address_len = len(self.management_address)\n if management_address_len == 0 or management_address_len > 31:\n raise LLDPInvalidLengthField(\n 'management address must be 1..31 characters long - '\n 'got string of size {}'.format(management_address_len))", "response": "run layer specific checks\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef GetIcmpStatistics():\n statistics = MIB_ICMP()\n _GetIcmpStatistics(byref(statistics))\n results = _struct_to_dict(statistics)\n del(statistics)\n return results", "response": "Return all Windows ICMP stats from iphlpapi"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting all Windows Adapters addresses from iphlpapi", "response": "def GetAdaptersAddresses(AF=AF_UNSPEC):\n \"\"\"Return all Windows Adapters addresses from iphlpapi\"\"\"\n # We get the size first\n size = ULONG()\n flags = GAA_FLAG_INCLUDE_PREFIX\n res = _GetAdaptersAddresses(AF, flags,\n None, None,\n byref(size))\n if res != 0x6f: # BUFFER OVERFLOW -> populate size\n raise RuntimeError(\"Error getting structure length (%d)\" % res)\n # Now let's build our buffer\n pointer_type = POINTER(IP_ADAPTER_ADDRESSES)\n buffer = create_string_buffer(size.value)\n AdapterAddresses = ctypes.cast(buffer, pointer_type)\n # And call GetAdaptersAddresses\n res = _GetAdaptersAddresses(AF, flags,\n None, AdapterAddresses,\n byref(size))\n if res != NO_ERROR:\n raise RuntimeError(\"Error retrieving table (%d)\" % res)\n results = _resolve_list(AdapterAddresses)\n del(AdapterAddresses)\n return results"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef GetIpForwardTable():\n # We get the size first\n size = ULONG()\n res = _GetIpForwardTable(None, byref(size), False)\n if res != 0x7a: # ERROR_INSUFFICIENT_BUFFER -> populate size\n raise RuntimeError(\"Error getting structure length (%d)\" % res)\n # Now let's build our buffer\n pointer_type = PMIB_IPFORWARDTABLE\n buffer = create_string_buffer(size.value)\n pIpForwardTable = ctypes.cast(buffer, pointer_type)\n # And call GetAdaptersAddresses\n res = _GetIpForwardTable(pIpForwardTable, byref(size), True)\n if res != NO_ERROR:\n raise RuntimeError(\"Error retrieving table (%d)\" % res)\n results = []\n for i in range(pIpForwardTable.contents.NumEntries):\n results.append(_struct_to_dict(pIpForwardTable.contents.Table[i]))\n del(pIpForwardTable)\n return results", "response": "Get all Windows routes from iphlpapi"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef GetIpForwardTable2(AF=AF_UNSPEC):\n if WINDOWS_XP:\n raise OSError(\"Not available on Windows XP !\")\n table = PMIB_IPFORWARD_TABLE2()\n res = _GetIpForwardTable2(AF, byref(table))\n if res != NO_ERROR:\n raise RuntimeError(\"Error retrieving table (%d)\" % res)\n results = []\n for i in range(table.contents.NumEntries):\n results.append(_struct_to_dict(table.contents.Table[i]))\n _FreeMibTable(table)\n return results", "response": "Return all Windows routes from iphlpapi"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef encrypt(self, data):\n if False in six.itervalues(self.ready):\n raise CipherError(data)\n encryptor = self._cipher.encryptor()\n tmp = encryptor.update(data) + encryptor.finalize()\n self.iv = tmp[-self.block_size:]\n return tmp", "response": "Encrypt the data. Also, update the cipher iv. This is needed for SSLv3\n and TLS 1.0. For TLS 1.1/1.2, it is overwritten in TLS.post_build()."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\npacks RADIUS data prior computing the authentication MAC", "response": "def prepare_packed_data(radius_packet, packed_req_authenticator):\n \"\"\"\n Pack RADIUS data prior computing the authentication MAC\n \"\"\"\n\n packed_hdr = struct.pack(\"!B\", radius_packet.code)\n packed_hdr += struct.pack(\"!B\", radius_packet.id)\n packed_hdr += struct.pack(\"!H\", radius_packet.len)\n\n packed_attrs = b''\n for attr in radius_packet.attributes:\n packed_attrs += raw(attr)\n\n return packed_hdr + packed_req_authenticator + packed_attrs"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef register_variant(cls):\n\n if hasattr(cls, \"val\"):\n cls.registered_attributes[cls.val] = cls\n else:\n cls.registered_attributes[cls.type.default] = cls", "response": "Registers the RADIUS attributes defined in this module."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ndispatch the given class to the right RadiusAttribute class.", "response": "def dispatch_hook(cls, _pkt=None, *args, **kargs):\n \"\"\"\n Returns the right RadiusAttribute class for the given data.\n \"\"\"\n\n if _pkt:\n attr_type = orb(_pkt[0])\n return cls.registered_attributes.get(attr_type, cls)\n return cls"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef compute_message_authenticator(radius_packet, packed_req_authenticator,\n shared_secret):\n \"\"\"\n Computes the \"Message-Authenticator\" of a given RADIUS packet.\n \"\"\"\n\n data = prepare_packed_data(radius_packet, packed_req_authenticator)\n radius_hmac = hmac.new(shared_secret, data, hashlib.md5)\n\n return radius_hmac.digest()", "response": "Computes the Message - Authenticator of a given RADIUS packet."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef compute_authenticator(self, packed_request_auth, shared_secret):\n\n data = prepare_packed_data(self, packed_request_auth)\n radius_mac = hashlib.md5(data + shared_secret)\n return radius_mac.digest()", "response": "Computes the authenticator field based on the request auth and shared secret."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef do_build(self):\n tmp = self.explicit\n self.explicit = True\n b = super(KeyShareEntry, self).do_build()\n self.explicit = tmp\n return b", "response": "This method is used to build the key share entry."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_privkey(self):\n if self.group in _tls_named_ffdh_groups:\n params = _ffdh_groups[_tls_named_ffdh_groups[self.group]][0]\n privkey = params.generate_private_key()\n self.privkey = privkey\n pubkey = privkey.public_key()\n self.key_exchange = pubkey.public_numbers().y\n elif self.group in _tls_named_curves:\n if _tls_named_curves[self.group] == \"x25519\":\n if conf.crypto_valid_advanced:\n privkey = x25519.X25519PrivateKey.generate()\n self.privkey = privkey\n pubkey = privkey.public_key()\n self.key_exchange = pubkey.public_bytes()\n elif _tls_named_curves[self.group] != \"x448\":\n curve = ec._CURVE_TYPES[_tls_named_curves[self.group]]()\n privkey = ec.generate_private_key(curve, default_backend())\n self.privkey = privkey\n pubkey = privkey.public_key()\n try:\n # cryptography >= 2.5\n self.key_exchange = pubkey.public_bytes(\n serialization.Encoding.X962,\n serialization.PublicFormat.UncompressedPoint\n )\n except TypeError:\n # older versions\n self.key_exchange = pubkey.public_numbers().encode_point()", "response": "This method creates a private key for the current class."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _guess_iface_name(netif):\n with os.popen('%s -l' % conf.prog.ifconfig) as fdesc:\n ifaces = fdesc.readline().strip().split(' ')\n matches = [iface for iface in ifaces if iface.startswith(netif)]\n if len(matches) == 1:\n return matches[0]\n return None", "response": "Try to guess the name of the interface that is truncated from the ifconfig output of ifconfig - l. If there is only one possible candidate then we return None."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a list of IPv6 addresses configured on the interface ifname.", "response": "def _in6_getifaddr(ifname):\n \"\"\"\n Returns a list of IPv6 addresses configured on the interface ifname.\n \"\"\"\n\n # Get the output of ifconfig\n try:\n f = os.popen(\"%s %s\" % (conf.prog.ifconfig, ifname))\n except OSError:\n log_interactive.warning(\"Failed to execute ifconfig.\")\n return []\n\n # Iterate over lines and extract IPv6 addresses\n ret = []\n for line in f:\n if \"inet6\" in line:\n addr = line.rstrip().split(None, 2)[1] # The second element is the IPv6 address # noqa: E501\n else:\n continue\n if '%' in line: # Remove the interface identifier if present\n addr = addr.split(\"%\", 1)[0]\n\n # Check if it is a valid IPv6 address\n try:\n inet_pton(socket.AF_INET6, addr)\n except (socket.error, ValueError):\n continue\n\n # Get the scope and keep the address\n scope = in6_getscope(addr)\n ret.append((addr, scope, ifname))\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef in6_getifaddr():\n\n # List all network interfaces\n if OPENBSD:\n try:\n f = os.popen(\"%s\" % conf.prog.ifconfig)\n except OSError:\n log_interactive.warning(\"Failed to execute ifconfig.\")\n return []\n\n # Get the list of network interfaces\n splitted_line = []\n for l in f:\n if \"flags\" in l:\n iface = l.split()[0].rstrip(':')\n splitted_line.append(iface)\n\n else: # FreeBSD, NetBSD or Darwin\n try:\n f = os.popen(\"%s -l\" % conf.prog.ifconfig)\n except OSError:\n log_interactive.warning(\"Failed to execute ifconfig.\")\n return []\n\n # Get the list of network interfaces\n splitted_line = f.readline().rstrip().split()\n\n ret = []\n for i in splitted_line:\n ret += _in6_getifaddr(i)\n return ret", "response": "Get the list of addresses of all interfaces associated to the interface in the system."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef read_routes6():\n\n # Call netstat to retrieve IPv6 routes\n fd_netstat = os.popen(\"netstat -rn -f inet6\")\n\n # List interfaces IPv6 addresses\n lifaddr = in6_getifaddr()\n if not lifaddr:\n return []\n\n # Routes header information\n got_header = False\n mtu_present = False\n prio_present = False\n\n # Parse the routes\n routes = []\n for line in fd_netstat.readlines():\n\n # Parse the routes header and try to identify extra columns\n if not got_header:\n if \"Destination\" == line[:11]:\n got_header = True\n mtu_present = \"Mtu\" in line\n prio_present = \"Prio\" in line\n continue\n\n # Parse a route entry according to the operating system\n splitted_line = line.split()\n if OPENBSD or NETBSD:\n index = 5 + mtu_present + prio_present\n if len(splitted_line) < index:\n warning(\"Not enough columns in route entry !\")\n continue\n destination, next_hop, flags = splitted_line[:3]\n dev = splitted_line[index]\n else:\n # FREEBSD or DARWIN\n if len(splitted_line) < 4:\n warning(\"Not enough columns in route entry !\")\n continue\n destination, next_hop, flags, dev = splitted_line[:4]\n\n # XXX: TODO: add metrics for unix.py (use -e option on netstat)\n metric = 1\n\n # Check flags\n if \"U\" not in flags: # usable route\n continue\n if \"R\" in flags: # Host or net unreachable\n continue\n if \"m\" in flags: # multicast address\n # Note: multicast routing is handled in Route6.route()\n continue\n\n # Replace link with the default route in next_hop\n if \"link\" in next_hop:\n next_hop = \"::\"\n\n # Default prefix length\n destination_plen = 128\n\n # Extract network interface from the zone id\n if '%' in destination:\n destination, dev = destination.split('%')\n if '/' in dev:\n # Example: fe80::%lo0/64 ; dev = \"lo0/64\"\n dev, destination_plen = dev.split('/')\n if '%' in next_hop:\n next_hop, dev = next_hop.split('%')\n\n # Ensure that the next hop is a valid IPv6 address\n if not in6_isvalid(next_hop):\n # Note: the 'Gateway' column might contain a MAC address\n next_hop = \"::\"\n\n # Modify parsed routing entries\n # Note: these rules are OS specific and may evolve over time\n if destination == \"default\":\n destination, destination_plen = \"::\", 0\n elif '/' in destination:\n # Example: fe80::/10\n destination, destination_plen = destination.split('/')\n if '/' in dev:\n # Example: ff02::%lo0/32 ; dev = \"lo0/32\"\n dev, destination_plen = dev.split('/')\n\n # Check route entries parameters consistency\n if not in6_isvalid(destination):\n warning(\"Invalid destination IPv6 address in route entry !\")\n continue\n try:\n destination_plen = int(destination_plen)\n except Exception:\n warning(\"Invalid IPv6 prefix length in route entry !\")\n continue\n if in6_ismlladdr(destination) or in6_ismnladdr(destination):\n # Note: multicast routing is handled in Route6.route()\n continue\n\n if LOOPBACK_NAME in dev:\n # Handle ::1 separately\n cset = [\"::1\"]\n next_hop = \"::\"\n else:\n # Get possible IPv6 source addresses\n devaddrs = (x for x in lifaddr if x[2] == dev)\n cset = construct_source_candidate_set(destination, destination_plen, devaddrs) # noqa: E501\n\n if len(cset):\n routes.append((destination, destination_plen, next_hop, dev, cset, metric)) # noqa: E501\n\n fd_netstat.close()\n return routes", "response": "Reads IPv6 routes from netstat and returns a list of IPv6 routes than can be used by Scapy."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _read_config_file(cf, _globals=globals(), _locals=locals(), interactive=True): # noqa: E501\n log_loading.debug(\"Loading config file [%s]\", cf)\n try:\n exec(compile(open(cf).read(), cf, 'exec'), _globals, _locals)\n except IOError as e:\n if interactive:\n raise\n log_loading.warning(\"Cannot read config file [%s] [%s]\", cf, e)\n except Exception:\n if interactive:\n raise\n log_loading.exception(\"Error during evaluation of config file [%s]\", cf)", "response": "Read a config file and return a tuple of the base classes that can be used to instantiate the appropriate class."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nload a Python module to make variables objects and functions available globally.", "response": "def _load(module, globals_dict=None, symb_list=None):\n \"\"\"Loads a Python module to make variables, objects and functions\navailable globally.\n\n The idea is to load the module using importlib, then copy the\nsymbols to the global symbol table.\n\n \"\"\"\n if globals_dict is None:\n globals_dict = six.moves.builtins.__dict__\n try:\n mod = importlib.import_module(module)\n if '__all__' in mod.__dict__:\n # import listed symbols\n for name in mod.__dict__['__all__']:\n if symb_list is not None:\n symb_list.append(name)\n globals_dict[name] = mod.__dict__[name]\n else:\n # only import non-private symbols\n for name, sym in six.iteritems(mod.__dict__):\n if _validate_local(name):\n if symb_list is not None:\n symb_list.append(name)\n globals_dict[name] = sym\n except Exception:\n log_interactive.error(\"Loading module %s\", module, exc_info=True)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef load_module(name, globals_dict=None, symb_list=None):\n _load(\"scapy.modules.\" + name,\n globals_dict=globals_dict, symb_list=symb_list)", "response": "Loads a Scapy module to make variables objects and functions available globally."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef load_layer(name, globals_dict=None, symb_list=None):\n _load(\"scapy.layers.\" + LAYER_ALIASES.get(name, name),\n globals_dict=globals_dict, symb_list=symb_list)", "response": "Loads a Scapy layer module to make variables objects and functions\n available globally."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nloads a Scapy contrib module to make variables objects and functions available globally.", "response": "def load_contrib(name, globals_dict=None, symb_list=None):\n \"\"\"Loads a Scapy contrib module to make variables, objects and\n functions available globally.\n\n If no contrib module can be found with the given name, try to find\n a layer module, since a contrib module may become a layer module.\n\n \"\"\"\n try:\n importlib.import_module(\"scapy.contrib.\" + name)\n _load(\"scapy.contrib.\" + name,\n globals_dict=globals_dict, symb_list=symb_list)\n except ImportError as e:\n # if layer not found in contrib, try in layers\n try:\n load_layer(name,\n globals_dict=globals_dict, symb_list=symb_list)\n except ImportError:\n raise e"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nshow the list of all existing contribs.", "response": "def list_contrib(name=None, ret=False, _debug=False):\n \"\"\"Show the list of all existing contribs.\n Params:\n - name: filter to search the contribs\n - ret: whether the function should return a dict instead of printing it\n \"\"\"\n # _debug: checks that all contrib modules have correctly defined:\n # # scapy.contrib.description = [...]\n # # scapy.contrib.status = [...]\n # # scapy.contrib.name = [...] (optional)\n # or set the flag:\n # # scapy.contrib.description = skip\n # to skip the file\n if name is None:\n name = \"*.py\"\n elif \"*\" not in name and \"?\" not in name and not name.endswith(\".py\"):\n name += \".py\"\n results = []\n dir_path = os.path.join(os.path.dirname(__file__), \"contrib\")\n if sys.version_info >= (3, 5):\n name = os.path.join(dir_path, \"**\", name)\n iterator = glob.iglob(name, recursive=True)\n else:\n name = os.path.join(dir_path, name)\n iterator = glob.iglob(name)\n for f in iterator:\n mod = f.replace(os.path.sep, \".\").partition(\"contrib.\")[2]\n if mod.startswith(\"__\"):\n continue\n if mod.endswith(\".py\"):\n mod = mod[:-3]\n desc = {\"description\": None, \"status\": None, \"name\": mod}\n for l in io.open(f, errors=\"replace\"):\n if l[0] != \"#\":\n continue\n p = l.find(\"scapy.contrib.\")\n if p >= 0:\n p += 14\n q = l.find(\"=\", p)\n key = l[p:q].strip()\n value = l[q + 1:].strip()\n desc[key] = value\n if desc[\"status\"] == \"skip\":\n break\n if desc[\"description\"] and desc[\"status\"]:\n results.append(desc)\n break\n if _debug:\n if desc[\"status\"] == \"skip\":\n pass\n elif not desc[\"description\"] or not desc[\"status\"]:\n raise Scapy_Exception(\"Module %s is missing its \"\n \"contrib infos !\" % mod)\n results.sort(key=lambda x: x[\"name\"])\n if ret:\n return results\n else:\n for desc in results:\n print(\"%(name)-20s: %(description)-40s status=%(status)s\" % desc)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsaving the current Scapy session to the file specified in fname.", "response": "def save_session(fname=None, session=None, pickleProto=-1):\n \"\"\"Save current Scapy session to the file specified in the fname arg.\n\n params:\n - fname: file to save the scapy session in\n - session: scapy session to use. If None, the console one will be used\n - pickleProto: pickle proto version (default: -1 = latest)\"\"\"\n from scapy import utils\n if fname is None:\n fname = conf.session\n if not fname:\n conf.session = fname = utils.get_temp_file(keep=True)\n log_interactive.info(\"Use [%s] as session file\" % fname)\n\n if session is None:\n try:\n session = get_ipython().user_ns\n except Exception:\n session = six.moves.builtins.__dict__[\"scapy_session\"]\n\n to_be_saved = session.copy()\n if \"__builtins__\" in to_be_saved:\n del(to_be_saved[\"__builtins__\"])\n\n for k in list(to_be_saved):\n i = to_be_saved[k]\n if hasattr(i, \"__module__\") and (k[0] == \"_\" or i.__module__.startswith(\"IPython\")): # noqa: E501\n del(to_be_saved[k])\n if isinstance(i, ConfClass):\n del(to_be_saved[k])\n elif isinstance(i, (type, type, types.ModuleType)):\n if k[0] != \"_\":\n log_interactive.error(\"[%s] (%s) can't be saved.\", k, type(to_be_saved[k])) # noqa: E501\n del(to_be_saved[k])\n\n try:\n os.rename(fname, fname + \".bak\")\n except OSError:\n pass\n\n f = gzip.open(fname, \"wb\")\n six.moves.cPickle.dump(to_be_saved, f, pickleProto)\n f.close()\n del f"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef load_session(fname=None):\n if fname is None:\n fname = conf.session\n try:\n s = six.moves.cPickle.load(gzip.open(fname, \"rb\"))\n except IOError:\n try:\n s = six.moves.cPickle.load(open(fname, \"rb\"))\n except IOError:\n # Raise \"No such file exception\"\n raise\n\n scapy_session = six.moves.builtins.__dict__[\"scapy_session\"]\n scapy_session.clear()\n scapy_session.update(s)\n update_ipython_session(scapy_session)\n\n log_loading.info(\"Loaded session [%s]\" % fname)", "response": "Load current Scapy session from the file specified in the fname arg."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef update_session(fname=None):\n if fname is None:\n fname = conf.session\n try:\n s = six.moves.cPickle.load(gzip.open(fname, \"rb\"))\n except IOError:\n s = six.moves.cPickle.load(open(fname, \"rb\"))\n scapy_session = six.moves.builtins.__dict__[\"scapy_session\"]\n scapy_session.update(s)\n update_ipython_session(scapy_session)", "response": "Update the current Scapy session from the file specified in the fname arg."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncreating an interactive session and execute the passed commands and return all output", "response": "def autorun_get_interactive_session(cmds, **kargs):\n \"\"\"Create an interactive session and execute the\n commands passed as \"cmds\" and return all output\n\n params:\n - cmds: a list of commands to run\n returns: (output, returned)\n\n The output contains both sys.stdout and sys.stderr logs\"\"\"\n sstdout, sstderr = sys.stdout, sys.stderr\n sw = StringWriter()\n try:\n try:\n sys.stdout = sys.stderr = sw\n res = autorun_commands(cmds, **kargs)\n except StopAutorun as e:\n e.code_run = sw.s\n raise\n finally:\n sys.stdout, sys.stderr = sstdout, sstderr\n return sw.s, res"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _check_field_type(self, pkt, index):\n my_idx = pkt.fields_desc.index(self)\n try:\n next_field = pkt.fields_desc[my_idx + index]\n if type(next_field) is not LEBitField and \\\n next_field.__class__.__base__ is not LEBitField:\n raise LEBitFieldSequenceException('field after field {} must '\n 'be of type LEBitField or '\n 'derived classes'.format(self.name)) # noqa: E501\n except IndexError:\n # no more fields -> error\n raise LEBitFieldSequenceException('Missing further LEBitField '\n 'based fields after field '\n '{} '.format(self.name))", "response": "check if the field addressed by given index is of type LEBitField or bitField"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nextracts data from raw string and returns the bit field value and the bits_in_set of the bitfield.", "response": "def getfield(self, pkt, s):\n\n \"\"\"\n extract data from raw str\n\n collect all instances belonging to the bit field set.\n if we reach a field that ends at a octet boundary, dissect the whole bit field at once # noqa: E501\n\n :param pkt: packet instance the field belongs to\n :param s: raw string representing the frame -or- tuple containing raw str, number of bits and array of fields # noqa: E501\n :return: tuple containing raw str, number of bits and array of fields -or- remaining raw str and value of this # noqa: E501\n \"\"\"\n\n if type(s) is tuple and len(s) == 3:\n s, bits_in_set, fields = s\n else:\n bits_in_set = 0\n fields = []\n\n bits_in_set += self.size\n\n fields.append(self)\n\n if bits_in_set % 8:\n # we are in between the bitfield\n return (s, bits_in_set, fields), None\n\n else:\n cur_val = 0\n cur_val_bit_idx = 0\n this_val = 0\n\n field_idx = 0\n field = fields[field_idx]\n field_required_bits = field.size\n idx = 0\n\n s = bytearray(s)\n bf_total_byte_length = bits_in_set // 8\n\n for octet in s[0:bf_total_byte_length]:\n idx += 1\n\n octet_bits_left = 8\n\n while octet_bits_left:\n\n if field_required_bits == octet_bits_left:\n # whole field fits into remaining bits\n # as this also signals byte-alignment this should exit the inner and outer loop # noqa: E501\n cur_val |= octet << cur_val_bit_idx\n pkt.fields[field.name] = cur_val\n\n '''\n TODO: check if do_dessect() needs a non-None check for assignment to raw_packet_cache_fields # noqa: E501\n\n setfieldval() is evil as it sets raw_packet_cache_fields to None - but this attribute # noqa: E501\n is accessed in do_dissect() without checking for None... exception is caught and the # noqa: E501\n user ends up with a layer decoded as raw...\n\n pkt.setfieldval(field.name, int(bit_str[:field.size], 2)) # noqa: E501\n '''\n\n octet_bits_left = 0\n\n this_val = cur_val\n\n elif field_required_bits < octet_bits_left:\n # pick required bits\n cur_val |= (octet & ((2 ** field_required_bits) - 1)) << cur_val_bit_idx # noqa: E501\n pkt.fields[field.name] = cur_val\n\n # remove consumed bits\n octet >>= field_required_bits\n octet_bits_left -= field_required_bits\n\n # and move to the next field\n field_idx += 1\n field = fields[field_idx]\n field_required_bits = field.size\n cur_val_bit_idx = 0\n cur_val = 0\n\n elif field_required_bits > octet_bits_left:\n # take remaining bits\n cur_val |= octet << cur_val_bit_idx\n\n cur_val_bit_idx += octet_bits_left\n field_required_bits -= octet_bits_left\n octet_bits_left = 0\n\n return s[bf_total_byte_length:], this_val"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef post_build(self, pkt, pay):\n\n class _EtherCatLengthCalc(Packet):\n \"\"\"\n dummy class used to generate str representation easily\n \"\"\"\n fields_desc = [\n LEBitField('length', None, 11),\n LEBitField('_reserved', 0, 1),\n LEBitField('type', 0, 4),\n ]\n\n payload_len = len(pay)\n\n # length field is 11 bit\n if payload_len > 2047:\n raise ValueError('payload size {} exceeds maximum length {} '\n 'of EtherCat message.'.format(payload_len, 2047))\n\n self.length = payload_len\n\n vlan_headers_total_size = 0\n upper_layer = self.underlayer\n\n # add size occupied by VLAN tags\n while upper_layer and isinstance(upper_layer, Dot1Q):\n vlan_headers_total_size += 4\n upper_layer = upper_layer.underlayer\n\n if not isinstance(upper_layer, Ether):\n raise Exception('missing Ether layer')\n\n pad_len = EtherCat.ETHER_FRAME_MIN_LEN - (EtherCat.ETHER_HEADER_LEN +\n vlan_headers_total_size +\n EtherCat.ETHERCAT_HEADER_LEN + # noqa: E501\n payload_len +\n EtherCat.ETHER_FSC_LEN)\n\n if pad_len > 0:\n pad = Padding()\n pad.load = b'\\x00' * pad_len\n\n return raw(_EtherCatLengthCalc(length=self.length,\n type=self.type)) + pay + raw(pad)\n return raw(_EtherCatLengthCalc(length=self.length,\n type=self.type)) + pay", "response": "This method is called when the packet is sent to the EtherCat."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncompute the interface ID in modified EUI - 64 format associated to the given MAC address.", "response": "def in6_mactoifaceid(mac, ulbit=None):\n \"\"\"\n Compute the interface ID in modified EUI-64 format associated\n to the Ethernet address provided as input.\n value taken by U/L bit in the interface identifier is basically\n the reversed value of that in given MAC address it can be forced\n to a specific value by using optional 'ulbit' parameter.\n \"\"\"\n if len(mac) != 17:\n return None\n m = \"\".join(mac.split(':'))\n if len(m) != 12:\n return None\n first = int(m[0:2], 16)\n if ulbit is None or not (ulbit == 0 or ulbit == 1):\n ulbit = [1, '-', 0][first & 0x02]\n ulbit *= 2\n first = \"%.02x\" % ((first & 0xFD) | ulbit)\n eui64 = first + m[2:4] + \":\" + m[4:6] + \"FF:FE\" + m[6:8] + \":\" + m[8:12]\n return eui64.upper()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nextracting the MAC address from a given iface ID.", "response": "def in6_ifaceidtomac(ifaceid):\n \"\"\"\n Extract the mac address from provided iface ID. Iface ID is provided\n in printable format (\"XXXX:XXFF:FEXX:XXXX\", eventually compressed). None\n is returned on error.\n \"\"\"\n try:\n # Set ifaceid to a binary form\n ifaceid = inet_pton(socket.AF_INET6, \"::\" + ifaceid)[8:16]\n except Exception:\n return None\n if ifaceid[3:5] != b'\\xff\\xfe': # Check for burned-in MAC address\n return None\n\n # Unpacking and converting first byte of faceid to MAC address equivalent\n first = struct.unpack(\"B\", ifaceid[:1])[0]\n ulbit = 2 * [1, '-', 0][first & 0x02]\n first = struct.pack(\"B\", ((first & 0xFD) | ulbit))\n # Split into two vars to remove the \\xff\\xfe bytes\n oui = first + ifaceid[1:3]\n end = ifaceid[5:]\n # Convert and reconstruct into a MAC Address\n mac_bytes = [\"%.02x\" % orb(x) for x in list(oui + end)]\n return \":\".join(mac_bytes)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef in6_addrtomac(addr):\n mask = inet_pton(socket.AF_INET6, \"::ffff:ffff:ffff:ffff\")\n x = in6_and(mask, inet_pton(socket.AF_INET6, addr))\n ifaceid = inet_ntop(socket.AF_INET6, x)[2:]\n return in6_ifaceidtomac(ifaceid)", "response": "Extract the mac address from provided address."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef in6_addrtovendor(addr):\n mac = in6_addrtomac(addr)\n if mac is None or conf.manufdb is None:\n return None\n\n res = conf.manufdb._get_manuf(mac)\n if len(res) == 17 and res.count(':') != 5: # Mac address, i.e. unknown\n res = \"UNKNOWN\"\n\n return res", "response": "Convert an IPv6 address to the vendor name."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef in6_getLinkScopedMcastAddr(addr, grpid=None, scope=2):\n if scope not in [0, 1, 2]:\n return None\n try:\n if not in6_islladdr(addr):\n return None\n addr = inet_pton(socket.AF_INET6, addr)\n except Exception:\n warning(\"in6_getLinkScopedMcastPrefix(): Invalid address provided\")\n return None\n\n iid = addr[8:]\n\n if grpid is None:\n grpid = b'\\x00\\x00\\x00\\x00'\n else:\n if isinstance(grpid, (bytes, str)):\n if len(grpid) == 8:\n try:\n grpid = int(grpid, 16) & 0xffffffff\n except Exception:\n warning(\"in6_getLinkScopedMcastPrefix(): Invalid group id provided\") # noqa: E501\n return None\n elif len(grpid) == 4:\n try:\n grpid = struct.unpack(\"!I\", grpid)[0]\n except Exception:\n warning(\"in6_getLinkScopedMcastPrefix(): Invalid group id provided\") # noqa: E501\n return None\n grpid = struct.pack(\"!I\", grpid)\n\n flgscope = struct.pack(\"B\", 0xff & ((0x3 << 4) | scope))\n plen = b'\\xff'\n res = b'\\x00'\n a = b'\\xff' + flgscope + res + plen + iid + grpid\n\n return inet_ntop(socket.AF_INET6, a)", "response": "Generate a Link - Scoped Multicast Address as described in RFC 4489."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef in6_getLocalUniquePrefix():\n # Extracted from RFC 1305 (NTP) :\n # NTP timestamps are represented as a 64-bit unsigned fixed-point number,\n # in seconds relative to 0h on 1 January 1900. The integer part is in the\n # first 32 bits and the fraction part in the last 32 bits.\n\n # epoch = (1900, 1, 1, 0, 0, 0, 5, 1, 0)\n # x = time.time()\n # from time import gmtime, strftime, gmtime, mktime\n # delta = mktime(gmtime(0)) - mktime(self.epoch)\n # x = x-delta\n\n tod = time.time() # time of day. Will bother with epoch later\n i = int(tod)\n j = int((tod - i) * (2**32))\n tod = struct.pack(\"!II\", i, j)\n mac = RandMAC()\n # construct modified EUI-64 ID\n eui64 = inet_pton(socket.AF_INET6, '::' + in6_mactoifaceid(mac))[8:]\n import hashlib\n globalid = hashlib.sha1(tod + eui64).digest()[:5]\n return inet_ntop(socket.AF_INET6, b'\\xfd' + globalid + b'\\x00' * 10)", "response": "Returns a pseudo - randomly generated Local Unique prefix."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef in6_getRandomizedIfaceId(ifaceid, previous=None):\n\n s = b\"\"\n if previous is None:\n d = b\"\".join(chb(x) for x in range(256))\n for _ in range(8):\n s += chb(random.choice(d))\n previous = s\n s = inet_pton(socket.AF_INET6, \"::\" + ifaceid)[8:] + previous\n import hashlib\n s = hashlib.md5(s).digest()\n s1, s2 = s[:8], s[8:]\n s1 = chb(orb(s1[0]) | 0x04) + s1[1:]\n s1 = inet_ntop(socket.AF_INET6, b\"\\xff\" * 8 + s1)[20:]\n s2 = inet_ntop(socket.AF_INET6, b\"\\xff\" * 8 + s2)[20:]\n return (s1, s2)", "response": "This function generates a randomized interface identifier for the specified interface ID."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nconverts an IPv6 address in printable representation to RFC 1924 Compact Representation ; - )", "response": "def in6_ptoc(addr):\n \"\"\"\n Converts an IPv6 address in printable representation to RFC\n 1924 Compact Representation ;-)\n Returns None on error.\n \"\"\"\n try:\n d = struct.unpack(\"!IIII\", inet_pton(socket.AF_INET6, addr))\n except Exception:\n return None\n res = 0\n m = [2**96, 2**64, 2**32, 1]\n for i in range(4):\n res += d[i] * m[i]\n rem = res\n res = []\n while rem:\n res.append(_rfc1924map[rem % 85])\n rem = rem // 85\n res.reverse()\n return \"\".join(res)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef in6_isaddrTeredo(x):\n our = inet_pton(socket.AF_INET6, x)[0:4]\n teredoPrefix = inet_pton(socket.AF_INET6, conf.teredoPrefix)[0:4]\n return teredoPrefix == our", "response": "Returns True if provided address is a Teredo."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef teredoAddrExtractInfo(x):\n addr = inet_pton(socket.AF_INET6, x)\n server = inet_ntop(socket.AF_INET, addr[4:8])\n flag = struct.unpack(\"!H\", addr[8:10])[0]\n mappedport = struct.unpack(\"!H\", strxor(addr[10:12], b'\\xff' * 2))[0]\n mappedaddr = inet_ntop(socket.AF_INET, strxor(addr[12:16], b'\\xff' * 4))\n return server, flag, mappedaddr, mappedport", "response": "Extract information from a Teredo address."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef in6_iseui64(x):\n eui64 = inet_pton(socket.AF_INET6, '::ff:fe00:0')\n x = in6_and(inet_pton(socket.AF_INET6, x), eui64)\n return x == eui64", "response": "Returns True if provided address has an interface identifier part\n created in modified EUI - 64 format."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef in6_getnsma(a):\n\n r = in6_and(a, inet_pton(socket.AF_INET6, '::ff:ffff'))\n r = in6_or(inet_pton(socket.AF_INET6, 'ff02::1:ff00:0'), r)\n return r", "response": "Returns a network format multicast address for given address."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef in6_getha(prefix):\n r = in6_and(inet_pton(socket.AF_INET6, prefix), in6_cidr2mask(64))\n r = in6_or(r, inet_pton(socket.AF_INET6, '::fdff:ffff:ffff:fffe'))\n return inet_ntop(socket.AF_INET6, r)", "response": "Returns the anycast address associated with all home agents on a given subnet."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef in6_isincluded(addr, prefix, plen):\n temp = inet_pton(socket.AF_INET6, addr)\n pref = in6_cidr2mask(plen)\n zero = inet_pton(socket.AF_INET6, prefix)\n return zero == in6_and(temp, pref)", "response": "Returns True when addr belongs to prefix and plen."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns True if provided address is a link - local solicited node multicast address i. e. belongs to ff00 : 0 / 104. False is returned otherwise.", "response": "def in6_isllsnmaddr(str):\n \"\"\"\n Return True if provided address is a link-local solicited node\n multicast address, i.e. belongs to ff02::1:ff00:0/104. False is\n returned otherwise.\n \"\"\"\n temp = in6_and(b\"\\xff\" * 13 + b\"\\x00\" * 3, inet_pton(socket.AF_INET6, str))\n temp2 = b'\\xff\\x02\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\\xff\\x00\\x00\\x00'\n return temp == temp2"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef in6_getscope(addr):\n if in6_isgladdr(addr) or in6_isuladdr(addr):\n scope = IPV6_ADDR_GLOBAL\n elif in6_islladdr(addr):\n scope = IPV6_ADDR_LINKLOCAL\n elif in6_issladdr(addr):\n scope = IPV6_ADDR_SITELOCAL\n elif in6_ismaddr(addr):\n if in6_ismgladdr(addr):\n scope = IPV6_ADDR_GLOBAL\n elif in6_ismlladdr(addr):\n scope = IPV6_ADDR_LINKLOCAL\n elif in6_ismsladdr(addr):\n scope = IPV6_ADDR_SITELOCAL\n elif in6_ismnladdr(addr):\n scope = IPV6_ADDR_LOOPBACK\n else:\n scope = -1\n elif addr == '::1':\n scope = IPV6_ADDR_LOOPBACK\n else:\n scope = -1\n return scope", "response": "Returns the scope of the address."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef in6_get_common_plen(a, b):\n def matching_bits(byte1, byte2):\n for i in range(8):\n cur_mask = 0x80 >> i\n if (byte1 & cur_mask) != (byte2 & cur_mask):\n return i\n return 8\n\n tmpA = inet_pton(socket.AF_INET6, a)\n tmpB = inet_pton(socket.AF_INET6, b)\n for i in range(16):\n mbits = matching_bits(orb(tmpA[i]), orb(tmpB[i]))\n if mbits != 8:\n return 8 * i + mbits\n return 128", "response": "Return the common prefix length of IPv6 addresses a and b."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef in6_isvalid(address):\n\n try:\n socket.inet_pton(socket.AF_INET6, address)\n return True\n except Exception:\n return False", "response": "Return True if the given string is a valid IPv6 address string False otherwise."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the IPv4 address configured on ifname packed with inet_pton.", "response": "def get_if_raw_addr(ifname):\n \"\"\"Returns the IPv4 address configured on 'ifname', packed with inet_pton.\"\"\" # noqa: E501\n\n # Get ifconfig output\n try:\n fd = os.popen(\"%s %s\" % (conf.prog.ifconfig, ifname))\n except OSError as msg:\n warning(\"Failed to execute ifconfig: (%s)\", msg)\n return b\"\\0\\0\\0\\0\"\n\n # Get IPv4 addresses\n addresses = [l for l in fd if l.find(\"inet \") >= 0]\n if not addresses:\n warning(\"No IPv4 address found on %s !\", ifname)\n return b\"\\0\\0\\0\\0\"\n\n # Pack the first address\n address = addresses[0].split(' ')[1]\n if '/' in address: # NetBSD 8.0\n address = address.split(\"/\")[0]\n return socket.inet_pton(socket.AF_INET, address)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the packed MAC address configured on ifname.", "response": "def get_if_raw_hwaddr(ifname):\n \"\"\"Returns the packed MAC address configured on 'ifname'.\"\"\"\n\n NULL_MAC_ADDRESS = b'\\x00' * 6\n\n # Handle the loopback interface separately\n if ifname == LOOPBACK_NAME:\n return (ARPHDR_LOOPBACK, NULL_MAC_ADDRESS)\n\n # Get ifconfig output\n try:\n fd = os.popen(\"%s %s\" % (conf.prog.ifconfig, ifname))\n except OSError as msg:\n raise Scapy_Exception(\"Failed to execute ifconfig: (%s)\" % msg)\n\n # Get MAC addresses\n addresses = [l for l in fd.readlines() if l.find(\"ether\") >= 0 or\n l.find(\"lladdr\") >= 0 or\n l.find(\"address\") >= 0]\n if not addresses:\n raise Scapy_Exception(\"No MAC address found on %s !\" % ifname)\n\n # Pack and return the MAC address\n mac = addresses[0].split(' ')[1]\n mac = [chr(int(b, 16)) for b in mac.split(':')]\n return (ARPHDR_ETHER, ''.join(mac))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_dev_bpf():\n\n # Get the first available BPF handle\n for bpf in range(256):\n try:\n fd = os.open(\"/dev/bpf%i\" % bpf, os.O_RDWR)\n return (fd, bpf)\n except OSError:\n continue\n\n raise Scapy_Exception(\"No /dev/bpf handle is available !\")", "response": "Returns an opened BPF file object"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef attach_filter(fd, bpf_filter, iface):\n bp = compile_filter(bpf_filter, iface)\n # Assign the BPF program to the interface\n ret = LIBC.ioctl(c_int(fd), BIOCSETF, cast(pointer(bp), c_char_p))\n if ret < 0:\n raise Scapy_Exception(\"Can't attach the BPF filter !\")", "response": "Attach a BPF filter to the BPF file descriptor"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a list containing all network interfaces.", "response": "def get_if_list():\n \"\"\"Returns a list containing all network interfaces.\"\"\"\n\n # Get ifconfig output\n try:\n fd = os.popen(\"%s -a\" % conf.prog.ifconfig)\n except OSError as msg:\n raise Scapy_Exception(\"Failed to execute ifconfig: (%s)\" % msg)\n\n # Get interfaces\n interfaces = [line[:line.find(':')] for line in fd.readlines()\n if \": flags\" in line.lower()]\n return interfaces"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_working_ifaces():\n\n # Only root is allowed to perform the following ioctl() call\n if os.getuid() != 0:\n return []\n\n # Test all network interfaces\n interfaces = []\n for ifname in get_if_list():\n\n # Unlike pcap_findalldevs(), we do not care of loopback interfaces.\n if ifname == LOOPBACK_NAME:\n continue\n\n # Get interface flags\n try:\n result = get_if(ifname, SIOCGIFFLAGS)\n except IOError:\n warning(\"ioctl(SIOCGIFFLAGS) failed on %s !\", ifname)\n continue\n\n # Convert flags\n ifflags = struct.unpack(\"16xH14x\", result)[0]\n if ifflags & 0x1: # IFF_UP\n\n # Get a BPF handle\n fd = get_dev_bpf()[0]\n if fd is None:\n raise Scapy_Exception(\"No /dev/bpf are available !\")\n\n # Check if the interface can be used\n try:\n fcntl.ioctl(fd, BIOCSETIF, struct.pack(\"16s16x\",\n ifname.encode()))\n except IOError:\n pass\n else:\n ifnum, ifab = _IFNUM.search(ifname).groups()\n interfaces.append((ifname, int(ifnum) if ifnum else -1, ifab))\n finally:\n # Close the file descriptor\n os.close(fd)\n\n # Sort to mimic pcap_findalldevs() order\n interfaces.sort(key=lambda elt: (elt[1], elt[2], elt[0]))\n\n return [iface[0] for iface in interfaces]", "response": "Returns an ordered list of interfaces that could be used with BPF."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating a temporary file and returns its path.", "response": "def get_temp_file(keep=False, autoext=\"\", fd=False):\n \"\"\"Creates a temporary file.\n\n :param keep: If False, automatically delete the file when Scapy exits.\n :param autoext: Suffix to add to the generated file name.\n :param fd: If True, this returns a file-like object with the temporary\n file opened. If False (default), this returns a file path.\n \"\"\"\n f = tempfile.NamedTemporaryFile(prefix=\"scapy\", suffix=autoext,\n delete=False)\n if not keep:\n conf.temp_files.append(f.name)\n\n if fd:\n return f\n else:\n # Close the file so something else can take it.\n f.close()\n return f.name"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate a temporary file and returns its name.", "response": "def get_temp_dir(keep=False):\n \"\"\"Creates a temporary file, and returns its name.\n\n :param keep: If False (default), the directory will be recursively\n deleted when Scapy exits.\n :return: A full path to a temporary directory.\n \"\"\"\n\n dname = tempfile.mkdtemp(prefix=\"scapy\")\n\n if not keep:\n conf.temp_files.append(dname)\n\n return dname"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nbuilding a tcpdump like hexadecimal view of a Packet", "response": "def hexdump(x, dump=False):\n \"\"\"Build a tcpdump like hexadecimal view\n\n :param x: a Packet\n :param dump: define if the result must be printed or returned in a variable\n :returns: a String only when dump=True\n \"\"\"\n s = \"\"\n x = bytes_encode(x)\n x_len = len(x)\n i = 0\n while i < x_len:\n s += \"%04x \" % i\n for j in range(16):\n if i + j < x_len:\n s += \"%02X \" % orb(x[i + j])\n else:\n s += \" \"\n s += \" %s\\n\" % sane_color(x[i:i + 16])\n i += 16\n # remove trailing \\n\n s = s[:-1] if s.endswith(\"\\n\") else s\n if dump:\n return s\n else:\n print(s)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nbuilds an equivalent view of hexdump() on a single line Note that setting both onlyasc and onlyhex to 1 results in a empty output :param x: a Packet :param onlyasc: 1 to display only the ascii view :param onlyhex: 1 to display only the hexadecimal view :param dump: print the view if False :returns: a String only when dump=True", "response": "def linehexdump(x, onlyasc=0, onlyhex=0, dump=False):\n \"\"\"Build an equivalent view of hexdump() on a single line\n\n Note that setting both onlyasc and onlyhex to 1 results in a empty output\n\n :param x: a Packet\n :param onlyasc: 1 to display only the ascii view\n :param onlyhex: 1 to display only the hexadecimal view\n :param dump: print the view if False\n :returns: a String only when dump=True\n \"\"\"\n s = \"\"\n s = hexstr(x, onlyasc=onlyasc, onlyhex=onlyhex, color=not dump)\n if dump:\n return s\n else:\n print(s)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nbuilding a per byte hexadecimal representation of a packet.", "response": "def chexdump(x, dump=False):\n \"\"\"Build a per byte hexadecimal representation\n\n Example:\n >>> chexdump(IP())\n 0x45, 0x00, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x40, 0x00, 0x7c, 0xe7, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00, 0x00, 0x01 # noqa: E501\n\n :param x: a Packet\n :param dump: print the view if False\n :returns: a String only if dump=True\n \"\"\"\n x = bytes_encode(x)\n s = \", \".join(\"%#04x\" % orb(x) for x in x)\n if dump:\n return s\n else:\n print(s)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nbuilds a fancy tcpdump like hex from bytes.", "response": "def hexstr(x, onlyasc=0, onlyhex=0, color=False):\n \"\"\"Build a fancy tcpdump like hex from bytes.\"\"\"\n x = bytes_encode(x)\n _sane_func = sane_color if color else sane\n s = []\n if not onlyasc:\n s.append(\" \".join(\"%02X\" % orb(b) for b in x))\n if not onlyhex:\n s.append(_sane_func(x))\n return \" \".join(s)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nshow differences between two binary strings", "response": "def hexdiff(x, y):\n \"\"\"Show differences between 2 binary strings\"\"\"\n x = bytes_encode(x)[::-1]\n y = bytes_encode(y)[::-1]\n SUBST = 1\n INSERT = 1\n d = {(-1, -1): (0, (-1, -1))}\n for j in range(len(y)):\n d[-1, j] = d[-1, j - 1][0] + INSERT, (-1, j - 1)\n for i in range(len(x)):\n d[i, -1] = d[i - 1, -1][0] + INSERT, (i - 1, -1)\n\n for j in range(len(y)):\n for i in range(len(x)):\n d[i, j] = min((d[i - 1, j - 1][0] + SUBST * (x[i] != y[j]), (i - 1, j - 1)), # noqa: E501\n (d[i - 1, j][0] + INSERT, (i - 1, j)),\n (d[i, j - 1][0] + INSERT, (i, j - 1)))\n\n backtrackx = []\n backtracky = []\n i = len(x) - 1\n j = len(y) - 1\n while not (i == j == -1):\n i2, j2 = d[i, j][1]\n backtrackx.append(x[i2 + 1:i + 1])\n backtracky.append(y[j2 + 1:j + 1])\n i, j = i2, j2\n\n x = y = i = 0\n colorize = {0: lambda x: x,\n -1: conf.color_theme.left,\n 1: conf.color_theme.right}\n\n dox = 1\n doy = 0\n btx_len = len(backtrackx)\n while i < btx_len:\n linex = backtrackx[i:i + 16]\n liney = backtracky[i:i + 16]\n xx = sum(len(k) for k in linex)\n yy = sum(len(k) for k in liney)\n if dox and not xx:\n dox = 0\n doy = 1\n if dox and linex == liney:\n doy = 1\n\n if dox:\n xd = y\n j = 0\n while not linex[j]:\n j += 1\n xd -= 1\n print(colorize[doy - dox](\"%04x\" % xd), end=' ')\n x += xx\n line = linex\n else:\n print(\" \", end=' ')\n if doy:\n yd = y\n j = 0\n while not liney[j]:\n j += 1\n yd -= 1\n print(colorize[doy - dox](\"%04x\" % yd), end=' ')\n y += yy\n line = liney\n else:\n print(\" \", end=' ')\n\n print(\" \", end=' ')\n\n cl = \"\"\n for j in range(16):\n if i + j < btx_len:\n if line[j]:\n col = colorize[(linex[j] != liney[j]) * (doy - dox)]\n print(col(\"%02X\" % orb(line[j])), end=' ')\n if linex[j] == liney[j]:\n cl += sane_color(line[j])\n else:\n cl += col(sane(line[j]))\n else:\n print(\" \", end=' ')\n cl += \" \"\n else:\n print(\" \", end=' ')\n if j == 7:\n print(\"\", end=' ')\n\n print(\" \", cl)\n\n if doy or not yy:\n doy = 0\n dox = 1\n i += 16\n else:\n if yy:\n dox = 0\n doy = 1\n else:\n i += 16"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncalculating the Fletcher - 16 checkbytes returned as 2 byte binary - string.", "response": "def fletcher16_checkbytes(binbuf, offset):\n \"\"\"Calculates the Fletcher-16 checkbytes returned as 2 byte binary-string.\n\n Including the bytes into the buffer (at the position marked by offset) the # noqa: E501\n global Fletcher-16 checksum of the buffer will be 0. Thus it is easy to verify # noqa: E501\n the integrity of the buffer on the receiver side.\n\n For details on the algorithm, see RFC 2328 chapter 12.1.7 and RFC 905 Annex B. # noqa: E501\n \"\"\"\n\n # This is based on the GPLed C implementation in Zebra # noqa: E501\n if len(binbuf) < offset:\n raise Exception(\"Packet too short for checkbytes %d\" % len(binbuf))\n\n binbuf = binbuf[:offset] + b\"\\x00\\x00\" + binbuf[offset + 2:]\n (c0, c1) = _fletcher16(binbuf)\n\n x = ((len(binbuf) - offset - 1) * c0 - c1) % 255\n\n if (x <= 0):\n x += 255\n\n y = 510 - c0 - x\n\n if (y > 255):\n y -= 255\n return chb(x) + chb(y)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef randstring(l):\n return b\"\".join(struct.pack('B', random.randint(0, 255)) for _ in range(l))", "response": "Returns a random string of length l"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the binary XOR of the 2 provided strings s1 and s2.", "response": "def strxor(s1, s2):\n \"\"\"\n Returns the binary XOR of the 2 provided strings s1 and s2. s1 and s2\n must be of same length.\n \"\"\"\n return b\"\".join(map(lambda x, y: chb(orb(x) ^ orb(y)), s1, s2))"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef do_graph(graph, prog=None, format=None, target=None, type=None, string=None, options=None): # noqa: E501\n\n if format is None:\n if WINDOWS:\n format = \"png\" # use common format to make sure a viewer is installed # noqa: E501\n else:\n format = \"svg\"\n if string:\n return graph\n if type is not None:\n format = type\n if prog is None:\n prog = conf.prog.dot\n start_viewer = False\n if target is None:\n if WINDOWS:\n target = get_temp_file(autoext=\".\" + format)\n start_viewer = True\n else:\n with ContextManagerSubprocess(\"do_graph()\", conf.prog.display):\n target = subprocess.Popen([conf.prog.display],\n stdin=subprocess.PIPE).stdin\n if format is not None:\n format = \"-T%s\" % format\n if isinstance(target, str):\n if target.startswith('|'):\n target = subprocess.Popen(target[1:].lstrip(), shell=True,\n stdin=subprocess.PIPE).stdin\n elif target.startswith('>'):\n target = open(target[1:].lstrip(), \"wb\")\n else:\n target = open(os.path.abspath(target), \"wb\")\n proc = subprocess.Popen(\"\\\"%s\\\" %s %s\" % (prog, options or \"\", format or \"\"), # noqa: E501\n shell=True, stdin=subprocess.PIPE, stdout=target)\n proc.stdin.write(bytes_encode(graph))\n proc.stdin.close()\n proc.wait()\n try:\n target.close()\n except Exception:\n pass\n if start_viewer:\n # Workaround for file not found error: We wait until tempfile is written. # noqa: E501\n waiting_start = time.time()\n while not os.path.exists(target.name):\n time.sleep(0.1)\n if time.time() - waiting_start > 3:\n warning(\"Temporary file '%s' could not be written. Graphic will not be displayed.\", tempfile) # noqa: E501\n break\n else:\n if conf.prog.display == conf.prog._default:\n os.startfile(target.name)\n else:\n with ContextManagerSubprocess(\"do_graph()\", conf.prog.display):\n subprocess.Popen([conf.prog.display, target.name])", "response": "This function will run a GraphViz program and return the graph as a string."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\npickling a Python object to a gzipped file.", "response": "def save_object(fname, obj):\n \"\"\"Pickle a Python object\"\"\"\n\n fd = gzip.open(fname, \"wb\")\n six.moves.cPickle.dump(obj, fd)\n fd.close()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncorrupt a given percentage or number of bytes from a string", "response": "def corrupt_bytes(s, p=0.01, n=None):\n \"\"\"Corrupt a given percentage or number of bytes from a string\"\"\"\n s = array.array(\"B\", bytes_encode(s))\n s_len = len(s)\n if n is None:\n n = max(1, int(s_len * p))\n for i in random.sample(range(s_len), n):\n s[i] = (s[i] + random.randint(1, 255)) % 256\n return s.tostring() if six.PY2 else s.tobytes()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef wrpcap(filename, pkt, *args, **kargs):\n with PcapWriter(filename, *args, **kargs) as fdesc:\n fdesc.write(pkt)", "response": "Write a list of packets to a pcap file"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef rdpcap(filename, count=-1):\n with PcapReader(filename) as fdesc:\n return fdesc.read_all(count=count)", "response": "Read a pcap or pcapng file and return a list of packets"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef import_hexcap():\n re_extract_hexcap = re.compile(r\"^((0x)?[0-9a-fA-F]{2,}[ :\\t]{,3}|) *(([0-9a-fA-F]{2} {,2}){,16})\") # noqa: E501\n p = \"\"\n try:\n while True:\n line = input().strip()\n if not line:\n break\n try:\n p += re_extract_hexcap.match(line).groups()[2]\n except Exception:\n warning(\"Parsing error during hexcap\")\n continue\n except EOFError:\n pass\n\n p = p.replace(\" \", \"\")\n return hex_bytes(p)", "response": "Imports a tcpdump like hexadecimal view of a node s network."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nrun Wireshark on a list of packets.", "response": "def wireshark(pktlist, wait=False, **kwargs):\n \"\"\"\n Runs Wireshark on a list of packets.\n\n See :func:`tcpdump` for more parameter description.\n\n Note: this defaults to wait=False, to run Wireshark in the background.\n \"\"\"\n return tcpdump(pktlist, prog=conf.prog.wireshark, wait=wait, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef tdecode(pktlist, args=None, **kwargs):\n if args is None:\n args = [\"-V\"]\n return tcpdump(pktlist, prog=conf.prog.tshark, args=args, **kwargs)", "response": "Run tshark on a list of packets."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef tcpdump(pktlist, dump=False, getfd=False, args=None,\n prog=None, getproc=False, quiet=False, use_tempfile=None,\n read_stdin_opts=None, linktype=None, wait=True):\n \"\"\"Run tcpdump or tshark on a list of packets.\n\n When using ``tcpdump`` on OSX (``prog == conf.prog.tcpdump``), this uses a\n temporary file to store the packets. This works around a bug in Apple's\n version of ``tcpdump``: http://apple.stackexchange.com/questions/152682/\n\n Otherwise, the packets are passed in stdin.\n\n This function can be explicitly enabled or disabled with the\n ``use_tempfile`` parameter.\n\n When using ``wireshark``, it will be called with ``-ki -`` to start\n immediately capturing packets from stdin.\n\n Otherwise, the command will be run with ``-r -`` (which is correct for\n ``tcpdump`` and ``tshark``).\n\n This can be overridden with ``read_stdin_opts``. This has no effect when\n ``use_tempfile=True``, or otherwise reading packets from a regular file.\n\npktlist: a Packet instance, a PacketList instance or a list of Packet\n instances. Can also be a filename (as a string), an open\n file-like object that must be a file format readable by\n tshark (Pcap, PcapNg, etc.) or None (to sniff)\n\ndump: when set to True, returns a string instead of displaying it.\ngetfd: when set to True, returns a file-like object to read data\n from tcpdump or tshark from.\ngetproc: when set to True, the subprocess.Popen object is returned\nargs: arguments (as a list) to pass to tshark (example for tshark:\n args=[\"-T\", \"json\"]).\nprog: program to use (defaults to tcpdump, will work with tshark)\nquiet: when set to True, the process stderr is discarded\nuse_tempfile: When set to True, always use a temporary file to store packets.\n When set to False, pipe packets through stdin.\n When set to None (default), only use a temporary file with\n ``tcpdump`` on OSX.\nread_stdin_opts: When set, a list of arguments needed to capture from stdin.\n Otherwise, attempts to guess.\nlinktype: If a Packet (or list) is passed in the ``pktlist`` argument,\n set the ``linktype`` parameter on ``wrpcap``. If ``pktlist`` is a\n path to a pcap file, then this option will have no effect.\nwait: If True (default), waits for the process to terminate before returning\n to Scapy. If False, the process will be detached to the background. If\n dump, getproc or getfd is True, these have the same effect as\n ``wait=False``.\n\nExamples:\n\n>>> tcpdump([IP()/TCP(), IP()/UDP()])\nreading from file -, link-type RAW (Raw IP)\n16:46:00.474515 IP 127.0.0.1.20 > 127.0.0.1.80: Flags [S], seq 0, win 8192, length 0 # noqa: E501\n16:46:00.475019 IP 127.0.0.1.53 > 127.0.0.1.53: [|domain]\n\n>>> tcpdump([IP()/TCP(), IP()/UDP()], prog=conf.prog.tshark)\n 1 0.000000 127.0.0.1 -> 127.0.0.1 TCP 40 20->80 [SYN] Seq=0 Win=8192 Len=0 # noqa: E501\n 2 0.000459 127.0.0.1 -> 127.0.0.1 UDP 28 53->53 Len=0\n\nTo get a JSON representation of a tshark-parsed PacketList(), one can:\n>>> import json, pprint\n>>> json_data = json.load(tcpdump(IP(src=\"217.25.178.5\", dst=\"45.33.32.156\"),\n... prog=conf.prog.tshark, args=[\"-T\", \"json\"],\n... getfd=True))\n>>> pprint.pprint(json_data)\n[{u'_index': u'packets-2016-12-23',\n u'_score': None,\n u'_source': {u'layers': {u'frame': {u'frame.cap_len': u'20',\n u'frame.encap_type': u'7',\n[...]\n u'frame.time_relative': u'0.000000000'},\n u'ip': {u'ip.addr': u'45.33.32.156',\n u'ip.checksum': u'0x0000a20d',\n[...]\n u'ip.ttl': u'64',\n u'ip.version': u'4'},\n u'raw': u'Raw packet data'}},\n u'_type': u'pcap_file'}]\n>>> json_data[0]['_source']['layers']['ip']['ip.ttl']\nu'64'\n \"\"\"\n getfd = getfd or getproc\n if prog is None:\n prog = [conf.prog.tcpdump]\n _prog_name = \"windump()\" if WINDOWS else \"tcpdump()\"\n elif isinstance(prog, six.string_types):\n _prog_name = \"{}()\".format(prog)\n prog = [prog]\n else:\n raise ValueError(\"prog must be a string\")\n\n # Build Popen arguments\n if args is None:\n args = []\n else:\n # Make a copy of args\n args = list(args)\n\n stdout = subprocess.PIPE if dump or getfd else None\n stderr = open(os.devnull) if quiet else None\n\n if use_tempfile is None:\n # Apple's tcpdump cannot read from stdin, see:\n # http://apple.stackexchange.com/questions/152682/\n use_tempfile = DARWIN and prog[0] == conf.prog.tcpdump\n\n if read_stdin_opts is None:\n if prog[0] == conf.prog.wireshark:\n # Start capturing immediately (-k) from stdin (-i -)\n read_stdin_opts = [\"-ki\", \"-\"]\n else:\n read_stdin_opts = [\"-r\", \"-\"]\n else:\n # Make a copy of read_stdin_opts\n read_stdin_opts = list(read_stdin_opts)\n\n if pktlist is None:\n # sniff\n with ContextManagerSubprocess(_prog_name, prog[0]):\n proc = subprocess.Popen(\n prog + args,\n stdout=stdout,\n stderr=stderr,\n )\n elif isinstance(pktlist, six.string_types):\n # file\n with ContextManagerSubprocess(_prog_name, prog[0]):\n proc = subprocess.Popen(\n prog + [\"-r\", pktlist] + args,\n stdout=stdout,\n stderr=stderr,\n )\n elif use_tempfile:\n tmpfile = get_temp_file(autoext=\".pcap\", fd=True)\n try:\n tmpfile.writelines(iter(lambda: pktlist.read(1048576), b\"\"))\n except AttributeError:\n wrpcap(tmpfile, pktlist, linktype=linktype)\n else:\n tmpfile.close()\n with ContextManagerSubprocess(_prog_name, prog[0]):\n proc = subprocess.Popen(\n prog + [\"-r\", tmpfile.name] + args,\n stdout=stdout,\n stderr=stderr,\n )\n else:\n # pass the packet stream\n with ContextManagerSubprocess(_prog_name, prog[0]):\n proc = subprocess.Popen(\n prog + read_stdin_opts + args,\n stdin=subprocess.PIPE,\n stdout=stdout,\n stderr=stderr,\n )\n try:\n proc.stdin.writelines(iter(lambda: pktlist.read(1048576), b\"\"))\n except AttributeError:\n wrpcap(proc.stdin, pktlist, linktype=linktype)\n except UnboundLocalError:\n raise IOError(\"%s died unexpectedly !\" % prog)\n else:\n proc.stdin.close()\n if dump:\n return b\"\".join(iter(lambda: proc.stdout.read(1048576), b\"\"))\n if getproc:\n return proc\n if getfd:\n return proc.stdout\n if wait:\n proc.wait()", "response": "Run tcpdump or tshark on a list of packets."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nrun hexedit on a list of packets then return the edited packets.", "response": "def hexedit(pktlist):\n \"\"\"Run hexedit on a list of packets, then return the edited packets.\"\"\"\n f = get_temp_file()\n wrpcap(f, pktlist)\n with ContextManagerSubprocess(\"hexedit()\", conf.prog.hexedit):\n subprocess.call([conf.prog.hexedit, f])\n pktlist = rdpcap(f)\n os.unlink(f)\n return pktlist"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_terminal_width():\n # Let's first try using the official API\n # (Python 3.3+)\n if not six.PY2:\n import shutil\n sizex = shutil.get_terminal_size(fallback=(0, 0))[0]\n if sizex != 0:\n return sizex\n # Backups / Python 2.7\n if WINDOWS:\n from ctypes import windll, create_string_buffer\n # http://code.activestate.com/recipes/440694-determine-size-of-console-window-on-windows/\n h = windll.kernel32.GetStdHandle(-12)\n csbi = create_string_buffer(22)\n res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)\n if res:\n (bufx, bufy, curx, cury, wattr,\n left, top, right, bottom, maxx, maxy) = struct.unpack(\"hhhhHhhhhhh\", csbi.raw) # noqa: E501\n sizex = right - left + 1\n # sizey = bottom - top + 1\n return sizex\n return None\n else:\n # We have various methods\n sizex = None\n # COLUMNS is set on some terminals\n try:\n sizex = int(os.environ['COLUMNS'])\n except Exception:\n pass\n if sizex:\n return sizex\n # We can query TIOCGWINSZ\n try:\n import fcntl\n import termios\n s = struct.pack('HHHH', 0, 0, 0, 0)\n x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)\n sizex = struct.unpack('HHHH', x)[1]\n except IOError:\n pass\n return sizex", "response": "Get the terminal width of a node."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncores function of the make_table suite, which generates the table", "response": "def __make_table(yfmtfunc, fmtfunc, endline, data, fxyz, sortx=None, sorty=None, seplinefunc=None): # noqa: E501\n \"\"\"Core function of the make_table suite, which generates the table\"\"\"\n vx = {}\n vy = {}\n vz = {}\n vxf = {}\n\n # Python 2 backward compatibility\n fxyz = lambda_tuple_converter(fxyz)\n\n tmp_len = 0\n for e in data:\n xx, yy, zz = [str(s) for s in fxyz(*e)]\n tmp_len = max(len(yy), tmp_len)\n vx[xx] = max(vx.get(xx, 0), len(xx), len(zz))\n vy[yy] = None\n vz[(xx, yy)] = zz\n\n vxk = list(vx)\n vyk = list(vy)\n if sortx:\n vxk.sort(key=sortx)\n else:\n try:\n vxk.sort(key=int)\n except Exception:\n try:\n vxk.sort(key=atol)\n except Exception:\n vxk.sort()\n if sorty:\n vyk.sort(key=sorty)\n else:\n try:\n vyk.sort(key=int)\n except Exception:\n try:\n vyk.sort(key=atol)\n except Exception:\n vyk.sort()\n\n if seplinefunc:\n sepline = seplinefunc(tmp_len, [vx[x] for x in vxk])\n print(sepline)\n\n fmt = yfmtfunc(tmp_len)\n print(fmt % \"\", end=' ')\n for x in vxk:\n vxf[x] = fmtfunc(vx[x])\n print(vxf[x] % x, end=' ')\n print(endline)\n if seplinefunc:\n print(sepline)\n for y in vyk:\n print(fmt % y, end=' ')\n for x in vxk:\n print(vxf[x] % vz.get((x, y), \"-\"), end=' ')\n print(endline)\n if seplinefunc:\n print(sepline)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef whois(ip_address):\n whois_ip = str(ip_address)\n try:\n query = socket.gethostbyname(whois_ip)\n except Exception:\n query = whois_ip\n s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n s.connect((\"whois.ripe.net\", 43))\n s.send(query.encode(\"utf8\") + b\"\\r\\n\")\n answer = b\"\"\n while True:\n d = s.recv(4096)\n answer += d\n if not d:\n break\n s.close()\n ignore_tag = b\"remarks:\"\n # ignore all lines starting with the ignore_tag\n lines = [line for line in answer.split(b\"\\n\") if not line or (line and not line.startswith(ignore_tag))] # noqa: E501\n # remove empty lines at the bottom\n for i in range(1, len(lines)):\n if not lines[-i].strip():\n del lines[-i]\n else:\n break\n return b\"\\n\".join(lines[3:])", "response": "Whois client for Python"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nopens ( if necessary ) filename and read the magic.", "response": "def open(filename):\n \"\"\"Open (if necessary) filename, and read the magic.\"\"\"\n if isinstance(filename, six.string_types):\n try:\n fdesc = gzip.open(filename, \"rb\")\n magic = fdesc.read(4)\n except IOError:\n fdesc = open(filename, \"rb\")\n magic = fdesc.read(4)\n else:\n fdesc = filename\n filename = getattr(fdesc, \"name\", \"No name\")\n magic = fdesc.read(4)\n return filename, fdesc, magic"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef read_all(self, count=-1):\n res = []\n while count != 0:\n count -= 1\n p = self.read_packet()\n if p is None:\n break\n res.append(p)\n return res", "response": "return a list of all packets in the pcap file"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef read_packet(self, size=MTU):\n while True:\n try:\n blocktype, blocklen = struct.unpack(self.endian + \"2I\",\n self.f.read(8))\n except struct.error:\n return None\n block = self.f.read(blocklen - 12)\n if blocklen % 4:\n pad = self.f.read(4 - (blocklen % 4))\n warning(\"PcapNg: bad blocklen %d (MUST be a multiple of 4. \"\n \"Ignored padding %r\" % (blocklen, pad))\n try:\n if (blocklen,) != struct.unpack(self.endian + 'I',\n self.f.read(4)):\n warning(\"PcapNg: Invalid pcapng block (bad blocklen)\")\n except struct.error:\n return None\n res = self.blocktypes.get(blocktype,\n lambda block, size: None)(block, size)\n if res is not None:\n return res", "response": "Read a packet from the file and return the corresponding object."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef read_block_idb(self, block, _):\n options = block[16:]\n tsresol = 1000000\n while len(options) >= 4:\n code, length = struct.unpack(self.endian + \"HH\", options[:4])\n # PCAP Next Generation (pcapng) Capture File Format\n # 4.2. - Interface Description Block\n # http://xml2rfc.tools.ietf.org/cgi-bin/xml2rfc.cgi?url=https://raw.githubusercontent.com/pcapng/pcapng/master/draft-tuexen-opsawg-pcapng.xml&modeAsFormat=html/ascii&type=ascii#rfc.section.4.2\n if code == 9 and length == 1 and len(options) >= 5:\n tsresol = orb(options[4])\n tsresol = (2 if tsresol & 128 else 10) ** (tsresol & 127)\n if code == 0:\n if length != 0:\n warning(\"PcapNg: invalid option length %d for end-of-option\" % length) # noqa: E501\n break\n if length % 4:\n length += (4 - (length % 4))\n options = options[4 + length:]\n self.interfaces.append(struct.unpack(self.endian + \"HxxI\", block[:8]) +\n (tsresol,))", "response": "Read the IDB block and store it in the internal list."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nread an enhanced packet block.", "response": "def read_block_epb(self, block, size):\n \"\"\"Enhanced Packet Block\"\"\"\n intid, tshigh, tslow, caplen, wirelen = struct.unpack(\n self.endian + \"5I\",\n block[:20],\n )\n return (block[20:20 + caplen][:size],\n RawPcapNgReader.PacketMetadata(linktype=self.interfaces[intid][0], # noqa: E501\n tsresol=self.interfaces[intid][2], # noqa: E501\n tshigh=tshigh,\n tslow=tslow,\n wirelen=wirelen))"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nwrite a Packet or bytes to a pcap file.", "response": "def write(self, pkt):\n \"\"\"\n Writes a Packet or bytes to a pcap file.\n\n :param pkt: Packet(s) to write (one record for each Packet), or raw\n bytes to write (as one record).\n :type pkt: iterable[Packet], Packet or bytes\n \"\"\"\n if isinstance(pkt, bytes):\n if not self.header_present:\n self._write_header(pkt)\n self._write_packet(pkt)\n else:\n pkt = pkt.__iter__()\n for p in pkt:\n if not self.header_present:\n self._write_header(p)\n self._write_packet(p)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nwrite a single packet to the pcap file.", "response": "def _write_packet(self, packet, sec=None, usec=None, caplen=None,\n wirelen=None):\n \"\"\"\n Writes a single packet to the pcap file.\n\n :param packet: Packet, or bytes for a single packet\n :type packet: Packet or bytes\n :param sec: time the packet was captured, in seconds since epoch. If\n not supplied, defaults to now.\n :type sec: int or long\n :param usec: If ``nano=True``, then number of nanoseconds after the\n second that the packet was captured. If ``nano=False``,\n then the number of microseconds after the second the\n packet was captured. If ``sec`` is not specified,\n this value is ignored.\n :type usec: int or long\n :param caplen: The length of the packet in the capture file. If not\n specified, uses ``len(raw(packet))``.\n :type caplen: int\n :param wirelen: The length of the packet on the wire. If not\n specified, tries ``packet.wirelen``, otherwise uses\n ``caplen``.\n :type wirelen: int\n :returns: None\n :rtype: None\n \"\"\"\n if hasattr(packet, \"time\"):\n if sec is None:\n sec = int(packet.time)\n usec = int(round((packet.time - sec) *\n (1000000000 if self.nano else 1000000)))\n if usec is None:\n usec = 0\n\n rawpkt = raw(packet)\n caplen = len(rawpkt) if caplen is None else caplen\n\n if wirelen is None:\n if hasattr(packet, \"wirelen\"):\n wirelen = packet.wirelen\n if wirelen is None:\n wirelen = caplen\n\n RawPcapWriter._write_packet(\n self, rawpkt, sec=sec, usec=usec, caplen=caplen, wirelen=wirelen)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nplay VoIP packets with RAW data that are either sniffed from an IP or from a list of packets.", "response": "def voip_play(s1, lst=None, **kargs):\n \"\"\"Play VoIP packets with RAW data that\n are either sniffed either from an IP, or\n specified as a list.\n\n It will play only the incoming packets !\n\n :param s1: The IP of the src of all VoIP packets.\n :param lst: (optional) A list of packets to load\n :type s1: string\n :type lst: list\n\n :Example:\n\n >>> voip_play(\"64.2.142.189\")\n while calling '411@ideasip.com'\n\n >>> voip_play(\"64.2.142.189\", lst)\n with list a list of packets with VoIP data\n in their RAW layer\n\n .. seealso:: voip_play2\n to play both the outcoming and incoming packets\n at the same time.\n\n .. seealso:: voip_play3\n to read RTP VoIP packets\n \"\"\"\n\n dsp, rd = os.popen2(sox_base % \"\")\n\n def play(pkt):\n if not pkt:\n return\n if not pkt.haslayer(UDP) or not pkt.haslayer(IP):\n return\n ip = pkt.getlayer(IP)\n if s1 == ip.src:\n dsp.write(pkt.getlayer(conf.raw_layer).load[12:])\n try:\n if lst is None:\n sniff(store=0, prn=play, **kargs)\n else:\n for p in lst:\n play(p)\n finally:\n dsp.close()\n rd.close()"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef phantom_mode(pkt):\n if not pkt.tls_session:\n return False\n if not pkt.tls_session.tls_version:\n return False\n return pkt.tls_session.tls_version < 0x0303", "response": "Return True if we are in phantom mode."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef addfield(self, pkt, s, val):\n v = pkt.tls_session.tls_version\n if v and v < 0x0300:\n return s\n return super(SigLenField, self).addfield(pkt, s, val)", "response": "Add a sig_len field."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nupdating our own sig_val.", "response": "def _update_sig(self, m, key):\n \"\"\"\n Sign 'm' with the PrivKey 'key' and update our own 'sig_val'.\n Note that, even when 'sig_alg' is not None, we use the signature scheme\n of the PrivKey (neither do we care to compare the both of them).\n \"\"\"\n if self.sig_alg is None:\n if self.tls_session.tls_version >= 0x0300:\n self.sig_val = key.sign(m, t='pkcs', h='md5-sha1')\n else:\n self.sig_val = key.sign(m, t='pkcs', h='md5')\n else:\n h, sig = _tls_hash_sig[self.sig_alg].split('+')\n if sig.endswith('pss'):\n t = \"pss\"\n else:\n t = \"pkcs\"\n self.sig_val = key.sign(m, t=t, h=h)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _verify_sig(self, m, cert):\n if self.sig_val:\n if self.sig_alg:\n h, sig = _tls_hash_sig[self.sig_alg].split('+')\n if sig.endswith('pss'):\n t = \"pss\"\n else:\n t = \"pkcs\"\n return cert.verify(m, self.sig_val, t=t, h=h)\n else:\n if self.tls_session.tls_version >= 0x0300:\n return cert.verify(m, self.sig_val, t='pkcs', h='md5-sha1')\n else:\n return cert.verify(m, self.sig_val, t='pkcs', h='md5')\n return False", "response": "Verify that the signature of m is valid for the Cert cert."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef register_pubkey(self):\n p = pkcs_os2ip(self.dh_p)\n g = pkcs_os2ip(self.dh_g)\n pn = dh.DHParameterNumbers(p, g)\n\n y = pkcs_os2ip(self.dh_Ys)\n public_numbers = dh.DHPublicNumbers(y, pn)\n\n s = self.tls_session\n s.server_kx_pubkey = public_numbers.public_key(default_backend())\n\n if not s.client_kx_ffdh_params:\n s.client_kx_ffdh_params = pn.parameters(default_backend())", "response": "XXX Check that the pubkey received is in the group."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef fill_missing(self):\n s = self.tls_session\n\n if self.curve_type is None:\n self.curve_type = _tls_ec_curve_types[\"named_curve\"]\n\n if self.named_curve is None:\n curve = ec.SECP256R1()\n s.server_kx_privkey = ec.generate_private_key(curve,\n default_backend())\n self.named_curve = next((cid for cid, name in six.iteritems(_tls_named_curves) # noqa: E501\n if name == curve.name), 0)\n else:\n curve_name = _tls_named_curves.get(self.named_curve)\n if curve_name is None:\n # this fallback is arguable\n curve = ec.SECP256R1()\n else:\n curve_cls = ec._CURVE_TYPES.get(curve_name)\n if curve_cls is None:\n # this fallback is arguable\n curve = ec.SECP256R1()\n else:\n curve = curve_cls()\n s.server_kx_privkey = ec.generate_private_key(curve,\n default_backend())\n\n if self.point is None:\n pubkey = s.server_kx_privkey.public_key()\n try:\n # cryptography >= 2.5\n self.point = pubkey.public_bytes(\n serialization.Encoding.X962,\n serialization.PublicFormat.UncompressedPoint\n )\n except TypeError:\n # older versions\n self.key_exchange = pubkey.public_numbers().encode_point()\n # else, we assume that the user wrote the server_kx_privkey by himself\n if self.pointlen is None:\n self.pointlen = len(self.point)\n\n if not s.client_kx_ecdh_params:\n s.client_kx_ecdh_params = curve", "response": "This method is called by the server key exchange module to fill missing values."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nregisters the public key in the server.", "response": "def register_pubkey(self):\n \"\"\"\n XXX Support compressed point format.\n XXX Check that the pubkey received is on the curve.\n \"\"\"\n # point_format = 0\n # if self.point[0] in [b'\\x02', b'\\x03']:\n # point_format = 1\n\n curve_name = _tls_named_curves[self.named_curve]\n curve = ec._CURVE_TYPES[curve_name]()\n import_point = ec.EllipticCurvePublicNumbers.from_encoded_point\n pubnum = import_point(curve, self.point)\n s = self.tls_session\n s.server_kx_pubkey = pubnum.public_key(default_backend())\n\n if not s.client_kx_ecdh_params:\n s.client_kx_ecdh_params = curve"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef post_dissection(self, m):\n s = self.tls_session\n\n # if there are kx params and keys, we assume the crypto library is ok\n if s.client_kx_ffdh_params:\n y = pkcs_os2ip(self.dh_Yc)\n param_numbers = s.client_kx_ffdh_params.parameter_numbers()\n public_numbers = dh.DHPublicNumbers(y, param_numbers)\n s.client_kx_pubkey = public_numbers.public_key(default_backend())\n\n if s.server_kx_privkey and s.client_kx_pubkey:\n ZZ = s.server_kx_privkey.exchange(s.client_kx_pubkey)\n s.pre_master_secret = ZZ\n s.compute_ms_and_derive_keys()", "response": "This method is called when the user has done some additional dissection of the certificate."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef post_build(self, pkt, pay):\n enc = pkt\n\n s = self.tls_session\n s.pre_master_secret = enc\n s.compute_ms_and_derive_keys()\n\n if s.server_tmp_rsa_key is not None:\n enc = s.server_tmp_rsa_key.encrypt(pkt, t=\"pkcs\")\n elif s.server_certs is not None and len(s.server_certs) > 0:\n enc = s.server_certs[0].encrypt(pkt, t=\"pkcs\")\n else:\n warning(\"No material to encrypt Pre Master Secret\")\n\n tmp_len = b\"\"\n if s.tls_version >= 0x0301:\n tmp_len = struct.pack(\"!H\", len(enc))\n return tmp_len + enc + pay", "response": "This method is called after the message is sent to the server."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nconverting a Python 2 function as lambda (x,y): x + y In the Python 3 format: lambda x,y : x + y", "response": "def lambda_tuple_converter(func):\n \"\"\"\n Converts a Python 2 function as\n lambda (x,y): x + y\n In the Python 3 format:\n lambda x,y : x + y\n \"\"\"\n if func is not None and func.__code__.co_argcount == 1:\n return lambda *args: func(args[0] if len(args) == 1 else args)\n else:\n return func"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef base64_bytes(x):\n if six.PY2:\n return base64.decodestring(x)\n return base64.decodebytes(bytes_encode(x))", "response": "Turn base64 into bytes"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef bytes_base64(x):\n if six.PY2:\n return base64.encodestring(x).replace('\\n', '')\n return base64.encodebytes(bytes_encode(x)).replace(b'\\n', b'')", "response": "Turn bytes into base64"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef m2i(self, pkt, m):\n cls = Raw\n if pkt.type == 22:\n if len(m) >= 1:\n msgtype = orb(m[0])\n cls = _tls_handshake_cls.get(msgtype, Raw)\n elif pkt.type == 20:\n cls = TLSChangeCipherSpec\n elif pkt.type == 21:\n cls = TLSAlert\n elif pkt.type == 23:\n cls = TLSApplicationData\n\n if cls is Raw:\n return Raw(m)\n else:\n try:\n return cls(m, tls_session=pkt.tls_session)\n except Exception:\n if conf.debug_dissector:\n raise\n return Raw(m)", "response": "Parses a message into an internal object."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef getfield(self, pkt, s):\n tmp_len = self.length_from(pkt)\n lst = []\n ret = b\"\"\n remain = s\n if tmp_len is not None:\n remain, ret = s[:tmp_len], s[tmp_len:]\n\n if remain == b\"\":\n if (((pkt.tls_session.tls_version or 0x0303) > 0x0200) and\n hasattr(pkt, \"type\") and pkt.type == 23):\n return ret, [TLSApplicationData(data=b\"\")]\n else:\n return ret, [Raw(load=b\"\")]\n\n if False in six.itervalues(pkt.tls_session.rcs.cipher.ready):\n return ret, _TLSEncryptedContent(remain)\n else:\n while remain:\n raw_msg = remain\n p = self.m2i(pkt, remain)\n if Padding in p:\n pad = p[Padding]\n remain = pad.load\n del(pad.underlayer.payload)\n if len(remain) != 0:\n raw_msg = raw_msg[:-len(remain)]\n else:\n remain = b\"\"\n\n if isinstance(p, _GenericTLSSessionInheritance):\n if not p.tls_session.frozen:\n p.post_dissection_tls_session_update(raw_msg)\n\n lst.append(p)\n return remain + ret, lst", "response": "This method is used to get the message header fields from the message header."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nupdates the context with information from the built packet.", "response": "def i2m(self, pkt, p):\n \"\"\"\n Update the context with information from the built packet.\n If no type was given at the record layer, we try to infer it.\n \"\"\"\n cur = b\"\"\n if isinstance(p, _GenericTLSSessionInheritance):\n if pkt.type is None:\n if isinstance(p, TLSChangeCipherSpec):\n pkt.type = 20\n elif isinstance(p, TLSAlert):\n pkt.type = 21\n elif isinstance(p, _TLSHandshake):\n pkt.type = 22\n elif isinstance(p, TLSApplicationData):\n pkt.type = 23\n p.tls_session = pkt.tls_session\n if not pkt.tls_session.frozen:\n cur = p.raw_stateful()\n p.post_build_tls_session_update(cur)\n else:\n cur = raw(p)\n else:\n pkt.type = 23\n cur = raw(p)\n return cur"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef addfield(self, pkt, s, val):\n res = b\"\"\n for p in val:\n res += self.i2m(pkt, p)\n if (isinstance(pkt, _GenericTLSSessionInheritance) and\n _tls_version_check(pkt.tls_session.tls_version, 0x0304) and\n not isinstance(pkt, TLS13ServerHello)):\n return s + res\n if not pkt.type:\n pkt.type = 0\n hdr = struct.pack(\"!B\", pkt.type) + s[1:5]\n return hdr + res", "response": "Add the fields to the header."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nverifies the HMAC of the record header and the message.", "response": "def _tls_hmac_verify(self, hdr, msg, mac):\n \"\"\"\n Provided with the record header, the TLSCompressed.fragment and the\n HMAC, return True if the HMAC is correct. If we could not compute the\n HMAC because the key was missing, there is no sense in verifying\n anything, thus we also return True.\n\n Meant to be used with a block cipher or a stream cipher.\n It would fail with an AEAD cipher, because rcs.hmac would be None.\n See RFC 5246, section 6.2.3.\n \"\"\"\n read_seq_num = struct.pack(\"!Q\", self.tls_session.rcs.seq_num)\n self.tls_session.rcs.seq_num += 1\n\n mac_len = self.tls_session.rcs.mac_len\n if mac_len == 0: # should be TLS_NULL_WITH_NULL_NULL\n return True\n if len(mac) != mac_len:\n return False\n\n alg = self.tls_session.rcs.hmac\n version = struct.unpack(\"!H\", hdr[1:3])[0]\n try:\n if version > 0x300:\n h = alg.digest(read_seq_num + hdr + msg)\n elif version == 0x300:\n h = alg.digest_sslv3(read_seq_num + hdr[:1] + hdr[3:5] + msg)\n else:\n raise Exception(\"Unrecognized version.\")\n except HMACError:\n h = mac\n return h == mac"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ndecompresses the given string.", "response": "def _tls_decompress(self, s):\n \"\"\"\n Provided with the TLSCompressed.fragment,\n return the TLSPlaintext.fragment.\n \"\"\"\n alg = self.tls_session.rcs.compression\n return alg.decompress(s)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef pre_dissect(self, s):\n if len(s) < 5:\n raise Exception(\"Invalid record: header is too short.\")\n\n msglen = struct.unpack('!H', s[3:5])[0]\n hdr, efrag, r = s[:5], s[5:5 + msglen], s[msglen + 5:]\n\n iv = mac = pad = b\"\"\n self.padlen = None\n decryption_success = False\n\n cipher_type = self.tls_session.rcs.cipher.type\n\n if cipher_type == 'block':\n version = struct.unpack(\"!H\", s[1:3])[0]\n\n # Decrypt\n try:\n if version >= 0x0302:\n # Explicit IV for TLS 1.1 and 1.2\n block_size = self.tls_session.rcs.cipher.block_size\n iv, efrag = efrag[:block_size], efrag[block_size:]\n self.tls_session.rcs.cipher.iv = iv\n pfrag = self._tls_decrypt(efrag)\n else:\n # Implicit IV for SSLv3 and TLS 1.0\n pfrag = self._tls_decrypt(efrag)\n except CipherError as e:\n # This will end up dissected as _TLSEncryptedContent.\n cfrag = e.args[0]\n else:\n decryption_success = True\n # Excerpt below better corresponds to TLS 1.1 IV definition,\n # but the result is the same as with TLS 1.2 anyway.\n # This leading *IV* has been decrypted by _tls_decrypt with a\n # random IV, hence it does not correspond to anything.\n # What actually matters is that we got the first encrypted block # noqa: E501\n # in order to decrypt the second block (first data block).\n # if version >= 0x0302:\n # block_size = self.tls_session.rcs.cipher.block_size\n # iv, pfrag = pfrag[:block_size], pfrag[block_size:]\n # l = struct.unpack('!H', hdr[3:5])[0]\n # hdr = hdr[:3] + struct.pack('!H', l-block_size)\n\n # Extract padding ('pad' actually includes the trailing padlen)\n padlen = orb(pfrag[-1]) + 1\n mfrag, pad = pfrag[:-padlen], pfrag[-padlen:]\n self.padlen = padlen\n\n # Extract MAC\n tmp_len = self.tls_session.rcs.mac_len\n if tmp_len != 0:\n cfrag, mac = mfrag[:-tmp_len], mfrag[-tmp_len:]\n else:\n cfrag, mac = mfrag, b\"\"\n\n # Verify integrity\n chdr = hdr[:3] + struct.pack('!H', len(cfrag))\n is_mac_ok = self._tls_hmac_verify(chdr, cfrag, mac)\n if not is_mac_ok:\n pkt_info = self.firstlayer().summary()\n log_runtime.info(\"TLS: record integrity check failed [%s]\", pkt_info) # noqa: E501\n\n elif cipher_type == 'stream':\n # Decrypt\n try:\n pfrag = self._tls_decrypt(efrag)\n except CipherError as e:\n # This will end up dissected as _TLSEncryptedContent.\n cfrag = e.args[0]\n else:\n decryption_success = True\n mfrag = pfrag\n\n # Extract MAC\n tmp_len = self.tls_session.rcs.mac_len\n if tmp_len != 0:\n cfrag, mac = mfrag[:-tmp_len], mfrag[-tmp_len:]\n else:\n cfrag, mac = mfrag, b\"\"\n\n # Verify integrity\n chdr = hdr[:3] + struct.pack('!H', len(cfrag))\n is_mac_ok = self._tls_hmac_verify(chdr, cfrag, mac)\n if not is_mac_ok:\n pkt_info = self.firstlayer().summary()\n log_runtime.info(\"TLS: record integrity check failed [%s]\", pkt_info) # noqa: E501\n\n elif cipher_type == 'aead':\n # Authenticated encryption\n # crypto/cipher_aead.py prints a warning for integrity failure\n if (conf.crypto_valid_advanced and\n isinstance(self.tls_session.rcs.cipher, Cipher_CHACHA20_POLY1305)): # noqa: E501\n iv = b\"\"\n cfrag, mac = self._tls_auth_decrypt(hdr, efrag)\n else:\n iv, cfrag, mac = self._tls_auth_decrypt(hdr, efrag)\n decryption_success = True # see XXX above\n\n frag = self._tls_decompress(cfrag)\n\n if (decryption_success and\n not isinstance(self.tls_session.rcs.cipher, Cipher_NULL)):\n self.deciphered_len = len(frag)\n else:\n self.deciphered_len = None\n\n reconstructed_body = iv + frag + mac + pad\n\n return hdr + reconstructed_body + r", "response": "Decrypt verify and decompress the message s and return the _TLSEncryptedContent."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncommits the pending r/w state if it has been triggered.", "response": "def post_dissect(self, s):\n \"\"\"\n Commit the pending r/w state if it has been triggered (e.g. by an\n underlying TLSChangeCipherSpec or a SSLv2ClientMasterKey). We update\n nothing if the prcs was not set, as this probably means that we're\n working out-of-context (and we need to keep the default rcs).\n \"\"\"\n if self.tls_session.triggered_prcs_commit:\n if self.tls_session.prcs is not None:\n self.tls_session.rcs = self.tls_session.prcs\n self.tls_session.prcs = None\n self.tls_session.triggered_prcs_commit = False\n if self.tls_session.triggered_pwcs_commit:\n if self.tls_session.pwcs is not None:\n self.tls_session.wcs = self.tls_session.pwcs\n self.tls_session.pwcs = None\n self.tls_session.triggered_pwcs_commit = False\n return s"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncompress the given string.", "response": "def _tls_compress(self, s):\n \"\"\"\n Provided with the TLSPlaintext.fragment,\n return the TLSCompressed.fragment.\n \"\"\"\n alg = self.tls_session.wcs.compression\n return alg.compress(s)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _tls_auth_encrypt(self, s):\n write_seq_num = struct.pack(\"!Q\", self.tls_session.wcs.seq_num)\n self.tls_session.wcs.seq_num += 1\n add_data = (write_seq_num +\n pkcs_i2osp(self.type, 1) +\n pkcs_i2osp(self.version, 2) +\n pkcs_i2osp(len(s), 2))\n return self.tls_session.wcs.cipher.auth_encrypt(s, add_data,\n write_seq_num)", "response": "This method is used to encrypt the data with the AEAD cipher."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd the message to the HMAC.", "response": "def _tls_hmac_add(self, hdr, msg):\n \"\"\"\n Provided with the record header (concatenation of the TLSCompressed\n type, version and length fields) and the TLSCompressed.fragment,\n return the concatenation of the TLSCompressed.fragment and the HMAC.\n\n Meant to be used with a block cipher or a stream cipher.\n It would fail with an AEAD cipher, because wcs.hmac would be None.\n See RFC 5246, section 6.2.3.\n \"\"\"\n write_seq_num = struct.pack(\"!Q\", self.tls_session.wcs.seq_num)\n self.tls_session.wcs.seq_num += 1\n\n alg = self.tls_session.wcs.hmac\n version = struct.unpack(\"!H\", hdr[1:3])[0]\n if version > 0x300:\n h = alg.digest(write_seq_num + hdr + msg)\n elif version == 0x300:\n h = alg.digest_sslv3(write_seq_num + hdr[:1] + hdr[3:5] + msg)\n else:\n raise Exception(\"Unrecognized version.\")\n return msg + h"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _tls_pad(self, s):\n padding = b\"\"\n block_size = self.tls_session.wcs.cipher.block_size\n padlen = block_size - ((len(s) + 1) % block_size)\n if padlen == block_size:\n padlen = 0\n pad_pattern = chb(padlen)\n padding = pad_pattern * (padlen + 1)\n return s + padding", "response": "Pads the given string with the right padding."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef post_build(self, pkt, pay):\n # Compute the length of TLSPlaintext fragment\n hdr, frag = pkt[:5], pkt[5:]\n tmp_len = len(frag)\n hdr = hdr[:3] + struct.pack(\"!H\", tmp_len)\n\n # Compression\n cfrag = self._tls_compress(frag)\n tmp_len = len(cfrag) # Update the length as a result of compression\n hdr = hdr[:3] + struct.pack(\"!H\", tmp_len)\n\n cipher_type = self.tls_session.wcs.cipher.type\n\n if cipher_type == 'block':\n # Integrity\n mfrag = self._tls_hmac_add(hdr, cfrag)\n\n # Excerpt below better corresponds to TLS 1.1 IV definition,\n # but the result is the same as with TLS 1.2 anyway.\n # if self.version >= 0x0302:\n # l = self.tls_session.wcs.cipher.block_size\n # iv = randstring(l)\n # mfrag = iv + mfrag\n\n # Add padding\n pfrag = self._tls_pad(mfrag)\n\n # Encryption\n if self.version >= 0x0302:\n # Explicit IV for TLS 1.1 and 1.2\n tmp_len = self.tls_session.wcs.cipher.block_size\n iv = randstring(tmp_len)\n self.tls_session.wcs.cipher.iv = iv\n efrag = self._tls_encrypt(pfrag)\n efrag = iv + efrag\n else:\n # Implicit IV for SSLv3 and TLS 1.0\n efrag = self._tls_encrypt(pfrag)\n\n elif cipher_type == \"stream\":\n # Integrity\n mfrag = self._tls_hmac_add(hdr, cfrag)\n # Encryption\n efrag = self._tls_encrypt(mfrag)\n\n elif cipher_type == \"aead\":\n # Authenticated encryption (with nonce_explicit as header)\n efrag = self._tls_auth_encrypt(cfrag)\n\n if self.len is not None:\n # The user gave us a 'len', let's respect this ultimately\n hdr = hdr[:3] + struct.pack(\"!H\", self.len)\n else:\n # Update header with the length of TLSCiphertext.fragment\n hdr = hdr[:3] + struct.pack(\"!H\", len(efrag))\n\n # Now we commit the pending write state if it has been triggered (e.g.\n # by an underlying TLSChangeCipherSpec or a SSLv2ClientMasterKey). We\n # update nothing if the pwcs was not set. This probably means that\n # we're working out-of-context (and we need to keep the default wcs).\n if self.tls_session.triggered_pwcs_commit:\n if self.tls_session.pwcs is not None:\n self.tls_session.wcs = self.tls_session.pwcs\n self.tls_session.pwcs = None\n self.tls_session.triggered_pwcs_commit = False\n\n return hdr + efrag + pay", "response": "Apply the previous methods according to the writing cipher type."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef open(self):\n if not self.closed:\n return\n self.outs = self.ins = open(\n \"/dev/net/tun\" if LINUX else (\"/dev/%s\" % self.iface), \"r+b\",\n buffering=0\n )\n if LINUX:\n from fcntl import ioctl\n # TUNSETIFF = 0x400454ca\n # IFF_TUN = 0x0001\n # IFF_TAP = 0x0002\n # IFF_NO_PI = 0x1000\n ioctl(self.ins, 0x400454ca, struct.pack(\n \"16sH\", bytes_encode(self.iface),\n 0x0001 if self.mode_tun else 0x1002,\n ))\n self.closed = False", "response": "Open the TUN or TAP device."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef util_mic_len(pkt):\n ''' Calculate the length of the attribute value field '''\n if (pkt.nwk_seclevel == 0): # no encryption, no mic\n return 0\n elif (pkt.nwk_seclevel == 1): # MIC-32\n return 4\n elif (pkt.nwk_seclevel == 2): # MIC-64\n return 8\n elif (pkt.nwk_seclevel == 3): # MIC-128\n return 16\n elif (pkt.nwk_seclevel == 4): # ENC\n return 0\n elif (pkt.nwk_seclevel == 5): # ENC-MIC-32\n return 4\n elif (pkt.nwk_seclevel == 6): # ENC-MIC-64\n return 8\n elif (pkt.nwk_seclevel == 7): # ENC-MIC-128\n return 16\n else:\n return 0", "response": "Calculate the length of the attribute value field in the MIC - 32 or MIC - 64 representation."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nbuild a list of EIR messages to wrap this frame.", "response": "def build_eir(self):\n \"\"\"Builds a list of EIR messages to wrap this frame.\"\"\"\n\n return LowEnergyBeaconHelper.base_eir + [\n EIR_Hdr() / EIR_CompleteList16BitServiceUUIDs(svc_uuids=[\n EDDYSTONE_UUID]),\n EIR_Hdr() / EIR_ServiceData16BitUUID() / self\n ]"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating an Eddystone_Frame with a Eddystone_URL for a given URL.", "response": "def from_url(url):\n \"\"\"Creates an Eddystone_Frame with a Eddystone_URL for a given URL.\"\"\"\n url = url.encode('ascii')\n scheme = None\n for k, v in EDDYSTONE_URL_SCHEMES.items():\n if url.startswith(v):\n scheme = k\n url = url[len(v):]\n break\n else:\n raise Exception(\"URLs must start with EDDYSTONE_URL_SCHEMES\")\n\n return Eddystone_Frame() / Eddystone_URL(\n url_scheme=scheme,\n url=url)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef add(self, *args, **kargs):\n self.invalidate_cache()\n self.routes.append(self.make_route(*args, **kargs))", "response": "Add a new entry to the cache."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nremove all route entries that uses iff interface.", "response": "def ifdel(self, iff):\n \"\"\" removes all route entries that uses 'iff' interface. \"\"\"\n new_routes = []\n for rt in self.routes:\n if rt[3] != iff:\n new_routes.append(rt)\n self.invalidate_cache()\n self.routes = new_routes"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef ifadd(self, iff, addr):\n addr, plen = (addr.split(\"/\") + [\"128\"])[:2]\n addr = in6_ptop(addr)\n plen = int(plen)\n naddr = inet_pton(socket.AF_INET6, addr)\n nmask = in6_cidr2mask(plen)\n prefix = inet_ntop(socket.AF_INET6, in6_and(nmask, naddr))\n self.invalidate_cache()\n self.routes.append((prefix, plen, '::', iff, [addr], 1))", "response": "Add an interface iff with provided address into routing table."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a dict of cache entries for a given destination address.", "response": "def route(self, dst=None, dev=None, verbose=conf.verb):\n \"\"\"\n Provide best route to IPv6 destination address, based on Scapy\n internal routing table content.\n\n When a set of address is passed (e.g. 2001:db8:cafe:*::1-5) an address\n of the set is used. Be aware of that behavior when using wildcards in\n upper parts of addresses !\n\n If 'dst' parameter is a FQDN, name resolution is performed and result\n is used.\n\n if optional 'dev' parameter is provided a specific interface, filtering\n is performed to limit search to route associated to that interface.\n \"\"\"\n dst = dst or \"::/0\" # Enable route(None) to return default route\n # Transform \"2001:db8:cafe:*::1-5:0/120\" to one IPv6 address of the set\n dst = dst.split(\"/\")[0]\n savedst = dst # In case following inet_pton() fails\n dst = dst.replace(\"*\", \"0\")\n idx = dst.find(\"-\")\n while idx >= 0:\n m = (dst[idx:] + \":\").find(\":\")\n dst = dst[:idx] + dst[idx + m:]\n idx = dst.find(\"-\")\n\n try:\n inet_pton(socket.AF_INET6, dst)\n except socket.error:\n dst = socket.getaddrinfo(savedst, None, socket.AF_INET6)[0][-1][0]\n # TODO : Check if name resolution went well\n\n # Use the default interface while dealing with link-local addresses\n if dev is None and (in6_islladdr(dst) or in6_ismlladdr(dst)):\n dev = conf.iface\n\n # Deal with dev-specific request for cache search\n k = dst\n if dev is not None:\n k = dst + \"%%\" + (dev if isinstance(dev, six.string_types) else dev.pcap_name) # noqa: E501\n if k in self.cache:\n return self.cache[k]\n\n paths = []\n\n # TODO : review all kinds of addresses (scope and *cast) to see\n # if we are able to cope with everything possible. I'm convinced\n # it's not the case.\n # -- arnaud\n for p, plen, gw, iface, cset, me in self.routes:\n if dev is not None and iface != dev:\n continue\n if in6_isincluded(dst, p, plen):\n paths.append((plen, me, (iface, cset, gw)))\n elif (in6_ismlladdr(dst) and in6_islladdr(p) and in6_islladdr(cset[0])): # noqa: E501\n paths.append((plen, me, (iface, cset, gw)))\n\n if not paths:\n if verbose:\n warning(\"No route found for IPv6 destination %s \"\n \"(no default route?)\", dst)\n return (scapy.consts.LOOPBACK_INTERFACE, \"::\", \"::\")\n\n # Sort with longest prefix first then use metrics as a tie-breaker\n paths.sort(key=lambda x: (-x[0], x[1]))\n\n best_plen = (paths[0][0], paths[0][1])\n paths = [x for x in paths if (x[0], x[1]) == best_plen]\n\n res = []\n for p in paths: # Here we select best source address for every route\n tmp = p[2]\n srcaddr = get_source_addr_from_candidate_set(dst, tmp[1])\n if srcaddr is not None:\n res.append((p[0], p[1], (tmp[0], srcaddr, tmp[2])))\n\n if res == []:\n warning(\"Found a route for IPv6 destination '%s', but no possible source address.\", dst) # noqa: E501\n return (scapy.consts.LOOPBACK_INTERFACE, \"::\", \"::\")\n\n # Symptom : 2 routes with same weight (our weight is plen)\n # Solution :\n # - dst is unicast global. Check if it is 6to4 and we have a source\n # 6to4 address in those available\n # - dst is link local (unicast or multicast) and multiple output\n # interfaces are available. Take main one (conf.iface6)\n # - if none of the previous or ambiguity persists, be lazy and keep\n # first one\n\n if len(res) > 1:\n tmp = []\n if in6_isgladdr(dst) and in6_isaddr6to4(dst):\n # TODO : see if taking the longest match between dst and\n # every source addresses would provide better results\n tmp = [x for x in res if in6_isaddr6to4(x[2][1])]\n elif in6_ismaddr(dst) or in6_islladdr(dst):\n # TODO : I'm sure we are not covering all addresses. Check that\n tmp = [x for x in res if x[2][0] == conf.iface6]\n\n if tmp:\n res = tmp\n\n # Fill the cache (including dev-specific request)\n k = dst\n if dev is not None:\n k = dst + \"%%\" + (dev if isinstance(dev, six.string_types) else dev.pcap_name) # noqa: E501\n self.cache[k] = res[0][2]\n\n return res[0][2]"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncompresses a TLSPlaintext instance using a compression algorithm instance alg and return a TLSCompressed instance.", "response": "def _tls_compress(alg, p):\n \"\"\"\n Compress p (a TLSPlaintext instance) using compression algorithm instance\n alg and return a TLSCompressed instance.\n \"\"\"\n c = TLSCompressed()\n c.type = p.type\n c.version = p.version\n c.data = alg.compress(p.data)\n c.len = len(c.data)\n return c"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ndecompressing a TLSCompressed instance using the compression algorithm and return a TLSPlaintext instance.", "response": "def _tls_decompress(alg, c):\n \"\"\"\n Decompress c (a TLSCompressed instance) using compression algorithm\n instance alg and return a TLSPlaintext instance.\n \"\"\"\n p = TLSPlaintext()\n p.type = c.type\n p.version = c.version\n p.data = alg.decompress(c.data)\n p.len = len(p.data)\n return p"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _tls_mac_add(alg, c, write_seq_num):\n write_seq_num = struct.pack(\"!Q\", write_seq_num)\n h = alg.digest(write_seq_num + bytes(c))\n c.data += h\n c.len += alg.hash_len", "response": "Compute the MAC using the provided MAC alg instance over TLSCiphertext c using\n current write sequence number write_seq_num."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _tls_mac_verify(alg, p, read_seq_num):\n h_size = alg.hash_len\n if p.len < h_size:\n return False\n received_h = p.data[-h_size:]\n p.len -= h_size\n p.data = p.data[:-h_size]\n\n read_seq_num = struct.pack(\"!Q\", read_seq_num)\n h = alg.digest(read_seq_num + bytes(p))\n return h == received_h", "response": "Verify that the MAC in the provided message is valid."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nprovides with cipher block size parameter and current TLSCompressed packet p (after MAC addition), the function adds required, deterministic padding to p.data before encryption step, as it is defined for TLS (i.e. not SSL and its allowed random padding). The function has no return value.", "response": "def _tls_add_pad(p, block_size):\n \"\"\"\n Provided with cipher block size parameter and current TLSCompressed packet\n p (after MAC addition), the function adds required, deterministic padding\n to p.data before encryption step, as it is defined for TLS (i.e. not\n SSL and its allowed random padding). The function has no return value.\n \"\"\"\n padlen = -p.len % block_size\n padding = chb(padlen) * (padlen + 1)\n p.len += len(padding)\n p.data += padding"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _tls_del_pad(p):\n\n if p.len < 1:\n warning(\"Message format is invalid (padding)\")\n return False\n\n padlen = orb(p.data[-1])\n padsize = padlen + 1\n\n if p.len < padsize:\n warning(\"Invalid padding length\")\n return False\n\n if p.data[-padsize:] != chb(padlen) * padsize:\n warning(\"Padding content is invalid %s\", repr(p.data[-padsize:]))\n return False\n\n p.data = p.data[:-padsize]\n p.len -= padsize\n\n return True", "response": "This function is used to remove the trailing padding found in p. data."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _tls_encrypt(alg, p):\n c = TLSCiphertext()\n c.type = p.type\n c.version = p.version\n c.data = alg.encrypt(p.data)\n c.len = len(c.data)\n return c", "response": "This function encrypts a TLSCompressed packet and returns a TLSCiphertext object."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nprovide with a TLSCiphertext instance c, and a stream or block cipher alg, the function decrypts c.data and returns a newly created TLSPlaintext.", "response": "def _tls_decrypt(alg, c):\n \"\"\"\n Provided with a TLSCiphertext instance c, and a stream or block cipher alg,\n the function decrypts c.data and returns a newly created TLSPlaintext.\n \"\"\"\n p = TLSPlaintext()\n p.type = c.type\n p.version = c.version\n p.data = alg.decrypt(c.data)\n p.len = len(p.data)\n return p"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _tls_aead_auth_encrypt(alg, p, write_seq_num):\n P = bytes(p)\n write_seq_num = struct.pack(\"!Q\", write_seq_num)\n A = write_seq_num + P[:5]\n\n c = TLSCiphertext()\n c.type = p.type\n c.version = p.version\n c.data = alg.auth_encrypt(P, A, write_seq_num)\n c.len = len(c.data)\n return c", "response": "This function applies an AEAD cipher to p. data and returns a TLSCiphertext instance with the encrypted data."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _extcap_call(prog, args, keyword, values):\n p = subprocess.Popen(\n [prog] + args,\n stdout=subprocess.PIPE, stderr=subprocess.PIPE\n )\n data, err = p.communicate()\n if p.returncode != 0:\n raise OSError(\"%s returned with error code %s: %s\" % (prog,\n p.returncode,\n err))\n data = plain_str(data)\n res = []\n for ifa in data.split(\"\\n\"):\n ifa = ifa.strip()\n if not ifa.startswith(keyword):\n continue\n res.append(tuple([re.search(r\"{%s=([^}]*)}\" % val, ifa).group(1)\n for val in values]))\n return res", "response": "Function used to call a program using the extcap format and parse the results"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nbuilds a list of _HCSINullField with numbered \"Reserved\" names.", "response": "def _hcsi_null_range(*args, **kwargs):\n \"\"\"Builds a list of _HCSINullField with numbered \"Reserved\" names.\n\n Takes the same arguments as the ``range`` built-in.\n\n :returns: list[HCSINullField]\n \"\"\"\n return [\n HCSINullField('Reserved{:02d}'.format(x))\n for x in range(*args, **kwargs)\n ]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nconvert internal value to machine value", "response": "def i2m(self, pkt, x):\n \"\"\"Convert internal value to machine value\"\"\"\n if x is None:\n # Try to return zero if undefined\n x = self.h2i(pkt, 0)\n return x"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the main unicast address associated with provided interface in human readable form.", "response": "def get_if_addr6(iff):\n \"\"\"\n Returns the main global unicast address associated with provided\n interface, in human readable form. If no global address is found,\n None is returned.\n \"\"\"\n return next((x[0] for x in in6_getifaddr()\n if x[2] == iff and x[1] == IPV6_ADDR_GLOBAL), None)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the main unicast address associated with provided interface in network format.", "response": "def get_if_raw_addr6(iff):\n \"\"\"\n Returns the main global unicast address associated with provided\n interface, in network format. If no global address is found, None\n is returned.\n \"\"\"\n ip6 = get_if_addr6(iff)\n if ip6 is not None:\n return inet_pton(socket.AF_INET6, ip6)\n\n return None"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting representation name of a pnio frame ID.", "response": "def i2s_frameid(x):\n \"\"\" Get representation name of a pnio frame ID\n\n :param x: a key of the PNIO_FRAME_IDS dictionary\n :returns: str\n \"\"\"\n try:\n return PNIO_FRAME_IDS[x]\n except KeyError:\n pass\n if 0x0100 <= x < 0x1000:\n return \"RT_CLASS_3 (%4x)\" % x\n if 0x8000 <= x < 0xC000:\n return \"RT_CLASS_1 (%4x)\" % x\n if 0xC000 <= x < 0xFC00:\n return \"RT_CLASS_UDP (%4x)\" % x\n if 0xFF80 <= x < 0xFF90:\n return \"FragmentationFrameID (%4x)\" % x\n return x"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef s2i_frameid(x):\n try:\n return {\n \"RT_CLASS_3\": 0x0100,\n \"RT_CLASS_1\": 0x8000,\n \"RT_CLASS_UDP\": 0xC000,\n \"FragmentationFrameID\": 0xFF80,\n }[x]\n except KeyError:\n pass\n try:\n return next(key for key, value in six.iteritems(PNIO_FRAME_IDS)\n if value == x)\n except StopIteration:\n pass\n return x", "response": "Get pnio frame ID from a representation name"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef dispatch_hook(cls, _pkt=None, *args, **kargs):\n '''\n Attribution of correct type depending on version and pdu_type\n '''\n if _pkt and len(_pkt) >= 2:\n version = orb(_pkt[0])\n pdu_type = orb(_pkt[1])\n if version == 0:\n return PDU_CLASS_VERSION_0[pdu_type]\n elif version == 1:\n return PDU_CLASS_VERSION_1[pdu_type]\n return Raw", "response": "This method is used to dispatch the event from the backend to the appropriate handler."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef parse(self, data):\n\n ASNlist = []\n for line in data.splitlines()[1:]:\n line = plain_str(line)\n if \"|\" not in line:\n continue\n asn, ip, desc = [elt.strip() for elt in line.split('|')]\n if asn == \"NA\":\n continue\n asn = \"AS%s\" % asn\n ASNlist.append((ip, asn, desc))\n return ASNlist", "response": "Parse bulk cymru data"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nencodes the mrcode value to its IGMPv3 encoded time value if needed.", "response": "def encode_maxrespcode(self):\n \"\"\"Encode and replace the mrcode value to its IGMPv3 encoded time value if needed, # noqa: E501\n as specified in rfc3376#section-4.1.1.\n\n If value < 128, return the value specified. If >= 128, encode as a floating # noqa: E501\n point value. Value can be 0 - 31744.\n \"\"\"\n value = self.mrcode\n if value < 128:\n code = value\n elif value > 31743:\n code = 255\n else:\n exp = 0\n value >>= 3\n while(value > 31):\n exp += 1\n value >>= 1\n exp <<= 4\n code = 0x80 | exp | (value & 0x0F)\n self.mrcode = code"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _register_lltd_specific_class(*attr_types):\n def _register(cls):\n for attr_type in attr_types:\n SPECIFIC_CLASSES[attr_type] = cls\n type_fld = LLTDAttribute.fields_desc[0].copy()\n type_fld.default = attr_types[0]\n cls.fields_desc = [type_fld] + cls.fields_desc\n return cls\n return _register", "response": "This is a class decorator that registers LLTDAttributeSpecific with the given list of attr_types."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nupdate the builder using the provided plist. plist can be either a Packet or PacketList.", "response": "def parse(self, plist):\n \"\"\"Update the builder using the provided `plist`. `plist` can\n be either a Packet() or a PacketList().\n\n \"\"\"\n if not isinstance(plist, PacketList):\n plist = PacketList(plist)\n for pkt in plist[LLTD]:\n if LLTDQueryLargeTlv in pkt:\n key = \"%s:%s:%d\" % (pkt.real_dst, pkt.real_src, pkt.seq)\n self.types_offsets[key] = (pkt[LLTDQueryLargeTlv].type,\n pkt[LLTDQueryLargeTlv].offset)\n elif LLTDQueryLargeTlvResp in pkt:\n try:\n key = \"%s:%s:%d\" % (pkt.real_src, pkt.real_dst, pkt.seq)\n content, offset = self.types_offsets[key]\n except KeyError:\n continue\n loc = slice(offset, offset + pkt[LLTDQueryLargeTlvResp].len)\n key = \"%s > %s [%s]\" % (\n pkt.real_src, pkt.real_dst,\n LLTDQueryLargeTlv.fields_desc[0].i2s.get(content, content),\n )\n data = self.data.setdefault(key, array(\"B\"))\n datalen = len(data)\n if datalen < loc.stop:\n data.extend(array(\"B\", b\"\\x00\" * (loc.stop - datalen)))\n data[loc] = array(\"B\", pkt[LLTDQueryLargeTlvResp].value)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a dictionary object containing the content type and the content type of the content.", "response": "def get_data(self):\n \"\"\"Returns a dictionary object, keys are strings \"source >\n destincation [content type]\", and values are the content\n fetched, also as a string.\n\n \"\"\"\n return {key: \"\".join(chr(byte) for byte in data)\n for key, data in six.iteritems(self.data)}"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nconverts an IPv6 address from text representation into binary form.", "response": "def _inet6_pton(addr):\n \"\"\"Convert an IPv6 address from text representation into binary form,\nused when socket.inet_pton is not available.\n\n \"\"\"\n joker_pos = None\n result = b\"\"\n addr = plain_str(addr)\n if addr == '::':\n return b'\\x00' * 16\n if addr.startswith('::'):\n addr = addr[1:]\n if addr.endswith('::'):\n addr = addr[:-1]\n parts = addr.split(\":\")\n nparts = len(parts)\n for i, part in enumerate(parts):\n if not part:\n # \"::\" indicates one or more groups of 2 null bytes\n if joker_pos is None:\n joker_pos = len(result)\n else:\n # Wildcard is only allowed once\n raise _INET6_PTON_EXC\n elif i + 1 == nparts and '.' in part:\n # The last part of an IPv6 address can be an IPv4 address\n if part.count('.') != 3:\n # we have to do this since socket.inet_aton('1.2') ==\n # b'\\x01\\x00\\x00\\x02'\n raise _INET6_PTON_EXC\n try:\n result += socket.inet_aton(part)\n except socket.error:\n raise _INET6_PTON_EXC\n else:\n # Each part must be 16bit. Add missing zeroes before decoding.\n try:\n result += hex_bytes(part.rjust(4, \"0\"))\n except (binascii.Error, TypeError):\n raise _INET6_PTON_EXC\n # If there's a wildcard, fill up with zeros to reach 128bit (16 bytes)\n if joker_pos is not None:\n if len(result) == 16:\n raise _INET6_PTON_EXC\n result = (result[:joker_pos] + b\"\\x00\" * (16 - len(result)) +\n result[joker_pos:])\n if len(result) != 16:\n raise _INET6_PTON_EXC\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nconverts an IP address from text representation into binary form.", "response": "def inet_pton(af, addr):\n \"\"\"Convert an IP address from text representation into binary form.\"\"\"\n # Will replace Net/Net6 objects\n addr = plain_str(addr)\n # Use inet_pton if available\n try:\n return socket.inet_pton(af, addr)\n except AttributeError:\n try:\n return _INET_PTON[af](addr)\n except KeyError:\n raise socket.error(\"Address family not supported by protocol\")"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _inet6_ntop(addr):\n # IPv6 addresses have 128bits (16 bytes)\n if len(addr) != 16:\n raise ValueError(\"invalid length of packed IP address string\")\n\n # Decode to hex representation\n address = \":\".join(plain_str(bytes_hex(addr[idx:idx + 2])).lstrip('0') or '0' # noqa: E501\n for idx in range(0, 16, 2))\n\n try:\n # Get the longest set of zero blocks. We need to take a look\n # at group 1 regarding the length, as 0:0:1:0:0:2:3:4 would\n # have two matches: 0:0: and :0:0: where the latter is longer,\n # though the first one should be taken. Group 1 is in both\n # cases 0:0.\n match = max(_IP6_ZEROS.finditer(address),\n key=lambda m: m.end(1) - m.start(1))\n return '{}::{}'.format(address[:match.start()], address[match.end():])\n except ValueError:\n return address", "response": "Convert an IPv6 address from binary form into text representation."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nconvert an IP address from binary form into text representation.", "response": "def inet_ntop(af, addr):\n \"\"\"Convert an IP address from binary form into text representation.\"\"\"\n # Use inet_ntop if available\n addr = bytes_encode(addr)\n try:\n return socket.inet_ntop(af, addr)\n except AttributeError:\n try:\n return _INET_NTOP[af](addr)\n except KeyError:\n raise ValueError(\"unknown address family %d\" % af)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef recv_raw(self, x=MTU):\n ll = self.ins.datalink()\n if ll in conf.l2types:\n cls = conf.l2types[ll]\n else:\n cls = conf.default_l2\n warning(\"Unable to guess datalink type (interface=%s linktype=%i). Using %s\", # noqa: E501\n self.iface, ll, cls.name)\n\n pkt = None\n while pkt is None:\n pkt = self.ins.next()\n if pkt is not None:\n ts, pkt = pkt\n if pkt is None and scapy.consts.WINDOWS:\n raise TimeoutElapsed # To understand this behavior, have a look at L2pcapListenSocket's note # noqa: E501\n if pkt is None:\n return None, None, None\n return cls, pkt, ts", "response": "Receives a packet then returns a tuple containing cls pkt_data time"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef nonblock_recv(self):\n self.ins.setnonblock(1)\n p = self.recv(MTU)\n self.ins.setnonblock(0)\n return p", "response": "Receives and dissects a packet in non - blocking mode."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef delt(self, *args, **kargs):\n self.invalidate_cache()\n route = self.make_route(*args, **kargs)\n try:\n i = self.routes.index(route)\n del(self.routes[i])\n except ValueError:\n warning(\"no matching route found\")", "response": "delt - Delete a single entry from the cache"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning the IPv4 routes to a destination host.", "response": "def route(self, dst=None, verbose=conf.verb):\n \"\"\"Returns the IPv4 routes to a host.\n parameters:\n - dst: the IPv4 of the destination host\n\n returns: (iface, output_ip, gateway_ip)\n - iface: the interface used to connect to the host\n - output_ip: the outgoing IP that will be used\n - gateway_ip: the gateway IP that will be used\n \"\"\"\n dst = dst or \"0.0.0.0\" # Enable route(None) to return default route\n if isinstance(dst, bytes):\n try:\n dst = plain_str(dst)\n except UnicodeDecodeError:\n raise TypeError(\"Unknown IP address input (bytes)\")\n if dst in self.cache:\n return self.cache[dst]\n # Transform \"192.168.*.1-5\" to one IP of the set\n _dst = dst.split(\"/\")[0].replace(\"*\", \"0\")\n while True:\n idx = _dst.find(\"-\")\n if idx < 0:\n break\n m = (_dst[idx:] + \".\").find(\".\")\n _dst = _dst[:idx] + _dst[idx + m:]\n\n atol_dst = atol(_dst)\n paths = []\n for d, m, gw, i, a, me in self.routes:\n if not a: # some interfaces may not currently be connected\n continue\n aa = atol(a)\n if aa == atol_dst:\n paths.append(\n (0xffffffff, 1, (scapy.consts.LOOPBACK_INTERFACE, a, \"0.0.0.0\")) # noqa: E501\n )\n if (atol_dst & m) == (d & m):\n paths.append((m, me, (i, a, gw)))\n\n if not paths:\n if verbose:\n warning(\"No route found (no default route?)\")\n return scapy.consts.LOOPBACK_INTERFACE, \"0.0.0.0\", \"0.0.0.0\"\n # Choose the more specific route\n # Sort by greatest netmask and use metrics as a tie-breaker\n paths.sort(key=lambda x: (-x[0], x[1]))\n # Return interface\n ret = paths[0][2]\n self.cache[dst] = ret\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupdate the internal state of the TLS session.", "response": "def tls_session_update(self, msg_str):\n \"\"\"\n Either for parsing or building, we store the client_random\n along with the raw string representing this handshake message.\n \"\"\"\n super(TLSClientHello, self).tls_session_update(msg_str)\n\n self.tls_session.advertised_tls_version = self.version\n self.random_bytes = msg_str[10:38]\n self.tls_session.client_random = (struct.pack('!I',\n self.gmt_unix_time) +\n self.random_bytes)\n if self.ext:\n for e in self.ext:\n if isinstance(e, TLS_Ext_SupportedVersions):\n if self.tls_session.tls13_early_secret is None:\n # this is not recomputed if there was a TLS 1.3 HRR\n self.tls_session.compute_tls13_early_secrets()\n break"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nupdating the TLS session with the message string.", "response": "def tls_session_update(self, msg_str):\n \"\"\"\n Either for parsing or building, we store the server_random along with\n the raw string representing this handshake message. We also store the\n cipher suite (if recognized), and finally we instantiate the write and\n read connection states.\n \"\"\"\n super(TLSClientHello, self).tls_session_update(msg_str)\n\n s = self.tls_session\n s.tls_version = self.version\n s.server_random = self.random_bytes\n\n cs_cls = None\n if self.cipher:\n cs_val = self.cipher\n if cs_val not in _tls_cipher_suites_cls:\n warning(\"Unknown cipher suite %d from ServerHello\" % cs_val)\n # we do not try to set a default nor stop the execution\n else:\n cs_cls = _tls_cipher_suites_cls[cs_val]\n\n connection_end = s.connection_end\n s.pwcs = writeConnState(ciphersuite=cs_cls,\n connection_end=connection_end,\n tls_version=self.version)\n s.triggered_pwcs_commit = True\n s.prcs = readConnState(ciphersuite=cs_cls,\n connection_end=connection_end,\n tls_version=self.version)\n s.triggered_prcs_commit = True\n\n if self.tls_session.tls13_early_secret is None:\n # In case the connState was not pre-initialized, we could not\n # compute the early secrets at the ClientHello, so we do it here.\n self.tls_session.compute_tls13_early_secrets()\n s.compute_tls13_handshake_secrets()"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nbuilding the message with the appropriate parameters.", "response": "def build(self, *args, **kargs):\n \"\"\"\n We overload build() method in order to provide a valid default value\n for params based on TLS session if not provided. This cannot be done by\n overriding i2m() because the method is called on a copy of the packet.\n\n The 'params' field is built according to key_exchange.server_kx_msg_cls\n which should have been set after receiving a cipher suite in a\n previous ServerHello. Usual cases are:\n - None: for RSA encryption or fixed FF/ECDH. This should never happen,\n as no ServerKeyExchange should be generated in the first place.\n - ServerDHParams: for ephemeral FFDH. In that case, the parameter to\n server_kx_msg_cls does not matter.\n - ServerECDH*Params: for ephemeral ECDH. There are actually three\n classes, which are dispatched by _tls_server_ecdh_cls_guess on\n the first byte retrieved. The default here is b\"\\03\", which\n corresponds to ServerECDHNamedCurveParams (implicit curves).\n\n When the Server*DHParams are built via .fill_missing(), the session\n server_kx_privkey will be updated accordingly.\n \"\"\"\n fval = self.getfieldval(\"params\")\n if fval is None:\n s = self.tls_session\n if s.pwcs:\n if s.pwcs.key_exchange.export:\n cls = ServerRSAParams(tls_session=s)\n else:\n cls = s.pwcs.key_exchange.server_kx_msg_cls(b\"\\x03\")\n cls = cls(tls_session=s)\n try:\n cls.fill_missing()\n except Exception:\n if conf.debug_dissector:\n raise\n pass\n else:\n cls = Raw()\n self.params = cls\n\n fval = self.getfieldval(\"sig\")\n if fval is None:\n s = self.tls_session\n if s.pwcs:\n if not s.pwcs.key_exchange.anonymous:\n p = self.params\n if p is None:\n p = b\"\"\n m = s.client_random + s.server_random + raw(p)\n cls = _TLSSignature(tls_session=s)\n cls._update_sig(m, s.server_key)\n else:\n cls = Raw()\n else:\n cls = Raw()\n self.sig = cls\n\n return _TLSHandshake.build(self, *args, **kargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nchecks if the server_kx_pubkey is valid and verify the server_kx_pubkey.", "response": "def post_dissection(self, pkt):\n \"\"\"\n While previously dissecting Server*DHParams, the session\n server_kx_pubkey should have been updated.\n\n XXX Add a 'fixed_dh' OR condition to the 'anonymous' test.\n \"\"\"\n s = self.tls_session\n if s.prcs and s.prcs.key_exchange.no_ske:\n pkt_info = pkt.firstlayer().summary()\n log_runtime.info(\"TLS: useless ServerKeyExchange [%s]\", pkt_info)\n if (s.prcs and\n not s.prcs.key_exchange.anonymous and\n s.client_random and s.server_random and\n s.server_certs and len(s.server_certs) > 0):\n m = s.client_random + s.server_random + raw(self.params)\n sig_test = self.sig._verify_sig(m, s.server_certs[0])\n if not sig_test:\n pkt_info = pkt.firstlayer().summary()\n log_runtime.info(\"TLS: invalid ServerKeyExchange signature [%s]\", pkt_info)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef m2i(self, pkt, m):\n tmp_len = self.length_from(pkt)\n tbd, rem = m[:tmp_len], m[tmp_len:]\n\n s = pkt.tls_session\n cls = None\n\n if s.prcs and s.prcs.key_exchange:\n cls = s.prcs.key_exchange.client_kx_msg_cls\n\n if cls is None:\n return Raw(tbd) / Padding(rem)\n\n return cls(tbd, tls_session=s) / Padding(rem)", "response": "Convert a client_kx_msg to an internal representation."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef nmap_fp(target, oport=80, cport=81):\n sigs = nmap_sig(target, oport, cport)\n return nmap_search(sigs)", "response": "nmap fingerprinting\nnmap_fp - return a list of best guesses with accuracy\n"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef process_kwargs(obj, kwargs):\n '''\n Convenience wrapper around binwalk.core.module.Modules.kwargs.\n\n @obj - The class object (an instance of a sub-class of binwalk.core.module.Module).\n @kwargs - The kwargs provided to the object's __init__ method.\n\n Returns None.\n '''\n with Modules() as m:\n kwargs = m.kwargs(obj, kwargs)\n return kwargs", "response": "Wrapper around binwalk. core. module. Modules. kwargs.\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef show_help(fd=sys.stdout):\n '''\n Convenience wrapper around binwalk.core.module.Modules.help.\n\n @fd - An object with a write method (e.g., sys.stdout, sys.stderr, etc).\n\n Returns None.\n '''\n with Modules() as m:\n fd.write(m.help())", "response": "A convenience wrapper around binwalk. core. module. Modules. help. show_help"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting the next file to be scanned.", "response": "def next_file(self, close_previous=True):\n '''\n Gets the next file to be scanned (including pending extracted files, if applicable).\n Also re/initializes self.status.\n All modules should access the target file list through this method.\n '''\n fp = None\n\n # Ensure files are close to prevent IOError (too many open files)\n if close_previous:\n try:\n self.previous_next_file_fp.close()\n except KeyboardInterrupt as e:\n raise e\n except Exception:\n pass\n\n # Add any pending extracted files to the target_files list and reset\n # the extractor's pending file list\n self.target_file_list += self.extractor.pending\n\n # Reset all dependencies prior to continuing with another file.\n # This is particularly important for the extractor module, which must be reset\n # in order to reset its base output directory path for each file, and the\n # list of pending files.\n self.reset_dependencies()\n\n while self.target_file_list:\n next_target_file = self.target_file_list.pop(0)\n\n # Values in self.target_file_list are either already open files (BlockFile instances), or paths\n # to files that need to be opened for scanning.\n if isinstance(next_target_file, str):\n fp = self.config.open_file(next_target_file)\n else:\n fp = next_target_file\n\n if not fp:\n break\n else:\n if (self.config.file_name_filter(fp) == False or\n self._plugins_load_file(fp) == False):\n fp.close()\n fp = None\n continue\n else:\n self.status.clear()\n self.status.total = fp.length\n break\n\n if fp is not None:\n self.current_target_file_name = fp.path\n self.status.fp = fp\n else:\n self.current_target_file_name = None\n self.status.fp = None\n\n self.previous_next_file_fp = fp\n\n self._plugins_new_file(fp)\n\n return fp"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nclears the results and errors lists.", "response": "def clear(self, results=True, errors=True):\n '''\n Clears results and errors lists.\n '''\n if results:\n self.results = []\n if errors:\n self.errors = []"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef result(self, r=None, **kwargs):\n '''\n Validates a result, stores it in self.results and prints it.\n Accepts the same kwargs as the binwalk.core.module.Result class.\n\n @r - An existing instance of binwalk.core.module.Result.\n\n Returns an instance of binwalk.core.module.Result.\n '''\n if r is None:\n r = Result(**kwargs)\n\n # Add the name of the current module to the result\n r.module = self.__class__.__name__\n\n # Any module that is reporting results, valid or not, should be marked\n # as enabled\n if not self.enabled:\n self.enabled = True\n\n self.validate(r)\n self._plugins_result(r)\n\n # Update the progress status automatically if it is not being done\n # manually by the module\n if r.offset and r.file and self.AUTO_UPDATE_STATUS:\n self.status.total = r.file.length\n self.status.completed = r.offset\n self.status.fp = r.file\n\n for dependency in self.dependencies:\n try:\n getattr(self, dependency.attribute).callback(r)\n except AttributeError:\n continue\n\n if r.valid:\n self.results.append(r)\n\n if r.display:\n display_args = self._build_display_args(r)\n if display_args:\n self.config.display.format_strings(self.HEADER_FORMAT, self.RESULT_FORMAT)\n self.config.display.result(*display_args)\n\n return r", "response": "Validates a result stores it in self. results and prints it."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nstore the specified error in self. errors. A new error is created and the specified kwargs are passed to the Error class.", "response": "def error(self, **kwargs):\n '''\n Stores the specified error in self.errors.\n\n Accepts the same kwargs as the binwalk.core.module.Error class.\n\n Returns None.\n '''\n exception_header_width = 100\n\n e = Error(**kwargs)\n e.module = self.__class__.__name__\n\n self.errors.append(e)\n\n if e.exception:\n sys.stderr.write(\"\\n\" + e.module + \" Exception: \" + str(e.exception) + \"\\n\")\n sys.stderr.write(\"-\" * exception_header_width + \"\\n\")\n traceback.print_exc(file=sys.stderr)\n sys.stderr.write(\"-\" * exception_header_width + \"\\n\\n\")\n elif e.description:\n sys.stderr.write(\"\\n\" + e.module + \" Error: \" + e.description + \"\\n\\n\")"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef header(self):\n '''\n Displays the scan header, as defined by self.HEADER and self.HEADER_FORMAT.\n\n Returns None.\n '''\n self.config.display.format_strings(self.HEADER_FORMAT, self.RESULT_FORMAT)\n self.config.display.add_custom_header(self.VERBOSE_FORMAT, self.VERBOSE)\n\n if type(self.HEADER) == type([]):\n self.config.display.header(*self.HEADER, file_name=self.current_target_file_name)\n elif self.HEADER:\n self.config.display.header(self.HEADER, file_name=self.current_target_file_name)", "response": "Displays the scan header as defined by self. HEADER and self. HEADER_FORMAT.\n Returns None."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef main(self):\n '''\n Responsible for calling self.init, initializing self.config.display, and calling self.run.\n\n Returns the value returned from self.run.\n '''\n self.status = self.parent.status\n self.modules = self.parent.executed_modules\n\n # A special exception for the extractor module, which should be allowed to\n # override the verbose setting, e.g., if --matryoshka has been\n # specified\n if hasattr(self, \"extractor\") and self.extractor.config.verbose:\n self.config.verbose = self.config.display.verbose = True\n\n if not self.config.files:\n binwalk.core.common.debug(\"No target files specified, module %s terminated\" % self.name)\n return False\n\n self.reset_dependencies()\n\n try:\n self.init()\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n self.error(exception=e)\n return False\n\n try:\n self.config.display.format_strings(self.HEADER_FORMAT, self.RESULT_FORMAT)\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n self.error(exception=e)\n return False\n\n self._plugins_pre_scan()\n\n try:\n retval = self.run()\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n self.error(exception=e)\n return False\n\n self._plugins_post_scan()\n\n return retval", "response": "Main entry point for the base class."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef list(self, attribute=\"run\"):\n '''\n Finds all modules with the specified attribute.\n\n @attribute - The desired module attribute.\n\n Returns a list of modules that contain the specified attribute, in the order they should be executed.\n '''\n import binwalk.modules\n modules = {}\n\n for (name, module) in inspect.getmembers(binwalk.modules):\n if inspect.isclass(module) and hasattr(module, attribute):\n modules[module] = module.PRIORITY\n\n # user-defined modules\n import imp\n user_modules = binwalk.core.settings.Settings().user.modules\n for file_name in os.listdir(user_modules):\n if not file_name.endswith('.py'):\n continue\n module_name = file_name[:-3]\n try:\n user_module = imp.load_source(module_name, os.path.join(user_modules, file_name))\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n binwalk.core.common.warning(\"Error loading module '%s': %s\" % (file_name, str(e)))\n\n for (name, module) in inspect.getmembers(user_module):\n if inspect.isclass(module) and hasattr(module, attribute):\n modules[module] = module.PRIORITY\n\n return sorted(modules, key=modules.get, reverse=True)", "response": "Returns a list of modules that contain the specified attribute."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef help(self):\n '''\n Generates formatted help output.\n\n Returns the help string.\n '''\n modules = {}\n help_string = \"\\n\"\n help_string += \"Binwalk v%s\\n\" % binwalk.__version__\n help_string += \"Craig Heffner, ReFirmLabs\\n\"\n help_string += \"https://github.com/ReFirmLabs/binwalk\\n\"\n help_string += \"\\n\"\n help_string += \"Usage: binwalk [OPTIONS] [FILE1] [FILE2] [FILE3] ...\\n\"\n\n # Build a dictionary of modules and their ORDER attributes.\n # This makes it easy to sort modules by their ORDER attribute for\n # display.\n for module in self.list(attribute=\"CLI\"):\n if module.CLI:\n modules[module] = module.ORDER\n\n for module in sorted(modules, key=modules.get, reverse=True):\n help_string += \"\\n%s Options:\\n\" % module.TITLE\n\n for module_option in module.CLI:\n if module_option.long and not module_option.hidden:\n long_opt = '--' + module_option.long\n\n if module_option.dtype:\n optargs = \"=<%s>\" % module_option.dtype\n else:\n optargs = \"\"\n\n if module_option.short:\n short_opt = \"-\" + module_option.short + \",\"\n else:\n short_opt = \" \"\n\n fmt = \" %%s %%s%%-%ds%%s\\n\" % (25 - len(long_opt))\n help_string += fmt % (short_opt, long_opt, optargs, module_option.description)\n\n return help_string + \"\\n\"", "response": "Generates formatted help output."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef execute(self, *args, **kwargs):\n '''\n Executes all appropriate modules according to the options specified in args/kwargs.\n\n Returns a list of executed module objects.\n '''\n run_modules = []\n orig_arguments = self.arguments\n\n if args or kwargs:\n self._set_arguments(list(args), kwargs)\n\n # Run all modules\n for module in self.list():\n obj = self.run(module)\n\n # Add all loaded modules that marked themselves as enabled to the\n # run_modules list\n for (module, obj) in iterator(self.executed_modules):\n # Report the results if the module is enabled and if it is a\n # primary module or if it reported any results/errors\n if obj.enabled and (obj.PRIMARY or obj.results or obj.errors):\n run_modules.append(obj)\n\n self.arguments = orig_arguments\n\n return run_modules", "response": "Executes all appropriate modules according to the options specified in args or kwargs. Returns a list of module objects that were executed."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nruns a specific module.", "response": "def run(self, module, dependency=False, kwargs={}):\n '''\n Runs a specific module.\n '''\n try:\n obj = self.load(module, kwargs)\n\n if isinstance(obj, binwalk.core.module.Module) and obj.enabled:\n obj.main()\n self.status.clear()\n\n # If the module is not being loaded as a dependency, add it to the executed modules dictionary.\n # This is used later in self.execute to determine which objects\n # should be returned.\n if not dependency:\n self.executed_modules[module] = obj\n\n # The unload method tells the module that we're done with it, and gives it a chance to do\n # any cleanup operations that may be necessary. We still retain\n # the object instance in self.executed_modules.\n obj._unload_dependencies()\n obj.unload()\n except KeyboardInterrupt as e:\n # Tell the status server to shut down, and give it time to clean\n # up.\n if self.status.running:\n self.status.shutdown = True\n while not self.status.finished:\n time.sleep(0.1)\n raise e\n\n return obj"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nprocessing the command line arguments for any specific module.", "response": "def argv(self, module, argv=sys.argv[1:]):\n '''\n Processes argv for any options specific to the specified module.\n\n @module - The module to process argv for.\n @argv - A list of command line arguments (excluding argv[0]).\n\n Returns a dictionary of kwargs for the specified module.\n '''\n kwargs = {'enabled': False}\n last_priority = {}\n parser = argparse.ArgumentParser(add_help=False)\n # Hack: This allows the ListActionParser class to correllate short options to long options.\n # There is probably a built-in way to do this in the\n # argparse.ArgumentParser class?\n parser.short_to_long = {}\n\n # Must build arguments from all modules so that:\n #\n # 1) Any conflicting arguments will raise an exception\n # 2) The only unknown arguments will be the target files, making them\n # easy to identify\n for m in self.list(attribute=\"CLI\"):\n\n for module_option in m.CLI:\n\n parser_args = []\n parser_kwargs = {}\n\n if not module_option.long:\n continue\n\n if module_option.short:\n parser_args.append('-' + module_option.short)\n parser_args.append('--' + module_option.long)\n parser_kwargs['dest'] = module_option.long\n\n if module_option.type is None:\n parser_kwargs['action'] = 'store_true'\n elif module_option.type is list:\n parser_kwargs['action'] = 'append'\n parser.short_to_long[\n module_option.short] = module_option.long\n\n parser.add_argument(*parser_args, **parser_kwargs)\n\n args, unknown = parser.parse_known_args(argv)\n args = args.__dict__\n\n # Only add parsed options pertinent to the requested module\n for module_option in module.CLI:\n\n if module_option.type == binwalk.core.common.BlockFile:\n\n for k in get_keys(module_option.kwargs):\n kwargs[k] = []\n for unk in unknown:\n kwargs[k].append(unk)\n\n elif has_key(args, module_option.long) and args[module_option.long] not in [None, False]:\n\n # Loop through all the kwargs for this command line option\n for (name, default_value) in iterator(module_option.kwargs):\n\n # If this kwarg has not been previously processed, or if its priority is equal to or\n # greater than the previously processed kwarg's priority,\n # then let's process it.\n if not has_key(last_priority, name) or last_priority[name] <= module_option.priority:\n\n # Track the priority for future iterations that may\n # process the same kwarg name\n last_priority[name] = module_option.priority\n\n try:\n kwargs[name] = module_option.convert(args[module_option.long], default_value)\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n raise ModuleException(\"Invalid usage: %s\" % str(e))\n\n binwalk.core.common.debug(\"%s :: %s => %s\" % (module.TITLE, str(argv), str(kwargs)))\n return kwargs"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nprocess a module s kwargs. All modules should use this for kwarg processing.", "response": "def kwargs(self, obj, kwargs):\n '''\n Processes a module's kwargs. All modules should use this for kwarg processing.\n\n @obj - An instance of the module (e.g., self)\n @kwargs - The kwargs passed to the module\n\n Returns None.\n '''\n if hasattr(obj, \"KWARGS\"):\n for module_argument in obj.KWARGS:\n if has_key(kwargs, module_argument.name):\n arg_value = kwargs[module_argument.name]\n else:\n arg_value = copy(module_argument.default)\n\n setattr(obj, module_argument.name, arg_value)\n\n for (k, v) in iterator(kwargs):\n if not hasattr(obj, k):\n setattr(obj, k, v)\n else:\n raise Exception(\"binwalk.core.module.Modules.process_kwargs: %s has no attribute 'KWARGS'\" % str(obj))"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nstarts the status server on the specified port.", "response": "def status_server(self, port):\n '''\n Starts the progress bar TCP service on the specified port.\n This service will only be started once per instance, regardless of the\n number of times this method is invoked.\n\n Failure to start the status service is considered non-critical; that is,\n a warning will be displayed to the user, but normal operation will proceed.\n '''\n if self.status_server_started == False:\n self.status_server_started = True\n try:\n self.status_service = binwalk.core.statuserver.StatusServer(port, self)\n except Exception as e:\n binwalk.core.common.warning(\"Failed to start status server on port %d: %s\" % (port, str(e)))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _make_short(self, data, endianness):\n data = binwalk.core.compat.str2bytes(data)\n return struct.unpack('%sH' % endianness, data)[0]", "response": "Returns a 2 byte integer."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_fname_len(self, bufflen=128):\n buff = self.meta.peek(bufflen)\n strlen = buff.find('\\0')\n for i, b in enumerate(buff[strlen:]):\n if b != '\\0':\n return strlen+i\n return bufflen", "response": "Returns the number of bytes designated for the filename."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _get_node(self):\n data = self.meta.read(self.node_size)\n return PFSNode(data, self.endianness)", "response": "Reads a chunk of meta data from file and returns a PFSNode."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns an iterator over the file meta - data entries one by one.", "response": "def entries(self):\n \"\"\"Returns file meta-data entries one by one.\"\"\"\n self.meta.seek(self.file_list_start)\n for i in range(0, self.num_files):\n yield self._get_node()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nextracting the actual string from the available bytes.", "response": "def _decode_fname(self):\n \"\"\"Extracts the actual string from the available bytes.\"\"\"\n self.fname = self.fname[:self.fname.find('\\0')]\n self.fname = self.fname.replace('\\\\', '/')"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _generate_regex(self, line):\n '''\n Generates a regular expression from the magic bytes of a signature.\n The regex is used by Magic._analyze.\n\n @line - The first SignatureLine object of the signature.\n\n Returns a compile regular expression.\n '''\n restr = \"\"\n\n # Strings and single byte signatures are taken at face value;\n # multi-byte integer values are turned into regex strings based\n # on their data type size and endianness.\n if line.type == 'regex':\n # Regex types are already compiled expressions.\n # Note that since re.finditer is used, unless the specified\n # regex accounts for it, overlapping signatures will be ignored.\n return line.value\n if line.type == 'string':\n restr = line.value\n elif line.size == 1:\n restr = chr(line.value)\n elif line.size == 2:\n if line.endianness == '<':\n restr = chr(line.value & 0xFF) + chr(line.value >> 8)\n elif line.endianness == '>':\n restr = chr(line.value >> 8) + chr(line.value & 0xFF)\n elif line.size == 4:\n if line.endianness == '<':\n restr = (chr(line.value & 0xFF) +\n chr((line.value >> 8) & 0xFF) +\n chr((line.value >> 16) & 0xFF) +\n chr(line.value >> 24))\n elif line.endianness == '>':\n restr = (chr(line.value >> 24) +\n chr((line.value >> 16) & 0xFF) +\n chr((line.value >> 8) & 0xFF) +\n chr(line.value & 0xFF))\n elif line.size == 8:\n if line.endianness == '<':\n restr = (chr(line.value & 0xFF) +\n chr((line.value >> 8) & 0xFF) +\n chr((line.value >> 16) & 0xFF) +\n chr((line.value >> 24) & 0xFF) +\n chr((line.value >> 32) & 0xFF) +\n chr((line.value >> 40) & 0xFF) +\n chr((line.value >> 48) & 0xFF) +\n chr(line.value >> 56))\n elif line.endianness == '>':\n restr = (chr(line.value >> 56) +\n chr((line.value >> 48) & 0xFF) +\n chr((line.value >> 40) & 0xFF) +\n chr((line.value >> 32) & 0xFF) +\n chr((line.value >> 24) & 0xFF) +\n chr((line.value >> 16) & 0xFF) +\n chr((line.value >> 8) & 0xFF) +\n chr(line.value & 0xFF))\n\n # Since re.finditer is used on a per-signature basis, signatures should be crafted carefully\n # to ensure that they aren't potentially self-overlapping (e.g., a signature of \"ABCDAB\" could\n # be confused by the byte sequence \"ABCDABCDAB\"). The longer the signature, the less likely an\n # unintentional overlap is, although files could still be maliciously crafted to cause false\n # negative results.\n #\n # Thus, unless a signature has been explicitly marked as knowingly overlapping ('{overlap}'),\n # spit out a warning about any self-overlapping signatures.\n if not binwalk.core.compat.has_key(line.tags, 'overlap'):\n for i in range(1, line.size):\n if restr[i:] == restr[0:(line.size - i)]:\n binwalk.core.common.warning(\"Signature '%s' is a self-overlapping signature!\" % line.text)\n break\n\n return re.compile(re.escape(restr))", "response": "Generates a regular expression from the magic bytes of a signature."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _filtered(self, text):\n '''\n Tests if a string should be filtered out or not.\n\n @text - The string to check against filter rules.\n\n Returns True if the string should be filtered out, i.e., not displayed.\n Returns False if the string should be displayed.\n '''\n filtered = None\n # Text is converted to lower case first, partially for historical\n # purposes, but also because it simplifies writing filter rules\n # (e.g., don't have to worry about case sensitivity).\n text = text.lower()\n\n for include in self.includes:\n if include.search(text):\n filtered = False\n break\n\n # If exclusive include filters have been specified and did\n # not match the text, then the text should be filtered out.\n if self.includes and filtered == None:\n return True\n\n for exclude in self.excludes:\n if exclude.search(text):\n filtered = True\n break\n\n # If no explicit exclude filters were matched, then the\n # text should *not* be filtered.\n if filtered == None:\n filtered = False\n\n return filtered", "response": "Tests if a string should be filtered out or not."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _do_math(self, offset, expression):\n '''\n Parses and evaluates complex expressions, e.g., \"(4.l+12)\", \"(6*32)\", etc.\n\n @offset - The offset inside self.data that the current signature starts at.\n @expressions - The expression to evaluate.\n\n Returns an integer value that is the result of the evaluated expression.\n '''\n # Does the expression contain an offset (e.g., \"(4.l+12)\")?\n if '.' in expression and '(' in expression:\n replacements = {}\n\n for period in [match.start() for match in self.period.finditer(expression)]:\n # Separate the offset field into the integer offset and type\n # values (o and t respsectively)\n s = expression[:period].rfind('(') + 1\n # The offset address may be an evaluatable expression, such as '(4+0.L)', typically the result\n # of the original offset being something like '(&0.L)'.\n o = binwalk.core.common.MathExpression(expression[s:period]).value\n t = expression[period + 1]\n\n # Re-build just the parsed offset portion of the expression\n text = \"%s.%c\" % (expression[s:period], t)\n\n # Have we already evaluated this offset expression? If so, skip\n # it.\n if binwalk.core.common.has_key(replacements, text):\n continue\n\n # The offset specified in the expression is relative to the\n # starting offset inside self.data\n o += offset\n\n # Read the value from self.data at the specified offset\n try:\n # Big and little endian byte format\n if t in ['b', 'B']:\n v = struct.unpack('b', binwalk.core.compat.str2bytes(self.data[o:o + 1]))[0]\n # Little endian short format\n elif t == 's':\n v = struct.unpack('h', binwalk.core.compat.str2bytes(self.data[o:o + 2]))[0]\n # Bit endian long format\n elif t == 'L':\n v = struct.unpack('>i', binwalk.core.compat.str2bytes(self.data[o:o + 4]))[0]\n # struct.error is thrown if there is not enough bytes in\n # self.data for the specified format type\n except struct.error as e:\n v = 0\n\n # Keep track of all the recovered values from self.data\n replacements[text] = v\n\n # Finally, replace all offset expressions with their corresponding\n # text value\n v = expression\n for (text, value) in binwalk.core.common.iterator(replacements):\n v = v.replace(text, \"%d\" % value)\n\n # If no offset, then it's just an evaluatable math expression (e.g.,\n # \"(32+0x20)\")\n else:\n v = expression\n\n # Evaluate the final expression\n value = binwalk.core.common.MathExpression(v).value\n\n return value", "response": "Parse and evaluate a complex expression and return the value of the expression."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _analyze(self, signature, offset):\n '''\n Analyzes self.data for the specified signature data at the specified offset .\n\n @signature - The signature to apply to the data.\n @offset - The offset in self.data to apply the signature to.\n\n Returns a dictionary of tags parsed from the data.\n '''\n description = []\n max_line_level = 0\n previous_line_end = 0\n tags = {'id': signature.id, 'offset':\n offset, 'invalid': False, 'once': False}\n\n # Apply each line of the signature to self.data, starting at the\n # specified offset\n for n in range(0, len(signature.lines)):\n line = signature.lines[n]\n\n # Ignore indentation levels above the current max indent level\n if line.level <= max_line_level:\n # If the relative offset of this signature line is just an\n # integer value, use it\n if isinstance(line.offset, int):\n line_offset = line.offset\n # Else, evaluate the complex expression\n else:\n # Format the previous_line_end value into a string. Add the '+' sign to explicitly\n # state that this value is to be added to any subsequent values in the expression\n # (e.g., '&0' becomes '4+0').\n ple = '%d+' % previous_line_end\n # Allow users to use either the '&0' (libmagic) or '&+0' (explcit addition) sytaxes;\n # replace both with the ple text.\n line_offset_text = line.offset.replace('&+', ple).replace('&', ple)\n # Evaluate the expression\n line_offset = self._do_math(offset, line_offset_text)\n\n # Sanity check\n if not isinstance(line_offset, int):\n raise ParserException(\"Failed to convert offset '%s' to a number: '%s'\" % (line.offset, line.text))\n\n # The start of the data needed by this line is at offset + line_offset.\n # The end of the data will be line.size bytes later.\n start = offset + line_offset\n end = start + line.size\n\n # If the line has a packed format string, unpack it\n if line.pkfmt:\n try:\n dvalue = struct.unpack(line.pkfmt, binwalk.core.compat.str2bytes(self.data[start:end]))[0]\n # Not enough bytes left in self.data for the specified\n # format size\n except struct.error as e:\n dvalue = 0\n # Else, this is a string\n else:\n # Wildcard strings have line.value == None\n if line.value is None:\n # Check to see if this is a string whose size is known and has been specified on a previous\n # signature line.\n if binwalk.core.compat.has_key(tags, 'strlen') and binwalk.core.compat.has_key(line.tags, 'string'):\n dvalue = self.data[start:(start + tags['strlen'])]\n # Else, just terminate the string at the first newline,\n # carriage return, or NULL byte\n else:\n dvalue = self.data[start:end].split('\\x00')[0].split('\\r')[0].split('\\n')[0]\n # Non-wildcard strings have a known length, specified in\n # the signature line\n else:\n dvalue = self.data[start:end]\n\n # Some integer values have special operations that need to be performed on them\n # before comparison (e.g., \"belong&0x0000FFFF\"). Complex math expressions are\n # supported here as well.\n # if isinstance(dvalue, int) and line.operator:\n if line.operator:\n try:\n # If the operator value of this signature line is just\n # an integer value, use it\n if isinstance(line.opvalue, int) or isinstance(line.opvalue, long):\n opval = line.opvalue\n # Else, evaluate the complex expression\n else:\n opval = self._do_math(offset, line.opvalue)\n\n # Perform the specified operation\n if line.operator == '&':\n dvalue &= opval\n elif line.operator == '|':\n dvalue |= opval\n elif line.operator == '*':\n dvalue *= opval\n elif line.operator == '+':\n dvalue += opval\n elif line.operator == '-':\n dvalue -= opval\n elif line.operator == '/':\n dvalue /= opval\n elif line.operator == '~':\n dvalue = ~opval\n elif line.operator == '^':\n dvalue ^= opval\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n raise ParserException(\"Operation '\" +\n str(dvalue) +\n \" \" +\n str(line.operator) +\n \"= \" +\n str(line.opvalue) +\n \"' failed: \" + str(e))\n\n # Does the data (dvalue) match the specified comparison?\n if ((line.value is None) or\n (line.regex and line.value.match(dvalue)) or\n (line.condition == '=' and dvalue == line.value) or\n (line.condition == '>' and dvalue > line.value) or\n (line.condition == '<' and dvalue < line.value) or\n (line.condition == '!' and dvalue != line.value) or\n (line.condition == '~' and (dvalue == ~line.value)) or\n (line.condition == '^' and (dvalue ^ line.value)) or\n (line.condition == '&' and (dvalue & line.value)) or\n (line.condition == '|' and (dvalue | line.value))):\n\n # Up until this point, date fields are treated as integer values,\n # but we want to display them as nicely formatted strings.\n if line.type == 'date':\n try:\n ts = datetime.datetime.utcfromtimestamp(dvalue)\n dvalue = ts.strftime(\"%Y-%m-%d %H:%M:%S\")\n except KeyboardInterrupt as e:\n raise e\n except Exception:\n dvalue = \"invalid timestamp\"\n\n # Generate the tuple for the format string\n dvalue_tuple = ()\n for x in self.fmtstr.finditer(line.format):\n dvalue_tuple += (dvalue,)\n\n # Format the description string\n desc = line.format % dvalue_tuple\n\n # If there was any description string, append it to the\n # list of description string parts\n if desc:\n description.append(desc)\n\n # Process tag keywords specified in the signature line. These have already been parsed out of the\n # original format string so that they can be processed\n # separately from the printed description string.\n for (tag_name, tag_value) in binwalk.core.compat.iterator(line.tags):\n # If the tag value is a string, try to format it\n if isinstance(tag_value, str):\n # Generate the tuple for the format string\n dvalue_tuple = ()\n for x in self.fmtstr.finditer(tag_value):\n dvalue_tuple += (dvalue,)\n\n # Format the tag string\n tags[tag_name] = tag_value % dvalue_tuple\n # Else, just use the raw tag value\n else:\n tags[tag_name] = tag_value\n\n # Some tag values are intended to be integer values, so\n # try to convert them as such\n try:\n tags[tag_name] = int(tags[tag_name], 0)\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n pass\n\n # Abort processing soon as this signature is marked invalid, unless invalid results\n # were explicitly requested. This means that the sooner invalid checks are made in a\n # given signature, the faster the scan can filter out false\n # positives.\n if not self.show_invalid and tags['invalid']:\n break\n\n # Look ahead to the next line in the signature; if its indent level is greater than\n # that of the current line, then track the end of data for the current line. This is\n # so that subsequent lines can use the '>>&0' offset syntax to specify relative offsets\n # from previous lines.\n try:\n next_line = signature.lines[n + 1]\n if next_line.level > line.level:\n if line.type == 'string':\n previous_line_end = line_offset + len(dvalue)\n else:\n previous_line_end = line_offset + line.size\n except IndexError as e:\n pass\n\n # If this line satisfied its comparison, +1 the max\n # indentation level\n max_line_level = line.level + 1\n else:\n # No match on the first line, abort\n if line.level == 0:\n break\n else:\n # If this line did not satisfy its comparison, then higher\n # indentation levels will not be accepted.\n max_line_level = line.level\n\n # Join the formatted description strings and remove backspace\n # characters (plus the preceeding character as well)\n tags['description'] = self.bspace.sub('', \" \".join(description))\n\n # This should never happen\n if not tags['description']:\n tags['display'] = False\n tags['invalid'] = True\n\n # If the formatted string contains non-printable characters, consider\n # it invalid\n if self.printable.match(tags['description']).group() != tags['description']:\n tags['invalid'] = True\n\n return tags", "response": "Analyzes the data for the specified signature at the specified offset."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef scan(self, data, dlen=None):\n '''\n Scan a data block for matching signatures.\n\n @data - A string of data to scan.\n @dlen - If specified, signatures at offsets larger than dlen will be ignored.\n\n Returns a list of SignatureResult objects.\n '''\n results = []\n matched_offsets = set()\n\n # Since data can potentially be quite a large string, make it available to other\n # methods via a class attribute so that it doesn't need to be passed around to\n # different methods over and over again.\n self.data = data\n\n # If dlen wasn't specified, search all of self.data\n if dlen is None:\n dlen = len(data)\n\n for signature in self.signatures:\n # Use regex to search the data block for potential signature\n # matches (fast)\n for match in signature.regex.finditer(data):\n # Take the offset of the start of the signature into account\n offset = match.start() - signature.offset\n\n # Signatures are ordered based on the length of their magic bytes (largest first).\n # If this offset has already been matched to a previous signature, ignore it unless\n # self.show_invalid has been specified. Also ignore obviously invalid offsets (<0)\n # as well as those outside the specified self.data range (dlen).\n if (offset not in matched_offsets or self.show_invalid) and offset >= 0 and offset < dlen:\n # if offset >= 0 and offset < dlen:\n # Analyze the data at this offset using the current\n # signature rule\n tags = self._analyze(signature, offset)\n\n # Generate a SignatureResult object and append it to the results list if the\n # signature is valid, or if invalid results were requested.\n if (not tags['invalid'] or self.show_invalid) and not self._filtered(tags['description']):\n # Only display results with the 'once' tag once.\n if tags['once']:\n if signature.title in self.display_once:\n continue\n else:\n self.display_once.add(signature.title)\n\n # Append the result to the results list\n results.append(SignatureResult(**tags))\n\n # Add this offset to the matched_offsets set, so that it can be ignored by\n # subsequent loops.\n matched_offsets.add(offset)\n\n # Sort results by offset\n results.sort(key=lambda x: x.offset, reverse=False)\n\n return results", "response": "Scan a data block for matching signatures."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nloads signatures from a file.", "response": "def load(self, fname):\n '''\n Load signatures from a file.\n\n @fname - Path to signature file.\n\n Returns None.\n '''\n # Magic files must be ASCII, else encoding issues can arise.\n fp = open(fname, \"r\")\n lines = fp.readlines()\n self.parse(lines)\n fp.close()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nparsing the signature file lines.", "response": "def parse(self, lines):\n '''\n Parse signature file lines.\n\n @lines - A list of lines from a signature file.\n\n Returns None.\n '''\n signature = None\n\n for line in lines:\n # Split at the first comment delimiter (if any) and strip the\n # result\n line = line.split('#')[0].strip()\n # Ignore blank lines and lines that are nothing but comments.\n # We also don't support the '!mime' style line entries.\n if line and line[0] != '!':\n # Parse this signature line\n sigline = SignatureLine(line)\n # Level 0 means the first line of a signature entry\n if sigline.level == 0:\n # If there is an existing signature, append it to the signature list,\n # unless the text in its title field has been filtered by user-defined\n # filter rules.\n if signature and not self._filtered(signature.title):\n self.signatures.append(signature)\n\n # Create a new signature object; use the size of self.signatures to\n # assign each signature a unique ID.\n signature = Signature(len(self.signatures), sigline)\n # Else, just append this line to the existing signature\n elif signature:\n # signature.append(sigline)\n signature.lines.append(sigline)\n # If this is not the first line of a signature entry and there is no other\n # existing signature entry, something is very wrong with the\n # signature file.\n else:\n raise ParserException(\"Invalid signature line: '%s'\" % line)\n\n # Add the final signature to the signature list\n if signature:\n if not self._filtered(signature.lines[0].format):\n self.signatures.append(signature)\n\n # Sort signatures by confidence (aka, length of their magic bytes),\n # largest first\n self.signatures.sort(key=lambda x: x.confidence, reverse=True)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _set_verbosity(self):\n '''\n Sets the appropriate verbosity.\n Must be called after self._test_target_files so that self.target_files is properly set.\n '''\n # If more than one target file was specified, enable verbose mode; else, there is\n # nothing in some outputs to indicate which scan corresponds to which\n # file.\n if len(self.target_files) > 1 and not self.verbose:\n self.verbose = True", "response": "Sets the appropriate verbosity."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncheck to see if a file should be scanned based on file name include and exclude filters.", "response": "def file_name_filter(self, fp):\n '''\n Checks to see if a file should be scanned based on file name include/exclude filters.\n Most useful for matryoshka scans where only certian files are desired.\n\n @fp - An instances of binwalk.common.BlockFile\n\n Returns True if the file should be scanned, False if not.\n '''\n if self.file_name_include_regex and not self.file_name_include_regex.search(fp.name):\n return False\n if self.file_name_exclude_regex and self.file_name_exclude_regex.search(fp.name):\n return False\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nopens the specified file with all pertinent configuration settings.", "response": "def open_file(self, fname, length=None, offset=None, swap=None, block=None, peek=None):\n '''\n Opens the specified file with all pertinent configuration settings.\n '''\n if length is None:\n length = self.length\n if offset is None:\n offset = self.offset\n if swap is None:\n swap = self.swap_size\n\n return binwalk.core.common.BlockFile(fname,\n subclass=self.subclass,\n length=length,\n offset=offset,\n swap=swap,\n block=block,\n peek=peek)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _open_target_files(self):\n '''\n Checks if the target files can be opened.\n Any files that cannot be opened are removed from the self.target_files list.\n '''\n # Validate the target files listed in target_files\n for tfile in self.files:\n # Ignore directories.\n if not self.subclass == io.FileIO or not os.path.isdir(tfile):\n # Make sure we can open the target files\n try:\n fp = self.open_file(tfile)\n fp.close()\n self.target_files.append(tfile)\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n self.error(description=\"Cannot open file %s (CWD: %s) : %s\" % (tfile, os.getcwd(), str(e)))", "response": "Checks if the target files can be opened."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nvalidates the result of a single entry.", "response": "def validate(self, r):\n '''\n Called automatically by self.result.\n '''\n if self.show_invalid:\n r.valid = True\n elif r.valid:\n if not r.description:\n r.valid = False\n\n if r.size and (r.size + r.offset) > r.file.size:\n r.valid = False\n\n if r.jump and (r.jump + r.offset) > r.file.size:\n r.valid = False\n\n if hasattr(r, \"location\") and (r.location != r.offset):\n r.valid = False\n\n if r.valid:\n # Don't keep displaying signatures that repeat a bunch of times\n # (e.g., JFFS2 nodes)\n if r.id == self.one_of_many:\n r.display = False\n elif r.many:\n self.one_of_many = r.id\n else:\n self.one_of_many = None"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nobtain a list of all user and system plugin modules.", "response": "def list_plugins(self):\n '''\n Obtain a list of all user and system plugin modules.\n\n Returns a dictionary of:\n\n {\n 'user' : {\n 'modules' : [list, of, module, names],\n 'descriptions' : {'module_name' : 'module pydoc string'},\n 'enabled' : {'module_name' : True},\n 'path' : \"path/to/module/plugin/directory\"\n },\n 'system' : {\n 'modules' : [list, of, module, names],\n 'descriptions' : {'module_name' : 'module pydoc string'},\n 'enabled' : {'module_name' : True},\n 'path' : \"path/to/module/plugin/directory\"\n }\n }\n '''\n\n plugins = {\n 'user': {\n 'modules': [],\n 'descriptions': {},\n 'enabled': {},\n 'path': None,\n },\n 'system': {\n 'modules': [],\n 'descriptions': {},\n 'enabled': {},\n 'path': None,\n }\n }\n\n for key in plugins.keys():\n if key == 'user':\n plugins[key]['path'] = self.settings.user.plugins\n else:\n plugins[key]['path'] = self.settings.system.plugins\n\n if plugins[key]['path']:\n for file_name in os.listdir(plugins[key]['path']):\n if file_name.endswith(self.MODULE_EXTENSION):\n module = file_name[:-len(self.MODULE_EXTENSION)]\n\n try:\n plugin = imp.load_source(module, os.path.join(plugins[key]['path'], file_name))\n plugin_class = self._find_plugin_class(plugin)\n\n plugins[key]['enabled'][module] = True\n plugins[key]['modules'].append(module)\n except KeyboardInterrupt as e:\n raise e\n # Python files in the plugins directory that are not\n # actually binwalk plugins will generate a TypeError\n # about converting an object to a string implicitly.\n # Don't need to warn about these.\n except TypeError:\n pass\n except Exception as e:\n binwalk.core.common.warning(\"Error loading plugin '%s': %s\" % (file_name, str(e)))\n plugins[key]['enabled'][module] = False\n\n try:\n plugins[key]['descriptions'][\n module] = plugin_class.__doc__.strip().split('\\n')[0]\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n plugins[key]['descriptions'][\n module] = 'No description'\n return plugins"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncalculates Shannon entropy on a given block of data.", "response": "def shannon(self, data):\n '''\n Performs a Shannon entropy analysis on a given block of data.\n '''\n entropy = 0\n\n if data:\n length = len(data)\n\n seen = dict(((chr(x), 0) for x in range(0, 256)))\n for byte in data:\n seen[byte] += 1\n\n for x in range(0, 256):\n p_x = float(seen[chr(x)]) / length\n if p_x > 0:\n entropy -= p_x * math.log(p_x, 2)\n\n return (entropy / 8)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef gzip(self, data, truncate=True):\n '''\n Performs an entropy analysis based on zlib compression ratio.\n This is faster than the shannon entropy analysis, but not as accurate.\n '''\n # Entropy is a simple ratio of: / \n e = float(float(len(zlib.compress(str2bytes(data), 9))) / float(len(data)))\n\n if truncate and e > 1.0:\n e = 1.0\n\n return e", "response": "Calculates the entropy of a file."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef nts(self, s):\n # Use the string up to the first null char.\n p = s.find(\"\\0\")\n if p == -1:\n return s\n return s[:p]", "response": "Convert a null - terminated string field to a python string."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef nti(self, s):\n # There are two possible encodings for a number field, see\n # itn() below.\n if s[0] != chr(0x80):\n try:\n n = int(self.nts(s) or \"0\", 8)\n except ValueError:\n raise ValueError(\"invalid tar header\")\n else:\n n = 0\n for i in xrange(len(s) - 1):\n n <<= 8\n n += ord(s[i + 1])\n return n", "response": "Convert a number field to a python number."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef debug(msg):\n '''\n Displays debug messages to stderr only if the Python interpreter was invoked with the -O flag.\n '''\n if DEBUG:\n sys.stderr.write(\"DEBUG: \" + msg + \"\\n\")\n sys.stderr.flush()", "response": "Displays debug messages to stderr only if DEBUG is set to True."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngenerating an MD5 hash of the specified file.", "response": "def file_md5(file_name):\n '''\n Generate an MD5 hash of the specified file.\n\n @file_name - The file to hash.\n\n Returns an MD5 hex digest string.\n '''\n md5 = hashlib.md5()\n\n with open(file_name, 'rb') as f:\n for chunk in iter(lambda: f.read(128 * md5.block_size), b''):\n md5.update(chunk)\n\n return md5.hexdigest()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef file_size(filename):\n '''\n Obtains the size of a given file.\n\n @filename - Path to the file.\n\n Returns the size of the file.\n '''\n # Using open/lseek works on both regular files and block devices\n fd = os.open(filename, os.O_RDONLY)\n try:\n return os.lseek(fd, 0, os.SEEK_END)\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n raise Exception(\n \"file_size failed to obtain the size of '%s': %s\" % (filename, str(e)))\n finally:\n os.close(fd)", "response": "Returns the size of a given file."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a string comprised of all data in between double quotes.", "response": "def get_quoted_strings(quoted_string):\n '''\n Returns a string comprised of all data in between double quotes.\n\n @quoted_string - String to get quoted data from.\n\n Returns a string of quoted data on success.\n Returns a blank string if no quoted data is present.\n '''\n try:\n # This regex grabs all quoted data from string.\n # Note that this gets everything in between the first and last double quote.\n # This is intentional, as printed (and quoted) strings from a target file may contain\n # double quotes, and this function should ignore those. However, it also means that any\n # data between two quoted strings (ex: '\"quote 1\" non-quoted data\n # \"quote 2\"') will also be included.\n return re.findall(r'\\\"(.*)\\\"', quoted_string)[0]\n except KeyboardInterrupt as e:\n raise e\n except Exception:\n return ''"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a unique file name based on the specified base name and extension.", "response": "def unique_file_name(base_name, extension=''):\n '''\n Creates a unique file name based on the specified base name.\n\n @base_name - The base name to use for the unique file name.\n @extension - The file extension to use for the unique file name.\n\n Returns a unique file string.\n '''\n idcount = 0\n\n if extension and not extension.startswith('.'):\n extension = '.%s' % extension\n\n fname = base_name + extension\n\n while os.path.exists(fname):\n fname = \"%s-%d%s\" % (base_name, idcount, extension)\n idcount += 1\n\n return fname"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef strings(filename, minimum=4):\n '''\n A strings generator, similar to the Unix strings utility.\n\n @filename - The file to search for strings in.\n @minimum - The minimum string length to search for.\n\n Yeilds printable ASCII strings from filename.\n '''\n result = \"\"\n\n with BlockFile(filename) as f:\n while True:\n (data, dlen) = f.read_block()\n if dlen < 1:\n break\n\n for c in data:\n if c in string.printable:\n result += c\n continue\n elif len(result) >= minimum:\n yield result\n result = \"\"\n else:\n result = \"\"", "response": "A generator that returns a list of strings from a file."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef bytes2str(bs):\n '''\n For cross compatibility between Python 2 and Python 3 strings.\n '''\n if isinstance(bs, type(b'')) and PY_MAJOR_VERSION > 2:\n return bs.decode('latin1')\n else:\n return bs", "response": "Convert a byte string to a unicode string."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate a set of rules for the current locale.", "response": "def create_rule(self, txtrule=None, regex=None, extension=None, cmd=None, codes=[0, None], recurse=True):\n '''\n Adds a set of rules to the extraction rule list.\n\n @txtrule - Rule string, or list of rule strings, in the format :[:]\n @regex - If rule string is not specified, this is the regular expression string to use.\n @extension - If rule string is not specified, this is the file extension to use.\n @cmd - If rule string is not specified, this is the command to run.\n Alternatively a callable object may be specified, which will be passed one argument: the path to the file to extract.\n @codes - A list of valid return codes for the extractor.\n @recurse - If False, extracted directories will not be recursed into when the matryoshka option is enabled.\n\n Returns None.\n '''\n rules = []\n created_rules = []\n match = False\n r = {\n 'extension': '',\n 'cmd': '',\n 'regex': None,\n 'codes': codes,\n 'recurse': recurse,\n }\n\n # Process single explicitly specified rule\n if not txtrule and regex and extension:\n r['extension'] = extension\n r['regex'] = re.compile(regex)\n if cmd:\n r['cmd'] = cmd\n\n return [r]\n\n # Process rule string, or list of rule strings\n if not isinstance(txtrule, type([])):\n rules = [txtrule]\n else:\n rules = txtrule\n\n for rule in rules:\n r['cmd'] = ''\n r['extension'] = ''\n\n try:\n values = self._parse_rule(rule)\n match = values[0]\n r['regex'] = re.compile(values[0])\n r['extension'] = values[1]\n r['cmd'] = values[2]\n r['codes'] = values[3]\n r['recurse'] = values[4]\n except KeyboardInterrupt as e:\n raise e\n except Exception:\n pass\n\n # Verify that the match string was retrieved.\n if match:\n created_rules.append(r)\n\n return created_rules"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef remove_rules(self, description):\n '''\n Remove all rules that match a specified description.\n\n @description - The description to match against.\n\n Returns the number of rules removed.\n '''\n rm = []\n description = description.lower()\n\n for i in range(0, len(self.extract_rules)):\n if self.extract_rules[i]['regex'].search(description):\n rm.append(i)\n\n for i in rm:\n self.extract_rules.pop(i)\n\n return len(rm)", "response": "Removes all rules that match a specified description."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nedit all rules that match a specified description.", "response": "def edit_rules(self, description, key, value):\n '''\n Edit all rules that match a specified description.\n\n @description - The description to match against.\n @key - The key to change for each matching rule.\n @value - The new key value for each matching rule.\n\n Returns the number of rules modified.\n '''\n count = 0\n description = description.lower()\n\n for i in range(0, len(self.extract_rules)):\n if self.extract_rules[i]['regex'].search(description):\n if has_key(self.extract_rules[i], key):\n self.extract_rules[i][key] = value\n count += 1\n\n return count"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_rules(self, description=None):\n '''\n Returns a list of extraction rules that match a given description.\n\n @description - The description to match against.\n\n Returns a list of extraction rules that match the given description.\n If no description is provided, a list of all rules are returned.\n '''\n if description:\n rules = []\n description = description.lower()\n for i in range(0, len(self.extract_rules)):\n if self.extract_rules[i]['regex'].search(description):\n rules.append(self.extract_rules[i])\n else:\n rules = self.extract_rules\n\n return rules", "response": "Returns a list of extraction rules that match a given description."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nload extraction rules from the specified file.", "response": "def load_from_file(self, fname):\n '''\n Loads extraction rules from the specified file.\n\n @fname - Path to the extraction rule file.\n\n Returns None.\n '''\n try:\n # Process each line from the extract file, ignoring comments\n with open(fname, 'r') as f:\n for rule in f.readlines():\n self.add_rule(rule.split(self.COMMENT_DELIM, 1)[0])\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n raise Exception(\"Extractor.load_from_file failed to load file '%s': %s\" % (fname, str(e)))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef load_defaults(self):\n '''\n Loads default extraction rules from the user and system extract.conf files.\n\n Returns None.\n '''\n # Load the user extract file first to ensure its rules take precedence.\n extract_files = [\n self.config.settings.user.extract,\n self.config.settings.system.extract,\n ]\n\n for extract_file in extract_files:\n if extract_file:\n try:\n self.load_from_file(extract_file)\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n if binwalk.core.common.DEBUG:\n raise Exception(\"Extractor.load_defaults failed to load file '%s': %s\" % (extract_file, str(e)))", "response": "Loads default extraction rules from the user and system extract. conf files. Returns None."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef build_output_directory(self, path):\n '''\n Set the output directory for extracted files.\n\n @path - The path to the file that data will be extracted from.\n\n Returns None.\n '''\n # If we have not already created an output directory for this target\n # file, create one now\n if not has_key(self.extraction_directories, path):\n basedir = os.path.dirname(path)\n basename = os.path.basename(path)\n\n if basedir != self.directory:\n # During recursive extraction, extracted files will be in subdirectories\n # of the CWD. This allows us to figure out the subdirectory by simply\n # splitting the target file's base directory on our known CWD.\n #\n # However, the very *first* file being scanned is not necessarily in the\n # CWD, so this will raise an IndexError. This is easy to handle though,\n # since the very first file being scanned needs to have its contents\n # extracted to ${CWD}/_basename.extracted, so we just set the subdir\n # variable to a blank string when an IndexError is encountered.\n try:\n subdir = basedir.split(self.directory)[1][1:]\n except IndexError as e:\n subdir = \"\"\n else:\n subdir = \"\"\n\n if self.output_directory_override:\n output_directory = os.path.join(self.directory, subdir, self.output_directory_override)\n else:\n outdir = os.path.join(self.directory, subdir, '_' + basename)\n output_directory = unique_file_name(outdir, extension='extracted')\n\n if not os.path.exists(output_directory):\n os.mkdir(output_directory)\n\n self.extraction_directories[path] = output_directory\n self.output[path].directory = os.path.realpath(output_directory) + os.path.sep\n # Else, just use the already created directory\n else:\n output_directory = self.extraction_directories[path]\n\n return output_directory", "response": "Build the output directory for extracted files."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef extract(self, offset, description, file_name, size, name=None):\n '''\n Extract an embedded file from the target file, if it matches an extract rule.\n Called automatically by Binwalk.scan().\n\n @offset - Offset inside the target file to begin the extraction.\n @description - Description of the embedded file to extract, as returned by libmagic.\n @file_name - Path to the target file.\n @size - Number of bytes to extract.\n @name - Name to save the file as.\n\n Returns the name of the extracted file (blank string if nothing was extracted).\n '''\n fname = ''\n rule = None\n recurse = False\n command_line = ''\n original_dir = os.getcwd()\n rules = self.match(description)\n file_path = os.path.realpath(file_name)\n\n # No extraction rules for this file\n if not rules:\n binwalk.core.common.debug(\"No extraction rules found for '%s'\" % description)\n return (None, None, False, str(None))\n else:\n binwalk.core.common.debug(\"Found %d matching extraction rules\" % len(rules))\n\n # Generate the output directory name where extracted files will be stored\n output_directory = self.build_output_directory(file_name)\n\n # Extract to end of file if no size was specified\n if not size:\n size = file_size(file_path) - offset\n\n if os.path.isfile(file_path):\n binwalk.core.common.debug(\"Changing directory to: %s\" % output_directory)\n os.chdir(output_directory)\n\n # Extract into subdirectories named by the offset\n if self.extract_into_subdirs:\n # Remove trailing L that is added by hex()\n offset_dir = \"0x%X\" % offset\n os.mkdir(offset_dir)\n os.chdir(offset_dir)\n\n # Loop through each extraction rule until one succeeds\n for i in range(0, len(rules)):\n rule = rules[i]\n\n binwalk.core.common.debug(\"Processing extraction rule #%d (%s)\" % (i, str(rule['cmd'])))\n\n # Make sure we don't recurse into any extracted directories if\n # instructed not to\n if rule['recurse'] in [True, False]:\n recurse = rule['recurse']\n else:\n recurse = True\n\n binwalk.core.common.debug(\"Extracting %s[%d:] to %s\" % (file_path, offset, name))\n\n # Copy out the data to disk, if we haven't already\n fname = self._dd(file_path, offset, size, rule['extension'], output_file_name=name)\n\n # If there was a command specified for this rule, try to execute it.\n # If execution fails, the next rule will be attempted.\n if rule['cmd']:\n\n # Note the hash of the original file; if --rm is specified and the\n # extraction utility modifies the original file rather than creating\n # a new one (AFAIK none currently do, but could happen in the future),\n # we don't want to remove this file.\n if self.remove_after_execute:\n fname_md5 = file_md5(fname)\n\n binwalk.core.common.debug(\"Executing extraction command %s\" % (str(rule['cmd'])))\n\n # Execute the specified command against the extracted file\n if self.run_extractors:\n (extract_ok, command_line) = self.execute(rule['cmd'], fname, rule['codes'])\n else:\n extract_ok = True\n command_line = ''\n\n binwalk.core.common.debug(\"Ran extraction command: %s\" % command_line)\n binwalk.core.common.debug(\"Extraction successful: %s\" % extract_ok)\n\n # Only clean up files if remove_after_execute was specified.\n # Only clean up files if the file was extracted sucessfully, or if we've run\n # out of extractors.\n if self.remove_after_execute and (extract_ok == True or i == (len(rules) - 1)):\n\n # Remove the original file that we extracted,\n # if it has not been modified by the extractor.\n try:\n if file_md5(fname) == fname_md5:\n os.unlink(fname)\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n pass\n\n # If the command executed OK, don't try any more rules\n if extract_ok == True:\n break\n # Else, remove the extracted file if this isn't the last rule in the list.\n # If it is the last rule, leave the file on disk for the\n # user to examine.\n elif i != (len(rules) - 1):\n try:\n os.unlink(fname)\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n pass\n\n # If there was no command to execute, just use the first rule\n else:\n break\n\n binwalk.core.common.debug(\"Changing directory back to: %s\" % original_dir)\n os.chdir(original_dir)\n\n return (output_directory, fname, recurse, command_line)", "response": "Extracts an embedded file from the target file."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _entry_offset(self, index, entries, description):\n '''\n Gets the offset of the first entry that matches the description.\n\n @index - Index into the entries list to begin searching.\n @entries - Dictionary of result entries.\n @description - Case insensitive description.\n\n Returns the offset, if a matching description is found.\n Returns -1 if a matching description is not found.\n '''\n description = description.lower()\n\n for (offset, infos) in entries[index:]:\n for info in infos:\n if info['description'].lower().startswith(description):\n return offset\n return -1", "response": "Returns the offset of the first entry that matches the description."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nchecks to see if the provided description string matches an extract rule. Returns the associated rule dictionary ordered by priority.", "response": "def match(self, description):\n '''\n Check to see if the provided description string matches an extract rule.\n Called internally by self.extract().\n\n @description - Description string to check.\n\n Returns the associated rule dictionary if a match is found.\n Returns None if no match is found.\n '''\n rules = []\n ordered_rules = []\n description = description.lower()\n\n for rule in self.extract_rules:\n if rule['regex'].search(description):\n rules.append(rule)\n\n # Plugin rules should take precedence over external extraction commands.\n for rule in rules:\n if callable(rule['cmd']):\n ordered_rules.append(rule)\n for rule in rules:\n if not callable(rule['cmd']):\n ordered_rules.append(rule)\n\n return ordered_rules"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _parse_rule(self, rule):\n '''\n Parses an extraction rule.\n\n @rule - Rule string.\n\n Returns an array of ['', '', '', '', ].\n '''\n values = rule.strip().split(self.RULE_DELIM, 4)\n\n if len(values) >= 4:\n codes = values[3].split(',')\n for i in range(0, len(codes)):\n try:\n codes[i] = int(codes[i], 0)\n except ValueError as e:\n binwalk.core.common.warning(\"The specified return code '%s' for extractor '%s' is not a valid number!\" % (codes[i], values[0]))\n values[3] = codes\n\n if len(values) >= 5:\n values[4] = (values[4].lower() == 'true')\n\n return values", "response": "Parses an extraction rule."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nextract a file embedded inside the target file. @file_name - Path to the target file. @offset - Offset inside the target file where the embedded file begins. @size - Number of bytes to extract. @extension - The file exension to assign to the extracted file on disk. @output_file_name - The requested name of the output file. Returns the extracted file name.", "response": "def _dd(self, file_name, offset, size, extension, output_file_name=None):\n '''\n Extracts a file embedded inside the target file.\n\n @file_name - Path to the target file.\n @offset - Offset inside the target file where the embedded file begins.\n @size - Number of bytes to extract.\n @extension - The file exension to assign to the extracted file on disk.\n @output_file_name - The requested name of the output file.\n\n Returns the extracted file name.\n '''\n total_size = 0\n # Default extracted file name is .\n default_bname = \"%X\" % (offset + self.config.base)\n\n # Make sure the output file name is a string\n if output_file_name is not None:\n output_file_name = str(output_file_name)\n\n if self.max_size and size > self.max_size:\n size = self.max_size\n\n if not output_file_name or output_file_name is None:\n bname = default_bname\n else:\n # Strip the output file name of invalid/dangerous characters (like file paths)\n bname = os.path.basename(output_file_name)\n\n fname = unique_file_name(bname, extension)\n\n try:\n # If byte swapping is enabled, we need to start reading at a swap-size\n # aligned offset, then index in to the read data appropriately.\n if self.config.swap_size:\n adjust = offset % self.config.swap_size\n else:\n adjust = 0\n\n offset -= adjust\n\n # Open the target file and seek to the offset\n fdin = self.config.open_file(file_name)\n fdin.seek(offset)\n\n # Open the output file\n try:\n fdout = BlockFile(fname, 'w')\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n # Fall back to the default name if the requested name fails\n fname = unique_file_name(default_bname, extension)\n fdout = BlockFile(fname, 'w')\n\n while total_size < size:\n (data, dlen) = fdin.read_block()\n if dlen < 1:\n break\n else:\n total_size += (dlen - adjust)\n if total_size > size:\n dlen -= (total_size - size)\n fdout.write(str2bytes(data[adjust:dlen]))\n adjust = 0\n\n # Cleanup\n fdout.close()\n fdin.close()\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n raise Exception(\"Extractor.dd failed to extract data from '%s' to '%s': %s\" %\n (file_name, fname, str(e)))\n\n binwalk.core.common.debug(\"Carved data block 0x%X - 0x%X from '%s' to '%s'\" %\n (offset, offset + size, file_name, fname))\n return fname"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef execute(self, cmd, fname, codes=[0, None]):\n '''\n Execute a command against the specified file.\n\n @cmd - Command to execute.\n @fname - File to run command against.\n @codes - List of return codes indicating cmd success.\n\n Returns True on success, False on failure, or None if the external extraction utility could not be found.\n '''\n tmp = None\n rval = 0\n retval = True\n command_list = []\n\n binwalk.core.common.debug(\"Running extractor '%s'\" % str(cmd))\n\n try:\n if callable(cmd):\n command_list.append(get_class_name_from_method(cmd))\n\n try:\n retval = cmd(fname)\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n binwalk.core.common.warning(\"Internal extractor '%s' failed with exception: '%s'\" % (str(cmd), str(e)))\n elif cmd:\n # If not in debug mode, create a temporary file to redirect\n # stdout and stderr to\n if not binwalk.core.common.DEBUG:\n tmp = tempfile.TemporaryFile()\n\n # Generate unique file paths for all paths in the current\n # command that are surrounded by UNIQUE_PATH_DELIMITER\n while self.UNIQUE_PATH_DELIMITER in cmd:\n need_unique_path = cmd.split(self.UNIQUE_PATH_DELIMITER)[\n 1].split(self.UNIQUE_PATH_DELIMITER)[0]\n unique_path = binwalk.core.common.unique_file_name(need_unique_path)\n cmd = cmd.replace(self.UNIQUE_PATH_DELIMITER + need_unique_path + self.UNIQUE_PATH_DELIMITER, unique_path)\n\n # Execute.\n for command in cmd.split(\"&&\"):\n\n # Replace all instances of FILE_NAME_PLACEHOLDER in the\n # command with fname\n command = command.strip().replace(self.FILE_NAME_PLACEHOLDER, fname)\n\n binwalk.core.common.debug(\"subprocess.call(%s, stdout=%s, stderr=%s)\" % (command, str(tmp), str(tmp)))\n rval = subprocess.call(shlex.split(command), stdout=tmp, stderr=tmp)\n\n if rval in codes:\n retval = True\n else:\n retval = False\n\n binwalk.core.common.debug('External extractor command \"%s\" completed with return code %d (success: %s)' % (cmd, rval, str(retval)))\n command_list.append(command)\n\n # TODO: Should errors from all commands in a command string be checked? Currently we only support\n # specifying one set of error codes, so at the moment, this is not done; it is up to the\n # final command to return success or failure (which presumably it will if previous necessary\n # commands were not successful, but this is an assumption).\n # if retval == False:\n # break\n\n except KeyboardInterrupt as e:\n raise e\n except Exception as e:\n binwalk.core.common.warning(\"Extractor.execute failed to run external extractor '%s': %s, '%s' might not be installed correctly\" % (str(cmd), str(e), str(cmd)))\n retval = None\n\n if tmp is not None:\n tmp.close()\n\n return (retval, '&&'.join(command_list))", "response": "Execute a command against the specified file."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nformats a line of text to fit in the terminal window.", "response": "def _format_line(self, line):\n '''\n Formats a line of text to fit in the terminal window.\n For Tim.\n '''\n delim = '\\n'\n offset = 0\n self.string_parts = []\n\n # Split the line into an array of columns, e.g., ['0', '0x00000000',\n # 'Some description here']\n line_columns = line.split(None, self.num_columns - 1)\n if line_columns:\n # Find where the start of the last column (description) starts in the line of text.\n # All line wraps need to be aligned to this offset.\n offset = line.rfind(line_columns[-1])\n # The delimiter will be a newline followed by spaces padding out\n # the line wrap to the alignment offset.\n delim += ' ' * offset\n\n if line_columns and self.fit_to_screen and len(line) > self.SCREEN_WIDTH:\n # Calculate the maximum length that each wrapped line can be\n max_line_wrap_length = self.SCREEN_WIDTH - offset\n # Append all but the last column to formatted_line\n formatted_line = line[:offset]\n\n # Loop to split up line into multiple max_line_wrap_length pieces\n while len(line[offset:]) > max_line_wrap_length:\n # Find the nearest space to wrap the line at (so we don't split\n # a word across two lines)\n split_offset = line[\n offset:offset + max_line_wrap_length].rfind(' ')\n # If there were no good places to split the line, just truncate\n # it at max_line_wrap_length\n if split_offset < 1:\n split_offset = max_line_wrap_length\n\n self._append_to_data_parts(line, offset, offset + split_offset)\n offset += split_offset\n\n # Add any remaining data (guarunteed to be max_line_wrap_length\n # long or shorter) to self.string_parts\n self._append_to_data_parts(line, offset, offset + len(line[offset:]))\n\n # Append self.string_parts to formatted_line; each part seperated\n # by delim\n formatted_line += delim.join(self.string_parts)\n else:\n formatted_line = line\n\n return formatted_line"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _configure_formatting(self):\n '''\n Configures output formatting, and fitting output to the current terminal width.\n\n Returns None.\n '''\n self.format_strings(self.DEFAULT_FORMAT, self.DEFAULT_FORMAT)\n\n if self.fit_to_screen:\n try:\n import fcntl\n import struct\n import termios\n\n # Get the terminal window width\n hw = struct.unpack('hh', fcntl.ioctl(1, termios.TIOCGWINSZ, '1234'))\n self.SCREEN_WIDTH = self.HEADER_WIDTH = hw[1]\n except KeyboardInterrupt as e:\n raise e\n except Exception:\n pass", "response": "Configures output formatting and fitting output to the current terminal width."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _magic_signature_files(self, system_only=False, user_only=False):\n '''\n Find all user/system magic signature files.\n\n @system_only - If True, only the system magic file directory will be searched.\n @user_only - If True, only the user magic file directory will be searched.\n\n Returns a list of user/system magic signature files.\n '''\n files = []\n user_binarch = self._user_path(self.BINWALK_MAGIC_DIR, self.BINARCH_MAGIC_FILE)\n system_binarch = self._system_path(self.BINWALK_MAGIC_DIR, self.BINARCH_MAGIC_FILE)\n\n def list_files(dir_path):\n # Ignore hidden dotfiles.\n return [os.path.join(dir_path, x) for x in os.listdir(dir_path) if not x.startswith('.')]\n\n if not system_only:\n user_dir = os.path.join(self.user_dir, self.BINWALK_USER_DIR, self.BINWALK_MAGIC_DIR)\n files += list_files(user_dir)\n if not user_only:\n system_dir = os.path.join(self.system_dir, self.BINWALK_MAGIC_DIR)\n files += list_files(system_dir)\n\n # Don't include binarch signatures in the default list of signature files.\n # It is specifically loaded when -A is specified on the command line.\n if user_binarch in files:\n files.remove(user_binarch)\n if system_binarch in files:\n files.remove(system_binarch)\n\n return files", "response": "Return a list of all user and system magic signature files."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nfind the specified magic file in the system or user magic file directories.", "response": "def find_magic_file(self, fname, system_only=False, user_only=False):\n '''\n Finds the specified magic file name in the system / user magic file directories.\n\n @fname - The name of the magic file.\n @system_only - If True, only the system magic file directory will be searched.\n @user_only - If True, only the user magic file directory will be searched.\n\n If system_only and user_only are not set, the user directory is always searched first.\n\n Returns the path to the file on success; returns None on failure.\n '''\n loc = None\n\n if not system_only:\n fpath = self._user_path(self.BINWALK_MAGIC_DIR, fname)\n if os.path.exists(fpath) and common.file_size(fpath) > 0:\n loc = fpath\n\n if loc is None and not user_only:\n fpath = self._system_path(self.BINWALK_MAGIC_DIR, fname)\n if os.path.exists(fpath) and common.file_size(fpath) > 0:\n loc = fpath\n\n return fpath"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets the user s home directory.", "response": "def _get_user_dir(self):\n '''\n Get the user's home directory.\n '''\n try:\n # This should work in both Windows and Unix environments\n for envname in ['USERPROFILE', 'HOME']:\n user_dir = os.getenv(envname)\n if user_dir is not None:\n return user_dir\n except KeyboardInterrupt as e:\n raise e\n except Exception:\n pass\n\n return ''"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nbuild an absolute path and creates the directory and file if they don t already exist.", "response": "def _file_path(self, dirname, filename):\n '''\n Builds an absolute path and creates the directory and file if they don't already exist.\n\n @dirname - Directory path.\n @filename - File name.\n\n Returns a full path of 'dirname/filename'.\n '''\n if not os.path.exists(dirname):\n try:\n os.makedirs(dirname)\n except KeyboardInterrupt as e:\n raise e\n except Exception:\n pass\n\n fpath = os.path.join(dirname, filename)\n\n if not os.path.exists(fpath):\n try:\n open(fpath, \"w\").close()\n except KeyboardInterrupt as e:\n raise e\n except Exception:\n pass\n\n return fpath"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _user_path(self, subdir, basename=''):\n '''\n Gets the full path to the 'subdir/basename' file in the user binwalk directory.\n\n @subdir - Subdirectory inside the user binwalk directory.\n @basename - File name inside the subdirectory.\n\n Returns the full path to the 'subdir/basename' file.\n '''\n try:\n return self._file_path(os.path.join(self.user_dir, self.BINWALK_USER_DIR, subdir), basename)\n except KeyboardInterrupt as e:\n raise e\n except Exception:\n return None", "response": "Gets the full path to the user binwalk file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the full path to the basename file in the system binwalk directory.", "response": "def _system_path(self, subdir, basename=''):\n '''\n Gets the full path to the 'subdir/basename' file in the system binwalk directory.\n\n @subdir - Subdirectory inside the system binwalk directory.\n @basename - File name inside the subdirectory.\n\n Returns the full path to the 'subdir/basename' file.\n '''\n try:\n return self._file_path(os.path.join(self.system_dir, subdir), basename)\n except KeyboardInterrupt as e:\n raise e\n except Exception:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _decrypt_and_extract(self, fname):\n '''\n This does the extraction (e.g., it decrypts the image and writes it to a new file on disk).\n '''\n with open(fname, \"r\") as fp_in:\n encrypted_data = fp_in.read()\n\n decrypted_data = self._hilink_decrypt(encrypted_data)\n\n with open(binwalk.core.common.unique_file_name(fname[:-4], \"dec\"), \"w\") as fp_out:\n fp_out.write(decrypted_data)", "response": "This does the decryption and extraction of the image."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nlock the main thread while the subscription in running", "response": "def run():\n print(\"Environment\", os.environ)\n try:\n os.environ[\"SELENIUM\"]\n except KeyError:\n print(\"Please set the environment variable SELENIUM to Selenium URL\")\n sys.exit(1)\n\n driver = WhatsAPIDriver(client='remote', command_executor=os.environ[\"SELENIUM\"])\n print(\"Waiting for QR\")\n driver.wait_for_login()\n print(\"Bot started\")\n\n driver.subscribe_new_messages(NewMessageObserver())\n print(\"Waiting for new messages...\")\n\n \"\"\" Locks the main thread while the subscription in running \"\"\"\n while True:\n time.sleep(60)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfunctioning to save the firefox profile to the permanant one", "response": "def save_firefox_profile(self, remove_old=False):\n \"\"\"Function to save the firefox profile to the permanant one\"\"\"\n self.logger.info(\"Saving profile from %s to %s\" % (self._profile.path, self._profile_path))\n\n if remove_old:\n if os.path.exists(self._profile_path):\n try:\n shutil.rmtree(self._profile_path)\n except OSError:\n pass\n\n shutil.copytree(os.path.join(self._profile.path), self._profile_path,\n ignore=shutil.ignore_patterns(\"parent.lock\", \"lock\", \".parentlock\"))\n else:\n for item in os.listdir(self._profile.path):\n if item in [\"parent.lock\", \"lock\", \".parentlock\"]:\n continue\n s = os.path.join(self._profile.path, item)\n d = os.path.join(self._profile_path, item)\n if os.path.isdir(s):\n shutil.copytree(s, d,\n ignore=shutil.ignore_patterns(\"parent.lock\", \"lock\", \".parentlock\"))\n else:\n shutil.copy2(s, d)\n\n with open(os.path.join(self._profile_path, self._LOCAL_STORAGE_FILE), 'w') as f:\n f.write(dumps(self.get_local_storage()))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nwaiting for QR to go away", "response": "def wait_for_login(self, timeout=90):\n \"\"\"Waits for the QR to go away\"\"\"\n WebDriverWait(self.driver, timeout).until(\n EC.visibility_of_element_located((By.CSS_SELECTOR, self._SELECTORS['mainPage']))\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting pairing QR code from client", "response": "def get_qr(self, filename=None):\n \"\"\"Get pairing QR code from client\"\"\"\n if \"Click to reload QR code\" in self.driver.page_source:\n self.reload_qr()\n qr = self.driver.find_element_by_css_selector(self._SELECTORS['qrCode'])\n if filename is None:\n fd, fn_png = tempfile.mkstemp(prefix=self.username, suffix='.png')\n else:\n fd = os.open(filename, os.O_RDWR | os.O_CREAT)\n fn_png = os.path.abspath(filename)\n self.logger.debug(\"QRcode image saved at %s\" % fn_png)\n qr.screenshot(fn_png)\n os.close(fd)\n return fn_png"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfetching list of all contacts", "response": "def get_contacts(self):\n \"\"\"\n Fetches list of all contacts\n This will return chats with people from the address book only\n Use get_all_chats for all chats\n\n :return: List of contacts\n :rtype: list[Contact]\n \"\"\"\n all_contacts = self.wapi_functions.getAllContacts()\n return [Contact(contact, self) for contact in all_contacts]"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nfetches list of added contacts", "response": "def get_my_contacts(self):\n \"\"\"\n Fetches list of added contacts\n\n :return: List of contacts\n :rtype: list[Contact]\n \"\"\"\n my_contacts = self.wapi_functions.getMyContacts()\n return [Contact(contact, self) for contact in my_contacts]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfetching all chats in the system", "response": "def get_all_chats(self):\n \"\"\"\n Fetches all chats\n\n :return: List of chats\n :rtype: list[Chat]\n \"\"\"\n chats = self.wapi_functions.getAllChats()\n if chats:\n return [factory_chat(chat, self) for chat in chats]\n else:\n return []"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfetching all unread messages from the server.", "response": "def get_unread(self, include_me=False, include_notifications=False, use_unread_count=False):\n \"\"\"\n Fetches unread messages\n :param include_me: Include user's messages\n :type include_me: bool or None\n :param include_notifications: Include events happening on chat\n :type include_notifications: bool or None\n :param use_unread_count: If set uses chat's 'unreadCount' attribute to fetch last n messages from chat\n :type use_unread_count: bool\n :return: List of unread messages grouped by chats\n :rtype: list[MessageGroup]\n \"\"\"\n raw_message_groups = self.wapi_functions.getUnreadMessages(include_me, include_notifications, use_unread_count)\n\n unread_messages = []\n for raw_message_group in raw_message_groups:\n chat = factory_chat(raw_message_group, self)\n messages = [factory_message(message, self) for message in raw_message_group['messages']]\n messages.sort(key=lambda message: message.timestamp)\n unread_messages.append(MessageGroup(chat, messages))\n\n return unread_messages"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nfetch all messages in a chat", "response": "def get_all_messages_in_chat(self, chat, include_me=False, include_notifications=False):\n \"\"\"\n Fetches messages in chat\n\n :param include_me: Include user's messages\n :type include_me: bool or None\n :param include_notifications: Include events happening on chat\n :type include_notifications: bool or None\n :return: List of messages in chat\n :rtype: list[Message]\n \"\"\"\n message_objs = self.wapi_functions.getAllMessagesInChat(chat.id, include_me, include_notifications)\n\n messages = []\n for message in message_objs:\n yield(factory_message(message, self))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_all_message_ids_in_chat(self, chat, include_me=False, include_notifications=False):\n return self.wapi_functions.getAllMessageIdsInChat(chat.id, include_me, include_notifications)", "response": "Fetches all message ids in a chat"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nfetch a message by its ID", "response": "def get_message_by_id(self, message_id):\n \"\"\"\n Fetch a message\n\n :param message_id: Message ID\n :type message_id: str\n :return: Message or False\n :rtype: Message\n \"\"\"\n result = self.wapi_functions.getMessageById(message_id)\n\n if result:\n result = factory_message(result, self)\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfetch a contact given its ID", "response": "def get_contact_from_id(self, contact_id):\n \"\"\"\n Fetches a contact given its ID\n\n :param contact_id: Contact ID\n :type contact_id: str\n :return: Contact or Error\n :rtype: Contact\n \"\"\"\n contact = self.wapi_functions.getContact(contact_id)\n\n if contact is None:\n raise ContactNotFoundError(\"Contact {0} not found\".format(contact_id))\n\n return Contact(contact, self)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfetching a chat given its ID", "response": "def get_chat_from_id(self, chat_id):\n \"\"\"\n Fetches a chat given its ID\n\n :param chat_id: Chat ID\n :type chat_id: str\n :return: Chat or Error\n :rtype: Chat\n \"\"\"\n chat = self.wapi_functions.getChatById(chat_id)\n if chat:\n return factory_chat(chat, self)\n\n raise ChatNotFoundError(\"Chat {0} not found\".format(chat_id))"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_chat_from_phone_number(self, number, createIfNotFound = False):\n for chat in self.get_all_chats():\n if not isinstance(chat, UserChat) or number not in chat.id:\n continue\n return chat\n if createIfNotFound:\n self.create_chat_by_number(number)\n self.wait_for_login()\n for chat in self.get_all_chats():\n if not isinstance(chat, UserChat) or number not in chat.id:\n continue\n return chat\n\n raise ChatNotFoundError('Chat for phone {0} not found'.format(number))", "response": "Gets chat by phone number"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the status of the driver", "response": "def get_status(self):\n \"\"\"\n Returns status of the driver\n\n :return: Status\n :rtype: WhatsAPIDriverStatus\n \"\"\"\n if self.driver is None:\n return WhatsAPIDriverStatus.NotConnected\n if self.driver.session_id is None:\n return WhatsAPIDriverStatus.NotConnected\n try:\n self.driver.find_element_by_css_selector(self._SELECTORS['mainPage'])\n return WhatsAPIDriverStatus.LoggedIn\n except NoSuchElementException:\n pass\n try:\n self.driver.find_element_by_css_selector(self._SELECTORS['qrCode'])\n return WhatsAPIDriverStatus.NotLoggedIn\n except NoSuchElementException:\n pass\n return WhatsAPIDriverStatus.Unknown"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef contact_get_common_groups(self, contact_id):\n for group in self.wapi_functions.getCommonGroups(contact_id):\n yield factory_chat(group, self)", "response": "Returns the groups common between a user and a contact with given id."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef convert_to_base64(self, path):\n\n mime = magic.Magic(mime=True)\n content_type = mime.from_file(path)\n archive = ''\n with open(path, \"rb\") as image_file:\n archive = b64encode(image_file.read())\n archive = archive.decode('utf-8')\n return 'data:' + content_type + ';base64,' + archive", "response": "converts the file path to base64 and returns the converted string formatted for the send media function"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef send_media(self, path, chatid, caption):\n imgBase64 = self.convert_to_base64(path)\n filename = os.path.split(path)[-1]\n return self.wapi_functions.sendImage(imgBase64, chatid, filename, caption)", "response": "This method is used to send a file to the chat using the sendImage function of wapi. js"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_profile_pic_from_id(self, id):\n profile_pic = self.wapi_functions.getProfilePicFromId(id)\n if profile_pic:\n return b64decode(profile_pic)\n else:\n return False", "response": "Get full profile picture from an ID"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_profile_pic_small_from_id(self, id):\n profile_pic_small = self.wapi_functions.getProfilePicSmallFromId(id)\n if profile_pic:\n return b64decode(profile_pic_small)\n else:\n return False", "response": "Get small profile pic from an ID"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nusing this method to delete a message from a chat", "response": "def delete_message(self, chat_id, message_array, revoke=False):\n \"\"\"\n Delete a chat\n\n :param chat_id: id of chat\n :param message_array: one or more message(s) id\n :param revoke: Set to true so the message will be deleted for everyone, not only you\n :return:\n \"\"\"\n return self.wapi_functions.deleteMessage(chat_id, message_array, revoke=False)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nchecking if a number is valid in the whatsapp service.", "response": "def check_number_status(self, number_id):\n \"\"\"\n Check if a number is valid/registered in the whatsapp service\n\n :param number_id: number id\n :return:\n \"\"\"\n number_status = self.wapi_functions.checkNumberStatus(number_id)\n return NumberStatus(number_status, self)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_unread_messages(self,\n include_me=False,\n include_notifications=False):\n \"\"\"\n I fetch unread messages.\n\n :param include_me: if user's messages are to be included\n :type include_me: bool\n\n :param include_notifications: if events happening on chat are to be included\n :type include_notifications: bool\n\n :return: list of unread messages\n :rtype: list\n \"\"\"\n return list(self.driver.get_unread_messages_in_chat(\n self.id,\n include_me,\n include_notifications\n ))", "response": "I fetch unread messages."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef load_earlier_messages_till(self, last):\n timestamp = time.mktime(last.timetuple())\n self.driver.wapi_functions.loadEarlierMessagesTillDate(self.id, timestamp)", "response": "Triggers loading of messages till a specific point in time\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef factory_message(js_obj, driver):\n if js_obj is None:\n return\n\n if \"lat\" in js_obj and \"lng\" in js_obj and js_obj[\"lat\"] and js_obj[\"lng\"]:\n return GeoMessage(js_obj, driver)\n\n if js_obj[\"isMedia\"]:\n return MediaMessage(js_obj, driver)\n\n if js_obj[\"isNotification\"]:\n return NotificationMessage(js_obj, driver)\n\n if 'isMMS' in js_obj and js_obj[\"isMMS\"]:\n return MMSMessage(js_obj, driver)\n\n if js_obj[\"type\"] in [\"vcard\", \"multi_vcard\"]:\n return VCardMessage(js_obj, driver)\n\n return Message(js_obj, driver)", "response": "Factory function for creating appropriate object given selenium JS object"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ninitial the global logger variable", "response": "def create_logger():\n \"\"\"Initial the global logger variable\"\"\"\n global logger\n\n formatter = logging.Formatter('%(asctime)s|%(levelname)s|%(message)s')\n handler = TimedRotatingFileHandler(log_file, when=\"midnight\", interval=1)\n handler.setFormatter(formatter)\n handler.setLevel(log_level)\n handler.suffix = \"%Y-%m-%d\"\n logger = logging.getLogger(\"sacplus\")\n logger.setLevel(log_level)\n logger.addHandler(handler)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ninitialises a new webwhatsapi object based on the given client_id.", "response": "def init_driver(client_id):\n \"\"\"Initialises a new driver via webwhatsapi module\n \n @param client_id: ID of user client\n @return webwhatsapi object\n \"\"\"\n\n # Create profile directory if it does not exist\n profile_path = CHROME_CACHE_PATH + str(client_id)\n if not os.path.exists(profile_path):\n os.makedirs(profile_path)\n \n # Options to customize chrome window\n chrome_options = [\n 'window-size=' + CHROME_WINDOW_SIZE,\n '--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/60.0.3112.78 Chrome/60.0.3112.78 Safari/537.36'\n ]\n if CHROME_IS_HEADLESS:\n chrome_options.append('--headless')\n if CHROME_DISABLE_GPU:\n chrome_options.append('--disable-gpu')\n \n # Create a whatsapidriver object\n d = WhatsAPIDriver(\n username=client_id, \n profile=profile_path, \n client='chrome', \n chrome_options=chrome_options\n )\n return d"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef delete_client(client_id, preserve_cache):\n if client_id in drivers:\n drivers.pop(client_id).quit()\n try:\n timers[client_id].stop()\n timers[client_id] = None\n release_semaphore(client_id)\n semaphores[client_id] = None\n except:\n pass\n\n if not preserve_cache:\n pth = CHROME_CACHE_PATH + g.client_id\n shutil.rmtree(pth)", "response": "Delete all objects related to a client."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef init_timer(client_id):\n if client_id in timers and timers[client_id]:\n timers[client_id].start()\n return\n # Create a timer to call check_new_message function after every 2 seconds.\n # client_id param is needed to be passed to check_new_message\n timers[client_id] = RepeatedTimer(2, check_new_messages, client_id)", "response": "Create a timer for the client driver to watch for events\n \n "} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncheck for new unread messages and send them to the custom api", "response": "def check_new_messages(client_id):\n \"\"\"Check for new unread messages and send them to the custom api\n\n @param client_id: ID of client user\n \"\"\"\n # Return if driver is not defined or if whatsapp is not logged in.\n # Stop the timer as well\n if client_id not in drivers or not drivers[client_id] or not drivers[client_id].is_logged_in():\n timers[client_id].stop()\n return\n\n # Acquire a lock on thread\n if not acquire_semaphore(client_id, True):\n return\n\n try:\n # Get all unread messages\n res = drivers[client_id].get_unread()\n # Mark all of them as seen\n for message_group in res:\n message_group.chat.send_seen()\n # Release thread lock\n release_semaphore(client_id)\n # If we have new messages, do something with it\n if res:\n print(res)\n except:\n pass\n finally:\n # Release lock anyway, safekeeping\n release_semaphore(client_id)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget the status of a perticular client.", "response": "def get_client_info(client_id):\n \"\"\"Get the status of a perticular client, as to he/she is connected or not\n \n @param client_id: ID of client user\n @return JSON object {\n \"driver_status\": webdriver status\n \"is_alive\": if driver is active or not\n \"is_logged_in\": if user is logged in or not\n \"is_timer\": if timer is running or not\n }\n \"\"\"\n if client_id not in drivers:\n return None\n\n driver_status = drivers[client_id].get_status()\n is_alive = False\n is_logged_in = False\n if (driver_status == WhatsAPIDriverStatus.NotLoggedIn\n or driver_status == WhatsAPIDriverStatus.LoggedIn):\n is_alive = True\n if driver_status == WhatsAPIDriverStatus.LoggedIn:\n is_logged_in = True\n \n return {\n \"is_alive\": is_alive,\n \"is_logged_in\": is_logged_in,\n \"is_timer\": bool(timers[client_id]) and timers[client_id].is_running\n }"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef create_static_profile_path(client_id):\n profile_path = os.path.join(STATIC_FILES_PATH, str(client_id))\n if not os.path.exists(profile_path):\n os.makedirs(profile_path)\n return profile_path", "response": "Create a profile path folder if not exist"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef before_request():\n global logger\n \n if not request.url_rule:\n abort(404)\n\n if logger == None:\n create_logger()\n logger.info(\"API call \" + request.method + \" \" + request.url)\n\n auth_key = request.headers.get('auth-key')\n g.client_id = request.headers.get('client_id')\n rule_parent = request.url_rule.rule.split('/')[1]\n \n if API_KEY and auth_key != API_KEY:\n abort(401, 'you must send valid auth-key')\n raise Exception()\n\n if not g.client_id and rule_parent != 'admin':\n abort(400, 'client ID is mandatory')\n\n acquire_semaphore(g.client_id)\n\n # Create a driver object if not exist for client requests.\n if rule_parent != 'admin':\n if g.client_id not in drivers:\n drivers[g.client_id] = init_client(g.client_id)\n \n g.driver = drivers[g.client_id]\n g.driver_status = WhatsAPIDriverStatus.Unknown\n \n if g.driver is not None:\n g.driver_status = g.driver.get_status()\n \n # If driver status is unkown, means driver has closed somehow, reopen it\n if (g.driver_status != WhatsAPIDriverStatus.NotLoggedIn\n and g.driver_status != WhatsAPIDriverStatus.LoggedIn):\n drivers[g.client_id] = init_client(g.client_id)\n g.driver_status = g.driver.get_status()\n \n init_timer(g.client_id)", "response": "This function runs before every API request. It creates a new object and checks for required prerequisites and sets global variables for other functions to use."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a new client driver.", "response": "def create_client():\n \"\"\"Create a new client driver. The driver is automatically created in \n before_request function.\"\"\"\n result = False\n if g.client_id in drivers:\n result = True\n return jsonify({'Success': result})"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef delete_client():\n preserve_cache = request.args.get('preserve_cache', False)\n delete_client(g.client_id, preserve_cache)\n return jsonify({'Success': True})", "response": "Delete all objects related to client"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncapture chrome screen image and send it back.", "response": "def get_screen():\n \"\"\"Capture chrome screen image and send it back. If the screen is currently \n at qr scanning phase, return the image of qr only, else return image of full\n screen\"\"\"\n img_title = 'screen_' + g.client_id + '.png'\n image_path = STATIC_FILES_PATH + img_title\n if g.driver_status != WhatsAPIDriverStatus.LoggedIn:\n try:\n g.driver.get_qr(image_path)\n return send_file(image_path, mimetype='image/png')\n except Exception as err:\n pass\n g.driver.screenshot(image_path)\n return send_file(image_path, mimetype='image/png')"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget all unread messages", "response": "def get_unread_messages():\n \"\"\"Get all unread messages\"\"\"\n mark_seen = request.args.get('mark_seen', True)\n unread_msg = g.driver.get_unread()\n\n if mark_seen:\n for msg in unread_msg:\n msg.chat.send_seen()\n\n return jsonify(unread_msg)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_messages(chat_id):\n\n mark_seen = request.args.get('mark_seen', True)\n\n chat = g.driver.get_chat_from_id(chat_id)\n msgs = list(g.driver.get_all_messages_in_chat(chat))\n\n for msg in msgs:\n print(msg.id)\n\n if mark_seen:\n for msg in msgs:\n try:\n msg.chat.send_seen()\n except:\n pass\n \n return jsonify(msgs)", "response": "Return all of the chat messages in a single chat"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsends a message to a chat", "response": "def send_message(chat_id):\n \"\"\"Send a message to a chat\n If a media file is found, send_media is called, else a simple text message\n is sent\n \"\"\"\n\n files = request.files\n\n if files:\n res = send_media(chat_id, request)\n else:\n message = request.form.get('message')\n res = g.driver.chat_send_message(chat_id, message)\n\n if res:\n return jsonify(res)\n else:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndownload a media file from the message", "response": "def download_message_media(msg_id):\n \"\"\"Download a media file\"\"\"\n message = g.driver.get_message_by_id(msg_id)\n\n if not message or not message.mime:\n abort(404)\n\n profile_path = create_static_profile_path(g.client_id)\n filename = message.save_media(profile_path, True)\n\n if os.path.exists(filename):\n return send_file(filename, mimetype=message.mime)\n\n abort(404)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_active_clients():\n global drivers\n\n if not drivers:\n return jsonify([])\n\n result = {client: get_client_info(client) for client in drivers}\n return jsonify(result)", "response": "Get a list of all active clients and their status"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef run_clients():\n clients = request.form.get('clients')\n if not clients:\n return jsonify({'Error': 'no clients provided'})\n\n result = {}\n for client_id in clients.split(','):\n if client_id not in drivers:\n init_client(client_id)\n init_timer(client_id)\n\n result[client_id] = get_client_info(client_id)\n\n return jsonify(result)", "response": "Create driver for all clients"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef kill_clients():\n clients = request.form.get('clients').split(',')\n kill_dead = request.args.get('kill_dead', default=False)\n kill_dead = kill_dead and kill_dead in ['true', '1']\n\n if not kill_dead and not clients:\n return jsonify({'Error': 'no clients provided'})\n\n for client in list(drivers.keys()):\n if kill_dead and not drivers[client].is_logged_in() or client in clients:\n drivers.pop(client).quit()\n try:\n timers[client].stop()\n timers[client] = None\n release_semaphore(client)\n semaphores[client] = None\n except:\n pass\n\n return get_active_clients()", "response": "Force kill all the clients and all the others"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef driver_needed(func):\n\n def wrapped(self, *args):\n if not self.driver:\n raise AttributeError(\"No driver passed to object\")\n\n return func(self, *args)\n\n return wrapped", "response": "Decorator for WhatsappObjectWithId methods that need to communicate with the browser"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef dist_dir(self):\n '''The dist dir at which to place the finished distribution.'''\n if self.distribution is None:\n warning('Tried to access {}.dist_dir, but {}.distribution '\n 'is None'.format(self, self))\n exit(1)\n return self.distribution.dist_dir", "response": "The dist dir at which to place the finished distribution."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nchecking what recipes are being built to see which of the alternative and optional dependencies are being used and returns a list of these.", "response": "def check_recipe_choices(self):\n '''Checks what recipes are being built to see which of the alternative\n and optional dependencies are being used,\n and returns a list of these.'''\n recipes = []\n built_recipes = self.ctx.recipe_build_order\n for recipe in self.recipe_depends:\n if isinstance(recipe, (tuple, list)):\n for alternative in recipe:\n if alternative in built_recipes:\n recipes.append(alternative)\n break\n return sorted(recipes)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef prepare_build_dir(self):\n '''Ensure that a build dir exists for the recipe. This same single\n dir will be used for building all different archs.'''\n self.build_dir = self.get_build_dir()\n self.common_dir = self.get_common_dir()\n copy_files(join(self.bootstrap_dir, 'build'), self.build_dir)\n copy_files(join(self.common_dir, 'build'), self.build_dir,\n override=False)\n if self.ctx.symlink_java_src:\n info('Symlinking java src instead of copying')\n shprint(sh.rm, '-r', join(self.build_dir, 'src'))\n shprint(sh.mkdir, join(self.build_dir, 'src'))\n for dirn in listdir(join(self.bootstrap_dir, 'build', 'src')):\n shprint(sh.ln, '-s', join(self.bootstrap_dir, 'build', 'src', dirn),\n join(self.build_dir, 'src'))\n with current_directory(self.build_dir):\n with open('project.properties', 'w') as fileh:\n fileh.write('target=android-{}'.format(self.ctx.android_api))", "response": "Ensure that a build dir exists for the recipe. This same single\n dir will be used for building all different archs."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfinding all the available bootstraps and return them.", "response": "def list_bootstraps(cls):\n '''Find all the available bootstraps and return them.'''\n forbidden_dirs = ('__pycache__', 'common')\n bootstraps_dir = join(dirname(__file__), 'bootstraps')\n for name in listdir(bootstraps_dir):\n if name in forbidden_dirs:\n continue\n filen = join(bootstraps_dir, name)\n if isdir(filen):\n yield name"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_bootstrap_from_recipes(cls, recipes, ctx):\n '''Returns a bootstrap whose recipe requirements do not conflict with\n the given recipes.'''\n info('Trying to find a bootstrap that matches the given recipes.')\n bootstraps = [cls.get_bootstrap(name, ctx)\n for name in cls.list_bootstraps()]\n acceptable_bootstraps = []\n for bs in bootstraps:\n if not bs.can_be_chosen_automatically:\n continue\n possible_dependency_lists = expand_dependencies(bs.recipe_depends)\n for possible_dependencies in possible_dependency_lists:\n ok = True\n for recipe in possible_dependencies:\n recipe = Recipe.get_recipe(recipe, ctx)\n if any([conflict in recipes for conflict in recipe.conflicts]):\n ok = False\n break\n for recipe in recipes:\n try:\n recipe = Recipe.get_recipe(recipe, ctx)\n except ValueError:\n conflicts = []\n else:\n conflicts = recipe.conflicts\n if any([conflict in possible_dependencies\n for conflict in conflicts]):\n ok = False\n break\n if ok and bs not in acceptable_bootstraps:\n acceptable_bootstraps.append(bs)\n info('Found {} acceptable bootstraps: {}'.format(\n len(acceptable_bootstraps),\n [bs.name for bs in acceptable_bootstraps]))\n if acceptable_bootstraps:\n info('Using the first of these: {}'\n .format(acceptable_bootstraps[0].name))\n return acceptable_bootstraps[0]\n return None", "response": "Returns a bootstrap that matches the given recipes."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_bootstrap(cls, name, ctx):\n '''Returns an instance of a bootstrap with the given name.\n\n This is the only way you should access a bootstrap class, as\n it sets the bootstrap directory correctly.\n '''\n if name is None:\n return None\n if not hasattr(cls, 'bootstraps'):\n cls.bootstraps = {}\n if name in cls.bootstraps:\n return cls.bootstraps[name]\n mod = importlib.import_module('pythonforandroid.bootstraps.{}'\n .format(name))\n if len(logger.handlers) > 1:\n logger.removeHandler(logger.handlers[1])\n bootstrap = mod.bootstrap\n bootstrap.bootstrap_dir = join(ctx.root_dir, 'bootstraps', name)\n bootstrap.ctx = ctx\n return bootstrap", "response": "Returns an instance of a bootstrap with the given name."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef distribute_libs(self, arch, src_dirs, wildcard='*', dest_dir=\"libs\"):\n '''Copy existing arch libs from build dirs to current dist dir.'''\n info('Copying libs')\n tgt_dir = join(dest_dir, arch.arch)\n ensure_dir(tgt_dir)\n for src_dir in src_dirs:\n for lib in glob.glob(join(src_dir, wildcard)):\n shprint(sh.cp, '-a', lib, tgt_dir)", "response": "Copy existing arch libs from build dirs to current dist dir."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncopies existing javaclasses from build dir to current dist dir.", "response": "def distribute_javaclasses(self, javaclass_dir, dest_dir=\"src\"):\n '''Copy existing javaclasses from build dir to current dist dir.'''\n info('Copying java files')\n ensure_dir(dest_dir)\n for filename in glob.glob(javaclass_dir):\n shprint(sh.cp, '-a', filename, dest_dir)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef distribute_aars(self, arch):\n '''Process existing .aar bundles and copy to current dist dir.'''\n info('Unpacking aars')\n for aar in glob.glob(join(self.ctx.aars_dir, '*.aar')):\n self._unpack_aar(aar, arch)", "response": "Process existing. aar bundles and copy to current dist dir."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nunpacks content of. aar bundle and copy to current dist dir.", "response": "def _unpack_aar(self, aar, arch):\n '''Unpack content of .aar bundle and copy to current dist dir.'''\n with temp_directory() as temp_dir:\n name = splitext(basename(aar))[0]\n jar_name = name + '.jar'\n info(\"unpack {} aar\".format(name))\n debug(\" from {}\".format(aar))\n debug(\" to {}\".format(temp_dir))\n shprint(sh.unzip, '-o', aar, '-d', temp_dir)\n\n jar_src = join(temp_dir, 'classes.jar')\n jar_tgt = join('libs', jar_name)\n debug(\"copy {} jar\".format(name))\n debug(\" from {}\".format(jar_src))\n debug(\" to {}\".format(jar_tgt))\n ensure_dir('libs')\n shprint(sh.cp, '-a', jar_src, jar_tgt)\n\n so_src_dir = join(temp_dir, 'jni', arch.arch)\n so_tgt_dir = join('libs', arch.arch)\n debug(\"copy {} .so\".format(name))\n debug(\" from {}\".format(so_src_dir))\n debug(\" to {}\".format(so_tgt_dir))\n ensure_dir(so_tgt_dir)\n so_files = glob.glob(join(so_src_dir, '*.so'))\n for f in so_files:\n shprint(sh.cp, '-a', f, so_tgt_dir)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a set of modified recipes between the current branch and the one in param.", "response": "def modified_recipes(branch='origin/master'):\n \"\"\"\n Returns a set of modified recipes between the current branch and the one\n in param.\n \"\"\"\n # using the contrib version on purpose rather than sh.git, since it comes\n # with a bunch of fixes, e.g. disabled TTY, see:\n # https://stackoverflow.com/a/20128598/185510\n git_diff = sh.contrib.git.diff('--name-only', branch)\n recipes = set()\n for file_path in git_diff:\n if 'pythonforandroid/recipes/' in file_path:\n recipe = file_path.split('/')[2]\n recipes.add(recipe)\n return recipes"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef build(target_python, requirements):\n if not requirements:\n return\n testapp = 'setup_testapp_python2.py'\n android_sdk_home = os.environ['ANDROID_SDK_HOME']\n android_ndk_home = os.environ['ANDROID_NDK_HOME']\n if target_python == TargetPython.python3:\n testapp = 'setup_testapp_python3_sqlite_openssl.py'\n requirements.add(target_python.name)\n requirements = ','.join(requirements)\n logger.info('requirements: {}'.format(requirements))\n with current_directory('testapps/'):\n # iterates to stream the output\n for line in sh.python(\n testapp, 'apk', '--sdk-dir', android_sdk_home,\n '--ndk-dir', android_ndk_home, '--bootstrap', 'sdl2', '--requirements',\n requirements, _err_to_out=True, _iter=True):\n print(line)", "response": "Builds an APK given a target Python and a set of requirements."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_recipe_env(self, arch=None, with_flags_in_cc=True):\n env = super(GeventRecipe, self).get_recipe_env(arch, with_flags_in_cc)\n # CFLAGS may only be used to specify C compiler flags, for macro definitions use CPPFLAGS\n regex = re.compile(r'(?:\\s|^)-[DI][\\S]+')\n env['CPPFLAGS'] = ''.join(re.findall(regex, env['CFLAGS'])).strip()\n env['CFLAGS'] = re.sub(regex, '', env['CFLAGS'])\n info('Moved \"{}\" from CFLAGS to CPPFLAGS.'.format(env['CPPFLAGS']))\n # LDFLAGS may only be used to specify linker flags, for libraries use LIBS\n regex = re.compile(r'(?:\\s|^)-l[\\w\\.]+')\n env['LIBS'] = ''.join(re.findall(regex, env['LDFLAGS'])).strip()\n env['LDFLAGS'] = re.sub(regex, '', env['LDFLAGS'])\n info('Moved \"{}\" from LDFLAGS to LIBS.'.format(env['LIBS']))\n return env", "response": "Returns the environment dictionary for the gevent recipe."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef extract_from_ast(node, gettext_functions=GETTEXT_FUNCTIONS,\n babel_style=True):\n \"\"\"Extract localizable strings from the given template node. Per\n default this function returns matches in babel style that means non string\n parameters as well as keyword arguments are returned as `None`. This\n allows Babel to figure out what you really meant if you are using\n gettext functions that allow keyword arguments for placeholder expansion.\n If you don't want that behavior set the `babel_style` parameter to `False`\n which causes only strings to be returned and parameters are always stored\n in tuples. As a consequence invalid gettext calls (calls without a single\n string parameter or string parameters after non-string parameters) are\n skipped.\n\n This example explains the behavior:\n\n >>> from jinja2 import Environment\n >>> env = Environment()\n >>> node = env.parse('{{ (_(\"foo\"), _(), ngettext(\"foo\", \"bar\", 42)) }}')\n >>> list(extract_from_ast(node))\n [(1, '_', 'foo'), (1, '_', ()), (1, 'ngettext', ('foo', 'bar', None))]\n >>> list(extract_from_ast(node, babel_style=False))\n [(1, '_', ('foo',)), (1, 'ngettext', ('foo', 'bar'))]\n\n For every string found this function yields a ``(lineno, function,\n message)`` tuple, where:\n\n * ``lineno`` is the number of the line on which the string was found,\n * ``function`` is the name of the ``gettext`` function used (if the\n string was extracted from embedded Python code), and\n * ``message`` is the string itself (a ``unicode`` object, or a tuple\n of ``unicode`` objects for functions with multiple string arguments).\n\n This extraction function operates on the AST and is because of that unable\n to extract any comments. For comment support you have to use the babel\n extraction interface or extract comments yourself.\n \"\"\"\n for node in node.find_all(nodes.Call):\n if not isinstance(node.node, nodes.Name) or \\\n node.node.name not in gettext_functions:\n continue\n\n strings = []\n for arg in node.args:\n if isinstance(arg, nodes.Const) and \\\n isinstance(arg.value, basestring):\n strings.append(arg.value)\n else:\n strings.append(None)\n\n for arg in node.kwargs:\n strings.append(None)\n if node.dyn_args is not None:\n strings.append(None)\n if node.dyn_kwargs is not None:\n strings.append(None)\n\n if not babel_style:\n strings = tuple(x for x in strings if x is not None)\n if not strings:\n continue\n else:\n if len(strings) == 1:\n strings = strings[0]\n else:\n strings = tuple(strings)\n yield node.lineno, node.node.name, strings", "response": "Extract localizable strings from the given template node."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef babel_extract(fileobj, keywords, comment_tags, options):\n extensions = set()\n for extension in options.get('extensions', '').split(','):\n extension = extension.strip()\n if not extension:\n continue\n extensions.add(import_string(extension))\n if InternationalizationExtension not in extensions:\n extensions.add(InternationalizationExtension)\n\n environment = get_spontaneous_environment(\n options.get('block_start_string', BLOCK_START_STRING),\n options.get('block_end_string', BLOCK_END_STRING),\n options.get('variable_start_string', VARIABLE_START_STRING),\n options.get('variable_end_string', VARIABLE_END_STRING),\n options.get('comment_start_string', COMMENT_START_STRING),\n options.get('comment_end_string', COMMENT_END_STRING),\n options.get('line_statement_prefix') or LINE_STATEMENT_PREFIX,\n options.get('line_comment_prefix') or LINE_COMMENT_PREFIX,\n str(options.get('trim_blocks', TRIM_BLOCKS)).lower() in \\\n ('1', 'on', 'yes', 'true'),\n NEWLINE_SEQUENCE, frozenset(extensions),\n # fill with defaults so that environments are shared\n # with other spontaneus environments. The rest of the\n # arguments are optimizer, undefined, finalize, autoescape,\n # loader, cache size, auto reloading setting and the\n # bytecode cache\n True, Undefined, None, False, None, 0, False, None\n )\n\n source = fileobj.read().decode(options.get('encoding', 'utf-8'))\n try:\n node = environment.parse(source)\n tokens = list(environment.lex(environment.preprocess(source)))\n except TemplateSyntaxError as e:\n # skip templates with syntax errors\n return\n\n finder = _CommentFinder(tokens, comment_tags)\n for lineno, func, message in extract_from_ast(node, keywords):\n yield lineno, func, message, finder.find_comments(lineno)", "response": "This method is used to extract a set of translation functions from a file - like object."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef parse(self, parser):\n lineno = next(parser.stream).lineno\n\n # find all the variables referenced. Additionally a variable can be\n # defined in the body of the trans block too, but this is checked at\n # a later state.\n plural_expr = None\n variables = {}\n while parser.stream.current.type != 'block_end':\n if variables:\n parser.stream.expect('comma')\n\n # skip colon for python compatibility\n if parser.stream.skip_if('colon'):\n break\n\n name = parser.stream.expect('name')\n if name.value in variables:\n parser.fail('translatable variable %r defined twice.' %\n name.value, name.lineno,\n exc=TemplateAssertionError)\n\n # expressions\n if parser.stream.current.type == 'assign':\n next(parser.stream)\n variables[name.value] = var = parser.parse_expression()\n else:\n variables[name.value] = var = nodes.Name(name.value, 'load')\n if plural_expr is None:\n plural_expr = var\n\n parser.stream.expect('block_end')\n\n plural = plural_names = None\n have_plural = False\n referenced = set()\n\n # now parse until endtrans or pluralize\n singular_names, singular = self._parse_block(parser, True)\n if singular_names:\n referenced.update(singular_names)\n if plural_expr is None:\n plural_expr = nodes.Name(singular_names[0], 'load')\n\n # if we have a pluralize block, we parse that too\n if parser.stream.current.test('name:pluralize'):\n have_plural = True\n next(parser.stream)\n if parser.stream.current.type != 'block_end':\n name = parser.stream.expect('name')\n if name.value not in variables:\n parser.fail('unknown variable %r for pluralization' %\n name.value, name.lineno,\n exc=TemplateAssertionError)\n plural_expr = variables[name.value]\n parser.stream.expect('block_end')\n plural_names, plural = self._parse_block(parser, False)\n next(parser.stream)\n referenced.update(plural_names)\n else:\n next(parser.stream)\n\n # register free names as simple name expressions\n for var in referenced:\n if var not in variables:\n variables[var] = nodes.Name(var, 'load')\n\n # no variables referenced? no need to escape\n if not referenced:\n singular = singular.replace('%%', '%')\n if plural:\n plural = plural.replace('%%', '%')\n\n if not have_plural:\n plural_expr = None\n elif plural_expr is None:\n parser.fail('pluralize without variables', lineno)\n\n if variables:\n variables = nodes.Dict([nodes.Pair(nodes.Const(x, lineno=lineno), y)\n for x, y in variables.items()])\n else:\n variables = None\n\n node = self._make_node(singular, plural, variables, plural_expr)\n node.set_lineno(lineno)\n return node", "response": "Parse a translatable tag."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _make_node(self, singular, plural, variables, plural_expr):\n # singular only:\n if plural_expr is None:\n gettext = nodes.Name('gettext', 'load')\n node = nodes.Call(gettext, [nodes.Const(singular)],\n [], None, None)\n\n # singular and plural\n else:\n ngettext = nodes.Name('ngettext', 'load')\n node = nodes.Call(ngettext, [\n nodes.Const(singular),\n nodes.Const(plural),\n plural_expr\n ], [], None, None)\n\n # mark the return value as safe if we are in an\n # environment with autoescaping turned on\n if self.environment.autoescape:\n node = nodes.MarkSafe(node)\n\n if variables:\n node = nodes.Mod(node, variables)\n return nodes.Output([node])", "response": "Generates a useful node from the data provided."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef load_extensions(environment, extensions):\n result = {}\n for extension in extensions:\n if isinstance(extension, basestring):\n extension = import_string(extension)\n result[extension.identifier] = extension(environment)\n return result", "response": "Load the extensions from the list and bind it to the environment."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef extend(self, **attributes):\n for key, value in attributes.iteritems():\n if not hasattr(self, key):\n setattr(self, key, value)", "response": "Add the items to the instance of the environment if they do not exist yet."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncompiles a node or template source code.", "response": "def compile(self, source, name=None, filename=None, raw=False,\n defer_init=False):\n \"\"\"Compile a node or template source code. The `name` parameter is\n the load name of the template after it was joined using\n :meth:`join_path` if necessary, not the filename on the file system.\n the `filename` parameter is the estimated filename of the template on\n the file system. If the template came from a database or memory this\n can be omitted.\n\n The return value of this method is a python code object. If the `raw`\n parameter is `True` the return value will be a string with python\n code equivalent to the bytecode returned otherwise. This method is\n mainly used internally.\n\n `defer_init` is use internally to aid the module code generator. This\n causes the generated code to be able to import without the global\n environment variable to be set.\n\n .. versionadded:: 2.4\n `defer_init` parameter added.\n \"\"\"\n source_hint = None\n try:\n if isinstance(source, basestring):\n source_hint = source\n source = self._parse(source, name, filename)\n if self.optimized:\n source = optimize(source, self)\n source = generate(source, self, name, filename,\n defer_init=defer_init)\n if raw:\n return source\n if filename is None:\n filename = '