1 | #include "protocol_ex.h" |
2 | |
3 | #include "config.h" |
4 | #include "uuid_manager.h" |
5 | |
6 | #include <base/system.h> |
7 | #include <engine/message.h> |
8 | |
9 | #include <new> |
10 | |
11 | void RegisterUuids(CUuidManager *pManager) |
12 | { |
13 | #define UUID(id, name) pManager->RegisterName(id, name); |
14 | #include "protocol_ex_msgs.h" |
15 | #undef UUID |
16 | } |
17 | |
18 | int UnpackMessageId(int *pId, bool *pSys, CUuid *pUuid, CUnpacker *pUnpacker, CMsgPacker *pPacker) |
19 | { |
20 | *pId = 0; |
21 | *pSys = false; |
22 | mem_zero(block: pUuid, size: sizeof(*pUuid)); |
23 | |
24 | int MsgId = pUnpacker->GetInt(); |
25 | |
26 | if(pUnpacker->Error()) |
27 | { |
28 | return UNPACKMESSAGE_ERROR; |
29 | } |
30 | |
31 | *pId = MsgId >> 1; |
32 | *pSys = MsgId & 1; |
33 | |
34 | if(*pId < 0 || *pId >= OFFSET_UUID) |
35 | { |
36 | return UNPACKMESSAGE_ERROR; |
37 | } |
38 | |
39 | if(*pId != 0) // NETMSG_EX, NETMSGTYPE_EX |
40 | { |
41 | return UNPACKMESSAGE_OK; |
42 | } |
43 | |
44 | *pId = g_UuidManager.UnpackUuid(pUnpacker, pOut: pUuid); |
45 | |
46 | if(*pId == UUID_INVALID || *pId == UUID_UNKNOWN) |
47 | { |
48 | return UNPACKMESSAGE_ERROR; |
49 | } |
50 | |
51 | if(*pSys) |
52 | { |
53 | switch(*pId) |
54 | { |
55 | case NETMSG_WHATIS: |
56 | { |
57 | CUuid Uuid2; |
58 | int Id2 = g_UuidManager.UnpackUuid(pUnpacker, pOut: &Uuid2); |
59 | if(Id2 == UUID_INVALID) |
60 | { |
61 | break; |
62 | } |
63 | if(Id2 == UUID_UNKNOWN) |
64 | { |
65 | new(pPacker) CMsgPacker(NETMSG_IDONTKNOW, true); |
66 | pPacker->AddRaw(pData: &Uuid2, Size: sizeof(Uuid2)); |
67 | } |
68 | else |
69 | { |
70 | new(pPacker) CMsgPacker(NETMSG_ITIS, true); |
71 | pPacker->AddRaw(pData: &Uuid2, Size: sizeof(Uuid2)); |
72 | pPacker->AddString(pStr: g_UuidManager.GetName(Id: Id2), Limit: 0); |
73 | } |
74 | return UNPACKMESSAGE_ANSWER; |
75 | } |
76 | case NETMSG_IDONTKNOW: |
77 | if(g_Config.m_Debug) |
78 | { |
79 | CUuid Uuid2; |
80 | g_UuidManager.UnpackUuid(pUnpacker, pOut: &Uuid2); |
81 | if(pUnpacker->Error()) |
82 | break; |
83 | char aBuf[UUID_MAXSTRSIZE]; |
84 | FormatUuid(Uuid: Uuid2, pBuffer: aBuf, BufferLength: sizeof(aBuf)); |
85 | dbg_msg(sys: "uuid" , fmt: "peer: unknown %s" , aBuf); |
86 | } |
87 | break; |
88 | case NETMSG_ITIS: |
89 | if(g_Config.m_Debug) |
90 | { |
91 | CUuid Uuid2; |
92 | g_UuidManager.UnpackUuid(pUnpacker, pOut: &Uuid2); |
93 | const char *pName = pUnpacker->GetString(SanitizeType: CUnpacker::SANITIZE_CC); |
94 | if(pUnpacker->Error()) |
95 | break; |
96 | char aBuf[UUID_MAXSTRSIZE]; |
97 | FormatUuid(Uuid: Uuid2, pBuffer: aBuf, BufferLength: sizeof(aBuf)); |
98 | dbg_msg(sys: "uuid" , fmt: "peer: %s %s" , aBuf, pName); |
99 | } |
100 | break; |
101 | } |
102 | } |
103 | return UNPACKMESSAGE_OK; |
104 | } |
105 | |