File: ampctld.c

package info (click to toggle)
hamlib 4.6.5-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,984 kB
  • sloc: ansic: 262,996; sh: 6,135; cpp: 1,578; perl: 876; makefile: 855; python: 148; awk: 58; xml: 26
file content (746 lines) | stat: -rw-r--r-- 18,605 bytes parent folder | download
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
/*
 * ampctld.c - (C) Stephane Fillod 2000-2011
 *             (C) Nate Bargmann 2010,2011,2012,2013
 *
 * This program test/control an amplifier using Hamlib.
 * It takes commands from network connection.
 *
 *
 *   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.,
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <hamlib/config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <getopt.h>
#include <errno.h>
#include <signal.h>

#include <sys/types.h>          /* See NOTES */

#ifdef HAVE_NETINET_IN_H
#  include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#  include <arpa/inet.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#  include <sys/socket.h>
#elif HAVE_WS2TCPIP_H
#  include <ws2tcpip.h>
#  include <fcntl.h>
#  if defined(HAVE_WSPIAPI_H)
#    include <wspiapi.h>
#  endif
#endif
#ifdef HAVE_NETDB_H
#  include <netdb.h>
#endif

#ifdef HAVE_PTHREAD
#  include <pthread.h>
#endif

#include <hamlib/amplifier.h>

#include "ampctl_parse.h"
#include "amplist.h"
#include "rig.h"

struct handle_data
{
    AMP *amp;
    int sock;
    struct sockaddr_storage cli_addr;
    socklen_t clilen;
};

void *handle_socket(void *arg);

void usage();

/*
 * Reminder: when adding long options,
 * keep up to date SHORT_OPTIONS, usage()'s output and man page. thanks.
 * NB: do NOT use -W since it's reserved by POSIX.
 * TODO: add an option to read from a file
 */
#define SHORT_OPTIONS "m:r:s:C:t:T:LuvhVlZ"
static struct option long_options[] =
{
    {"model",           1, 0, 'm'},
    {"amp-file",        1, 0, 'r'},
    {"serial-speed",    1, 0, 's'},
    {"port",            1, 0, 't'},
    {"listen-addr",     1, 0, 'T'},
    {"list",            0, 0, 'l'},
    {"set-conf",        1, 0, 'C'},
    {"show-conf",       0, 0, 'L'},
    {"dump-caps",       0, 0, 'u'},
    {"debug-time-stamps", 0, 0, 'Z'},
    {"verbose",         0, 0, 'v'},
    {"help",            0, 0, 'h'},
    {"version",         0, 0, 'V'},
    {0, 0, 0, 0}
};

int interactive = 1;    /* no cmd because of daemon */
int prompt = 0 ;        /* Daemon mode for rigparse return string */

const char *portno = "4531";
const char *src_addr = NULL;    /* INADDR_ANY */

char send_cmd_term = '\r';      /* send_cmd termination char */

#define MAXCONFLEN 2048


static void handle_error(enum rig_debug_level_e lvl, const char *msg)
{
    int e;
#ifdef __MINGW32__
    LPVOID lpMsgBuf;

    lpMsgBuf = (LPVOID)"Unknown error";
    e = WSAGetLastError();

    if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
                      | FORMAT_MESSAGE_FROM_SYSTEM
                      | FORMAT_MESSAGE_IGNORE_INSERTS,
                      NULL,
                      e,
                      // Default language
                      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                      (LPTSTR)&lpMsgBuf,
                      0,
                      NULL))
    {

        rig_debug(lvl, "%s: Network error %d: %s\n", msg, e, (char *)lpMsgBuf);
        LocalFree(lpMsgBuf);
    }
    else
    {
        rig_debug(lvl, "%s: Network error %d\n", msg, e);
    }

#else
    e = errno;
    rig_debug(lvl, "%s: Network error %d: %s\n", msg, e, strerror(e));
#endif
}


