yp.c 25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * Copyright 2000-2004, Jack Moffitt <jack@xiph.org, 
 *                      Michael Smith <msmith@xiph.org>,
 *                      oddsock <oddsock@xiph.org>,
 *                      Karl Heyes <karl@xiph.org>
 *                      and others (see AUTHORS for details).
 */

13
/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- */
14
15
16
17
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

18
19
#include <stdio.h>
#include <string.h>
20
#include <stdlib.h>
21
#include <curl/curl.h>
22
23
24
25
26
27
28
29
30

#include <thread/thread.h>

#include "connection.h"
#include "refbuf.h"
#include "client.h"
#include "logging.h"
#include "format.h"
#include "source.h"
31
#include "cfgfile.h"
32
#include "stats.h"
33
34
35
36

#ifdef WIN32
#define snprintf _snprintf
#endif
37

Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
38
#define CATMODULE "yp" 
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
struct yp_server
{
    char        *url;
    unsigned    url_timeout;
    unsigned    touch_interval;
    int         remove;

    CURL *curl;
    struct ypdata_tag *mounts, *pending_mounts;
    struct yp_server *next;
    char curl_error[CURL_ERROR_SIZE];
};



typedef struct ypdata_tag
{
    int remove;
58
    int release;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    int cmd_ok;

    char *sid;
    char *mount;
    char *url;
    char *listen_url;
    char *server_name;
    char *server_desc;
    char *server_genre;
    char *cluster_password;
    char *bitrate;
    char *audio_info;
    char *server_type;
    char *current_song;
73
    char *subtype;
74
75
76
77
78
79
80
81
82
83
84
85
86
87

    struct yp_server *server;
    time_t      next_update;
    unsigned    touch_interval;
    char        *error_msg;
    unsigned    (*process)(struct ypdata_tag *yp, char *s, unsigned len);

    struct ypdata_tag *next;
} ypdata_t;


static rwlock_t yp_lock;
static mutex_t yp_pending_lock;

88
volatile static struct yp_server *active_yps = NULL, *pending_yps = NULL;
89
static volatile int yp_update = 0;
90
91
92
93
94
static int yp_running;
static time_t now;
static thread_type *yp_thread;

static void *yp_update_thread(void *arg);
95
static void add_yp_info (ypdata_t *yp, void *info, int type);
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
static unsigned do_yp_remove (ypdata_t *yp, char *s, unsigned len);
static unsigned do_yp_add (ypdata_t *yp, char *s, unsigned len);
static unsigned do_yp_touch (ypdata_t *yp, char *s, unsigned len);
static void yp_destroy_ypdata(ypdata_t *ypdata);


/* curl callback used to parse headers coming back from the YP server */
static int handle_returned_header (void *ptr, size_t size, size_t nmemb, void *stream)
{
    ypdata_t *yp = stream;
    unsigned bytes = size * nmemb;

    /* DEBUG2 ("header from YP is \"%.*s\"", bytes, ptr); */
    if (strncmp (ptr, "YPResponse: 1", 13) == 0)
        yp->cmd_ok = 1;

    if (strncmp (ptr, "YPMessage: ", 11) == 0)
    {
        unsigned len = bytes - 11;
        free (yp->error_msg);
        yp->error_msg = calloc (1, len);
        if (yp->error_msg)
            sscanf (ptr, "YPMessage: %[^\r\n]", yp->error_msg);
    }

    if (yp->process == do_yp_add)
    {
        if (strncmp (ptr, "SID: ", 5) == 0)
        {
            unsigned len = bytes - 5;
            free (yp->sid);
            yp->sid = calloc (1, len);
            if (yp->sid)
                sscanf (ptr, "SID: %[^\r\n]", yp->sid);
        }
        if (strncmp (ptr, "TouchFreq: ", 11) == 0)
        {
            unsigned secs;
            sscanf (ptr, "TouchFreq: %u", &secs);
            if (secs < 30)
                secs = 30;
            DEBUG1 ("server touch interval is %u", secs);
            yp->touch_interval = secs;
        }
    }
    return (int)bytes;
}


