· 7 years ago · Apr 25, 2018, 05:42 AM
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#define SIXTYFIVE 65
11
12void set(struct variables *list, char* variableName, char* value, int* counter);
13char* get(char* variableName, struct variables *list, int *counter);
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\n");
105 FILE *fp;
106 char path[BUFFLEN];
107 char command[70];
108 int len;
109 struct digest_request dreq;
110 struct init_response ires;
111 ires.three_b_pad[0] = '\0';
112 ires.three_b_pad[1] = '\0';
113 ires.three_b_pad[2] = '\0';
114 ires.returnCode = 0;
115 rio_t rio;
116 Rio_readinitb(&rio, connfd);
117 read(connfd, &dreq, sizeof(dreq));
118
119 printf("value length is %d\n", ntohs(dreq.valLength));
120 printf("value is %s\n", dreq.val);
121
122 //get the command
123 len = snprintf(command, sizeof(command), "/bin/echo %s | /usr/bin/sha256sum",dreq.val);
124 if (len <= sizeof(command))
125 {
126 fp = popen(command, "r");
127 }
128
129 // Open the command for reading.
130 if (fp == NULL) {
131 printf("Failed to run command\n" );
132 }
133
134 // Read the output a line at a time to path
135 fgets(path, sizeof(path)-1, fp);
136
137 // close
138 pclose(fp);
139
140 path[SIXTYFIVE] = '\0';
141 printf("path: %s\n", path);
142
143 strcpy(ires.val, path);
144 ires.valLength = SIXTYFIVE;
145 printf("path: %s\n", ires.val);
146
147 write(connfd, &ires, sizeof(ires));
148 //read(connfd, &dreq.val, ntohs(dreq.valLength));
149
150 }
151
152 //RUN
153 else if(ntohs(sireq.requestType) == RUN){
154 printf("We don't actually have run, sorry\n");
155 //run();
156 }
157
158 //EVERYTHING ELSE
159 else {
160 sires.returnCode = -1;
161 }
162 printf("--------------------------\n");
163 Close(connfd);
164 printf("you made it all the way through once\n");
165 }
166 }
167}
168
169//helper functions
170
171void set(struct variables *list, char* variableName, char* value, int* counter){
172 //add values and variables to arrays
173 strcpy(list[*counter].varN, variableName);
174 strcpy(list[*counter].varV, value);
175 list[*counter].pos = *counter;
176 (*counter)++;
177}
178
179char* get(char* variableName, struct variables *list, int *counter){
180 //find index of variableName
181
182 for (int i=0; i<*counter; i++){
183 if(strcmp(list[i].varN,variableName)==0){
184 return list[i].varV;
185 }
186 }
187 return NULL;
188}