-
Notifications
You must be signed in to change notification settings - Fork 0
/
passivesock.c
104 lines (88 loc) · 2.55 KB
/
passivesock.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
/* passivesock.c - passivesock */
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <netdb.h>
static u_short portbase = 37000; /* port base, for non-root servers */
/*------------------------------------------------------------------------
* passivesock - allocate & bind a server socket using TCP or UDP
*------------------------------------------------------------------------
*/
int
passivesock(
char *service, /* service associated with the desired port */
char *protocol, /* name of protocol to use ("tcp" or "udp") */
int qlen, /* max length of the server request queue */
int *rport )
{
struct servent *pse; /* pointer to service information entry */
struct protoent *ppe; /* pointer to protocol information entry*/
struct sockaddr_in sin; /* an Internet endpoint address */
int s, type; /* socket descriptor and socket type */
memset((char *)&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
/* Map service name to port number */
if ( *rport )
{
/* If a 1 is in this field, choose a free port */
sin.sin_port = htons((u_short) 0);
}
else
{
if ( pse = getservbyname(service, protocol) )
sin.sin_port = htons(ntohs((u_short)pse->s_port)
+ portbase);
else if ( (sin.sin_port = htons((u_short)atoi(service))) == 0 )
{
fprintf( stderr, "can't get \"%s\" service entry\n", service);
exit(-1);
}
}
/* Map protocol name to protocol number */
if ( (ppe = getprotobyname(protocol)) == 0)
{
fprintf( stderr, "can't get \"%s\" protocol entry\n", protocol);
exit(-1);
}
/* Use protocol to choose a socket type */
if (strcmp(protocol, "udp") == 0)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
/* Allocate a socket */
s = socket(PF_INET, type, ppe->p_proto);
if (s < 0)
{
fprintf( stderr, "can't create socket: %s\n", strerror(errno));
exit(-1);
}
/* Bind the socket */
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
fprintf( stderr, "can't bind to %s port: %s\n", service, strerror(errno));
exit(-1);
}
if (type == SOCK_STREAM && listen(s, qlen) < 0)
{
fprintf( stderr, "can't listen on %s port: %s\n", service, strerror(errno));
exit(-1);
}
if ( *rport )
{
int len;
/* return the selected port in rport */
len = sizeof(sin);
if ( getsockname( s, (struct sockaddr *)&sin, &len ) )
{
fprintf( stderr, "chatd: cannot getsockname: %s\n", strerror(errno) );
exit(-1);
}
*rport = ntohs(sin.sin_port);
}
return s;
}