/* capture returned data, but don't do anything with it, shouldn't be any */
static int handle_returned_data (void *ptr, size_t size, size_t nmemb, void *stream)
{
    return (int)(size*nmemb);
}


/* search the active and pending YP server lists */
static struct yp_server *find_yp_server (const char *url)
{
    struct yp_server *server;

157
    server = (struct yp_server *)active_yps;
158
159
160
161
162
163
    while (server)
    {
        if (strcmp (server->url, url) == 0)
            return server;
        server = server->next;
    }
164
    server = (struct yp_server *)pending_yps;
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    while (server)
    {
        if (strcmp (server->url, url) == 0)
            break;
        server = server->next;
    }
    return server;
}


static void destroy_yp_server (struct yp_server *server)
{
    if (server == NULL)
        return;
    DEBUG1 ("Removing YP server entry for %s", server->url);
    if (server->curl)
        curl_easy_cleanup (server->curl);
    if (server->mounts) WARN0 ("active ypdata not freed up");
    if (server->pending_mounts) WARN0 ("pending ypdata not freed up");
    free (server->url);
    free (server);
}



/* search for a ypdata entry corresponding to a specific mountpoint */
191
static ypdata_t *find_yp_mount (ypdata_t *mounts, const char *mount)
192
{
193
    ypdata_t *yp = mounts;
194
195
196
197
198
199
200
201
202
    while (yp)
    {
        if (strcmp (yp->mount, mount) == 0)
            break;
        yp = yp->next;
    }
    return yp;
}

203
204
205

void yp_recheck_config (ice_config_t *config)
{
206
207
208
209
210
211
    int i;
    struct yp_server *server;

    DEBUG0("Updating YP configuration");
    thread_rwlock_rlock (&yp_lock);

212
    server = (struct yp_server *)active_yps;
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    while (server)
    {
        server->remove = 1;
        server = server->next;
    }
    /* for each yp url in config, check to see if one exists 
       if not, then add it. */
    for (i=0 ; i < config->num_yp_directories; i++)
    {
        server = find_yp_server (config->yp_url[i]);
        if (server == NULL)
        {
            server = calloc (1, sizeof (struct yp_server));

            if (server == NULL)
            {
                destroy_yp_server (server);
                break;
            }
            server->url = strdup (config->yp_url[i]);
            server->url_timeout = config->yp_url_timeout[i];
234
            server->touch_interval = config->yp_touch_interval[i];
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
            server->curl = curl_easy_init();
            if (server->curl == NULL)
            {
                destroy_yp_server (server);
                break;
            }
            if (server->touch_interval < 30)
                server->touch_interval = 30;
            curl_easy_setopt (server->curl, CURLOPT_URL, server->url);
            curl_easy_setopt (server->curl, CURLOPT_HEADERFUNCTION, handle_returned_header);
            curl_easy_setopt (server->curl, CURLOPT_WRITEFUNCTION, handle_returned_data);
            curl_easy_setopt (server->curl, CURLOPT_WRITEDATA, server->curl);
            curl_easy_setopt (server->curl, CURLOPT_TIMEOUT, server->url_timeout);
            curl_easy_setopt (server->curl, CURLOPT_NOSIGNAL, 1L);
            curl_easy_setopt (server->curl, CURLOPT_ERRORBUFFER, &(server->curl_error[0]));
250
            server->next = (struct yp_server *)pending_yps;
251
252
253
254
255
256
257
258
259
260
261
            pending_yps = server;
            INFO3 ("Adding new YP server \"%s\" (timeout %ds, default interval %ds)",
                    server->url, server->url_timeout, server->touch_interval);
        }
        else
        {
            server->remove = 0;
        }
    }
    thread_rwlock_unlock (&yp_lock);
    yp_update = 1;
262
263
}

264

265
266
void yp_initialize()
{
267
    ice_config_t *config = config_get_config();
268
269
    thread_rwlock_create (&yp_lock);
    thread_mutex_create (&yp_pending_lock);
270
271
    yp_recheck_config (config);
    config_release_config ();
272
273
    yp_thread = thread_create("YP Touch Thread", yp_update_thread,
                            (void *)NULL, THREAD_ATTACHED);
274
}
275
276
277
278
279