int main(int argc, char *argv[])
{
    AMP *my_amp;        /* handle to amp (instance) */
    amp_model_t my_model = AMP_MODEL_DUMMY;

    int retcode;        /* generic return code from functions */

    int verbose = 0;
    int show_conf = 0;
    int dump_caps_opt = 0;
    const char *amp_file = NULL;
    int serial_rate = 0;
    char conf_parms[MAXCONFLEN] = "";

    struct addrinfo hints, *result, *saved_result;
    int sock_listen;
    int reuseaddr = 1;
    char host[NI_MAXHOST];
    char serv[NI_MAXSERV];

#ifdef HAVE_PTHREAD
    pthread_t thread;
    pthread_attr_t attr;
#endif
    struct handle_data *arg;
#ifdef SIGPIPE
#if HAVE_SIGACTION
    struct sigaction act;
#endif
#endif

    while (1)
    {
        int c;
        int option_index = 0;
        char dummy[2];

        c = getopt_long(argc, argv, SHORT_OPTIONS, long_options, &option_index);

        if (c == -1)
        {
            break;
        }

        switch (c)
        {
        case 'h':
            usage();
            exit(0);

        case 'V':
            version();
            exit(0);

        case 'm':
            if (!optarg)
            {
                usage();    /* wrong arg count */
                exit(1);
            }

            my_model = atoi(optarg);
            break;

        case 'r':
            if (!optarg)
            {
                usage();    /* wrong arg count */
                exit(1);
            }

            amp_file = optarg;
            break;

        case 's':
            if (!optarg)
            {
                usage();    /* wrong arg count */
                exit(1);
            }

            if (sscanf(optarg, "%d%1s", &serial_rate, dummy) != 1)
            {
                fprintf(stderr, "Invalid baud rate of %s\n", optarg);
                exit(1);
            }

            break;

        case 'C':
            if (!optarg)
            {
                usage();    /* wrong arg count */
                exit(1);
            }

            if (*conf_parms != '\0')
            {
                strcat(conf_parms, ",");
            }

            if (strlen(conf_parms) + strlen(optarg) > MAXCONFLEN - 24)
            {
                printf("Length of conf_parms exceeds internal maximum of %d\n",
                       MAXCONFLEN - 24);
                return 1;
            }

            strncat(conf_parms, optarg, MAXCONFLEN - strlen(conf_parms) - 1);
            break;

        case 't':
            if (!optarg)
            {
                usage();    /* wrong arg count */
                exit(1);
            }

            portno = optarg;
            break;

        case 'T':
            if (!optarg)
            {
                usage();    /* wrong arg count */
                exit(1);
            }

            src_addr = optarg;
            break;

        case 'v':
            verbose++;
            break;

        case 'L':
            show_conf++;
            break;

        case 'l':
            list_models();
            exit(0);

        case 'u':
            dump_caps_opt++;
            break;

        case 'Z':
            rig_set_debug_time_stamp(1);
            break;

        default:
            usage();    /* unknown option? */
            exit(1);
        }
    }

    rig_set_debug(verbose);

    rig_debug(RIG_DEBUG_VERBOSE, "ampctld, %s\n", hamlib_version2);
    rig_debug(RIG_DEBUG_VERBOSE, "%s",
              "Report bugs to <hamlib-developer@lists.sourceforge.net>\n\n");

    my_amp = amp_init(my_model);

    if (!my_amp)
    {
        fprintf(stderr,
                "Unknown amp num %d, or initialization error.\n",
                my_model);

        fprintf(stderr, "Please check with --list option.\n");
        exit(2);
    }

    char *token = strtok(conf_parms, ",");

    while (token)
    {
        char mytoken[100], myvalue[100];
        hamlib_token_t lookup;
        sscanf(token, "%99[^=]=%99s", mytoken, myvalue);
        //printf("mytoken=%s,myvalue=%s\n",mytoken, myvalue);
        lookup = amp_token_lookup(my_amp, mytoken);

        if (lookup == 0)
        {
            rig_debug(RIG_DEBUG_ERR, "%s: no such token as '%s'\n", __func__, mytoken);
            token = strtok(NULL, ",");
            continue;
        }

        retcode = amp_set_conf(my_amp, lookup, myvalue);

        if (retcode != RIG_OK)
        {
            fprintf(stderr, "Config parameter error: %s\n", rigerror(retcode));
            exit(2);
        }

        token = strtok(NULL, ",");
    }

    if (amp_file)
    {
        strncpy(AMPPORT(my_amp)->pathname, amp_file, HAMLIB_FILPATHLEN - 1);
    }

    /* FIXME: bound checking and port type == serial */
    if (serial_rate != 0)
    {
        AMPPORT(my_amp)->parm.serial.rate = serial_rate;
    }

    /*
     * print out conf parameters
     */
    if (show_conf)
    {
        amp_token_foreach(my_amp, print_conf_list, (rig_ptr_t)my_amp);
    }

    /*
     * Print out conf parameters, and exits immediately as we may be
     * interested only in only caps, and rig_open may fail.
     */
    if (dump_caps_opt)
    {
        dumpcaps_amp(my_amp, stdout);
        amp_cleanup(my_amp);    /* if you care about memory */
        exit(0);
    }

    retcode = amp_open(my_amp);

    if (retcode != RIG_OK)
    {
        fprintf(stderr, "amp_open: error = %s \n", rigerror(retcode));
        exit(2);
    }

    if (verbose > 0)
    {
        printf("Opened amp model %d, '%s'\n",
               my_amp->caps->amp_model,
               my_amp->caps->model_name);
    }

    rig_debug(RIG_DEBUG_VERBOSE,
              "Backend version: %s, Status: %s\n",
              my_amp->caps->version,
              rig_strstatus(my_amp->caps->status));

#ifdef __MINGW32__
#  ifndef SO_OPENTYPE
#    define SO_OPENTYPE     0x7008
#  endif
#  ifndef SO_SYNCHRONOUS_NONALERT
#    define SO_SYNCHRONOUS_NONALERT 0x20
#  endif
#  ifndef INVALID_SOCKET
#    define INVALID_SOCKET -1
#  endif

    WSADATA wsadata;

    if (WSAStartup(MAKEWORD(1, 1), &wsadata) == SOCKET_ERROR)
    {
        fprintf(stderr, "WSAStartup socket error\n");
        exit(1);
    }

    {
        int sockopt = SO_SYNCHRONOUS_NONALERT;
        setsockopt(INVALID_SOCKET,
                   SOL_SOCKET,
                   SO_OPENTYPE,
                   (char *)&sockopt,
                   sizeof(sockopt));
    }

#endif

    /*
     * Prepare listening socket
     */
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_UNSPEC;        /* Allow IPv4 or IPv6 */
    hints.ai_socktype = SOCK_STREAM;    /* TCP socket */
    hints.ai_flags = AI_PASSIVE;        /* For wildcard IP address */
    hints.ai_protocol = 0;              /* Any protocol */

    retcode = getaddrinfo(src_addr, portno, &hints, &result);

    if (retcode != 0)
    {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(retcode));
        exit(2);
    }

    saved_result = result;

    do
    {
        sock_listen = socket(result->ai_family,
                             result->ai_socktype,
                             result->ai_protocol);

        if (sock_listen < 0)
        {
            handle_error(RIG_DEBUG_ERR, "socket");
            freeaddrinfo(result);   /* No longer needed */
            exit(1);
        }

        if (setsockopt(sock_listen, SOL_SOCKET, SO_REUSEADDR,
                       (char *)&reuseaddr, sizeof(reuseaddr)) < 0)
        {

            handle_error(RIG_DEBUG_ERR, "setsockopt");
            freeaddrinfo(result);   /* No longer needed */
            exit(1);
        }

#ifdef IPV6_V6ONLY

        if (AF_INET6 == result->ai_family)
        {
            /* allow IPv4 mapped to IPv6 clients, MS & BSD default this
               to 1 i.e. disallowed */
            int sockopt = 0;

            if (setsockopt(sock_listen,
                           IPPROTO_IPV6,
                           IPV6_V6ONLY,
                           (char *)&sockopt,
                           sizeof(sockopt))
                    < 0)
            {

                handle_error(RIG_DEBUG_ERR, "setsockopt");
                freeaddrinfo(saved_result);     /* No longer needed */
                exit(1);
            }
        }

#endif

        if (0 == bind(sock_listen, result->ai_addr, result->ai_addrlen))
        {
            break;
        }

        handle_error(RIG_DEBUG_WARN, "binding failed (trying next interface)");
#ifdef __MINGW32__
        closesocket(sock_listen);
#else
        close(sock_listen);
#endif
    }
    while ((result = result->ai_next) != NULL);

    freeaddrinfo(saved_result);     /* No longer needed */

    if (NULL == result)
    {
        rig_debug(RIG_DEBUG_ERR, "%s: bind error - no available interface\n", __func__);
        exit(1);
    }

    if (listen(sock_listen, 4) < 0)
    {
        handle_error(RIG_DEBUG_ERR, "listening");
        exit(1);
    }

