source: roaraudio/roard/network.c @ 4228:111d72854d33

Last change on this file since 4228:111d72854d33 was 4228:111d72854d33, checked in by phi, 14 years ago

do not create new clients if accept retruned -1

File size: 4.8 KB
Line 
1//network.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "roard.h"
27
28#ifdef ROAR_SUPPORT_LISTEN
29
30#ifdef ROAR_BROKEN_PEERCRED
31#undef SO_PEERCRED
32#endif
33
34#ifdef ROAR_HAVE_SELECT
35#define _CAN_OPERATE
36#endif
37
38int net_check_listen  (void) {
39 struct roar_vio_selecttv rtv;
40 struct roar_vio_select   sv[ROAR_MAX_LISTEN_SOCKETS];
41 size_t num = 0;
42 ssize_t ret;
43 int i;
44
45 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
46  if ( g_listen[i].used ) {
47   ROAR_VIO_SELECT_SETVIO(&(sv[num]), &(g_listen[i].sock), ROAR_VIO_SELECT_READ);
48   sv[num].ud.si = i;
49   num++;
50  }
51 }
52
53 if ( num == 0 )
54  return 0;
55
56 rtv.sec  = 0;
57 rtv.nsec = 1000;
58
59 if ( (ret = roar_vio_select(sv, num, &rtv, NULL)) > 0 ) {
60  for (i = 0; i < num; i++) {
61   if ( sv[i].eventsa & ROAR_VIO_SELECT_READ ) {
62    if ( net_get_new_client(&(g_listen[sv[i].ud.si])) == -1 )
63     return -1;
64   }
65  }
66 }
67
68 return ret;
69}
70
71int net_get_new_client (struct roard_listen * lsock) {
72 int fh;
73 int client;
74 int socket;
75 struct roar_client * c;
76 struct roar_vio_calls    vio;
77 struct sockaddr_storage  addr;
78 socklen_t                addrlen = sizeof(addr);
79
80 if ( roar_vio_ctl(&(lsock->sock), ROAR_VIO_CTL_GET_FH, &socket) == -1 ) {
81  // next is needed for winsock:
82  if ( roar_vio_ctl(&(lsock->sock), ROAR_VIO_CTL_GET_SELECT_FH, &socket) == -1 ) {
83   ROAR_DBG("net_get_new_client(void) = -1 // can not find any acceptable socket to accept() on");
84   return -1;
85  }
86 }
87
88 fh = accept(socket, (struct sockaddr*)&addr, &addrlen);
89
90 ROAR_DBG("net_get_new_client(void): fh = %i", fh);
91
92 if ( fh == -1 )
93  return -1;
94
95#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
96 if ( lsock->proto == ROAR_PROTO_RSOUND ) {
97  client = emul_rsound_on_connect(fh, lsock);
98  switch (client) {
99   case -1: return -1; break;
100   case -2: return  0; break;
101   default: // TODO: write error handling
102     clients_get(client, &c);
103     fh = c->fh;
104    break;
105  }
106 } else {
107#endif
108
109 client = clients_new();
110
111 if ( client == -1 ) {
112  ROAR_DBG("net_get_new_client(void) = -1 // can not create new client");
113  close(fh);
114  return -1;
115 }
116
117 if ( clients_set_fh(client, fh) == -1 ) {
118  ROAR_ERR("net_get_new_client(void): Can not set client's fh");
119
120  clients_delete(client);
121  close(fh);
122
123  ROAR_DBG("net_get_new_client(void) = -1");
124  return -1;
125 }
126#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
127 }
128#endif
129
130 if ( clients_get(client, &c) != -1 ) {
131  if ( roar_nnode_free(&(c->nnode)) == -1 )
132   return -1;
133
134  if ( roar_nnode_new_from_sockaddr(&(c->nnode), (struct sockaddr*)&addr, addrlen) == -1 )
135   return -1;
136 }
137
138 ROAR_DBG("net_get_new_client(*): proto=0x%.4x", lsock->proto);
139
140 if ( clients_set_proto(client, lsock->proto) == -1 ) {
141  ROAR_WARN("net_get_new_client(*): Setting proto(0x%.4x) of client %i failed.", lsock->proto, client);
142  return -1;
143 }
144
145 switch (lsock->proto) {
146  case ROAR_PROTO_ROARAUDIO:
147    // nothing needed to be done here
148   break;
149#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
150#ifdef ROAR_HAVE_H_ESD
151  case ROAR_PROTO_ESOUND:
152    ROAR_DBG("net_get_new_client(*): execing ESD CONNECT command");
153
154    if ( roar_vio_open_fh_socket(&vio, fh) == -1 )
155     return -1;
156
157    ROAR_DBG("net_get_new_client(*): creating VIO OK");
158
159    if ( emul_esd_exec_command(client, ESD_PROTO_CONNECT, &vio) == -1 )
160     return -1;
161
162    ROAR_DBG("net_get_new_client(*): CONNECT execed sucessfully");
163   break;
164#endif
165#endif
166#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
167  case ROAR_PROTO_SIMPLE:
168    if ( emul_simple_on_connect(client, lsock) == -1 )
169     return -1;
170   break;
171#endif
172#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
173  case ROAR_PROTO_RSOUND: // nothing to do here.
174   break;
175#endif
176#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
177  case ROAR_PROTO_RPLAY: // nothing to do here.
178
179    if ( roar_vio_open_fh_socket(&vio, fh) == -1 )
180     return -1;
181
182    if ( emul_rplay_on_status(client, NULL, &vio, NULL, 0) == -1 )
183     return -1;
184   break;
185#endif
186  default:
187    // OS independiend code to close the socket:
188    if ( roar_vio_open_fh_socket(&vio, fh) == -1 )
189     return -1;
190    roar_vio_close(&vio);
191    return -1;
192   break;
193 }
194
195// close(fh);
196
197 return 0;
198}
199
200#endif
201
202//ll
Note: See TracBrowser for help on using the repository browser.