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 | int net_check_listen (void) { |
---|
32 | int r; |
---|
33 | fd_set sl; |
---|
34 | struct timeval tv; |
---|
35 | |
---|
36 | FD_ZERO(&sl); |
---|
37 | FD_SET(g_listen_socket, &sl); |
---|
38 | |
---|
39 | tv.tv_sec = 0; |
---|
40 | tv.tv_usec = 1; |
---|
41 | |
---|
42 | if ((r = select(g_listen_socket + 1, &sl, NULL, NULL, &tv)) > 0) { |
---|
43 | ROAR_DBG("net_check_listen(void): We have a connection!"); |
---|
44 | return net_get_new_client(); |
---|
45 | } |
---|
46 | |
---|
47 | return r; |
---|
48 | } |
---|
49 | |
---|
50 | int net_get_new_client (void) { |
---|
51 | int fh; |
---|
52 | int client; |
---|
53 | #ifdef SO_PEERCRED |
---|
54 | struct roar_client * c; |
---|
55 | struct ucred cred; |
---|
56 | socklen_t cred_len = sizeof(cred); |
---|
57 | #endif |
---|
58 | |
---|
59 | fh = accept(g_listen_socket, NULL, NULL); |
---|
60 | |
---|
61 | ROAR_DBG("net_get_new_client(void): fh = %i", fh); |
---|
62 | |
---|
63 | client = clients_new(); |
---|
64 | |
---|
65 | if ( clients_set_fh(client, fh) == -1 ) { |
---|
66 | ROAR_ERR("net_get_new_client(void): Can not set client's fh"); |
---|
67 | |
---|
68 | clients_delete(client); |
---|
69 | close(fh); |
---|
70 | |
---|
71 | ROAR_DBG("net_get_new_client(void) = -1"); |
---|
72 | return -1; |
---|
73 | } |
---|
74 | |
---|
75 | #ifdef SO_PEERCRED |
---|
76 | if ( clients_get(client, &c) != -1 ) { |
---|
77 | if (getsockopt(fh, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != -1) { |
---|
78 | if ( cred.pid != 0 ) { |
---|
79 | c->pid = cred.pid; |
---|
80 | c->uid = cred.uid; |
---|
81 | c->gid = cred.gid; |
---|
82 | } |
---|
83 | } else { |
---|
84 | ROAR_DBG("req_on_identify(): Can't get creds via SO_PEERCRED: %s", strerror(errno)); |
---|
85 | } |
---|
86 | } |
---|
87 | #endif |
---|
88 | |
---|
89 | // close(fh); |
---|
90 | |
---|
91 | return 0; |
---|
92 | } |
---|
93 | |
---|
94 | //ll |
---|