source: roaraudio/roard/network.c @ 2530:921f7ac0be31

Last change on this file since 2530:921f7ac0be31 was 2530:921f7ac0be31, checked in by phi, 15 years ago

added support for multi homed server

File size: 3.3 KB
Line 
1//network.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include "roard.h"
26
27#ifdef ROAR_SUPPORT_LISTEN
28
29#ifdef ROAR_BROKEN_PEERCRED
30#undef SO_PEERCRED
31#endif
32
33#ifdef ROAR_HAVE_SELECT
34#define _CAN_OPERATE
35#endif
36
37int net_check_listen  (void) {
38#ifdef _CAN_OPERATE
39 int r;
40 fd_set sl;
41 struct timeval tv;
42 int i;
43 int max_fh = -1;
44
45 FD_ZERO(&sl);
46
47 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
48  if ( g_listen_socket[i] != -1 ) {
49   if ( g_listen_socket[i] > max_fh )
50    max_fh = g_listen_socket[i];
51
52   FD_SET(g_listen_socket[i], &sl);
53  }
54 }
55
56 if ( max_fh == -1 )
57  return 0;
58
59 tv.tv_sec  = 0;
60 tv.tv_usec = 1;
61
62 if ((r = select(max_fh + 1, &sl, NULL, NULL, &tv)) > 0) {
63  ROAR_DBG("net_check_listen(void): We have a connection!");
64  for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
65   if ( g_listen_socket[i] != -1 ) {
66    if ( FD_ISSET(g_listen_socket[i], &sl) ) {
67     if ( net_get_new_client(g_listen_socket[i], g_listen_proto[i]) == -1 )
68      return -1;
69    }
70   }
71  }
72 }
73
74 return r;
75#else
76 return -1;
77#endif
78}
79
80#ifdef _CAN_OPERATE
81int net_get_new_client (int sock, int proto) {
82 int fh;
83 int client;
84#if defined(SO_PEERCRED) || defined(ROAR_HAVE_GETPEEREID)
85 struct roar_client * c;
86#endif
87#ifdef SO_PEERCRED
88 struct ucred cred;
89 socklen_t cred_len = sizeof(cred);
90#endif
91 struct roar_vio_calls vio;
92
93 fh = accept(sock, NULL, NULL);
94
95 ROAR_DBG("net_get_new_client(void): fh = %i", fh);
96
97 client = clients_new();
98
99 if ( clients_set_fh(client, fh) == -1 ) {
100  ROAR_ERR("net_get_new_client(void): Can not set client's fh");
101
102  clients_delete(client);
103  close(fh);
104
105  ROAR_DBG("net_get_new_client(void) = -1");
106  return -1;
107 }
108
109#ifdef SO_PEERCRED
110 if ( clients_get(client, &c) != -1 ) {
111  if (getsockopt(fh, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != -1) {
112   if ( cred.pid != 0 ) {
113    c->pid = cred.pid;
114    c->uid = cred.uid;
115    c->gid = cred.gid;
116   }
117  } else {
118   ROAR_DBG("req_on_identify(): Can't get creds via SO_PEERCRED: %s", strerror(errno));
119  }
120 }
121#elif defined(ROAR_HAVE_GETPEEREID)
122 if ( clients_get(client, &c) != -1 ) {
123  if (getpeereid(fh, &(c->uid), &(c->gid)) == -1) {
124   ROAR_DBG("req_on_identify(): Can't get creds via getpeereid(): %s", strerror(errno));
125  }
126 }
127#endif
128
129 ROAR_DBG("net_get_new_client(*): proto=0x%.4x", proto);
130
131 if ( clients_set_proto(client, proto) == -1 )
132  return -1;
133
134 switch (proto) {
135  case ROAR_PROTO_ESOUND:
136    if ( roar_vio_open_fh(&vio, fh) == -1 )
137     return -1;
138
139    if ( emul_esd_exec_command(client, ESD_PROTO_CONNECT, &vio) == -1 )
140     return -1;
141   break;
142 }
143
144// close(fh);
145
146 return 0;
147}
148#endif
149
150#endif
151
152//ll
Note: See TracBrowser for help on using the repository browser.