/
ls-server.c
111 lines (95 loc) · 3.05 KB
/
ls-server.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
/*****************************************************************************/
/*** ls-server.c ***/
/*** ***/
/*** Demonstrate calling an external (/bin/ls) program through a connection***/
/*****************************************************************************/
/**************************************************************************
* This is an example of a server that links clients to external programs
* with the exec() call. Of course, the external program does not know
* about the socket, so the all I/O has to be redirected to stdin/stdout.
* The redirection requires the program to fork a new process, so that the
* server can still talk to its own stdin/stdout.
**************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <sys/socket.h>
#include <resolv.h>
#include <arpa/inet.h>
#include <errno.h>
#define MY_PORT 9999
void sig_handler(int sig)
{
if ( sig == SIGCHLD )
{ int retval;
wait(&retval);
}
}
int main()
{ int sd;
struct sigaction act;
struct sockaddr_in self;
bzero(&act, sizeof(act));
act.sa_handler = sig_handler;
act.sa_flags = SA_NOCLDSTOP;
sigaction(SIGCHLD, &act, 0);
/*---Create new socket---*/
if ( (sd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
{
perror("Socket");
exit(errno);
}
/* if ( setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, 0, 0) < 0 ) perror("SocketOpt");*/
/*---Initialize own server address---*/
bzero(&self, sizeof(self));
self.sin_family = AF_INET;
self.sin_addr.s_addr = INADDR_ANY;
self.sin_port = htons(MY_PORT);
/*---Give the socket a "name" (set the IP and Port addresses)---*/
if ( bind(sd, (struct sockaddr*)&self, sizeof(self)) != 0 )
{
perror("Bind");
abort();
}
/*---Make the socket a "listening socket"---*/
if ( listen(sd, 20) != 0 )
{
perror("Listen");
abort();
}
/*---Forever...---*/
while (1)
{ static int client;
int sd_copy, size;
struct sockaddr_in client_info;
/*---Accept client connection (creates a new communication port)---*/
size = sizeof(client_info);
client = accept(sd, (struct sockaddr*)&client_info, &size);
if ( client < 0 )
perror("Accept");
else
{
printf("Connected: %s:%d\n", inet_ntoa(client_info.sin_addr), ntohs(client_info.sin_port));
/*---Create child task to handle service---*/
if ( fork() != 0 )
{
/*Parent*/ /*---The parent doesn't need the child's data connection---*/
close(client);
}
else
{
/*Child*/ /*---Child doesn't need access to request connection---*/
close(sd);
/*---Map stdin, stdout and stderr to the data connection---*/
dup2(client, 0);
dup2(client, 1);
dup2(client, 2);
/*---Call external program---*/
execl("/bin/ls", "/bin/ls", "-al", "/sbin", 0);
}
}
}
/*---Clean up (should never get here)---*/
close(sd);
return 0;
}