· 7 years ago · Apr 25, 2018, 06:20 AM
1//CS270 SPRING 2018
2//FILENAME: smalld.c
3//PURPOSE: daemon, recieves and proecesses message from smallServerLib
4//AUTHORS: Diego Andrade, Arthur Davis
5//SOURCES: http://csapp.cs.cmu.edu/2e/ics2/code/netp/echo.c
6// http://csapp.cs.cmu.edu/2e/ics2/code/netp/echoclient.c
7
8#include "csapp.h"
9#include "smallServerLib.c"
10#include "string.h"
11
12#define BUFF 256
13#define SET 0
14#define GET 1
15#define DIGEST 2
16#define RUN 3
17#define SIXTYFIVE 65
18
19void set(struct variables *list, char* variableName, char* value, int* counter);
20char* get(char* variableName, struct variables *list, int *counter);
21
22
23int main(int argc, char** argv){
24 //initialize all needed variables
25 int listenfd, connfd, port, SecretKey;
26 socklen_t clientLen;
27 rio_t rio;
28 struct sockaddr_in clientaddr;
29
30
31 int counter = 0;
32 struct variables list[100];
33
34 SecretKey = atoi(argv[2]);
35 port = atoi(argv[1]);
36
37 //listen for connection request
38 listenfd = Open_listenfd(port);
39
40 //establish connection
41 while(1){
42 clientLen = sizeof(clientaddr);
43 connfd = Accept(listenfd, (SA*)&clientaddr, &clientLen);
44 Gethostbyaddr((const char*)&clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr),AF_INET);
45 inet_ntoa(clientaddr.sin_addr);
46 //printf("%s %s\n", hp->h_name, haddrp);
47 Rio_readinitb(&rio, connfd);
48
49 //initialize init struct
50 struct init_request sireq;
51 Rio_readn(connfd, &sireq, sizeof(sireq));
52 struct init_response sires;
53
54 printf("\nSecret key = %d\n", ntohl(sireq.secretKey));
55
56
57 //if secret key is wrong, replies -1
58 if ((long)SecretKey != ntohl(sireq.secretKey)){
59
60
61 //return to client
62 sires.returnCode = -1;
63 Rio_writen(connfd, &sires, sizeof(sires));
64 }
65
66 //else do this switch if statement tree
67 else{
68 //SET
69 if(ntohs(sireq.requestType == SET)){
70 printf("Request type = set\n");
71 struct init_response ires;
72 struct set_request sreq;
73 ires.three_b_pad[0] = '\0';
74 ires.three_b_pad[0] = '\0';
75 ires.three_b_pad[0] = '\0';
76 ires.returnCode = 0;
77 rio_t rio;
78 Rio_readinitb(&rio,connfd);
79 read(connfd, &sreq, sizeof(sreq));
80 write(connfd, &ires, sizeof(ires));
81 set(list, sreq.varName, sreq.val, &counter);
82 printf("Detail = %s: %s\n", sreq.varName, sreq.val);
83 }
84
85 //GET
86 else if(ntohs(sireq.requestType) == GET){
87 printf("Request type = get\n");
88 struct get_request greq;
89 struct init_response ires;
90 ires.three_b_pad[0] = '\0';
91 ires.three_b_pad[1] = '\0';
92 ires.three_b_pad[2] = '\0';
93 ires.returnCode = 0;
94 rio_t rio;
95
96 Rio_readinitb(&rio, connfd);
97 read(connfd, &greq, sizeof(greq));
98
99 char* varVal = get(greq.varName, list, &counter);
100 ires.valLength = (strlen(varVal));
101 strcpy(ires.val, varVal);
102 printf("value of %s is %s\n", greq.varName, ires.val);// This prints the correct thing, so get() works
103 Rio_writen(connfd, &ires, SIX+BUFFLEN);
104
105 }
106
107 //DIGEST
108 else if(ntohs(sireq.requestType) == DIGEST){
109 printf("Request type = digest\n");
110 FILE *fp;
111 char path[BUFFLEN];
112 char command[70];
113 int len;
114 struct digest_request dreq;
115 struct init_response ires;
116 ires.three_b_pad[0] = '\0';
117 ires.three_b_pad[1] = '\0';
118 ires.three_b_pad[2] = '\0';
119 ires.returnCode = 0;
120 rio_t rio;
121 Rio_readinitb(&rio, connfd);
122 Rio_readn(connfd, &dreq, BUFFLEN+sizeof(dreq)+1);
123
124
125 printf("Detail = %s\n", dreq.val);
126
127 //get the command
128 len = snprintf(command, sizeof(command), "/bin/echo %s | /usr/bin/sha256sum",dreq.val);
129 if (len <= sizeof(command))
130 {
131 fp = popen(command, "r");
132 }
133
134 // Open the command for reading.
135 if (fp == NULL) {
136 printf("Failed to run command\n" );
137 }
138
139 // Read the output a line at a time to path
140 fgets(path, sizeof(path)-1, fp);
141
142 // close
143 pclose(fp);
144
145 path[SIXTYFIVE] = '\0';
146
147 strcpy(ires.val, path);
148 ires.valLength = SIXTYFIVE;
149
150 write(connfd, &ires, sizeof(ires));
151
152 }
153
154 //RUN
155 else if(ntohs(sireq.requestType) == RUN){
156 printf("We don't actually have run, sorry\n");
157 //run();
158 }
159
160 //EVERYTHING ELSE
161 else {
162 sires.returnCode = -1;
163 }
164 printf("--------------------------\n");
165 Close(connfd);
166 }
167 }
168}
169
170//helper functions
171
172void set(struct variables *list, char* variableName, char* value, int* counter){
173 //add values and variables to arrays
174 for(int i = 0; i < *counter; i ++){
175 if(strcmp(list[i].varN,variableName)==0){
176 strcpy(list[*counter].varV, value);
177 return;
178 }
179 }
180 strcpy(list[*counter].varN, variableName);
181 strcpy(list[*counter].varV, value);
182 list[*counter].pos = *counter;
183 (*counter)++;
184}
185
186char* get(char* variableName, struct variables *list, int *counter){
187 //find index of variableName
188
189 for (int i=0; i<*counter; i++){
190 if(strcmp(list[i].varN,variableName)==0){
191 return list[i].varV;
192 }
193 }
194 return "failure";
195}