yp.c 11.8 KB
Newer Older
1 2
#include <stdio.h>
#include <string.h>
3
#include <stdlib.h>
4 5 6 7 8 9 10 11 12 13 14 15

#include <thread/thread.h>

#include "connection.h"
#include "refbuf.h"
#include "client.h"
#include "logging.h"
#include "format.h"
#include "geturl.h"
#include "source.h"
#include "config.h"

Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
16
#define CATMODULE "yp" 
17

Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
18
int yp_submit_url(int curl_con, char *yp_url, char *url, char *type)
19
{
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
20 21 22 23
    int ret = 0;

    curl_easy_setopt(curl_get_handle(curl_con), CURLOPT_URL, yp_url);
    curl_easy_setopt(curl_get_handle(curl_con), CURLOPT_POSTFIELDS, url);
24 25
    curl_easy_setopt(curl_get_handle(curl_con), CURLOPT_TIMEOUT, 
            config_get_config()->yp_url_timeout);
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
26 27

    /* get it! */
28 29 30
    memset(curl_get_result(curl_con), 0, sizeof(struct curl_memory_struct));
    memset(curl_get_header_result(curl_con), 0, 
            sizeof(struct curl_memory_struct2));
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
31 32 33 34 35 36 37 38 39 40 41

    curl_easy_perform(curl_get_handle(curl_con));

    curl_print_header_result(curl_get_header_result(curl_con));

    if (curl_get_header_result(curl_con)->response == ACK) {
        INFO2("Successfull ACK from %s (%s)", type, yp_url);
        ret = 1;
    }
    else {
        if (strlen(curl_get_header_result(curl_con)->message) > 0) {
42 43
            ERROR3("Got a NAK from %s(%s) (%s)", type,
                    curl_get_header_result(curl_con)->message, yp_url);
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
44 45 46 47 48 49 50
        }
        else {
            ERROR2("Got a NAK from %s(Unknown) (%s)", type, yp_url);
        }
        ret = 0;
    }
    return ret;
51 52 53 54
}

void *yp_touch_thread(void *arg)
{
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
55 56 57
    yp_touch((source_t *)arg);
    thread_exit(0);
    return NULL;
58
}
59 60

