1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
/*
* kissnetd.c : Simple kiss broadcast daemon between several
* pseudo-tty devices.
* Each kiss frame received on one pty is broadcasted
* to each other one.
*
* ATEPRA FPAC/Linux Project
*
* F1OAT 960804 - Frederic RIBLE
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>
#include <errno.h>
#include <syslog.h>
#include <time.h>
#include <limits.h>
#include <config.h>
#include <scm-version.h>
static int VerboseMode;
static int MaxFrameSize = 512;
#define REOPEN_TIMEOUT 30 /* try tio reopen every 10 s */
struct PortDescriptor {
char Name[PATH_MAX];
int Fd;
unsigned char *FrameBuffer;
int BufferIndex;
time_t TimeLastOpen;
char namepts[PATH_MAX]; /* name of the unix98 pts slaves, which
* the client has to use */
int is_active;
};
static struct PortDescriptor *PortList[FD_SETSIZE];
static int NbPort;
static void Usage(void)
{
fprintf(stderr, "\nUsage : kissnetd [-v] [-f size] [-p num | /dev/pty?? [/dev/pty??]* ]\n");
fprintf(stderr, " -v : Verbose mode, trace on stdout\n");
fprintf(stderr, " -f size : Set max frame size to size bytes (default 512)\n");
fprintf(stderr, " -p num : Number of /dev/ptmx-master-devices has to open\n");
exit(1);
}
static void Banner(int Small)
{
if (Small) {
printf("kissnetd V %s by Frederic RIBLE F1OAT - ATEPRA FPAC/Linux Project\n", FULL_VER);
}
else {
printf("****************************************\n");
printf("* Network broadcast between kiss ports *\n");
printf("* ATEPRA FPAC/Linux Project *\n");
printf("****************************************\n");
printf("* %*skissnetd Version %s%*s*\n",
(int) (10 - strlen(FULL_VER) / 2), "",
FULL_VER,
(int) (10 - (strlen(FULL_VER) + 1)) / 2, "");
printf("* by Frederic RIBLE F1OAT *\n");
printf("****************************************\n");
}
}
static void NewPort(char *Name)
{
struct PortDescriptor *MyPort;
if (VerboseMode) {
printf("Opening port %s\n", Name);
}
if (NbPort == FD_SETSIZE) {
fprintf(stderr, "Cannot handle %s : too many ports\n", Name);
exit(1);
}
MyPort = calloc(1, sizeof(struct PortDescriptor));
if (MyPort) MyPort->FrameBuffer = calloc(sizeof (unsigned char), MaxFrameSize);
if (!MyPort || !MyPort->FrameBuffer) {
perror("cannot allocate port descriptor");
exit(1);
}
strncpy(MyPort->Name, Name, PATH_MAX-1);
MyPort->Name[PATH_MAX-1] = '\0';
MyPort->Fd = -1;
MyPort->FrameBuffer[0] = 0xC0;
MyPort->BufferIndex = 1;
MyPort->namepts [0] = '\0';
MyPort->is_active = 0;
PortList[NbPort++] = MyPort;
}
static void ReopenPort(int PortNumber)
{
char MyString[28 + PATH_MAX];
PortList[PortNumber]->TimeLastOpen = time(NULL);
if (VerboseMode) {
printf("Reopening port %d\n", PortNumber);
}
if (PortList[PortNumber]->namepts[0] == '\0') {
syslog(LOG_WARNING, "kissnetd : Opening port %s\n", PortList[PortNumber]->Name);
PortList[PortNumber]->Fd = open(PortList[PortNumber]->Name, O_RDWR | O_NONBLOCK);
if (PortList[PortNumber]->Fd < 0) {
syslog(LOG_WARNING, "kissnetd : Error opening port %s : %s\n",
PortList[PortNumber]->Name, strerror(errno));
if (VerboseMode) {
sprintf(MyString, "cannot reopen %s", PortList[PortNumber]->Name);
perror(MyString);
}
return;
}
PortList[PortNumber]->is_active = 1;
if (!strcmp(PortList[PortNumber]->Name, "/dev/ptmx")) {
char *npts;
/* get name of pts-device */
npts = ptsname(PortList[PortNumber]->Fd);
if (npts == NULL) {
sprintf(MyString, "Cannot get name of pts-device.\n");
syslog(LOG_WARNING, "kissnetd : Cannot get name of pts-device\n");
exit(1);
}
strncpy(PortList[PortNumber]->namepts, npts, PATH_MAX-1);
PortList[PortNumber]->namepts[PATH_MAX-1] = '\0';
/* unlock pts-device */
if (unlockpt(PortList[PortNumber]->Fd) == -1) {
sprintf(MyString, "Cannot unlock pts-device %s\n", PortList[PortNumber]->namepts);
syslog(LOG_WARNING, "kissnetd : Cannot unlock pts-device %s\n", PortList[PortNumber]->namepts);
exit(1);
}
syslog(LOG_WARNING, "kissnetd : Using /dev/ptmx with slave pty %s\n", PortList[PortNumber]->namepts);
}
} else {
if (PortList[PortNumber]->Fd == -1) {
syslog(LOG_WARNING, "kissnetd : Cannot reopen port ptmx (slave %s) : not supported by ptmx-device\n",
PortList[PortNumber]->namepts);
if (VerboseMode) {
sprintf(MyString, "cannot reopen ptmx (slave %s).", PortList[PortNumber]->namepts);
perror(MyString);
}
return;
}
syslog(LOG_WARNING, "kissnetd : Trying to poll port ptmx (slave %s).\n",
PortList[PortNumber]->namepts);
PortList[PortNumber]->is_active = 1;
}
}
static void TickReopen(void)
{
int i;
static int wrote_info;
time_t CurrentTime = time(NULL);
for (i=0; i<NbPort; i++) {
if (PortList[i]->Fd >= 0 && PortList[i]->is_active == 1) continue;
if ( (CurrentTime - PortList[i]->TimeLastOpen) > REOPEN_TIMEOUT ) ReopenPort(i);
}
if (!wrote_info) {
for (i=0; i<NbPort; i++) {
if (PortList[i]->namepts[0] != '\0') {
if (wrote_info == 0)
printf("\nAwaiting client connects on:\n");
else
printf(" ");
printf("%s", PortList[i]->namepts);
wrote_info = 1;
}
}
if (wrote_info > 0) {
printf("\n");
if (!VerboseMode) {
fflush(stdout);
fflush(stderr);
close(0);
close(1);
close(2);
}
}
}
}
static void Broadcast(int InputPort)
{
int i;
int rc;
/* Broadcast only info frames */
if (PortList[InputPort]->FrameBuffer[1] != 0x00 && \
PortList[InputPort]->FrameBuffer[1] != 0x20 && \
PortList[InputPort]->FrameBuffer[1] != 0x80)
return;
for (i=0; i<NbPort; i++) {
int offset = 0;
if (i == InputPort) continue;
if (PortList[i]->Fd < 0 || PortList[i]->is_active == 0) continue;
again:
rc = write(PortList[i]->Fd,
PortList[InputPort]->FrameBuffer+offset,
PortList[InputPort]->BufferIndex-offset);
if (rc < 0) {
if (errno == EAGAIN) {
if (PortList[i]->namepts[0] == '\0')
syslog(LOG_WARNING, "kissnetd : write buffer full on port %s. dropping frame. %s",
PortList[i]->Name, strerror(errno));
else
syslog(LOG_WARNING, "kissnetd : write buffer full on ptmx port %s. dropping frame. %s",
PortList[i]->namepts, strerror(errno));
continue;
}
if (PortList[i]->namepts[0] == '\0')
syslog(LOG_WARNING, "kissnetd : Error writing to port %s : %s\n",
PortList[i]->Name, strerror(errno));
else
syslog(LOG_WARNING, "kissnetd : Error writing to port ptmx (slave %s) : %s\n",
PortList[i]->namepts, strerror(errno));
if (VerboseMode) perror("write");
PortList[i]->is_active = 0;
if (PortList[i]->namepts[0] == '\0') {
close(PortList[i]->Fd);
PortList[i]->Fd = -1;
}
continue;
}
if (VerboseMode) {
printf("Sending %d bytes on port %d : rc=%d\n",
PortList[InputPort]->BufferIndex,
i, rc);
}
if (rc < PortList[InputPort]->BufferIndex-offset) {
offset += rc;
goto again;
}
}
}
static void ProcessInput(int PortNumber)
{
static unsigned char MyBuffer[2048];
int Length;
int i;
struct PortDescriptor *MyPort = PortList[PortNumber];
Length = read(MyPort->Fd, MyBuffer, sizeof(MyBuffer));
if (VerboseMode) {
printf("Read port %d : rc=%d\n", PortNumber, Length);
}
if (!Length) return;
if (Length < 0) {
if (errno == EAGAIN)
return;
if (MyPort->namepts[0] == '\0')
syslog(LOG_WARNING, "kissnetd : Error reading from port %s : %s\n",
PortList[PortNumber]->Name, strerror(errno));
else
syslog(LOG_WARNING, "kissnetd : Error reading from port ptmx (slave %s) : %s\n",
PortList[PortNumber]->namepts, strerror(errno));
if (VerboseMode) perror("read");
MyPort->is_active = 0;
if (MyPort->namepts[0] == '\0') {
close(MyPort->Fd);
MyPort->Fd = -1;
}
return;
}
for (i=0; i<Length; i++) {
if (MyPort->BufferIndex == MaxFrameSize) {
if (MyBuffer[i] == 0xC0) {
if (VerboseMode) printf("Drop frame too long\n");
MyPort->BufferIndex = 1;
}
}
else {
MyPort->FrameBuffer[MyPort->BufferIndex++] = MyBuffer[i];
if (MyBuffer[i] == 0xC0) {
Broadcast(PortNumber);
MyPort->BufferIndex = 1;
}
}
}
}
static void ProcessPortList(void)
{
static fd_set MyFdSet;
int i, rc;
struct timeval Timeout;
Timeout.tv_sec = 1;
Timeout.tv_usec = 0;
FD_ZERO(&MyFdSet);
for (i=0; i<NbPort; i++) {
if (PortList[i]->Fd >= 0 && PortList[i]->is_active) FD_SET(PortList[i]->Fd, &MyFdSet);
}
rc = select(FD_SETSIZE, &MyFdSet, NULL, NULL, &Timeout);
if (VerboseMode) printf("select : rc=%d\n", rc);
if (!rc ) TickReopen();
if (rc > 0) {
for (i=0; i<NbPort && rc; i++) {
if (PortList[i]->Fd < 0) continue;
if (FD_ISSET(PortList[i]->Fd, &MyFdSet)) {
ProcessInput(i);
rc--;
}
}
}
}
static void ProcessArgv(int argc, char *argv[])
{
int opt;
int i=0;
int ptmxdevices = 0;
while ((opt = getopt(argc, argv, "vf:p:")) != -1) {
switch (opt) {
case 'v':
VerboseMode = 1;
break;
case 'f':
MaxFrameSize = atoi(argv[optind]);
break;
case 'p':
ptmxdevices = atoi(optarg);
if (ptmxdevices < 1) {
fprintf(stderr, "error: too many devices\n");
exit(1);
}
for (i=0; i < ptmxdevices; i++)
NewPort("/dev/ptmx");
break;
default:
fprintf(stderr, "Invalid option %s\n", argv[optind]);
Usage();
exit(1);
}
}
while (optind < argc)
NewPort(argv[optind++]);
if (NbPort < 2) {
fprintf(stderr, "This multiplexer needs at least two pty's\n");
exit(1);
}
}
int main(int argc, char *argv[])
{
if (argc < 2) {
Banner(0);
Usage();
}
else {
Banner(1);
}
ProcessArgv(argc, argv);
while (1) ProcessPortList();
return 0;
}
|