· 7 years ago · Apr 23, 2018, 11:26 AM
1#include "csapp.h"
2#include "smallServerLib.c"
3
4#define BUFFLEN 256
5#define SET 0
6#define GET 1
7#define DIGEST 2
8#define RUN 3
9
10void set(char* variableName, char* value, char** varnamearray, char** valarray, int* counter);
11char* get(char* variableName, char** varnamearray, char** valarray, int counter);
12
13int main(int argc, char** argv){
14 //initialize all needed variables
15 int listenfd, connfd, port, SecretKey;
16 socklen_t clientLen;
17 rio_t rio;
18 struct sockaddr_in clientaddr;
19 struct hostent *hp;
20 char *haddrp;
21 char** varnamearray;
22 char** valarray;
23 int counter =0;
24
25 SecretKey = atoi(argv[2]);
26 port = atoi(argv[1]);
27
28 //listen for connection request
29 listenfd = Open_listenfd(port);
30
31 //establish connection
32 while(1){
33 clientLen = sizeof(clientaddr);
34 connfd = Accept(listenfd, (SA*)&clientaddr, &clientLen);
35 hp = Gethostbyaddr((const char*)&clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr),AF_INET);
36 haddrp = inet_ntoa(clientaddr.sin_addr);
37 printf("%s %s\n", hp->h_name, haddrp);
38 Rio_readinitb(&rio, connfd);
39
40 //initialize init struct
41 struct init_request sireq;
42 Rio_readn(connfd, &sireq, sizeof(sireq));
43 struct init_response sires;
44
45 //if secret key is wrong, replies -1
46 if ((long)SecretKey != ntohl(sireq.secretKey)){
47 printf("\nSecret key = %d\n", ntohl(sireq.secretKey));
48 printf("--------------------------\n");
49
50 //return to client
51 sires.returnCode = -1;
52 Rio_writen(connfd, &sires, sizeof(sires));
53 }
54
55 //else do this switch if statement tree
56 else{
57 //SET
58 if(ntohs(sireq.requestType == SET)){
59 //return to client
60 sires.returnCode = 0;
61 Rio_writen(connfd, &sires, sizeof(sires));
62
63 char svariableName[15];
64 char svalue[100];
65 struct set_request sreq;
66
67 Rio_readn(connfd, &sreq, 19+strlen(sreq.val));
68 strcpy(svariableName, sreq.varName);
69 strcpy(svalue, sreq.val);
70 set(svariableName, svalue, varnamearray, valarray, &counter);
71 }
72
73 //GET
74 else if(ntohs(sireq.requestType) == GET){
75 //return to client
76 sires.returnCode = 0;
77 Rio_writen(connfd, &sires, sizeof(sires));
78
79 char svariableName[15];
80 struct get_request sreq;
81
82 Rio_readn(connfd, &sreq, sizeof(svariableName));
83 strcpy(svariableName, sreq.varName);
84 char* retVal = get(svariableName, varnamearray, valarray, counter);
85 printf("return value: %s\n", retVal);
86 }
87
88 //DIGEST
89 else if(ntohl(sireq.requestType) == DIGEST){
90 printf("digest");
91 digest();
92 }
93
94 //RUN
95 else if(ntohl(sireq.requestType) == RUN){
96 printf("run");
97 run();
98 }
99
100 //EVERYTHING ELSE
101 else {
102 sires.returnCode = -1;
103 }
104
105 printf("you made it all the way through once\n");
106 }
107 }
108 return 0;
109}
110
111//helper functions
112
113void set(char* variableName, char* value, char** varnamearray, char** valarray, int* counter){
114 //add values and variables to arrays
115 varnamearray[*counter] = variableName;
116 valarray[*counter] = value;
117 printf("name: %s\n", varnamearray[*counter]);
118 printf("value: %s\n", valarray[*counter]);
119 (counter)++;
120}
121
122char* get(char* variableName, char** varnamearray, char** valarray, int counter){
123 //find index of variableName
124 printf("namecorrect: %s\n", variableName);
125 printf("counter: %d\n", counter);
126 for (int i=0; i<counter; i++){
127 printf("namearray: %s\n", varnamearray[i]);
128 printf("valuearray: %s\n", valarray[i]);
129 if (varnamearray[i]==variableName){
130 printf("hiday");
131 return valarray[i];
132 }
133 }
134 return 0;
135}
136
137int digest(){
138 return 0;
139}
140
141int run(){
142 return 0;
143}