Skip to content

Commit 99d4d90

Browse files
committed
KISS over TCP behaved strangely with multiple client apps attached.
1 parent 2328ecd commit 99d4d90

File tree

9 files changed

+62
-151
lines changed

9 files changed

+62
-151
lines changed

aclients.c

+4-17
Original file line numberDiff line numberDiff line change
@@ -549,12 +549,7 @@ static void * client_thread_net (void *arg)
549549

550550
mon_cmd.kind_lo = 'k';
551551

552-
#if __WIN32__
553-
send (server_sock, (char*)(&mon_cmd), sizeof(mon_cmd), 0);
554-
#else
555-
err = write (server_sock, (char*)(&mon_cmd), sizeof(mon_cmd));
556-
#endif
557-
552+
SOCK_SEND (server_sock, (char*)(&mon_cmd), sizeof(mon_cmd));
558553

559554
/*
560555
* Print all of the monitored packets.
@@ -563,14 +558,10 @@ static void * client_thread_net (void *arg)
563558
while (1) {
564559
int n;
565560

566-
#if __WIN32__
567-
n = recv (server_sock, (char*)(&mon_cmd), sizeof(mon_cmd), 0);
568-
#else
569-
n = read (server_sock, (char*)(&mon_cmd), sizeof(mon_cmd));
570-
#endif
561+
n = SOCK_RECV (server_sock, (char*)(&mon_cmd), sizeof(mon_cmd));
571562

572563
if (n != sizeof(mon_cmd)) {
573-
printf ("Read error, client %d received %d command bytes.\n", my_index, n);
564+
printf ("Read error, client %d received %d command bytes. Terminating.\n", my_index, n);
574565
exit (1);
575566
}
576567

@@ -581,11 +572,7 @@ static void * client_thread_net (void *arg)
581572
assert (mon_cmd.data_len >= 0 && mon_cmd.data_len < (int)(sizeof(data)));
582573

583574
if (mon_cmd.data_len > 0) {
584-
#if __WIN32__
585-
n = recv (server_sock, data, mon_cmd.data_len, 0);
586-
#else
587-
n = read (server_sock, data, mon_cmd.data_len);
588-
#endif
575+
n = SOCK_RECV (server_sock, data, mon_cmd.data_len);
589576

590577
if (n != mon_cmd.data_len) {
591578
printf ("Read error, client %d received %d data bytes.\n", my_index, n);

audio_win.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ int audio_get (int a)
850850

851851
assert (A->udp_sock > 0);
852852

853-
res = recv (A->udp_sock, A->stream_data, SDR_UDP_BUF_MAXLEN, 0);
853+
res = SOCK_RECV (A->udp_sock, A->stream_data, SDR_UDP_BUF_MAXLEN);
854854
if (res <= 0) {
855855
text_color_set(DW_COLOR_ERROR);
856856
dw_printf ("Can't read from udp socket, errno %d", WSAGetLastError());

direwolf.h

+12
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,18 @@ typedef pthread_mutex_t dw_mutex_t;
249249

250250

251251

252+
// Formerly used write/read on Linux, for some forgotten reason,
253+
// but always using send/recv makes more sense.
254+
// Need option to prevent a SIGPIPE signal on Linux. (added for 1.5 beta 2)
255+
256+
#if __WIN32__
257+
#define SOCK_SEND(s,data,size) send(s,data,size,0)
258+
#else
259+
#define SOCK_SEND(s,data,size) send(s,data,size, MSG_NOSIGNAL)
260+
#endif
261+
#define SOCK_RECV(s,data,size) recv(s,data,size,0)
262+
263+
252264
/* Platform differences for string functions. */
253265

254266

igate.c

+3-7
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ static void send_msg_to_server (const char *imsg, int imsg_len)
12661266

12671267

12681268
#if __WIN32__
1269-
err = send (igate_sock, stemp, stemp_len, 0);
1269+
err = SOCK_SEND (igate_sock, stemp, stemp_len);
12701270
if (err == SOCKET_ERROR)
12711271
{
12721272
text_color_set(DW_COLOR_ERROR);
@@ -1277,7 +1277,7 @@ static void send_msg_to_server (const char *imsg, int imsg_len)
12771277
WSACleanup();
12781278
}
12791279
#else
1280-
err = write (igate_sock, stemp, stemp_len);
1280+
err = SOCK_SEND (igate_sock, stemp, stemp_len);
12811281
if (err <= 0)
12821282
{
12831283
text_color_set(DW_COLOR_ERROR);
@@ -1319,11 +1319,7 @@ static int get1ch (void)
13191319
// TODO: might read complete packets and unpack from own buffer
13201320
// rather than using a system call for each byte.
13211321

1322-
#if __WIN32__
1323-
n = recv (igate_sock, (char*)(&ch), 1, 0);
1324-
#else
1325-
n = read (igate_sock, &ch, 1);
1326-
#endif
1322+
n = SOCK_RECV (igate_sock, (char*)(&ch), 1);
13271323

13281324
if (n == 1) {
13291325
#if DEBUG9

kiss.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ void kisspt_init (struct misc_config_s *mc)
220220
}
221221
}
222222
else {
223-
text_color_set(DW_COLOR_INFO);
224-
dw_printf ("Use -p command line option to enable KISS pseudo terminal.\n");
223+
//text_color_set(DW_COLOR_INFO);
224+
//dw_printf ("Use -p command line option to enable KISS pseudo terminal.\n");
225225
}
226226

