source: roaraudio/roard/network.c @ 2815:8cf175ac7bf9

Last change on this file since 2815:8cf175ac7bf9 was 2815:8cf175ac7bf9, checked in by phi, 15 years ago

added nnode support

File size: 4.2 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 struct sockaddr_storage  addr;
93 socklen_t                addrlen = sizeof(addr);
94
95 fh = accept(sock, (struct sockaddr*)&addr, &addrlen);
96
97 ROAR_DBG("net_get_new_client(void): fh = %i", fh);
98
99 client = clients_new();
100
101 if ( clients_set_fh(client, fh) == -1 ) {
102  ROAR_ERR("net_get_new_client(void): Can not set client's fh");
103
104  clients_delete(client);
105  close(fh);
106
107  ROAR_DBG("net_get_new_client(void) = -1");
108  return -1;
109 }
110
111#ifdef SO_PEERCRED
112 if ( clients_get(client, &c) != -1 ) {
113  if (getsockopt(fh, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != -1) {
114   if ( cred.pid != 0 ) {
115    c->pid = cred.pid;
116    c->uid = cred.uid;
117    c->gid = cred.gid;
118   }
119  } else {
120   ROAR_DBG("req_on_identify(): Can't get creds via SO_PEERCRED: %s", strerror(errno));
121  }
122 }
123#elif defined(ROAR_HAVE_GETPEEREID)
124 if ( clients_get(client, &c) != -1 ) {
125  if (getpeereid(fh, &(c->uid), &(c->gid)) == -1) {
126   ROAR_DBG("req_on_identify(): Can't get creds via getpeereid(): %s", strerror(errno));
127  }
128 }
129#endif
130
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 ROAR_DBG("net_get_new_client(*): proto=0x%.4x", proto);
138
139 if ( clients_set_proto(client, proto) == -1 ) {
140  ROAR_WARN("net_get_new_client(*): Setting proto(0x%.4x) of client %i failed.", proto, client);
141  return -1;
142 }
143
144 switch (proto) {
145  case ROAR_PROTO_ROARAUDIO:
146    // nothing needed to be done here
147   break;
148#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
149#ifdef ROAR_HAVE_H_ESD
150  case ROAR_PROTO_ESOUND:
151    ROAR_DBG("net_get_new_client(*): execing ESD CONNECT command");
152
153    if ( roar_vio_open_fh_socket(&vio, fh) == -1 )
154     return -1;
155
156    ROAR_DBG("net_get_new_client(*): creating VIO OK");
157
158    if ( emul_esd_exec_command(client, ESD_PROTO_CONNECT, &vio) == -1 )
159     return -1;
160
161    ROAR_DBG("net_get_new_client(*): CONNECT execed sucessfully");
162   break;
163#endif
164#endif
165  default:
166    // OS independiend code to close the socket:
167    if ( roar_vio_open_fh_socket(&vio, fh) == -1 )
168     return -1;
169    roar_vio_close(&vio);
170    return -1;
171   break;
172 }
173
174// close(fh);
175
176 return 0;
177}
178#endif
179
180#endif
181
182//ll
Note: See TracBrowser for help on using the repository browser.