common.h 3.41 KB
Newer Older
1
2
3
#ifndef COMMON_H
#define COMMON_H

powelld's avatar
powelld committed
4
5
6
7
8
9
#include <stdarg.h>
#include <stdio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <pthread.h>
#include <signal.h>
powelld's avatar
powelld committed
10
11
#include <stdbool.h>
#include "cJSON.h"
powelld's avatar
powelld committed
12

powelld's avatar
powelld committed
13
14
#define DEFAULT_SERVER_PORT 4433
#define DEFAULT_MBOX_PORT 8423
powelld's avatar
powelld committed
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
#define str(x) #x
#define xstr(x) str(x)

#define MAX_CONTEXT_LEN (100)

#ifndef UNUSED
#define UNUSED(X) ((void)(x))
#endif

#define MAXSTRLEN (1024)

/* error codes are made of two parts
 * the low 16 bits is a global enum for the type of error
 * the high 16 bits is a local enum that only means something
 * specific to the function that was called.
 * So, an error opening a file will produce a global error FILE_NOT_READABLE
 * but the local error will be used by the function to indicate which file and/or why
 */
#define MKERROR(eGlobalStatus,eLocalStatus) (((eLocalStatus)<<16 & 0xFFFF0000)|((eGlobalStatus)0x0000FFFF))
#define GETLOCALERROR(error,dest) ((dest)=(((error)>>16)&0xFFFF))
#define GETGLOBALERROR(error,dest) ((dest)=(error)&0xFFFF)
typedef enum eERROR_CODES {
	SUCCESS=0,
	INVALID_COMMAND_LINE_ARGS,
	FILE_NOT_FOUND,
	FILE_NOT_READABLE,
	FILE_NOT_WRITABLE,
	FILE_PARSE_ERROR,
	INVALID_POINTER,
	INTEGER_OUT_OF_RANGE,
	ARRAY_OVERFLOW,
	MEMORY_ALLOCATION_FAILURE,
	ERROR_IN_LIBRARY_CALL,
	NETWORK_CONNECT_FAIL,
	ERROR_INTERRUPT,
} ERROR_STATUS;

#define ID_CLIENT (1u)
#define ID_SERVER (2u)
#define ID_MIDDLEBOX_MIN (4u)
#define ID_MIDDLEBOX_MAX (255u)



/*function to wait for a specific FILE to be ready for reading*/
ERROR_STATUS
COMMON_ReadWaitFile(FILE *phF);
/*function to wait for a specific File Descriptor (file, socket, pipe) to be ready for reading*/
ERROR_STATUS
COMMON_ReadWaitFD(int iFD);
ERROR_STATUS
COMMON_ReadProxyListFile(SSL *ptSSL,
		const char *sFilename);
ERROR_STATUS
COMMON_InitProxySet(SSL *ptSSL);
ERROR_STATUS
COMMON_InitMulticontextSet(SSL *ptSSL);
ERROR_STATUS
COMMON_AppendProxy(SSL *ptSSL,
		const char *psProxyURL);
ERROR_STATUS
powelld's avatar
powelld committed
76
77
COMMON_SetServer(SSL *ptSSL,
		const char *psURL);
powelld's avatar
powelld committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
ERROR_STATUS
COMMON_AppendContext(SSL *ptSSL,
		const char *psContextDesc);
ERROR_STATUS COMMON_SetProxyAccessPermissionByID(SSL *ptSSL, int iSliceID, int iMiddleboxNum,
							int bGrantRead, int bGrantWrite);
ERROR_STATUS COMMON_TcpConnect(int *piSocket, const char *sHost, int iPort);
void COMMON_LogErrorAndExit(int iExitCode, FILE *pLog, const char *csFmtStr, ...);
void COMMON_CheckLogErrorAndExit(int iExitCode, FILE *pLog, const char *csFmtStr, ...);
void COMMON_Log(FILE *pLog, const char *csFmtStr, ...);
void COMMON_CheckLog(int iExitCode, FILE *pLog, const char *csFmtStr, ...);

ERROR_STATUS COMMON_InitializeSSLCtx(SSL_CTX **pptCtx,
		const char *sMyKeyfile, const char *sMyPassword,
		const char *sCAKeysFile,
		unsigned int iID); /*todo - check the name of this in spec - the byte that identifies the middlebox number, client or server*/
void COMMON_DestroyCtx(SSL_CTX *ptCtx);

powelld's avatar
powelld committed
95
96
char* COMMON_WriteJSONFile (cJSON* data, char* source);
cJSON* COMMON_ReadJSONFile (char* filename);
powelld's avatar
powelld committed
97

powelld's avatar
powelld committed
98
//char* COMMON_GetPrintableBuffer (char* inputBuf, int inputBufLen, int* wasBase64Encoded);
powelld's avatar
powelld committed
99

powelld's avatar
powelld committed
100
101
int COMMON_Base64Encode(const unsigned char* buffer, size_t length, char** b64text); 
int COMMON_Base64Decode(char* b64message, unsigned char** buffer, size_t* length); 
102

powelld's avatar
powelld committed
103
104
105
106
char* COMMON_CallExternalProcess(const char* commandString, unsigned int* lengt);

char* COMMON_MakeNullTerminatedCopy (const char* buf, const unsigned int length);
void COMMON_PrintCertificateDetails (X509* cert);
107
108

#endif