-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverFTP.c
177 lines (141 loc) · 5.17 KB
/
serverFTP.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
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
/*
* echoserveri.c - An iterative echo server
*/
#include "csapp.h"
// Hown much slaves shall we create
#define NPROC 5
#define MAX_NAME_LEN 256
void ftpHandler(int connfd);
/*
* Note that this code only works with IPv4 addresses
* (IPv6 is not supported)
*/
pid_t myProcess[NPROC];
int myfd[NPROC][2];
int currP = 0;
void crush(int sig){
int status;
pid_t pid;
int i;
for (i = 0; i < NPROC; i++) {
kill(myProcess[i], SIGINT);
}
while ((pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0){
if (status) {
printf("An error occured while closing process %d\n", pid);
} else {
printf("Process %d safely closed.\n", pid);
}
}
exit(0);
}
int main(int argc, char **argv)
{
int listenfd, connfd;
pid_t handler;
socklen_t clientlen;
struct sockaddr_in clientaddr;
char client_ip_string[INET_ADDRSTRLEN];
char client_hostname[MAX_NAME_LEN];
int position;
signal(SIGINT, crush);
if (argc != 1) {
fprintf(stderr, "usage: %s\n", argv[0]);
exit(0);
}
//port = atoi(argv[1]);
clientlen = (socklen_t)sizeof(clientaddr);
listenfd = Open_listenfd(2121);
int i;
for (i = 0; i < NPROC; i++) {
socketpair(PF_LOCAL, SCM_RIGHTS, 0, myfd[i]);
if ((handler = Fork()) == 0){
close(myfd[i][1]);
// Create the handler
printf("Started process %d\n", i);
struct msghdr m;
struct cmsghdr *cm;
struct iovec iov;
char dummy[100];
char buf[CMSG_SPACE(sizeof(int))];
//ssize_t readlen;
int *fdlist;
do {
// Code indigeste de réception de socket
// Librement inspiré et adapté du thread suivant :
// https://stackoverflow.com/questions/14427898/how-can-i-pass-a-socket-from-parent-to-child-processes
iov.iov_base = dummy;
iov.iov_len = sizeof(dummy);
memset(&m, 0, sizeof(m));
m.msg_iov = &iov;
m.msg_iovlen = 1;
m.msg_controllen = CMSG_SPACE(sizeof(int));
m.msg_control = buf;
recvmsg(myfd[i][0], &m, 0);
printf("Awaiting connection...\n");
/* Do your error handling here in case recvmsg fails */
int received_file_descriptor = -1; /* Default: none was received */
for (cm = CMSG_FIRSTHDR(&m); cm; cm = CMSG_NXTHDR(&m, cm)) {
if (cm->cmsg_level == SOL_SOCKET && cm->cmsg_type == SCM_RIGHTS) {
//int nfds = (cm->cmsg_len - CMSG_LEN(0)) / sizeof(int);
fdlist = (int *)CMSG_DATA(cm);
received_file_descriptor = *fdlist;
break;
}
}
printf("%d got file descriptor %d\n", i, received_file_descriptor);
ftpHandler(received_file_descriptor);
Close(received_file_descriptor);
} while(1);
} else {
// Register it's id on a table
close(myfd[i][0]);
myProcess[i] = handler;
}
}
// Start on first process
position = 0;
// Wait for a connection
while(1) {
printf("The next handler will be %d\n", position);
connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
/* determine the name of the client */
Getnameinfo((SA *) &clientaddr, clientlen,
client_hostname, MAX_NAME_LEN, 0, 0, 0);
/* determine the textual representation of the client's IP address */
Inet_ntop(AF_INET, &clientaddr.sin_addr, client_ip_string,
INET_ADDRSTRLEN);
printf("server connected to %s (%s) - looking for handler available\n", client_hostname,
client_ip_string);
// Code indigeste de transmission de socket
// Librement inspiré et adapté du thread suivant :
// https://stackoverflow.com/questions/14427898/how-can-i-pass-a-socket-from-parent-to-child-processes
struct msghdr m;
struct cmsghdr *cm;
struct iovec iov;
char buf[CMSG_SPACE(sizeof(int))];
char dummy[2];
memset(&m, 0, sizeof(m));
m.msg_controllen = CMSG_SPACE(sizeof(int));
m.msg_control = &buf;
memset(m.msg_control, 0, m.msg_controllen);
cm = CMSG_FIRSTHDR(&m);
cm->cmsg_level = SOL_SOCKET;
cm->cmsg_type = SCM_RIGHTS;
cm->cmsg_len = CMSG_LEN(sizeof(int));
*((int *)CMSG_DATA(cm)) = connfd;
m.msg_iov = &iov;
m.msg_iovlen = 1;
iov.iov_base = dummy;
iov.iov_len = 1;
dummy[0] = 0; /* doesn't matter what data we send */
sendmsg(myfd[position][1], &m, 0);
Close(connfd);
if (position >= NPROC-1) {
position = 0;
} else {
position++;
}
}
exit(0);
}