Touch up rigctld.c and rotctld.c with astyle

Prior reformat had only been a reindent with Emacs.  Now use astyle.
Hamlib-3.1
Nate Bargmann 2016-02-14 18:05:55 -06:00
rodzic 022ba0f48b
commit 4595f77f99
2 zmienionych plików z 160 dodań i 82 usunięć

Wyświetl plik

@ -76,15 +76,14 @@
* TODO: add an option to read from a file * TODO: add an option to read from a file
*/ */
#define SHORT_OPTIONS "m:r:p:d:P:D:s:c:T:t:C:lLuovhV" #define SHORT_OPTIONS "m:r:p:d:P:D:s:c:T:t:C:lLuovhV"
static struct option long_options[] = static struct option long_options[] = {
{
{"model", 1, 0, 'm'}, {"model", 1, 0, 'm'},
{"rig-file", 1, 0, 'r'}, {"rig-file", 1, 0, 'r'},
{"ptt-file", 1, 0, 'p'}, {"ptt-file", 1, 0, 'p'},
{"dcd-file", 1, 0, 'd'}, {"dcd-file", 1, 0, 'd'},
{"ptt-type", 1, 0, 'P'}, {"ptt-type", 1, 0, 'P'},
{"dcd-type", 1, 0, 'D'}, {"dcd-type", 1, 0, 'D'},
{"serial-speed",1, 0, 's'}, {"serial-speed", 1, 0, 's'},
{"civaddr", 1, 0, 'c'}, {"civaddr", 1, 0, 'c'},
{"listen-addr", 1, 0, 'T'}, {"listen-addr", 1, 0, 'T'},
{"port", 1, 0, 't'}, {"port", 1, 0, 't'},
@ -106,7 +105,7 @@ struct handle_data {
socklen_t clilen; socklen_t clilen;
}; };
void * handle_socket(void * arg); void *handle_socket(void *arg);
void usage(void); void usage(void);
int interactive = 1; /* no cmd because of daemon */ int interactive = 1; /* no cmd because of daemon */
@ -120,7 +119,7 @@ const char *src_addr = NULL; /* INADDR_ANY */
#define MAXCONFLEN 128 #define MAXCONFLEN 128
static void handle_error (enum rig_debug_level_e lvl, const char *msg) static void handle_error(enum rig_debug_level_e lvl, const char *msg)
{ {
int e; int e;
#ifdef __MINGW32__ #ifdef __MINGW32__
@ -128,6 +127,7 @@ static void handle_error (enum rig_debug_level_e lvl, const char *msg)
lpMsgBuf = (LPVOID)"Unknown error"; lpMsgBuf = (LPVOID)"Unknown error";
e = WSAGetLastError(); e = WSAGetLastError();
if (FormatMessage( if (FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_SYSTEM |
@ -136,18 +136,19 @@ static void handle_error (enum rig_debug_level_e lvl, const char *msg)
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
// Default language // Default language
(LPTSTR)&lpMsgBuf, 0, NULL)) { (LPTSTR)&lpMsgBuf, 0, NULL)) {
rig_debug (lvl, "%s: Network error %d: %s\n", msg, e, lpMsgBuf); rig_debug(lvl, "%s: Network error %d: %s\n", msg, e, lpMsgBuf);
LocalFree(lpMsgBuf); LocalFree(lpMsgBuf);
} else { } else {
rig_debug (lvl, "%s: Network error %d\n", msg, e); rig_debug(lvl, "%s: Network error %d\n", msg, e);
} }
#else #else
e = errno; e = errno;
rig_debug (lvl, "%s: Network error %d: %s\n", msg, e, strerror (e)); rig_debug(lvl, "%s: Network error %d: %s\n", msg, e, strerror(e));
#endif #endif
} }
int main (int argc, char *argv[]) int main(int argc, char *argv[])
{ {
RIG *my_rig; /* handle to rig (instance) */ RIG *my_rig; /* handle to rig (instance) */
rig_model_t my_model = RIG_MODEL_DUMMY; rig_model_t my_model = RIG_MODEL_DUMMY;
@ -157,7 +158,7 @@ int main (int argc, char *argv[])
int verbose = 0; int verbose = 0;
int show_conf = 0; int show_conf = 0;
int dump_caps_opt = 0; int dump_caps_opt = 0;
const char *rig_file=NULL, *ptt_file=NULL, *dcd_file=NULL; const char *rig_file = NULL, *ptt_file = NULL, *dcd_file = NULL;
ptt_type_t ptt_type = RIG_PTT_NONE; ptt_type_t ptt_type = RIG_PTT_NONE;
dcd_type_t dcd_type = RIG_DCD_NONE; dcd_type_t dcd_type = RIG_DCD_NONE;
int serial_rate = 0; int serial_rate = 0;
@ -175,51 +176,63 @@ int main (int argc, char *argv[])
int c; int c;
int option_index = 0; int option_index = 0;
c = getopt_long (argc, argv, SHORT_OPTIONS, c = getopt_long(argc, argv, SHORT_OPTIONS,
long_options, &option_index); long_options, &option_index);
if (c == -1) if (c == -1)
break; break;
switch(c) { switch (c) {
case 'h': case 'h':
usage(); usage();
exit(0); exit(0);
case 'V': case 'V':
version(); version();
exit(0); exit(0);
case 'm': case 'm':
if (!optarg) { if (!optarg) {
usage(); /* wrong arg count */ usage(); /* wrong arg count */
exit(1); exit(1);
} }
my_model = atoi(optarg); my_model = atoi(optarg);
break; break;
case 'r': case 'r':
if (!optarg) { if (!optarg) {
usage(); /* wrong arg count */ usage(); /* wrong arg count */
exit(1); exit(1);
} }
rig_file = optarg; rig_file = optarg;
break; break;
case 'p': case 'p':
if (!optarg) { if (!optarg) {
usage(); /* wrong arg count */ usage(); /* wrong arg count */
exit(1); exit(1);
} }
ptt_file = optarg; ptt_file = optarg;
break; break;
case 'd': case 'd':
if (!optarg) { if (!optarg) {
usage(); /* wrong arg count */ usage(); /* wrong arg count */
exit(1); exit(1);
} }
dcd_file = optarg; dcd_file = optarg;
break; break;
case 'P': case 'P':
if (!optarg) { if (!optarg) {
usage(); /* wrong arg count */ usage(); /* wrong arg count */
exit(1); exit(1);
} }
if (!strcmp(optarg, "RIG")) if (!strcmp(optarg, "RIG"))
ptt_type = RIG_PTT_RIG; ptt_type = RIG_PTT_RIG;
else if (!strcmp(optarg, "DTR")) else if (!strcmp(optarg, "DTR"))
@ -234,12 +247,15 @@ int main (int argc, char *argv[])
ptt_type = RIG_PTT_NONE; ptt_type = RIG_PTT_NONE;
else else
ptt_type = atoi(optarg); ptt_type = atoi(optarg);
break; break;
case 'D': case 'D':
if (!optarg) { if (!optarg) {
usage(); /* wrong arg count */ usage(); /* wrong arg count */
exit(1); exit(1);
} }
if (!strcmp(optarg, "RIG")) if (!strcmp(optarg, "RIG"))
dcd_type = RIG_DCD_RIG; dcd_type = RIG_DCD_RIG;
else if (!strcmp(optarg, "DSR")) else if (!strcmp(optarg, "DSR"))
@ -254,59 +270,77 @@ int main (int argc, char *argv[])
dcd_type = RIG_DCD_NONE; dcd_type = RIG_DCD_NONE;
else else
dcd_type = atoi(optarg); dcd_type = atoi(optarg);
break; break;
case 'c': case 'c':
if (!optarg) { if (!optarg) {
usage(); /* wrong arg count */ usage(); /* wrong arg count */
exit(1); exit(1);
} }
civaddr = optarg; civaddr = optarg;
break; break;
case 's': case 's':
if (!optarg) { if (!optarg) {
usage(); /* wrong arg count */ usage(); /* wrong arg count */
exit(1); exit(1);
} }
serial_rate = atoi(optarg); serial_rate = atoi(optarg);
break; break;
case 'C': case 'C':
if (!optarg) { if (!optarg) {
usage(); /* wrong arg count */ usage(); /* wrong arg count */
exit(1); exit(1);
} }
if (*conf_parms != '\0') if (*conf_parms != '\0')
strcat(conf_parms, ","); strcat(conf_parms, ",");
strncat(conf_parms, optarg, MAXCONFLEN-strlen(conf_parms));
strncat(conf_parms, optarg, MAXCONFLEN - strlen(conf_parms));
break; break;
case 't': case 't':
if (!optarg) { if (!optarg) {
usage(); /* wrong arg count */ usage(); /* wrong arg count */
exit(1); exit(1);
} }
portno = optarg; portno = optarg;
break; break;
case 'T': case 'T':
if (!optarg) { if (!optarg) {
usage(); /* wrong arg count */ usage(); /* wrong arg count */
exit(1); exit(1);
} }
src_addr = optarg; src_addr = optarg;
break; break;
case 'o': case 'o':
vfo_mode++; vfo_mode++;
break; break;
case 'v': case 'v':
verbose++; verbose++;
break; break;
case 'L': case 'L':
show_conf++; show_conf++;
break; break;
case 'l': case 'l':
list_models(); list_models();
exit(0); exit(0);
case 'u': case 'u':
dump_caps_opt++; dump_caps_opt++;
break; break;
default: default:
usage(); /* unknown option? */ usage(); /* unknown option? */
exit(1); exit(1);
@ -343,17 +377,22 @@ int main (int argc, char *argv[])
*/ */
if (ptt_type != RIG_PTT_NONE) if (ptt_type != RIG_PTT_NONE)
my_rig->state.pttport.type.ptt = ptt_type; my_rig->state.pttport.type.ptt = ptt_type;
if (dcd_type != RIG_DCD_NONE) if (dcd_type != RIG_DCD_NONE)
my_rig->state.dcdport.type.dcd = dcd_type; my_rig->state.dcdport.type.dcd = dcd_type;
if (ptt_file) if (ptt_file)
strncpy(my_rig->state.pttport.pathname, ptt_file, FILPATHLEN - 1); strncpy(my_rig->state.pttport.pathname, ptt_file, FILPATHLEN - 1);
if (dcd_file) if (dcd_file)
strncpy(my_rig->state.dcdport.pathname, dcd_file, FILPATHLEN - 1); strncpy(my_rig->state.dcdport.pathname, dcd_file, FILPATHLEN - 1);
/* FIXME: bound checking and port type == serial */ /* FIXME: bound checking and port type == serial */
if (serial_rate != 0) if (serial_rate != 0)
my_rig->state.rigport.parm.serial.rate = serial_rate; my_rig->state.rigport.parm.serial.rate = serial_rate;
if (civaddr) if (civaddr)
rig_set_conf(my_rig, rig_token_lookup(my_rig, "civaddr"), civaddr); rig_set_conf(my_rig, rig_token_lookup(my_rig, "civaddr"), civaddr);
/* /*
* print out conf parameters * print out conf parameters
@ -375,7 +414,7 @@ int main (int argc, char *argv[])
retcode = rig_open(my_rig); retcode = rig_open(my_rig);
if (retcode != RIG_OK) { if (retcode != RIG_OK) {
fprintf(stderr,"rig_open: error = %s \n", rigerror(retcode)); fprintf(stderr, "rig_open: error = %s \n", rigerror(retcode));
exit(2); exit(2);
} }
@ -398,8 +437,9 @@ int main (int argc, char *argv[])
# endif # endif
WSADATA wsadata; WSADATA wsadata;
if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
fprintf(stderr,"WSAStartup socket error\n"); if (WSAStartup(MAKEWORD(1, 1), &wsadata) == SOCKET_ERROR) {
fprintf(stderr, "WSAStartup socket error\n");
exit(1); exit(1);
} }
@ -417,51 +457,57 @@ int main (int argc, char *argv[])
hints.ai_protocol = 0; /* Any protocol */ hints.ai_protocol = 0; /* Any protocol */
retcode = getaddrinfo(src_addr, portno, &hints, &result); retcode = getaddrinfo(src_addr, portno, &hints, &result);
if (retcode != 0) { if (retcode != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(retcode)); fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(retcode));
exit(2); exit(2);
} }
saved_result = result; saved_result = result;
do { do {
sock_listen = socket(result->ai_family, result->ai_socktype, sock_listen = socket(result->ai_family, result->ai_socktype,
result->ai_protocol); result->ai_protocol);
if (sock_listen < 0) { if (sock_listen < 0) {
handle_error (RIG_DEBUG_ERR, "socket"); handle_error(RIG_DEBUG_ERR, "socket");
freeaddrinfo(saved_result); /* No longer needed */ freeaddrinfo(saved_result); /* No longer needed */
exit(2); exit(2);
} }
if (setsockopt(sock_listen, SOL_SOCKET, SO_REUSEADDR, if (setsockopt(sock_listen, SOL_SOCKET, SO_REUSEADDR,
(char *)&reuseaddr, sizeof(reuseaddr)) < 0) { (char *)&reuseaddr, sizeof(reuseaddr)) < 0) {
handle_error (RIG_DEBUG_ERR, "setsockopt"); handle_error(RIG_DEBUG_ERR, "setsockopt");
freeaddrinfo(saved_result); /* No longer needed */ freeaddrinfo(saved_result); /* No longer needed */
exit (1); exit(1);
} }
#ifdef IPV6_V6ONLY #ifdef IPV6_V6ONLY
if (AF_INET6 == result->ai_family) { if (AF_INET6 == result->ai_family) {
/* allow IPv4 mapped to IPv6 clients Windows and BSD default /* allow IPv4 mapped to IPv6 clients Windows and BSD default
this to 1 (i.e. disallowed) and we prefer it off */ this to 1 (i.e. disallowed) and we prefer it off */
sockopt = 0; sockopt = 0;
if (setsockopt(sock_listen, IPPROTO_IPV6, IPV6_V6ONLY, if (setsockopt(sock_listen, IPPROTO_IPV6, IPV6_V6ONLY,
(char *)&sockopt, sizeof(sockopt)) < 0) { (char *)&sockopt, sizeof(sockopt)) < 0) {
handle_error (RIG_DEBUG_ERR, "setsockopt"); handle_error(RIG_DEBUG_ERR, "setsockopt");
freeaddrinfo(saved_result); /* No longer needed */ freeaddrinfo(saved_result); /* No longer needed */
exit (1); exit(1);
} }
} }
#endif #endif
if (0 == bind(sock_listen, result->ai_addr, result->ai_addrlen)) { if (0 == bind(sock_listen, result->ai_addr, result->ai_addrlen)) {
break; break;
} }
handle_error (RIG_DEBUG_WARN, "binding failed (trying next interface)"); handle_error(RIG_DEBUG_WARN, "binding failed (trying next interface)");
#ifdef __MINGW32__ #ifdef __MINGW32__
closesocket (sock_listen); closesocket(sock_listen);
#else #else
close (sock_listen); close(sock_listen);
#endif #endif
} while ((result = result->ai_next) != NULL); } while ((result = result->ai_next) != NULL);
@ -469,12 +515,12 @@ int main (int argc, char *argv[])
if (NULL == result) { if (NULL == result) {
rig_debug(RIG_DEBUG_ERR, "bind error - no available interface\n"); rig_debug(RIG_DEBUG_ERR, "bind error - no available interface\n");
exit (1); exit(1);
} }
if (listen(sock_listen, 4) < 0) { if (listen(sock_listen, 4) < 0) {
handle_error (RIG_DEBUG_ERR, "listening"); handle_error(RIG_DEBUG_ERR, "listening");
exit (1); exit(1);
} }
/* /*
@ -491,21 +537,21 @@ int main (int argc, char *argv[])
if (!arg) { if (!arg) {
rig_debug(RIG_DEBUG_ERR, "malloc: %s\n", strerror(errno)); rig_debug(RIG_DEBUG_ERR, "malloc: %s\n", strerror(errno));
exit (1); exit(1);
} }
arg->rig = my_rig; arg->rig = my_rig;
arg->clilen = sizeof (arg->cli_addr); arg->clilen = sizeof(arg->cli_addr);
arg->sock = accept(sock_listen, (struct sockaddr *)&arg->cli_addr, &arg->clilen); arg->sock = accept(sock_listen, (struct sockaddr *)&arg->cli_addr, &arg->clilen);
if (arg->sock < 0) { if (arg->sock < 0) {
handle_error (RIG_DEBUG_ERR, "accept"); handle_error(RIG_DEBUG_ERR, "accept");
break; break;
} }
if ((retcode = getnameinfo ((struct sockaddr const *)&arg->cli_addr, arg->clilen, host, if ((retcode = getnameinfo((struct sockaddr const *)&arg->cli_addr, arg->clilen, host,
sizeof (host), serv, sizeof (serv), NI_NOFQDN)) < 0) { sizeof(host), serv, sizeof(serv), NI_NOFQDN)) < 0) {
rig_debug (RIG_DEBUG_WARN, "Peer lookup error: %s", gai_strerror (retcode)); 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); rig_debug(RIG_DEBUG_VERBOSE, "Connection opened from %s:%s\n", host, serv);
@ -520,6 +566,7 @@ int main (int argc, char *argv[])
rig_debug(RIG_DEBUG_ERR, "pthread_create: %s\n", strerror(retcode)); rig_debug(RIG_DEBUG_ERR, "pthread_create: %s\n", strerror(retcode));
break; break;
} }
#else #else
handle_socket(arg); handle_socket(arg);
#endif #endif
@ -539,7 +586,7 @@ int main (int argc, char *argv[])
/* /*
* This is the function run by the threads * This is the function run by the threads
*/ */
void * handle_socket(void *arg) void *handle_socket(void *arg)
{ {
struct handle_data *handle_data_arg = (struct handle_data *)arg; struct handle_data *handle_data_arg = (struct handle_data *)arg;
FILE *fsockin; FILE *fsockin;
@ -560,6 +607,7 @@ void * handle_socket(void *arg)
#else #else
fsockin = fdopen(handle_data_arg->sock, "rb"); fsockin = fdopen(handle_data_arg->sock, "rb");
#endif #endif
if (!fsockin) { if (!fsockin) {
rig_debug(RIG_DEBUG_ERR, "fdopen in: %s\n", strerror(errno)); rig_debug(RIG_DEBUG_ERR, "fdopen in: %s\n", strerror(errno));
goto handle_exit; goto handle_exit;
@ -570,6 +618,7 @@ void * handle_socket(void *arg)
#else #else
fsockout = fdopen(handle_data_arg->sock, "wb"); fsockout = fdopen(handle_data_arg->sock, "wb");
#endif #endif
if (!fsockout) { if (!fsockout) {
rig_debug(RIG_DEBUG_ERR, "fdopen out: %s\n", strerror(errno)); rig_debug(RIG_DEBUG_ERR, "fdopen out: %s\n", strerror(errno));
fclose(fsockin); fclose(fsockin);
@ -579,14 +628,15 @@ void * handle_socket(void *arg)
do { do {
retcode = rigctl_parse(handle_data_arg->rig, fsockin, fsockout, NULL, 0); retcode = rigctl_parse(handle_data_arg->rig, fsockin, fsockout, NULL, 0);
if (ferror(fsockin) || ferror(fsockout)) if (ferror(fsockin) || ferror(fsockout))
retcode = 1; retcode = 1;
} while (retcode == 0 || retcode == 2); } while (retcode == 0 || retcode == 2);
if ((retcode = getnameinfo ((struct sockaddr const *)&handle_data_arg->cli_addr, if ((retcode = getnameinfo((struct sockaddr const *)&handle_data_arg->cli_addr,
handle_data_arg->clilen, host, sizeof (host), handle_data_arg->clilen, host, sizeof(host),
serv, sizeof (serv), NI_NOFQDN)) < 0) { serv, sizeof(serv), NI_NOFQDN)) < 0) {
rig_debug (RIG_DEBUG_WARN, "Peer lookup error: %s", gai_strerror (retcode)); rig_debug(RIG_DEBUG_WARN, "Peer lookup error: %s", gai_strerror(retcode));
} }
rig_debug(RIG_DEBUG_VERBOSE, "Connection closed from %s:%s\n", rig_debug(RIG_DEBUG_VERBOSE, "Connection closed from %s:%s\n",
@ -636,7 +686,7 @@ void usage(void)
" -v, --verbose set verbose mode, cumulative\n" " -v, --verbose set verbose mode, cumulative\n"
" -h, --help display this help and exit\n" " -h, --help display this help and exit\n"
" -V, --version output version information and exit\n\n", " -V, --version output version information and exit\n\n",
portno ); portno);
usage_rig(stdout); usage_rig(stdout);

Wyświetl plik

@ -72,7 +72,7 @@ struct handle_data {
socklen_t clilen; socklen_t clilen;
}; };
void * handle_socket(void * arg); void *handle_socket(void *arg);
void usage(); void usage();
@ -83,11 +83,10 @@ void usage();
* TODO: add an option to read from a file * TODO: add an option to read from a file
*/ */
#define SHORT_OPTIONS "m:r:s:C:t:T:LuvhVl" #define SHORT_OPTIONS "m:r:s:C:t:T:LuvhVl"
static struct option long_options[] = static struct option long_options[] = {
{
{"model", 1, 0, 'm'}, {"model", 1, 0, 'm'},
{"rot-file", 1, 0, 'r'}, {"rot-file", 1, 0, 'r'},
{"serial-speed",1, 0, 's'}, {"serial-speed", 1, 0, 's'},
{"port", 1, 0, 't'}, {"port", 1, 0, 't'},
{"listen-addr", 1, 0, 'T'}, {"listen-addr", 1, 0, 'T'},
{"list", 0, 0, 'l'}, {"list", 0, 0, 'l'},
@ -101,7 +100,7 @@ static struct option long_options[] =
}; };
int interactive = 1; /* no cmd because of daemon */ int interactive = 1; /* no cmd because of daemon */
int prompt= 0 ; /* Daemon mode for rigparse return string */ int prompt = 0 ; /* Daemon mode for rigparse return string */
const char *portno = "4533"; const char *portno = "4533";
const char *src_addr = NULL; /* INADDR_ANY */ const char *src_addr = NULL; /* INADDR_ANY */
@ -111,7 +110,7 @@ char send_cmd_term = '\r'; /* send_cmd termination char */
#define MAXCONFLEN 128 #define MAXCONFLEN 128
static void handle_error (enum rig_debug_level_e lvl, const char *msg) static void handle_error(enum rig_debug_level_e lvl, const char *msg)
{ {
int e; int e;
#ifdef __MINGW32__ #ifdef __MINGW32__
@ -128,18 +127,19 @@ static void handle_error (enum rig_debug_level_e lvl, const char *msg)
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
// Default language // Default language
(LPTSTR)&lpMsgBuf, 0, NULL)) { (LPTSTR)&lpMsgBuf, 0, NULL)) {
rig_debug (lvl, "%s: Network error %d: %s\n", msg, e, lpMsgBuf); rig_debug(lvl, "%s: Network error %d: %s\n", msg, e, lpMsgBuf);
LocalFree(lpMsgBuf); LocalFree(lpMsgBuf);
} else { } else {
rig_debug (lvl, "%s: Network error %d\n", msg, e); rig_debug(lvl, "%s: Network error %d\n", msg, e);
} }
#else #else
e = errno; e = errno;
rig_debug (lvl, "%s: Network error %d: %s\n", msg, e, strerror (e)); rig_debug(lvl, "%s: Network error %d: %s\n", msg, e, strerror(e));
#endif #endif
} }
int main (int argc, char *argv[]) int main(int argc, char *argv[])
{ {
ROT *my_rot; /* handle to rot (instance) */ ROT *my_rot; /* handle to rot (instance) */
rot_model_t my_model = ROT_MODEL_DUMMY; rot_model_t my_model = ROT_MODEL_DUMMY;
@ -149,7 +149,7 @@ int main (int argc, char *argv[])
int verbose = 0; int verbose = 0;
int show_conf = 0; int show_conf = 0;
int dump_caps_opt = 0; int dump_caps_opt = 0;
const char *rot_file=NULL; const char *rot_file = NULL;
int serial_rate = 0; int serial_rate = 0;
char conf_parms[MAXCONFLEN] = ""; char conf_parms[MAXCONFLEN] = "";
@ -160,78 +160,98 @@ int main (int argc, char *argv[])
char host[NI_MAXHOST]; char host[NI_MAXHOST];
char serv[NI_MAXSERV]; char serv[NI_MAXSERV];
while(1) { while (1) {
int c; int c;
int option_index = 0; int option_index = 0;
c = getopt_long(argc, argv, SHORT_OPTIONS, c = getopt_long(argc, argv, SHORT_OPTIONS,
long_options, &option_index); long_options, &option_index);
if (c == -1) if (c == -1)
break; break;
switch(c) { switch (c) {
case 'h': case 'h':
usage(); usage();
exit(0); exit(0);
case 'V': case 'V':
version(); version();
exit(0); exit(0);
case 'm': case 'm':
if (!optarg) { if (!optarg) {
usage(); /* wrong arg count */ usage(); /* wrong arg count */
exit(1); exit(1);
} }
my_model = atoi(optarg); my_model = atoi(optarg);
break; break;
case 'r': case 'r':
if (!optarg) { if (!optarg) {
usage(); /* wrong arg count */ usage(); /* wrong arg count */
exit(1); exit(1);
} }
rot_file = optarg; rot_file = optarg;
break; break;
case 's': case 's':
if (!optarg) { if (!optarg) {
usage(); /* wrong arg count */ usage(); /* wrong arg count */
exit(1); exit(1);
} }
serial_rate = atoi(optarg); serial_rate = atoi(optarg);
break; break;
case 'C': case 'C':
if (!optarg) { if (!optarg) {
usage(); /* wrong arg count */ usage(); /* wrong arg count */
exit(1); exit(1);
} }
if (*conf_parms != '\0') if (*conf_parms != '\0')
strcat(conf_parms, ","); strcat(conf_parms, ",");
strncat(conf_parms, optarg, MAXCONFLEN-strlen(conf_parms));
strncat(conf_parms, optarg, MAXCONFLEN - strlen(conf_parms));
break; break;
case 't': case 't':
if (!optarg) { if (!optarg) {
usage(); /* wrong arg count */ usage(); /* wrong arg count */
exit(1); exit(1);
} }
portno = optarg; portno = optarg;
break; break;
case 'T': case 'T':
if (!optarg) { if (!optarg) {
usage(); /* wrong arg count */ usage(); /* wrong arg count */
exit(1); exit(1);
} }
src_addr = optarg; src_addr = optarg;
break; break;
case 'v': case 'v':
verbose++; verbose++;
break; break;
case 'L': case 'L':
show_conf++; show_conf++;
break; break;
case 'l': case 'l':
list_models(); list_models();
exit(0); exit(0);
case 'u': case 'u':
dump_caps_opt++; dump_caps_opt++;
break; break;
default: default:
usage(); /* unknown option? */ usage(); /* unknown option? */
exit(1); exit(1);
@ -287,7 +307,7 @@ int main (int argc, char *argv[])
retcode = rot_open(my_rot); retcode = rot_open(my_rot);
if (retcode != RIG_OK) { if (retcode != RIG_OK) {
fprintf(stderr,"rot_open: error = %s \n", rigerror(retcode)); fprintf(stderr, "rot_open: error = %s \n", rigerror(retcode));
exit(2); exit(2);
} }
@ -311,8 +331,8 @@ int main (int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) { if (WSAStartup(MAKEWORD(1, 1), &wsadata) == SOCKET_ERROR) {
fprintf(stderr,"WSAStartup socket error\n"); fprintf(stderr, "WSAStartup socket error\n");
exit(1); exit(1);
} }
@ -330,6 +350,7 @@ int main (int argc, char *argv[])
hints.ai_protocol = 0; /* Any protocol */ hints.ai_protocol = 0; /* Any protocol */
retcode = getaddrinfo(src_addr, portno, &hints, &result); retcode = getaddrinfo(src_addr, portno, &hints, &result);
if (retcode != 0) { if (retcode != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(retcode)); fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(retcode));
exit(2); exit(2);
@ -342,19 +363,20 @@ int main (int argc, char *argv[])
result->ai_protocol); result->ai_protocol);
if (sock_listen < 0) { if (sock_listen < 0) {
handle_error (RIG_DEBUG_ERR, "socket"); handle_error(RIG_DEBUG_ERR, "socket");
freeaddrinfo(result); /* No longer needed */ freeaddrinfo(result); /* No longer needed */
exit(1); exit(1);
} }
if (setsockopt(sock_listen, SOL_SOCKET, SO_REUSEADDR, if (setsockopt(sock_listen, SOL_SOCKET, SO_REUSEADDR,
(char *)&reuseaddr,sizeof(reuseaddr)) < 0) { (char *)&reuseaddr, sizeof(reuseaddr)) < 0) {
handle_error (RIG_DEBUG_ERR, "setsockopt"); handle_error(RIG_DEBUG_ERR, "setsockopt");
freeaddrinfo(result); /* No longer needed */ freeaddrinfo(result); /* No longer needed */
exit (1); exit(1);
} }
#ifdef IPV6_V6ONLY #ifdef IPV6_V6ONLY
if (AF_INET6 == result->ai_family) { if (AF_INET6 == result->ai_family) {
/* allow IPv4 mapped to IPv6 clients, MS & BSD default this /* allow IPv4 mapped to IPv6 clients, MS & BSD default this
to 1 i.e. disallowed */ to 1 i.e. disallowed */
@ -362,21 +384,23 @@ int main (int argc, char *argv[])
if (setsockopt(sock_listen, IPPROTO_IPV6, IPV6_V6ONLY, if (setsockopt(sock_listen, IPPROTO_IPV6, IPV6_V6ONLY,
(char *)&sockopt, sizeof(sockopt)) < 0) { (char *)&sockopt, sizeof(sockopt)) < 0) {
handle_error (RIG_DEBUG_ERR, "setsockopt"); handle_error(RIG_DEBUG_ERR, "setsockopt");
freeaddrinfo(saved_result); /* No longer needed */ freeaddrinfo(saved_result); /* No longer needed */
exit (1); exit(1);
} }
} }
#endif #endif
if (0 == bind(sock_listen, result->ai_addr, result->ai_addrlen)) { if (0 == bind(sock_listen, result->ai_addr, result->ai_addrlen)) {
break; break;
} }
handle_error (RIG_DEBUG_WARN, "binding failed (trying next interface)");
handle_error(RIG_DEBUG_WARN, "binding failed (trying next interface)");
#ifdef __MINGW32__ #ifdef __MINGW32__
closesocket (sock_listen); closesocket(sock_listen);
#else #else
close (sock_listen); close(sock_listen);
#endif #endif
} while ((result = result->ai_next) != NULL); } while ((result = result->ai_next) != NULL);
@ -384,12 +408,12 @@ int main (int argc, char *argv[])
if (NULL == result) { if (NULL == result) {
rig_debug(RIG_DEBUG_ERR, "bind error - no available interface\n"); rig_debug(RIG_DEBUG_ERR, "bind error - no available interface\n");
exit (1); exit(1);
} }
if (listen(sock_listen, 4) < 0) { if (listen(sock_listen, 4) < 0) {
handle_error (RIG_DEBUG_ERR, "listening"); handle_error(RIG_DEBUG_ERR, "listening");
exit (1); exit(1);
} }
/* /*
@ -406,7 +430,7 @@ int main (int argc, char *argv[])
if (!arg) { if (!arg) {
rig_debug(RIG_DEBUG_ERR, "malloc: %s\n", strerror(errno)); rig_debug(RIG_DEBUG_ERR, "malloc: %s\n", strerror(errno));
exit (1); exit(1);
} }
arg->rot = my_rot; arg->rot = my_rot;
@ -415,14 +439,14 @@ int main (int argc, char *argv[])
&arg->clilen); &arg->clilen);
if (arg->sock < 0) { if (arg->sock < 0) {
handle_error (RIG_DEBUG_ERR, "accept"); handle_error(RIG_DEBUG_ERR, "accept");
break; break;
} }
if ((retcode = getnameinfo ((struct sockaddr const *)&arg->cli_addr, arg->clilen, if ((retcode = getnameinfo((struct sockaddr const *)&arg->cli_addr, arg->clilen,
host, sizeof (host), serv, sizeof (serv), host, sizeof(host), serv, sizeof(serv),
NI_NOFQDN)) < 0) { NI_NOFQDN)) < 0) {
rig_debug (RIG_DEBUG_WARN, "Peer lookup error: %s", gai_strerror (retcode)); rig_debug(RIG_DEBUG_WARN, "Peer lookup error: %s", gai_strerror(retcode));
} }
rig_debug(RIG_DEBUG_VERBOSE, "Connection opened from %s:%s\n", rig_debug(RIG_DEBUG_VERBOSE, "Connection opened from %s:%s\n",
@ -438,6 +462,7 @@ int main (int argc, char *argv[])
rig_debug(RIG_DEBUG_ERR, "pthread_create: %s\n", strerror(retcode)); rig_debug(RIG_DEBUG_ERR, "pthread_create: %s\n", strerror(retcode));
break; break;
} }
#else #else
handle_socket(arg); handle_socket(arg);
#endif #endif
@ -457,7 +482,7 @@ int main (int argc, char *argv[])
/* /*
* This is the function run by the threads * This is the function run by the threads
*/ */
void * handle_socket(void *arg) void *handle_socket(void *arg)
{ {
struct handle_data *handle_data_arg = (struct handle_data *)arg; struct handle_data *handle_data_arg = (struct handle_data *)arg;
FILE *fsockin; FILE *fsockin;
@ -478,6 +503,7 @@ void * handle_socket(void *arg)
#else #else
fsockin = fdopen(handle_data_arg->sock, "rb"); fsockin = fdopen(handle_data_arg->sock, "rb");
#endif #endif
if (!fsockin) { if (!fsockin) {
rig_debug(RIG_DEBUG_ERR, "fdopen in: %s\n", strerror(errno)); rig_debug(RIG_DEBUG_ERR, "fdopen in: %s\n", strerror(errno));
goto handle_exit; goto handle_exit;
@ -488,6 +514,7 @@ void * handle_socket(void *arg)
#else #else
fsockout = fdopen(handle_data_arg->sock, "wb"); fsockout = fdopen(handle_data_arg->sock, "wb");
#endif #endif
if (!fsockout) { if (!fsockout) {
rig_debug(RIG_DEBUG_ERR, "fdopen out: %s\n", strerror(errno)); rig_debug(RIG_DEBUG_ERR, "fdopen out: %s\n", strerror(errno));
fclose(fsockin); fclose(fsockin);
@ -496,14 +523,15 @@ void * handle_socket(void *arg)
do { do {
retcode = rotctl_parse(handle_data_arg->rot, fsockin, fsockout, NULL, 0); retcode = rotctl_parse(handle_data_arg->rot, fsockin, fsockout, NULL, 0);
if (ferror(fsockin) || ferror(fsockout)) if (ferror(fsockin) || ferror(fsockout))
retcode = 1; retcode = 1;
} while (retcode == 0 || retcode == 2); } while (retcode == 0 || retcode == 2);
if ((retcode = getnameinfo ((struct sockaddr const *)&handle_data_arg->cli_addr, if ((retcode = getnameinfo((struct sockaddr const *)&handle_data_arg->cli_addr,
handle_data_arg->clilen, host, sizeof (host), handle_data_arg->clilen, host, sizeof(host),
serv, sizeof (serv), NI_NOFQDN)) < 0) { serv, sizeof(serv), NI_NOFQDN)) < 0) {
rig_debug (RIG_DEBUG_WARN, "Peer lookup error: %s", gai_strerror (retcode)); rig_debug(RIG_DEBUG_WARN, "Peer lookup error: %s", gai_strerror(retcode));
} }
rig_debug(RIG_DEBUG_VERBOSE, "Connection closed from %s:%s\n", rig_debug(RIG_DEBUG_VERBOSE, "Connection closed from %s:%s\n",