File: sapserver.cpp

package info (click to toggle)
minisapserver 0.3.6-1.1
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 704 kB
  • ctags: 149
  • sloc: sh: 4,268; cpp: 1,079; makefile: 72
file content (318 lines) | stat: -rw-r--r-- 9,137 bytes parent folder | download | duplicates (2)
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
/*****************************************************************************
 * sapserver.cpp : SAP discovery service mini-server
 ****************************************************************************
 * Copyright (C) 1998-2004 VideoLAN
 * $Id: sapserver.cpp 342 2008-06-30 17:05:19Z courmisch $ 
 *
 * Authors: Arnaud Schauly <gitan@via.ecp.fr>
 *          Clément Stenac <zorglub@via.ecp.fr>
 *          Damien Lucas <nitrox@videolan.org>
 *          Rémi Denis-Courmont <rem # videolan.org>
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <inttypes.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <locale.h>
#include <string>
#include <vector>
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif

using namespace std;

#include "sapserver.h"
#include "program.h"
#include "message.h"
#include "broadcast.h"
#include "parser.h"

#ifdef CONFIG_SLP
    #include <slp.h>
    #include "lslp.h"
#endif

/*************************************************
 * Signal handler
 *************************************************/
static int should_exit = 0;

static void
exit_handler (int signum)
{
    signal (signum, exit_handler);
    should_exit = 1;
}


/*************************************************
 * Display the help
 *************************************************/
static void
help(void)
{
  printf(PACKAGE_NAME"\n"
  "Options:\n"
  "  -d      Use this to daemonize the process (run in the background)\n"
  "  -f      Use this to give a configuration file "
                "(default is %s)\n"
  "  -c      Same as -f\n"
  "  -h      Display this help\n"
  "  -t      \"slp\" or \"sap\". sap is default\n"
  "  -s      Display a dot for each packet sent\n"
  "  -u      Unregister services (SLP only)\n"
  "  -V      Display version information\n", DEFAULT_CONF);
}

static void
version(void)
{
    puts(PACKAGE_NAME" version "PACKAGE_VERSION"\n"
    "Copyright (C) 1999-2005 VideoLAN project\n"
    "This is free software; see the source for copying conditions.\n"
    "There is NO warranty; not even for MERCHANTABILITY or\n"
    "FITNESS FOR A PARTICULAR PURPOSE.");
}

typedef struct
{
    struct sockaddr_storage addr;
    socklen_t addrlen;
    Message *message;
} Announce;

#ifndef HAVE_CLEARENV
extern char **environ;

static int
clearenv (void)
{
	environ = NULL;
	return 0;
}
#endif

/*************************************************
 * The main function
 *************************************************/
