-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrclient.c
More file actions
408 lines (333 loc) · 10.5 KB
/
rclient.c
File metadata and controls
408 lines (333 loc) · 10.5 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// rclient.c
// Andrew Mauragis
// Due 4/25/13
//
// This file runs the remote procedure call client. It must be compiled and linked into
// a client program with the function entry(argc int, char** argv). The client will set
// up a socket and provide RPC calls, then call entry after appropriately trimming the
// arguments.
//
// This client works with rserver.c and implements open, close, read, write, and lseek
// on the server.
#include <sys/socket.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <stdlib.h>
#include <errno.h>
#include "rpcdefs.h"
#include "rclient.h"
// need a static file descriptor representing the connection to the server
static int connection = -1;
// main
// Connects to the server and passes the correct arguments to the client program
// after the client returns, closes the connection and exits.
//
// expects a minimum of two arguments, host name and port. Any further arugments
// are transparantly passed to the client program
//
// returns an error code, as expected.
int main (int argc, char* argv[])
{
// make sure we have at least 2 arguments
if (argc < 3)
{
fprintf(stderr,"Not enough arguments");
exit(ARGS_ERROR);
}
char* remhost;
unsigned short remport;
// get remote host and port
remhost = argv[1];
remport = atoi(argv[2]);
// set up TCP socket
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
perror("Socket error");
exit(SOCKET_ERROR);
}
// initialize socket struct
sockaddr_in_t s;
memset(&s, 0, sizeof(sockaddr_in_t));
// get port number and address
struct hostent* hostaddr = gethostbyname(remhost);
if(hostaddr == NULL)
{
perror("GetHost error");
return GETHOST_ERROR;
}
// populate socket struct
s.sin_family = AF_INET;
memcpy(&s.sin_addr, hostaddr->h_addr, hostaddr->h_length);
s.sin_port = htons(remport);
// attempt to connect to server
if (-1 == connect(sock, (sockaddr_t*)&s, sizeof(sockaddr_in_t)))
{
perror("Cannot connect");
return CONNECTION_ERROR;
}
// now that we have the socket working, we copy it into the static variable
connection = sock;
// everything shoudl be setup and we're ready to invoke the client program
char** newArgv = (char**)calloc((1+(argc-3)), sizeof(char*));
newArgv[0] = "user_program";
int i;
for (i=3; i<argc; i++)
{
newArgv[i-2] = argv[i];
}
int newArgc = (1+(argc-3));
// Call user program
int ret = entry(newArgc, newArgv);
// Free memory
free(newArgv);
// close socket, we're done
close(connection);
return ret;
}
// Open
// RPC call, implements open() with the target on the remote server
//
// Functions by gathering arguments, packetizing them, sending them
// to the server and reading back the response for returning
//
// expects a path name, open flags, and open mode
// returns an error code/fd that matches the open() called remotely
int Open(const char* pathname, int flags, mode_t mode)
{
// puts("OPEN!");
// first we calculate the length we need
// 1 byte for opcode, path name, null, flags
int path_length = strlen(pathname);
int pkt_length = 1 + path_length + 1 + sizeof(int) + sizeof(mode_t);
// now we initalize an rpc packet
int pktIndex = 0;
unsigned char pkt[pkt_length];
memset(pkt, 0, pkt_length);
// copy in the opcode first
pkt[pktIndex] = OPCODE_OPEN;
pktIndex++;
// copy in path (first arg)
memcpy(pkt+pktIndex, pathname, path_length);
pktIndex += path_length;
// copy in null terminator
pkt[pktIndex] = 0;
pktIndex++;
// copy flags
memcpy(pkt+pktIndex, &flags, sizeof(int));
pktIndex += sizeof(int);
// copy mode
memcpy(pkt+pktIndex, &mode, sizeof(mode_t));
pktIndex += sizeof(mode_t);
if (connection == -1)
{
perror("Static socket invalid");
return CONNECTION_ERROR;
}
// write packet to server
int writeval = write(connection, pkt, pkt_length);
if (writeval < 0) return WRITE_ERROR;
// get response from server
int response[2];
int readval = read(connection, response, 2*sizeof(int));
if (readval < 0) return READ_ERROR;
int func_return = response[0];
int func_errno = response[1];
// set up errno and return the return value
errno = func_errno;
return func_return;
}
// Close
// RPC call, implements close() with the target on the remote server
//
// Functions by gathering arguments, packetizing them, sending them
// to the server and reading back the response for returning
//
// expects a file descriptor
// returns an error code that matches the close() called remotely
int Close(int fd)
{
// puts("CLOSE!");
// length is opcode + file descriptor
int pktLength = (1 + sizeof(int));
// create packet
int index = 0;
unsigned char pkt[pktLength];
// copy in opcode
pkt[index] = OPCODE_CLOSE;
index++;
// copy in file descriptor
memcpy(&pkt[index], &fd, sizeof(int));
// verify socket is correct
if (connection == -1)
{
perror("Static socket invalid");
return CONNECTION_ERROR;
}
// write packet to server
int writeval = write(connection, pkt, pktLength);
if (writeval < 0) return WRITE_ERROR;
// get response from server
int response[2];
int readval = read(connection, response, 2*sizeof(int));
if (readval < 0) return READ_ERROR;
int func_return = response[0];
int func_errno = response[1];
// set up errno and return the return value
errno = func_errno;
return func_return;
}
// Read
// RPC call, implements read() with the target on the remote server
//
// Functions by gathering arguments, packetizing them, sending them
// to the server and reading back the response for returning and the
// buffer that was read into.
//
// expects a file descriptor, pointer to a buffer, and size
// return value that matches the read() called remotely
ssize_t Read(int fd, void* buf, size_t count)
{
// length will be opcode + fd + count
int pktLength = 1 + sizeof(int) + sizeof(size_t);
// create a packet
int pktIndex = 0;
unsigned char pkt[pktLength];
// copy in opcode
pkt[pktIndex] = OPCODE_READ;
pktIndex++;
// copy fd
memcpy(pkt+pktIndex, &fd, sizeof(int));
pktIndex += sizeof(int);
// copy count
memcpy(pkt+pktIndex, &count, sizeof(size_t));
pktIndex += sizeof(size_t);
// verify socket is correct
if (connection == -1)
{
perror("Static socket invalid");
return CONNECTION_ERROR;
}
// write packet to server
int writeval = write(connection, pkt, pktLength);
if (writeval < 0) return WRITE_ERROR;
// get response from server
int response[2];
void* readbuf[count];
int readval = read(connection, response, 2*sizeof(int));
if (readval < 0) return READ_ERROR;
// get read buffer from server
readval = read(connection, readbuf, count);
int func_return = response[0];
int func_errno = response[1];
// copy read buffer into pointer specified by caller
memcpy(buf, readbuf, count);
// set up errno and return the return value
errno = func_errno;
return func_return;
}
// Write
// RPC call, implements write() with the target on the remote server
//
// Functions by gathering arguments and buffer data, packetizing them,
// sending them to the server and reading back the response
//
// Note: we had to switch count and buffer in the packet order so we
// know how big to make the buffer, and know where count will be in
// the packet
//
// expects a file descriptor, pointer to a buffer, and size
// return value that matches the write() called remotely
ssize_t Write(int fd, const void* buf, size_t count)
{
// puts("WRITE!");
// message length: opcode + fd + buffer length + count
int pktLength = 1 + sizeof(int) + count + sizeof(size_t);
// build the packet
int pktIndex = 0;
unsigned char pkt[pktLength];
// copy in opcode
pkt[pktIndex] = OPCODE_WRITE;
pktIndex++;
// copy in fd
memcpy(pkt+pktIndex, &fd, sizeof(int));
pktIndex += sizeof(int);
// copy in count
// had to switch this around with buf so we know where count
// will be in the packet
memcpy(pkt+pktIndex, &count, sizeof(size_t));
pktIndex += sizeof(size_t);
// copy in buf
memcpy(pkt+pktIndex, buf, count);
pktIndex += count;
// verify socket is correct
if (connection == -1)
{
perror("Static socket invalid");
return CONNECTION_ERROR;
}
// write packet to server
int writeval = write(connection, pkt, pktLength);
if (writeval < 0) return WRITE_ERROR;
// get response from server
int response[2];
int readval = read(connection, response, 2*sizeof(int));
if (readval < 0) return READ_ERROR;
int func_return = response[0];
int func_errno = response[1];
// set up errno and return the return value
errno = func_errno;
return func_return;
}
// Lseek
// RPC call, implements lseek() with the target on the remote server
//
// Functions by gathering arguments, packetizing them, sending them
// to the server and reading back the response for returning
//
// expects a file descriptor, offset, and whence value
// returns an error code that matches the lseek() called remotely
off_t Lseek(int fd, off_t offset, int whence)
{
// length: opcode, fd, offset, whence
int pktLength = 1+ 2*sizeof(int) + sizeof(off_t);
// build packet
int pktIndex = 0;
unsigned char pkt[pktIndex];
// copy opcode
pkt[pktIndex] = OPCODE_SEEK;
pktIndex++;
// copy fd
memcpy(pkt+pktIndex, &fd, sizeof(int));
pktIndex += sizeof(int);
// copy offset
memcpy(pkt+pktIndex, &offset, sizeof(off_t));
pktIndex += sizeof(off_t);
// copy whence
memcpy(pkt+pktIndex, &whence, sizeof(int));
pktIndex += sizeof(int);
// verify socket is correct
if (connection == -1)
{
perror("Static socket invalid");
return CONNECTION_ERROR;
}
// write packet to server
int writeval = write(connection, pkt, pktLength);
if (writeval < 0) return WRITE_ERROR;
// get response from server
int response[2];
int readval = read(connection, response, 2*sizeof(int));
if (readval < 0) return READ_ERROR;
int func_return = response[0];
int func_errno = response[1];
// set up errno and return the return value
errno = func_errno;
return func_return;
}