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