int yp_remove(source_t *source)
61
{
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    char *url = NULL;
    int url_size = 0;
    int ret = 0;
    int curl_con = 0;
    int i = 0;

    long current_time = 0;

    current_time = time(&current_time);

    for (i=0; i<source->num_yp_directories; i++) {
        source->ypdata[i]->yp_last_touch = current_time;
        if (source->ypdata[i]->sid == 0) {
            return 0;
        }
        else {
            if (strlen(source->ypdata[i]->sid) == 0) {
                return 0;
            }
        }
        if (source->ypdata) {
            url_size = strlen("action=remove&sid=") + 1;
            url_size += strlen(source->ypdata[i]->sid);
            url_size += 1024;
86
            url = malloc(url_size);
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
87
            sprintf(url, "action=remove&sid=%s", 
88
                    source->ypdata[i]->sid);
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
89 90 91 92 93
            curl_con = curl_get_connection();
            if (curl_con < 0) {
                ERROR0("Unable to get auth connection");
            }
            else {
94 95 96
                /* specify URL to get */
                ret = yp_submit_url(curl_con, source->ypdata[i]->yp_url, 
                        url, "yp_remove");
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
97 98 99 100 101 102 103 104
            }
            if (url) {
                free(url);
            }
            curl_release_connection(curl_con);
        }
    }
    return 1;
105
}
106
int yp_touch(source_t *source)
107
{
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
108 109 110 111 112 113 114 115
    char *url = NULL;
    int url_size = 0;
    int ret = 0;
    int curl_con = 0;
    int i = 0;
    int regen_sid = 0;
    long current_time = 0;

116
    current_time = time(NULL);
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    for (i=0; i<source->num_yp_directories; i++) {
        source->ypdata[i]->yp_last_touch = current_time;
        if (source->ypdata[i]->sid == 0) {
            regen_sid = 1;
        }
        else {
            if (strlen(source->ypdata[i]->sid) == 0) {
                regen_sid = 1;
            }
        }
        if (regen_sid) {
            if (!yp_add(source, i)) {
                return 0;
            }
        }
        if (source->ypdata) {
            url_size = strlen("action=touch&sid=&st=&listeners=") + 1;
            if (source->ypdata[i]->current_song) {
                url_size += strlen(source->ypdata[i]->current_song);
            }
            else {
                source->ypdata[i]->current_song = (char *)malloc(1);
139
                source->ypdata[i]->current_song[0] = 0;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
140 141 142 143 144 145
            }
            if (source->ypdata[i]->sid) {
                url_size += strlen(source->ypdata[i]->sid);
            }
            else {
                source->ypdata[i]->sid = (char *)malloc(1);
146
                source->ypdata[i]->sid[0] = 0;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
147 148
            }
            url_size += 1024;
149 150 151 152 153
            url = malloc(url_size);
            sprintf(url, "action=touch&sid=%s&st=%s&listeners=%ld", 
                    source->ypdata[i]->sid,
                    source->ypdata[i]->current_song,
                    source->listeners);
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
154 155 156 157 158 159

            curl_con = curl_get_connection();
            if (curl_con < 0) {
                ERROR0("Unable to get auth connection");
            }
            else {
160 161 162
                /* specify URL to get */
                ret = yp_submit_url(curl_con, source->ypdata[i]->yp_url, 
                        url, "yp_touch");
163
                if (!ret) {
164
                    source->ypdata[i]->sid[0] = 0;
165
                }
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
166 167 168 169 170 171 172 173
            }
           if (url) {
               free(url);
           } 
           curl_release_connection(curl_con);
        }
    }
    return 1;
174
}
175
int yp_add(source_t *source, int which)
176
{
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    char *url = NULL;
    int url_size = 0;
    int ret = 0;
    int curl_con = 0;
    int i = 0;
    int ok = 0;

    for (i=0; i<source->num_yp_directories; i++) {
        if (which != -1) {
            if (i == which) {
                ok = 1;
            }
            else {
                ok = 0;
            }
        }
        else {
            ok = 1;
        }

        if (ok) {
            if (source->ypdata[i]) {
199 200 201
                url_size = strlen("action=add&sn=&genre=&cpswd="
                                  "&desc=&url=&listenurl=&type=&b=&")
                                  + 1;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
202 203 204 205 206
                if (source->ypdata[i]->server_name) {
                    url_size += strlen(source->ypdata[i]->server_name);
                }
                else {
                    source->ypdata[i]->server_name = (char *)malloc(1);
207
                    source->ypdata[i]->server_name[0] = 0;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
208 209 210 211 212 213
                }
                if (source->ypdata[i]->server_desc) {
                    url_size += strlen(source->ypdata[i]->server_desc);
                }
                else {
                    source->ypdata[i]->server_desc = (char *)malloc(1);
214
                    source->ypdata[i]->server_desc[0] = 0;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
215 216 217 218 219 220
                }
                if (source->ypdata[i]->server_genre) {
                    url_size += strlen(source->ypdata[i]->server_genre);
                }
                else {
                    source->ypdata[i]->server_genre = (char *)malloc(1);
221
                    source->ypdata[i]->server_genre[0] = 0;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
222 223 224 225 226 227
                }
                if (source->ypdata[i]->cluster_password) {
                    url_size += strlen(source->ypdata[i]->cluster_password);
                }
                else {
                    source->ypdata[i]->cluster_password = (char *)malloc(1);
228
                    source->ypdata[i]->cluster_password[0] = 0;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
229 230 231 232 233 234
                }
                if (source->ypdata[i]->server_url) {
                    url_size += strlen(source->ypdata[i]->server_url);
                }
                else {
                    source->ypdata[i]->server_url = (char *)malloc(1);
235
                    source->ypdata[i]->server_url[0] = 0;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
236 237 238 239 240 241
                }
                if (source->ypdata[i]->listen_url) {
                    url_size += strlen(source->ypdata[i]->listen_url);
                }
                else {
                    source->ypdata[i]->listen_url = (char *)malloc(1);
242
                    source->ypdata[i]->listen_url[0] = 0;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
243 244 245 246 247 248
                }
                if (source->ypdata[i]->server_type) {
                    url_size += strlen(source->ypdata[i]->server_type);
                }
                else {
                    source->ypdata[i]->server_type = (char *)malloc(1);
249
                    source->ypdata[i]->server_type[0] = 0;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
250 251 252 253 254 255
                }
                if (source->ypdata[i]->bitrate) {
                    url_size += strlen(source->ypdata[i]->bitrate);
                }
                else {
                    source->ypdata[i]->bitrate = (char *)malloc(1);
256
                    source->ypdata[i]->bitrate[0] = 0;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
257 258 259 260 261 262
                }
                if (source->ypdata[i]->current_song) {
                    url_size += strlen(source->ypdata[i]->current_song);
                }
                else {
                    source->ypdata[i]->current_song = (char *)malloc(1);
263
                    source->ypdata[i]->current_song[0] = 0;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
264
                }
265 266 267 268 269 270 271 272
                if (source->ypdata[i]->audio_info) {
                    url_size += strlen(source->ypdata[i]->audio_info);
                }
                else {
                    source->ypdata[i]->audio_info = (char *)malloc(1);
                    source->ypdata[i]->audio_info[0] = 0;
                }

Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
273
                url_size += 1024;
274
                url = malloc(url_size);
275 276
                sprintf(url, "action=add&sn=%s&genre=%s&cpswd=%s&desc="
                             "%s&url=%s&listenurl=%s&type=%s&b=%s&%s", 
277 278 279 280 281 282 283
                        source->ypdata[i]->server_name,
                        source->ypdata[i]->server_genre,
                        source->ypdata[i]->cluster_password,
                        source->ypdata[i]->server_desc,
                        source->ypdata[i]->server_url,
                        source->ypdata[i]->listen_url,
                        source->ypdata[i]->server_type,
284 285
                        source->ypdata[i]->bitrate,
                        source->ypdata[i]->audio_info);
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
286 287 288 289 290 291 292

               curl_con = curl_get_connection();
               if (curl_con < 0) {
                   ERROR0("Unable to get auth connection");
               }
               else {
                   /* specify URL to get */
293 294
                   ret = yp_submit_url(curl_con, source->ypdata[i]->yp_url, 
                           url, "yp_add");
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
295 296 297 298 299 300 301 302

                   if (ret) {
                       if (strlen(curl_get_header_result(curl_con)->sid) > 0) {
                           if (source->ypdata) {
                               if (source->ypdata[i]->sid) {
                                   free(source->ypdata[i]->sid);
                                   source->ypdata[i]->sid = NULL;
                               }
303 304 305 306 307 308 309 310
                               source->ypdata[i]->sid = (char *)malloc(
                                       strlen(curl_get_header_result(curl_con)->
                                           sid) +1);
                               strcpy(source->ypdata[i]->sid, 
                                       curl_get_header_result(curl_con)->sid);
                               source->ypdata[i]->yp_touch_interval = 
                                   curl_get_header_result(
                                           curl_con)->touch_interval;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
311 312 313 314 315 316 317 318 319 320 321 322
                           }
                       }
                   }
               }
               if (url) {
                   free(url);
               }
               curl_release_connection(curl_con);
            }
        }
    }
    return 1;
323 324
}

Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
325
ypdata_t *yp_create_ypdata()
326
{
327
    return calloc(1, sizeof(ypdata_t));
328
}
329

Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
330
void yp_destroy_ypdata(ypdata_t *ypdata)
331
{
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    if (ypdata) {
        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->server_url) {
            free(ypdata->server_url);
        }
        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);
        }
363 364 365
        if (ypdata->audio_info) {
            free(ypdata->audio_info);
        }
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
366 367
        free(ypdata);
    }
368
}
369