/* handler for curl, checks if successful handling occurred */
static int send_to_yp (const char *cmd, ypdata_t *yp, char *post)
280
{
281
282
283
284
285
286
287
288
289
290
291
    int curlcode;
    struct yp_server *server = yp->server;

    /* DEBUG2 ("send YP (%s):%s", cmd, post); */
    yp->cmd_ok = 0;
    curl_easy_setopt (server->curl, CURLOPT_POSTFIELDS, post);
    curl_easy_setopt (server->curl, CURLOPT_WRITEHEADER, yp);
    curlcode = curl_easy_perform (server->curl);
    if (curlcode)
    {
        yp->process = do_yp_add;
292
        yp->next_update += 300;
293
294
        ERROR2 ("connection to %s failed with \"%s\"", server->url, server->curl_error);
        return -1;
295
    }
296
297
    if (yp->cmd_ok == 0)
    {
298
299
        if (yp->error_msg == NULL)
            yp->error_msg = strdup ("no response from server");
300
        yp->process = do_yp_add;
301
        yp->next_update += 300;
302
303
        ERROR3 ("YP %s on %s failed: %s", cmd, server->url, yp->error_msg);
        return -1;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
304
    }
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
    DEBUG2 ("YP %s at %s succeeded", cmd, server->url);
    return 0;
}


/* routines for building and issues requests to the YP server */
static unsigned do_yp_remove (ypdata_t *yp, char *s, unsigned len)
{
    if (yp->sid)
    {
        int ret = snprintf (s, len, "action=remove&sid=%s", yp->sid);
        if (ret >= (signed)len)
            return ret+1;

        INFO1 ("clearing up YP entry for %s", yp->mount);
        send_to_yp ("remove", yp, s);
        free (yp->sid);
        yp->sid = NULL;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
323
    }
324
325
326
    yp_update = 1;
    yp->remove = 1;
    yp->process = do_yp_add;
327

328
    return 0;
329
330
}

331
332

static unsigned do_yp_add (ypdata_t *yp, char *s, unsigned len)
333
{
334
335
336
    int ret;
    char *value;

337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
    value = stats_get_value (yp->mount, "server_type");
    add_yp_info (yp, value, YP_SERVER_TYPE);
    free (value);

    value = stats_get_value (yp->mount, "server_name");
    add_yp_info (yp, value, YP_SERVER_NAME);
    free (value);

    value = stats_get_value (yp->mount, "server_url");
    add_yp_info (yp, value, YP_SERVER_URL);
    free (value);

    value = stats_get_value (yp->mount, "genre");
    add_yp_info (yp, value, YP_SERVER_GENRE);
    free (value);

    value = stats_get_value (yp->mount, "bitrate");
    add_yp_info (yp, value, YP_BITRATE);
    free (value);

    value = stats_get_value (yp->mount, "server_description");
    add_yp_info (yp, value, YP_SERVER_DESC);
    free (value);

361
    value = stats_get_value (yp->mount, "subtype");
362
363
364
365
366
367
    add_yp_info (yp, value, YP_SUBTYPE);
    free (value);

    value = stats_get_value (yp->mount, "audio_info");
    add_yp_info (yp, value, YP_AUDIO_INFO);
    free (value);
368
369
370

    ret = snprintf (s, len, "action=add&sn=%s&genre=%s&cpswd=%s&desc="
                    "%s&url=%s&listenurl=%s&type=%s&stype=%s&b=%s&%s\r\n",
371
372
                    yp->server_name, yp->server_genre, yp->cluster_password,
                    yp->server_desc, yp->url, yp->listen_url,
373
                    yp->server_type, yp->subtype, yp->bitrate, yp->audio_info);
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
    if (ret >= (signed)len)
        return ret+1;
    if (send_to_yp ("add", yp, s) == 0)
    {
        yp->process = do_yp_touch;
        /* force first touch in 5 secs */
        yp->next_update = time(NULL) + 5;
    }

    return 0;
}


