· 7 years ago · Apr 23, 2018, 04:46 PM
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <unistd.h> //read, write
5#include <sys/types.h>
6#include <sys/socket.h>
7#include <netdb.h>
8#include <iostream>
9#include <vector>
10#include "libsmall.hpp"
11
12using namespace std;
13
14#define NUM_ARGS 3
15#define MAX_LINE_SIZE 100
16
17struct return_set{
18 char return_code = 0;
19 char pad = '0';
20 char padd = '0';
21 char paddd = '0';
22 //used these three separate variables instead of pad[3] or maybe pad[4]? Wasn't sure how to
23 //do with 3 chars and the \0 char.
24};
25
26int main(int argc, char** argv){
27
28
29 //Process command-line args
30 if(argc != NUM_ARGS){
31 perror("Usage: ./smallserver PORT SECRET_KEY\n");
32 exit(EXIT_FAILURE);
33 }
34 int port = atoi(argv[1]);
35 unsigned int secret_key = atoi(argv[2]);
36
37
38 char padding_buff[2];
39 unsigned int secret_key_client; //Read in secret key
40 short request_type; //Read in request type
41
42 vector<string> variableList;
43 vector<string> valueList;
44
45 int listener_fd, communicator_fd; //File Descriptors
46 struct sockaddr_in server_address;
47
48 listener_fd = socket(AF_INET, SOCK_STREAM, 0);
49 bzero(&server_address, sizeof(server_address));
50
51 server_address.sin_family = AF_INET;
52 server_address.sin_addr.s_addr = htons(INADDR_ANY); //Listen for any IP addresses
53 server_address.sin_port = htons(port); //Listen on port given
54 bind(listener_fd, (struct sockaddr *) &server_address, sizeof(server_address));
55 listen(listener_fd, 1);
56 while(1){
57 //unsigned int key;
58 communicator_fd = accept(listener_fd, (struct sockaddr *) NULL, NULL);
59 read(communicator_fd, &secret_key_client, 4); //Read first 4 bytes (0-3) into buff. Secret key
60 secret_key_client = ntohl(secret_key_client);
61 read(communicator_fd, &request_type, 2); //Read next 2 bytes (5-6) into buff. Contains type of request.
62 read(communicator_fd, padding_buff, 2); //Read next 2 bytes (7-8) into buff. Contains two bytes of padding
63
64
65 //secret_key_client = ntohl(secret_key_client); //Flip byte order
66
67 //Validate secret key
68 if(secret_key_client != secret_key) {
69 cout << "Secret key = " << secret_key;
70 close(communicator_fd);
71 }
72 else {
73 request_type = htons(request_type);
74 //cout << "req: " << request_type << endl;
75
76 //What type of request is it?
77 if(request_type == 0){
78 //Set Request
79
80 char set_buff[16]; //Variable name
81 short val_len;
82 short val_check;
83 char value_buff[100]; //Value of variable
84 read(communicator_fd, set_buff, 16);
85 read(communicator_fd, &val_len, 2);
86 val_check = htonl(val_len);
87 val_len = htons(val_len);
88 if(val_check > 101){
89 exit(EXIT_FAILURE);
90 }
91 read(communicator_fd, value_buff, val_len+1);
92
93 string varname(set_buff);
94 string value(value_buff);
95
96 //Insert into vectors
97 variableList.push_back(string(set_buff));
98 valueList.push_back(string(value_buff));
99
100 cout << "Secret key = " << secret_key << endl;
101 cout << "Request type = set" << endl;
102 cout<< "Detail = " << string(set_buff) << endl;
103 cout << "Completion = success" << endl;
104 cout << "--------------------------" << endl;
105
106 //Send response back
107 return_set set;
108 write(communicator_fd, &set, 4);
109
110 }
111
112 if(request_type == 1){
113 //Get Request
114 char var_buff[16];
115 read(communicator_fd, var_buff, 16);
116 //cout << string(var_buff);
117 string returnstring;
118 bool success = false;
119 if(variableList.size() != 0){
120 for(unsigned int i = 0; i < variableList.size(); i++){
121 if(variableList[i] == string(var_buff)){
122 success = true;
123 returnstring = valueList[i];
124 }
125 }
126 }
127
128 if(success == true){
129 int stringlen = returnstring.length()+1;
130 char writestring[stringlen];
131 //strcpy(writestring, returnstring.c_str());
132 short length = sizeof(writestring);
133 short write_length = htons(length);
134 memset(writestring, '\0', sizeof(writestring));
135 write(communicator_fd, 0, 1);
136 write(communicator_fd, 0, 3);
137 write(communicator_fd, &write_length, 2);
138 write(communicator_fd, writestring, length);
139 }
140 else{
141 char failure = -1;
142 write(communicator_fd, &failure, 1);
143 }
144 cout << "Secret key = " << secret_key << endl;
145 cout << "Request type = get" << endl;
146 cout << "Detail = " << var_buff << endl;
147 if(success == true){
148 cout << "Completion = success" << endl;
149 cout << "--------------------------" << endl;
150 cout << returnstring << endl;
151 close(communicator_fd);
152 }
153 else{
154 cout << "Completion = failure" << endl;
155 cout << "--------------------------" << endl;
156 close(communicator_fd);
157 }
158
159
160
161 }
162
163 else if(request_type == 2){
164 //**PLEASE IGNORE, WE DID NOT CORRECTLY IMPLREMENT**
165 //Digest Request
166 //cout << "im now in digest" << endl;
167 short val_len;
168 char digest_buff[100];
169
170 read(communicator_fd, &val_len, 2);
171 val_len = htons(val_len);
172 if(val_len > 101){
173 exit(EXIT_FAILURE);
174 }
175
176 read(communicator_fd, digest_buff, val_len);
177
178 string value(digest_buff);
179
180 string command = "/bin/echo " + value + " | /usr/bin/sha256sum";
181 FILE* pipe = popen(command.c_str(), "r");
182 if(!pipe){
183 cout << "yea u thought" << endl;
184 exit(EXIT_FAILURE);
185 }
186 char buff[100];
187 fgets(buff, sizeof(buff),pipe);
188 pclose(pipe);
189 cout << "did this work lmao: " << string(buff);
190
191 //Send response
192
193 send(communicator_fd, 0, 1, 0); //Send success code (0 or -1)
194 char padding[3];
195 send(communicator_fd, padding, 3, 0);
196 short length = strlen(buff);
197 cout << "length: " << length;
198 length = htons(length);
199 send(communicator_fd, &length, 2, 0);
200 send(communicator_fd, buff, strlen(buff), 0);
201 }
202 }
203/*
204 else if(strcmp(secret_key_buff, "3") == 0){
205 //Run Request
206 }
207*/
208// else{
209// exit(EXIT_FAILURE);
210// }
211
212 }
213
214}