-
Notifications
You must be signed in to change notification settings - Fork 0
/
secctp.c
230 lines (213 loc) · 6.71 KB
/
secctp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* SecCTP support library */
#include "secctp.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
/** Generate correctly formatted SecCTP message
*
* @param msg Pointer to buffer where the resulting message is stored
* which should contain applicable info-line
* @param headers Composed message headers (must include default headers
* @param body Optional message body
*
* @return Length of formatted message or -1 in case of error
* Reference SecCTP draft Sec. 7
*/
int generateMsg(char *msg, char *headers, char *body) {
//All messages require the date header field
int ret = -1;
if (msg && headers) {
time_t now = time(0);
struct tm tm = *gmtime(&now);
char *time = (char *)calloc(strlen(DATE_FORMAT)+1, sizeof(char));
strftime(time,strlen(DATE_FORMAT),DATE_FORMAT, &tm);
strcat(msg, time);
strcat(msg,DELIM);
strcat(msg, headers);
strcat(msg,DELIM); //mandatory blank line
if (body)
strcat(msg, body);
ret = strlen(msg);
free(time);
}
return ret;
}
/** Generate correctly formatted SecCTP Hello message
*
* @param msg Pointer to buffer where the resulting message is stored
* @param method Message method {INFO, GET, POST}
* @param user_headers Optional user supplied message headers
* @param body Optional message body
*
* @return Length of formatted message or -1 in case of error
*
* Reference SecCTP draft Sec. 7.1
*/
int generateHello(char *msg, char *method, char *user_headers, char *body){
int ret = -1;
char *headers = (char*)calloc(MAX_HEADER_SIZE, sizeof(char));
if (headers != NULL) {
//Hello messages require accepted languages header field
strcat(headers, LANG);
if (user_headers) {
strcat(headers,user_headers); //Not robust against overflow
}
if (msg && method) {
sprintf(msg, "%s %s", method, VERSION);
strcat(msg,DELIM);
ret = generateMsg(msg, headers, body);
}
free(headers);
}
return ret;
}
/** Generate correctly formatted SecCTP Request message
*
* @param msg Pointer to buffer where the resulting message is stored
* @param method Message method {INFO, GET, POST}
* @param uri Uniform resource identifier being requested
* @param user_headers Optional user supplied message headers
* @param body Optional message body
*
* @return Length of formatted message or -1 in case of error
*
* Reference SecCTP draft Sec. 7.2
*/
int generateReq(char *msg, char *method, char *uri, char *user_headers, char *body){
int ret = -1;
char *headers = (char*)calloc(MAX_HEADER_SIZE, sizeof(char));
if (headers != NULL) {
//Hello messages require accepted languages header field
strcat(headers, LANG);
if (user_headers) {
strcat(headers,user_headers); //Not robust against overflow
}
if (msg && method && uri && headers) {
sprintf(msg, "%s %s %s", method, uri, VERSION);
strcat(msg,DELIM);
ret = generateMsg(msg, headers, body);
}
free(headers);
}
return ret;
}
/** Generate correctly formatted SecCTP Response message
*
* @param msg Pointer to buffer where the resulting message is stored
* @param status_code Response code to applicable request
* @param user_headers Optional user supplied message headers
* @param body Optional message body
*
* @return Length of formatted message or -1 in case of error
*
* Reference SecCTP draft Sec. 7.3
*/
int generateResp(char *msg, int status_code, char *user_headers, char *body){
int ret = -1;
char *headers = (char*)calloc(MAX_HEADER_SIZE, sizeof(char));
if (headers != NULL) {
//Hello messages require accepted languages header field
strcat(headers, LANG);
if (user_headers) {
strcat(headers,user_headers); //Not robust against overflow
}
if (check_code(status_code) && msg && headers) {
sprintf(msg, "%s %d", VERSION, status_code);
strcat(msg,DELIM);
ret = generateMsg(msg, headers, body);
}
free(headers);
}
return ret;
}
/** Helper function for parsing SecCTP messages
*
* @param contents Pointer to struct where the message contents will be stored
* @param msg SecCTP message to be processed
*
* @return 0 for success, -1 in case of error or invalid message
*
* Reference SecCTP draft Sec. 7,1-3
*/
int parseMessage(msgContents *contents, char *msg) {
int ret = -1;
char *tok;
char *infoline;
int count = 0;
if (msg != NULL) { /* else msg is not a valid pointer */
/* extract info line */
tok = strtok(msg, DELIM);
if (tok != NULL) { /* else message is empty*/
infoline = tok;
/*extract headers */
tok = strtok(NULL, DELIM);
if (tok != NULL) { /* else message is invalid */
if (contents->headers != NULL)
contents->headers[0] = '\0';
else
contents->headers = (char*) calloc(MAX_HEADER_SIZE, sizeof(char));
while (tok != NULL && count <= MAX_HEADER_SIZE - 1) {
strncat(contents->headers, tok, strlen(tok));
strncat(contents->headers, DELIM, 2);
count += strlen(tok);
tok = strtok(NULL, DELIM);
}
/* extract body if exists, otherwise will set to NULL */
contents->body = strtok(NULL, DELIM);
/* parse info line */
char *toks[3];
count = 0;
tok = strtok(infoline, " ");
while ( tok != NULL && count < 3) {
toks[count++] = tok;
tok = strtok(NULL, " ");
}
if (count == 3) { //Request
contents->type = REQ;
contents->method = toks[0];
contents->resource = toks[1];
contents->version = toks[2];
ret = 0;
} else if (count == 2) { //Hello or Response
if (strlen(toks[0]) == strlen(VERSION)) { //Matches format for Repsonse
contents->type = RESP;
contents->version = toks[0];
char *end_ptr;
int code = strtol(toks[1], &end_ptr, 10);
if check_code(code) {/* if valid, set return flag */
contents->status = code;
ret = 0;
}
} else if (strlen(toks[1]) == strlen(VERSION)) { //Matches format for hello
contents->type = HELLO;
contents->method = toks[0];
contents->version = toks[1];
ret = 0;
} //Else invalid
} // else invalid message
}
}
}
return ret;
}
/** Temporary basic authorization function
*
* @param headers Pointer to entire SecCTP message headers field
*
* @return 0 for success, non-zero indicates authentication failure
*
* Assumes credentials are always default example secctp:pass
*/
int authorization(char *headers) {
int ret = 0;
char creds[MAX_CRED_LENGTH];
if (headers) { //TODO: Change to authentication against list/dbase/hash of credentials
if (sscanf(strstr(headers, AUTH_TAG)+strlen(AUTH_TAG) ,"%s\r\n%*s",creds) > 0
&& strlen(DEFAULT_CREDS) == strlen(creds) ) {
ret = (strncmp(DEFAULT_CREDS, creds, strlen(DEFAULT_CREDS)) == 0);
}
}
return ret;
}