summaryrefslogtreecommitdiffhomepage
path: root/dev/Kernel/NetworkKit/IPC.h
blob: c60bf065321f53d62a4136bba0dac79d99bdc5bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* -------------------------------------------

	Copyright (C) 2024, Theater Quality Corp, all rights reserved..

	File: IPC.h.
	Purpose: IPC protocol.

------------------------------------------- */

#ifndef INC_IPC_H
#define INC_IPC_H

#include <NewKit/Defines.h>
#include <NewKit/KString.h>
#include <Hints/CompilerHint.h>

/// @file IPC.h
/// @brief IPC comm. protocol.

/// IA separator.
#define kIPCRemoteSeparator ":"

/// Interchange address, consists of PID:TEAM.
#define kIPCRemoteInvalid "00:00"

#define kIPCHeaderMagic (0x4950434)

namespace Kernel
{
	struct IPC_ADDR;
	struct IPC_MSG;

	/// @brief 128-bit IPC address.
	struct PACKED IPC_ADDR final
	{
		UInt64 UserProcessID;
		UInt64 UserProcessTeam;

		////////////////////////////////////
		// some operators.
		////////////////////////////////////

		bool operator==(const IPC_ADDR& addr) noexcept
		{
			return addr.UserProcessID == this->UserProcessID && addr.UserProcessTeam == this->UserProcessTeam;
		}

		bool operator==(IPC_ADDR& addr) noexcept
		{
			return addr.UserProcessID == this->UserProcessID && addr.UserProcessTeam == this->UserProcessTeam;
		}
	};

	typedef struct IPC_ADDR IPCEPAddressKind;

	enum
	{
		kIPCLittleEndian = 0,
		kIPCBigEndian	 = 1,
		kIPCMixedEndian	 = 2,
	};

	constexpr inline auto kIPCMsgSize = 6094U;

	/// @brief IPC connection header, message cannot be greater than 6K.
	typedef struct IPC_MSG final
	{
		UInt32			 IpcHeaderMagic; // cRemoteHeaderMagic
		UInt8			 IpcEndianess;	 // 0 : LE, 1 : BE
		SizeT			 IpcPacketSize;
		IPCEPAddressKind IpcFrom;
		IPCEPAddressKind IpcTo;
		UInt32			 IpcCRC32;
		UInt32			 IpcMsg;
		UInt32			 IpcMsgSz;
		UInt8			 IpcData[kIPCMsgSize];

		/// @brief Passes the message to target, could be anything, HTTP packet, JSON or whatever.
		Bool Pass(IPC_MSG* target) noexcept
		{
			if (target && target->IpcFrom == this->IpcTo)
			{
				if (this->IpcMsgSz > target->IpcMsgSz)
					return No;

				rt_copy_memory(this->IpcData, target->IpcData, this->IpcMsgSz);

				return Yes;
			}

			return No;
		}
	} PACKED IPC_MSG;

	/// @brief Sanitize packet function
	/// @retval true packet is correct.
	/// @retval false packet is incorrect and process has crashed.
	Bool ipc_sanitize_packet(_Input IPC_MSG* pckt_in);

	/// @brief Construct packet function
	/// @retval true packet is correct.
	/// @retval false packet is incorrect and process has crashed.
	Bool ipc_construct_packet(_Output _Input IPC_MSG** pckt_in);
} // namespace Kernel

#endif // INC_IPC_H