source: roaraudio/roard/network.c @ 5567:6ecf012d7063

Last change on this file since 5567:6ecf012d7063 was 5567:6ecf012d7063, checked in by phi, 12 years ago

roard now tries to auto load missing protocols as plugins (Closes: #275)

File size: 5.5 KB
RevLine 
[0]1//network.c:
2
[668]3/*
[5381]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012
[668]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[668]23 *
24 */
25
[0]26#include "roard.h"
27
[1494]28#ifdef ROAR_SUPPORT_LISTEN
29
[1055]30#ifdef ROAR_BROKEN_PEERCRED
31#undef SO_PEERCRED
32#endif
33
[1484]34#ifdef ROAR_HAVE_SELECT
35#define _CAN_OPERATE
36#endif
37
[0]38int net_check_listen  (void) {
[3802]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;
[2530]43 int i;
44
45 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
[3802]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++;
[2530]50  }
51 }
52
[3802]53 if ( num == 0 )
[2530]54  return 0;
[0]55
[3802]56 rtv.sec  = 0;
57 rtv.nsec = 1000;
[0]58
[3802]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;
[2530]64   }
65  }
[0]66 }
67
[3802]68 return ret;
[0]69}
70
[3253]71int net_get_new_client (struct roard_listen * lsock) {
[5567]72 const struct roard_proto_handle * proto;
[0]73 int fh;
74 int client;
[3802]75 int socket;
[1183]76 struct roar_client * c;
[2815]77 struct roar_vio_calls    vio;
78 struct sockaddr_storage  addr;
79 socklen_t                addrlen = sizeof(addr);
[4683]80 int supported = 0;
[0]81
[3912]82 if ( roar_vio_ctl(&(lsock->sock), ROAR_VIO_CTL_GET_FH, &socket) == -1 ) {
83  // next is needed for winsock:
84  if ( roar_vio_ctl(&(lsock->sock), ROAR_VIO_CTL_GET_SELECT_FH, &socket) == -1 ) {
85   ROAR_DBG("net_get_new_client(void) = -1 // can not find any acceptable socket to accept() on");
86   return -1;
87  }
88 }
[3802]89
90 fh = accept(socket, (struct sockaddr*)&addr, &addrlen);
[0]91
92 ROAR_DBG("net_get_new_client(void): fh = %i", fh);
93
[4228]94 if ( fh == -1 )
95  return -1;
96
[3684]97#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
98 if ( lsock->proto == ROAR_PROTO_RSOUND ) {
99  client = emul_rsound_on_connect(fh, lsock);
100  switch (client) {
101   case -1: return -1; break;
102   case -2: return  0; break;
103   default: // TODO: write error handling
104     clients_get(client, &c);
105     fh = c->fh;
106    break;
107  }
108 } else {
109#endif
110
[0]111 client = clients_new();
112
[3910]113 if ( client == -1 ) {
114  ROAR_DBG("net_get_new_client(void) = -1 // can not create new client");
[3911]115  close(fh);
[3910]116  return -1;
117 }
118
[0]119 if ( clients_set_fh(client, fh) == -1 ) {
120  ROAR_ERR("net_get_new_client(void): Can not set client's fh");
121
122  clients_delete(client);
123  close(fh);
124
125  ROAR_DBG("net_get_new_client(void) = -1");
126  return -1;
127 }
[3684]128#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
129 }
130#endif
[0]131
[2820]132 if ( clients_get(client, &c) != -1 ) {
133  if ( roar_nnode_free(&(c->nnode)) == -1 )
134   return -1;
[2815]135
[2820]136  if ( roar_nnode_new_from_sockaddr(&(c->nnode), (struct sockaddr*)&addr, addrlen) == -1 )
137   return -1;
138 }
[2815]139
[3253]140 ROAR_DBG("net_get_new_client(*): proto=0x%.4x", lsock->proto);
[2530]141
[3253]142 if ( clients_set_proto(client, lsock->proto) == -1 ) {
143  ROAR_WARN("net_get_new_client(*): Setting proto(0x%.4x) of client %i failed.", lsock->proto, client);
[2529]144  return -1;
[2789]145 }
[2529]146
[3253]147 switch (lsock->proto) {
[2789]148  case ROAR_PROTO_ROARAUDIO:
149    // nothing needed to be done here
150   break;
[2545]151#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
[2791]152#ifdef ROAR_HAVE_H_ESD
[2530]153  case ROAR_PROTO_ESOUND:
[2789]154    ROAR_DBG("net_get_new_client(*): execing ESD CONNECT command");
155
156    if ( roar_vio_open_fh_socket(&vio, fh) == -1 )
[2530]157     return -1;
[2529]158
[2789]159    ROAR_DBG("net_get_new_client(*): creating VIO OK");
160
[2530]161    if ( emul_esd_exec_command(client, ESD_PROTO_CONNECT, &vio) == -1 )
162     return -1;
[2789]163
164    ROAR_DBG("net_get_new_client(*): CONNECT execed sucessfully");
[2530]165   break;
[2545]166#endif
[3359]167#endif
[3603]168#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
[3253]169  case ROAR_PROTO_SIMPLE:
[3255]170    if ( emul_simple_on_connect(client, lsock) == -1 )
171     return -1;
[3253]172   break;
[3596]173#endif
[3684]174#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
175  case ROAR_PROTO_RSOUND: // nothing to do here.
176   break;
177#endif
[3981]178#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
179  case ROAR_PROTO_RPLAY: // nothing to do here.
[3984]180
181    if ( roar_vio_open_fh_socket(&vio, fh) == -1 )
182     return -1;
183
184    if ( emul_rplay_on_status(client, NULL, &vio, NULL, 0) == -1 )
185     return -1;
[3981]186   break;
187#endif
[2789]188  default:
189    // OS independiend code to close the socket:
190    if ( roar_vio_open_fh_socket(&vio, fh) == -1 )
191     return -1;
[4683]192
[5567]193    proto = clients_get_protohandle(lsock->proto);
194    supported = 0;
195    if ( proto != NULL ) {
196     switch (proto->type) {
197      case ROARD_PROTO_TYPE_BUILDIN:
198        //this should not end up here.
199        ROAR_WARN("net_get_new_client(lsock=%p): proto(%i) marked as buildin but isn't. BAD.", lsock, lsock->proto);
[4683]200        supported = 0;
[5567]201       break;
202      case ROARD_PROTO_TYPE_ROARDPROTO:
203        supported = 1;
204        if ( proto->impl.roardproto.new_client != NULL ) {
205         if ( proto->impl.roardproto.new_client(client, &vio, lsock) == -1 ) {
206          supported = 0;
207         }
208        }
209       break;
[4683]210     }
211    }
212
213    if ( !supported ) {
214     clients_delete(client);
215     //roar_vio_close(&vio);
216     return -1;
217    }
[2789]218   break;
[2530]219 }
[2529]220
[0]221// close(fh);
222
223 return 0;
224}
225
[1494]226#endif
227
[0]228//ll
Note: See TracBrowser for help on using the repository browser.