static unsigned do_yp_touch (ypdata_t *yp, char *s, unsigned len)
{
    unsigned listeners = 0;
    char *val, *artist, *title;
    int ret;
392
    char *max_listeners;
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409

    artist = (char *)stats_get_value (yp->mount, "artist");
    title = (char *)stats_get_value (yp->mount, "title");
    if (artist || title)
    {
         char *song;
         char *separator = " - ";
         if (artist == NULL)
         {
             artist = strdup("");
             separator = "";
         }
         if (title == NULL) title = strdup("");
         song = malloc (strlen (artist) + strlen (title) + strlen (separator) +1);
         if (song)
         {
             sprintf (song, "%s%s%s", artist, separator, title);
410
411
             add_yp_info(yp, song, YP_CURRENT_SONG);
             stats_event (yp->mount, "yp_currently_playing", song);
412
413
414
415
416
417
418
419
420
421
422
423
             free (song);
         }
    }
    free (artist);
    free (title);

    val = (char *)stats_get_value (yp->mount, "listeners");
    if (val)
    {
        listeners = atoi (val);
        free (val);
    }
424
425
426
427
428
429
    max_listeners = stats_get_value (yp->mount, "max_listeners");
    if (max_listeners == NULL || strcmp (max_listeners, "unlimited") == 0)
    {
        free (max_listeners);
        max_listeners = (char *)stats_get_value (NULL, "client_limit");
    }
430
431
432
    val = stats_get_value (yp->mount, "subtype");
    if (val)
    {
433
        add_yp_info (yp, val, YP_SUBTYPE);
434
435
        free (val);
    }
436
437

    ret = snprintf (s, len, "action=touch&sid=%s&st=%s"
438
439
            "&listeners=%u&max_listeners=%s&stype=%s\r\n",
            yp->sid, yp->current_song, listeners, max_listeners, yp->subtype);
440

441
    free (max_listeners);
442
443
444
445
446
    if (ret >= (signed)len)
        return ret+1; /* space required for above text and nul*/

    send_to_yp ("touch", yp, s);
    return 0;
447
}
448

449
450
451


static void process_ypdata (struct yp_server *server, ypdata_t *yp)
452
{
453
454
    unsigned len = 512;
    char *s = NULL, *tmp;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
455

456
457
458
    if (now < yp->next_update)
        return;
    yp->next_update = now + yp->touch_interval;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
459

460
461
462
463
464
465
466
    /* loop just in case the memory area isn't big enough */
    while (1)
    {
        unsigned ret;
        if ((tmp = realloc (s, len)) == NULL)
            return;
        s = tmp;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
467

468
469
470
471
472
473
        if (yp->release)
        {
            yp->process = do_yp_remove;
            yp->next_update = 0;
        }

474
475
476
477
478
        ret = yp->process (yp, s, len);
        if (ret == 0)
        {
           free (s);
           return;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
479
        }
480
        len = ret;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
481
    }
482
}
483
484
485


static void yp_process_server (struct yp_server *server)
486
{
487
488
489
490
491
492
493
494
495
496
497
498
499
500
    ypdata_t *yp;

    /* DEBUG1("processing yp server %s", server->url); */
    yp = server->mounts;
    while (yp)
    {
        now = time (NULL);
        process_ypdata (server, yp);
        yp = yp->next;
    }
}



501
static ypdata_t *create_yp_entry (const char *mount)
502
503
{
    ypdata_t *yp;
504
    char *s;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
505

506
507
508
509
510
511
    yp = calloc (1, sizeof (ypdata_t));
    do
    {
        unsigned len = 512;
        int ret;
        char *url;
512
        mount_proxy *mountproxy = NULL;
513
514
515
516
        ice_config_t *config;

        if (yp == NULL)
            break;
517
        yp->mount = strdup (mount);
518
519
520
521
522
523
524
525
526
        yp->server_name = strdup ("");
        yp->server_desc = strdup ("");
        yp->server_genre = strdup ("");
        yp->bitrate = strdup ("");
        yp->server_type = strdup ("");
        yp->cluster_password = strdup ("");
        yp->url = strdup ("");
        yp->current_song = strdup ("");
        yp->audio_info = strdup ("");
527
        yp->subtype = strdup ("");
528
529
530
531
532
533
        yp->process = do_yp_add;

        url = malloc (len);
        if (url == NULL)
            break;
        config = config_get_config();
534
        ret = snprintf (url, len, "http://%s:%d%s", config->hostname, config->port, mount);
535
        if (ret >= (signed)len)
536
        {
537
538
            s = realloc (url, ++ret);
            if (s) url = s;
539
            snprintf (url, ret, "http://%s:%d%s", config->hostname, config->port, mount);
540
        }
541

542
        mountproxy = config_find_mount (config, mount);
543
        if (mountproxy && mountproxy->cluster_password)
544
            add_yp_info (yp, mountproxy->cluster_password, YP_CLUSTER_PASSWORD);
545
        config_release_config();
546

547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
        yp->listen_url = util_url_escape (url);
        free (url);
        if (yp->listen_url == NULL)
            break;

        return yp;
    } while (0);

    yp_destroy_ypdata (yp);
    return NULL;
}


