{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Ethernet Header and Frame" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from byteclasses.handlers.network.eth_hdr import EthHdr\n", "from byteclasses.handlers.network.eth_frame import EthFrame\n", "from byteclasses.print import byteclass_inspect, collection_table, byteclass_info\n", "from rich.console import Console\n", "from rich.table import Table" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "eth_hdr = EthHdr()\n", "data = bytearray(b\"\\x00\\x0c\\x29\\xb4\\x90\\x14\\xec\\xf4\\xbb\\x96\\x12\\x0e\\x08\\x00\")\n", "eth_hdr.attach(memoryview(data))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
                                                  Byteclass Info                                                   \n",
       "┏━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n",
       "┃ Property  Value                                                                                                ┃\n",
       "┡━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n",
       "│ type()    EthHdr                                                                                               │\n",
       "│ mro       EthHdr -> object                                                                                     │\n",
       "│ len()     14                                                                                                   │\n",
       "│ str()     EthHdr(dst_mac=[UInt8(0), UInt8(12), UInt8(41), UInt8(180), UInt8(144), UInt8(20)],                  │\n",
       "│           src_mac=[UInt8(236), UInt8(244), UInt8(187), UInt8(150), UInt8(18), UInt8(14)],                      │\n",
       "│           ether_type=UInt16(2048))                                                                             │\n",
       "│ repr()    EthHdr(dst_mac=[UInt8(0), UInt8(12), UInt8(41), UInt8(180), UInt8(144), UInt8(20)],                  │\n",
       "│           src_mac=[UInt8(236), UInt8(244), UInt8(187), UInt8(150), UInt8(18), UInt8(14)],                      │\n",
       "│           ether_type=UInt16(2048))                                                                             │\n",
       "│ .data     bytearray(b'\\x00\\x0c)\\xb4\\x90\\x14\\xec\\xf4\\xbb\\x96\\x12\\x0e\\x08\\x00')                                  │\n",
       "└──────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────┘\n",
       "
\n" ], "text/plain": [ "\u001b[3m Byteclass Info \u001b[0m\n", "┏━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", "┃\u001b[1m \u001b[0m\u001b[1mProperty\u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mValue \u001b[0m\u001b[1m \u001b[0m┃\n", "┡━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", "│\u001b[90m \u001b[0m\u001b[1;90mtype() \u001b[0m\u001b[90m \u001b[0m│\u001b[90m \u001b[0m\u001b[90mEthHdr \u001b[0m\u001b[90m \u001b[0m│\n", "│\u001b[91m \u001b[0m\u001b[1;91mmro \u001b[0m\u001b[91m \u001b[0m│\u001b[91m \u001b[0m\u001b[91mEthHdr -> object \u001b[0m\u001b[91m \u001b[0m│\n", "│\u001b[92m \u001b[0m\u001b[1;92mlen() \u001b[0m\u001b[92m \u001b[0m│\u001b[92m \u001b[0m\u001b[92m14 \u001b[0m\u001b[92m \u001b[0m│\n", "│\u001b[93m \u001b[0m\u001b[1;93mstr() \u001b[0m\u001b[93m \u001b[0m│\u001b[93m \u001b[0m\u001b[93mEthHdr(dst_mac=[UInt8(0), UInt8(12), UInt8(41), UInt8(180), UInt8(144), UInt8(20)], \u001b[0m\u001b[93m \u001b[0m│\n", "│\u001b[93m \u001b[0m│\u001b[93m \u001b[0m\u001b[93msrc_mac=[UInt8(236), UInt8(244), UInt8(187), UInt8(150), UInt8(18), UInt8(14)], \u001b[0m\u001b[93m \u001b[0m│\n", "│\u001b[93m \u001b[0m│\u001b[93m \u001b[0m\u001b[93mether_type=UInt16(2048)) \u001b[0m\u001b[93m \u001b[0m│\n", "│\u001b[94m \u001b[0m\u001b[1;94mrepr() \u001b[0m\u001b[94m \u001b[0m│\u001b[94m \u001b[0m\u001b[94mEthHdr(dst_mac=[UInt8(0), UInt8(12), UInt8(41), UInt8(180), UInt8(144), UInt8(20)], \u001b[0m\u001b[94m \u001b[0m│\n", "│\u001b[94m \u001b[0m│\u001b[94m \u001b[0m\u001b[94msrc_mac=[UInt8(236), UInt8(244), UInt8(187), UInt8(150), UInt8(18), UInt8(14)], \u001b[0m\u001b[94m \u001b[0m│\n", "│\u001b[94m \u001b[0m│\u001b[94m \u001b[0m\u001b[94mether_type=UInt16(2048)) \u001b[0m\u001b[94m \u001b[0m│\n", "│\u001b[95m \u001b[0m\u001b[1;95m.data \u001b[0m\u001b[95m \u001b[0m│\u001b[95m \u001b[0m\u001b[95mbytearray(b'\\x00\\x0c)\\xb4\\x90\\x14\\xec\\xf4\\xbb\\x96\\x12\\x0e\\x08\\x00') \u001b[0m\u001b[95m \u001b[0m│\n", "└──────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────┘\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "byteclass_info(eth_hdr)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
╭───────────────────────────────────────── Byteclass Inspect ──────────────────────────────────────────╮\n",
       "│     00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f  │\n",
       "│ ---------------------------------------------------------------------------------------------------  │\n",
       "│ 0x0 00 0c 29 b4 90 14ec f4 bb 96 12 0e08 00                                                          │\n",
       "╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
       "
\n" ], "text/plain": [ "╭───────────────────────────────────────── Byteclass Inspect ──────────────────────────────────────────╮\n", "│ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f │\n", "│ --------------------------------------------------------------------------------------------------- │\n", "│ 0x0 \u001b[90m00 0c 29 b4 90 14\u001b[0m\u001b[91mec f4 bb 96 12 0e\u001b[0m\u001b[92m08 00\u001b[0m │\n", "╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
                                        Legend                                         \n",
       "┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n",
       "┃ Member      Value                                                                  ┃\n",
       "┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n",
       "│ dst_mac     [UInt8(0), UInt8(12), UInt8(41), UInt8(180), UInt8(144), UInt8(20)]    │\n",
       "│ src_mac     [UInt8(236), UInt8(244), UInt8(187), UInt8(150), UInt8(18), UInt8(14)] │\n",
       "│ ether_type  2048                                                                   │\n",
       "└────────────┴────────────────────────────────────────────────────────────────────────┘\n",
       "
\n" ], "text/plain": [ "\u001b[3m Legend \u001b[0m\n", "┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", "┃\u001b[1m \u001b[0m\u001b[1mMember \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mValue \u001b[0m\u001b[1m \u001b[0m┃\n", "┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", "│\u001b[90m \u001b[0m\u001b[1;90mdst_mac \u001b[0m\u001b[90m \u001b[0m│\u001b[90m \u001b[0m\u001b[90m[UInt8(0), UInt8(12), UInt8(41), UInt8(180), UInt8(144), UInt8(20)] \u001b[0m\u001b[90m \u001b[0m│\n", "│\u001b[91m \u001b[0m\u001b[1;91msrc_mac \u001b[0m\u001b[91m \u001b[0m│\u001b[91m \u001b[0m\u001b[91m[UInt8(236), UInt8(244), UInt8(187), UInt8(150), UInt8(18), UInt8(14)]\u001b[0m\u001b[91m \u001b[0m│\n", "│\u001b[92m \u001b[0m\u001b[1;92mether_type\u001b[0m\u001b[92m \u001b[0m│\u001b[92m \u001b[0m\u001b[92m2048 \u001b[0m\u001b[92m \u001b[0m│\n", "└────────────┴────────────────────────────────────────────────────────────────────────┘\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "byteclass_inspect(eth_hdr)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
                                                      EthHdr                                                       \n",
       "┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n",
       "┃ Member      Value                                                                 Data                        ┃\n",
       "┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n",
       "│ dst_mac     [UInt8(0), UInt8(12), UInt8(41), UInt8(180), UInt8(144), UInt8(20)]   b'\\x00\\x0c)\\xb4\\x90\\x14'    │\n",
       "│ src_mac     [UInt8(236), UInt8(244), UInt8(187), UInt8(150), UInt8(18),           b'\\xec\\xf4\\xbb\\x96\\x12\\x0e' │\n",
       "│             UInt8(14)]                                                                                        │\n",
       "│ ether_type  2048                                                                  b'\\x08\\x00'                 │\n",
       "└────────────┴──────────────────────────────────────────────────────────────────────┴─────────────────────────────┘\n",
       "
\n" ], "text/plain": [ "\u001b[3m EthHdr \u001b[0m\n", "┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", "┃\u001b[1m \u001b[0m\u001b[1mMember \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mValue \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mData \u001b[0m\u001b[1m \u001b[0m┃\n", "┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", "│\u001b[90m \u001b[0m\u001b[1;90mdst_mac \u001b[0m\u001b[90m \u001b[0m│\u001b[90m \u001b[0m\u001b[90m[UInt8(0), UInt8(12), UInt8(41), UInt8(180), UInt8(144), UInt8(20)] \u001b[0m\u001b[90m \u001b[0m│\u001b[90m \u001b[0m\u001b[90mb'\\x00\\x0c)\\xb4\\x90\\x14' \u001b[0m\u001b[90m \u001b[0m│\n", "│\u001b[91m \u001b[0m\u001b[1;91msrc_mac \u001b[0m\u001b[91m \u001b[0m│\u001b[91m \u001b[0m\u001b[91m[UInt8(236), UInt8(244), UInt8(187), UInt8(150), UInt8(18), \u001b[0m\u001b[91m \u001b[0m│\u001b[91m \u001b[0m\u001b[91mb'\\xec\\xf4\\xbb\\x96\\x12\\x0e'\u001b[0m\u001b[91m \u001b[0m│\n", "│\u001b[91m \u001b[0m│\u001b[91m \u001b[0m\u001b[91mUInt8(14)] \u001b[0m\u001b[91m \u001b[0m│\u001b[91m \u001b[0m│\n", "│\u001b[92m \u001b[0m\u001b[1;92mether_type\u001b[0m\u001b[92m \u001b[0m│\u001b[92m \u001b[0m\u001b[92m2048 \u001b[0m\u001b[92m \u001b[0m│\u001b[92m \u001b[0m\u001b[92mb'\\x08\\x00' \u001b[0m\u001b[92m \u001b[0m│\n", "└────────────┴──────────────────────────────────────────────────────────────────────┴─────────────────────────────┘\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "collection_table(eth_hdr)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "data = b\"\\xf0\\x9f\\xc2\\xdf\\x16\\x1f\\x00\\x0c\\x29\\xfa\\xa3\\x37\\x08\\x00\\x45\\x00\\x00\\x54\\x39\\x03\\x40\\x00\\x40\\x01\\x68\\xd8\\xc0\\xa8\\xc8\\x15\\x08\\x08\\x08\\x08\\x08\\x00\\xda\\xda\\x20\\x62\\x00\\x01\\x52\\xf2\\xa8\\x62\\x00\\x00\\x00\\x00\\x3d\\x9a\\x05\\x00\\x00\\x00\\x00\\x00\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f\\x20\\x21\\x22\\x23\\x24\\x25\\x26\\x27\\x28\\x29\\x2a\\x2b\\x2c\\x2d\\x2e\\x2f\\x30\\x31\\x32\\x33\\x34\\x35\\x36\\x37\"\n", "ef = EthFrame(data=data)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
                                      Ethernet Frame                                      \n",
       "┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n",
       "┃ Name        Value                                                                     ┃\n",
       "┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n",
       "│ len(ef)    │ 98                                                                        │\n",
       "│ str(ef)    │ EthFrame(src_mac=00:0c:29:fa:a3:37, dst_mac=f0:9f:c2:df:16:1f, type=IPV4) │\n",
       "│ repr(ef)   │ EthFrame(data=<memory at 0x12066bc40>)                                    │\n",
       "│ ef.data    │ <memory at 0x12066bc40>                                                   │\n",
       "│ ef.src_mac │ 00:0c:29:fa:a3:37                                                         │\n",
       "│ ef.dst_mac │ f0:9f:c2:df:16:1f                                                         │\n",
       "│ ef.type    │ IPV4                                                                      │\n",
       "└────────────┴───────────────────────────────────────────────────────────────────────────┘\n",
       "
\n" ], "text/plain": [ "\u001b[3m Ethernet Frame \u001b[0m\n", "┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", "┃\u001b[1m \u001b[0m\u001b[1mName \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mValue \u001b[0m\u001b[1m \u001b[0m┃\n", "┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", "│ len(ef) │ 98 │\n", "│ str(ef) │ EthFrame(src_mac=00:0c:29:fa:a3:37, dst_mac=f0:9f:c2:df:16:1f, type=IPV4) │\n", "│ repr(ef) │ EthFrame(data=) │\n", "│ ef.data │ │\n", "│ ef.src_mac │ 00:0c:29:fa:a3:37 │\n", "│ ef.dst_mac │ f0:9f:c2:df:16:1f │\n", "│ ef.type │ IPV4 │\n", "└────────────┴───────────────────────────────────────────────────────────────────────────┘\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "table = Table(title=\"Ethernet Frame\")\n", "\n", "table.add_column(\"Name\")\n", "table.add_column(\"Value\")\n", "\n", "table.add_row(\"len(ef)\", str(len(ef)))\n", "table.add_row(\"str(ef)\", str(ef))\n", "table.add_row(\"repr(ef)\", repr(ef))\n", "table.add_row(\"ef.data\", str(ef.data))\n", "table.add_row(\"ef.src_mac\", str(ef.src_mac))\n", "table.add_row(\"ef.dst_mac\", str(ef.dst_mac))\n", "table.add_row(\"ef.type\", str(ef.type))\n", "\n", "console = Console()\n", "console.print(table)" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 2 }