-
Notifications
You must be signed in to change notification settings - Fork 5
/
htcpcp_server.c
207 lines (172 loc) · 4.55 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
/////////////////////////////////////////////////////////////////////////
//
// HTCPCP implementation
// Hyper Text Coffee Pot Control Protocol
//
// Aug 1st 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
struct HTCPCP_Req
{
int potNum;
int socket;
int reqType;
char opts[100][255];
} ;
////////////////////////////////////////////////////////////////////
// splits requests, either <var>:<val> or <method> <pot>
////////////////////////////////////////////////////////////////////
int splitVarVal(char* buf, char* var, char* val){
int varLen;
char* tmp;
if( strstr( buf, ":" )!=NULL ){
varLen = (strlen(buf)-strlen(strstr(buf, ":" )));
strncpy(var,buf,varLen);
var[varLen]='\0';
strcpy(val,buf+varLen+1); // +1 for skipping ":"
//printf( "var: |%s|\n", var );
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;
}
}
void CoffeeRequestHandler( char *buff, int newsock) {
char *line = NULL;
char lineBuf[1024];
char varBuf[256];
char valBuf[256];
char *var = NULL;
int varlen=0;
int type;
const char LineDel[] = "\r\n";
const char VarDel[] = ": ";
const char SpaceDel[] = " ";
printf("\nRequest:\n%s\n",buff);
fflush(stdout);
line = strtok( buff , LineDel);
while( line != NULL ) {
printf( "extracted req: |%s|\n", line );
strcpy(lineBuf,line);
type = splitVarVal(lineBuf,varBuf,valBuf);
if (type == METHOD){
printf("Its a method. %s=%s\n",varBuf,valBuf);
} else if ( type == VAR ) {
printf("Its a var. %s=%s\n",varBuf,valBuf);
}
line = strtok( NULL, LineDel );
}
}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
int main(int argc, char **argv, char **environ) {
pid_t pid;
int sockid;
int PORT;
// structs to hold addrs
struct sockaddr_in server_addr, client_addr;
char coffee_request[512]; // coffe request
struct mType mimeTypes[700];
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);
}
// while not killed accept sockets
while (1) {
int newsock;
int client_len = sizeof(client_addr);
int clientaddrlength;
clientaddrlength = sizeof(client_addr);
newsock = accept(sockid, (struct server_addr *) &client_addr, &clientaddrlength);
if (newsock < 0) {
perror("accept");
exit(-1);
}
if ( (pid = fork()) < 0) {
perror("Cannot fork");
exit(0);
}
else if( pid == 0 ) {
// spawn child process
int r;
char buff[1024];
int read_so_far = 0;
int bytesRcvd;
char ref[1024], rowRef[1024];
close(sockid);
memset(buff, 0, 1024);
// Read client request
if ((bytesRcvd = read(newsock, buff, RCVBUFSIZE - 1)) <= 0) {
perror("read");
exit(-1);
}
CoffeeRequestHandler(buff, newsock);
// stop and close socket
shutdown(newsock, 1);
close(newsock);
printf("\n");
exit(0);
}
// parent, dont care about this socket
close(newsock);
}
}