/* Check for changes in the YP servers configured */
static void check_servers ()
{
563
564
    struct yp_server *server = (struct yp_server *)active_yps,
                     **server_p = (struct yp_server **)&active_yps;
565
566
567
568
569
570
571
572
573
574

    while (server)
    {
        if (server->remove)
        {
            struct yp_server *to_go = server;
            DEBUG1 ("YP server \"%s\"removed", server->url);
            *server_p = server->next;
            server = server->next;
            destroy_yp_server (to_go);
575
            continue;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
576
        }
577
578
579
580
581
582
583
584
        server_p = &server->next;
        server = server->next;
    }
    /* add new server entries */
    while (pending_yps)
    {
        avl_node *node;

585
        server = (struct yp_server *)pending_yps;
586
587
588
        pending_yps = server->next;

        DEBUG1("Add pending yps %s", server->url);
589
        server->next = (struct yp_server *)active_yps;
590
591
592
593
594
595
596
597
598
599
        active_yps = server;

        /* new YP server configured, need to populate with existing sources */
        avl_tree_rlock (global.source_tree);
        node = avl_get_first (global.source_tree);
        while (node)
        {
            ypdata_t *yp;

            source_t *source = node->key;
600
            if ((yp = create_yp_entry (source->mount)) != NULL)
601
602
603
604
605
606
            {
                DEBUG1 ("Adding existing mount %s", source->mount);
                yp->server = server;
                yp->touch_interval = server->touch_interval;
                yp->next = server->mounts;
                server->mounts = yp;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
607
            }
608
            node = avl_get_next (node);
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
609
        }
610
        avl_tree_unlock (global.source_tree);
611
    }
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
}


static void add_pending_yp (struct yp_server *server)
{
    ypdata_t *current, *yp;
    unsigned count = 0;

    if (server->pending_mounts == NULL)
        return;
    current = server->mounts;
    server->mounts = server->pending_mounts;
    server->pending_mounts = NULL;
    yp = server->mounts;
    while (1)
    {
        count++;
        if (yp->next == NULL)
            break;
        yp = yp->next;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
632
    }
633
634
635
636
    yp->next = current;
    DEBUG2 ("%u YP entries added to %s", count, server->url);
}

637

638
639
640
static void delete_marked_yp (struct yp_server *server)
{
    ypdata_t *yp = server->mounts, **prev = &server->mounts;
641

642
643
644
645
646
647
648
649
650
651
652
653
654
655
    while (yp)
    {
        if (yp->remove)
        {
            ypdata_t *to_go = yp;
            DEBUG2 ("removed %s from YP server %s", yp->mount, server->url);
            *prev = yp->next;
            yp = yp->next;
            yp_destroy_ypdata (to_go);
            continue;
        }
        prev = &yp->next;
        yp = yp->next;
    }
656
}
657
658
659


