Repository navigation
Expand file tree
/
Copy pathserver_ftp_functions.c
More file actions
144 lines (123 loc) · 4.13 KB
/
Copy pathserver_ftp_functions.c
File metadata and controls
144 lines (123 loc) · 4.13 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
/*
* Function: some functions of server_ftp
*
* Author : Daniel Lai
* Created On: 2014-02-21
*/
#include <server_ftp.h>
static size_t size_packet = sizeof(struct packet);
struct client_info* client_info_alloc(int s, int c)
{
struct client_info* ci = (struct client_info*) malloc(sizeof(struct client_info));
ci->sfd = s;
ci->cid = c;
return ci;
}
// the operation of GET file command
void command_get(struct packet* shp, struct packet* data, int sfd_client, char* lpwd)
{
int x;
FILE* f = fopen(shp->buffer, "rb");
shp->type = INFO;
shp->comid = GET;
strcpy(shp->buffer, f ? "OK: File found; processing..." : "ER: Error opening file.");
printpacket(shp, HP);
data = htonp(shp);
if(( x = send(sfd_client, data, size_packet, 0)) != size_packet)
er("send()", x);
if(f)
{
shp->type = DATA;
//send_file(shp, data, sfd_client, f);
send_file(shp, data, sfd_server_data, f);
fclose(f);
}
send_EOT(shp, data, sfd_server_data);
//send_EOT(shp, data, sfd_client);
}
// the opration of DIR command
void command_dir(struct packet* shp, struct packet* data, int sfd_client, char* lpwd)
{
int x;
shp->type = DATA;
DIR* d = opendir(lpwd);
if(!d)
{
er("opendir()", (int) d);
sprintf(shp->buffer, "ER: could not get the dir info!\n");
data = htonp(shp);
if( (x = send(sfd_server_data, data, size_packet,0)) != size_packet)
er("send()",x);
}
else
{
sprintf(shp->buffer, "OK: get the dir info.\n");
data = htonp(shp);
if(( x = send(sfd_server_data, data, size_packet, 0)) != size_packet)
er("send()",x);
}
struct dirent* e;
while(e = readdir(d))
{
sprintf(shp->buffer, "%s\t%s", e->d_type == 4 ? "DIR:" : e->d_type == 8 ? "FILE:" : "UNDEF:" , e->d_name);
data = htonp(shp);
//if(( x = send(sfd_client, data, size_packet, 0)) != size_packet)
if(( x = send(sfd_server_data, data, size_packet, 0)) != size_packet)
er("send()", x);
}
send_EOT(shp, data, sfd_server_data);
//send_EOT(shp, data, sfd_client);
}
// the operation of PRT command
void command_prt(struct packet* shp, struct packet* data, int sfd_client, char* lpwd)
{
if(DEBUG)
printf("Starting prt...\n");
int x;
int dataportnum = atoi(shp->buffer);
shp->type = INFO;
shp->comid = PRT;
strcpy(shp->buffer, dataportnum ? "OK: Port Gotten; processing..." : "ER: Error getting port num.");
printpacket(shp, HP);
data = htonp(shp);
if(( x = send(sfd_client, data, size_packet, 0)) != size_packet)
er("send()", x);
if(DEBUG)
printf("OK, waiting for connection\n");
if(dataportnum)
{
struct sockaddr_in skin_server_data;
int x;
size_t size_sockaddr = sizeof(struct sockaddr), size_packet = sizeof(struct packet);
if(( x = sfd_server_data = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
er("socket()", x);
memset((char*) & skin_server_data, 0, sizeof(struct sockaddr_in));
skin_server_data.sin_family = AF_INET;
skin_server_data.sin_addr.s_addr = inet_addr(IPCLIENT);
skin_server_data.sin_port = htons(dataportnum);
if(( x = connect(sfd_server_data, (struct sockaddr*) &skin_server_data, size_sockaddr)) < 0)
er("connect()", x);
printf(ID "Data channel build. Communicating with client @ %s:%d...\n\n", IPCLIENT, dataportnum);
}
send_EOT(shp, data, sfd_client);
}
// the operation of BYE command
void command_bye(struct packet* shp, struct packet* data, int sfd_client, char* lpwd)
{
if(DEBUG)
printf("Connection closing...\n");
/*
int x;
shp->type = INFO;
shp->comid = BYE;
strcpy(shp->buffer, "Connection closing...\n");
data = htonp(shp);
if(( x = send(sfd_client, data, size_packet, 0)) != size_packet)
er("send()", x);
send_EOT(shp, data, sfd_client);
*/
printf(ID "Data channel with client closed!\n");
close(sfd_server_data);
printf(ID "Command channel with client closed!\n");
close(sfd_client);
}