alsa.audio: limit the supported frequencies to common set
[AROS.git] / workbench / network / stacks / AROSTCP / netlib / serveraccept.c
blobdc2829d9ae0323d7758c85272610951a64fb2084
1 /* $Id$
3 * serveraccept - accept a server connection on named port
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
7 * All rights reserved.
8 * Copyright © 2005 Pavel Fedin
9 */
11 /****** net.lib/serveraccept ***********************************************
13 NAME
14 serveraccept - Accept a server connection on named port
16 SYNOPSIS
17 socket = serveraccept(name, peer);
19 long serveraccept(char *, struct sockaddr_in *);
21 DESCRIPTION
22 The serveraccept() library call binds a socket to the named Internet
23 TCP port. Then it listens the socket and accepts the connection to
24 the port. The peer's socket address is returned in sockaddr pointed
25 by sockaddr argument.
27 The port name is resolved by getservbyname() call. A numeric value
28 for port name is also accepted.
30 This module is meant for daemon developing.
32 INPUTS
33 name - port name or numeric string.
34 peer - pointer to struct sockaddr_in
36 RESULT
37 socket - positive socket id for success or -1 for failure.
39 peer - sockaddr_in structure containing peer's internet address.
40 Note that on error, the structure containing peer address
41 is not necessarily updated.
43 SEE ALSO
44 bsdsocket/accept, bsdsocket/getservbyname
46 *****************************************************************************
50 #ifdef AMIGA
51 #include <clib/netlib_protos.h>
52 #include <proto/socket.h>
53 #include <proto/dos.h>
54 #endif /* AMIGA */
56 #include <errno.h>
57 #include <netdb.h>
59 #include <sys/param.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
63 #include <signal.h>
65 #include <dos/dos.h>
66 #include <dos/var.h>
68 #include <stdlib.h>
69 #include <string.h>
72 * serveraccept:
73 * Accept a server socket from the named port
75 long
76 serveraccept(char *pname, struct sockaddr_in *ha)
78 struct sockaddr_in sin;
79 socklen_t ha_len = sizeof(*ha);
80 int s, sa;
81 LONG port;
82 struct servent *sp;
83 long on = 1;
85 /* Create address corresponding our service */
86 bzero((caddr_t)&sin, sizeof(sin));
87 sin.sin_len = sizeof(struct sockaddr_in);
88 sin.sin_family = AF_INET;
90 /* A port must be in the range 1 - 65535 */
91 if (StrToLong(pname, &port) > 0 && port < 65536 )
92 sin.sin_port = port;
93 else if (sp = getservbyname(pname, "tcp"))
94 sin.sin_port = sp->s_port;
95 else {
96 return -1;
99 sin.sin_addr.s_addr = INADDR_ANY;
101 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
102 PrintNetFault(Errno(), "socket");
103 return -1;
106 /* Reuse this port */
107 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) {
108 PrintNetFault(Errno(), "setsockopt");
109 sa = -1; goto Return;
112 /* Bind it to socket */
113 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0 ) {
114 PrintNetFault(Errno(), "bind");
115 sa = -1; goto Return;
118 if (listen(s, 1) < 0) {
119 PrintNetFault(Errno(), "listen");
120 sa = -1; goto Return;
123 if ((sa = accept(s, (struct sockaddr *)ha, &ha_len)) < 0){
124 PrintNetFault(Errno(), "accept");
127 Return:
128 CloseSocket(s);
129 return sa;