Debian Codegraph @

kiss/kissnetd.c

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