[4060] | 1 | //roarclientpass.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010 |
---|
| 5 | * |
---|
| 6 | * This file is part of roarclients 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, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
| 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include <roaraudio.h> |
---|
| 27 | |
---|
| 28 | void usage (void) { |
---|
| 29 | printf("roarclientpass [OPTIONS]...\n"); |
---|
| 30 | |
---|
| 31 | printf("\nOptions:\n\n"); |
---|
| 32 | |
---|
| 33 | printf(" --server SERVER - Set server hostname\n" |
---|
| 34 | " --stdin - Client is on stdin\n" |
---|
| 35 | " --stdout - Client is on stdout\n" |
---|
| 36 | " --stdio - Same as --stdin --stdout\n" |
---|
| 37 | " --stderr - Client is on stderr\n" |
---|
| 38 | " --client-fh FH - Client is on FH\n" |
---|
| 39 | " --proto PROTO - Client uses protocol PROTO (default: RoarAudio)\n" |
---|
| 40 | " --byteorder BO - Client uses byteorder BO (default: network)\n" |
---|
| 41 | " --listen - This is a listen mode connection\n" |
---|
[4061] | 42 | " --mode MODE - Set mode of operation: none, listen, connect (default: none)\n" |
---|
| 43 | " --bind BIND - Set host/node/path for mode listen and connect\n" |
---|
| 44 | " --port PORT - Set port for mode listen and connect\n" |
---|
| 45 | // " --type TYPE - Set type for mode listen and connect (default: unknown)\n" |
---|
[4060] | 46 | " --help - Show this help\n" |
---|
| 47 | ); |
---|
| 48 | |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | #define _BV(x) (1<<(x)) |
---|
| 52 | #define F_STDIN _BV(ROAR_STDIN) |
---|
| 53 | #define F_STDOUT _BV(ROAR_STDOUT) |
---|
| 54 | #define F_STDERR _BV(ROAR_STDERR) |
---|
| 55 | |
---|
| 56 | int main (int argc, char * argv[]) { |
---|
| 57 | struct roar_connection con; |
---|
| 58 | struct roar_client client; |
---|
| 59 | char * server = NULL; |
---|
| 60 | char * k; |
---|
| 61 | int i; |
---|
| 62 | int clientfh = -1; |
---|
| 63 | int cflags = 0; |
---|
| 64 | int flags = 0; |
---|
| 65 | int proto = ROAR_PROTO_ROARAUDIO; |
---|
| 66 | int byteorder = ROAR_BYTEORDER_NETWORK; |
---|
[4061] | 67 | int mode = ROAR_SOCKET_MODE_NONE; |
---|
| 68 | int type = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
| 69 | char * host = NULL; |
---|
| 70 | int port = -1; |
---|
[4060] | 71 | |
---|
| 72 | for (i = 1; i < argc; i++) { |
---|
| 73 | k = argv[i]; |
---|
| 74 | |
---|
| 75 | if ( !strcmp(k, "--server") || !strcmp(k, "-s") ) { |
---|
| 76 | server = argv[++i]; |
---|
| 77 | } else if ( !strcmp(k, "--stdin") ) { |
---|
| 78 | cflags |= F_STDIN; |
---|
| 79 | } else if ( !strcmp(k, "--stdout") ) { |
---|
| 80 | cflags |= F_STDOUT; |
---|
| 81 | } else if ( !strcmp(k, "--stderr") ) { |
---|
| 82 | cflags |= F_STDERR; |
---|
| 83 | } else if ( !strcmp(k, "--stdio") ) { |
---|
| 84 | cflags |= F_STDIN|F_STDOUT; |
---|
| 85 | } else if ( !strcmp(k, "--client-fh") ) { |
---|
| 86 | clientfh = atoi(argv[++i]); |
---|
| 87 | } else if ( !strcmp(k, "--proto") ) { |
---|
| 88 | proto = roar_str2proto(argv[++i]); |
---|
| 89 | } else if ( !strcmp(k, "--byteorder") ) { |
---|
| 90 | byteorder = roar_str2byteorder(argv[++i]); |
---|
| 91 | } else if ( !strcmp(k, "--listen") ) { |
---|
| 92 | flags |= ROAR_CLIENTPASS_FLAG_LISTEN; |
---|
[4061] | 93 | } else if ( !strcmp(k, "--mode") ) { |
---|
| 94 | k = argv[++i]; |
---|
| 95 | if ( !strcasecmp(k, "none") ) { |
---|
| 96 | mode = ROAR_SOCKET_MODE_NONE; |
---|
| 97 | } else if ( !strcasecmp(k, "listen") ) { |
---|
| 98 | mode = ROAR_SOCKET_MODE_LISTEN; |
---|
| 99 | flags |= ROAR_CLIENTPASS_FLAG_LISTEN; |
---|
| 100 | } else if ( !strcasecmp(k, "connect") ) { |
---|
| 101 | mode = ROAR_SOCKET_MODE_CONNECT; |
---|
| 102 | flags -= ROAR_CLIENTPASS_FLAG_LISTEN; |
---|
| 103 | } else { |
---|
| 104 | ROAR_ERR("unknown mode: %s", k); |
---|
| 105 | return 1; |
---|
| 106 | } |
---|
| 107 | } else if ( !strcmp(k, "--bind") ) { |
---|
| 108 | host = argv[++i]; |
---|
| 109 | } else if ( !strcmp(k, "--port") ) { |
---|
| 110 | port = atoi(argv[++i]); |
---|
[4063] | 111 | } else if ( !strcmp(k, "--help") || !strcmp(k, "-h") ) { |
---|
| 112 | usage(); |
---|
| 113 | return 0; |
---|
[4060] | 114 | } else { |
---|
| 115 | ROAR_ERR("unknown argument: %s", k); |
---|
| 116 | usage(); |
---|
| 117 | return 1; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | if ( cflags & F_STDERR ) { |
---|
| 122 | #ifdef ROAR_HAVE_SYSLOG |
---|
| 123 | roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG); |
---|
| 124 | #else |
---|
| 125 | roar_debug_set_stderr_fh(-1); |
---|
| 126 | #endif |
---|
| 127 | } else { |
---|
| 128 | roar_debug_set_stderr_fh(ROAR_STDERR); |
---|
| 129 | } |
---|
| 130 | |
---|
[4061] | 131 | if ( mode != ROAR_SOCKET_MODE_NONE ) { |
---|
| 132 | if ( clientfh != -1 ) { |
---|
| 133 | ROAR_ERR("Too may socket types given"); |
---|
| 134 | return 30; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | clientfh = roar_socket_open(mode, type, host, port); |
---|
| 138 | |
---|
| 139 | if ( clientfh == -1 ) { |
---|
| 140 | ROAR_ERR("Unabled to open socket"); |
---|
| 141 | return 31; |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
[4060] | 145 | if ( clientfh == -1 ) { |
---|
| 146 | if ( cflags & F_STDIN ) { |
---|
| 147 | clientfh = ROAR_STDIN; |
---|
| 148 | } else if ( cflags & F_STDOUT ) { |
---|
| 149 | clientfh = ROAR_STDOUT; |
---|
| 150 | } else if ( cflags & F_STDERR ) { |
---|
| 151 | clientfh = ROAR_STDERR; |
---|
| 152 | } else { |
---|
| 153 | ROAR_ERR("No client socket given"); |
---|
[4065] | 154 | return 32; |
---|
[4060] | 155 | } |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | roar_client_new(&client); |
---|
| 159 | roar_client_set_fh(&client, clientfh); |
---|
| 160 | roar_client_set_proto(&client, proto, byteorder); |
---|
| 161 | |
---|
| 162 | if ( roar_simple_connect(&con, server, "roarclientpass") == -1 ) { |
---|
| 163 | ROAR_ERR("Can not connect to server"); |
---|
| 164 | return 10; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | if ( roar_client_pass(&con, &client, flags) == -1 ) { |
---|
| 168 | ROAR_ERR("Can not pass client fh to server"); |
---|
| 169 | roar_disconnect(&con); |
---|
| 170 | return 20; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | roar_disconnect(&con); |
---|
| 174 | |
---|
| 175 | return 0; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | //ll |
---|