#ifdef SIGPIPE
    /* Ignore SIGPIPE as we will handle it at the write()/send() calls
       that will consequently fail with EPIPE. All child threads will
       inherit this disposition which is what we want. */
#if HAVE_SIGACTION
    memset(&act, 0, sizeof act);
    act.sa_handler = SIG_IGN;
    act.sa_flags = SA_RESTART;

    if (sigaction(SIGPIPE, &act, NULL))
    {
        handle_error(RIG_DEBUG_ERR, "sigaction");
    }

#elif HAVE_SIGNAL

    if (SIG_ERR == signal(SIGPIPE, SIG_IGN))
    {
        handle_error(RIG_DEBUG_ERR, "signal");
    }

#endif
#endif

    /*
     * main loop accepting connections
     */
    do
    {
        arg = calloc(1, sizeof(struct handle_data));

        if (!arg)
        {
            rig_debug(RIG_DEBUG_ERR, "calloc: %s\n", strerror(errno));
            exit(1);
        }

        arg->amp = my_amp;
        arg->clilen = sizeof(arg->cli_addr);
        arg->sock = accept(sock_listen,
                           (struct sockaddr *) &arg->cli_addr,
                           &arg->clilen);

        if (arg->sock < 0)
        {
            handle_error(RIG_DEBUG_ERR, "accept");
            break;
        }

        if ((retcode = getnameinfo((struct sockaddr const *)&arg->cli_addr,
                                   arg->clilen,
                                   host,
                                   sizeof(host),
                                   serv,
                                   sizeof(serv),
                                   NI_NUMERICHOST | NI_NUMERICSERV))
                < 0)
        {

            rig_debug(RIG_DEBUG_WARN,
                      "Peer lookup error: %s",
                      gai_strerror(retcode));
        }

        rig_debug(RIG_DEBUG_VERBOSE,
                  "Connection opened from %s:%s\n",
                  host,
                  serv);

#ifdef HAVE_PTHREAD
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

        retcode = pthread_create(&thread, &attr, handle_socket, arg);

        if (retcode != 0)
        {
            rig_debug(RIG_DEBUG_ERR, "pthread_create: %s\n", strerror(retcode));
            break;
        }

#else
        handle_socket(arg);
#endif
    }

    while (retcode == 0);

    amp_close(my_amp); /* close port */
    amp_cleanup(my_amp); /* if you care about memory */