int main(int argc, char *argv[])
{
    int result;
    const struct option opts[] =
        {
                { "conf",       required_argument, NULL, 'c' },
                { "config",     required_argument, NULL, 'c' },
                { "daemon",     no_argument,       NULL, 'd' },
                { "daemonize",  no_argument,       NULL, 'd' },
                { "file",       required_argument, NULL, 'f' },
                { "help",       no_argument,       NULL, 'h' },
                { "dotmode",    no_argument,       NULL, 's' },
                { "type",       required_argument, NULL, 't' },
                { "unregister", no_argument,       NULL, 'u' },
                { "version",    no_argument,       NULL, 'V' },
                { NULL,         no_argument,       NULL, '\0'}
        };


    setvbuf (stdout, NULL, _IOLBF, 0);
    setvbuf (stderr, NULL, _IOLBF, 0);
    setlocale(LC_ALL, "");
    Config config(DEFAULT_CONF);

    /* Parse the command line */
    while((result = getopt_long(argc,argv,"c:df:hst:uV", opts, NULL))>0)
    {
        switch(result)
        {
            case 'c':
            case 'f':
                config.SetFile(optarg);
                break;
            case 's':
                config.SetDotMode(true);
                break;
            case 'd':
                config.SetDaemonMode(true);
                break;
            case 't':
                config.SetType(optarg);
                break;
            case 'u':
                config.SetReverse(true);
                break;
            case 'h':
                help();
                return 0;
            case 'V':
                version();
                return 0;
            default:
                return -1;
        }
    }

    /* clean up environment */
    clearenv();
    /* make sure stdin, stdout and stderr exists */
    if (dup (2) < 3)
        return 1;

    /* Get the programs */
    puts("+ Parsing configuration file\n");
    if(config.Parse())
    {
        fputs("+ Parsing failed\n", stderr);
        return 1;
    }

    /* TODO check configuration */
    printf("+ %u programs loaded\n",config.Programs.size());
    if (config.Programs.size() == 0)
    {
        puts ("+ Nothing to do. Exiting.");
        return 0;
    }

    if(config.GetType() == TYPE_SAP)
    {
        printf("+ Packet TTL set to %u\n",config.GetTTL());
        printf("+ Running as %s.\n",
               config.GetDaemonMode() ? "daemon" : "program");

        /* Create the broadcast object */
        Broadcast broadcast (config.GetTTL (), config.GetInterface ());

        vector<Announce *> announces;

        for (unsigned int i = 0; i < config.Programs.size(); i++)
        {
            Program *program = config.Programs[i];

            Announce *announce = new Announce;
            if (broadcast.GuessDestination (program->GetAddress ().c_str (),
                                            &announce->addr,
                                            &announce->addrlen))
            {
                fputs("- Network initialization failed. Aborting\n", stderr);
                delete announce;
                /* FIXME memory leak in announces */
                return 1;
            }

            /* Create a new message */
            Message *message = new Message(0x4212+i, "1.2.3.4");
            /* Add the program */
            message->AddProgram(config.Programs[i]);

            announce->message = message;
            announces.push_back( announce );
        }

        /* Forking if necessary */
        if(config.GetDaemonMode())
        {
            puts("+ Forking ... \n");
            daemon(0,0);
        }

        unsigned n = config.Programs.size();
        lldiv_t d = lldiv (1000000000LL * config.GetDelay() / n, 1000000000);
        struct timespec delay;
        delay.tv_sec = d.quot;
        delay.tv_nsec = d.rem;

        signal (SIGINT, exit_handler);
        signal (SIGTERM, exit_handler);
        signal (SIGHUP, exit_handler);
        signal (SIGQUIT, exit_handler);
        setvbuf (stdout, NULL, _IONBF, 0);

        while(!should_exit)
        {
            for( unsigned int i = 0; i< announces.size() ; i ++ )
            {
                Announce *announce = announces[i];
                /* Send the message */
                if (broadcast.Send (announce->message ,
                                    (struct sockaddr *)&announce->addr,
                                    announce->addrlen))
                {
                    fputs ("- Message send failed\n", stderr);
                }
                else
                if(config.GetDotMode())
                    fputc('.', stdout);

                /* Wait for next sending */
                if(should_exit)
                    break;
                nanosleep( &delay, NULL );
            }
        }

        for( unsigned int i = 0; i < announces.size() ; i ++ )
        {
            Announce *announce = announces[i];
            delete announce->message;
            delete announce;
        }
    }
    else
    {
#ifndef CONFIG_SLP
        fputs("- SLP not compiled in...Aborting\n", stderr);
        return 1;
#endif
#ifdef CONFIG_SLP
        SLP slp;

        for(unsigned int i=0 ; i<config.Programs.size(); i++)
        {
            if(!config.GetReverse())
            {
                slp.Register(config.Programs[i]);
            }
            else
            {
                slp.UnRegister(config.Programs[i]);
            }
        }
        if(!config.GetReverse())
        {
            puts("+ Programs registered. To unregister them, run me with -u");
        }
        else
        {
            puts("+ Programs unregistered");
        }
#endif
    }

    puts("Done.");

    return 0;
}