-
Notifications
You must be signed in to change notification settings - Fork 5
/
htcpcp_server.c
316 lines (271 loc) · 7.31 KB
/
htcpcp_server.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/////////////////////////////////////////////////////////////////////////
//
// HTCPCP implementation
// Hyper Text Coffee Pot Control Protocol
//
// Copyleft/Copyright (c)2011
// Matthias Lee
// James Eastwood
/////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <signal.h>
#include <time.h>
#include "pot.c"
#define QUEUELENGTH 10
#define RCVBUFSIZE 256
#define METHOD 0
#define VAR 1
#define TRUE 1
#define FALSE 0
#define POTCNT 5
typedef struct {
pthread_t tid;
int T_id;
int sock;
int busy;
potStruct *pot;
} T_vars;
T_vars threadVars[POTCNT+1];
potStruct Pots[POTCNT];
////////////////////////////////////////////////////////////////////
// splits requests, either <var>:<val> or <method> <pot>
////////////////////////////////////////////////////////////////////
int splitVarVal(char* buf, char* var, char* val, char tdel){
const char *del;
int varLen;
char* tmp;
if(tdel==';')
del=";";
else
del=":";
if( strstr( buf, del )!=NULL ){
varLen = (strlen(buf)-strlen(strstr(buf, del )));
strncpy(var,buf,varLen);
var[varLen]='\0';
//printf( "var: |%s|\n", var );
strcpy(val,buf+varLen+1); // +1 for skipping ":"
//printf( "val: |%s|\n", val );
return VAR;
} else {
varLen = (strlen(buf)-strlen(strstr(buf, " " )));
strncpy(var,buf,varLen);
var[varLen]='\0';
tmp=buf+varLen+1; // +1 for skipping " "
varLen = (strlen(tmp)-strlen(strstr(tmp, " " )));
strncpy(val,tmp,varLen);
//printf( "Method/pot: |%s|%s|\n", var, val );
return METHOD;
}
}
char* strip(char* str){
char *end;
// Trim leading space
while(isspace(*str)) str++;
if(*str == 0) // All spaces?
return str;
// Trim trailing space
end = str + strlen(str) - 1;
while(end > str && isspace(*end)) end--;
// Write new null terminator
*(end+1) = 0;
return str;
}
int CoffeeRequestHandler( char *buff, int *potNum, char *method, char additions[1024]) {
char *line = NULL;
char lineBuf[1024];
char varBuf[255];
char valBuf[255];
char tmpBuf[255];
int type;
const char LineDel[] = "\r\n";
printf("\nRequest:\n%s\n",buff);
fflush(stdout);
line = strtok( buff , LineDel);
if(line == NULL)
return -1;
while( line != NULL ) {
//printf( "extracted req: |%s|\n", strip(line) );
strcpy(lineBuf,line);
type = splitVarVal(lineBuf,varBuf,valBuf,':');
if (type == METHOD){
//printf("Its a method. %s=%s\n",varBuf,valBuf);
strcpy(method,strip(varBuf));
*potNum=atoi(strstr(valBuf,"-")+1);
} else if ( type == VAR ) {
memset(tmpBuf, 0, 255);
//printf("Its a var. %s=%s\n",varBuf,valBuf);
if(strcmp(varBuf, "Accept-Additions")==0){
strcpy(additions,strip(valBuf));
}
}
line = strtok( NULL, LineDel );
}
if(strlen(varBuf)<1 || strlen(valBuf)<1)
return -1;
else
return 0;
}
static void *thread(void *ptr) {
T_vars *vars;
vars = (T_vars *) ptr;
char method[255];
int potNum;
int type;
char *line=NULL;
char tmpAdd[1024];
char lineBuf[2048];
char varBuf[255];
char valBuf[255];
const char del[] = ",";
potNum=1;
char Adds[20][255];
printf("Thread %d\n",(int)vars->T_id);
fflush(stdout);
char buff[1024];
int addCnt=0;
int bytesRcvd,x;
memset(buff, 0, 1024);
memset(method, 0, 255);
// Read client request
if ((bytesRcvd = read((int)vars->sock, buff, RCVBUFSIZE - 1)) <= 0) {
perror("read");
exit(-1);
}
if(CoffeeRequestHandler(buff, &potNum, method, tmpAdd)>=0){
line = strtok( tmpAdd , del);
while( line != NULL ) {
//printf( "extracted add: |%s|\n", line );
strcpy(lineBuf,strip(line));
type = splitVarVal(lineBuf,varBuf,valBuf,';');
sprintf(Adds[addCnt],"%s;%s",varBuf,valBuf);
line = strtok( NULL, del );
addCnt++;
}
for(x=0;x<addCnt;x++){
printf("add %s\n",Adds[x]);
}
printf("METHOD is %s\n",method);
printf("POT num is %d\n",potNum);
} else {
printf("Parsing error!\n");
}
// LOGIC HERE
if (potNum>=POTCNT) {
strcpy(lineBuf,"HTCPCP/1.0 418 I'm a teapot\r\n");
} else {
if( strcmp(method,"BREW") ==0 ){
brew(&vars->pot[potNum], Adds, lineBuf);
} else if( strcmp(method,"PUT") == 0) {
put(&vars->pot[potNum], lineBuf);
} else if( strcmp(method,"GET") == 0) {
get(&vars->pot[potNum], lineBuf);
} else if( strcmp(method,"WHEN") == 0) {
when(&vars->pot[potNum], lineBuf);
} else{
strcpy(lineBuf,"HTCPCP/1.0 418 I'm a teapot\r\n");
}
}
if (write((int)vars->sock, lineBuf, strlen((char*)lineBuf)) <= 0) {
perror("write");
exit(-1);
}
// stop and close socket
shutdown((int)vars->sock, 1);
close((int)vars->sock);
vars->busy = FALSE;
}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
int main(int argc, char **argv, char **environ) {
pid_t pid;
char tmpBuf[RCVBUFSIZE];
int sockid, tmpsock;
int PORT;
int i;
int curThread;
// structs to hold addrs
struct sockaddr_in server_addr, client_addr;
for(i=0; i<POTCNT; i++){
resetPot(&Pots[i]);
}
if( argc != 2 ) {
fprintf(stderr, "USAGE: %s <port number>\n", argv[0]);
exit(-1);
}
PORT = atoi(argv[1]); // parse port num
if ( (pid = fork()) < 0) {
perror("Cannot fork");
exit(0);
}
else if (pid != 0) {
// keep track of pids for final kill
char t[128];
sprintf(t, "echo %d > %s.pid\n", pid, argv[0]);
system(t);
exit(0);
}
// Create, bind it, listen on socket
signal(SIGCHLD, SIG_IGN);
if( (sockid = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(-1);
}
// setup sockaddr
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
// accept any connection on PORT
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(PORT);
// bind
if (bind(sockid, (struct sockaddr *) &server_addr, sizeof(server_addr)) != 0) {
perror("can't bind to socket");
exit(-1);
}
if (listen(sockid, QUEUELENGTH) < 0) {
perror("listen");
exit(-1);
}
unsigned int clientaddrlength;
clientaddrlength = sizeof(client_addr);
printf("Welcome to the potLogic CoffeeTr0n!\n");
printf("\tReady to brew.. there are %d Pots available\n",POTCNT);
// while not killed accept sockets
while (1) {
curThread=0;
while(threadVars[curThread].busy == TRUE && curThread < POTCNT){
curThread++;
}
if(curThread == POTCNT){
printf("TOO MANY CONNECTIONS\n");
tmpsock = accept(sockid, (struct sockaddr *) &client_addr, &clientaddrlength);
strcpy(tmpBuf,"HTCPCP/1.0 418 I'm a teapot\r\n");
if (write(tmpsock, tmpBuf, strlen((char*)tmpBuf)) <= 0) {
perror("write");
exit(-1);
}
} else {
threadVars[curThread].sock = accept(sockid, (struct sockaddr *) &client_addr, &clientaddrlength);
threadVars[curThread].pot = Pots;
if (threadVars[curThread].sock < 0) {
perror("accept");
exit(-1);
}
threadVars[curThread].T_id = curThread;
threadVars[curThread].busy = TRUE;
if( pthread_create(&threadVars[curThread].tid, NULL, &thread, (void *) &threadVars[curThread]) !=0 ) {
perror("pthread_create");
exit(-2);
}
printf("Created thread %d\n",curThread);
}
}
}