Reformat rigctld.c and rotctld.c

Several lines in each file benefitted from formatting so just reformat
the entire files into Linux C Style.
libusb-1-0
Nate Bargmann 2016-02-12 20:46:38 -06:00
rodzic e4e17ce862
commit 9b45fce776
2 zmienionych plików z 419 dodań i 413 usunięć

Wyświetl plik

@ -24,7 +24,7 @@
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif
#include <stdio.h>
@ -39,26 +39,26 @@
#include <sys/types.h> /* See NOTES */
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
# include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
# include <arpa/inet.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <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
# include <ws2tcpip.h>
# include <fcntl.h>
# if defined(HAVE_WSPIAPI_H)
# include <wspiapi.h>
# endif
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
# include <netdb.h>
#endif
#ifdef HAVE_PTHREAD
#include <pthread.h>
# include <pthread.h>
#endif
#include <hamlib/rig.h>
@ -129,21 +129,18 @@ static void handle_error (enum rig_debug_level_e lvl, const char *msg)
lpMsgBuf = (LPVOID)"Unknown error";
e = WSAGetLastError();
if (FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, e,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
// Default language
(LPTSTR)&lpMsgBuf, 0, NULL))
{
rig_debug (lvl, "%s: Network error %d: %s\n", msg, e, lpMsgBuf);
LocalFree(lpMsgBuf);
}
else
{
rig_debug (lvl, "%s: Network error %d\n", msg, e);
}
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, e,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
// Default language
(LPTSTR)&lpMsgBuf, 0, NULL)) {
rig_debug (lvl, "%s: Network error %d: %s\n", msg, e, 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));
@ -174,164 +171,165 @@ int main (int argc, char *argv[])
char host[NI_MAXHOST];
char serv[NI_MAXSERV];
while(1) {
while (1) {
int c;
int option_index = 0;
c = getopt_long (argc, argv, SHORT_OPTIONS,
long_options, &option_index);
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);
}
rig_file = optarg;
break;
case 'p':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
ptt_file = optarg;
break;
case 'd':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
dcd_file = optarg;
break;
case 'P':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
if (!strcmp(optarg, "RIG"))
ptt_type = RIG_PTT_RIG;
else if (!strcmp(optarg, "DTR"))
ptt_type = RIG_PTT_SERIAL_DTR;
else if (!strcmp(optarg, "RTS"))
ptt_type = RIG_PTT_SERIAL_RTS;
else if (!strcmp(optarg, "PARALLEL"))
ptt_type = RIG_PTT_PARALLEL;
else if (!strcmp(optarg, "CM108"))
ptt_type = RIG_PTT_CM108;
else if (!strcmp(optarg, "NONE"))
ptt_type = RIG_PTT_NONE;
else
ptt_type = atoi(optarg);
break;
case 'D':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
if (!strcmp(optarg, "RIG"))
dcd_type = RIG_DCD_RIG;
else if (!strcmp(optarg, "DSR"))
dcd_type = RIG_DCD_SERIAL_DSR;
else if (!strcmp(optarg, "CTS"))
dcd_type = RIG_DCD_SERIAL_CTS;
else if (!strcmp(optarg, "CD"))
dcd_type = RIG_DCD_SERIAL_CAR;
else if (!strcmp(optarg, "PARALLEL"))
dcd_type = RIG_DCD_PARALLEL;
else if (!strcmp(optarg, "NONE"))
dcd_type = RIG_DCD_NONE;
else
dcd_type = atoi(optarg);
break;
case 'c':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
civaddr = optarg;
break;
case 's':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
serial_rate = atoi(optarg);
break;
case 'C':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
if (*conf_parms != '\0')
strcat(conf_parms, ",");
strncat(conf_parms, optarg, MAXCONFLEN-strlen(conf_parms));
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 'o':
vfo_mode++;
break;
case 'v':
verbose++;
break;
case 'L':
show_conf++;
break;
case 'l':
list_models();
exit(0);
case 'u':
dump_caps_opt++;
break;
default:
usage(); /* unknown option? */
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);
}
rig_file = optarg;
break;
case 'p':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
ptt_file = optarg;
break;
case 'd':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
dcd_file = optarg;
break;
case 'P':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
if (!strcmp(optarg, "RIG"))
ptt_type = RIG_PTT_RIG;
else if (!strcmp(optarg, "DTR"))
ptt_type = RIG_PTT_SERIAL_DTR;
else if (!strcmp(optarg, "RTS"))
ptt_type = RIG_PTT_SERIAL_RTS;
else if (!strcmp(optarg, "PARALLEL"))
ptt_type = RIG_PTT_PARALLEL;
else if (!strcmp(optarg, "CM108"))
ptt_type = RIG_PTT_CM108;
else if (!strcmp(optarg, "NONE"))
ptt_type = RIG_PTT_NONE;
else
ptt_type = atoi(optarg);
break;
case 'D':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
if (!strcmp(optarg, "RIG"))
dcd_type = RIG_DCD_RIG;
else if (!strcmp(optarg, "DSR"))
dcd_type = RIG_DCD_SERIAL_DSR;
else if (!strcmp(optarg, "CTS"))
dcd_type = RIG_DCD_SERIAL_CTS;
else if (!strcmp(optarg, "CD"))
dcd_type = RIG_DCD_SERIAL_CAR;
else if (!strcmp(optarg, "PARALLEL"))
dcd_type = RIG_DCD_PARALLEL;
else if (!strcmp(optarg, "NONE"))
dcd_type = RIG_DCD_NONE;
else
dcd_type = atoi(optarg);
break;
case 'c':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
civaddr = optarg;
break;
case 's':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
serial_rate = atoi(optarg);
break;
case 'C':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
if (*conf_parms != '\0')
strcat(conf_parms, ",");
strncat(conf_parms, optarg, MAXCONFLEN-strlen(conf_parms));
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 'o':
vfo_mode++;
break;
case 'v':
verbose++;
break;
case 'L':
show_conf++;
break;
case 'l':
list_models();
exit(0);
case 'u':
dump_caps_opt++;
break;
default:
usage(); /* unknown option? */
exit(1);
}
}
rig_set_debug(verbose);
rig_set_debug(verbose);
rig_debug(RIG_DEBUG_VERBOSE, "rigctld, %s\n", hamlib_version);
rig_debug(RIG_DEBUG_VERBOSE, "Report bugs to "
"<hamlib-developer@lists.sourceforge.net>\n\n");
"<hamlib-developer@lists.sourceforge.net>\n\n");
my_rig = rig_init(my_model);
if (!my_rig) {
fprintf(stderr, "Unknown rig num %d, or initialization error.\n",
my_model);
my_model);
fprintf(stderr, "Please check with --list option.\n");
exit(2);
}
retcode = set_conf(my_rig, conf_parms);
if (retcode != RIG_OK) {
fprintf(stderr, "Config parameter error: %s\n", rigerror(retcode));
exit(2);
@ -375,6 +373,7 @@ int main (int argc, char *argv[])
}
retcode = rig_open(my_rig);
if (retcode != RIG_OK) {
fprintf(stderr,"rig_open: error = %s \n", rigerror(retcode));
exit(2);
@ -382,9 +381,10 @@ int main (int argc, char *argv[])
if (verbose > 0)
printf("Opened rig model %d, '%s'\n", my_rig->caps->rig_model,
my_rig->caps->model_name);
my_rig->caps->model_name);
rig_debug(RIG_DEBUG_VERBOSE, "Backend version: %s, Status: %s\n",
my_rig->caps->version, rig_strstatus(my_rig->caps->status));
my_rig->caps->version, rig_strstatus(my_rig->caps->status));
#ifdef __MINGW32__
# ifndef SO_OPENTYPE
@ -397,8 +397,8 @@ int main (int argc, char *argv[])
# define INVALID_SOCKET -1
# endif
WSADATA wsadata;
if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
WSADATA wsadata;
if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
fprintf(stderr,"WSAStartup socket error\n");
exit(1);
}
@ -418,59 +418,59 @@ int main (int argc, char *argv[])
retcode = getaddrinfo(src_addr, portno, &hints, &result);
if (retcode != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(retcode));
exit(2);
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(retcode));
exit(2);
}
saved_result = result;
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(saved_result); /* No longer needed */
exit(2);
}
do {
sock_listen = socket(result->ai_family, result->ai_socktype,
result->ai_protocol);
if (sock_listen < 0) {
handle_error (RIG_DEBUG_ERR, "socket");
freeaddrinfo(saved_result); /* No longer needed */
exit(2);
}
if (setsockopt(sock_listen, SOL_SOCKET, SO_REUSEADDR,
(char *)&reuseaddr, sizeof(reuseaddr)) < 0) {
if (setsockopt(sock_listen, SOL_SOCKET, SO_REUSEADDR,
(char *)&reuseaddr, sizeof(reuseaddr)) < 0) {
handle_error (RIG_DEBUG_ERR, "setsockopt");
freeaddrinfo(saved_result); /* No longer needed */
exit (1);
}
#ifdef IPV6_V6ONLY
if (AF_INET6 == result->ai_family) {
/* allow IPv4 mapped to IPv6 clients Windows and BSD default
this to 1 (i.e. disallowed) and we prefer it off */
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);
}
#ifdef IPV6_V6ONLY
if (AF_INET6 == result->ai_family) {
/* allow IPv4 mapped to IPv6 clients Windows and BSD default
this to 1 (i.e. disallowed) and we prefer it off */
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)");
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);
closesocket (sock_listen);
#else
close (sock_listen);
close (sock_listen);
#endif
} while ((result = result->ai_next) != NULL);
} while ((result = result->ai_next) != NULL);
freeaddrinfo(saved_result); /* No longer needed */
if (NULL == result)
{
rig_debug(RIG_DEBUG_ERR, "bind error - no available interface\n");
exit (1);
}
if (NULL == result) {
rig_debug(RIG_DEBUG_ERR, "bind error - no available interface\n");
exit (1);
}
if (listen(sock_listen, 4) < 0) {
handle_error (RIG_DEBUG_ERR, "listening");
@ -488,6 +488,7 @@ int main (int argc, char *argv[])
struct handle_data *arg;
arg = malloc(sizeof(struct handle_data));
if (!arg) {
rig_debug(RIG_DEBUG_ERR, "malloc: %s\n", strerror(errno));
exit (1);
@ -496,24 +497,25 @@ int main (int argc, char *argv[])
arg->rig = my_rig;
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_NOFQDN)) < 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);
if ((retcode = getnameinfo ((struct sockaddr const *)&arg->cli_addr, arg->clilen, host,
sizeof (host), serv, sizeof (serv), NI_NOFQDN)) < 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;
@ -521,8 +523,7 @@ int main (int argc, char *argv[])
#else
handle_socket(arg);
#endif
}
while (retcode == 0);
} while (retcode == 0);
rig_close(my_rig); /* close port */
rig_cleanup(my_rig); /* if you care about memory */
@ -549,6 +550,7 @@ void * handle_socket(void *arg)
#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;
@ -579,17 +581,16 @@ void * handle_socket(void *arg)
retcode = rigctl_parse(handle_data_arg->rig, fsockin, fsockout, NULL, 0);
if (ferror(fsockin) || ferror(fsockout))
retcode = 1;
}
while (retcode == 0 || retcode == 2);
} 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_NOFQDN)) < 0) {
rig_debug (RIG_DEBUG_WARN, "Peer lookup error: %s", gai_strerror (retcode));
}
if ((retcode = getnameinfo ((struct sockaddr const *)&handle_data_arg->cli_addr
, handle_data_arg->clilen, host, sizeof (host)
, serv, sizeof (serv), NI_NOFQDN)) < 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);
host, serv);
fclose(fsockin);
#ifndef __MINGW32__
@ -613,29 +614,29 @@ handle_exit:
void usage(void)
{
printf("Usage: rigctld [OPTION]...\n"
"Daemon serving COMMANDs to a connected radio transceiver or receiver.\n\n");
"Daemon serving COMMANDs to a connected radio transceiver or receiver.\n\n");
printf(
" -m, --model=ID select radio model number. See model list\n"
" -r, --rig-file=DEVICE set device of the radio to operate on\n"
" -p, --ptt-file=DEVICE set device of the PTT device to operate on\n"
" -d, --dcd-file=DEVICE set device of the DCD device to operate on\n"
" -P, --ptt-type=TYPE set type of the PTT device to operate on\n"
" -D, --dcd-type=TYPE set type of the DCD device to operate on\n"
" -s, --serial-speed=BAUD set serial speed of the serial port\n"
" -c, --civaddr=ID set CI-V address, decimal (for Icom rigs only)\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"
" -o, --vfo do not default to VFO_CURR, require extra vfo arg\n"
" -v, --verbose set verbose mode, cumulative\n"
" -h, --help display this help and exit\n"
" -V, --version output version information and exit\n\n",
portno );
" -m, --model=ID select radio model number. See model list\n"
" -r, --rig-file=DEVICE set device of the radio to operate on\n"
" -p, --ptt-file=DEVICE set device of the PTT device to operate on\n"
" -d, --dcd-file=DEVICE set device of the DCD device to operate on\n"
" -P, --ptt-type=TYPE set type of the PTT device to operate on\n"
" -D, --dcd-type=TYPE set type of the DCD device to operate on\n"
" -s, --serial-speed=BAUD set serial speed of the serial port\n"
" -c, --civaddr=ID set CI-V address, decimal (for Icom rigs only)\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"
" -o, --vfo do not default to VFO_CURR, require extra vfo arg\n"
" -v, --verbose set verbose mode, cumulative\n"
" -h, --help display this help and exit\n"
" -V, --version output version information and exit\n\n",
portno );
usage_rig(stdout);

