Debian Codegraph @

librplay/rptp.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* $Id: rptp.c,v 1.4 1999/03/10 07:57:56 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * rplay is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * rplay is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with rplay; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 #include "version.h"
30 #include "rplay.h"
31 #include <sys/errno.h>
32 #ifdef ultrix
33 #include <fcntl.h>
34 #else
35 #include <sys/fcntl.h>
36 #endif
37 #include <netdb.h>
38 #include <unistd.h>
39 #ifdef HAVE_STRING_H
40 #include <string.h>
41 #endif
42 #ifdef __STDC__
43 #include <stdarg.h>
44 #else
45 #include <varargs.h>
46 #endif
47 #include <errno.h>
•48 #include <ctype.h>
49
50 int rptp_errno = RPTP_ERROR_NONE;
51
52 char *rptp_errlist[] =
53 {
54 "no error", /* RPTP_ERROR_NONE */
55 "out of memory", /* RPTP_ERROR_MEMORY */
56 "host not found", /* RPTP_ERROR_HOST */
57 "connection failed", /* RPTP_ERROR_CONNECT */
58 "cannot create socket", /* RPTP_ERROR_SOCKET */
59 "cannot open socket", /* RPTP_ERROR_OPEN */
60 "cannot read from socket", /* RPTP_ERROR_READ */
61 "cannot write to socket", /* RPTP_ERROR_WRITE */
62 "cannot ping rplay server", /* RPTP_ERROR_PING */
63 "connection timeout", /* RPTP_ERROR_TIMEOUT */
64 "RPTP protocol error", /* RPTP_ERROR_PROTOCOL */
65 };
66
67 #ifdef __STDC__
68 unsigned long inet_addr(char *rp);
69 #else
70 unsigned long inet_addr();
71 #endif
72
73 #ifndef HAVE_STRDUP
74 #ifdef __STDC__
75 static char *
76 strdup(char *str)
77 #else
78 static char *
79 strdup(str)
80 char *str;
81 #endif
82 {
83 char *p;
84
85 p = (char *) malloc(strlen(str) + 1);
86 if (p == NULL)
87 {
88 return NULL;
89 }
90 else
91 {
92 strcpy(p, str);
93 return p;
94 }
95 }
96 #endif /* HAVE_STRDUP */
97
98 /*
99 * open an RPTP connection to host using port
100 * the connection response will be stored in the response buffer
101 */
102 #ifdef __STDC__
103 int
104 rptp_open(char *host, int port, char *response, int response_size)
105 #else
106 int
107 rptp_open(host, port, response, response_size)
108 char *host;
109 int port;
110 char *response;
111 int response_size;
112 #endif
113 {
114 u_long addr;
115 struct sockaddr_in s;
116 struct hostent *hp;
117 int rptp_fd;
118 int i, n;
119
120 rptp_errno = RPTP_ERROR_NONE;
121
122 memset((char *) &s, 0, sizeof(s));
123
124 addr = inet_addr(host);
125 if (addr == 0xffffffff)
126 {
127 hp = gethostbyname(host);
128 if (hp == NULL)
129 {
130 rptp_errno = RPTP_ERROR_HOST;
131 return -1;
132 }
133 memcpy((char *) &s.sin_addr.s_addr, (char *) hp->h_addr, hp->h_length);
134 }
135 else
136 {
137 memcpy((char *) &s.sin_addr.s_addr, (char *) &addr, sizeof(addr));
138 }
139
140 s.sin_port = htons(port);
141 s.sin_family = AF_INET;
142
143 for (i = 0; i < RPTP_CONNECT_ATTEMPTS; i++)
144 {
145 if (rplay_ping(host) < 0)
146 {
147 rptp_errno = RPTP_ERROR_PING;
148 return -1;
149 }
150
151 rptp_fd = socket(AF_INET, SOCK_STREAM, 0);
152 if (rptp_fd < 0)
153 {
154 rptp_errno = RPTP_ERROR_SOCKET;
155 return -1;
156 }
157
158 n = connect(rptp_fd, (struct sockaddr *) &s, sizeof(s));
159 if (n == 0)
160 {
161 /*
162 * successful connection
163 */
164 rptp_getline(rptp_fd, response, response_size);
165 if (response[0] == RPTP_ERROR)
166 {
167 rptp_errno = RPTP_ERROR_OPEN;
168 return -1;
169 }
170 return rptp_fd;
171 }
172
173 switch (errno)
174 {
175 case ECONNREFUSED:
176 case EINTR:
177 close(rptp_fd);
178 if (i + 1 != RPTP_CONNECT_ATTEMPTS)
179 {
180 sleep(RPTP_PING_DELAY);
181 }
182 break;
183
184 default:
185 rptp_errno = RPTP_ERROR_CONNECT;
186 return -1;
187 }
188 }
189
190 rptp_errno = RPTP_ERROR_CONNECT;
191
192 return -1;
193 }
194
195 /*
196 * read nbytes from the RPTP connection
197 *
198 * return the number of bytes actually read
199 *
200 * The routine was adapted from readn() in "Unix Network Programming"
201 * by W. Richard Stevens, page 279.
202 */
203 #ifdef __STDC__
204 int
205 rptp_read(int rptp_fd, char *ptr, int nbytes)
206 #else
207 int
208 rptp_read(rptp_fd, ptr, nbytes)
209 int rptp_fd;
210 char *ptr;
211 int nbytes;
212 #endif
213 {
214 int nleft, nread;
215
216 rptp_errno = RPTP_ERROR_NONE;
217
218 nleft = nbytes;
219
220 while (nleft > 0)
221 {
222 nread = read(rptp_fd, ptr, nleft);
223 if (nread < 0)
224 {
225 if (errno == EINTR)
226 {
227 continue;
228 }
229 rptp_errno = RPTP_ERROR_READ;
230 return -1;
231 }
232 if (nread == 0)
233 {
234 break;
235 }
236 nleft -= nread;
237 ptr += nread;
238 }
239
240 return nbytes - nleft;
241 }
242
243 /*
244 * write nbytes to the RPTP connection
245 *
246 * return the number of bytes actually written
247 *
248 * The routine was adapted from writen() in "Unix Network Programming"
249 * by W. Richard Stevens, page 279-80.
250 */
251 #ifdef __STDC__
252 int
253 rptp_write(int rptp_fd, char *ptr, int nbytes)
254 #else
255 int
256 rptp_write(rptp_fd, ptr, nbytes)
257 int rptp_fd;
258 char *ptr;
259 int nbytes;
260 #endif
261 {
262 int nleft, nwritten;
263
264 rptp_errno = RPTP_ERROR_NONE;
265
266 nleft = nbytes;
267
268 while (nleft > 0)
269 {
270 nwritten = write(rptp_fd, ptr, nleft);
271 if (nwritten < 0)
272 {
273 if (errno == EINTR)
274 {
275 continue;
276 }
277 rptp_errno = RPTP_ERROR_WRITE;
278 return -1;
279 }
280 else if (nwritten == 0)
281 {
282 rptp_errno = RPTP_ERROR_WRITE;
283 return -1;
284 }
285 nleft -= nwritten;
286 ptr += nwritten;
287 }
288
289 return nbytes - nleft;
290 }
291
292 /*
293 * write a line to the RPTP connection
294 *
295 * "\r\n" is appened to the string
296 */
297 #ifdef __STDC__
298 int
299 rptp_putline(int rptp_fd, char *fmt,...)
300 #else
301 int
302 rptp_putline(va_alist)
303 va_dcl
304 #endif
305 {
306 va_list args;
307 char buf[RPTP_MAX_LINE];
308
309 #ifdef __STDC__
310 va_start(args, fmt);
311 #else
312 int rptp_fd;
313 char *fmt;
314 va_start(args);
315 rptp_fd = va_arg(args, int);
316 fmt = va_arg(args, char *);
317 #endif
318
319 rptp_errno = RPTP_ERROR_NONE;
320
321 vsprintf(buf, fmt, args);
322 va_end(args);
323 strcat(buf, "\r\n");
324
325 return rptp_write(rptp_fd, buf, strlen(buf)) != strlen(buf) ? -1 : 0;
326 }
327
328 /*
329 * read a line from the RPTP connection
330 *
331 * "\r\n" is removed from the line
332 */
333 #ifdef __STDC__
334 int
335 rptp_getline(int rptp_fd, char *buf, int nbytes)
336 #else
337 int
338 rptp_getline(rptp_fd, buf, nbytes)
339 int rptp_fd;
340 char *buf;
341 int nbytes;
342 #endif
343 {
344 int i, n, nleft, x;
345 char *ptr;
346 char tmp_buf[RPTP_MAX_LINE];
347
348 rptp_errno = RPTP_ERROR_NONE;
349
350 nleft = nbytes;
351 ptr = buf;
352
353 while (nleft > 0)
354 {
355 /*
356 * peek at the message so only the necessary data
357 * is actually read
358 */
359 n = recv(rptp_fd, ptr, nleft, MSG_PEEK);
360 if (n < 0)
361 {
362 if (errno == EINTR)
363 {
364 continue;
365 }
366 rptp_errno = RPTP_ERROR_READ;
367 return -1;
368 }
369 else if (n == 0)
370 {
371 rptp_errno = RPTP_ERROR_READ;
372 return -1;
373 }
374 nleft -= n;
375 for (i = 0; i < n; i++)
376 {
377 if (ptr[i] == '\r')
378 {
379 ptr[i] = '\0';
380 }
381 else if (ptr[i] == '\n')
382 {
383 ptr[i] = '\0';
384 break;
385 }
386 }
387 again:
388 x = read(rptp_fd, tmp_buf, i == n ? n : i + 1);
389 if (x < 0)
390 {
391 if (errno == EINTR)
392 {
393 goto again;
394 }
395 rptp_errno = RPTP_ERROR_READ;
396 return -1;
397 }
398 else if (x == 0)
399 {
400 rptp_errno = RPTP_ERROR_READ;
401 return -1;
402 }
403 else if (i < n)
404 {
405 return 0;
406 }
407 else
408 {
409 ptr += n;
410 }
411 }
412
413 rptp_errno = RPTP_ERROR_READ;
414
415 return -1;
416 }
417
418 /*
419 * send the RPTP command to the RPTP connection storing the response in the
420 * response buffer
421 */
422 #ifdef __STDC__
423 int
424 rptp_command(int rptp_fd, char *command, char *response, int response_size)
425 #else
426 int
427 rptp_command(rptp_fd, command, response, response_size)
428 int rptp_fd;
429 char *command;
430 char *response;
431 int response_size;
432 #endif
433 {
434 rptp_errno = RPTP_ERROR_NONE;
435
436 if (rptp_putline(rptp_fd, command) < 0)
437 {
438 return -1;
439 }
440 if (rptp_getline(rptp_fd, response, response_size) < 0)
441 {
442 return -1;
443 }
444 switch (response[0])
445 {
446 case RPTP_TIMEOUT:
447 rptp_errno = RPTP_ERROR_TIMEOUT;
448 return -1;
449
450 case RPTP_ERROR:
451 return 1;
452
453 case RPTP_OK:
454 case RPTP_NOTIFY:
455 return 0;
456
457 default:
458 rptp_errno = RPTP_ERROR_PROTOCOL;
459 return -1;
460 }
461 }
462
463 /*
464 * close the RPTP connection
465 */
466 #ifdef __STDC__
467 int
468 rptp_close(int rptp_fd)
469 #else
470 int
471 rptp_close(rptp_fd)
472 int rptp_fd;
473 #endif
474 {
475 rptp_errno = RPTP_ERROR_NONE;
476
477 close(rptp_fd);
478
479 return 0;
480 }
481
482 /*
483 * report RPTP errors
484 */
485 #ifdef __STDC__
486 void
487 rptp_perror(char *message)
488 #else
489 void
490 rptp_perror(message)
491 char *message;
492 #endif
493 {
494 fprintf(stderr, "%s: %s\n", message, rptp_errlist[rptp_errno]);
495 }
496
497 /*
498 * List definitions used in rptp_parse.
499 */
500 typedef struct _list
501 {
502 struct _list *next;
503 char *name;
504 char *value;
505 }
506 LIST;
507
508 static LIST *list = NULL, **list_next = &list;
509
510 #define list_alloc() \
511 *list_next = (LIST *) malloc (sizeof(LIST)); \
512 if (!*list_next) \
513 { \
514 return NULL; \
515 }
516
517 #define list_add(xname, xvalue) \
518 list_alloc(); \
519 (*list_next)->name = xname; \
520 (*list_next)->value = xvalue; \
521 list_next = &(*list_next)->next; \
522 *list_next = NULL;
523
524 #define list_free() \
525 { \
526 LIST *l; \
527 for (; list; l = list, list = list->next, free ((char *)l)); \
528 list = NULL; \
529 list_next = &list; \
530 }
531
532 #ifdef __STDC__
533 char *
534 rptp_parse(char *response, char *name)
535 #else
536 char *
537 rptp_parse(response, name)
538 char *response;
539 char *name;
540 #endif
541 {
542 static char *buf;
543 static LIST *list_pos, *cache_pos;
544
545 /* No more name-value pairs. */
546 if (!response && !name && !list_pos)
547 {
548 return NULL;
549 }
550 /* Load the new `name=value' list. */
551 else if (response)
552 {
553 char *p;
554 char *response_name = "", *response_value = "";
555
556 list_free();
557 if (buf)
558 {
559 free((char *) buf);
560 }
561 buf = strdup(response);
562
563 p = buf;
564
565 /* Skip any initial RPTP characters. */
566 switch (*p)
567 {
568 case RPTP_ERROR:
569 case RPTP_OK:
570 case RPTP_NOTIFY:
571 p++;
572 break;
573 }
574
575 while (p && *p)
576 {
577 /* Skip white-space. */
578 if (isspace(*p))
579 {
580 for (p++; isspace(*p); p++) ;
581 continue;
582 }
583
584 /* `name' */
585 response_name = p;
586 p = strpbrk(p, "= \t\r\n");
587
588 /* `value' */
589 if (p && *p == '=')
590 {
591 int quoted = 0;
592
593 *p++ = '\0'; /* Remove the `=' */
594
595 if (*p == '"') /* Start of a quoted value */
596 {
597 p++;
598 quoted++;
599 }
600
601 response_value = p;
602 if (quoted)
603 {
604 p = strchr(p, '"');
605 }
606 else
607 {
608 p = strpbrk(p, " \t\r\n");
609 }
610 if (p)
611 {
612 *p++ = '\0';
613 }
614 }
615 else if (p)
616 {
617 *p++ = '\0';
618 }
619
620 list_add(response_name, response_value);
621 response_name = "";
622 response_value = "";
623 }
624
625 list_pos = list;
626 cache_pos = NULL;
627 }
628
629 /* Search for `name'. */
630 if (name)
631 {
632 LIST *l;
633 char *p;
634
635 /* Skip any leading dashes. */
636 while (*name == '-')
637 {
638 name++;
639 }
640
641 if (cache_pos)
642 {
643 for (p = cache_pos->name; *p && *p == '-'; p++) ; /* skip leading dashes */
644 if (strcmp(name, p) == 0)
645 {
646 return cache_pos->value;
647 }
648 }
649
650 for (l = list; l; l = l->next)
651 {
652 for (p = l->name; *p && *p == '-'; p++) ; /* skip leading dashes */
653 if (strcmp(p, name) == 0)
654 {
655 return l->value;
656 }
657 }
658 return NULL; /* Not found */
659 }
660 /* Cycle through the names. */
661 else if (list_pos)
662 {
663 cache_pos = list_pos;
664 list_pos = list_pos->next;
665 return cache_pos->name;
666 }
667
668 return NULL;
669 }