· 7 years ago · Apr 22, 2018, 11:52 PM
1#include "csapp.h"
2//#include "csapp.c"
3
4#define BUFFLEN 256
5#define SET 0
6#define GET 1
7#define DIGEST 2
8#define RUN 3
9
10//structs
11#include <string>
12#include <netinet/in.h>
13
14struct set_request{
15 uint32_t secretKey = 0;
16 uint16_t requestType = 0;
17 char two_b_pad[2] = {};
18 char varName[15] = {};
19 uint16_t valLength = 0;
20 char value[100]={};
21};
22
23struct init_request{
24 uint32_t secretKey = 0;
25 uint16_t requestType = 0;
26 char two_b_pad[2] = {};
27};
28/*
29struct set_request{
30 char varName[15] = {};
31 short valLength = 0;
32 char value[100]={};
33};
34*/
35struct get_request{
36 char varName[15] = {};
37};
38
39struct digest_request{
40 uint16_t valLength;
41 char * value;
42};
43
44struct run_request{
45 char * valid_value;
46};
47
48struct init_response{
49 signed char returnCode;
50 char three_b_pad[3];
51};
52
53struct get_response{
54 uint16_t valLength;
55 char * value;
56};
57
58int smallSet(char *MachineName, char* port, char* SecretKey, char *variableName, char *value){
59 int clientfd;
60 int set = SET;
61 rio_t rio;
62 char* host = MachineName;
63 printf("%d %s\n", atoi(port), MachineName);
64 //create connection
65 //clientfd = Open_clientfd("daan222.netlab.uky.edu", 3001);
66 clientfd = Open_clientfd(host, atoi(port));
67 Rio_readinitb(&rio, clientfd);
68
69
70 //initialBuf(initBuf,buf,4);
71 //Rio_writen(clientfd,buf,4);
72
73 printf("got this far\n");
74 Rio_writen(clientfd, SecretKey, sizeof(SecretKey)); //send SecretKey
75 Rio_readlineb(&rio, SecretKey, sizeof(SecretKey));
76
77 Rio_writen(clientfd, (char*)&set, set);
78 Rio_readlineb(&rio, (char*)&set, set);
79
80 Rio_writen(clientfd, variableName, sizeof(variableName)); //send variable name
81 Rio_readlineb(&rio, variableName, sizeof(variableName));
82
83 Rio_writen(clientfd, value, sizeof(value)); //send variable value
84 Rio_readlineb(&rio, value, sizeof(value));
85
86 Close(clientfd);
87
88 return 0;
89}
90
91int smallGet(char *MachineName, char* port, char* SecretKey, char *variableName, char *value){
92 int clientfd;
93 int set = SET;
94 char *host;
95 rio_t rio;
96 host = MachineName;
97 printf("got this far1\n");
98 //create connection
99 clientfd = Open_clientfd(host, atoi(port));
100 Rio_readinitb(&rio, clientfd);
101 printf("got this far\n");
102 Rio_writen(clientfd, SecretKey, sizeof(SecretKey)); //send SecretKey
103 Rio_readlineb(&rio, SecretKey, sizeof(SecretKey));
104
105 Rio_writen(clientfd, (char*)&set, set);
106 Rio_readlineb(&rio, (char*)&set, set);
107
108 Rio_writen(clientfd, variableName, sizeof(variableName)); //send variable name
109 Rio_readlineb(&rio, variableName, sizeof(variableName));
110
111 Rio_writen(clientfd, value, sizeof(value)); //send variable value
112 Rio_readlineb(&rio, value, sizeof(value));
113
114 Close(clientfd);
115
116 return 0;
117}
118
119int smallDigest(char *MachineName, int port, int SecretKey, char *data, int dataLength, char *result, int *resultLength){
120 return 0;
121}