source: roaraudio/roard/network.c @ 3255:970b81dc7c18

Last change on this file since 3255:970b81dc7c18 was 3255:970b81dc7c18, checked in by phi, 14 years ago

got PA simple protocol basicly to work

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[i].socket != -1 ) {
49   if ( g_listen[i].socket > max_fh )
50    max_fh = g_listen[i].socket;
51
52   FD_SET(g_listen[i].socket, &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[i].socket != -1 ) {
66    if ( FD_ISSET(g_listen[i].socket, &sl) ) {
67     if ( net_get_new_client(&(g_listen[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 (struct roard_listen * lsock) {
82 int fh;
83 int client;
84 struct roar_client * c;
85#ifdef SO_PEERCRED
86 struct ucred cred;
87 socklen_t cred_len = sizeof(cred);
88#endif
89 struct roar_vio_calls    vio;
90 struct sockaddr_storage  addr;
91 socklen_t                addrlen = sizeof(addr);
92
93 fh = accept(lsock->socket, (struct sockaddr*)&addr, &addrlen);
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 if ( clients_get(client, &c) != -1 ) {
110#ifdef SO_PEERCRED
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#elif defined(ROAR_HAVE_GETPEEREID)
121  if (getpeereid(fh, &(c->uid), &(c->gid)) == -1) {
122   ROAR_DBG("req_on_identify(): Can't get creds via getpeereid(): %s", strerror(errno));
123  }
124#endif
125
126  if ( roar_nnode_free(&(c->nnode)) == -1 )
127   return -1;
128
129  if ( roar_nnode_new_from_sockaddr(&(c->nnode), (struct sockaddr*)&addr, addrlen) == -1 )
130   return -1;
131 }
132
133 ROAR_DBG("net_get_new_client(*): proto=0x%.4x", lsock->proto);
134
135 if ( clients_set_proto(client, lsock->proto) == -1 ) {
136  ROAR_WARN("net_get_new_client(*): Setting proto(0x%.4x) of client %i failed.", lsock->proto, client);
137  return -1;
138 }
139
140 switch (lsock->proto) {
141  case ROAR_PROTO_ROARAUDIO:
142    // nothing needed to be done here
143   break;
144#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
145#ifdef ROAR_HAVE_H_ESD
146  case ROAR_PROTO_ESOUND:
147    ROAR_DBG("net_get_new_client(*): execing ESD CONNECT command");
148
149    if ( roar_vio_open_fh_socket(&vio, fh) == -1 )
150     return -1;
151
152    ROAR_DBG("net_get_new_client(*): creating VIO OK");
153
154    if ( emul_esd_exec_command(client, ESD_PROTO_CONNECT, &vio) == -1 )
155     return -1;
156
157    ROAR_DBG("net_get_new_client(*): CONNECT execed sucessfully");
158   break;
159#endif
160  case ROAR_PROTO_SIMPLE:
161    if ( emul_simple_on_connect(client, lsock) == -1 )
162     return -1;
163   break;
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.