static void *yp_update_thread(void *arg)
660
{
661
662
663
664
665
666
667
668
669
670
671
    INFO0("YP update thread started");

    yp_running = 1;
    while (yp_running)
    {
        struct yp_server *server;

        thread_sleep (200000);

        /* do the YP communication */
        thread_rwlock_rlock (&yp_lock);
672
        server = (struct yp_server *)active_yps;
673
674
675
676
677
        while (server)
        {
            /* DEBUG1 ("trying %s", server->url); */
            yp_process_server (server);
            server = server->next;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
678
        }
679
680
681
682
683
684
685
        thread_rwlock_unlock (&yp_lock);

        /* update the local YP structure */
        if (yp_update)
        {
            thread_rwlock_wlock (&yp_lock);
            check_servers ();
686
            server = (struct yp_server *)active_yps;
687
688
689
690
691
692
            while (server)
            {
                /* DEBUG1 ("Checking yps %s", server->url); */
                add_pending_yp (server);
                delete_marked_yp (server);
                server = server->next;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
693
            }
694
695
            yp_update = 0;
            thread_rwlock_unlock (&yp_lock);
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
696
697
        }
    }
698
699
700
701
702
    thread_rwlock_destroy (&yp_lock);
    thread_mutex_destroy (&yp_pending_lock);
    /* free server and ypdata left */
    while (active_yps)
    {
703
        struct yp_server *server = (struct yp_server *)active_yps;
704
705
706
        active_yps = server->next;
        destroy_yp_server (server);
    }
707

708
    return NULL;
709
}
710

711
712
713


static void yp_destroy_ypdata(ypdata_t *ypdata)
714
{
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
715
    if (ypdata) {
716
717
718
719
720
        if (ypdata->mount) {
            free (ypdata->mount);
        }
        if (ypdata->url) {
            free (ypdata->url);
721
        }
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
        if (ypdata->sid) {
            free(ypdata->sid);
        }
        if (ypdata->server_name) {
            free(ypdata->server_name);
        }
        if (ypdata->server_desc) {
            free(ypdata->server_desc);
        }
        if (ypdata->server_genre) {
            free(ypdata->server_genre);
        }
        if (ypdata->cluster_password) {
            free(ypdata->cluster_password);
        }
        if (ypdata->listen_url) {
            free(ypdata->listen_url);
        }
        if (ypdata->current_song) {
            free(ypdata->current_song);
        }
        if (ypdata->bitrate) {
            free(ypdata->bitrate);
        }
        if (ypdata->server_type) {
            free(ypdata->server_type);
        }
749
750
751
        if (ypdata->audio_info) {
            free(ypdata->audio_info);
        }
752
        free (ypdata->subtype);
753
754
        free (ypdata->error_msg);
        free (ypdata);
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
755
    }
756
}
757

758
static void add_yp_info (ypdata_t *yp, void *info, int type)
759
760
{
    char *escaped;
761
762

    if (!info)
763
        return;
764
765
766

    switch (type)
    {
767
        case YP_SERVER_NAME:
768
769
770
771
772
773
774
775
            escaped = util_url_escape(info);
            if (escaped)
            {
                if (yp->server_name)
                    free (yp->server_name);
                yp->server_name = escaped;
            }
            break;
776
        case YP_SERVER_DESC:
777
778
779
780
781
782
783
784
            escaped = util_url_escape(info);
            if (escaped)
            {
                if (yp->server_desc)
                    free (yp->server_desc);
                yp->server_desc = escaped;
            }
            break;
785
        case YP_SERVER_GENRE:
786
787
788
789
790
791
792
793
            escaped = util_url_escape(info);
            if (escaped)
            {
                if (yp->server_genre)
                    free (yp->server_genre);
                yp->server_genre = escaped;
            }
            break;
794
        case YP_SERVER_URL:
795
796
797
798
799
800
801
802
            escaped = util_url_escape(info);
            if (escaped)
            {
                if (yp->url)
                    free (yp->url);
                yp->url = escaped;
            }
            break;
803
        case YP_BITRATE:
804
805
806
807
808
809
810
811
            escaped = util_url_escape(info);
            if (escaped)
            {
                if (yp->bitrate)
                    free (yp->bitrate);
                yp->bitrate = escaped;
            }
            break;
812
        case YP_AUDIO_INFO:
813
814
815
            escaped = util_url_escape(info);
            if (escaped)
            {
816
                free (yp->audio_info);
817
818
                yp->audio_info = escaped;
            }
819
            break;
820
        case YP_SERVER_TYPE:
821
822
823
824
825
826
827
828
            escaped = util_url_escape(info);
            if (escaped)
            {
                if (yp->server_type)
                    free (yp->server_type);
                yp->server_type = escaped;
            }
            break;
829
        case YP_CURRENT_SONG:
830
831
832
833
834
835
836
837
            escaped = util_url_escape(info);
            if (escaped)
            {
                if (yp->current_song)
                    free (yp->current_song);
                yp->current_song = escaped;
            }
            break;
838
839
840
841
842
843
844
845
846
        case YP_CLUSTER_PASSWORD:
            escaped = util_url_escape(info);
            if (escaped)
            {
                if (yp->cluster_password)
                    free (yp->cluster_password);
                yp->cluster_password = escaped;
            }
            break;
847
848
849
850
851
852
853
854
        case YP_SUBTYPE:
            escaped = util_url_escape(info);
            if (escaped)
            {
                free (yp->subtype);
                yp->subtype = escaped;
            }
            break;
855
856
857
858
859
    }
}


