source: roaraudio/roarclients/roarshout.c @ 4757:0e96e320f0b9

Last change on this file since 4757:0e96e320f0b9 was 4757:0e96e320f0b9, checked in by phi, 13 years ago

warn if password is default password ('hackme')

File size: 7.4 KB
Line 
1//roarshout.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
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#ifdef ROAR_HAVE_LIBSHOUT
29#include <shout/shout.h>
30
31#define BUFSIZE 2048
32
33void usage (void) {
34 printf("roarshout [OPTIONS]... [address [port [password [mountpoint]]]\n");
35
36 printf("\nRoarAudio Options:\n\n");
37
38 printf("    --server SERVER    - Set server hostname\n"
39        "    --rate   RATE      - Set sample rate\n"
40        "    --bits   BITS      - Set bits per sample\n"
41        "    --chans  CHANNELS  - Set number of channels\n"
42        "    --codec  CODEC     - Set the codec\n"
43        " -h --help             - Show this help\n"
44        "    --pw-arg           - Password is supplied as argument (default).\n"
45        "    --pw-ask           - Ask user for password interactively.\n"
46        "    --pw-dstr          - Read password from file. Filename is supplied\n"
47        "                         as normal password argument.\n"
48       );
49
50 printf("\nlibshout Options:\n\n");
51
52 printf(" -p --public           - Allow listing in stream directory\n"
53        " -d          DESC      - Set stream description\n"
54        " -g          GENRE     - Set stream genre\n"
55        " -n          NAME      - Set stream name\n"
56        " -u          URL       - Set stream URL/homepage\n"
57       );
58
59}
60
61char * read_pw_from_file(char * filename, char * buffer, size_t bufferlen) {
62 struct roar_vio_defaults def;
63 struct roar_vio_calls file;
64 ssize_t len;
65
66 if (filename == NULL || buffer == NULL)
67  return NULL;
68
69 if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_RDONLY, 0644) == -1 )
70  return NULL;
71
72 if ( roar_vio_open_dstr(&file, filename, &def, 1) == -1 )
73  return NULL;
74
75 len = roar_vio_read(&file, buffer, bufferlen - 1);
76
77 if ( len == -1 )
78  return NULL;
79
80 // strip newlions.
81 for (; buffer[len-1] == '\r' || buffer[len-1] == '\n'; len--);
82
83 buffer[len] = 0;
84
85 roar_vio_close(&file);
86
87 return buffer;
88}
89
90int main (int argc, char * argv[]) {
91 enum { ARG, ASK, DSTR } pw_source = ARG;
92 int    rate     = 44100;
93 int    bits     = 16;
94 int    channels = 2;
95 int    codec    = ROAR_CODEC_OGG_VORBIS;
96 char * server   = NULL;
97 char * k;
98 char * s_server = NULL;
99 char * s_mount  = NULL;
100 char * s_pw     = NULL;
101 int    s_port   = -1;
102 char * s_desc   = NULL;
103 char * s_genre  = NULL;
104 char * s_name   = NULL;
105 char * s_url    = NULL;
106 int    s_public = 0;
107 int    fh;
108 int    i;
109 char buf[BUFSIZE];
110 shout_t * shout;
111 char password_buf[128];
112
113 for (i = 1; i < argc; i++) {
114  k = argv[i];
115
116  if ( strcmp(k, "--server") == 0 ) {
117   server = argv[++i];
118  } else if ( strcmp(k, "--rate") == 0 ) {
119   rate = atoi(argv[++i]);
120  } else if ( strcmp(k, "--bits") == 0 ) {
121   bits = atoi(argv[++i]);
122  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) {
123   channels = atoi(argv[++i]);
124  } else if ( strcmp(k, "--codec") == 0 ) {
125   codec = roar_str2codec(argv[++i]);
126  } else if ( strcmp(k, "--pw-arg") == 0 ) {
127   pw_source = ARG;
128  } else if ( strcmp(k, "--pw-ask") == 0 ) {
129   pw_source = ASK;
130  } else if ( strcmp(k, "--pw-dstr") == 0 ) {
131   pw_source = DSTR;
132  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--public") == 0 ) {
133   s_public = 1;
134  } else if ( strcmp(k, "-d") == 0 ) {
135   s_desc   = argv[++i];
136  } else if ( strcmp(k, "-g") == 0 ) {
137   s_genre  = argv[++i];
138  } else if ( strcmp(k, "-n") == 0 ) {
139   s_name   = argv[++i];
140  } else if ( strcmp(k, "-u") == 0 ) {
141   s_url    = argv[++i];
142  } else if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
143   usage();
144   return 0;
145  } else if ( s_server == NULL ) {
146   s_server = k;
147  } else if ( s_port   == -1 ) {
148   s_port   = atoi(k);
149  } else if ( s_pw     == NULL ) {
150   s_pw     = k;
151  } else if ( s_mount  == NULL ) {
152   s_mount  = k;
153  } else {
154   fprintf(stderr, "Error: unknown argument: %s\n", k);
155   usage();
156   return 1;
157  }
158 }
159
160 switch (pw_source) {
161  case ARG:
162    // nothing to do
163   break;
164  case ASK:
165    if ( roar_passwd_simple_ask_pw(&s_pw, "Password for icecast server?", NULL) == -1 ) {
166     fprintf(stderr, "Error: unabled to read password from user.\n");
167     return 1;
168    }
169    strncpy(password_buf, s_pw, sizeof(password_buf)-1);
170    roar_mm_free(s_pw);
171    password_buf[sizeof(password_buf)-2] = 0;
172    s_pw = password_buf;
173   break;
174  case DSTR:
175    if ( (s_pw = read_pw_from_file(s_pw, password_buf, sizeof(password_buf))) == NULL ) {
176     fprintf(stderr, "Error: unabled to read password from file.\n");
177     return 1;
178    }
179   break;
180 }
181
182 if ( s_server == NULL )
183  s_server = "localhost";
184
185 if ( s_mount == NULL )
186  s_mount  = "/roar.ogg";
187
188 if ( s_pw == NULL )
189  s_pw     = "hackme";
190
191 if ( s_port == -1 )
192  s_port   = 8000;
193
194 if ( !strcasecmp(s_pw, "hackme") ) {
195  fprintf(stderr, "Warning: Your password is very weak. Change it!\n");
196 }
197
198 shout_init();
199
200 if (!(shout = shout_new())) {
201  ROAR_ERR("Can not create shout object");
202  return 1;
203 }
204
205 if (shout_set_host(shout, s_server) != SHOUTERR_SUCCESS) {
206  ROAR_ERR("Error setting hostname: %s", shout_get_error(shout));
207  return 1;
208 }
209
210 if (shout_set_protocol(shout, SHOUT_PROTOCOL_HTTP) != SHOUTERR_SUCCESS) {
211  ROAR_ERR("Error setting protocol: %s", shout_get_error(shout));
212  return 1;
213 }
214
215 if (shout_set_port(shout, s_port) != SHOUTERR_SUCCESS) {
216  ROAR_ERR("Error setting port: %s", shout_get_error(shout));
217  return 1;
218 }
219
220 if (shout_set_password(shout, s_pw) != SHOUTERR_SUCCESS) {
221  ROAR_ERR("Error setting password: %s", shout_get_error(shout));
222  return 1;
223 }
224
225 if (shout_set_mount(shout, s_mount) != SHOUTERR_SUCCESS) {
226  ROAR_ERR("Error setting mount: %s", shout_get_error(shout));
227  return 1;
228 }
229
230 if (shout_set_user(shout, "source") != SHOUTERR_SUCCESS) {
231  ROAR_ERR("Error setting user: %s", shout_get_error(shout));
232  return 1;
233 }
234
235 if (shout_set_format(shout, SHOUT_FORMAT_OGG) != SHOUTERR_SUCCESS) {
236  ROAR_ERR("Error setting format: %s", shout_get_error(shout));
237  return 1;
238 }
239
240 shout_set_public(shout, s_public);
241
242 if (s_desc  != NULL)
243  shout_set_description(shout, s_desc);
244
245 if (s_genre != NULL)
246  shout_set_genre(shout, s_genre);
247
248 if (s_name  != NULL)
249  shout_set_name(shout, s_name);
250
251 if (s_url   != NULL)
252  shout_set_url(shout, s_url);
253
254 if ( (fh = roar_simple_monitor(rate, channels, bits, codec, server, "roarshout")) == -1 ) {
255  fprintf(stderr, "Error: can not start monitoring\n");
256  return 1;
257 }
258
259 if (shout_open(shout) != SHOUTERR_SUCCESS) {
260  ROAR_ERR("Can not open connection via libshout!");
261  return -1;
262 }
263
264 while((i = read(fh, buf, BUFSIZE)))
265  if (shout_send(shout, (unsigned char*)buf, i) != SHOUTERR_SUCCESS)
266   break;
267
268 roar_simple_close(fh);
269
270 shout_sync(shout);
271
272 shout_close(shout);
273
274 shout_shutdown();
275
276 return 0;
277}
278
279#else
280int main (void) {
281 fprintf(stderr, "No libshout support compiled in!\n");
282 return 1;
283}
284#endif
285//ll
Note: See TracBrowser for help on using the repository browser.