Debian Codegraph @

kiss/mkiss.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * mkiss.c
3 * Fake out AX.25 code into supporting dual port TNCS by routing serial
4 * port data to/from two pseudo ttys.
5 *
6 * Version 1.03
7 *
8 * N0BEL
9 * Kevin Uhlir
10 * [email protected]
11 *
12 * 1.01 12/30/95 Ron Curry - Fixed FD_STATE bug where FD_STATE was being used for
13 * three state machines causing spurious writes to wrong TNC port. FD_STATE is
14 * now used for real serial port, FD0_STATE for first pseudo tty, and FD1_STATE
15 * for second pseudo tty. This was an easy fix but a MAJOR bug.
16 *
17 * 1.02 3/1/96 Jonathan Naylor - Make hardware handshaking default to off.
18 * Allowed for true multiplexing of the data from the two pseudo ttys.
19 * Numerous formatting changes.
20 *
21 * 1.03 4/20/96 Tomi Manninen - Rewrote KISS en/decapsulation (much of the
22 * code taken from linux/drivers/net/slip.c). Added support for all the 16
23 * possible kiss ports and G8BPQ-style checksumming on ttyinterface. Now
24 * mkiss also sends a statistic report to stderr if a SIGUSR1 is sent to it.
25 *
26 * 1.04 25/5/96 Jonathan Naylor - Added check for UUCP style tty lock files.
27 * As they are now supported by kissattach as well.
28 *
29 * 1.05 20/8/96 Jonathan Naylor - Convert to becoming a daemon and added
30 * system logging.
31 *
32 * 1.06 23/11/96 Tomi Manninen - Added simple support for polled kiss.
33 *
34 * 1.07 12/24/97 Deti Fliegl - Added Flexnet/BayCom CRC mode with commandline
35 * parameter -f
36 *
37 * 1.08 xx/xx/99 Tom Mazouch - Adjustable poll interval
38 */
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <sys/time.h>
45 #include <time.h>
46 #include <sys/types.h>
47 #include <unistd.h>
48 #include <sys/ioctl.h>
49 #include <termios.h>
50 #include <fcntl.h>
51 #include <signal.h>
52 #include <ctype.h>
53 #include <syslog.h>
54 #include <limits.h>
55
56 #include <netax25/ttyutils.h>
57 #include <netax25/daemon.h>
58
59 #include <config.h>cxx . . $ `<file>/config.h`/
60 #include <scm-version.h>cxx . . $ `<file>/scm-version.h`/
61
62 #define FLEX_CRCcxx . . $ `kiss/mkiss.c:62:9`! 2
63 #define G8BPQ_CRCcxx . . $ `kiss/mkiss.c:63:9`! 1
64
65 #define SIZEcxx . . $ `kiss/mkiss.c:65:9`! 4096
66
67 #define FENDcxx . . $ `kiss/mkiss.c:67:9`! 0300 /* Frame End (0xC0) */
68 #define FESCcxx . . $ `kiss/mkiss.c:68:9`! 0333 /* Frame Escape (0xDB) */
69 #define TFENDcxx . . $ `kiss/mkiss.c:69:9`! 0334 /* Transposed Frame End (0xDC) */
70 #define TFESCcxx . . $ `kiss/mkiss.c:70:9`! 0335 /* Transposed Frame Escape (0xDD) */
71
72 #define ACKREQcxx . . $ `kiss/mkiss.c:72:9`! 0x0C
73 #define POLLcxx . . $ `kiss/mkiss.c:73:9`! 0x0E
74
75 /*
76 * Keep these off the stack.
77 */
78 static unsigned char ibufcxx . . $ ibuf.[SIZEcxx . . $ `kiss/mkiss.c:65:9`!]; /* buffer for input operations */
79 static unsigned char obufcxx . . $ obuf.[SIZEcxx . . $ `kiss/mkiss.c:65:9`!]; /* buffer for kiss_tx() */
80
81 static int crc_errorscxx . . $ crc_errors.;
82 static int invalid_portscxx . . $ invalid_ports.;
83 static int return_pollscxx . . $ return_polls. ;
84
85 static char *usage_stringcxx . . $ usage_string. = "usage: mkiss [-p interval] [-c] [-f] [-h] [-l] [-s speed] [-v] [-x <num_ptmx_devices>] ttyinterface pty ..\n";
86
87 static int dump_reportcxx . . $ dump_report. = FALSEcxx . . $ `<external>/9611143140894422846/ttyutils.h:32:9`!;
88
89 static int loggingcxx . . $ logging. = FALSEcxx . . $ `<external>/9611143140894422846/ttyutils.h:32:9`!;
90 static int crcflagcxx . . $ crcflag. = FALSEcxx . . $ `<external>/9611143140894422846/ttyutils.h:32:9`!;
91 static int hwflagcxx . . $ hwflag. = FALSEcxx . . $ `<external>/9611143140894422846/ttyutils.h:32:9`!;
92 static int pollspeedcxx . . $ pollspeed.;
93
94 /* CRC-stuff */
95 typedef unsigned short int u16cxx . . $ u16#;
96 #define CRCTYPcxx . . $ `kiss/mkiss.c:96:9`! 0x20
97 static u16cxx . . $ u16# crctabcxx . . $ crctab.[256];
98
99 struct ifacecxx . . $ iface#
100 {
101 char *namecxx . . $ iface#name.; /* Interface name (/dev/???) */
102 int fdcxx . . $ iface#fd.; /* File descriptor */
103 int escapedcxx . . $ iface#escaped.; /* FESC received? */
104 u16cxx . . $ u16# crccxx . . $ iface#crc.; /* Incoming frame crc */
105 unsigned char obufcxx . . $ iface#obuf.[SIZEcxx . . $ `kiss/mkiss.c:65:9`!]; /* TX buffer */
106 unsigned char *optrcxx . . $ iface#optr.; /* Next byte to transmit */
107 unsigned int errorscxx . . $ iface#errors.; /* KISS protocol error count */
108 unsigned int nondatacxx . . $ iface#nondata.; /* Non-data frames rx count */
109 unsigned int rxpacketscxx . . $ iface#rxpackets.; /* RX frames count */
110 unsigned int txpacketscxx . . $ iface#txpackets.; /* TX frames count */
111 unsigned long rxbytescxx . . $ iface#rxbytes.; /* RX bytes count */
112 unsigned long txbytescxx . . $ iface#txbytes.; /* TX bytes count */
113 char nameptscxx . . $ iface#namepts.[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!]; /* name of the unix98 pts slaves, which
114 * the client has to use */
115 };
116
117 static struct ifacecxx . . $ iface# *ttycxx . . $ tty.;
118 static struct ifacecxx . . $ iface# *ptycxx . . $ pty.[16];
119 static int numptyscxx . . $ numptys.;
120
121 static void init_crccxx . . $ init_crc(49f6e7a06ebc5aa8).(void)
122 {
123 short int i, j;
124 u16cxx . . $ u16# accum, data;
125
126 for (i = 0; i < 256; i++) { /* fill table with CRC of values... */
127 accum = 0xffff;
128 data = i;
129 for (j = 0; j < 8; ++j) {
130 if ((data^accum) & 0x0001)
131 /* if msb of data^accum is TRUE */
132 /* then shift and subtract poly */
133 accum = (accum >> 1) ^ 0x8408;
134 else
135 /* otherwise: transparent shift */
136 accum >>= 1;
137 data >>= 1; /* move up next bit for XOR */
138 }
139 crctabcxx . . $ crctab.[i] = accum;
140 }
141 }
142
143 static int pollcxx . . $ poll(9b79fb6aee4c0440).(int fd, int ports)
144 {
145 unsigned char buffer[3];
146 static int port;
147
148 buffer[0] = FENDcxx . . $ `kiss/mkiss.c:67:9`!;
149 buffer[1] = POLLcxx . . $ `kiss/mkiss.c:73:9`! | (port << 4);
150 buffer[2] = FENDcxx . . $ `kiss/mkiss.c:67:9`!;
151 if (writecxx . . $ write(41184a59d38b4f65).(fd, buffer, 3) == -1) {
152 perrorcxx . . $ perror(85c52e162fed56f9).("mkiss: poll: write");
153 return 1;
154 }
155 if (++port >= ports)
156 port = 0;
157 return 0;
158 }
159
160 static int put_ubytecxx . . $ put_ubyte(459f97b7209a254a).(unsigned char* s, u16cxx . . $ u16#* crc, unsigned char c, int usecrc)
161 {
162 int len = 1;
163
164 if (c == FENDcxx . . $ `kiss/mkiss.c:67:9`!) {
165 *s++ = FESCcxx . . $ `kiss/mkiss.c:68:9`!;
166 *s++ = TFENDcxx . . $ `kiss/mkiss.c:69:9`!;
167 len++;
168 } else {
169 *s++ = c;
170 if (c == FESCcxx . . $ `kiss/mkiss.c:68:9`!) {
171 *s++ = TFESCcxx . . $ `kiss/mkiss.c:70:9`!;
172 len++;
173 }
174 }
175
176 switch (usecrc) {
177 case G8BPQ_CRCcxx . . $ `kiss/mkiss.c:63:9`!:
178 *crc ^= c; /* Adjust checksum */
179 break;
180 case FLEX_CRCcxx . . $ `kiss/mkiss.c:62:9`!:
181 *crc = (*crc<<8)^crctabcxx . . $ crctab.[(*crc>>8)^((u16cxx . . $ u16#)((c)&255))];
182 break;
183 }
184
185 return len;
186 }
187
188 static int kiss_txcxx . . $ kiss_tx(ff42464727bfcdd6).(int fd, int port, unsigned char *s, int len, int usecrc)
189 {
190 unsigned char *ptr = obufcxx . . $ obuf.;
191 unsigned char c, cmd;
192 u16cxx . . $ u16# crc = 0;
193 int i;
194
195 cmd = s[0] & 0x0F;
196
197 /* Non-data frames don't get a checksum byte */
198 if (usecrc == G8BPQ_CRCcxx . . $ `kiss/mkiss.c:63:9`! && cmd != 0 && cmd != ACKREQcxx . . $ `kiss/mkiss.c:72:9`!)
199 usecrc = FALSEcxx . . $ `<external>/9611143140894422846/ttyutils.h:32:9`!;
200
201 /*
202 * Send an initial FEND character to flush out any
203 * data that may have accumulated in the receiver
204 * due to line noise.
205 */
206 *ptr++ = FENDcxx . . $ `kiss/mkiss.c:67:9`!;
207
208 if (usecrc == FLEX_CRCcxx . . $ `kiss/mkiss.c:62:9`!) {
209 crc = 0xffff;
210 ptr += put_ubytecxx . . $ put_ubyte(459f97b7209a254a).(ptr, &crc, CRCTYPcxx . . $ `kiss/mkiss.c:96:9`!, usecrc);
211 c = *s++;
212 } else {
213 c = *s++;
214 c = (c & 0x0F) | (port << 4);
215 ptr += put_ubytecxx . . $ put_ubyte(459f97b7209a254a).(ptr, &crc, c, usecrc);
216 }
217
218 /*
219 * For each byte in the packet, send the appropriate
220 * character sequence, according to the SLIP protocol.
221 */
222 for (i = 0; i < len - 1; i++)
223 ptr += put_ubytecxx . . $ put_ubyte(459f97b7209a254a).(ptr, &crc, s[i], usecrc);
224
225 /*
226 * Now the checksum...
227 */
228 switch (usecrc) {
229 case G8BPQ_CRCcxx . . $ `kiss/mkiss.c:63:9`!:
230 c = crc & 0xFF;
231 ptr += put_ubytecxx . . $ put_ubyte(459f97b7209a254a).(ptr, &crc, c, usecrc);
232 break;
233 case FLEX_CRCcxx . . $ `kiss/mkiss.c:62:9`!:
234 {
235 u16cxx . . $ u16# u = crc;
236 ptr += put_ubytecxx . . $ put_ubyte(459f97b7209a254a).(ptr, &crc, u / 256, usecrc);
237 ptr += put_ubytecxx . . $ put_ubyte(459f97b7209a254a).(ptr, &crc, u & 255, usecrc);
238 }
239 break;
240 }
241
242 *ptr++ = FENDcxx . . $ `kiss/mkiss.c:67:9`!;
243 return writecxx . . $ write(41184a59d38b4f65).(fd, obufcxx . . $ obuf., ptr - obufcxx . . $ obuf.);
244 }
245
246 static int kiss_rxcxx . . $ kiss_rx(6d282942b045e3b1).(struct ifacecxx . . $ iface# *ifp, unsigned char c, int usecrc)
247 {
248 int len;
249
250 switch (c) {
251 case FENDcxx . . $ `kiss/mkiss.c:67:9`!:
252 len = ifp->optrcxx . . $ iface#optr. - ifp->obufcxx . . $ iface#obuf.;
253
254 if (len != 0 && ifp->escapedcxx . . $ iface#escaped.) { /* protocol error... */
255 len = 0; /* ...drop frame */
256 ifp->errorscxx . . $ iface#errors.++;
257 }
258
259 if (len != 0) {
260 switch (usecrc) {
261 case G8BPQ_CRCcxx . . $ `kiss/mkiss.c:63:9`!:
262 if ((ifp->obufcxx . . $ iface#obuf.[0] & 0x0F) != 0) {
263 /*
264 * Non-data frames don't have checksum.
265 */
266 usecrc = 0;
267 if ((ifp->obufcxx . . $ iface#obuf.[0] & 0x0F) == POLLcxx . . $ `kiss/mkiss.c:73:9`!) {
268 /* drop returned polls */
269 len = 0;
270 return_pollscxx . . $ return_polls.++;
271 } else
272 ifp->nondatacxx . . $ iface#nondata.++;
273 } else {
274 if ((ifp->crccxx . . $ iface#crc. & 0xFF) != 0) {
275 /* checksum failed... */
276 /* ...drop frame */
277 len = 0;
278 crc_errorscxx . . $ crc_errors.++;
279 } else
280 /* delete checksum byte */
281 len--;
282 }
283 break;
284 case FLEX_CRCcxx . . $ `kiss/mkiss.c:62:9`!:
285 if (len > 14 && ifp->crccxx . . $ iface#crc. == 0x7070) {
286 len -= 2;
287 *ifp->obufcxx . . $ iface#obuf. = 0;
288 } else {
289 len = 0;
290 crc_errorscxx . . $ crc_errors.++;
291 }
292 break;
293 }
294 }
295
296 if (len != 0) {
297 ifp->rxpacketscxx . . $ iface#rxpackets.++;
298 ifp->rxbytescxx . . $ iface#rxbytes. += len;
299 }
300
301 /*
302 * Clean up.
303 */
304 ifp->optrcxx . . $ iface#optr. = ifp->obufcxx . . $ iface#obuf.;
305 if (usecrc == FLEX_CRCcxx . . $ `kiss/mkiss.c:62:9`!)
306 ifp->crccxx . . $ iface#crc. = 0xffff;
307 else
308 ifp->crccxx . . $ iface#crc. = 0;
309 ifp->escapedcxx . . $ iface#escaped. = FALSEcxx . . $ `<external>/9611143140894422846/ttyutils.h:32:9`!;
310 return len;
311 case FESCcxx . . $ `kiss/mkiss.c:68:9`!:
312 ifp->escapedcxx . . $ iface#escaped. = TRUEcxx . . $ `<external>/9611143140894422846/ttyutils.h:28:9`!;
313 return 0;
314 case TFESCcxx . . $ `kiss/mkiss.c:70:9`!:
315 if (ifp->escapedcxx . . $ iface#escaped.) {
316 ifp->escapedcxx . . $ iface#escaped. = FALSEcxx . . $ `<external>/9611143140894422846/ttyutils.h:32:9`!;
317 c = FESCcxx . . $ `kiss/mkiss.c:68:9`!;
318 }
319 break;
320 case TFENDcxx . . $ `kiss/mkiss.c:69:9`!:
321 if (ifp->escapedcxx . . $ iface#escaped.) {
322 ifp->escapedcxx . . $ iface#escaped. = FALSEcxx . . $ `<external>/9611143140894422846/ttyutils.h:32:9`!;
323 c = FENDcxx . . $ `kiss/mkiss.c:67:9`!;
324 }
325 break;
326 default:
327 if (ifp->escapedcxx . . $ iface#escaped.) { /* protocol error... */
328 ifp->escapedcxx . . $ iface#escaped. = FALSEcxx . . $ `<external>/9611143140894422846/ttyutils.h:32:9`!;
329 ifp->errorscxx . . $ iface#errors.++;
330 }
331 break;
332 }
333
334 *ifp->optrcxx . . $ iface#optr.++ = c;
335
336 switch (usecrc) {
337 case G8BPQ_CRCcxx . . $ `kiss/mkiss.c:63:9`!:
338 ifp->crccxx . . $ iface#crc. ^= c;
339 break;
340 case FLEX_CRCcxx . . $ `kiss/mkiss.c:62:9`!:
341 ifp->crccxx . . $ iface#crc. = (ifp->crccxx . . $ iface#crc. << 8) ^ crctabcxx . . $ crctab.[(ifp->crccxx . . $ iface#crc. >> 8) ^ c];
342 break;
343 default:
344 break;
345 }
346
347 return 0;
348 }
349
350 static void sigterm_handlercxx . . $ sigterm_handler(d4f767463ce0a6b3).(int sig)
351 {
352 int i;
353
354 if (loggingcxx . . $ logging.) {
355 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "terminating on SIGTERM\n");
356 closelogcxx . . $ closelog(49f6e7a06ebc5aa8).();
357 }
358
359 tty_unlockcxx . . $ tty_unlock(1ef8e6ec083aabb9).(ttycxx . . $ tty.->namecxx . . $ iface#name.);
360 closecxx . . $ close(7864480464b09eea).(ttycxx . . $ tty.->fdcxx . . $ iface#fd.);
361 freecxx . . $ free(5d22bdacc48458e8).(ttycxx . . $ tty.);
362
363 for (i = 0; i < numptyscxx . . $ numptys.; i++) {
364 if (ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd. == -1)
365 continue;
366 if (ptycxx . . $ pty.[i]->nameptscxx . . $ iface#namepts.[0] != '\0')
367 continue;
368 tty_unlockcxx . . $ tty_unlock(1ef8e6ec083aabb9).(ptycxx . . $ pty.[i]->namecxx . . $ iface#name.);
369 closecxx . . $ close(7864480464b09eea).(ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd.);
370 freecxx . . $ free(5d22bdacc48458e8).(ptycxx . . $ pty.[i]);
371 }
372
373 exitcxx . . $ exit(3ca292c338df9bb3).(0);
374 }
375
376 static void sigusr1_handlercxx . . $ sigusr1_handler(d4f767463ce0a6b3).(int sig)
377 {
378 signalcxx . . $ signal(c2d516773f91faf8).(SIGUSR1cxx . . $ `<external>/1457673850468744829/signum-arch.h:49:9`!, sigusr1_handlercxx . . $ sigusr1_handler(d4f767463ce0a6b3).);
379 dump_reportcxx . . $ dump_report. = TRUEcxx . . $ `<external>/9611143140894422846/ttyutils.h:28:9`!;
380 }
381
382 static void reportcxx . . $ report(49f6e7a06ebc5aa8).(void)
383 {
384 int i;
•385 time_tcxx . . $ time_t# t;
386
387 timecxx . . $ time(7f38134db6521d23).(&t);
388 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "version %s.", FULL_VERcxx . . $ `scm-version.h:6:9`!);
389 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "Status report at %s", ctimecxx . . $ ctime(4e4bbfda8d4b3d77).(&t));
390 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "Hardware handshaking %sabled.",
391 hwflagcxx . . $ hwflag. ? "en" : "dis");
392 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "G8BPQ checksumming %sabled.",
393 crcflagcxx . . $ crcflag. == G8BPQ_CRCcxx . . $ `kiss/mkiss.c:63:9`! ? "en" : "dis");
394 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "FLEX checksumming %sabled.",
395 crcflagcxx . . $ crcflag. == FLEX_CRCcxx . . $ `kiss/mkiss.c:62:9`! ? "en" : "dis");
396
397 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "polling %sabled.",
398 pollspeedcxx . . $ pollspeed. ? "en" : "dis");
399 if (pollspeedcxx . . $ pollspeed.)
400 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "Poll interval %d00ms", pollspeedcxx . . $ pollspeed.);
401 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "ttyinterface is %s (fd=%d)", ttycxx . . $ tty.->namecxx . . $ iface#name., ttycxx . . $ tty.->fdcxx . . $ iface#fd.);
402 for (i = 0; i < numptyscxx . . $ numptys.; i++)
403 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "pty%d is %s (fd=%d)", i, ptycxx . . $ pty.[i]->namecxx . . $ iface#name.,
404 ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd.);
405 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "Checksum errors: %d", crc_errorscxx . . $ crc_errors.);
406 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "Invalid ports: %d", invalid_portscxx . . $ invalid_ports.);
407 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "Returned polls: %d", return_pollscxx . . $ return_polls.);
408 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "Interface TX frames TX bytes RX frames RX bytes Errors");
409 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "%-11s %-9u %-9lu %-9u %-9lu %u",
410 ttycxx . . $ tty.->namecxx . . $ iface#name.,
411 ttycxx . . $ tty.->txpacketscxx . . $ iface#txpackets., ttycxx . . $ tty.->txbytescxx . . $ iface#txbytes.,
412 ttycxx . . $ tty.->rxpacketscxx . . $ iface#rxpackets., ttycxx . . $ tty.->rxbytescxx . . $ iface#rxbytes.,
413 ttycxx . . $ tty.->errorscxx . . $ iface#errors.);
414 for (i = 0; i < numptyscxx . . $ numptys.; i++) {
415 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "%-11s %-9u %-9lu %-9u %-9lu %u",
416 ptycxx . . $ pty.[i]->namecxx . . $ iface#name.,
417 ptycxx . . $ pty.[i]->txpacketscxx . . $ iface#txpackets., ptycxx . . $ pty.[i]->txbytescxx . . $ iface#txbytes.,
418 ptycxx . . $ pty.[i]->rxpacketscxx . . $ iface#rxpackets., ptycxx . . $ pty.[i]->rxbytescxx . . $ iface#rxbytes.,
419 ptycxx . . $ pty.[i]->errorscxx . . $ iface#errors.);
420 }
421 }
422
423 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[])
424 {
425 unsigned char *icp;
426 int topfd;
427 fd_setcxx . . $ fd_set# readfd;
428 struct timevalcxx . . $ timeval# timeout, pollinterval;
429 int retval, i, size, len;
430 int speed = -1;
431 int ptmxdevices = 0;
432 char *npts;
433 int wrote_info = 0;
434
435 while ((size = getoptcxx . . $ getopt(2483c31ff9bab884).(argc, argv, "cfhlp:s:vx:")) != -1) {
436 switch (size) {
437 case 'c':
438 crcflagcxx . . $ crcflag. = G8BPQ_CRCcxx . . $ `kiss/mkiss.c:63:9`!;
439 break;
440 case 'f':
441 crcflagcxx . . $ crcflag. = FLEX_CRCcxx . . $ `kiss/mkiss.c:62:9`!;
442 break;
443 case 'h':
444 hwflagcxx . . $ hwflag. = TRUEcxx . . $ `<external>/9611143140894422846/ttyutils.h:28:9`!;
445 break;
446 case 'l':
447 loggingcxx . . $ logging. = TRUEcxx . . $ `<external>/9611143140894422846/ttyutils.h:28:9`!;
448 break;
449 case 'p':
450 pollspeedcxx . . $ pollspeed. = atoicxx . . $ atoi(5c4306d8553b3696).(optargcxx . . $ optarg.);
451 pollinterval.tv_seccxx . . $ timeval#tv_sec. = pollspeedcxx . . $ pollspeed. / 10;
452 pollinterval.tv_useccxx . . $ timeval#tv_usec. = (pollspeedcxx . . $ pollspeed. % 10) * 100000L;
453 break;
454 case 's':
455 speed = atoicxx . . $ atoi(5c4306d8553b3696).(optargcxx . . $ optarg.);
456 break;
457 case 'x':
458 ptmxdevices = atoicxx . . $ atoi(5c4306d8553b3696).(optargcxx . . $ optarg.);
459 if (ptmxdevices < 1 || ptmxdevices > 16) {
460 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "mkiss: too %s devices\n", ptmxdevices < 1 ? "few" : "many");
461 return 1;
462 }
463 break;
464 case 'v':
465 printfcxx . . $ printf(581fddd72a8db8fc).("mkiss: %s\n", FULL_VERcxx . . $ `scm-version.h:6:9`!);
466 return 1;
467 case ':':
468 case '?':
469 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s", usage_stringcxx . . $ usage_string.);
470 return 1;
471 }
472 }
473
474 if ((argc - optindcxx . . $ optind.) < 2 && ptmxdevices == 0) {
475 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s", usage_stringcxx . . $ usage_string.);
476 return 1;
477 }
478
479 if ((argc - optindcxx . . $ optind.) < 1 && ptmxdevices > 0) {
480 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s", usage_stringcxx . . $ usage_string.);
481 return 1;
482 }
483
484 numptyscxx . . $ numptys. = argc - optindcxx . . $ optind. - 1;
485 if ((numptyscxx . . $ numptys. + ptmxdevices) > 16) {
486 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "mkiss: max 16 pty interfaces allowed.\n");
487 return 1;
488 }
489
490 /*
491 * Check for lock files before opening any TTYs
492 */
493 if (tty_is_lockedcxx . . $ tty_is_locked(1ef8e6ec083aabb9).(argv[optindcxx . . $ optind.])) {
494 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "mkiss: tty %s is locked by another process\n", argv[optindcxx . . $ optind.]);
495 return 1;
496 }
497 for (i = 0; i < numptyscxx . . $ numptys.; i++) {
498 if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).("/dev/ptmx", argv[optindcxx . . $ optind. + i + 1]))
499 continue;
500 if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).("none", argv[optindcxx . . $ optind. + i + 1]))
501 continue;
502 if (tty_is_lockedcxx . . $ tty_is_locked(1ef8e6ec083aabb9).(argv[optindcxx . . $ optind. + i + 1])) {
503 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "mkiss: pty %s is locked by another process\n", argv[optindcxx . . $ optind. + i + 1]);
504 return 1;
505 }
506 }
507
508 /*
509 * Open and configure the tty interface. Open() is
510 * non-blocking so it won't block regardless of the modem
511 * status lines.
512 */
513 ttycxx . . $ tty. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(struct ifacecxx . . $ iface#));
514 if (ttycxx . . $ tty. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
515 perrorcxx . . $ perror(85c52e162fed56f9).("mkiss: malloc");
516 return 1;
517 }
518
519 ttycxx . . $ tty.->fdcxx . . $ iface#fd. = opencxx . . $ open(e156e3aa1e5c4ad1).(argv[optindcxx . . $ optind.], O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_NDELAYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:65:10`!);
520 if (ttycxx . . $ tty.->fdcxx . . $ iface#fd. == -1) {
521 perrorcxx . . $ perror(85c52e162fed56f9).("mkiss: open");
522 return 1;
523 }
524
525 ttycxx . . $ tty.->namecxx . . $ iface#name. = argv[optindcxx . . $ optind.];
526 tty_rawcxx . . $ tty_raw(9b79fb6aee4c0440).(ttycxx . . $ tty.->fdcxx . . $ iface#fd., hwflagcxx . . $ hwflag.);
527 if (speed != -1 && !tty_speedcxx . . $ tty_speed(9b79fb6aee4c0440).(ttycxx . . $ tty.->fdcxx . . $ iface#fd., speed)) {
528 closecxx . . $ close(7864480464b09eea).(ttycxx . . $ tty.->fdcxx . . $ iface#fd.);
529 return 1;
530 }
531 ttycxx . . $ tty.->optrcxx . . $ iface#optr. = ttycxx . . $ tty.->obufcxx . . $ iface#obuf.;
532 topfd = ttycxx . . $ tty.->fdcxx . . $ iface#fd.;
533 ttycxx . . $ tty.->nameptscxx . . $ iface#namepts.[0] = '\0';
534
535 /*
536 * Make it block again...
537 */
538 fcntlcxx . . $ fcntl(cac4efb70f20a0fb).(ttycxx . . $ tty.->fdcxx . . $ iface#fd., F_SETFLcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:169:9`!, 0);
539
540 /*
541 * Open and configure the pty interfaces
542 */
543 for (i = 0; i < numptyscxx . . $ numptys.+ptmxdevices; i++) {
544 static char name_ptmx[] = "/dev/ptmx";
545 char *pty_name = (i < numptyscxx . . $ numptys. ? argv[optindcxx . . $ optind.+i+1] : name_ptmx);
546
547 ptycxx . . $ pty.[i] = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(struct ifacecxx . . $ iface#));
548 if (ptycxx . . $ pty.[i] == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
549 perrorcxx . . $ perror(85c52e162fed56f9).("mkiss: malloc");
550 return 1;
551 }
552 if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(pty_name, "none")) {
553 ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd. = -1;
554 strcpycxx . . $ strcpy(b7b3a83273496486).(ptycxx . . $ pty.[i]->nameptscxx . . $ iface#namepts., "none");
555 } else {
556 ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd. = opencxx . . $ open(e156e3aa1e5c4ad1).(pty_name, O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`!);
557 if (ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd. == -1) {
558 perrorcxx . . $ perror(85c52e162fed56f9).("mkiss: open");
559 freecxx . . $ free(5d22bdacc48458e8).(ptycxx . . $ pty.[i]);
560 ptycxx . . $ pty.[i] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
561 return 1;
562 }
563 tty_rawcxx . . $ tty_raw(9b79fb6aee4c0440).(ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd., FALSEcxx . . $ `<external>/9611143140894422846/ttyutils.h:32:9`!);
564 topfd = (ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd. > topfd) ? ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd. : topfd;
565 ptycxx . . $ pty.[i]->nameptscxx . . $ iface#namepts.[0] = '\0';
566 }
567 ptycxx . . $ pty.[i]->namecxx . . $ iface#name. = pty_name;
568 ptycxx . . $ pty.[i]->optrcxx . . $ iface#optr. = ptycxx . . $ pty.[i]->obufcxx . . $ iface#obuf.;
569 if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(ptycxx . . $ pty.[i]->namecxx . . $ iface#name., "/dev/ptmx")) {
570 /* get name of pts-device */
571 npts = ptsnamecxx . . $ ptsname(ff224f080444ee45).(ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd.);
572 if (npts == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
573 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "mkiss: Cannot get name of pts-device.\n");
574 freecxx . . $ free(5d22bdacc48458e8).(ptycxx . . $ pty.[i]);
575 ptycxx . . $ pty.[i] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
576 return 1;
577 }
578 strncpycxx . . $ strncpy(255158a415b673b0).(ptycxx . . $ pty.[i]->nameptscxx . . $ iface#namepts., npts, PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!-1);
579 ptycxx . . $ pty.[i]->nameptscxx . . $ iface#namepts.[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!-1] = '\0';
580
581 /* unlock pts-device */
582 if (unlockptcxx . . $ unlockpt(7864480464b09eea).(ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd.) == -1) {
583 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "mkiss: Cannot unlock pts-device %s\n", ptycxx . . $ pty.[i]->nameptscxx . . $ iface#namepts.);
584 freecxx . . $ free(5d22bdacc48458e8).(ptycxx . . $ pty.[i]);
585 ptycxx . . $ pty.[i] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
586 return 1;
587 }
588 if (wrote_info == 0)
589 printfcxx . . $ printf(581fddd72a8db8fc).("\nAwaiting client connects on:\n");
590 else
591 printfcxx . . $ printf(581fddd72a8db8fc).(" ");
592 printfcxx . . $ printf(581fddd72a8db8fc).("%s", ptycxx . . $ pty.[i]->nameptscxx . . $ iface#namepts.);
593 wrote_info = 1;
594 }
595 }
596
597 if (wrote_info > 0)
598 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
599
600 numptyscxx . . $ numptys.=numptyscxx . . $ numptys.+ptmxdevices;
601
602 /*
603 * Now all the ports are open, lock them.
604 */
605 tty_lockcxx . . $ tty_lock(1ef8e6ec083aabb9).(ttycxx . . $ tty.->namecxx . . $ iface#name.);
606 for (i = 0; i < numptyscxx . . $ numptys.; i++) {
607 if (ptycxx . . $ pty.[i]->nameptscxx . . $ iface#namepts.[0] == '\0')
608 tty_lockcxx . . $ tty_lock(1ef8e6ec083aabb9).(ptycxx . . $ pty.[i]->namecxx . . $ iface#name.);
609 }
610
611 if (loggingcxx . . $ logging.) {
612 openlogcxx . . $ openlog(7b85637613228108).("mkiss", LOG_PIDcxx . . $ `<external>/6168640806541382331/syslog.h:162:9`!, LOG_DAEMONcxx . . $ `<external>/6168640806541382331/syslog.h:96:9`!);
613 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "starting");
614 }
615
616 if (wrote_info > 0) {
617 fflushcxx . . $ fflush(89bc1efcdac0ca22).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
618 fflushcxx . . $ fflush(89bc1efcdac0ca22).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
619 closecxx . . $ close(7864480464b09eea).(0);
620 closecxx . . $ close(7864480464b09eea).(1);
621 closecxx . . $ close(7864480464b09eea).(2);
622 }
623
624 signalcxx . . $ signal(c2d516773f91faf8).(SIGHUPcxx . . $ `<external>/11773118241360591791/signum-generic.h:56:9`!, SIG_IGNcxx . . $ `<external>/11773118241360591791/signum-generic.h:30:9`!);
625 signalcxx . . $ signal(c2d516773f91faf8).(SIGUSR1cxx . . $ `<external>/1457673850468744829/signum-arch.h:49:9`!, sigusr1_handlercxx . . $ sigusr1_handler(d4f767463ce0a6b3).);
626 signalcxx . . $ signal(c2d516773f91faf8).(SIGTERMcxx . . $ `<external>/11773118241360591791/signum-generic.h:53:9`!, sigterm_handlercxx . . $ sigterm_handler(d4f767463ce0a6b3).);
627
628 if (!daemon_startcxx . . $ daemon_start(7864480464b09eea).(FALSEcxx . . $ `<external>/9611143140894422846/ttyutils.h:32:9`!)) {
629 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "mkiss: cannot become a daemon\n");
630 return 1;
631 }
632
633 init_crccxx . . $ init_crc(49f6e7a06ebc5aa8).();
634 /*
635 * Loop until an error occurs on a read.
636 */
637 while (TRUEcxx . . $ `<external>/9611143140894422846/ttyutils.h:28:9`!) {
638 FD_ZEROcxx . . $ `<external>/946878812840035423/select.h:88:9`!(&readfd);
639 FD_SETcxx . . $ `<external>/946878812840035423/select.h:85:9`!(tty->fd, &readfd);
640 for (i = 0; i < numptyscxx . . $ numptys.; i++)
641 if (ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd. != -1)
642 FD_SETcxx . . $ `<external>/946878812840035423/select.h:85:9`!(pty[i]->fd, &readfd);
643
644 if (pollspeedcxx . . $ pollspeed.)
645 timeout = pollinterval;
646
647 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0;
648 retval = selectcxx . . $ select(6d24c7514c8a5b88).(topfd + 1, &readfd, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, pollspeedcxx . . $ pollspeed. ? &timeout : NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
649
650 if (retval == -1) {
651 if (dump_reportcxx . . $ dump_report.) {
652 if (loggingcxx . . $ logging.)
653 reportcxx . . $ report(49f6e7a06ebc5aa8).();
654 dump_reportcxx . . $ dump_report. = FALSEcxx . . $ `<external>/9611143140894422846/ttyutils.h:32:9`!;
655 continue;
656 } else {
657 perrorcxx . . $ perror(85c52e162fed56f9).("mkiss: select");
658 continue;
659 }
660 }
661
662 /*
663 * Timer expired - let's poll...
664 */
665 if (retval == 0 && pollspeedcxx . . $ pollspeed.) {
666 pollcxx . . $ poll(9b79fb6aee4c0440).(ttycxx . . $ tty.->fdcxx . . $ iface#fd., numptyscxx . . $ numptys.);
667 continue;
668 }
669
670 /*
671 * A character has arrived on the ttyinterface.
672 */
673 if (ttycxx . . $ tty.->fdcxx . . $ iface#fd. > -1 && FD_ISSETcxx . . $ `<external>/946878812840035423/select.h:87:9`!(tty->fd, &readfd)) {
674 if ((size = readcxx . . $ read(7e34d0a36f355389).(ttycxx . . $ tty.->fdcxx . . $ iface#fd., ibufcxx . . $ ibuf., SIZEcxx . . $ `kiss/mkiss.c:65:9`!)) < 0 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!) {
675 if (loggingcxx . . $ logging.)
676 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "tty->fd: %m");
677 break;
678 }
679 for (icp = ibufcxx . . $ ibuf.; size > 0; size--, icp++) {
680 len = kiss_rxcxx . . $ kiss_rx(6d282942b045e3b1).(ttycxx . . $ tty., *icp, crcflagcxx . . $ crcflag.);
681 if (len != 0) {
682 if ((i = (*ttycxx . . $ tty.->obufcxx . . $ iface#obuf. & 0xF0) >> 4) < numptyscxx . . $ numptys.) {
683 if (ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd. != -1) {
684 kiss_txcxx . . $ kiss_tx(ff42464727bfcdd6).(ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd., 0, ttycxx . . $ tty.->obufcxx . . $ iface#obuf., len, FALSEcxx . . $ `<external>/9611143140894422846/ttyutils.h:32:9`!);
685 ptycxx . . $ pty.[i]->txpacketscxx . . $ iface#txpackets.++;
686 ptycxx . . $ pty.[i]->txbytescxx . . $ iface#txbytes. += len;
687 }
688 } else
689 invalid_portscxx . . $ invalid_ports.++;
690 if (pollspeedcxx . . $ pollspeed.)
691 pollcxx . . $ poll(9b79fb6aee4c0440).(ttycxx . . $ tty.->fdcxx . . $ iface#fd., numptyscxx . . $ numptys.);
692 }
693 }
694 }
695
696 for (i = 0; i < numptyscxx . . $ numptys.; i++) {
697 /*
698 * A character has arrived on pty[i].
699 */
700 if (ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd. > -1 && FD_ISSETcxx . . $ `<external>/946878812840035423/select.h:87:9`!(pty[i]->fd, &readfd)) {
701 if ((size = readcxx . . $ read(7e34d0a36f355389).(ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd., ibufcxx . . $ ibuf., SIZEcxx . . $ `kiss/mkiss.c:65:9`!)) < 0 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!) {
702 if (loggingcxx . . $ logging.)
703 syslogcxx . . $ syslog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "pty[%d]->fd: %m\n", i);
704 goto end;
705 }
706 for (icp = ibufcxx . . $ ibuf.; size > 0; size--, icp++) {
707 len = kiss_rxcxx . . $ kiss_rx(6d282942b045e3b1).(ptycxx . . $ pty.[i], *icp, FALSEcxx . . $ `<external>/9611143140894422846/ttyutils.h:32:9`!);
708 if (len != 0) {
709 kiss_txcxx . . $ kiss_tx(ff42464727bfcdd6).(ttycxx . . $ tty.->fdcxx . . $ iface#fd., i, ptycxx . . $ pty.[i]->obufcxx . . $ iface#obuf., len, crcflagcxx . . $ crcflag.);
710 ttycxx . . $ tty.->txpacketscxx . . $ iface#txpackets.++;
711 ttycxx . . $ tty.->txbytescxx . . $ iface#txbytes. += len;
712 }
713 }
714 }
715 }
716 }
717
718 end:
719 if (loggingcxx . . $ logging.)
720 closelogcxx . . $ closelog(49f6e7a06ebc5aa8).();
721
722 tty_unlockcxx . . $ tty_unlock(1ef8e6ec083aabb9).(ttycxx . . $ tty.->namecxx . . $ iface#name.);
723 closecxx . . $ close(7864480464b09eea).(ttycxx . . $ tty.->fdcxx . . $ iface#fd.);
724 freecxx . . $ free(5d22bdacc48458e8).(ttycxx . . $ tty.);
725
726 for (i = 0; i < numptyscxx . . $ numptys.; i++) {
727 if (ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd. == -1)
728 continue;
729 if (ptycxx . . $ pty.[i]->nameptscxx . . $ iface#namepts.[0] != '\0')
730 continue;
731 tty_unlockcxx . . $ tty_unlock(1ef8e6ec083aabb9).(ptycxx . . $ pty.[i]->namecxx . . $ iface#name.);
732 closecxx . . $ close(7864480464b09eea).(ptycxx . . $ pty.[i]->fdcxx . . $ iface#fd.);
733 freecxx . . $ free(5d22bdacc48458e8).(ptycxx . . $ pty.[i]);
734 }
735
736 return 1;
737 }