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