-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftps.c
136 lines (118 loc) · 2.96 KB
/
ftps.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
//_______________________
//| Andrew Lee |
//| CSE5462 Lab 02 |
//| 09-05-16 ftps.c |
//|_____________________|
// NOTE: Large portion of code copied from Project 1
// TCP server.c example.
// Required Libraries
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#define BUFSIZE 1000
void main (int argc, char *argv[]) {
// Variables
int port;
int sock;
int msgsock;
int rval = 0;
int filesize = 0;
char filename[20] = "";
struct sockaddr_in sin_addr;
char databufin[BUFSIZE];
char databufout[BUFSIZE] = "Server Response: hi";
// Ensure Proper Argument Usage
if (argc != 2) {
printf("ERROR: Incorrect number of arguements.");
printf(" Please run using the following format...\n");
printf("ftps <local-port>\n\n");
exit(1);
}
port = atoi(argv[1]);
// Wait for client connection
printf("TCP Server Initialized. Awaiting Clients...\n");
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Error: Unable to Open Datagram Socket.");
exit(1);
}
// Construct sent socket
sin_addr.sin_family = AF_INET;
sin_addr.sin_addr.s_addr = INADDR_ANY;
sin_addr.sin_port = htons(port);
// Bind Socket
if (bind(sock, (struct sockaddr *)&sin_addr, sizeof(struct sockaddr_in)) < 0) {
perror("Error: Unable to Bind Stream Socket");
exit(1);
}
// Listen for socket connections (Max open connections is 5)
listen(sock, 5);
// Accept 1 connection: msgsocket
if ((msgsock = accept(sock, (struct sockaddr *)NULL, (int *)NULL)) == -1) {
perror("Error: Unable to connect Stream Socket.");
exit(1);
}
// Clear Buffer (Set it to 0)
bzero(databufin, sizeof(databufin));
// Read Header
rval = 24;
while (rval > 0) {
rval -= recv(msgsock, databufin, rval, 0);
}
// Determine Size of file from header
char temp[20];
int i = 0;
for (i = 0; i < 4; i++) {
temp[i] = databufin[i];
}
filesize = ntohl(*((uint32_t *)(temp)));
printf("Filesize: %d\n", filesize);
// Determine Filename
for (i = 0; i < 20; i++) {
temp[i] = databufin[i+4];
}
printf("Filename: %s\n", temp);
filename[0] = 'r';
filename[1] = 'e';
filename[2] = 'c';
filename[3] = 'v';
filename[4] = 'd';
filename[5] = '/';
strcat(filename,temp);
printf("Filename: %s\n", filename);
struct stat st;
if(stat("recvd",&st) != 0)
{
mkdir("recvd",0700);
}
// Output File
FILE *oufp;
oufp = fopen(filename, "w+");
if (oufp == NULL) {
perror("Error: Unable to create output file.");
}
// Recieve and Write Data
rval = 0;
int tempval = 0;
while (rval < filesize) {
tempval = recv(msgsock, databufin, BUFSIZE, 0);
rval += tempval;
if (tempval < 0) {
perror("Error: Unable to read Stream Socket.");
exit(1);
}
// Write to Output File
fwrite(databufin, 1, tempval, oufp);
//fprintf(oufp, "%s", databufin); //Troubleshooting
}
// Close File/Connections
fclose(oufp);
close(msgsock);
close(sock);
}