source: roaraudio/roard/network.c @ 1484:435ae42d82e7

Last change on this file since 1484:435ae42d82e7 was 1484:435ae42d82e7, checked in by phi, 15 years ago

only check for new connections on listening socket if we have select()

File size: 2.5 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_BROKEN_PEERCRED
28#undef SO_PEERCRED
29#endif
30
31#ifdef ROAR_HAVE_SELECT
32#define _CAN_OPERATE
33#endif
34
35int net_check_listen  (void) {
36#ifdef _CAN_OPERATE
37 int r;
38 fd_set sl;
39 struct timeval tv;
40
41 FD_ZERO(&sl);
42 FD_SET(g_listen_socket, &sl);
43
44 tv.tv_sec  = 0;
45 tv.tv_usec = 1;
46
47 if ((r = select(g_listen_socket + 1, &sl, NULL, NULL, &tv)) > 0) {
48  ROAR_DBG("net_check_listen(void): We have a connection!");
49  return net_get_new_client();
50 }
51
52 return r;
53#else
54 return -1;
55#endif
56}
57
58#ifdef _CAN_OPERATE
59int net_get_new_client (void) {
60 int fh;
61 int client;
62#if defined(SO_PEERCRED) || defined(ROAR_HAVE_GETPEEREID)
63 struct roar_client * c;
64#endif
65#ifdef SO_PEERCRED
66 struct ucred cred;
67 socklen_t cred_len = sizeof(cred);
68#endif
69
70 fh = accept(g_listen_socket, NULL, NULL);
71
72 ROAR_DBG("net_get_new_client(void): fh = %i", fh);
73
74 client = clients_new();
75
76 if ( clients_set_fh(client, fh) == -1 ) {
77  ROAR_ERR("net_get_new_client(void): Can not set client's fh");
78
79  clients_delete(client);
80  close(fh);
81
82  ROAR_DBG("net_get_new_client(void) = -1");
83  return -1;
84 }
85
86#ifdef SO_PEERCRED
87 if ( clients_get(client, &c) != -1 ) {
88  if (getsockopt(fh, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != -1) {
89   if ( cred.pid != 0 ) {
90    c->pid = cred.pid;
91    c->uid = cred.uid;
92    c->gid = cred.gid;
93   }
94  } else {
95   ROAR_DBG("req_on_identify(): Can't get creds via SO_PEERCRED: %s", strerror(errno));
96  }
97 }
98#elif defined(ROAR_HAVE_GETPEEREID)
99 if ( clients_get(client, &c) != -1 ) {
100  if (getpeereid(fh, &(c->uid), &(c->gid)) == -1) {
101   ROAR_DBG("req_on_identify(): Can't get creds via getpeereid(): %s", strerror(errno));
102  }
103 }
104#endif
105
106// close(fh);
107
108 return 0;
109}
110#endif
111
112//ll
Note: See TracBrowser for help on using the repository browser.