[0] | 1 | //network.c: |
---|
| 2 | |
---|
[668] | 3 | /* |
---|
[3358] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010 |
---|
[668] | 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] | 37 | int 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++) { |
---|
[3129] | 48 | if ( g_listen[i].socket != -1 ) { |
---|
| 49 | if ( g_listen[i].socket > max_fh ) |
---|
| 50 | max_fh = g_listen[i].socket; |
---|
[2530] | 51 | |
---|
[3129] | 52 | FD_SET(g_listen[i].socket, &sl); |
---|
[2530] | 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++) { |
---|
[3129] | 65 | if ( g_listen[i].socket != -1 ) { |
---|
| 66 | if ( FD_ISSET(g_listen[i].socket, &sl) ) { |
---|
[3253] | 67 | if ( net_get_new_client(&(g_listen[i])) == -1 ) |
---|
[2530] | 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 |
---|
[3253] | 81 | int net_get_new_client (struct roard_listen * lsock) { |
---|
[0] | 82 | int fh; |
---|
| 83 | int client; |
---|
[1183] | 84 | struct roar_client * c; |
---|
[490] | 85 | #ifdef SO_PEERCRED |
---|
| 86 | struct ucred cred; |
---|
| 87 | socklen_t cred_len = sizeof(cred); |
---|
| 88 | #endif |
---|
[2815] | 89 | struct roar_vio_calls vio; |
---|
| 90 | struct sockaddr_storage addr; |
---|
| 91 | socklen_t addrlen = sizeof(addr); |
---|
[0] | 92 | |
---|
[3253] | 93 | fh = accept(lsock->socket, (struct sockaddr*)&addr, &addrlen); |
---|
[0] | 94 | |
---|
| 95 | ROAR_DBG("net_get_new_client(void): fh = %i", fh); |
---|
| 96 | |
---|
| 97 | client = clients_new(); |
---|
| 98 | |
---|
| 99 | if ( clients_set_fh(client, fh) == -1 ) { |
---|
| 100 | ROAR_ERR("net_get_new_client(void): Can not set client's fh"); |
---|
| 101 | |
---|
| 102 | clients_delete(client); |
---|
| 103 | close(fh); |
---|
| 104 | |
---|
| 105 | ROAR_DBG("net_get_new_client(void) = -1"); |
---|
| 106 | return -1; |
---|
| 107 | } |
---|
| 108 | |
---|
[2820] | 109 | if ( clients_get(client, &c) != -1 ) { |
---|
[490] | 110 | #ifdef SO_PEERCRED |
---|
| 111 | if (getsockopt(fh, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != -1) { |
---|
| 112 | if ( cred.pid != 0 ) { |
---|
| 113 | c->pid = cred.pid; |
---|
| 114 | c->uid = cred.uid; |
---|
| 115 | c->gid = cred.gid; |
---|
| 116 | } |
---|
| 117 | } else { |
---|
| 118 | ROAR_DBG("req_on_identify(): Can't get creds via SO_PEERCRED: %s", strerror(errno)); |
---|
| 119 | } |
---|
[1182] | 120 | #elif defined(ROAR_HAVE_GETPEEREID) |
---|
| 121 | if (getpeereid(fh, &(c->uid), &(c->gid)) == -1) { |
---|
| 122 | ROAR_DBG("req_on_identify(): Can't get creds via getpeereid(): %s", strerror(errno)); |
---|
| 123 | } |
---|
[490] | 124 | #endif |
---|
| 125 | |
---|
[2820] | 126 | if ( roar_nnode_free(&(c->nnode)) == -1 ) |
---|
| 127 | return -1; |
---|
[2815] | 128 | |
---|
[2820] | 129 | if ( roar_nnode_new_from_sockaddr(&(c->nnode), (struct sockaddr*)&addr, addrlen) == -1 ) |
---|
| 130 | return -1; |
---|
| 131 | } |
---|
[2815] | 132 | |
---|
[3253] | 133 | ROAR_DBG("net_get_new_client(*): proto=0x%.4x", lsock->proto); |
---|
[2530] | 134 | |
---|
[3253] | 135 | if ( clients_set_proto(client, lsock->proto) == -1 ) { |
---|
| 136 | ROAR_WARN("net_get_new_client(*): Setting proto(0x%.4x) of client %i failed.", lsock->proto, client); |
---|
[2529] | 137 | return -1; |
---|
[2789] | 138 | } |
---|
[2529] | 139 | |
---|
[3253] | 140 | switch (lsock->proto) { |
---|
[2789] | 141 | case ROAR_PROTO_ROARAUDIO: |
---|
| 142 | // nothing needed to be done here |
---|
| 143 | break; |
---|
[2545] | 144 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD |
---|
[2791] | 145 | #ifdef ROAR_HAVE_H_ESD |
---|
[2530] | 146 | case ROAR_PROTO_ESOUND: |
---|
[2789] | 147 | ROAR_DBG("net_get_new_client(*): execing ESD CONNECT command"); |
---|
| 148 | |
---|
| 149 | if ( roar_vio_open_fh_socket(&vio, fh) == -1 ) |
---|
[2530] | 150 | return -1; |
---|
[2529] | 151 | |
---|
[2789] | 152 | ROAR_DBG("net_get_new_client(*): creating VIO OK"); |
---|
| 153 | |
---|
[2530] | 154 | if ( emul_esd_exec_command(client, ESD_PROTO_CONNECT, &vio) == -1 ) |
---|
| 155 | return -1; |
---|
[2789] | 156 | |
---|
| 157 | ROAR_DBG("net_get_new_client(*): CONNECT execed sucessfully"); |
---|
[2530] | 158 | break; |
---|
[2545] | 159 | #endif |
---|
[3359] | 160 | #endif |
---|
[3253] | 161 | case ROAR_PROTO_SIMPLE: |
---|
[3255] | 162 | if ( emul_simple_on_connect(client, lsock) == -1 ) |
---|
| 163 | return -1; |
---|
[3253] | 164 | break; |
---|
[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 |
---|