ftp/ftps.c
author Madhusudan.C.S <madhusudancs@gmail.com>
Mon, 18 Jan 2010 14:10:13 +0530
changeset 0 30d751ac6d49
child 1 47438813ede2
permissions -rw-r--r--
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.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
#include <rpc/rpc.h>
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
#include <stdio.h>
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
#include "ftp.h"
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
extern __thread int errno;
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
readfile_res* retrieve_file_1_svc(request *req, struct svc_req *rqstp)
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
{
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
	FILE *file;
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    10
	char data[1024];
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    11
	int bytes;
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
	static readfile_res res;
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    13
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
    file = fopen(req->name, "r");
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    15
	if (file == NULL) {
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    16
		res.errno = errno;
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    17
		return (&res);
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    18
    }
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    19
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    20
    xdr_free((xdrproc_t)xdr_readfile_res, (char *)&res);
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
    fseek (file, req->start, SEEK_SET);
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    23
	bytes = fread(data, 1, MAXLEN, file);
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    24
    res.readfile_res_u.chunk.data = data;
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    25
	res.readfile_res_u.chunk.bytes = bytes;
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    26
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    27
	/*
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    28
     * Return the result
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    29
     */
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    30
    res.errno = 0;
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    31
    fclose(file);
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    32
	return (&res);
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    33
}
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    34
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    35
int* send_file_1_svc(chunksend *rec, struct svc_req *rqstp)
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    36
{
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    37
	FILE *file;
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    38
    int write_bytes;
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    39
    static int result;
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    40
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    41
	file = fopen(rec->name, "a");
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    42
	if (file == NULL) {
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    43
		result = errno;
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    44
		return &result;
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    45
	}
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    46
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    47
    write_bytes = fwrite(rec->data, 1, rec->bytes, file);
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    48
	fclose(file);
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    49
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    50
    result = 0;
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    51
    return &result;
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    52
}
30d751ac6d49 Solution to first assignment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    53