recv_common: fix memory leak and invalid free in error path
[paraslash.git] / recv_common.c
1 /*
2  * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file recv_common.c common functions of para_recv and para_audiod */
20
21 #include "para.h"
22
23 #include "list.h"
24 #include "sched.h"
25 #include "recv.h"
26 #include "string.h"
27
28 DEFINE_RECEIVER_ARRAY;
29 static void *parse_receiver_args(int receiver_num, char *options)
30 {
31         struct receiver *r = &receivers[receiver_num];
32         char **argv;
33         int argc, i;
34         void *conf;
35
36
37 //      PARA_DEBUG_LOG("%s, options: %s\n", r->name,
38 //              options? options : "(none)");
39         if (options) {
40                 PARA_DEBUG_LOG("options: %s\n", options);
41                 argc = split_args(options, &argv, " \t");
42                 for (i = argc - 1; i >= 0; i--)
43                         argv[i + 1] = argv[i];
44                 argv[0] = para_strdup(r->name);
45                 argc += 1;
46                 PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
47         } else {
48                 argc = 1;
49                 argv = para_malloc(2 * sizeof(char*));
50                 argv[0] = NULL;
51                 argv[1] = NULL;
52         }
53         conf = r->parse_config(argc, argv);
54         free(argv);
55         return conf;
56 }
57
58 /**
59  * check if given string is a valid command line for any receiver
60  *
61  * \param \ra string of the form receiver_name:options
62  * \param receiver_num contains the number of the receiver upon success
63  *
64  * This function checks whether \a ra starts with the name of a supported
65  * paraslash receiver, optinally followed by a colon and any options for that
66  * receiver. If a valid receiver name was found and further are present, the
67  * remaining part of \a ra is passed to that receiver's config parser.
68  *
69  * \return On success, a pointer to the gengetopt args info struct is returned
70  * and \a receiver_num contains the number of the receiver. Otherwise this function
71  * returns \p NULL.
72  */
73 void *check_receiver_arg(char *ra, int *receiver_num)
74 {
75         int j;
76
77         PARA_DEBUG_LOG("checking %s\n", ra);
78         for (j = 0; receivers[j].name; j++) {
79                 const char *name = receivers[j].name;
80                 size_t len = strlen(name);
81                 char c;
82                 if (strlen(ra) < len)
83                         continue;
84                 if (strncmp(name, ra, len))
85                         continue;
86                 c = ra[len];
87                 if (c && c != ' ')
88                         continue;
89                 if (c && !receivers[j].parse_config)
90                         return NULL;
91                 *receiver_num = j;
92                 return parse_receiver_args(j, c? ra + len + 1: NULL);
93         }
94         PARA_ERROR_LOG("%s", "receiver not found\n");
95         return NULL;
96 }