Solution to first assignment.
First assignment was to implement a FTP Server and Client
using SUN RPC. This is in C. Doesn't support binary file
formats yet. Both binary file format transfer support and
Python equivalent of the code is to be added yet.
#include <rpc/rpc.h>
#include <stdio.h>
#include "ftp.h"
extern __thread int errno;
readfile_res* retrieve_file_1_svc(request *req, struct svc_req *rqstp)
{
FILE *file;
char data[1024];
int bytes;
static readfile_res res;
file = fopen(req->name, "r");
if (file == NULL) {
res.errno = errno;
return (&res);
}
xdr_free((xdrproc_t)xdr_readfile_res, (char *)&res);
fseek (file, req->start, SEEK_SET);
bytes = fread(data, 1, MAXLEN, file);
res.readfile_res_u.chunk.data = data;
res.readfile_res_u.chunk.bytes = bytes;
/*
* Return the result
*/
res.errno = 0;
fclose(file);
return (&res);
}
int* send_file_1_svc(chunksend *rec, struct svc_req *rqstp)
{
FILE *file;
int write_bytes;
static int result;
file = fopen(rec->name, "a");
if (file == NULL) {
result = errno;
return &result;
}
write_bytes = fwrite(rec->data, 1, rec->bytes, file);
fclose(file);
result = 0;
return &result;
}