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