· 7 years ago · Apr 25, 2018, 03:36 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
11void set(struct variables *list, char* variableName, char* value, int* counter);
12char* get(char* variableName, struct variables *list, int *counter);
13
14int main(int argc, char** argv){
15 //initialize all needed variables
16 int listenfd, connfd, port, SecretKey;
17 socklen_t clientLen;
18 rio_t rio;
19 struct sockaddr_in clientaddr;
20// struct hostent *hp;
21 //char *haddrp;
22
23 int counter = 0;
24 struct variables list[100];
25
26 SecretKey = atoi(argv[2]);
27 port = atoi(argv[1]);
28
29 //listen for connection request
30 listenfd = Open_listenfd(port);
31
32 //establish connection
33 while(1){
34 clientLen = sizeof(clientaddr);
35 connfd = Accept(listenfd, (SA*)&clientaddr, &clientLen);
36 Gethostbyaddr((const char*)&clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr),AF_INET);
37 inet_ntoa(clientaddr.sin_addr);
38 //printf("%s %s\n", hp->h_name, haddrp);
39 Rio_readinitb(&rio, connfd);
40
41 //initialize init struct
42 struct init_request sireq;
43 Rio_readn(connfd, &sireq, sizeof(sireq));
44 struct init_response sires;
45
46 printf("\nSecret key = %d\n", ntohl(sireq.secretKey));
47
48
49 //if secret key is wrong, replies -1
50 if ((long)SecretKey != ntohl(sireq.secretKey)){
51
52
53 //return to client
54 sires.returnCode = -1;
55 Rio_writen(connfd, &sires, sizeof(sires));
56 }
57
58 //else do this switch if statement tree
59 else{
60 //SET
61 if(ntohs(sireq.requestType == SET)){
62 printf("Request type = set\n");
63 struct init_response ires;
64 struct set_request sreq;
65 ires.three_b_pad[0] = '\0';
66 ires.three_b_pad[0] = '\0';
67 ires.three_b_pad[0] = '\0';
68 ires.returnCode = 0;
69 rio_t rio;
70 Rio_readinitb(&rio,connfd);
71 read(connfd, &sreq, sizeof(sreq));
72 write(connfd, &ires, sizeof(ires));
73 set(list, sreq.varName, sreq.val, &counter);
74 printf("variable is %s\n", sreq.varName);
75 printf("value is %s\n", sreq.val);
76 }
77
78 //GET
79 else if(ntohs(sireq.requestType) == GET){
80 printf("Request type = get\n");
81 struct get_request greq;
82 struct init_response ires;
83 ires.three_b_pad[0] = '\0';
84 ires.three_b_pad[1] = '\0';
85 ires.three_b_pad[2] = '\0';
86 ires.returnCode = 0;
87 rio_t rio;
88
89 Rio_readinitb(&rio, connfd);
90 read(connfd, &greq, sizeof(greq));
91
92 char* varVal = get(greq.varName, list, &counter);
93 ires.valLength = (strlen(varVal));
94 //int vLength =ires.valLength;
95 strcpy(ires.val, varVal);
96 printf("value of %s is %s\n", greq.varName, ires.val);// This prints the correct thing, so get() works
97 Rio_writen(connfd, &ires, SIX+BUFFLEN);
98
99 }
100
101 //DIGEST
102 else if(ntohs(sireq.requestType) == DIGEST){
103 printf("Request type = digest\n");
104 FILE *fp;
105 char path[BUFFLEN];
106 char command[70];
107 int len;
108 struct digest_request dreq;
109 struct init_response ires;
110 ires.three_b_pad[0] = '\0';
111 ires.three_b_pad[1] = '\0';
112 ires.three_b_pad[2] = '\0';
113 ires.returnCode = 0;
114 rio_t rio;
115 Rio_readinitb(&rio, connfd);
116 read(connfd, &dreq, sizeof(dreq));
117
118 printf("value length is %d\n", ntohs(dreq.valLength));
119 printf("value is %s\n", dreq.val);
120
121 //get the command
122 len = snprintf(command, sizeof(command), "/bin/echo %s | /usr/bin/sha256sum",dreq.val);
123 if (len <= sizeof(command))
124 {
125 fp = popen(command, "r");
126 }
127
128 // Open the command for reading.
129 if (fp == NULL) {
130 printf("Failed to run command\n" );
131 }
132
133 // Read the output a line at a time to path
134 fgets(path, sizeof(path)-1, fp);
135
136 // close
137 pclose(fp);
138
139 path[65] = '\0';
140 printf("path: %s\n", path);
141 printf("size: %d\n", 65);
142
143 strcpy(ires.val, path);
144 ires.valLength = 65;
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}