[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 | |
---|
[1055] | 27 | #ifdef ROAR_BROKEN_PEERCRED |
---|
| 28 | #undef SO_PEERCRED |
---|
| 29 | #endif |
---|
| 30 | |
---|
[0] | 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; |
---|
[1183] | 53 | #if defined(SO_PEERCRED) || defined(ROAR_HAVE_GETPEEREID) |
---|
| 54 | struct roar_client * c; |
---|
| 55 | #endif |
---|
[490] | 56 | #ifdef SO_PEERCRED |
---|
| 57 | struct ucred cred; |
---|
| 58 | socklen_t cred_len = sizeof(cred); |
---|
| 59 | #endif |
---|
[0] | 60 | |
---|
| 61 | fh = accept(g_listen_socket, NULL, NULL); |
---|
| 62 | |
---|
| 63 | ROAR_DBG("net_get_new_client(void): fh = %i", fh); |
---|
| 64 | |
---|
| 65 | client = clients_new(); |
---|
| 66 | |
---|
| 67 | if ( clients_set_fh(client, fh) == -1 ) { |
---|
| 68 | ROAR_ERR("net_get_new_client(void): Can not set client's fh"); |
---|
| 69 | |
---|
| 70 | clients_delete(client); |
---|
| 71 | close(fh); |
---|
| 72 | |
---|
| 73 | ROAR_DBG("net_get_new_client(void) = -1"); |
---|
| 74 | return -1; |
---|
| 75 | } |
---|
| 76 | |
---|
[490] | 77 | #ifdef SO_PEERCRED |
---|
| 78 | if ( clients_get(client, &c) != -1 ) { |
---|
| 79 | if (getsockopt(fh, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != -1) { |
---|
| 80 | if ( cred.pid != 0 ) { |
---|
| 81 | c->pid = cred.pid; |
---|
| 82 | c->uid = cred.uid; |
---|
| 83 | c->gid = cred.gid; |
---|
| 84 | } |
---|
| 85 | } else { |
---|
| 86 | ROAR_DBG("req_on_identify(): Can't get creds via SO_PEERCRED: %s", strerror(errno)); |
---|
| 87 | } |
---|
| 88 | } |
---|
[1182] | 89 | #elif defined(ROAR_HAVE_GETPEEREID) |
---|
| 90 | if ( clients_get(client, &c) != -1 ) { |
---|
| 91 | if (getpeereid(fh, &(c->uid), &(c->gid)) == -1) { |
---|
| 92 | ROAR_DBG("req_on_identify(): Can't get creds via getpeereid(): %s", strerror(errno)); |
---|
| 93 | } |
---|
| 94 | } |
---|
[490] | 95 | #endif |
---|
| 96 | |
---|
[0] | 97 | // close(fh); |
---|
| 98 | |
---|
| 99 | return 0; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | //ll |
---|