#ifdef __MINGW32__
    WSACleanup();
#endif

    return 0;
}


/*
 * This is the function run by the threads
 */
void *handle_socket(void *arg)
{
    struct handle_data *handle_data_arg = (struct handle_data *)arg;
    FILE *fsockin;
    FILE *fsockout;
    int retcode;
    char host[NI_MAXHOST];
    char serv[NI_MAXSERV];

#ifdef __MINGW32__
    int sock_osfhandle = _open_osfhandle(handle_data_arg->sock, _O_RDONLY);

    if (sock_osfhandle == -1)
    {
        rig_debug(RIG_DEBUG_ERR, "_open_osfhandle error: %s\n", strerror(errno));
        goto handle_exit;
    }

    fsockin = _fdopen(sock_osfhandle,  "rb");
#else
    fsockin = fdopen(handle_data_arg->sock, "rb");
#endif

    if (!fsockin)
    {
        rig_debug(RIG_DEBUG_ERR, "fdopen in: %s\n", strerror(errno));
        goto handle_exit;
    }

#ifdef __MINGW32__
    fsockout = _fdopen(sock_osfhandle, "wb");
#else
    fsockout = fdopen(handle_data_arg->sock, "wb");
#endif

    if (!fsockout)
    {
        rig_debug(RIG_DEBUG_ERR, "fdopen out: %s\n", strerror(errno));
        fclose(fsockin);
        goto handle_exit;
    }

    do
    {
        retcode = ampctl_parse(handle_data_arg->amp, fsockin, fsockout, NULL, 0);

        if (ferror(fsockin) || ferror(fsockout))
        {
            retcode = 1;
        }
    }
    while (retcode == 0 || retcode == 2);

    if ((retcode = getnameinfo((struct sockaddr const *)&handle_data_arg->cli_addr,
                               handle_data_arg->clilen,
                               host,
                               sizeof(host),
                               serv,
                               sizeof(serv),
                               NI_NUMERICHOST | NI_NUMERICSERV))
            < 0)
    {

        rig_debug(RIG_DEBUG_WARN,
                  "Peer lookup error: %s",
                  gai_strerror(retcode));
    }

    rig_debug(RIG_DEBUG_VERBOSE,
              "Connection closed from %s:%s\n",
              host,
              serv);

    fclose(fsockin);
#ifndef __MINGW32__
    fclose(fsockout);
#endif

handle_exit:
#ifdef __MINGW32__
    closesocket(handle_data_arg->sock);
#else
    close(handle_data_arg->sock);
#endif
    free(arg);

#ifdef HAVE_PTHREAD
    pthread_exit(NULL);
#endif
    return NULL;
}


void usage()
{
    printf("Usage: ampctld [OPTION]... [COMMAND]...\n"
           "Daemon serving COMMANDs to a connected amplifier.\n\n");

    printf(
        "  -m, --model=ID                select amplifier model number. See model list (-l)\n"
        "  -r, --amp-file=DEVICE         set device of the amplifier to operate on\n"
        "  -s, --serial-speed=BAUD       set serial speed of the serial port\n"
        "  -t, --port=NUM                set TCP listening port, default %s\n"
        "  -T, --listen-addr=IPADDR      set listening IP address, default ANY\n"
        "  -C, --set-conf=PARM=VAL       set config parameters\n"
        "  -L, --show-conf               list all config parameters\n"
        "  -l, --list                    list all model numbers and exit\n"
        "  -u, --dump-caps               dump capabilities and exit\n"
        "  -v, --verbose                 set verbose mode, cumulative (-v to -vvvvv)\n"
        "  -Z, --debug-time-stamps       enable time stamps for debug messages\n"
        "  -h, --help                    display this help and exit\n"
        "  -V, --version                 output version information and exit\n\n",
        portno);

    usage_amp(stdout);

    printf("\nReport bugs to <hamlib-developer@lists.sourceforge.net>.\n");
}