2015-07-27 00:35:07 +00:00
|
|
|
//
|
|
|
|
// This file is part of Dire Wolf, an amateur radio packet TNC.
|
2021-01-05 00:43:00 +00:00
|
|
|
// Copyright (C) 2011-2014, 2015, 2017, 2021 John Langner, WB2OSZ
|
2015-07-27 00:35:07 +00:00
|
|
|
//
|
|
|
|
// 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Module: kissnet.c
|
|
|
|
*
|
|
|
|
* Purpose: Provide service to other applications via KISS protocol via TCP socket.
|
|
|
|
*
|
|
|
|
* Input:
|
|
|
|
*
|
|
|
|
* Outputs:
|
|
|
|
*
|
|
|
|
* Description: This provides a TCP socket for communication with a client application.
|
|
|
|
*
|
|
|
|
* It implements the KISS TNS protocol as described in:
|
|
|
|
* http://www.ka9q.net/papers/kiss.html
|
|
|
|
*
|
|
|
|
* Briefly, a frame is composed of
|
|
|
|
*
|
|
|
|
* * FEND (0xC0)
|
|
|
|
* * Contents - with special escape sequences so a 0xc0
|
|
|
|
* byte in the data is not taken as end of frame.
|
|
|
|
* as part of the data.
|
|
|
|
* * FEND
|
|
|
|
*
|
|
|
|
* The first byte of the frame contains:
|
|
|
|
*
|
|
|
|
* * port number in upper nybble.
|
|
|
|
* * command in lower nybble.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Commands from application recognized:
|
|
|
|
*
|
2017-05-03 21:41:37 +00:00
|
|
|
* _0 Data Frame AX.25 frame in raw format.
|
2015-07-27 00:35:07 +00:00
|
|
|
*
|
2017-05-03 21:41:37 +00:00
|
|
|
* _1 TXDELAY See explanation in xmit.c.
|
2015-07-27 00:35:07 +00:00
|
|
|
*
|
2017-05-03 21:41:37 +00:00
|
|
|
* _2 Persistence " "
|
2015-07-27 00:35:07 +00:00
|
|
|
*
|
2017-05-03 21:41:37 +00:00
|
|
|
* _3 SlotTime " "
|
2015-07-27 00:35:07 +00:00
|
|
|
*
|
2017-05-03 21:41:37 +00:00
|
|
|
* _4 TXtail " "
|
2015-07-27 00:35:07 +00:00
|
|
|
* Spec says it is obsolete but Xastir
|
|
|
|
* sends it and we respect it.
|
|
|
|
*
|
2017-05-03 21:41:37 +00:00
|
|
|
* _5 FullDuplex Ignored.
|
2015-07-27 00:35:07 +00:00
|
|
|
*
|
2017-05-03 21:41:37 +00:00
|
|
|
* _6 SetHardware TNC specific.
|
2015-07-27 00:35:07 +00:00
|
|
|
*
|
|
|
|
* FF Return Exit KISS mode. Ignored.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Messages sent to client application:
|
|
|
|
*
|
2017-05-03 21:41:37 +00:00
|
|
|
* _0 Data Frame Received AX.25 frame in raw format.
|
2015-07-27 00:35:07 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* References: Getting Started with Winsock
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/desktop/bb530742(v=vs.85).aspx
|
|
|
|
*
|
|
|
|
* Future: Originally we had:
|
|
|
|
* KISS over serial port.
|
|
|
|
* AGW over socket.
|
|
|
|
* This is the two of them munged together and we end up with duplicate code.
|
|
|
|
* It would have been better to separate out the transport and application layers.
|
|
|
|
* Maybe someday.
|
|
|
|
*
|
|
|
|
*---------------------------------------------------------------*/
|
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
/*
|
|
|
|
Separate TCP ports per radio:
|
|
|
|
|
|
|
|
An increasing number of people are using multiple radios.
|
|
|
|
direwolf is capable of handling many radio channels and
|
|
|
|
provides cross-band repeating, etc.
|
|
|
|
Maybe a single stereo audio interface is used for 2 radios.
|
|
|
|
|
|
|
|
+------------+ tcp 8001, all channels
|
|
|
|
Radio A -------- | | -------------------------- Application A
|
|
|
|
| direwolf |
|
|
|
|
Radio B -------- | | -------------------------- Application B
|
|
|
|
+------------+ tcp 8001, all channels
|
|
|
|
|
|
|
|
The KISS protocol has a 4 bit field for the TNC port (which I prefer to
|
|
|
|
call channel because port has too many different meanings).
|
|
|
|
direwolf handles this fine. However, most applications were written assuming
|
|
|
|
that a TNC could only talk to a single radio. On reception, they ignore the
|
|
|
|
channel in the KISS frame. For transmit, the channel is always set to 0.
|
|
|
|
|
|
|
|
Many people are using the work-around of two separate instances of direwolf.
|
|
|
|
|
|
|
|
+------------+ tcp 8001, KISS ch 0
|
|
|
|
Radio A -------- | direwolf | -------------------------- Application A
|
|
|
|
+------------+
|
|
|
|
|
|
|
|
+------------+ tcp 8002, KISS ch 0
|
|
|
|
Radio B -------- | direwolf | -------------------------- Application B
|
|
|
|
+------------+
|
|
|
|
|
|
|
|
|
|
|
|
Or they might be using a single application that knows how to talk to multiple
|
|
|
|
single port TNCs. But they don't know how to multiplex multiple channels
|
|
|
|
thru a single KISS stream.
|
|
|
|
|
|
|
|
+------------+ tcp 8001, KISS ch 0
|
|
|
|
Radio A -------- | direwolf | ------------------------
|
|
|
|
+------------+ \
|
|
|
|
-- Application
|
|
|
|
+------------+ tcp 8002, KISS ch 0 /
|
|
|
|
Radio B -------- | direwolf | ------------------------
|
|
|
|
+------------+
|
|
|
|
|
|
|
|
Using two different instances of direwolf means more complex configuration
|
|
|
|
and loss of cross-channel digipeating. It is possible to use a stereo
|
|
|
|
audio interface but some ALSA magic is required to make it look like two
|
|
|
|
independent virtual mono interfaces.
|
|
|
|
|
|
|
|
In version 1.7, we add the capability of multiple KISS TCP ports, each for
|
|
|
|
a single radio channel. e.g.
|
|
|
|
|
|
|
|
KISSPORT 8001 1
|
|
|
|
KISSPORT 8002 2
|
|
|
|
|
|
|
|
Now can use a single instance of direwolf.
|
|
|
|
|
|
|
|
|
|
|
|
+------------+ tcp 8001, KISS ch 0
|
|
|
|
Radio A -------- | | -------------------------- Application A
|
|
|
|
| direwolf |
|
|
|
|
Radio B -------- | | -------------------------- Application B
|
|
|
|
+------------+ tcp 8002, KISS ch 0
|
|
|
|
|
|
|
|
When receiving, the KISS channel is set to 0.
|
|
|
|
- only radio channel 1 would be sent over tcp port 8001.
|
|
|
|
- only radio channel 2 would be sent over tcp port 8001.
|
|
|
|
|
|
|
|
When transmitting, the KISS channel is ignored.
|
|
|
|
- frames from tcp port 8001 are transmitted on radio channel 1.
|
|
|
|
- frames from tcp port 8002 are transmitted on radio channel 2.
|
|
|
|
|
|
|
|
Of course, you could also use an application, capable of connecting to
|
|
|
|
multiple single radio TNCs. Separate TCP ports actually go to the
|
|
|
|
same direwolf instance.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2015-07-27 00:35:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Native Windows: Use the Winsock interface.
|
|
|
|
* Linux: Use the BSD socket interface.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-07-03 22:09:34 +00:00
|
|
|
#include "direwolf.h" // Sets _WIN32_WINNT for XP API level needed by ws2tcpip.h
|
|
|
|
|
|
|
|
|
2015-07-27 00:35:07 +00:00
|
|
|
#if __WIN32__
|
|
|
|
#include <winsock2.h>
|
2016-07-03 22:09:34 +00:00
|
|
|
#include <ws2tcpip.h> // _WIN32_WINNT must be set to 0x0501 before including this
|
2015-07-27 00:35:07 +00:00
|
|
|
#else
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
2015-07-27 01:17:23 +00:00
|
|
|
#include <errno.h>
|
2015-07-27 00:35:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2019-12-05 03:38:12 +00:00
|
|
|
#include <stddef.h>
|
2015-07-27 00:35:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "tq.h"
|
|
|
|
#include "ax25_pad.h"
|
|
|
|
#include "textcolor.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "kissnet.h"
|
|
|
|
#include "kiss_frame.h"
|
|
|
|
#include "xmit.h"
|
|
|
|
|
2016-07-03 22:09:34 +00:00
|
|
|
void hex_dump (unsigned char *p, int len); // This should be in a .h file.
|
|
|
|
|
2015-07-27 00:35:07 +00:00
|
|
|
|
2017-05-03 21:41:37 +00:00
|
|
|
|
|
|
|
|
2015-07-27 00:35:07 +00:00
|
|
|
|
2016-07-03 22:09:34 +00:00
|
|
|
// TODO: define in one place, use everywhere.
|
|
|
|
#if __WIN32__
|
|
|
|
#define THREAD_F unsigned __stdcall
|
|
|
|
#else
|
|
|
|
#define THREAD_F void *
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static THREAD_F connect_listen_thread (void *arg);
|
|
|
|
static THREAD_F kissnet_listen_thread (void *arg);
|
2015-07-27 00:35:07 +00:00
|
|
|
|
|
|
|
|
2019-01-02 00:59:58 +00:00
|
|
|
static struct misc_config_s *s_misc_config_p;
|
2015-07-27 00:35:07 +00:00
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
|
|
|
|
// Each TCP port has its own status block.
|
|
|
|
// There is a variable number so use a linked list.
|
|
|
|
|
|
|
|
static struct kissport_status_s *all_ports = NULL;
|
|
|
|
|
2015-07-27 00:35:07 +00:00
|
|
|
static int kiss_debug = 0; /* Print information flowing from and to client. */
|
|
|
|
|
|
|
|
void kiss_net_set_debug (int n)
|
|
|
|
{
|
|
|
|
kiss_debug = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Name: kissnet_init
|
|
|
|
*
|
|
|
|
* Purpose: Set up a server to listen for connection requests from
|
|
|
|
* an application such as Xastir or APRSIS32.
|
2021-01-05 00:43:00 +00:00
|
|
|
* This is called once from the main program.
|
2015-07-27 00:35:07 +00:00
|
|
|
*
|
|
|
|
* Inputs: mc->kiss_port - TCP port for server.
|
2015-07-27 01:17:23 +00:00
|
|
|
* 0 means disable. New in version 1.2.
|
|
|
|
*
|
2015-07-27 00:35:07 +00:00
|
|
|
* Outputs:
|
|
|
|
*
|
|
|
|
* Description: This starts two threads:
|
|
|
|
* * to listen for a connection from client app.
|
|
|
|
* * to listen for commands from client app.
|
|
|
|
* so the main application doesn't block while we wait for these.
|
|
|
|
*
|
|
|
|
*--------------------------------------------------------------------*/
|
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
static void kissnet_init_one (struct kissport_status_s *kps);
|
2015-07-27 00:35:07 +00:00
|
|
|
|
|
|
|
void kissnet_init (struct misc_config_s *mc)
|
2021-01-05 00:43:00 +00:00
|
|
|
{
|
|
|
|
s_misc_config_p = mc;
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_KISS_TCP_PORTS; i++) {
|
|
|
|
if (mc->kiss_port[i] != 0) {
|
|
|
|
struct kissport_status_s *kps = calloc(sizeof(struct kissport_status_s), 1);
|
|
|
|
|
|
|
|
kps->tcp_port = mc->kiss_port[i];
|
|
|
|
kps->chan = mc->kiss_chan[i];
|
|
|
|
kissnet_init_one (kps);
|
|
|
|
|
|
|
|
// Add to list.
|
|
|
|
kps->pnext = all_ports;
|
|
|
|
all_ports = kps;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void kissnet_init_one (struct kissport_status_s *kps)
|
2015-07-27 00:35:07 +00:00
|
|
|
{
|
2017-05-03 21:41:37 +00:00
|
|
|
int client;
|
|
|
|
|
2015-07-27 00:35:07 +00:00
|
|
|
#if __WIN32__
|
|
|
|
HANDLE connect_listen_th;
|
2017-05-03 21:41:37 +00:00
|
|
|
HANDLE cmd_listen_th[MAX_NET_CLIENTS];
|
2015-07-27 00:35:07 +00:00
|
|
|
#else
|
|
|
|
pthread_t connect_listen_tid;
|
2017-05-03 21:41:37 +00:00
|
|
|
pthread_t cmd_listen_tid[MAX_NET_CLIENTS];
|
2015-07-27 00:35:07 +00:00
|
|
|
int e;
|
2016-07-03 22:09:34 +00:00
|
|
|
#endif
|
2019-01-02 00:59:58 +00:00
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
|
2015-07-27 00:35:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
text_color_set(DW_COLOR_DEBUG);
|
2021-01-05 00:43:00 +00:00
|
|
|
dw_printf ("kissnet_init ( tcp port %d, radio chan = %d )\n", kps->tcp_port, kps->chan);
|
2015-07-27 00:35:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2017-05-03 21:41:37 +00:00
|
|
|
for (client=0; client<MAX_NET_CLIENTS; client++) {
|
2021-01-05 00:43:00 +00:00
|
|
|
kps->client_sock[client] = -1;
|
|
|
|
memset (&(kps->kf[client]), 0, sizeof(kps->kf[client]));
|
2017-05-03 21:41:37 +00:00
|
|
|
}
|
2015-07-27 00:35:07 +00:00
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
if (kps->tcp_port == 0) {
|
2015-07-27 01:17:23 +00:00
|
|
|
text_color_set(DW_COLOR_INFO);
|
|
|
|
dw_printf ("Disabled KISS network client port.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-27 00:35:07 +00:00
|
|
|
/*
|
2017-05-03 21:41:37 +00:00
|
|
|
* This waits for a client to connect and sets client_sock[n].
|
2015-07-27 00:35:07 +00:00
|
|
|
*/
|
|
|
|
#if __WIN32__
|
2021-01-05 00:43:00 +00:00
|
|
|
connect_listen_th = (HANDLE)_beginthreadex (NULL, 0, connect_listen_thread, (void *)kps, 0, NULL);
|
2015-07-27 00:35:07 +00:00
|
|
|
if (connect_listen_th == NULL) {
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
2021-01-05 00:43:00 +00:00
|
|
|
dw_printf ("Could not create KISS socket connect listening thread for tcp port %d, radio chan %d\n", kps->tcp_port, kps->chan);
|
2015-07-27 00:35:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#else
|
2021-01-05 00:43:00 +00:00
|
|
|
e = pthread_create (&connect_listen_tid, NULL, connect_listen_thread, (void *)kps);
|
2015-07-27 00:35:07 +00:00
|
|
|
if (e != 0) {
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
|
|
|
perror("Could not create KISS socket connect listening thread");
|
2021-01-05 00:43:00 +00:00
|
|
|
dw_printf ("for tcp port %d, radio chan %d\n", kps->tcp_port, kps->chan);
|
2015-07-27 00:35:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2017-05-03 21:41:37 +00:00
|
|
|
* These read messages from client when client_sock[n] is valid.
|
|
|
|
* Currently we start up a separate thread for each potential connection.
|
|
|
|
* Possible later refinement. Start one now, others only as needed.
|
2015-07-27 00:35:07 +00:00
|
|
|
*/
|
2017-05-03 21:41:37 +00:00
|
|
|
for (client = 0; client < MAX_NET_CLIENTS; client++) {
|
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
kps->arg2 = client;
|
|
|
|
|
2015-07-27 00:35:07 +00:00
|
|
|
#if __WIN32__
|
2021-01-05 00:43:00 +00:00
|
|
|
cmd_listen_th[client] = (HANDLE)_beginthreadex (NULL, 0, kissnet_listen_thread, (void*)kps, 0, NULL);
|
2017-05-03 21:41:37 +00:00
|
|
|
if (cmd_listen_th[client] == NULL) {
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
2018-02-12 00:45:44 +00:00
|
|
|
dw_printf ("Could not create KISS command listening thread for client %d\n", client);
|
2017-05-03 21:41:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-27 00:35:07 +00:00
|
|
|
#else
|
2021-01-05 00:43:00 +00:00
|
|
|
e = pthread_create (&(cmd_listen_tid[client]), NULL, kissnet_listen_thread, (void *)kps);
|
2017-05-03 21:41:37 +00:00
|
|
|
if (e != 0) {
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
2018-02-12 00:45:44 +00:00
|
|
|
dw_printf ("Could not create KISS command listening thread for client %d\n", client);
|
|
|
|
// Replace add perror with better message handling.
|
|
|
|
perror("");
|
2017-05-03 21:41:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-27 00:35:07 +00:00
|
|
|
#endif
|
2021-01-05 00:43:00 +00:00
|
|
|
// Wait for new thread to get content of arg2 before reusing it for the next thread create.
|
|
|
|
|
|
|
|
int timer = 0;
|
|
|
|
while (kps->arg2 >= 0) {
|
|
|
|
SLEEP_MS(10);
|
|
|
|
timer++;
|
|
|
|
if (timer > 100) { // 1 second - thread did not start
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
|
|
|
dw_printf ("KISS data listening thread did not start for tcp port %d, client slot %d\n", kps->tcp_port, client);
|
|
|
|
kps->arg2 = -1; // Keep moving along.
|
|
|
|
}
|
|
|
|
}
|
2017-05-03 21:41:37 +00:00
|
|
|
}
|
2015-07-27 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Name: connect_listen_thread
|
|
|
|
*
|
|
|
|
* Purpose: Wait for a connection request from an application.
|
|
|
|
*
|
2021-01-05 00:43:00 +00:00
|
|
|
* Inputs: arg - KISS port status block.
|
2015-07-27 00:35:07 +00:00
|
|
|
*
|
|
|
|
* Outputs: client_sock - File descriptor for communicating with client app.
|
|
|
|
*
|
|
|
|
* Description: Wait for connection request from client and establish
|
|
|
|
* communication.
|
|
|
|
* Note that the client can go away and come back again and
|
|
|
|
* re-establish communication without restarting this application.
|
|
|
|
*
|
|
|
|
*--------------------------------------------------------------------*/
|
|
|
|
|
2016-07-03 22:09:34 +00:00
|
|
|
static THREAD_F connect_listen_thread (void *arg)
|
2015-07-27 00:35:07 +00:00
|
|
|
{
|
2021-01-05 00:43:00 +00:00
|
|
|
struct kissport_status_s *kps = arg;
|
|
|
|
|
2015-07-27 00:35:07 +00:00
|
|
|
#if __WIN32__
|
|
|
|
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *ai = NULL;
|
|
|
|
int err;
|
2021-01-05 00:43:00 +00:00
|
|
|
char tcp_port_str[12];
|
2015-07-27 00:35:07 +00:00
|
|
|
|
|
|
|
SOCKET listen_sock;
|
|
|
|
WSADATA wsadata;
|
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
snprintf (tcp_port_str, sizeof(tcp_port_str), "%d", kps->tcp_port);
|
2015-07-27 00:35:07 +00:00
|
|
|
#if DEBUG
|
|
|
|
text_color_set(DW_COLOR_DEBUG);
|
2021-01-05 00:43:00 +00:00
|
|
|
dw_printf ("DEBUG: kissnet port = %d = '%s'\n", (int)(ptrdiff_t)arg, tcp_port_str);
|
2015-07-27 00:35:07 +00:00
|
|
|
#endif
|
|
|
|
err = WSAStartup (MAKEWORD(2,2), &wsadata);
|
|
|
|
if (err != 0) {
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
|
|
|
dw_printf("WSAStartup failed: %d\n", err);
|
2016-07-03 22:09:34 +00:00
|
|
|
return (0);
|
2015-07-27 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 2) {
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
|
|
|
dw_printf("Could not find a usable version of Winsock.dll\n");
|
|
|
|
WSACleanup();
|
|
|
|
//sleep (1);
|
2016-07-03 22:09:34 +00:00
|
|
|
return (0);
|
2015-07-27 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memset (&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
err = getaddrinfo(NULL, tcp_port_str, &hints, &ai);
|
2015-07-27 00:35:07 +00:00
|
|
|
if (err != 0) {
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
|
|
|
dw_printf("getaddrinfo failed: %d\n", err);
|
|
|
|
//sleep (1);
|
|
|
|
WSACleanup();
|
2016-07-03 22:09:34 +00:00
|
|
|
return (0);
|
2015-07-27 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listen_sock= socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
|
|
|
|
if (listen_sock == INVALID_SOCKET) {
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
|
|
|
dw_printf ("connect_listen_thread: Socket creation failed, err=%d", WSAGetLastError());
|
2016-07-03 22:09:34 +00:00
|
|
|
return (0);
|
2015-07-27 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
text_color_set(DW_COLOR_DEBUG);
|
2021-01-05 00:43:00 +00:00
|
|
|
dw_printf("Binding to port %s ... \n", tcp_port_str);
|
2015-07-27 00:35:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
err = bind( listen_sock, ai->ai_addr, (int)ai->ai_addrlen);
|
|
|
|
if (err == SOCKET_ERROR) {
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
2015-07-27 01:17:23 +00:00
|
|
|
dw_printf("Bind failed with error: %d\n", WSAGetLastError()); // TODO: provide corresponding text.
|
2021-01-05 00:43:00 +00:00
|
|
|
dw_printf("Some other application is probably already using port %s.\n", tcp_port_str);
|
2015-07-27 01:17:23 +00:00
|
|
|
dw_printf("Try using a different port number with KISSPORT in the configuration file.\n");
|
2015-07-27 00:35:07 +00:00
|
|
|
freeaddrinfo(ai);
|
|
|
|
closesocket(listen_sock);
|
|
|
|
WSACleanup();
|
2016-07-03 22:09:34 +00:00
|
|
|
return (0);
|
2015-07-27 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
text_color_set(DW_COLOR_DEBUG);
|
2021-01-05 00:43:00 +00:00
|
|
|
dw_printf("opened KISS socket as fd (%d) on port (%s) for stream i/o\n", listen_sock, tcp_port_str );
|
2015-07-27 00:35:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
2017-05-03 21:41:37 +00:00
|
|
|
int client;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
client = -1;
|
|
|
|
for (c = 0; c < MAX_NET_CLIENTS && client < 0; c++) {
|
2021-01-05 00:43:00 +00:00
|
|
|
if (kps->client_sock[c] <= 0) {
|
2017-05-03 21:41:37 +00:00
|
|
|
client = c;
|
|
|
|
}
|
2015-07-27 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 21:41:37 +00:00
|
|
|
/*
|
|
|
|
* Listen for connection if we have not reached maximum.
|
|
|
|
*/
|
|
|
|
if (client >= 0) {
|
|
|
|
|
|
|
|
if(listen(listen_sock, MAX_NET_CLIENTS) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
|
|
|
dw_printf("Listen failed with error: %d\n", WSAGetLastError());
|
|
|
|
return (0);
|
|
|
|
}
|
2015-07-27 00:35:07 +00:00
|
|
|
|
2017-05-03 21:41:37 +00:00
|
|
|
text_color_set(DW_COLOR_INFO);
|
2021-01-05 00:43:00 +00:00
|
|
|
if (kps->chan == -1) {
|
|
|
|
dw_printf("Ready to accept KISS TCP client application %d on port %s ...\n", client, tcp_port_str);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dw_printf("Ready to accept KISS TCP client application %d on port %s (radio channel %d) ...\n", client, tcp_port_str, kps->chan);
|
|
|
|
}
|
|
|
|
|
|
|
|
kps->client_sock[client] = accept(listen_sock, NULL, NULL);
|
2015-07-27 00:35:07 +00:00
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
if (kps->client_sock[client] == -1) {
|
2017-05-03 21:41:37 +00:00
|
|
|
text_color_set(DW_COLOR_ERROR);
|
|
|
|
dw_printf("Accept failed with error: %d\n", WSAGetLastError());
|
|
|
|
closesocket(listen_sock);
|
|
|
|
WSACleanup();
|
|
|
|
return (0);
|
|
|
|
}
|
2015-07-27 00:35:07 +00:00
|
|
|
|
2017-05-03 21:41:37 +00:00
|
|
|
text_color_set(DW_COLOR_INFO);
|
2021-01-05 00:43:00 +00:00
|
|
|
if (kps->chan == -1) {
|
|
|
|
dw_printf("\nAttached to KISS TCP client application %d on port %s ...\n\n", client, tcp_port_str);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dw_printf("\nAttached to KISS TCP client application %d on port %s (radio channel %d) ...\n\n", client, tcp_port_str, kps->chan);
|
|
|
|
}
|
2015-07-27 00:35:07 +00:00
|
|
|
|
2017-05-03 21:41:37 +00:00
|
|
|
// Reset the state and buffer.
|
2021-01-05 00:43:00 +00:00
|
|
|
memset (&(kps->kf[client]), 0, sizeof(kps->kf[client]));
|
2017-05-03 21:41:37 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
SLEEP_SEC(1); /* wait then check again if more clients allowed. */
|
|
|
|
}
|
2015-07-27 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 01:17:23 +00:00
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
#else /* End of Windows case, now Linux / Unix / Mac OSX. */
|
2015-07-27 01:17:23 +00:00
|
|
|
|
2015-07-27 00:35:07 +00:00
|
|
|
|
|
|
|
struct sockaddr_in sockaddr; /* Internet socket address stuct */
|
|
|
|
socklen_t sockaddr_size = sizeof(struct sockaddr_in);
|
|
|
|
int listen_sock;
|
2015-09-07 23:56:20 +00:00
|
|
|
int bcopt = 1;
|
2015-07-27 00:35:07 +00:00
|
|
|
|
|
|
|
listen_sock= socket(AF_INET,SOCK_STREAM,0);
|
|
|
|
if (listen_sock == -1) {
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
|
|
|
perror ("connect_listen_thread: Socket creation failed");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2015-09-07 23:56:20 +00:00
|
|
|
/* Version 1.3 - as suggested by G8BPQ. */
|
|
|
|
/* Without this, if you kill the application then try to run it */
|
|
|
|
/* again quickly the port number is unavailable for a while. */
|
|
|
|
/* Don't try doing the same thing On Windows; It has a different meaning. */
|
|
|
|
/* http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t */
|
|
|
|
|
|
|
|
setsockopt (listen_sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&bcopt, 4);
|
|
|
|
|
2015-07-27 00:35:07 +00:00
|
|
|
sockaddr.sin_addr.s_addr = INADDR_ANY;
|
2021-01-05 00:43:00 +00:00
|
|
|
sockaddr.sin_port = htons(kps->tcp_port);
|
2015-07-27 00:35:07 +00:00
|
|
|
sockaddr.sin_family = AF_INET;
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
text_color_set(DW_COLOR_DEBUG);
|
2021-01-05 00:43:00 +00:00
|
|
|
dw_printf("Binding to port %d ... \n", kps->tcp_port);
|
2015-07-27 00:35:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (bind(listen_sock,(struct sockaddr*)&sockaddr,sizeof(sockaddr)) == -1) {
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
2015-07-27 01:17:23 +00:00
|
|
|
dw_printf("Bind failed with error: %d\n", errno);
|
|
|
|
dw_printf("%s\n", strerror(errno));
|
2021-01-05 00:43:00 +00:00
|
|
|
dw_printf("Some other application is probably already using port %d.\n", kps->tcp_port);
|
2015-07-27 01:17:23 +00:00
|
|
|
dw_printf("Try using a different port number with KISSPORT in the configuration file.\n");
|
2015-07-27 00:35:07 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
getsockname( listen_sock, (struct sockaddr *)(&sockaddr), &sockaddr_size);
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
text_color_set(DW_COLOR_DEBUG);
|
2017-05-03 21:41:37 +00:00
|
|
|
dw_printf("opened KISS TCP socket as fd (%d) on port (%d) for stream i/o\n", listen_sock, ntohs(sockaddr.sin_port) );
|
2015-07-27 00:35:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
2017-05-03 21:41:37 +00:00
|
|
|
int client;
|
|
|
|
int c;
|
2015-07-27 00:35:07 +00:00
|
|
|
|
2017-05-03 21:41:37 +00:00
|
|
|
client = -1;
|
|
|
|
for (c = 0; c < MAX_NET_CLIENTS && client < 0; c++) {
|
2021-01-05 00:43:00 +00:00
|
|
|
if (kps->client_sock[c] <= 0) {
|
2017-05-03 21:41:37 +00:00
|
|
|
client = c;
|
|
|
|
}
|
2015-07-27 00:35:07 +00:00
|
|
|
}
|
2017-05-03 21:41:37 +00:00
|
|
|
|
|
|
|
if (client >= 0) {
|
|
|
|
|
|
|
|
if(listen(listen_sock,MAX_NET_CLIENTS) == -1)
|
|
|
|
{
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
|
|
|
perror ("connect_listen_thread: Listen failed");
|
|
|
|
return (NULL);
|
|
|
|
}
|
2015-07-27 00:35:07 +00:00
|
|
|
|
2017-05-03 21:41:37 +00:00
|
|
|
text_color_set(DW_COLOR_INFO);
|
2021-01-05 00:43:00 +00:00
|
|
|
if (kps->chan == -1) {
|
|
|
|
dw_printf("Ready to accept KISS TCP client application %d on port %s ...\n", client, tcp_port_str);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dw_printf("Ready to accept KISS TCP client application %d on port %s (radio channel %d) ...\n", client, tcp_port_str, kps->chan);
|
|
|
|
}
|
|
|
|
|
|
|
|
kps->client_sock[client] = accept(listen_sock, (struct sockaddr*)(&sockaddr),&sockaddr_size);
|
2015-07-27 00:35:07 +00:00
|
|
|
|
2017-05-03 21:41:37 +00:00
|
|
|
text_color_set(DW_COLOR_INFO);
|
2021-01-05 00:43:00 +00:00
|
|
|
if (kps->chan == -1) {
|
|
|
|
dw_printf("\nAttached to KISS TCP client application %d on port %s ...\n\n", client, tcp_port_str);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dw_printf("\nAttached to KISS TCP client application %d on port %s (radio channel %d) ...\n\n", client, tcp_port_str, kps->chan);
|
|
|
|
}
|
2015-07-27 00:35:07 +00:00
|
|
|
|
2017-05-03 21:41:37 +00:00
|
|
|
// Reset the state and buffer.
|
2021-01-05 00:43:00 +00:00
|
|
|
memset (&(kps->kf[client]), 0, sizeof(kps->kf[client]));
|
2017-05-03 21:41:37 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
SLEEP_SEC(1); /* wait then check again if more clients allowed. */
|
|
|
|
}
|
2015-07-27 00:35:07 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Name: kissnet_send_rec_packet
|
|
|
|
*
|
2021-01-05 00:43:00 +00:00
|
|
|
* Purpose: Send a packet, received over the radio, to the client app.
|
2015-07-27 00:35:07 +00:00
|
|
|
*
|
|
|
|
* Inputs: chan - Channel number where packet was received.
|
|
|
|
* 0 = first, 1 = second if any.
|
|
|
|
*
|
2017-09-12 02:28:54 +00:00
|
|
|
// TODO: add kiss_cmd
|
|
|
|
*
|
2015-07-27 00:35:07 +00:00
|
|
|
* fbuf - Address of raw received frame buffer
|
|
|
|
* or a text string.
|
|
|
|
*
|
2017-09-12 02:28:54 +00:00
|
|
|
* kiss_cmd - Usually KISS_CMD_DATA_FRAME but we can also have
|
|
|
|
* KISS_CMD_SET_HARDWARE when responding to a query.
|
|
|
|
*
|
2015-07-27 00:35:07 +00:00
|
|
|
* flen - Number of bytes for AX.25 frame.
|
2017-05-03 21:41:37 +00:00
|
|
|
* When called from kiss_rec_byte, flen will be -1
|
|
|
|
* indicating a text string rather than frame content.
|
|
|
|
* This is used to fake out an application that thinks
|
|
|
|
* it is using a traditional TNC and tries to put it
|
|
|
|
* into KISS mode.
|
|
|
|
*
|
2021-01-05 00:43:00 +00:00
|
|
|
* onlykps - KISS TCP status block pointer or NULL.
|
|
|
|
*
|
|
|
|
* onlyclient - It is possible to have more than client attached
|
2017-05-03 21:41:37 +00:00
|
|
|
* at the same time with TCP KISS.
|
2021-01-05 00:43:00 +00:00
|
|
|
* Starting with version 1.7 we can have multiple TCP ports.
|
|
|
|
* When a frame is received from the radio we normally want it
|
|
|
|
* to go to all of the clients.
|
|
|
|
* In this case specify NULL for onlykps and -1 tcp client.
|
2017-05-03 21:41:37 +00:00
|
|
|
* When responding to a command from the client, we want
|
|
|
|
* to send only to that one client app. In this case
|
2021-01-05 00:43:00 +00:00
|
|
|
* a non NULL kps and onlyclient >= 0.
|
2017-05-03 21:41:37 +00:00
|
|
|
*
|
|
|
|
* Description: Send message to client(s) if connected.
|
2015-07-27 00:35:07 +00:00
|
|
|
* Disconnect from client, and notify user, if any error.
|
|
|
|
*
|
|
|
|
*--------------------------------------------------------------------*/
|
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
void kissnet_send_rec_packet (int chan, int kiss_cmd, unsigned char *fbuf, int flen,
|
|
|
|
struct kissport_status_s *onlykps, int onlyclient)
|
2015-07-27 00:35:07 +00:00
|
|
|
{
|
|
|
|
unsigned char kiss_buff[2 * AX25_MAX_PACKET_LEN];
|
|
|
|
int kiss_len;
|
|
|
|
int err;
|
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
// Something received over the radio would normally be sent to all attached clients.
|
2017-05-03 21:41:37 +00:00
|
|
|
// However, there are times we want to send a response only to a particular client.
|
|
|
|
// In the case of a serial port or pseudo terminal, there is only one potential client.
|
|
|
|
// so the response would be sent to only one place. A new parameter has been added for this.
|
2015-07-27 00:35:07 +00:00
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
for (struct kissport_status_s *kps = all_ports; kps != NULL; kps = kps->pnext) {
|
|
|
|
|
|
|
|
if (onlykps == NULL || kps == onlykps) {
|
2015-07-27 00:35:07 +00:00
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
for (int client = 0; client < MAX_NET_CLIENTS; client++) {
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
if (onlyclient == -1 || client == onlyclient) {
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
if (kps->client_sock[client] != -1) {
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
if (flen < 0) {
|
2015-07-27 00:35:07 +00:00
|
|
|
|
2017-05-03 21:41:37 +00:00
|
|
|
// A client app might think it is attached to a traditional TNC.
|
|
|
|
// It might try sending commands over and over again trying to get the TNC into KISS mode.
|
|
|
|
// We recognize this attempt and send it something to keep it happy.
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
text_color_set(DW_COLOR_ERROR);
|
|
|
|
dw_printf ("KISS TCP: Something unexpected from client application.\n");
|
|
|
|
dw_printf ("Is client app treating this like an old TNC with command mode?\n");
|
|
|
|
dw_printf ("This can be caused by the application sending commands to put a\n");
|
|
|
|
dw_printf ("traditional TNC into KISS mode. It is usually a harmless warning.\n");
|
|
|
|
dw_printf ("For best results, configure for a KISS-only TNC to avoid this.\n");
|
|
|
|
dw_printf ("In the case of APRSISCE/32, use \"Simply(KISS)\" rather than \"KISS.\"\n");
|
|
|
|
|
|
|
|
flen = strlen((char*)fbuf);
|
|
|
|
if (kiss_debug) {
|
|
|
|
kiss_debug_print (TO_CLIENT, "Fake command prompt", fbuf, flen);
|
|
|
|
}
|
|
|
|
strlcpy ((char *)kiss_buff, (char *)fbuf, sizeof(kiss_buff));
|
|
|
|
kiss_len = strlen((char *)kiss_buff);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
unsigned char stemp[AX25_MAX_PACKET_LEN + 1];
|
|
|
|
|
|
|
|
assert (flen < (int)(sizeof(stemp)));
|
|
|
|
|
|
|
|
// New in 1.7.
|
|
|
|
// Previously all channels were sent to everyone.
|
|
|
|
// We now have tcp ports which carry only a single radio channel.
|
|
|
|
// The application will see KISS channel 0 regardless of the radio channel.
|
|
|
|
|
|
|
|
if (kps->chan == -1) {
|
|
|
|
// Normal case, all channels.
|
|
|
|
stemp[0] = (chan << 4) | kiss_cmd;
|
|
|
|
}
|
|
|
|
else if (kps->chan == chan) {
|
|
|
|
// Single radio channel for this port. Application sees 0.
|
|
|
|
stemp[0] = (0 << 4) | kiss_cmd;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Skip it.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy (stemp+1, fbuf, flen);
|
|
|
|
|
|
|
|
if (kiss_debug >= 2) {
|
|
|
|
/* AX.25 frame with the CRC removed. */
|
|
|
|
text_color_set(DW_COLOR_DEBUG);
|
|
|
|
dw_printf ("\n");
|
|
|
|
dw_printf ("Packet content before adding KISS framing and any escapes:\n");
|
|
|
|
hex_dump (fbuf, flen);
|
|
|
|
}
|
|
|
|
|
|
|
|
kiss_len = kiss_encapsulate (stemp, flen+1, kiss_buff);
|
|
|
|
|
|
|
|
/* This has the escapes and the surrounding FENDs. */
|
|
|
|
|
|
|
|
if (kiss_debug) {
|
|
|
|
kiss_debug_print (TO_CLIENT, NULL, kiss_buff, kiss_len);
|
|
|
|
}
|
|
|
|
}
|
2015-07-27 00:35:07 +00:00
|
|
|
|
|
|
|
#if __WIN32__
|
2021-01-05 00:43:00 +00:00
|
|
|
err = SOCK_SEND(kps->client_sock[client], (char*)kiss_buff, kiss_len);
|
|
|
|
if (err == SOCKET_ERROR) {
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
|
|
|
dw_printf ("\nError %d sending message to KISS client application %d on port %d. Closing connection.\n\n", WSAGetLastError(), client, kps->tcp_port);
|
|
|
|
closesocket (kps->client_sock[client]);
|
|
|
|
kps->client_sock[client] = -1;
|
|
|
|
WSACleanup();
|
|
|
|
}
|
2015-07-27 00:35:07 +00:00
|
|
|
#else
|
2021-01-05 00:43:00 +00:00
|
|
|
err = SOCK_SEND (kps->client_sock[client], kiss_buff, kiss_len);
|
|
|
|
if (err <= 0) {
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
|
|
|
dw_printf ("\nError %d sending message to KISS client application %d on port %d. Closing connection.\n\n", WSAGetLastError(), client, kps->tcp_port);
|
|
|
|
close (kps->client_sock[client]);
|
|
|
|
kps->client_sock[client] = -1;
|
|
|
|
}
|
2015-07-27 00:35:07 +00:00
|
|
|
#endif
|
2021-01-05 00:43:00 +00:00
|
|
|
} // frame length >= 0
|
|
|
|
} // if all clients or the one specifie
|
|
|
|
} // for each client on the tcp port
|
|
|
|
} // if all ports or the one specified
|
|
|
|
} // for each tcp port
|
2015-07-27 00:35:07 +00:00
|
|
|
|
|
|
|
} /* end kissnet_send_rec_packet */
|
|
|
|
|
|
|
|
|
2019-01-02 00:59:58 +00:00
|
|
|
/*-------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Name: kissnet_copy
|
|
|
|
*
|
|
|
|
* Purpose: Send data from one network KISS client to all others.
|
|
|
|
*
|
|
|
|
* Inputs: in_msg - KISS frame data without the framing or escapes.
|
2021-01-05 00:43:00 +00:00
|
|
|
* The first byte is channel and command (should be data).
|
|
|
|
* Caller no longer cares this byte. We will clobber it here.
|
2019-01-02 00:59:58 +00:00
|
|
|
*
|
|
|
|
* in_len - Number of bytes in above.
|
|
|
|
*
|
2021-01-05 00:43:00 +00:00
|
|
|
* chan - Channel. Use this instead of first byte of in_msg.
|
2019-01-02 00:59:58 +00:00
|
|
|
*
|
2021-01-05 00:43:00 +00:00
|
|
|
* cmd - KISS command nybble.
|
2019-01-02 00:59:58 +00:00
|
|
|
* Should be 0 because I'm expecting this only for data.
|
|
|
|
*
|
|
|
|
* from_client - Number of network (TCP) client instance.
|
|
|
|
* Should be 0, 1, 2, ...
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Global In: kiss_copy - From misc. configuration.
|
|
|
|
* This enables the feature.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Description: Send message to any attached network KISS clients, other than the one where it came from.
|
|
|
|
* Enable this by putting KISSCOPY in the configuration file.
|
|
|
|
* Note that this applies only to network (TCP) KISS clients, not serial port, or pseudo terminal.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*--------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
void kissnet_copy (unsigned char *in_msg, int in_len, int chan, int cmd, struct kissport_status_s *from_kps, int from_client)
|
2019-01-02 00:59:58 +00:00
|
|
|
{
|
|
|
|
unsigned char kiss_buff[2 * AX25_MAX_PACKET_LEN];
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
|
|
if (s_misc_config_p->kiss_copy) {
|
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
for (struct kissport_status_s *kps = all_ports; kps != NULL; kps = kps->pnext) {
|
|
|
|
|
|
|
|
for (int client = 0; client < MAX_NET_CLIENTS; client++) {
|
|
|
|
|
|
|
|
if ( ! ( kps == from_kps && client == from_client ) ) { // To all but origin.
|
2019-01-02 00:59:58 +00:00
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
if (kps->client_sock[client] != -1) {
|
2019-01-02 00:59:58 +00:00
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
if (kps-> chan == -1 || kps->chan == chan) {
|
2019-01-02 00:59:58 +00:00
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
// Two different cases here:
|
|
|
|
// - The TCP port allows all channels, or
|
|
|
|
// - The TCP port allows only one channel. In this case set KISS channel to 0.
|
2019-01-02 00:59:58 +00:00
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
if (kps->chan == -1) {
|
|
|
|
in_msg[0] = (chan << 4) | cmd;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
in_msg[0] = 0 | cmd; // set channel to zero.
|
|
|
|
}
|
|
|
|
|
|
|
|
int kiss_len = kiss_encapsulate (in_msg, in_len, kiss_buff);
|
|
|
|
|
|
|
|
/* This has the escapes and the surrounding FENDs. */
|
|
|
|
|
|
|
|
if (kiss_debug) {
|
|
|
|
kiss_debug_print (TO_CLIENT, NULL, kiss_buff, kiss_len);
|
|
|
|
}
|
2019-01-02 00:59:58 +00:00
|
|
|
|
|
|
|
#if __WIN32__
|
2021-01-05 00:43:00 +00:00
|
|
|
err = SOCK_SEND(kps->client_sock[client], (char*)kiss_buff, kiss_len);
|
|
|
|
if (err == SOCKET_ERROR) {
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
|
|
|
dw_printf ("\nError %d copying message to KISS TCP port %d client %d application. Closing connection.\n\n", WSAGetLastError(), kps->tcp_port, client);
|
|
|
|
closesocket (kps->client_sock[client]);
|
|
|
|
kps->client_sock[client] = -1;
|
|
|
|
WSACleanup();
|
|
|
|
}
|
2019-01-02 00:59:58 +00:00
|
|
|
#else
|
2021-01-05 00:43:00 +00:00
|
|
|
err = SOCK_SEND (kps->client_sock[client], kiss_buff, kiss_len);
|
|
|
|
if (err <= 0) {
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
|
|
|
dw_printf ("\nError copying message to KISS TCP port %d client %d application. Closing connection.\n\n", kps->tcp_port, client);
|
|
|
|
close (kps->client_sock[client]);
|
|
|
|
kps->client_sock[client] = -1;
|
|
|
|
}
|
2019-01-02 00:59:58 +00:00
|
|
|
#endif
|
2021-01-05 00:43:00 +00:00
|
|
|
} // Channel is allowed on this port.
|
|
|
|
} // socket is open
|
|
|
|
} // if origin and destination different.
|
|
|
|
} // loop over all KISS network clients for one port.
|
|
|
|
} // loop over all KISS TCP ports
|
2019-01-02 00:59:58 +00:00
|
|
|
} // Feature enabled.
|
|
|
|
|
|
|
|
} /* end kissnet_copy */
|
|
|
|
|
|
|
|
|
2015-07-27 00:35:07 +00:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Name: kissnet_listen_thread
|
|
|
|
*
|
|
|
|
* Purpose: Wait for KISS messages from an application.
|
|
|
|
*
|
2017-05-03 21:41:37 +00:00
|
|
|
* Inputs: arg - client number, 0 .. MAX_NET_CLIENTS-1
|
2015-07-27 00:35:07 +00:00
|
|
|
*
|
2017-05-03 21:41:37 +00:00
|
|
|
* Outputs: client_sock[n] - File descriptor for communicating with client app.
|
2015-07-27 00:35:07 +00:00
|
|
|
*
|
|
|
|
* Description: Process messages from the client application.
|
|
|
|
* Note that the client can go away and come back again and
|
|
|
|
* re-establish communication without restarting this application.
|
|
|
|
*
|
|
|
|
*--------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Return one byte (value 0 - 255) */
|
|
|
|
|
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
static int kiss_get (struct kissport_status_s *kps, int client)
|
2015-07-27 00:35:07 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
while (kps->client_sock[client] <= 0) {
|
2015-07-27 00:35:07 +00:00
|
|
|
SLEEP_SEC(1); /* Not connected. Try again later. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Just get one byte at a time. */
|
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
unsigned char ch;
|
|
|
|
int n = SOCK_RECV (kps->client_sock[client], (char *)(&ch), 1);
|
2015-07-27 00:35:07 +00:00
|
|
|
|
|
|
|
if (n == 1) {
|
|
|
|
#if DEBUG9
|
|
|
|
dw_printf (log_fp, "%02x %c %c", ch,
|
|
|
|
isprint(ch) ? ch : '.' ,
|
|
|
|
(isupper(ch>>1) || isdigit(ch>>1) || (ch>>1) == ' ') ? (ch>>1) : '.');
|
|
|
|
if (ch == FEND) fprintf (log_fp, " FEND");
|
|
|
|
if (ch == FESC) fprintf (log_fp, " FESC");
|
|
|
|
if (ch == TFEND) fprintf (log_fp, " TFEND");
|
|
|
|
if (ch == TFESC) fprintf (log_fp, " TFESC");
|
|
|
|
if (ch == '\r') fprintf (log_fp, " CR");
|
|
|
|
if (ch == '\n') fprintf (log_fp, " LF");
|
|
|
|
fprintf (log_fp, "\n");
|
|
|
|
if (ch == FEND) fflush (log_fp);
|
|
|
|
#endif
|
|
|
|
return(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
text_color_set(DW_COLOR_ERROR);
|
2021-01-05 00:43:00 +00:00
|
|
|
dw_printf ("\nKISS client application %d on TCP port %d has gone away.\n\n", client, kps->tcp_port);
|
2015-07-27 00:35:07 +00:00
|
|
|
#if __WIN32__
|
2021-01-05 00:43:00 +00:00
|
|
|
closesocket (kps->client_sock[client]);
|
2015-07-27 00:35:07 +00:00
|
|
|
#else
|
2021-01-05 00:43:00 +00:00
|
|
|
close (kps->client_sock[client]);
|
2015-07-27 00:35:07 +00:00
|
|
|
#endif
|
2021-01-05 00:43:00 +00:00
|
|
|
kps->client_sock[client] = -1;
|
2015-07-27 00:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-03 22:09:34 +00:00
|
|
|
static THREAD_F kissnet_listen_thread (void *arg)
|
2015-07-27 00:35:07 +00:00
|
|
|
{
|
2021-01-05 00:43:00 +00:00
|
|
|
struct kissport_status_s *kps = arg;
|
2018-02-12 00:45:44 +00:00
|
|
|
|
2021-01-05 00:43:00 +00:00
|
|
|
int client = kps->arg2;
|
|
|
|
assert (client >= 0 && client < MAX_NET_CLIENTS);
|
|
|
|
|
|
|
|
kps->arg2 = -1; // Indicates thread is running so
|
|
|
|
// arg2 can be reused for the next one.
|
2018-02-12 00:45:44 +00:00
|
|
|
|
2015-07-27 00:35:07 +00:00
|
|
|
#if DEBUG
|
|
|
|
text_color_set(DW_COLOR_DEBUG);
|
2021-01-05 00:43:00 +00:00
|
|
|
dw_printf ("kissnet_listen_thread ( tcp_port = %d, client = %d, socket fd = %d )\n", kps->tcp_port, client, kps->client_sock[client]);
|
2015-07-27 00:35:07 +00:00
|
|
|
#endif
|
|
|
|
|
2017-05-03 21:41:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
// So why is kissnet_send_rec_packet mentioned here for incoming from the client app?
|
|
|
|
// The logic exists for the serial port case where the client might think it is
|
|
|
|
// attached to a traditional TNC. It might try sending commands over and over again
|
|
|
|
// trying to get the TNC into KISS mode. To keep it happy, we recognize this attempt
|
|
|
|
// and send it something to keep it happy.
|
|
|
|
// In the case of a serial port or pseudo terminal, there is only one potential client
|
|
|
|
// so the response would be sent to only one place.
|
|
|
|
// Starting in version 1.5, this now can have multiple attached clients. We wouldn't
|
|
|
|
// want to send the response to all of them. Actually, we should be providing only
|
|
|
|
// "Simply KISS" as some call it.
|
|
|
|
|
|
|
|
|
2015-07-27 00:35:07 +00:00
|
|
|
while (1) {
|
2021-01-05 00:43:00 +00:00
|
|
|
unsigned char ch = kiss_get(kps, client);
|
|
|
|
kiss_rec_byte (&(kps->kf[client]), ch, kiss_debug, kps, client, kissnet_send_rec_packet);
|
2015-07-27 01:05:48 +00:00
|
|
|
}
|
2015-07-27 00:35:07 +00:00
|
|
|
|
2016-07-03 22:09:34 +00:00
|
|
|
#if __WIN32__
|
|
|
|
return(0);
|
|
|
|
#else
|
|
|
|
return (THREAD_F) 0; /* Unreachable but avoids compiler warning. */
|
|
|
|
#endif
|
2015-07-27 00:35:07 +00:00
|
|
|
|
|
|
|
} /* end kissnet_listen_thread */
|
|
|
|
|
|
|
|
/* end kissnet.c */
|