-
Notifications
You must be signed in to change notification settings - Fork 0
/
domU_acceptor_server.cpp
228 lines (186 loc) · 6.07 KB
/
domU_acceptor_server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
// #include <iostream>
// #include <fstream>
#include </home/scorpion/mtechProj/hmac.cpp> // include crypto functions
#define CREATE_SERVER_SOCKET(port) \
int sockfd, newsockfd, portno;\
socklen_t clilen;\
struct sockaddr_in serv_addr, cli_addr;\
/*create a socket file descriptor */ \
sockfd = socket(AF_INET, SOCK_STREAM, 0); \
if (sockfd < 0) \
error("ERROR opening socket"); \
bzero((char *) &serv_addr, sizeof(serv_addr)); /* set server address bits to zero*/ \
portno = port; \
serv_addr.sin_family = AF_INET; \
serv_addr.sin_addr.s_addr = INADDR_ANY; /* INADDR_ANY is server ip address*/ \
serv_addr.sin_port = htons(portno); /* htons converts port number in host byte order to a port number in network byte order.*/ \
/* bind the server to port and host ... this steps makes it a server socket */ \
if (bind(sockfd, (struct sockaddr *) &serv_addr, \
sizeof(serv_addr)) < 0) \
error("ERROR on binding");
#define CREATE_CLIENT_SOCKET(port,ser) \
int32_t sockfd, portno, n;\
struct sockaddr_in serv_addr;\
struct hostent *server;\
sockfd = socket(AF_INET, SOCK_STREAM, 0); \
if (sockfd < 0) \
error("ERROR opening socket");\
/*Get the port number and server */ \
portno = port; \
server = ser; \
if (server == NULL) {\
fprintf(stderr,"ERROR, no such host\n"); \
exit(0); \
} \
/*Initialie socket port, address and address family */ \
bzero((char *) &serv_addr, sizeof(serv_addr)); \
serv_addr.sin_family = AF_INET; \
bcopy((char *)server->h_addr, \
(char *)&serv_addr.sin_addr.s_addr, \
server->h_length); \
serv_addr.sin_port = htons(portno); \
/*connect to server */ \
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) \
error("ERROR connecting");
typedef unsigned char byte;
struct bundle{
int a,b;
byte eflag;
byte nonce[16];
byte content_ab[32]; // fixing the content size to 32 bytes for now
byte hmac[32];
byte inputs;
int outputs;
};
typedef struct bundle packet;
using namespace std;
int block_recv(const int fd, char* data, unsigned int len)
{
int i, j = 0;
while (len > 0) {
i = recv(fd, data, len, 0);
if (i <= 0) {
return -1;
}
data += i;
len -= i;
j += i;
}
return j;
}
void error(const char *msg)
{
perror(msg);
exit(1);
}
void fetch_content(packet* p, int a, int b)
{
byte mem[1000];
for(int i = 0; i < 1000;i++)
{
mem[i] = i;
}
int count = 0;
for(int i = a;i<=b;i++)
p->content_ab[count++] = mem[i];
}
int verify_sign(byte* msg, int len, byte* sign)
{
EVP_PKEY *vkey = NULL;
int rc = make_skey(&vkey); /* generate vkey */
assert(rc == 0);
if(rc != 0)
exit(1);
assert(vkey != NULL);
if(vkey == NULL)
exit(1);
printf("%d\n", sizeof(sign));
rc = verify_it(msg, len, sign, 32, vkey);
return rc;
}
void fwdReqToDom0(packet p)
{
CREATE_CLIENT_SOCKET(7891,gethostbyname("localhost"))
// send packet
int left = sizeof(p);
int wc;
while (left) {
wc = write(sockfd, (byte*)(&p) + sizeof(p) - left, left);
if (wc < 0)
error("ERROR writing to socket");
left -= wc;
}
byte sendBuffer[256];
bzero(sendBuffer,sizeof(sendBuffer));
n = read(sockfd,sendBuffer,sizeof(sendBuffer) -1 );
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",sendBuffer);
close(sockfd);
}
int main(int argc, char *argv[])
{
//Declaration section
packet p;
byte buffer[256];
int x[10] = {0,32,64,96,128,170,192,224,256,288};
int y[10] = {31,63,95,127,169,191,223,255,287,319};
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
CREATE_SERVER_SOCKET(atoi(argv[1])) // creates and binds the server socket
// while(1){ /* listen continuously */
listen(sockfd,5);
// accept a connection
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
if(block_recv(newsockfd, (char*)(&p), sizeof(p)) != sizeof(p))
{
error("ERROR while reading from socket");
}
p.a = ntohl(p.a);
printf("\na: %d\n",p.a);
p.b = ntohl(p.b);
printf("b: %d\n\n",p.b);
print_it("nonce",p.nonce,sizeof(p.nonce));
print_it("signature",p.hmac,sizeof(p.hmac));
printf("eflag: %d\n", p.eflag);
// verify the signature to check integrity of request ..code commented due to some problems
// int rc = verify_sign((byte*)&p,(sizeof(int)*2)+17,p.hmac);
// if(rc == 0) {
// printf("Verified signature\n");
// } else {
// printf("Failed to verify signature, return code %d\n", rc);
// close(newsockfd);
// continue; /* close socket start listening again */
// }
bzero(p.hmac,sizeof(p.hmac)); /*remove signature after verification*/
fetch_content(&p,p.a,p.b); /*Fetch contents of memory between locations a & b*/
print_it("Content[a,b]",p.content_ab,sizeof(p.content_ab));
p.inputs = 5; /* give a random input */
int n = write(newsockfd,"app dom received request",24);
if (n < 0) error("ERROR writing to socket");
close(newsockfd);
close(sockfd);
//send data to control domain
// we can later use a seperate thread to do this instead of a function call
fwdReqToDom0(p);
// }
return 0;
}