[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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[668] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
[0] | 26 | #include "roard.h" |
---|
| 27 | |
---|
[1494] | 28 | #ifdef ROAR_SUPPORT_LISTEN |
---|
| 29 | |
---|
[1055] | 30 | #ifdef ROAR_BROKEN_PEERCRED |
---|
| 31 | #undef SO_PEERCRED |
---|
| 32 | #endif |
---|
| 33 | |
---|
[1484] | 34 | #ifdef ROAR_HAVE_SELECT |
---|
| 35 | #define _CAN_OPERATE |
---|
| 36 | #endif |
---|
| 37 | |
---|
[0] | 38 | int net_check_listen (void) { |
---|
[1484] | 39 | #ifdef _CAN_OPERATE |
---|
[0] | 40 | int r; |
---|
| 41 | fd_set sl; |
---|
| 42 | struct timeval tv; |
---|
[2530] | 43 | int i; |
---|
| 44 | int max_fh = -1; |
---|
[0] | 45 | |
---|
| 46 | FD_ZERO(&sl); |
---|
[2530] | 47 | |
---|
| 48 | for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) { |
---|
[3129] | 49 | if ( g_listen[i].socket != -1 ) { |
---|
| 50 | if ( g_listen[i].socket > max_fh ) |
---|
| 51 | max_fh = g_listen[i].socket; |
---|
[2530] | 52 | |
---|
[3129] | 53 | FD_SET(g_listen[i].socket, &sl); |
---|
[2530] | 54 | } |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | if ( max_fh == -1 ) |
---|
| 58 | return 0; |
---|
[0] | 59 | |
---|
| 60 | tv.tv_sec = 0; |
---|
| 61 | tv.tv_usec = 1; |
---|
| 62 | |
---|
[2530] | 63 | if ((r = select(max_fh + 1, &sl, NULL, NULL, &tv)) > 0) { |
---|
[0] | 64 | ROAR_DBG("net_check_listen(void): We have a connection!"); |
---|
[2530] | 65 | for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) { |
---|
[3129] | 66 | if ( g_listen[i].socket != -1 ) { |
---|
| 67 | if ( FD_ISSET(g_listen[i].socket, &sl) ) { |
---|
[3253] | 68 | if ( net_get_new_client(&(g_listen[i])) == -1 ) |
---|
[2530] | 69 | return -1; |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | } |
---|
[0] | 73 | } |
---|
| 74 | |
---|
| 75 | return r; |
---|
[1484] | 76 | #else |
---|
| 77 | return -1; |
---|
| 78 | #endif |
---|
[0] | 79 | } |
---|
| 80 | |
---|
[1484] | 81 | #ifdef _CAN_OPERATE |
---|
[3253] | 82 | int net_get_new_client (struct roard_listen * lsock) { |
---|
[0] | 83 | int fh; |
---|
| 84 | int client; |
---|
[1183] | 85 | struct roar_client * c; |
---|
[490] | 86 | #ifdef SO_PEERCRED |
---|
| 87 | struct ucred cred; |
---|
| 88 | socklen_t cred_len = sizeof(cred); |
---|
| 89 | #endif |
---|
[2815] | 90 | struct roar_vio_calls vio; |
---|
| 91 | struct sockaddr_storage addr; |
---|
| 92 | socklen_t addrlen = sizeof(addr); |
---|
[0] | 93 | |
---|
[3253] | 94 | fh = accept(lsock->socket, (struct sockaddr*)&addr, &addrlen); |
---|
[0] | 95 | |
---|
| 96 | ROAR_DBG("net_get_new_client(void): fh = %i", fh); |
---|
| 97 | |
---|
| 98 | client = clients_new(); |
---|
| 99 | |
---|
| 100 | if ( clients_set_fh(client, fh) == -1 ) { |
---|
| 101 | ROAR_ERR("net_get_new_client(void): Can not set client's fh"); |
---|
| 102 | |
---|
| 103 | clients_delete(client); |
---|
| 104 | close(fh); |
---|
| 105 | |
---|
| 106 | ROAR_DBG("net_get_new_client(void) = -1"); |
---|
| 107 | return -1; |
---|
| 108 | } |
---|
| 109 | |
---|
[2820] | 110 | if ( clients_get(client, &c) != -1 ) { |
---|
[490] | 111 | #ifdef SO_PEERCRED |
---|
| 112 | if (getsockopt(fh, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != -1) { |
---|
| 113 | if ( cred.pid != 0 ) { |
---|
| 114 | c->pid = cred.pid; |
---|
| 115 | c->uid = cred.uid; |
---|
| 116 | c->gid = cred.gid; |
---|
| 117 | } |
---|
| 118 | } else { |
---|
| 119 | ROAR_DBG("req_on_identify(): Can't get creds via SO_PEERCRED: %s", strerror(errno)); |
---|
| 120 | } |
---|
[1182] | 121 | #elif defined(ROAR_HAVE_GETPEEREID) |
---|
| 122 | if (getpeereid(fh, &(c->uid), &(c->gid)) == -1) { |
---|
| 123 | ROAR_DBG("req_on_identify(): Can't get creds via getpeereid(): %s", strerror(errno)); |
---|
| 124 | } |
---|
[490] | 125 | #endif |
---|
| 126 | |
---|
[2820] | 127 | if ( roar_nnode_free(&(c->nnode)) == -1 ) |
---|
| 128 | return -1; |
---|
[2815] | 129 | |
---|
[2820] | 130 | if ( roar_nnode_new_from_sockaddr(&(c->nnode), (struct sockaddr*)&addr, addrlen) == -1 ) |
---|
| 131 | return -1; |
---|
| 132 | } |
---|
[2815] | 133 | |
---|
[3253] | 134 | ROAR_DBG("net_get_new_client(*): proto=0x%.4x", lsock->proto); |
---|
[2530] | 135 | |
---|
[3253] | 136 | if ( clients_set_proto(client, lsock->proto) == -1 ) { |
---|
| 137 | ROAR_WARN("net_get_new_client(*): Setting proto(0x%.4x) of client %i failed.", lsock->proto, client); |
---|
[2529] | 138 | return -1; |
---|
[2789] | 139 | } |
---|
[2529] | 140 | |
---|
[3253] | 141 | switch (lsock->proto) { |
---|
[2789] | 142 | case ROAR_PROTO_ROARAUDIO: |
---|
| 143 | // nothing needed to be done here |
---|
| 144 | break; |
---|
[2545] | 145 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD |
---|
[2791] | 146 | #ifdef ROAR_HAVE_H_ESD |
---|
[2530] | 147 | case ROAR_PROTO_ESOUND: |
---|
[2789] | 148 | ROAR_DBG("net_get_new_client(*): execing ESD CONNECT command"); |
---|
| 149 | |
---|
| 150 | if ( roar_vio_open_fh_socket(&vio, fh) == -1 ) |
---|
[2530] | 151 | return -1; |
---|
[2529] | 152 | |
---|
[2789] | 153 | ROAR_DBG("net_get_new_client(*): creating VIO OK"); |
---|
| 154 | |
---|
[2530] | 155 | if ( emul_esd_exec_command(client, ESD_PROTO_CONNECT, &vio) == -1 ) |
---|
| 156 | return -1; |
---|
[2789] | 157 | |
---|
| 158 | ROAR_DBG("net_get_new_client(*): CONNECT execed sucessfully"); |
---|
[2530] | 159 | break; |
---|
[2545] | 160 | #endif |
---|
[3359] | 161 | #endif |
---|
[3603] | 162 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE |
---|
[3253] | 163 | case ROAR_PROTO_SIMPLE: |
---|
[3255] | 164 | if ( emul_simple_on_connect(client, lsock) == -1 ) |
---|
| 165 | return -1; |
---|
[3253] | 166 | break; |
---|
[3596] | 167 | #endif |
---|
[2789] | 168 | default: |
---|
| 169 | // OS independiend code to close the socket: |
---|
| 170 | if ( roar_vio_open_fh_socket(&vio, fh) == -1 ) |
---|
| 171 | return -1; |
---|
| 172 | roar_vio_close(&vio); |
---|
| 173 | return -1; |
---|
| 174 | break; |
---|
[2530] | 175 | } |
---|
[2529] | 176 | |
---|
[0] | 177 | // close(fh); |
---|
| 178 | |
---|
| 179 | return 0; |
---|
| 180 | } |
---|
[1484] | 181 | #endif |
---|
[0] | 182 | |
---|
[1494] | 183 | #endif |
---|
| 184 | |
---|
[0] | 185 | //ll |
---|