CMSDK  2.0.1
NetworkProtocols.h
1 #if !defined(_NETWORKPROTOCOLS_H_)
2 #define _NETWORKPROTOCOLS_H_
3 
4 #pragma once
5 
6 #include "DataMessage.h"
7 #include "NetworkConnections.h"
8 
9 #define PROTOCOL_HTTP_SERVER 1
10 #define PROTOCOL_HTTP_CLIENT 2
11 #define PROTOCOL_MESSAGE 4
12 #define PROTOCOL_TELNET 8
13 #define PROTOCOL_3GCAM 16
14 #define ENCRYPTION_SSL 1
15 #define ENCRYPTION_AES 2
16 
17 namespace cmlabs{
18 
20 // Data Types
22 
23 #define HTTP_GET 1
24 #define HTTP_PUT 2
25 #define HTTP_POST 3
26 #define HTTP_HEAD 4
27 #define HTTP_DELETE 5
28 #define HTTP_OPTIONS 6
29 
30 static char HTTP_Type[][8] =
31 {
32 "",
33 "GET",
34 "PUT",
35 "POST",
36 "HEAD",
37 "DELETE",
38 "OPTIONS"
39 };
40 
41 //
42 //GET /path/file.html HTTP/1.0
43 //From: someuser@jmarshall.com
44 //User-Agent: HTTPTool/1.0
45 //if-modified-since: Sat, 29 Oct 1994 19:43:31 GMT
46 //[blank line here]
47 //
48 //POST /path/script.cgi HTTP/1.0
49 //From: frog@jmarshall.com
50 //User-Agent: HTTPTool/1.0
51 //Content-Type: application/x-www-form-urlencoded
52 //Content-Length: 32
53 //
54 //home=Cosby&favorite+flavor=flies
55 //
56 
58 public:
59  HTTPPostEntry() { content = NULL; contentSize = 0; }
60  virtual ~HTTPPostEntry() { delete [] content; }
61 
62  bool isValid() {
63  return ( name.length() && content && contentSize );
64  }
65  bool setContent(const char* content, uint32 size) {
66  if (!content) return false;
67  this->contentSize = size;
68  this->content = new char[size+1];
69  memcpy(this->content, content, size);
70  this->content[size] = 0;
71  return true;
72  }
73 
74  std::string name;
75  std::string filename;
76  std::string type;
77  uint32 contentSize;
78  char* content;
79 };
80 
81 class HTTPRequest {
82 public:
83 // HTTPRequest(uint64 startRecTime = 0);
84  HTTPRequest(uint64 source = 0, uint64 startRecTime = 0);
86  virtual ~HTTPRequest();
87 
88  // Reading it in from a socket
89  bool processHeader(const char* buffer, uint32 size, bool &isInvalid);
90  bool processContent(const char* buffer, uint32 size);
91  const char* getHeaderEntry(const char* entry);
92  const char* getRequest();
93  const char* getURI();
94  const char* getProtocol();
95  std::string decodeBasicAuthorization();
96  const char* getBasicAuthorization();
97  std::string getBasicAuthorizationUser();
98  std::string getBasicAuthorizationPassword();
99  bool setBasicAuthorization(const char* authB64);
100  bool setBasicAuthorization(const char* user, const char* password);
101  const char* getParameter(const char* entry);
102  const char* getPostData(const char* entry, uint32& size, const char** type);
103  const char* getPostData(const char* entry, uint32& size);
104  const char* getPostDataType(const char* entry);
105  bool parseURIParameters(const char* text);
106  bool parseContentParameters(const char* content, uint32 size);
107  bool parseContentChunk(const char* chunk, uint32 size);
108 
109  DataMessage* convertToMessage();
110 
111  const char* getContent(uint32& size);
112 
113  bool createRequest(uint8 type, const char* host, const char* uri, const char* content, uint32 contentSize, bool keepAlive, uint64 ifModifiedSince);
114  uint32 getSize();
115 
116  uint8 type;
117  uint64 time;
118  uint64 endReceiveTime;
119  uint64 source;
120  uint32 headerLength;
121  uint32 contentLength;
122  uint64 ifModifiedSince;
123  bool keepAlive;
124  char* data;
125 
126  std::map<std::string, std::string> entries;
127  std::map<std::string, std::string> params;
128  std::map<std::string, HTTPPostEntry*> postEntries;
129  std::string postBoundary;
130 };
131 
132 #define TELNET_WINDOWS 1
133 #define TELNET_UNIX 2
134 
135 class TelnetLine {
136 public:
137  TelnetLine(uint64 source);
138  virtual ~TelnetLine();
139 
140  bool setLine(const char* buffer, bool addCR);
141  bool setLine(const char* buffer, uint32 len, bool addCR = false);
142  bool giveData(char* buffer, uint32 len);
143  uint32 getSize();
144  bool setCR(uint8 type);
145 
146  char* data;
147  char cr[3];
148  uint64 time;
149  uint64 source;
150  uint32 size;
151  uint32 user;
152 };
153 
154 // HTTP/1.0 200 OK
155 // HTTP/1.0 500 Internal Server Error <H1>Error while creating internal page</H1>
156 // HTTP/1.0 200 OK
157 // Cache-Control: No-Cache
158 // HTTP/1.0 404 Object Not Found <H1>File not found</H1>
159 // HTTP/1.0 403 Access Forbidden <H1>Access Denied</H1>
160 
161 #define HTTP_OK 0
162 #define HTTP_MOVED_PERMANENTLY 1
163 #define HTTP_USE_LOCAL_COPY 2
164 #define HTTP_MALFORMED_URL 3
165 #define HTTP_UNAUTHORIZED 4
166 #define HTTP_ACCESS_DENIED 5
167 #define HTTP_FILE_NOT_FOUND 6
168 #define HTTP_INTERNAL_ERROR 7
169 #define HTTP_NOT_IMPLEMENTED 8
170 #define HTTP_SERVICE_NOT_AVAILABLE 9
171 #define HTTP_GATEWAY_TIMEOUT 10
172 #define HTTP_SERVER_UNAVAILABLE 11
173 #define HTTP_SERVER_NOREPLY 12
174 #define HTTP_SERVER_MALFORMED_REPLY 13
175 
176 static char HTTP_Status[][128] =
177 {
178 "HTTP/1.1 200 OK",
179 "HTTP/1.1 301 Moved Permanently",
180 "HTTP/1.1 304 Use Local Copy",
181 "HTTP/1.1 400 Bad Request <H1>Malformed URL</H1>",
182 "HTTP/1.1 401 Unauthorized",
183 "HTTP/1.1 403 Access Denied",
184 "HTTP/1.1 404 Object Not Found",
185 "HTTP/1.1 500 Internal Server Error <H1>Error while creating page</H1>",
186 "HTTP/1.1 501 Not Implemented <H1>Not implemented</H1>",
187 "HTTP/1.1 503 Service Unavailable <H1>Service Unavailable</H1>",
188 "HTTP/1.1 504 Gateway Timeout <H1>Gateway Timeout</H1>",
189 "HTTP/1.1 500 Internal Server Error <H1>Server unavailable</H1>",
190 "HTTP/1.1 500 Internal Server Error <H1>No reply from server</H1>",
191 "HTTP/1.1 500 Internal Server Error <H1>Malformed server reply</H1>"
192 };
193 
194 class HTTPReply {
195 public:
196  static HTTPReply* CreateErrorReply(uint8 type);
197  static HTTPReply* CreateAuthorizationReply(const char* realm);
198 
199  HTTPReply(uint64 source = 0);
200  HTTPReply(HTTPReply* reply);
201  virtual ~HTTPReply();
202 
203  // Reading it in from a socket
204  bool processHeader(const char* buffer, uint32 size, bool &isInvalid);
205  bool processContent(const char* buffer, uint32 size);
206  const char* getHeaderEntry(const char* entry);
207  const char* getProtocol();
208 
209  const char* getContent(uint32& size);
210 
211  // Generating the data
212  bool createPage(uint8 status, uint64 time, const char* serverName,
213  uint64 lastMod, bool keepAlive, bool cache, const char* contentType,
214  const char* content, uint32 contentSize = 0, const char* additionalHeaderEntries = NULL);
215 
216  bool createFromFile(uint64 time, const char* serverName,
217  uint64 ifLastMod, bool keepAlive, bool cache,
218  const char* filename);
219 
220  bool createOptionsResponse(uint8 status, uint64 time, const char* serverName, bool keepAlive, const char* origin, const char* operations);
221  bool createErrorPage(uint8 status, uint64 time, const char* serverName, bool keepAlive);
222  bool createAuthorizationReply(const char* realm);
223 
224  uint32 getSize();
225 
226  uint8 type;
227  uint64 time;
228  uint64 source;
229  uint32 headerLength;
230  uint32 contentLength;
231  bool keepAlive;
232  char* data;
233 
234  std::map<std::string, std::string> entries;
235  std::map<std::string, std::string> params;
236 };
237 
238 
240 // Auto Detect Protocols
242 
244 public:
245  uint8 protocol;
246  NetworkProtocol() {protocol = 0;}
247  static bool CheckBufferForCompatibility(const char* buffer, uint32 length) {return false;};
248  static bool InitialiseConversation(NetworkConnection* con) {return false;};
249 };
250 
252 {
253 public:
254  HTTPProtocol() {protocol = PROTOCOL_HTTP_SERVER;}
255  static bool CheckBufferForCompatibility(const char* buffer, uint32 length);
256  static bool InitialiseConversation(NetworkConnection* con);
257  static bool SendHTTPReply(NetworkConnection* con, HTTPReply* reply);
258  static HTTPReply* ReceiveHTTPReply(NetworkConnection* con, uint32 timeout);
259  static bool SendHTTPRequest(NetworkConnection* con, HTTPRequest* req);
260  static HTTPRequest* ReceiveHTTPRequest(NetworkConnection* con, uint32 timeout);
261 };
262 
264 {
265 public:
266  HTTPClientProtocol() {protocol = PROTOCOL_HTTP_CLIENT;}
267  static bool CheckBufferForCompatibility(const char* buffer, uint32 length);
268  static bool InitialiseConversation(NetworkConnection* con);
269  static bool SendHTTPReply(NetworkConnection* con, HTTPReply* reply);
270  static HTTPReply* ReceiveHTTPReply(NetworkConnection* con, uint32 timeout);
271  static bool SendHTTPRequest(NetworkConnection* con, HTTPRequest* req);
272  static HTTPRequest* ReceiveHTTPRequest(NetworkConnection* con, uint32 timeout);
273 };
274 
276 {
277 public:
278  MessageProtocol() {protocol = PROTOCOL_MESSAGE;}
279  static bool CheckBufferForCompatibility(const char* buffer, uint32 length);
280  static bool InitialiseConversation(NetworkConnection* con);
281  static bool SendMessage(NetworkConnection* con, DataMessage* msg, uint64 receiver = 0);
282  static DataMessage* ReceiveMessage(NetworkConnection* con, uint32 timeout);
283 };
284 
286 {
287 public:
288  TelnetProtocol() {protocol = PROTOCOL_TELNET;}
289  static bool CheckBufferForCompatibility(const char* buffer, uint32 length);
290  static bool InitialiseConversation(NetworkConnection* con);
291  static bool SendTelnetLine(NetworkConnection* con, TelnetLine* line);
292  static TelnetLine* ReceiveTelnetLine(NetworkConnection* con, uint32 timeout);
293  static TelnetLine* ReceiveTelnetLine(NetworkConnection* con, uint32 timeout, uint32 size);
294 };
295 
296 }
297 
298 #endif // _NETWORKPROTOCOLS_H_
Definition: NetworkProtocols.h:194
Definition: Bitmap.h:7
Definition: NetworkProtocols.h:57
Definition: NetworkProtocols.h:285
Definition: NetworkProtocols.h:251
Definition: NetworkProtocols.h:135
Definition: NetworkProtocols.h:275
Definition: NetworkConnections.h:95
Definition: NetworkProtocols.h:81
Definition: NetworkProtocols.h:243
Definition: DataMessage.h:95
Definition: NetworkProtocols.h:263