/* Add YP entries to active servers */
860
void yp_add (const char *mount)
861
862
863
864
865
866
867
868
{
    struct yp_server *server;

    /* make sure YP thread is not modifying the lists */
    thread_rwlock_rlock (&yp_lock);

    /* make sure we don't race against another yp_add */
    thread_mutex_lock (&yp_pending_lock);
869
    server = (struct yp_server *)active_yps;
870
871
872
873
    while (server)
    {
        ypdata_t *yp;
        /* add new ypdata to each servers pending yp */
874
        if ((yp = create_yp_entry (mount)) != NULL)
875
        {
876
            DEBUG2 ("Adding %s to %s", mount, server->url);
877
878
879
            yp->server = server;
            yp->touch_interval = server->touch_interval;
            yp->next = server->pending_mounts;
880
            yp->next_update = time(NULL) + 5;
881
882
883
884
885
886
887
888
889
890
891
892
893
894
            server->pending_mounts = yp;
            yp_update = 1;
        }
        server = server->next;
    }
    thread_mutex_unlock (&yp_pending_lock);
    thread_rwlock_unlock (&yp_lock);
}



/* Mark an existing entry in the YP list as to be marked for deletion */
void yp_remove (const char *mount)
{
895
    struct yp_server *server = (struct yp_server *)active_yps;
896
897
898
899

    thread_rwlock_rlock (&yp_lock);
    while (server)
    {
900
        ypdata_t *yp = find_yp_mount (server->mounts, mount);
901
902
        if (yp)
        {
903
904
            DEBUG2 ("release %s on YP %s", mount, server->url);
            yp->release = 1;
905
            yp->next_update = 0;
906
        }
907
        server = server->next;
908
    }
909
    thread_rwlock_unlock (&yp_lock);
910
}
911
912
913
914
915
916


/* This is similar to yp_remove, but we force a touch
 * attempt */
void yp_touch (const char *mount)
{
917
    struct yp_server *server = (struct yp_server *)active_yps;
918
    time_t trigger;
919
    ypdata_t *search_list = NULL;
920
921
922
923

    thread_rwlock_rlock (&yp_lock);
    /* do update in 3 secs, give stats chance to update */
    trigger = time(NULL) + 3;
924
925
926
    if (server)
        search_list = server->mounts;

927
928
    while (server)
    {
929
        ypdata_t *yp = find_yp_mount (search_list, mount);
930
931
        if (yp)
        {
932
            /* we may of found old entries not purged yet, so skip them */
933
            if (yp->release != 0 || yp->remove != 0)
934
935
936
937
            {
                search_list = yp->next;
                continue;
            }
938
939
940
941
942
            /* only force if touch */
            if (yp->process == do_yp_touch)
                yp->next_update = trigger;
        }
        server = server->next;
943
944
        if (server)
            search_list = server->mounts;
945
946
947
948
949
950
951
952
953
    }
    thread_rwlock_unlock (&yp_lock);
}


void yp_shutdown ()
{
    yp_running = 0;
    yp_update = 1;
954
955
    if (yp_thread)
        thread_join (yp_thread);
956
    curl_global_cleanup();
957
    INFO0 ("YP thread down");
958
959
}