1 #ifndef PROTO_H
2 #define PROTO_H
3 
4 #include "CedarType.h"
5 
6 #include "Mayaqua/MayaType.h"
7 #include "Mayaqua/Network.h"
8 
9 #define PROTO_OPTION_TOGGLE_NAME "Enabled"
10 
11 // OpenVPN sends 2 bytes, thus this is the buffer size.
12 // If another protocol requires more bytes to be detected, the buffer size must be increased.
13 #define PROTO_CHECK_BUFFER_SIZE	2
14 
15 #define PROTO_TCP_BUFFER_SIZE	(128 * 1024)
16 
17 typedef enum PROTO_MODE
18 {
19 	PROTO_MODE_UNKNOWN,
20 	PROTO_MODE_TCP,
21 	PROTO_MODE_UDP
22 } PROTO_MODE;
23 
24 typedef enum PROTO_OPTION_VALUE
25 {
26 	PROTO_OPTION_UNKNOWN,
27 	PROTO_OPTION_STRING,
28 	PROTO_OPTION_BOOL,
29 	PROTO_OPTION_UINT32
30 } PROTO_OPTION_VALUE;
31 
32 typedef struct PROTO
33 {
34 	CEDAR *Cedar;
35 	LIST *Containers;
36 	HASH_LIST *Sessions;
37 	UDPLISTENER *UdpListener;
38 } PROTO;
39 
40 struct PROTO_OPTION
41 {
42 	char *Name;
43 	PROTO_OPTION_VALUE Type;
44 	union
45 	{
46 		bool Bool;
47 		char *String;
48 		UINT UInt32;
49 	};
50 };
51 
52 typedef struct PROTO_IMPL
53 {
54 	const char *(*Name)();
55 	const PROTO_OPTION *(*Options)();
56 	char *(*OptionStringValue)(const char *name);
57 	bool (*Init)(void **param, const LIST *options, CEDAR *cedar, INTERRUPT_MANAGER *im, SOCK_EVENT *se, const char *cipher, const char *hostname);
58 	void (*Free)(void *param);
59 	bool (*IsPacketForMe)(const PROTO_MODE mode, const void *data, const UINT size);
60 	bool (*ProcessData)(void *param, TCP_RAW_DATA *in, FIFO *out);
61 	bool (*ProcessDatagrams)(void *param, LIST *in, LIST *out);
62 } PROTO_IMPL;
63 
64 typedef struct PROTO_CONTAINER
65 {
66 	const char *Name;
67 	LIST *Options;
68 	const PROTO_IMPL *Impl;
69 } PROTO_CONTAINER;
70 
71 typedef struct PROTO_SESSION
72 {
73 	void *Param;
74 	const PROTO *Proto;
75 	const PROTO_IMPL *Impl;
76 	IP SrcIp;
77 	USHORT SrcPort;
78 	IP DstIp;
79 	USHORT DstPort;
80 	LIST *DatagramsIn;
81 	LIST *DatagramsOut;
82 	SOCK_EVENT *SockEvent;
83 	INTERRUPT_MANAGER *InterruptManager;
84 	THREAD *Thread;
85 	LOCK *Lock;
86 	volatile bool Halt;
87 } PROTO_SESSION;
88 
89 void ProtoLog(const PROTO *proto, const PROTO_SESSION *session, const char *name, ...);
90 
91 int ProtoOptionCompare(void *p1, void *p2);
92 int ProtoContainerCompare(void *p1, void *p2);
93 int ProtoSessionCompare(void *p1, void *p2);
94 
95 UINT ProtoSessionHash(void *p);
96 
97 bool ProtoEnabled(const PROTO *proto, const char *name);
98 
99 PROTO *ProtoNew(CEDAR *cedar);
100 void ProtoDelete(PROTO *proto);
101 
102 PROTO_CONTAINER *ProtoContainerNew(const PROTO_IMPL *impl);
103 void ProtoContainerDelete(PROTO_CONTAINER *container);
104 
105 const PROTO_CONTAINER *ProtoDetect(const PROTO *proto, const PROTO_MODE mode, const UCHAR *data, const UINT size);
106 
107 PROTO_SESSION *ProtoSessionNew(const PROTO *proto, const PROTO_CONTAINER *container, const IP *src_ip, const USHORT src_port, const IP *dst_ip, const USHORT dst_port);
108 void ProtoSessionDelete(PROTO_SESSION *session);
109 
110 bool ProtoSetListenIP(PROTO *proto, const IP *ip);
111 bool ProtoSetUdpPorts(PROTO *proto, const LIST *ports);
112 
113 bool ProtoHandleConnection(PROTO *proto, SOCK *sock, const char *protocol);
114 void ProtoHandleDatagrams(UDPLISTENER *listener, LIST *datagrams);
115 void ProtoSessionThread(THREAD *thread, void *param);
116 
117 #endif
118