Wyświetl plik

@ -23,7 +23,7 @@
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif
#include <stdio.h>
@ -38,26 +38,26 @@
#include <sys/types.h> /* See NOTES */
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
# include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
# include <arpa/inet.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <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
# include <ws2tcpip.h>
# include <fcntl.h>
# if defined(HAVE_WSPIAPI_H)
# include <wspiapi.h>
# endif
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
# include <netdb.h>
#endif
#ifdef HAVE_PTHREAD
#include <pthread.h>
# include <pthread.h>
#endif
#include <hamlib/rotator.h>
@ -119,22 +119,20 @@ static void handle_error (enum rig_debug_level_e lvl, const char *msg)
lpMsgBuf = (LPVOID)"Unknown error";
e = WSAGetLastError();
if (FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, e,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
// Default language
(LPTSTR)&lpMsgBuf, 0, NULL))
{
rig_debug (lvl, "%s: Network error %d: %s\n", msg, e, lpMsgBuf);
LocalFree(lpMsgBuf);
}
else
{
rig_debug (lvl, "%s: Network error %d\n", msg, e);
}
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, e,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
// Default language
(LPTSTR)&lpMsgBuf, 0, NULL)) {
rig_debug (lvl, "%s: Network error %d: %s\n", msg, e, 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));
@ -166,77 +164,77 @@ int main (int argc, char *argv[])
int c;
int option_index = 0;
c = getopt_long (argc, argv, SHORT_OPTIONS,
long_options, &option_index);
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);
}
rot_file = optarg;
break;
case 's':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
serial_rate = atoi(optarg);
break;
case 'C':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
if (*conf_parms != '\0')
strcat(conf_parms, ",");
strncat(conf_parms, optarg, MAXCONFLEN-strlen(conf_parms));
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;
default:
usage(); /* unknown option? */
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);
}
rot_file = optarg;
break;
case 's':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
serial_rate = atoi(optarg);
break;
case 'C':
if (!optarg) {
usage(); /* wrong arg count */
exit(1);
}
if (*conf_parms != '\0')
strcat(conf_parms, ",");
strncat(conf_parms, optarg, MAXCONFLEN-strlen(conf_parms));
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;
default:
usage(); /* unknown option? */
exit(1);
}
}
@ -244,18 +242,19 @@ int main (int argc, char *argv[])
rig_debug(RIG_DEBUG_VERBOSE, "rotctld, %s\n", hamlib_version);
rig_debug(RIG_DEBUG_VERBOSE, "Report bugs to "
"<hamlib-developer@lists.sourceforge.net>\n\n");
"<hamlib-developer@lists.sourceforge.net>\n\n");
my_rot = rot_init(my_model);
if (!my_rot) {
fprintf(stderr, "Unknown rot num %d, or initialization error.\n",
my_model);
my_model);
fprintf(stderr, "Please check with --list option.\n");
exit(2);
}
retcode = set_conf(my_rot, conf_parms);
if (retcode != RIG_OK) {
fprintf(stderr, "Config parameter error: %s\n", rigerror(retcode));
exit(2);
@ -286,6 +285,7 @@ int main (int argc, char *argv[])
}
retcode = rot_open(my_rot);
if (retcode != RIG_OK) {
fprintf(stderr,"rot_open: error = %s \n", rigerror(retcode));
exit(2);
@ -293,10 +293,10 @@ int main (int argc, char *argv[])
if (verbose > 0)
printf("Opened rot model %d, '%s'\n", my_rot->caps->rot_model,
my_rot->caps->model_name);
my_rot->caps->model_name);
rig_debug(RIG_DEBUG_VERBOSE, "Backend version: %s, Status: %s\n",
my_rot->caps->version, rig_strstatus(my_rot->caps->status));
my_rot->caps->version, rig_strstatus(my_rot->caps->status));
#ifdef __MINGW32__
# ifndef SO_OPENTYPE
@ -310,6 +310,7 @@ int main (int argc, char *argv[])
# endif
WSADATA wsadata;
if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
fprintf(stderr,"WSAStartup socket error\n");
exit(1);
@ -336,55 +337,56 @@ int main (int argc, char *argv[])
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);
}
do {
sock_listen = socket(result->ai_family, result->ai_socktype,
result->ai_protocol);
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);
}
if (sock_listen < 0) {
handle_error (RIG_DEBUG_ERR, "socket");
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 */
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, "bind error - no available interface\n");
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 */
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, "bind error - no available interface\n");
exit (1);
}
if (listen(sock_listen, 4) < 0) {
handle_error (RIG_DEBUG_ERR, "listening");
exit (1);
@ -401,6 +403,7 @@ int main (int argc, char *argv[])
struct handle_data *arg;
arg = malloc(sizeof(struct handle_data));
if (!arg) {
rig_debug(RIG_DEBUG_ERR, "malloc: %s\n", strerror(errno));
exit (1);
@ -409,25 +412,28 @@ int main (int argc, char *argv[])
arg->rot = my_rot;
arg->clilen = sizeof(arg->cli_addr);
arg->sock = accept(sock_listen, (struct sockaddr *) &arg->cli_addr,
&arg->clilen);
&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_NOFQDN)) < 0)
{
rig_debug (RIG_DEBUG_WARN, "Peer lookup error: %s", gai_strerror (retcode));
}
if ((retcode = getnameinfo ((struct sockaddr const *)&arg->cli_addr, arg->clilen,
host, sizeof (host), serv, sizeof (serv),
NI_NOFQDN)) < 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);
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;
@ -435,8 +441,7 @@ int main (int argc, char *argv[])
#else
handle_socket(arg);
#endif
}
while (retcode == 0);
} while (retcode == 0);
rot_close(my_rot); /* close port */
rot_cleanup(my_rot); /* if you care about memory */
@ -463,6 +468,7 @@ void * handle_socket(void *arg)
#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;
@ -474,7 +480,7 @@ void * handle_socket(void *arg)
#endif
if (!fsockin) {
rig_debug(RIG_DEBUG_ERR, "fdopen in: %s\n", strerror(errno));
goto handle_exit;
goto handle_exit;
}
#ifdef __MINGW32__
@ -485,24 +491,23 @@ void * handle_socket(void *arg)
if (!fsockout) {
rig_debug(RIG_DEBUG_ERR, "fdopen out: %s\n", strerror(errno));
fclose(fsockin);
goto handle_exit;
goto handle_exit;
}
do {
retcode = rotctl_parse(handle_data_arg->rot, fsockin, fsockout, NULL, 0);
if (ferror(fsockin) || ferror(fsockout))
retcode = 1;
}
while (retcode == 0 || retcode == 2);
} 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_NOFQDN)) < 0) {
rig_debug (RIG_DEBUG_WARN, "Peer lookup error: %s", gai_strerror (retcode));
}
if ((retcode = getnameinfo ((struct sockaddr const *)&handle_data_arg->cli_addr
, handle_data_arg->clilen, host, sizeof (host)
, serv, sizeof (serv), NI_NOFQDN)) < 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);
host, serv);
fclose(fsockin);
#ifndef __MINGW32__
@ -526,21 +531,21 @@ handle_exit:
void usage()
{
printf("Usage: rotctld [OPTION]... [COMMAND]...\n"
"Daemon serving COMMANDs to a connected antenna rotator.\n\n");
"Daemon serving COMMANDs to a connected antenna rotator.\n\n");
printf(
" -m, --model=ID select rotator model number. See model list\n"
" -r, --rot-file=DEVICE set device of the rotator 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\n"
" -h, --help display this help and exit\n"
" -V, --version output version information and exit\n\n",
" -m, --model=ID select rotator model number. See model list\n"
" -r, --rot-file=DEVICE set device of the rotator 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\n"
" -h, --help display this help and exit\n"
" -V, --version output version information and exit\n\n",
portno);
usage_rot(stdout);