1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 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 157 158 159 160 161 162 163 164 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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 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 749 750 751 752 753
|
/*
* MOC - music on console
* Copyright (C) 2003 - 2005 Damian Pietras <daper@daper.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include "common.h"
#include "log.h"
#include "protocol.h"
#include "playlist.h"
#include "files.h"
/* Maximal socket name. */
#define UNIX_PATH_MAX 108
#define SOCKET_NAME "socket2"
#define nonblocking(fn, result, sock, buf, len) \
do { \
long flags = fcntl (sock, F_GETFL); \
if (flags == -1) \
fatal ("Getting flags for socket failed: %s", \
xstrerror (errno)); \
flags |= O_NONBLOCK; \
if (fcntl (sock, F_SETFL, O_NONBLOCK) == -1) \
fatal ("Setting O_NONBLOCK for the socket failed: %s", \
xstrerror (errno)); \
result = fn (sock, buf, len, 0); \
flags &= ~O_NONBLOCK; \
if (fcntl (sock, F_SETFL, flags) == -1) \
fatal ("Restoring flags for socket failed: %s", \
xstrerror (errno)); \
} while (0)
/* Buffer used to send data in one bigger chunk instead of sending sigle
* integer, string etc. values. */
struct packet_buf
{
char *buf;
size_t allocated;
size_t len;
};
/* Create a socket name, return NULL if the name could not be created. */
char *socket_name ()
{
char *socket_name = create_file_name (SOCKET_NAME);
if (strlen(socket_name) > UNIX_PATH_MAX)
fatal ("Can't create socket name!");
return socket_name;
}
/* Get an integer value from the socket, return == 0 on error. */
int get_int (int sock, int *i)
{
ssize_t res;
res = recv (sock, i, sizeof(int), 0);
if (res == -1)
log_errno ("recv() failed when getting int", errno);
return res == ssizeof(int) ? 1 : 0;
}
/* Get an integer value from the socket without blocking. */
enum noblock_io_status get_int_noblock (int sock, int *i)
{
ssize_t res;
char *err;
nonblocking (recv, res, sock, i, sizeof (int));
if (res == ssizeof (int))
return NB_IO_OK;
if (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
return NB_IO_BLOCK;
err = xstrerror (errno);
logit ("recv() failed when getting int (res %zd): %s", res, err);
free (err);
return NB_IO_ERR;
}
/* Send an integer value to the socket, return == 0 on error */
int send_int (int sock, int i)
{
ssize_t res;
res = send (sock, &i, sizeof(int), 0);
if (res == -1)
log_errno ("send() failed", errno);
return res == ssizeof(int) ? 1 : 0;
}
#if 0
/* Get a long value from the socket, return == 0 on error. */
static int get_long (int sock, long *i)
{
ssize_t res;
res = recv (sock, i, sizeof(long), 0);
if (res == -1)
log_errno ("recv() failed when getting int", errno);
return res == ssizeof(long) ? 1 : 0;
}
#endif
#if 0
/* Send a long value to the socket, return == 0 on error */
static int send_long (int sock, long i)
{
ssize_t res;
res = send (sock, &i, sizeof(long), 0);
if (res == -1)
log_errno ("send() failed", errno);
return res == ssizeof(long) ? 1 : 0;
}
#endif
/* Get the string from socket, return NULL on error. The memory is malloced. */
char *get_str (int sock)
{
int len, nread = 0;
char *str;
if (!get_int(sock, &len))
return NULL;
if (!RANGE(0, len, MAX_SEND_STRING)) {
logit ("Bad string length.");
return NULL;
}
str = (char *)xmalloc (sizeof(char) * (len + 1));
while (nread < len) {
ssize_t res;
res = recv (sock, str + nread, len - nread, 0);
if (res == -1) {
log_errno ("recv() failed when getting string", errno);
free (str);
return NULL;
}
if (res == 0) {
logit ("Unexpected EOF when getting string");
free (str);
return NULL;
}
nread += res;
}
str[len] = 0;
return str;
}
int send_str (int sock, const char *str)
{
int len;
len = strlen (str);
if (!send_int (sock, len))
return 0;
if (send (sock, str, len, 0) != len)
return 0;
return 1;
}
/* Get a time_t value from the socket, return == 0 on error. */
int get_time (int sock, time_t *i)
{
ssize_t res;
res = recv (sock, i, sizeof(time_t), 0);
if (res == -1)
log_errno ("recv() failed when getting time_t", errno);
return res == ssizeof(time_t) ? 1 : 0;
}
/* Send a time_t value to the socket, return == 0 on error */
int send_time (int sock, time_t i)
{
ssize_t res;
res = send (sock, &i, sizeof(time_t), 0);
if (res == -1)
log_errno ("send() failed", errno);
return res == ssizeof(time_t) ? 1 : 0;
}
static struct packet_buf *packet_buf_new ()
{
struct packet_buf *b;
b = (struct packet_buf *)xmalloc (sizeof(struct packet_buf));
b->buf = (char *)xmalloc (1024);
b->allocated = 1024;
b->len = 0;
return b;
}
static void packet_buf_free (struct packet_buf *b)
{
assert (b != NULL);
free (b->buf);
free (b);
}
/* Make sure that there is at least len bytes free. */
static void packet_buf_add_space (struct packet_buf *b, const size_t len)
{
assert (b != NULL);
if (b->allocated < b->len + len) {
b->allocated += len + 256; /* put some more space */
b->buf = (char *)xrealloc (b->buf, b->allocated);
}
}
/* Add an integer value to the buffer */
static void packet_buf_add_int (struct packet_buf *b, const int n)
{
assert (b != NULL);
packet_buf_add_space (b, sizeof(n));
memcpy (b->buf + b->len, &n, sizeof(n));
b->len += sizeof(n);
}
/* Add a string value to the buffer. */
static void packet_buf_add_str (struct packet_buf *b, const char *str)
{
int str_len;
assert (b != NULL);
assert (str != NULL);
str_len = strlen (str);
packet_buf_add_int (b, str_len);
packet_buf_add_space (b, str_len * sizeof(char));
memcpy (b->buf + b->len, str, str_len * sizeof(char));
b->len += str_len * sizeof(char);
}
/* Add a time_t value to the buffer. */
static void packet_buf_add_time (struct packet_buf *b, const time_t n)
{
assert (b != NULL);
packet_buf_add_space (b, sizeof(n));
memcpy (b->buf + b->len, &n, sizeof(n));
b->len += sizeof(n);
}
/* Add tags to the buffer. If tags == NULL, add empty tags. */
void packet_buf_add_tags (struct packet_buf *b, const struct file_tags *tags)
{
assert (b != NULL);
if (tags) {
packet_buf_add_str (b, tags->title ? tags->title : "");
packet_buf_add_str (b, tags->artist ? tags->artist : "");
packet_buf_add_str (b, tags->album ? tags->album : "");
packet_buf_add_int (b, tags->track);
packet_buf_add_int (b, tags->filled & TAGS_TIME ? tags->time : -1);
packet_buf_add_int (b, tags->filled);
}
else {
/* empty tags: */
packet_buf_add_str (b, ""); /* title */
packet_buf_add_str (b, ""); /* artist */
packet_buf_add_str (b, ""); /* album */
packet_buf_add_int (b, -1); /* track */
packet_buf_add_int (b, -1); /* time */
packet_buf_add_int (b, 0); /* filled */
}
}
/* Add an item to the buffer. */
void packet_buf_add_item (struct packet_buf *b, const struct plist_item *item)
{
packet_buf_add_str (b, item->file);
packet_buf_add_str (b, item->title_tags ? item->title_tags : "");
packet_buf_add_tags (b, item->tags);
packet_buf_add_time (b, item->mtime);
}
/* Send data to the socket. Return 0 on error. */
static int send_all (int sock, const char *buf, const size_t size)
{
ssize_t sent;
size_t send_pos = 0;
while (send_pos < size) {
sent = send (sock, buf + send_pos, size - send_pos, 0);
if (sent < 0) {
log_errno ("Error while sending data", errno);
return 0;
}
send_pos += sent;
}
return 1;
}
/* Send a playlist item to the socket. If item == NULL, send empty item mark
* (end of playlist). Return 0 on error. */
int send_item (int sock, const struct plist_item *item)
{
int res = 1;
struct packet_buf *b;
if (!item) {
if (!send_str(sock, "")) {
logit ("Error while sending empty item");
return 0;
}
return 1;
}
b = packet_buf_new ();
packet_buf_add_item (b, item);
if (!send_all(sock, b->buf, b->len)) {
logit ("Error when sending item");
res = 0;
}
packet_buf_free (b);
return res;
}
struct file_tags *recv_tags (int sock)
{
struct file_tags *tags = tags_new ();
if (!(tags->title = get_str(sock))) {
logit ("Error while receiving title");
tags_free (tags);
return NULL;
}
if (!(tags->artist = get_str(sock))) {
logit ("Error while receiving artist");
tags_free (tags);
return NULL;
}
if (!(tags->album = get_str(sock))) {
logit ("Error while receiving album");
tags_free (tags);
return NULL;
}
if (!get_int(sock, &tags->track)) {
logit ("Error while receiving track");
tags_free (tags);
return NULL;
}
if (!get_int(sock, &tags->time)) {
logit ("Error while receiving time");
tags_free (tags);
return NULL;
}
if (!get_int(sock, &tags->filled)) {
logit ("Error while receiving 'filled'");
tags_free (tags);
return NULL;
}
/* Set NULL instead of empty tags. */
if (!tags->title[0]) {
free (tags->title);
tags->title = NULL;
}
if (!tags->artist[0]) {
free (tags->artist);
tags->artist = NULL;
}
if (!tags->album[0]) {
free (tags->album);
tags->album = NULL;
}
return tags;
}
/* Send tags. If tags == NULL, send empty tags. Return 0 on error. */
int send_tags (int sock, const struct file_tags *tags)
{
int res = 1;
struct packet_buf *b;
b = packet_buf_new ();
packet_buf_add_tags (b, tags);
if (!send_all(sock, b->buf, b->len))
res = 0;
packet_buf_free (b);
return res;
}
/* Get a playlist item from the server.
* The end of the playlist is indicated by item->file being an empty string.
* The memory is malloc()ed. Returns NULL on error. */
struct plist_item *recv_item (int sock)
{
struct plist_item *item = plist_new_item ();
/* get the file name */
if (!(item->file = get_str(sock))) {
logit ("Error while receiving file name");
free (item);
return NULL;
}
if (item->file[0]) {
if (!(item->title_tags = get_str(sock))) {
logit ("Error while receiving tags title");
free (item->file);
free (item);
return NULL;
}
item->type = file_type (item->file);
if (!item->title_tags[0]) {
free (item->title_tags);
item->title_tags = NULL;
}
if (!(item->tags = recv_tags(sock))) {
logit ("Error while receiving tags");
free (item->file);
if (item->title_tags)
free (item->title_tags);
free (item);
return NULL;
}
if (!get_time(sock, &item->mtime)) {
logit ("Error while receiving mtime");
if (item->title_tags)
free (item->title_tags);
free (item->file);
tags_free (item->tags);
free (item);
return NULL;
}
}
return item;
}
struct move_ev_data *recv_move_ev_data (int sock)
{
struct move_ev_data *d;
d = (struct move_ev_data *)xmalloc (sizeof(struct move_ev_data));
if (!(d->from = get_str(sock))) {
logit ("Error while receiving 'from' data");
free (d);
return NULL;
}
if (!(d->to = get_str(sock))) {
logit ("Error while receiving 'to' data");
free (d->from);
free (d);
return NULL;
}
return d;
}
/* Push an event on the queue if it's not already there. */
void event_push (struct event_queue *q, const int event, void *data)
{
assert (q != NULL);
if (!q->head) {
q->head = (struct event *)xmalloc (sizeof(struct event));
q->head->next = NULL;
q->head->type = event;
q->head->data = data;
q->tail = q->head;
}
else {
assert (q->head != NULL);
assert (q->tail != NULL);
assert (q->tail->next == NULL);
q->tail->next = (struct event *)xmalloc (
sizeof(struct event));
q->tail = q->tail->next;
q->tail->next = NULL;
q->tail->type = event;
q->tail->data = data;
}
}
/* Remove the first event from the queue (don't free the data field). */
void event_pop (struct event_queue *q)
{
struct event *e;
assert (q != NULL);
assert (q->head != NULL);
assert (q->tail != NULL);
e = q->head;
q->head = e->next;
free (e);
if (q->tail == e)
q->tail = NULL; /* the queue is empty */
}
/* Get the pointer to the first item in the queue or NULL if the queue is
* empty. */
struct event *event_get_first (struct event_queue *q)
{
assert (q != NULL);
return q->head;
}
void free_tag_ev_data (struct tag_ev_response *d)
{
assert (d != NULL);
free (d->file);
tags_free (d->tags);
free (d);
}
void free_move_ev_data (struct move_ev_data *m)
{
assert (m != NULL);
assert (m->from != NULL);
assert (m->to != NULL);
free (m->to);
free (m->from);
free (m);
}
struct move_ev_data *move_ev_data_dup (const struct move_ev_data *m)
{
struct move_ev_data *new;
assert (m != NULL);
assert (m->from != NULL);
assert (m->to != NULL);
new = (struct move_ev_data *)xmalloc (sizeof(struct move_ev_data));
new->from = xstrdup (m->from);
new->to = xstrdup (m->to);
return new;
}
/* Free data associated with the event if any. */
void free_event_data (const int type, void *data)
{
if (type == EV_PLIST_ADD || type == EV_QUEUE_ADD) {
plist_free_item_fields ((struct plist_item *)data);
free (data);
}
else if (type == EV_FILE_TAGS)
free_tag_ev_data ((struct tag_ev_response *)data);
else if (type == EV_PLIST_DEL || type == EV_STATUS_MSG
|| type == EV_SRV_ERROR || type == EV_QUEUE_DEL)
free (data);
else if (type == EV_PLIST_MOVE || type == EV_QUEUE_MOVE)
free_move_ev_data ((struct move_ev_data *)data);
else if (data)
abort (); /* BUG */
}
/* Free event queue content without the queue structure. */
void event_queue_free (struct event_queue *q)
{
struct event *e;
assert (q != NULL);
while ((e = event_get_first(q))) {
free_event_data (e->type, e->data);
event_pop (q);
}
}
void event_queue_init (struct event_queue *q)
{
assert (q != NULL);
q->head = NULL;
q->tail = NULL;
}
#if 0
/* Search for an event of this type and return pointer to it or NULL if there
* was no such event. */
struct event *event_search (struct event_queue *q, const int event)
{
struct event *e;
assert (q != NULL);
while ((e = q->head)) {
if (e->type == event)
return e;
e = e->next;
}
return NULL;
}
#endif
/* Return != 0 if the queue is empty. */
int event_queue_empty (const struct event_queue *q)
{
assert (q != NULL);
return q->head == NULL ? 1 : 0;
}
/* Make a packet buffer filled with the event (with data). */
static struct packet_buf *make_event_packet (const struct event *e)
{
struct packet_buf *b;
assert (e != NULL);
b = packet_buf_new ();
packet_buf_add_int (b, e->type);
if (e->type == EV_PLIST_DEL
|| e->type == EV_QUEUE_DEL
|| e->type == EV_SRV_ERROR
|| e->type == EV_STATUS_MSG) {
assert (e->data != NULL);
packet_buf_add_str (b, e->data);
}
else if (e->type == EV_PLIST_ADD || e->type == EV_QUEUE_ADD) {
assert (e->data != NULL);
packet_buf_add_item (b, e->data);
}
else if (e->type == EV_FILE_TAGS) {
struct tag_ev_response *r;
assert (e->data != NULL);
r = e->data;
packet_buf_add_str (b, r->file);
packet_buf_add_tags (b, r->tags);
}
else if (e->type == EV_PLIST_MOVE || e->type == EV_QUEUE_MOVE) {
struct move_ev_data *m;
assert (e->data != NULL);
m = (struct move_ev_data *)e->data;
packet_buf_add_str (b, m->from);
packet_buf_add_str (b, m->to);
}
else if (e->data)
abort (); /* BUG */
return b;
}
/* Send the first event from the queue and remove it on success. If the
* operation would block return NB_IO_BLOCK. Return NB_IO_ERR on error
* or NB_IO_OK on success. */
enum noblock_io_status event_send_noblock (int sock, struct event_queue *q)
{
ssize_t res;
char *err;
struct packet_buf *b;
enum noblock_io_status result;
assert (q != NULL);
assert (!event_queue_empty(q));
b = make_event_packet (event_get_first(q));
/* We must do it in one send() call to be able to handle blocking. */
nonblocking (send, res, sock, b->buf, b->len);
if (res == (ssize_t)b->len) {
struct event *e;
e = event_get_first (q);
free_event_data (e->type, e->data);
event_pop (q);
result = NB_IO_OK;
goto exit;
}
if (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
logit ("Sending event would block");
result = NB_IO_BLOCK;
goto exit;
}
err = xstrerror (errno);
logit ("send()ing event failed (%zd): %s", res, err);
free (err);
result = NB_IO_ERR;
exit:
packet_buf_free (b);
return result;
}
|