227227

kissnet.c

+20-70
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ void kissnet_init (struct misc_config_s *mc)
208208
pthread_t cmd_listen_tid[MAX_NET_CLIENTS];
209209
int e;
210210
#endif
211-
int kiss_port = mc->kiss_port;
211+
int kiss_port = mc->kiss_port; /* default 8001 but easily changed. */
212212

213213

214214
#if DEBUG
@@ -258,14 +258,16 @@ void kissnet_init (struct misc_config_s *mc)
258258
cmd_listen_th[client] = (HANDLE)_beginthreadex (NULL, 0, kissnet_listen_thread, (void*)client, 0, NULL);
259259
if (cmd_listen_th[client] == NULL) {
260260
text_color_set(DW_COLOR_ERROR);
261-
dw_printf ("Could not create KISS socket command listening thread\n");
261+
dw_printf ("Could not create KISS command listening thread for client %d\n", client);
262262
return;
263263
}
264264
#else
265-
e = pthread_create (&(cmd_listen_tid[client]), NULL, kissnet_listen_thread, NULL);
265+
e = pthread_create (&(cmd_listen_tid[client]), NULL, kissnet_listen_thread, (void *)(long)client);
266266
if (e != 0) {
267267
text_color_set(DW_COLOR_ERROR);
268-
perror("Could not create KISS socket command listening thread");
268+
dw_printf ("Could not create KISS command listening thread for client %d\n", client);
269+
// Replace add perror with better message handling.
270+
perror("");
269271
return;
270272
}
271273
#endif
@@ -589,6 +591,10 @@ void kissnet_send_rec_packet (int chan, int kiss_cmd, unsigned char *fbuf, int f
589591
text_color_set(DW_COLOR_ERROR);
590592
dw_printf ("KISS TCP: Something unexpected from client application.\n");
591593
dw_printf ("Is client app treating this like an old TNC with command mode?\n");
594+
dw_printf ("This can be caused by the application sending commands to put a\n");
595+
dw_printf ("traditional TNC into KISS mode. It is usually a harmless warning.\n");
596+
dw_printf ("For best results, configure for a KISS-only TNC to avoid this.\n");
597+
dw_printf ("In the case of APRSISCE/32, use \"Simply(KISS)\" rather than \"KISS.\"\n");
592598

593599
flen = strlen((char*)fbuf);
594600
if (kiss_debug) {
@@ -623,21 +629,21 @@ void kissnet_send_rec_packet (int chan, int kiss_cmd, unsigned char *fbuf, int f
623629
}
624630

625631
#if __WIN32__
626-
err = send (client_sock[client], (char*)kiss_buff, kiss_len, 0);
632+
err = SOCK_SEND(client_sock[client], (char*)kiss_buff, kiss_len);
627633
if (err == SOCKET_ERROR)
628634
{
629635
text_color_set(DW_COLOR_ERROR);
630-
dw_printf ("\nError %d sending message to KISS client application. Closing connection.\n\n", WSAGetLastError());
636+
dw_printf ("\nError %d sending message to KISS client %d application. Closing connection.\n\n", WSAGetLastError(), client);
631637
closesocket (client_sock[client]);
632638
client_sock[client] = -1;
633639
WSACleanup();
634640
}
635641
#else
636-
err = write (client_sock[client], kiss_buff, kiss_len);
642+
err = SOCK_SEND (client_sock[client], kiss_buff, kiss_len);
637643
if (err <= 0)
638644
{
639645
text_color_set(DW_COLOR_ERROR);
640-
dw_printf ("\nError sending message to KISS client application. Closing connection.\n\n");
646+
dw_printf ("\nError sending message to KISS client %d application. Closing connection.\n\n", client);
641647
close (client_sock[client]);
642648
client_sock[client] = -1;
643649
}
@@ -649,63 +655,6 @@ void kissnet_send_rec_packet (int chan, int kiss_cmd, unsigned char *fbuf, int f
649655

650656

651657

652-
/*-------------------------------------------------------------------
653-
*
654-
* Name: read_from_socket
655-
*
656-
* Purpose: Read from socket until we have desired number of bytes.
657-
*
658-
* Inputs: fd - file descriptor.
659-
* ptr - address where data should be placed.
660-
* len - desired number of bytes.
661-
*
662-
* Description: Just a wrapper for the "read" system call but it should
663-
* never return fewer than the desired number of bytes.
664-
*
665-
* Not really needed for KISS because we are dealing with
666-
* a stream of bytes rather than message blocks.
667-
*
668-
*--------------------------------------------------------------------*/
669-
670-
static int read_from_socket (int fd, char *ptr, int len)
671-
{
672-
int got_bytes = 0;
673-
674-
#if DEBUG
675-
text_color_set(DW_COLOR_DEBUG);
676-
dw_printf ("read_from_socket (%d, %p, %d)\n", fd, ptr, len);
677-
#endif
678-
while (got_bytes < len) {
679-
int n;
680-
681-
#if __WIN32__
682-
n = recv (fd, ptr + got_bytes, len - got_bytes, 0);
683-
#else
684-
n = read (fd, ptr + got_bytes, len - got_bytes);
685-
#endif
686-
687-
//Would be useful to have more detailed explanation from the error code.
688-
689-
#if DEBUG
690-
text_color_set(DW_COLOR_DEBUG);
691-
dw_printf ("read_from_socket: n = %d\n", n);
692-
#endif
693-
if (n <= 0) {
694-
return (n);
695-
}
696-
697-
got_bytes += n;
698-
}
699-
assert (got_bytes >= 0 && got_bytes <= len);
700-
701-
#if DEBUG
702-
text_color_set(DW_COLOR_DEBUG);
703-
dw_printf ("read_from_socket: return %d\n", got_bytes);
704-
#endif
705-
return (got_bytes);
706-
}
707-
708-
709658
/*-------------------------------------------------------------------
710659
*
711660
* Name: kissnet_listen_thread
@@ -739,7 +688,7 @@ static int kiss_get (int client)
739688

740689
/* Just get one byte at a time. */
741690

742-
n = read_from_socket (client_sock[client], (char *)(&ch), 1);
691+
n = SOCK_RECV (client_sock[client], (char *)(&ch), 1);
743692

744693
if (n == 1) {
745694
#if DEBUG9
@@ -759,7 +708,7 @@ static int kiss_get (int client)
759708
}
760709

761710
text_color_set(DW_COLOR_ERROR);
762-
dw_printf ("\nError reading KISS byte from client application %d. Closing connection.\n\n", client);
711+
dw_printf ("\nKISS client application %d has gone away.\n\n", client);
763712
#if __WIN32__
764713
closesocket (client_sock[client]);
765714
#else
@@ -775,13 +724,14 @@ static THREAD_F kissnet_listen_thread (void *arg)
775724
{
776725
unsigned char ch;
777726

727+
728+
int client = (int)(long)arg;
729+
778730
#if DEBUG
779731
text_color_set(DW_COLOR_DEBUG);
780-
dw_printf ("kissnet_listen_thread ( socket = %d )\n", client_sock);
732+
dw_printf ("kissnet_listen_thread ( client = %d, socket fd = %d )\n", client, client_sock[client]);
781733
#endif
782734

783-
int client = (int)(long)arg;
784-
785735
assert (client >= 0 && client < MAX_NET_CLIENTS);
786736

787737

kissutil.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,6 @@ static THREAD_F tnc_listen_serial (void *arg);
9090
static void send_to_kiss_tnc (int chan, int cmd, char *data, int dlen);
9191
static void hex_dump (unsigned char *p, int len);
9292

93-
// Formerly used write/read on Linux, for some forgotten reason,
94-
// but making them the same seems to make more sense.
95-
#define SOCK_SEND(s,data,size) send(s,data,size,0)
96-
#define SOCK_RECV(s,data,size) recv(s,data,size,0)
97-
98-
9993
static void usage(void);
10094
static void usage2(void);
10195

@@ -809,6 +803,9 @@ void kiss_process_msg (unsigned char *kiss_msg, int kiss_len, int debug, int cli
809803

810804
ax25_safe_print ((char *)pinfo, info_len, 0);
811805
dw_printf ("\n");
806+
#if __WIN32__
807+
fflush (stdout);
808+
#endif
812809

813810
/*
814811
* Add to receive queue directory if specified.

0 commit comments

Comments
 (0)