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
RevLine 
[0]1//network.c:
2
[668]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
[0]25#include "roard.h"
26
[1494]27#ifdef ROAR_SUPPORT_LISTEN
28
[1055]29#ifdef ROAR_BROKEN_PEERCRED
30#undef SO_PEERCRED
31#endif
32
[1484]33#ifdef ROAR_HAVE_SELECT
34#define _CAN_OPERATE
35#endif
36
[0]37int net_check_listen  (void) {
[1484]38#ifdef _CAN_OPERATE
[0]39 int r;
40 fd_set sl;
41 struct timeval tv;
[2530]42 int i;
43 int max_fh = -1;
[0]44
45 FD_ZERO(&sl);
[2530]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;
[0]58
59 tv.tv_sec  = 0;
60 tv.tv_usec = 1;
61
[2530]62 if ((r = select(max_fh + 1, &sl, NULL, NULL, &tv)) > 0) {
[0]63  ROAR_DBG("net_check_listen(void): We have a connection!");
[2530]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  }
[0]72 }
73
74 return r;
[1484]75#else
76 return -1;
77#endif
[0]78}
79
[1484]80#ifdef _CAN_OPERATE
[2530]81int net_get_new_client (int sock, int proto) {
[0]82 int fh;
83 int client;
[1183]84#if defined(SO_PEERCRED) || defined(ROAR_HAVE_GETPEEREID)
85 struct roar_client * c;
86#endif
[490]87#ifdef SO_PEERCRED
88 struct ucred cred;
89 socklen_t cred_len = sizeof(cred);
90#endif
[2815]91 struct roar_vio_calls    vio;
92 struct sockaddr_storage  addr;
93 socklen_t                addrlen = sizeof(addr);
[0]94
[2815]95 fh = accept(sock, (struct sockaddr*)&addr, &addrlen);
[0]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
[490]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 }
[1182]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 }
[490]129#endif
130
[2815]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
[2530]137 ROAR_DBG("net_get_new_client(*): proto=0x%.4x", proto);
138
[2789]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);
[2529]141  return -1;
[2789]142 }
[2529]143
[2530]144 switch (proto) {
[2789]145  case ROAR_PROTO_ROARAUDIO:
146    // nothing needed to be done here
147   break;
[2545]148#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
[2791]149#ifdef ROAR_HAVE_H_ESD
[2530]150  case ROAR_PROTO_ESOUND:
[2789]151    ROAR_DBG("net_get_new_client(*): execing ESD CONNECT command");
152
153    if ( roar_vio_open_fh_socket(&vio, fh) == -1 )
[2530]154     return -1;
[2529]155
[2789]156    ROAR_DBG("net_get_new_client(*): creating VIO OK");
157
[2530]158    if ( emul_esd_exec_command(client, ESD_PROTO_CONNECT, &vio) == -1 )
159     return -1;
[2789]160
161    ROAR_DBG("net_get_new_client(*): CONNECT execed sucessfully");
[2530]162   break;
[2545]163#endif
164#endif
[2789]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;
[2530]172 }
[2529]173
[0]174// close(fh);
175
176 return 0;
177}
[1484]178#endif
[0]179
[1494]180#endif
181
[0]182//ll
Note: See TracBrowser for help on using the repository browser.