
وحدة معالجة مركزية بسيطة في الذاكرة لإنشاء بروتوكولات محايدة أثناء التشغيل
Harald هو وحدة معالجة مركزية (CPU) صغيرة عالية المستوى تعمل في الذاكرة، قادرة على معالجة مجموعة من التعليمات لتوليد بروتوكولات طبقة التطبيق لاستخدامها عبر بروتوكول شبكة معين (TCP/UDP).
سيستهلك Harald تدفقًا من OPCODES ويطبق تحويلات مختلفة على الحمولة (payload) لتصبح مناسبة لبروتوكول طبقة التطبيق المحدد.
[!TIP] لماذا Harald؟ ربما تعرف Harald Gormsson، ملك الدنمارك، بلقبه: Blátǫnn، أو كما ننطقه اليوم، Bluetooth. سُميت التقنية على لقب Harald لأنه ربط الممالك الدنماركية والفايكنج في ذلك الوقت. لذا أنا أسمي هذا على اسمه الأول.
يتصرف Harald كوحدة معالجة مركزية (CPU). يتبع مجموعة من OPCODES التي تحدد الإجراءات. بروتوكول (PROTOCOL). سيعالج Harald العمليات المطلوبة لإنشاء البروتوكول المطلوب في الوقت الفعلي.
بمجرد توفير PROTOCOL، سيتكرر Harald عبر PAYLOAD المراد إرساله من خلال اجتياز PROTOCOL بالكامل مرة واحدة لكل جزء (fragment). أي: إذا كان يجب تقسيم الحمولة إلى أجزاء متعددة، سيستخدم Harald نفس البروتوكول لكل جزء، منفذًا كل إجراء محدد في البروتوكول على جزء الحمولة الحالي.
يحتفظ Harald بعدد من السجلات (registries) لاستخدامها بواسطة البروتوكول لتخزين القيم. يمكن استخدام هذه السجلات لتنفيذ عمليات متعددة، مثل الجمع، والطرح، والترميز وفك الترميز... إلخ.
يخصص Harald ديناميكيًا مصفوفة من النتائج حيث يمكن للبروتوكول تخزين الاستجابات لكل طلب يتم تنفيذه عند إرسال جزء من الحمولة.

يدعم Harald مجموعة متنوعة من Opcodes لتوفيرها كبروتوكول. سيقوم Harald بتنفيذ مجموعة من العمليات عالية المستوى عند استلام Opcode معين. يتوقع كل Opcode مجموعة محددة مسبقًا من الوسائط بطول محدد مسبقًا.
| OPCODE::Nº | Description | ARGS::Size |
|---|---|---|
| H_F_APPEND::0x01 | Appends the following N Bytes to the end payload | Size::4, NBytes::N |
| H_F_PREPEND::0x02 | Prepends the following N Bytes to the end payload | Size::4, NBytes::N |
| H_F_SPLIT::0x03 | Splits the Payload in fragments of the given size | Size::4 |
| H_F_PAYLOAD_INJECT::0x04 | Injects the Payload fragment into the End Payload | - |
| H_F_SOCK_INIT::0x05 | Initializes the socket connection | IsIPAddress:1, SSL_Enabled::1, TCP/UDP::1, [(HostLen::4,HostName:N)/IPAddress::4], Port::2 |
| H_F_SEND::0x06 | Sends the constructed End Payload | - |
| H_F_RECV::0x07 | Receives up to N Bytes | Size::4 |
| H_F_CLOSE::0x08 | Closes the socket and cleans | - |
| H_F_INJECT::0x09 | Injects the requested registry into the End Payload | Reg::1 |
| H_F_ENCODE_STR::0x0A | Encodes a Payload field to its string representation | IsReg::1, [(Payload::1, Field::1)/Reg::1], Format::1 |
| H_F_DECODE_STR::0xA0 | Decodes the contents of a registry to its numeric representation | Reg::1 |
| H_F_ENCODE_STRB64::0x0B | Encodes a Payload field to its B64string representation | Payload::1, Field::1 |
| H_F_COMPUTE_FRAG_LENGTH::0x0D | Calculates the next fragment to be sent length | - |
| H_F_SEARCH::0x0E | Searches for a given byte pattern of a given N size in a given registry | Reg::1, Size::4, NBytes::N |
| H_F_SEEK::0x0F | Moves the cursor in a given payload by a given number of bytes | Payload::1, Offset::4 |
| H_F_PUSH::0x10 | Pushes a number of bytes or the contents of a registry into another registry | IsReg::1, [Size::4, NBytes::N]/SrcReg::1, DstReg::2 |
| H_F_ADD::0x12 | Adds two registries or a number to a registry | IsReg::1, [Size::4, NBytes::N]/SrcReg::1, DstReg::2 |
| H_F_SUBSTRACT::0x13 | Substracts two registries or a number to a registry | IsReg::1, [Size::4, NBytes::N]/SrcReg::1, DstReg::2 |
| H_F_READ::0x14 | Reads from a given registry up to the length stored in another registry | FromReg::1, SizeReg::1 |
| H_F_STORE::0x15 | Stores a registry value into Harald's Storage | Reg::1 |
سيولّد تدفق Opcode التالي البيانات المطلوبة لإرسالها لكي يُفهم الاتصال كـ HTTP. سيبدأ، ويعدّل الحمولة لتبدو كحركة مرور HTTP، ويرسلها عبر اتصال SSL/TLS، ويغلق الاتصال في النهاية.
H_F_SPLIT, 0x00, 0x00, 0x00, 0xFF, // Splits the Payload
H_F_APPEND, 0x00, 0x00, 0x00, 0x46, // Appends...
'P', 'O', 'S', 'T', ' ', '/', ' ', // POST /
'H', 'T', 'T', 'P', '/', '1', '.', '1', '\r', '\n', // HTTP/1.1
'H', 'o', 's', 't', ':', ' ', // Host:
'd', 'o', 'm', 'a', 'i', 'n', '.', 'c', 'o', 'm', '\r', '\n', // domain.com
'C', 'o', 'n', 'n', 'e', 'c', 't', 'i', 'o', 'n', ':', ' ', // Connection:
'c', 'l', 'o', 's', 'e', '\r', '\n', // close
'C', 'o', 'n', 't', 'e', 'n', 't', '-', // Content-
'L', 'e', 'n', 'g', 't', 'h', ':', ' ', // Length:
H_F_COMPUTE_FRAG_LENGTH, // Next Fragment's size
H_F_ENCODE_STR, H_PAY_END_PAYLOAD, H_PAY_FRAGMENT_SIZE, 'u', // FragSize encode
H_F_INJECT, H_REG_RET, // Inject the Fragment Size encoded str
H_F_APPEND, 0x00, 0x00, 0x00, 0x04, '\r', '\n', '\r', '\n', // Append Body begin
H_F_PAYLOAD_INJECT, // Inject the Payload
H_F_SOCK_INIT, 0x01, 0x01, 0x7F, 0x00, 0x00, 0x01, 0x04, 0xD2, // Init Socket
H_F_SEND, // Send the Payload over the socket
H_F_CLOSE // Close the socket and cleanup