-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe_networking.c
170 lines (151 loc) · 4.44 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
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
#include "pipe_networking.h"
//UPSTREAM = to the server / from the client
//DOWNSTREAM = to the client / from the server
/*=========================
server_setup
creates the WKP 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() {
int from_client;
char path[] = "/tmp/mario";
int a = mkfifo(path, 0666);
if (a == -1) {
perror("Creating WKP failed");
exit(1);
}
printf("Creating WKP succeeded");
// Blocks server until client opens pipe for writing
from_client = open(path, O_RDONLY);
if (from_client == -1) {
perror("Opening WKP failed");
remove(path);
exit(1);
}
printf("WKP opened");
remove(path);
//Returns file descriptor of pipe from client to server
return from_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 (Client's private pipe).
returns the file descriptor for the upstream pipe (see server setup).
=========================*/
int server_handshake(int *to_client) {
int from_client = server_setup();
char client_pipe[256];
if (read(from_client, client_pipe, sizeof(client_pipe)) <= 0) {
perror("Reading client pipe name failed");
close(from_client);
exit(1);
}
printf("Received client pipe name: %s\n", client_pipe);
*to_client = open(client_pipe, O_WRONLY);
if (*to_client == -1) {
perror("Opening client pipe failed");
close(from_client);
exit(1);
}
printf("Opened client pipe\n");
char confirmation[] = "Handshake successful!";
if (write(*to_client, confirmation, sizeof(confirmation)) == -1) {
perror("Writing to client pipe failed");
close(from_client);
close(*to_client);
exit(1);
}
printf("Writing to client pipe successful\n");
char ack[256];
if (read(from_client, ack, sizeof(ack)) <= 0) {
perror("Reading acknowledgment failed");
close(from_client);
close(*to_client);
exit(1);
}
printf("Received acknowledgment: %s\n", ack);
//Waiting, and eventually the client will know it exists and read the name
//Reads from the client, which has the other pipe
//Creates an unnamed pipe
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 pid[100];
sprintf(pid, "/tmp/%d", getpid());
//Client to server pipe is WKP, and Server to client pipe is named the pid
if (mkfifo(pid, 0666) == -1) {
perror("Error making server to client pipe");
exit(1);
}
*to_server = open("/tmp/mario", O_WRONLY);
if (*to_server == -1) {
perror("Opening WKP failed");
remove(pid);
exit(1);
}
if (write(*to_server, pid, sizeof(pid)) == -1) {
perror("Writing private pipe name failed");
close(*to_server);
remove(pid);
exit(1);
}
from_server = open(pid, O_RDONLY);
if (from_server == -1) {
perror("Opening private pipe failed");
close(*to_server);
remove(pid);
exit(1);
}
char synack[256];
if (read(from_server, synack, sizeof(synack)) <= 0) {
perror("Reading server confirmation failed");
close(*to_server);
close(from_server);
remove(pid);
exit(1);
}
printf("Received server confirmation: %s\n", synack);
char ack[] = "Acknowledgment received!";
if (write(*to_server, ack, sizeof(ack)) == -1) {
perror("Sending acknowledgment failed");
close(*to_server);
close(from_server);
remove(pid);
exit(1);
}
remove(pid);
return from_server;
}
/*=========================
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) {
int to_client = 0;
char client_pipe[256];
if (read(from_client, client_pipe, sizeof(client_pipe)) <= 0) {
perror("Reading client pipe name failed");
close(from_client);
exit(1);
}
to_client = open(client_pipe, O_WRONLY);
if (to_client == -1) {
perror("Opening client pipe failed");
close(from_client);
exit(1);
}
return to_client;
}