-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe_networking.c
144 lines (111 loc) · 3.59 KB
/
pipe_networking.c
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
#include "pipe_networking.h"
/*=========================
server_setup
args:
creates the WKP (upstream) and opens it, waiting for a
connection.
removes the WKP once a connection has been made
returns the file descriptor for the upstream pipe.
=========================*/
int server_setup() {
mkfifo(WKP, 0644);
int from_client = open(WKP, O_RDONLY, 0);
remove(WKP);
return from_client;
}
/*=========================
server_connect
args: int from_client
handles the subserver portion of the 3 way handshake
returns the file descriptor for the downstream pipe.
=========================*/
int server_connect(int from_client) {
char buffer[HANDSHAKE_BUFFER_SIZE] = {0};
read(from_client, buffer, sizeof(buffer));
printf("[server] handshake received: -%s-\n", buffer);
int to_client = open(buffer, O_WRONLY, 0);
srand(time(NULL));
int r = rand() % HANDSHAKE_BUFFER_SIZE;
sprintf(buffer, "%d", r);
write(to_client, buffer, sizeof(buffer));
read(from_client, buffer, sizeof(buffer));
printf("[server] handshake received: -%s-\n", buffer);
return to_client;
}
/*=========================
server_handshake
args: int * to_client
Performs the server side pipe 3 way handshake.
Sets *to_client to the file descriptor to the downstream pipe.
returns the file descriptor for the upstream pipe.
=========================*/
int server_handshake(int *to_client) {
int b, from_client;
char buffer[HANDSHAKE_BUFFER_SIZE];
printf("[server] handshake: making wkp\n");
b = mkfifo(WKP, 0600);
if ( b == -1 ) {
printf("mkfifo error %d: %s\n", errno, strerror(errno));
exit(-1);
}
//open & block
from_client = open(WKP, O_RDONLY, 0);
//remove WKP
remove(WKP);
printf("[server] handshake: removed wkp\n");
//read initial message
b = read(from_client, buffer, sizeof(buffer));
printf("[server] handshake received: -%s-\n", buffer);
*to_client = open(buffer, O_WRONLY, 0);
//create SYN_ACK message
srand(time(NULL));
int r = rand() % HANDSHAKE_BUFFER_SIZE;
sprintf(buffer, "%d", r);
write(*to_client, buffer, sizeof(buffer));
//rad and check ACK
read(from_client, buffer, sizeof(buffer));
int ra = atoi(buffer);
if (ra != r+1) {
printf("[server] handshake received bad ACK: -%s-\n", buffer);
exit(0);
}//bad response
printf("[server] handshake received: -%s-\n", buffer);
return from_client;
}
/*=========================
client_handshake
args: int * to_server
Performs the client side pipe 3 way handshake.
Sets *to_server to the file descriptor for the upstream pipe.
returns the file descriptor for the downstream pipe.
=========================*/
int client_handshake(int *to_server) {
int from_server;
char buffer[HANDSHAKE_BUFFER_SIZE];
char ppname[HANDSHAKE_BUFFER_SIZE];
//make private pipe
printf("[client] handshake: making pp\n");
sprintf(ppname, "%d", getpid() );
mkfifo(ppname, 0600);
//send pp name to server
printf("[client] handshake: connecting to wkp\n");
*to_server = open( WKP, O_WRONLY, 0);
if ( *to_server == -1 ) {
printf("open error %d: %s\n", errno, strerror(errno));
exit(1);
}
write(*to_server, ppname, sizeof(buffer));
//open and wait for connection
from_server = open(ppname, O_RDONLY, 0);
read(from_server, buffer, sizeof(buffer));
/*validate buffer code goes here */
printf("[client] handshake: received -%s-\n", buffer);
//remove pp
remove(ppname);
printf("[client] handshake: removed pp\n");
//send ACK to server
int r = atoi(buffer) + 1;
sprintf(buffer, "%d", r);
write(*to_server, buffer, sizeof(buffer));
return from_server;
}