1 | /* |
---|
2 | * main.c - Point-to-Point Protocol main module |
---|
3 | * |
---|
4 | * Copyright (c) 1989 Carnegie Mellon University. |
---|
5 | * All rights reserved. |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms are permitted |
---|
8 | * provided that the above copyright notice and this paragraph are |
---|
9 | * duplicated in all such forms and that any documentation, |
---|
10 | * advertising materials, and other materials related to such |
---|
11 | * distribution and use acknowledge that the software was developed |
---|
12 | * by Carnegie Mellon University. The name of the |
---|
13 | * University may not be used to endorse or promote products derived |
---|
14 | * from this software without specific prior written permission. |
---|
15 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
---|
16 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
---|
17 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
---|
18 | */ |
---|
19 | |
---|
20 | #ifndef lint |
---|
21 | /* static char rcsid[] = "$Id$"; */ |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <stdio.h> |
---|
25 | #include <ctype.h> |
---|
26 | /* #include <stdlib.h> */ |
---|
27 | #include <string.h> |
---|
28 | #include <unistd.h> |
---|
29 | #include <signal.h> |
---|
30 | #include <errno.h> |
---|
31 | #include <fcntl.h> |
---|
32 | #include <syslog.h> |
---|
33 | #include <netdb.h> |
---|
34 | #include <pwd.h> |
---|
35 | #include <sys/param.h> |
---|
36 | #include <sys/types.h> |
---|
37 | #include <sys/wait.h> |
---|
38 | #include <sys/time.h> |
---|
39 | #include <sys/resource.h> |
---|
40 | #include <sys/stat.h> |
---|
41 | #include <sys/socket.h> |
---|
42 | #include <sys/types.h> |
---|
43 | #include <netinet/in.h> |
---|
44 | |
---|
45 | #include <rtems.h> |
---|
46 | #include <rtems/rtems_bsdnet.h> |
---|
47 | |
---|
48 | /* #include <stbconfig.h> */ |
---|
49 | #include "pppd.h" |
---|
50 | #include "magic.h" |
---|
51 | #include "fsm.h" |
---|
52 | #include "lcp.h" |
---|
53 | #include "ipcp.h" |
---|
54 | #include "upap.h" |
---|
55 | #include "chap.h" |
---|
56 | #include "ccp.h" |
---|
57 | #include "pathnames.h" |
---|
58 | #include "patchlevel.h" |
---|
59 | |
---|
60 | #ifdef CBCP_SUPPORT |
---|
61 | #include "cbcp.h" |
---|
62 | #endif |
---|
63 | |
---|
64 | #if defined(SUNOS4) |
---|
65 | extern char *strerror(); |
---|
66 | #endif |
---|
67 | |
---|
68 | #ifdef IPX_CHANGE |
---|
69 | #include "ipxcp.h" |
---|
70 | #endif /* IPX_CHANGE */ |
---|
71 | #ifdef AT_CHANGE |
---|
72 | #include "atcp.h" |
---|
73 | #endif |
---|
74 | |
---|
75 | void SetStatusInfo(int state, char * text, int res); |
---|
76 | |
---|
77 | /* prototypes for routines in this file */ |
---|
78 | int connect_script(int fd); |
---|
79 | |
---|
80 | /* interface vars */ |
---|
81 | char ifname[32]; /* Interface name */ |
---|
82 | int interfunit; /* Interface unit number */ |
---|
83 | |
---|
84 | char *progname; /* Name of this program */ |
---|
85 | char hostname[MAXNAMELEN] = "dcna"; /* Our hostname */ |
---|
86 | static char default_devnam[MAXPATHLEN]; /* name of default device */ |
---|
87 | /*static pid_t pid; */ /* Our pid */ |
---|
88 | static uid_t uid; /* Our real user-id */ |
---|
89 | static int conn_running; /* we have a [dis]connector running */ |
---|
90 | |
---|
91 | int ttyfd = -1; /* Serial port file descriptor */ |
---|
92 | mode_t tty_mode = -1; /* Original access permissions to tty */ |
---|
93 | int baud_rate; /* Actual bits/second for serial device */ |
---|
94 | int hungup; /* terminal has been hung up */ |
---|
95 | int privileged; /* we're running as real uid root */ |
---|
96 | int need_holdoff; /* need holdoff period before restarting */ |
---|
97 | int detached; /* have detached from terminal */ |
---|
98 | |
---|
99 | int phase; /* where the link is at */ |
---|
100 | int kill_link; |
---|
101 | int open_ccp_flag; |
---|
102 | |
---|
103 | char **script_env; /* Env. variable values for scripts */ |
---|
104 | int s_env_nalloc; /* # words avail at script_env */ |
---|
105 | |
---|
106 | u_char outpacket_buf[PPP_MRU + PPP_HDRLEN]; /* buffer for outgoing packet */ |
---|
107 | u_char inpacket_buf[PPP_MRU + PPP_HDRLEN]; /* buffer for incoming packet */ |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | char *no_ppp_msg = "lack of PPP\n"; |
---|
112 | |
---|
113 | /* Prototypes for procedures local to this file. */ |
---|
114 | static void cleanup(void); |
---|
115 | static void close_tty __P((void)); |
---|
116 | static void get_input __P((void)); |
---|
117 | static void calltimeout __P((void)); |
---|
118 | static struct timeval *timeleft __P((struct timeval *)); |
---|
119 | static void holdoff_end __P((void *)); |
---|
120 | static void reap_kids __P((void)); |
---|
121 | /* XXX currently unused */ |
---|
122 | #if 0 |
---|
123 | static int device_script __P((char *[], int, int)); |
---|
124 | static void pr_log __P((void *, char *,...)); |
---|
125 | #endif |
---|
126 | |
---|
127 | extern char *ttyname __P((int)); |
---|
128 | extern char *getlogin __P((void)); |
---|
129 | int main __P((int, char *[])); |
---|
130 | |
---|
131 | |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | /* |
---|
136 | * PPP Data Link Layer "protocol" table. |
---|
137 | * One entry per supported protocol. |
---|
138 | * The last entry must be NULL. |
---|
139 | */ |
---|
140 | struct protent *protocols[] = |
---|
141 | { |
---|
142 | &lcp_protent, |
---|
143 | &pap_protent, |
---|
144 | &chap_protent, |
---|
145 | #ifdef CBCP_SUPPORT |
---|
146 | &cbcp_protent, |
---|
147 | #endif |
---|
148 | &ipcp_protent, |
---|
149 | &ccp_protent, |
---|
150 | #ifdef IPX_CHANGE |
---|
151 | &ipxcp_protent, |
---|
152 | #endif |
---|
153 | #ifdef AT_CHANGE |
---|
154 | &atcp_protent, |
---|
155 | #endif |
---|
156 | NULL |
---|
157 | }; |
---|
158 | |
---|
159 | extern int connect_stb(); |
---|
160 | |
---|
161 | |
---|
162 | extern int disconnect_stb(); |
---|
163 | |
---|
164 | |
---|
165 | extern struct in_addr rtems_bsdnet_nameserver[]; |
---|
166 | extern int rtems_bsdnet_nameserver_count; |
---|
167 | extern int __res_init(void); |
---|
168 | |
---|
169 | int pppdmain(argc, argv) |
---|
170 | int argc; |
---|
171 | char *argv[]; |
---|
172 | { |
---|
173 | int i; |
---|
174 | #if 0 |
---|
175 | struct timeval timo; |
---|
176 | #endif |
---|
177 | struct protent *protp; |
---|
178 | struct stat statbuf; |
---|
179 | #if 0 |
---|
180 | char t[100]; |
---|
181 | #endif |
---|
182 | |
---|
183 | |
---|
184 | phase = PHASE_INITIALIZE; |
---|
185 | |
---|
186 | strcpy(default_devnam, "/dev/modem"); |
---|
187 | strcpy(devnam, "/dev/modem"); |
---|
188 | |
---|
189 | script_env = NULL; |
---|
190 | |
---|
191 | /* if (gethostname(hostname, MAXNAMELEN) < 0 ) { |
---|
192 | die(1); |
---|
193 | } |
---|
194 | */ |
---|
195 | hostname[MAXNAMELEN - 1] = 0; |
---|
196 | |
---|
197 | uid = 0; |
---|
198 | privileged = uid == 0; |
---|
199 | |
---|
200 | /* |
---|
201 | * Initialize to the standard option set, then parse, in order, |
---|
202 | * the system options file, the user's options file, |
---|
203 | * the tty's options file, and the command line arguments. |
---|
204 | */ |
---|
205 | for (i = 0; (protp = protocols[i]) != NULL; ++i) |
---|
206 | (*protp->init) (0); |
---|
207 | |
---|
208 | |
---|
209 | |
---|
210 | #if 1 |
---|
211 | if (!ppp_available()) { |
---|
212 | exit(1); |
---|
213 | } |
---|
214 | #endif |
---|
215 | /* |
---|
216 | * Check that the options given are valid and consistent. |
---|
217 | */ |
---|
218 | sys_check_options(); |
---|
219 | auth_check_options(); |
---|
220 | for (i = 0; (protp = protocols[i]) != NULL; ++i) |
---|
221 | if (protp->check_options != NULL) |
---|
222 | (*protp->check_options) (); |
---|
223 | |
---|
224 | /* |
---|
225 | * If the user has specified the default device name explicitly, |
---|
226 | * pretend they hadn't. |
---|
227 | */ |
---|
228 | if (!default_device && strcmp(devnam, default_devnam) == 0) |
---|
229 | default_device = 1; |
---|
230 | if (default_device) |
---|
231 | nodetach = 1; |
---|
232 | |
---|
233 | /* |
---|
234 | * Initialize system-dependent stuff and magic number package. |
---|
235 | */ |
---|
236 | sys_init(); |
---|
237 | magic_init(); |
---|
238 | |
---|
239 | /* |
---|
240 | * Detach ourselves from the terminal, if required, |
---|
241 | * and identify who is running us. |
---|
242 | */ |
---|
243 | |
---|
244 | /* |
---|
245 | syslog(LOG_NOTICE, "pppd %s.%d%s started by %s, uid %d", |
---|
246 | VERSION, PATCHLEVEL, IMPLEMENTATION, p, uid); |
---|
247 | */ |
---|
248 | |
---|
249 | |
---|
250 | for (;;) { |
---|
251 | #if 0 /* FIXME: how do we synchonize? */ |
---|
252 | |
---|
253 | /* !!! here should be done some kind of synchronization - I did it following way ... */ |
---|
254 | /* GlobalSystemStatus is set of different things shared between different tasks ... */ |
---|
255 | #if 0 |
---|
256 | /* XXX PPPConfiguration */ |
---|
257 | GlobalSystemStatus * volatile stat; |
---|
258 | stat=LockSTBSystemParam(); |
---|
259 | stat->ConnectionStatus = NotConnected; |
---|
260 | UnlockSTBSystemParam(); |
---|
261 | #endif |
---|
262 | |
---|
263 | |
---|
264 | while (1) { |
---|
265 | rtems_event_set events; |
---|
266 | int status; |
---|
267 | |
---|
268 | #if 0 |
---|
269 | /* XXX PPPConfiguration */ |
---|
270 | stat=LockSTBSystemParam(); |
---|
271 | status=stat->WantConnection; |
---|
272 | UnlockSTBSystemParam(); |
---|
273 | |
---|
274 | if (status == Connect) break; |
---|
275 | #endif |
---|
276 | rtems_event_receive(RTEMS_EVENT_1, RTEMS_WAIT | RTEMS_EVENT_ANY, RTEMS_NO_TIMEOUT, &events); |
---|
277 | } |
---|
278 | #if 0 |
---|
279 | /* XXX PPPConfiguration */ |
---|
280 | stat=LockSTBSystemParam(); |
---|
281 | /* stat->WantConnection=DontConnect; */ |
---|
282 | stat->ConnectionStatus = Connecting; |
---|
283 | |
---|
284 | /* Here you can change default nameserver ... */ |
---|
285 | rtems_bsdnet_nameserver[0].s_addr=inet_addr(stat->DNS); |
---|
286 | rtems_bsdnet_nameserver_count=1; |
---|
287 | UnlockSTBSystemParam(); |
---|
288 | #endif |
---|
289 | /* initialize DNS services here */ |
---|
290 | SetStatusInfo(0, "Connecting...",0); |
---|
291 | |
---|
292 | __res_init(); |
---|
293 | #endif /* FIXME */ |
---|
294 | /* |
---|
295 | * Open the serial device and set it up to be the ppp interface. |
---|
296 | * First we open it in non-blocking mode so we can set the |
---|
297 | * various termios flags appropriately. If we aren't dialling |
---|
298 | * out and we want to use the modem lines, we reopen it later |
---|
299 | * in order to wait for the carrier detect signal from the modem. |
---|
300 | */ |
---|
301 | while ((ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0)) < 0) { |
---|
302 | if (errno != EINTR) |
---|
303 | syslog(LOG_ERR, "Failed to open %s: %m", devnam); |
---|
304 | |
---|
305 | if (!persist || errno != EINTR) |
---|
306 | goto fail; |
---|
307 | } |
---|
308 | |
---|
309 | hungup = 0; |
---|
310 | kill_link = 0; |
---|
311 | |
---|
312 | /* |
---|
313 | * Do the equivalent of `mesg n' to stop broadcast messages. |
---|
314 | */ |
---|
315 | tty_mode = statbuf.st_mode; |
---|
316 | |
---|
317 | /* run connection script */ |
---|
318 | /* |
---|
319 | * Set line speed, flow control, etc. |
---|
320 | * On most systems we set CLOCAL for now so that we can talk |
---|
321 | * to the modem before carrier comes up. But this has the |
---|
322 | * side effect that we might miss it if CD drops before we |
---|
323 | * get to clear CLOCAL below. On systems where we can talk |
---|
324 | * successfully to the modem with CLOCAL clear and CD down, |
---|
325 | * we can clear CLOCAL at this point. |
---|
326 | */ |
---|
327 | set_up_tty(ttyfd, 1); |
---|
328 | |
---|
329 | /* drop dtr to hang up in case modem is off hook */ |
---|
330 | setdtr(ttyfd, FALSE); |
---|
331 | sleep(1); |
---|
332 | setdtr(ttyfd, TRUE); |
---|
333 | { |
---|
334 | /* Make a call ... */ |
---|
335 | #if 0 |
---|
336 | /* XXX PPPConfiguration */ |
---|
337 | char t[100]; |
---|
338 | stat=LockSTBSystemParam(); |
---|
339 | sprintf(t,"Dzwoniê pod numer %s ...",stat->Phone_Number); |
---|
340 | UnlockSTBSystemParam(); |
---|
341 | SetStatusInfo(0, t,0); |
---|
342 | #endif |
---|
343 | } |
---|
344 | |
---|
345 | |
---|
346 | if ((i=connect_script(ttyfd)) >0) { |
---|
347 | #if 0 |
---|
348 | /* here go error messages ... */ |
---|
349 | static char *error_msgs[]={ "Bad script", "IO Error" |
---|
350 | "Timeout", "Busy", "No dialtone", "No carrier", |
---|
351 | "No answer", "No answer from server" }; |
---|
352 | setdtr(ttyfd, FALSE); |
---|
353 | sprintf(t,"Communication error: %s",error_msgs[i-1]); |
---|
354 | syslog(LOG_ERR, "Connect script failed"); |
---|
355 | SetStatusInfo(0, t,1); |
---|
356 | #endif |
---|
357 | goto fail; |
---|
358 | } |
---|
359 | |
---|
360 | else |
---|
361 | if (i<0) |
---|
362 | { |
---|
363 | #if 0 |
---|
364 | char t[100]; |
---|
365 | sprintf(t,"£¹czenie z Internetem z prêdkosci¹ %d baud...",-i); |
---|
366 | SetStatusInfo(0, t,0); |
---|
367 | #endif |
---|
368 | } |
---|
369 | else |
---|
370 | { |
---|
371 | #if 0 |
---|
372 | SetStatusInfo(0, "£¹czenie z Internetem...",0); |
---|
373 | #endif |
---|
374 | } |
---|
375 | syslog(LOG_INFO, "Serial connection established."); |
---|
376 | |
---|
377 | sleep(1); /* give it time to set up its terminal */ |
---|
378 | |
---|
379 | /* set line speed, flow control, etc.; clear CLOCAL if modem option */ |
---|
380 | set_up_tty(ttyfd, 0); |
---|
381 | |
---|
382 | /* reopen tty if necessary to wait for carrier */ |
---|
383 | |
---|
384 | /* run welcome script, if any */ |
---|
385 | /* if (welcomer && welcomer[0]) { |
---|
386 | if (device_script(welcomer, 21, ttyfd) < 0) |
---|
387 | syslog(LOG_WARNING, "Welcome script failed"); |
---|
388 | } |
---|
389 | */ |
---|
390 | /* set up the serial device as a ppp interface */ |
---|
391 | establish_ppp(ttyfd); |
---|
392 | |
---|
393 | /* syslog(LOG_INFO, "Using interface ppp%d", interfunit); */ |
---|
394 | (void) sprintf(ifname, "ppp%d", interfunit); |
---|
395 | |
---|
396 | /* |
---|
397 | * Start opening the connection and wait for |
---|
398 | * incoming events (reply, timeout, etc.). |
---|
399 | */ |
---|
400 | /* syslog(LOG_NOTICE, "Connect: %s <--> %s", ifname, devnam); */ |
---|
401 | |
---|
402 | rtems_bsdnet_semaphore_obtain(); |
---|
403 | |
---|
404 | lcp_lowerup(0); |
---|
405 | lcp_open(0); /* Start protocol */ |
---|
406 | |
---|
407 | rtems_bsdnet_semaphore_release(); |
---|
408 | #if 0 |
---|
409 | SetStatusInfo(0, "Po³¹czenie z Internetem nawi¹zane.",1); |
---|
410 | #endif |
---|
411 | for (phase = PHASE_ESTABLISH; phase != PHASE_DEAD;) { |
---|
412 | #if 0 |
---|
413 | wait_input(timeleft(&timo)); |
---|
414 | #endif |
---|
415 | calltimeout(); |
---|
416 | get_input(); |
---|
417 | #if 0 |
---|
418 | /* XXX PPPConfiguration */ |
---|
419 | stat=LockSTBSystemParam(); |
---|
420 | if (stat->WantConnection==DontConnect) { |
---|
421 | stat->ConnectionStatus = NotConnected; |
---|
422 | SetStatusInfo(0, "Roz³¹czam siê ...",0); |
---|
423 | lcp_close(0, ""); |
---|
424 | kill_link = 0; |
---|
425 | } |
---|
426 | UnlockSTBSystemParam(); |
---|
427 | #endif |
---|
428 | if (open_ccp_flag) { |
---|
429 | if (phase == PHASE_NETWORK) { |
---|
430 | ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ |
---|
431 | (*ccp_protent.open) (0); |
---|
432 | } |
---|
433 | open_ccp_flag = 0; |
---|
434 | } |
---|
435 | } |
---|
436 | /* |
---|
437 | * If we may want to bring the link up again, transfer |
---|
438 | * the ppp unit back to the loopback. Set the |
---|
439 | * real serial device back to its normal mode of operation. |
---|
440 | */ |
---|
441 | clean_check(); |
---|
442 | disestablish_ppp(ttyfd); |
---|
443 | #if 0 |
---|
444 | SetStatusInfo(0, "Po³¹czenie z Internetem zerwane.",1); |
---|
445 | #endif |
---|
446 | /* |
---|
447 | * Run disconnector script, if requested. |
---|
448 | * XXX we may not be able to do this if the line has hung up! |
---|
449 | */ |
---|
450 | /* if (disconnector && !hungup) { |
---|
451 | set_up_tty(ttyfd, 1); |
---|
452 | if (device_script(disconnector, ttyfd, ttyfd) < 0) { |
---|
453 | syslog(LOG_WARNING, "disconnect script failed"); |
---|
454 | } else { |
---|
455 | syslog(LOG_INFO, "Serial link disconnected."); |
---|
456 | } |
---|
457 | } |
---|
458 | */ |
---|
459 | fail: |
---|
460 | #if 0 |
---|
461 | /* XXX PPPConfiguration */ |
---|
462 | stat=LockSTBSystemParam(); |
---|
463 | stat->ConnectionStatus = NotConnected; |
---|
464 | stat->WantConnection=DontConnect; |
---|
465 | UnlockSTBSystemParam(); |
---|
466 | #endif |
---|
467 | if (ttyfd >= 0) |
---|
468 | close_tty(); |
---|
469 | |
---|
470 | |
---|
471 | |
---|
472 | } |
---|
473 | |
---|
474 | return 0; |
---|
475 | } |
---|
476 | |
---|
477 | /* |
---|
478 | * detach - detach us from the controlling terminal. |
---|
479 | */ |
---|
480 | void detach() |
---|
481 | { |
---|
482 | } |
---|
483 | |
---|
484 | /* |
---|
485 | * holdoff_end - called via a timeout when the holdoff period ends. |
---|
486 | */ |
---|
487 | |
---|
488 | static void holdoff_end(arg) |
---|
489 | void *arg; |
---|
490 | { |
---|
491 | phase = PHASE_DORMANT; |
---|
492 | } |
---|
493 | |
---|
494 | /* |
---|
495 | * get_input - called when incoming data is available. |
---|
496 | */ |
---|
497 | static void get_input() |
---|
498 | { |
---|
499 | int len, i; |
---|
500 | u_char *p; |
---|
501 | u_short protocol; |
---|
502 | struct protent *protp; |
---|
503 | |
---|
504 | p = inpacket_buf; /* point to beginning of packet buffer */ |
---|
505 | |
---|
506 | len = read_packet(inpacket_buf); |
---|
507 | if (len < 0) |
---|
508 | return; |
---|
509 | |
---|
510 | if (len == 0) { |
---|
511 | #if 0 |
---|
512 | /* XXX PPPConfiguration */ |
---|
513 | GlobalSystemStatus * volatile stat; |
---|
514 | #endif |
---|
515 | /* syslog(LOG_NOTICE, "Modem hangup"); */ |
---|
516 | hungup = 1; |
---|
517 | #if 0 |
---|
518 | /* XXX PPPConfiguration */ |
---|
519 | stat=LockSTBSystemParam(); |
---|
520 | stat->ConnectionStatus = NotConnected; |
---|
521 | UnlockSTBSystemParam(); |
---|
522 | #endif |
---|
523 | |
---|
524 | lcp_lowerdown(0); /* serial link is no longer available */ |
---|
525 | link_terminated(0); |
---|
526 | return; |
---|
527 | } |
---|
528 | /* if ((debug)) |
---|
529 | log_packet(p, len, "rcvd ", LOG_DEBUG); |
---|
530 | */ |
---|
531 | |
---|
532 | if (len < PPP_HDRLEN) { |
---|
533 | /* |
---|
534 | if (debug) |
---|
535 | MAINDEBUG((LOG_INFO, "io(): Received short packet.")); |
---|
536 | */ |
---|
537 | return; |
---|
538 | } |
---|
539 | /* We need to modify internal network structures here */ |
---|
540 | rtems_bsdnet_semaphore_obtain(); |
---|
541 | |
---|
542 | p += 2; /* Skip address and control */ |
---|
543 | GETSHORT(protocol, p); |
---|
544 | len -= PPP_HDRLEN; |
---|
545 | |
---|
546 | /* |
---|
547 | * Toss all non-LCP packets unless LCP is OPEN. |
---|
548 | */ |
---|
549 | if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { |
---|
550 | MAINDEBUG((LOG_INFO, |
---|
551 | "get_input: Received non-LCP packet when LCP not open.")); |
---|
552 | rtems_bsdnet_semaphore_release(); |
---|
553 | return; |
---|
554 | } |
---|
555 | /* |
---|
556 | * Until we get past the authentication phase, toss all packets |
---|
557 | * except LCP, LQR and authentication packets. |
---|
558 | */ |
---|
559 | if (phase <= PHASE_AUTHENTICATE |
---|
560 | && !(protocol == PPP_LCP || protocol == PPP_LQR |
---|
561 | || protocol == PPP_PAP || protocol == PPP_CHAP)) { |
---|
562 | |
---|
563 | rtems_bsdnet_semaphore_release(); |
---|
564 | return; |
---|
565 | } |
---|
566 | /* |
---|
567 | * Upcall the proper protocol input routine. |
---|
568 | */ |
---|
569 | for (i = 0; (protp = protocols[i]) != NULL; ++i) { |
---|
570 | if (protp->protocol == protocol && protp->enabled_flag) { |
---|
571 | (*protp->input) (0, p, len); |
---|
572 | rtems_bsdnet_semaphore_release(); |
---|
573 | return; |
---|
574 | } |
---|
575 | if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag |
---|
576 | && protp->datainput != NULL) { |
---|
577 | (*protp->datainput) (0, p, len); |
---|
578 | rtems_bsdnet_semaphore_release(); |
---|
579 | return; |
---|
580 | } |
---|
581 | } |
---|
582 | |
---|
583 | lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); |
---|
584 | rtems_bsdnet_semaphore_release(); |
---|
585 | } |
---|
586 | |
---|
587 | |
---|
588 | /* |
---|
589 | * quit - Clean up state and exit (with an error indication). |
---|
590 | */ |
---|
591 | void quit() |
---|
592 | { |
---|
593 | die(1); |
---|
594 | } |
---|
595 | |
---|
596 | /* |
---|
597 | * die - like quit, except we can specify an exit status. |
---|
598 | */ |
---|
599 | void die(status) |
---|
600 | int status; |
---|
601 | { |
---|
602 | cleanup(); |
---|
603 | } |
---|
604 | |
---|
605 | /* |
---|
606 | * cleanup - restore anything which needs to be restored before we exit |
---|
607 | */ |
---|
608 | /* ARGSUSED */ |
---|
609 | static void cleanup() |
---|
610 | { |
---|
611 | sys_cleanup(); |
---|
612 | |
---|
613 | if (ttyfd >= 0) |
---|
614 | close_tty(); |
---|
615 | |
---|
616 | } |
---|
617 | |
---|
618 | /* |
---|
619 | * close_tty - restore the terminal device and close it. |
---|
620 | */ |
---|
621 | static void close_tty() |
---|
622 | { |
---|
623 | disestablish_ppp(ttyfd); |
---|
624 | |
---|
625 | /* drop dtr to hang up */ |
---|
626 | setdtr(ttyfd, FALSE); |
---|
627 | /* |
---|
628 | * This sleep is in case the serial port has CLOCAL set by default, |
---|
629 | * and consequently will reassert DTR when we close the device. |
---|
630 | */ |
---|
631 | sleep(1); |
---|
632 | |
---|
633 | restore_tty(ttyfd); |
---|
634 | close(ttyfd); |
---|
635 | ttyfd = -1; |
---|
636 | } |
---|
637 | |
---|
638 | |
---|
639 | struct callout { |
---|
640 | struct timeval c_time; |
---|
641 | void *c_arg; |
---|
642 | void (*c_func) __P((void *)); |
---|
643 | struct callout *c_next; |
---|
644 | }; |
---|
645 | |
---|
646 | static struct callout *callout = NULL; |
---|
647 | static struct timeval timenow; |
---|
648 | |
---|
649 | /* |
---|
650 | * timeout - Schedule a timeout. |
---|
651 | * |
---|
652 | * Note that this timeout takes the number of seconds, NOT hz (as in |
---|
653 | * the kernel). |
---|
654 | */ |
---|
655 | void my_timeout(func, arg, time) |
---|
656 | void (*func) __P((void *)); |
---|
657 | void *arg; |
---|
658 | int time; |
---|
659 | { |
---|
660 | struct callout *newp, *p, **pp; |
---|
661 | |
---|
662 | MAINDEBUG((LOG_DEBUG, "Timeout %lx:%lx in %d seconds.", |
---|
663 | (long) func, (long) arg, time)); |
---|
664 | /* |
---|
665 | * Allocate timeout. |
---|
666 | */ |
---|
667 | if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) { |
---|
668 | /* syslog(LOG_ERR, "Out of memory in timeout()!"); */ |
---|
669 | die(1); |
---|
670 | } |
---|
671 | newp->c_arg = arg; |
---|
672 | newp->c_func = func; |
---|
673 | gettimeofday(&timenow, NULL); |
---|
674 | newp->c_time.tv_sec = timenow.tv_sec + time; |
---|
675 | newp->c_time.tv_usec = timenow.tv_usec; |
---|
676 | /* |
---|
677 | * Find correct place and link it in. |
---|
678 | */ |
---|
679 | for (pp = &callout; (p = *pp); pp = &p->c_next) |
---|
680 | if (newp->c_time.tv_sec < p->c_time.tv_sec |
---|
681 | || (newp->c_time.tv_sec == p->c_time.tv_sec |
---|
682 | && newp->c_time.tv_usec < p->c_time.tv_sec)) |
---|
683 | break; |
---|
684 | newp->c_next = p; |
---|
685 | *pp = newp; |
---|
686 | } |
---|
687 | |
---|
688 | /* |
---|
689 | * untimeout - Unschedule a timeout. |
---|
690 | */ |
---|
691 | |
---|
692 | void untimeout(func, arg) |
---|
693 | void (*func) __P((void *)); |
---|
694 | void *arg; |
---|
695 | { |
---|
696 | struct callout **copp, *freep; |
---|
697 | |
---|
698 | MAINDEBUG((LOG_DEBUG, "Untimeout %lx:%lx.", (long) func, (long) arg)); |
---|
699 | |
---|
700 | for (copp = &callout; (freep = *copp); copp = &freep->c_next) |
---|
701 | if (freep->c_func == func && freep->c_arg == arg) { |
---|
702 | *copp = freep->c_next; |
---|
703 | (void) free((char *) freep); |
---|
704 | break; |
---|
705 | } |
---|
706 | } |
---|
707 | |
---|
708 | |
---|
709 | /* |
---|
710 | * calltimeout - Call any timeout routines which are now due. |
---|
711 | */ |
---|
712 | static void calltimeout() |
---|
713 | { |
---|
714 | struct callout *p; |
---|
715 | |
---|
716 | while (callout != NULL) { |
---|
717 | p = callout; |
---|
718 | |
---|
719 | if (gettimeofday(&timenow, NULL) < 0) { |
---|
720 | die(1); |
---|
721 | } |
---|
722 | if (!(p->c_time.tv_sec < timenow.tv_sec |
---|
723 | || (p->c_time.tv_sec == timenow.tv_sec |
---|
724 | && p->c_time.tv_usec <= timenow.tv_usec))) |
---|
725 | break; /* no, it's not time yet */ |
---|
726 | |
---|
727 | callout = p->c_next; |
---|
728 | (*p->c_func) (p->c_arg); |
---|
729 | |
---|
730 | free((char *) p); |
---|
731 | } |
---|
732 | } |
---|
733 | |
---|
734 | |
---|
735 | /* |
---|
736 | * timeleft - return the length of time until the next timeout is due. |
---|
737 | */ |
---|
738 | static struct timeval * |
---|
739 | timeleft(tvp) |
---|
740 | struct timeval *tvp; |
---|
741 | { |
---|
742 | if (callout == NULL) |
---|
743 | return NULL; |
---|
744 | |
---|
745 | gettimeofday(&timenow, NULL); |
---|
746 | tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; |
---|
747 | tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; |
---|
748 | if (tvp->tv_usec < 0) { |
---|
749 | tvp->tv_usec += 1000000; |
---|
750 | tvp->tv_sec -= 1; |
---|
751 | } |
---|
752 | if (tvp->tv_sec < 0) |
---|
753 | tvp->tv_sec = tvp->tv_usec = 0; |
---|
754 | |
---|
755 | return tvp; |
---|
756 | } |
---|
757 | |
---|
758 | |
---|
759 | /* |
---|
760 | * term - Catch SIGTERM signal and SIGINT signal (^C/del). |
---|
761 | * |
---|
762 | * Indicates that we should initiate a graceful disconnect and exit. |
---|
763 | */ |
---|
764 | static void term(sig) |
---|
765 | int sig; |
---|
766 | { |
---|
767 | /* persist = 0; *//* don't try to restart */ |
---|
768 | kill_link = 1; |
---|
769 | } |
---|
770 | |
---|
771 | int modem_fd; /* FIXME: should not be global... */ |
---|
772 | int connect_script(int fd) |
---|
773 | { |
---|
774 | #if 0 /* FIXME: This is WinNT special */ |
---|
775 | char program[256] = "TIMEOUT@10@@CLIENT@CLIENTSERVER"; |
---|
776 | #else |
---|
777 | char program[256] = { 0 }; |
---|
778 | #endif |
---|
779 | #if 0 |
---|
780 | /* XXX PPPConfiguration */ |
---|
781 | GlobalSystemStatus * volatile stat; |
---|
782 | #endif |
---|
783 | #if 0 |
---|
784 | /* Connect scripts are almost the same as in Linux Chat ... */ |
---|
785 | static char *scripts[] = |
---|
786 | { |
---|
787 | "TIMEOUT@5@@\rAT@OK-+++\\c-OK@ATH0@TIMEOUT@90@OK@ATDT%s@CONNECT@", |
---|
788 | "TIMEOUT@5@@\rAT@OK-+++\\c-OK@ATH0@TIMEOUT@90@OK@ATDT%s@CONNECT@@ppp@@Username:@%s@Password:@%s@" |
---|
789 | }; |
---|
790 | #endif |
---|
791 | modem_fd = fd; |
---|
792 | #if 0 |
---|
793 | /* XXX PPPConfiguration */ |
---|
794 | stat=LockSTBSystemParam(); |
---|
795 | if (strcmp("",stat->PPP_User)) |
---|
796 | { |
---|
797 | stat->provider=Poland_TPSA; |
---|
798 | } |
---|
799 | else |
---|
800 | stat->provider=DumbLogin; |
---|
801 | switch (stat->provider) { |
---|
802 | case Poland_TPSA: |
---|
803 | sprintf(program, scripts[1], stat->Phone_Number, stat->PPP_User, stat->PPP_Password); |
---|
804 | break; |
---|
805 | default: |
---|
806 | sprintf(program, scripts[0], stat->Phone_Number); |
---|
807 | |
---|
808 | } |
---|
809 | UnlockSTBSystemParam(); |
---|
810 | #endif |
---|
811 | conn_running = 0; |
---|
812 | |
---|
813 | return chatmain(program); |
---|
814 | } |
---|
815 | |
---|
816 | |
---|
817 | /* |
---|
818 | * run-program - execute a program with given arguments, |
---|
819 | * but don't wait for it. |
---|
820 | * If the program can't be executed, logs an error unless |
---|
821 | * must_exist is 0 and the program file doesn't exist. |
---|
822 | */ |
---|
823 | int run_program(prog, args, must_exist) |
---|
824 | char *prog; |
---|
825 | char **args; |
---|
826 | int must_exist; |
---|
827 | { |
---|
828 | |
---|
829 | return 0; |
---|
830 | } |
---|
831 | |
---|
832 | |
---|
833 | /* |
---|
834 | * reap_kids - get status from any dead child processes, |
---|
835 | * and log a message for abnormal terminations. |
---|
836 | */ |
---|
837 | static void reap_kids() |
---|
838 | { |
---|
839 | } |
---|
840 | |
---|
841 | |
---|
842 | /* |
---|
843 | * log_packet - format a packet and log it. |
---|
844 | */ |
---|
845 | |
---|
846 | char line[256]; |
---|
847 | char *linep; |
---|
848 | |
---|
849 | /*#define log_packet(p, len, prefix, level) */ |
---|
850 | void log_packet(p, len, prefix, level) |
---|
851 | u_char *p; |
---|
852 | int len; |
---|
853 | char *prefix; |
---|
854 | int level; |
---|
855 | { |
---|
856 | strcpy(line, prefix); |
---|
857 | linep = line + strlen(line); |
---|
858 | /* format_packet(p, len, pr_log, NULL); */ |
---|
859 | /* if (linep != line) |
---|
860 | syslog(level, "%s", line); |
---|
861 | */ |
---|
862 | } |
---|
863 | |
---|
864 | /* |
---|
865 | * format_packet - make a readable representation of a packet, |
---|
866 | * calling `printer(arg, format, ...)' to output it. |
---|
867 | */ |
---|
868 | /*#define format_packet(p, len, printer, arg) */ |
---|
869 | |
---|
870 | void format_packet(p, len, printer, arg) |
---|
871 | u_char *p; |
---|
872 | int len; |
---|
873 | void (*printer) __P((void *, char *,...)); |
---|
874 | void *arg; |
---|
875 | { |
---|
876 | /* |
---|
877 | int i, n; |
---|
878 | u_short proto; |
---|
879 | struct protent *protp; |
---|
880 | |
---|
881 | if (len >= PPP_HDRLEN && p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) { |
---|
882 | p += 2; |
---|
883 | GETSHORT(proto, p); |
---|
884 | len -= PPP_HDRLEN; |
---|
885 | for (i = 0; (protp = protocols[i]) != NULL; ++i) |
---|
886 | if (proto == protp->protocol) |
---|
887 | break; |
---|
888 | if (protp != NULL) { |
---|
889 | printer(arg, "[%s", protp->name); |
---|
890 | n = (*protp->printpkt) (p, len, printer, arg); |
---|
891 | printer(arg, "]"); |
---|
892 | p += n; |
---|
893 | len -= n; |
---|
894 | } else { |
---|
895 | printer(arg, "[proto=0x%x]", proto); |
---|
896 | } |
---|
897 | } |
---|
898 | */ |
---|
899 | /* for (; len > 0; --len) { |
---|
900 | GETCHAR(x, p); |
---|
901 | printer(arg, " %.2x", x); |
---|
902 | } |
---|
903 | */ |
---|
904 | } |
---|
905 | |
---|
906 | |
---|
907 | |
---|
908 | /* XXX currently unused */ |
---|
909 | #if 0 |
---|
910 | static void |
---|
911 | pr_log __V((void *arg, char *fmt,...)) |
---|
912 | { |
---|
913 | int n; |
---|
914 | va_list pvar; |
---|
915 | char buf[256]; |
---|
916 | |
---|
917 | #if __STDC__ |
---|
918 | va_start(pvar, fmt); |
---|
919 | #else |
---|
920 | void *arg; |
---|
921 | char *fmt; |
---|
922 | va_start(pvar); |
---|
923 | arg = va_arg(pvar, void *); |
---|
924 | fmt = va_arg(pvar, char *); |
---|
925 | #endif |
---|
926 | |
---|
927 | n = vfmtmsg(buf, sizeof(buf), fmt, pvar); |
---|
928 | va_end(pvar); |
---|
929 | |
---|
930 | if (linep + n + 1 > line + sizeof(line)) { |
---|
931 | /* syslog(LOG_DEBUG, "%s", line); */ |
---|
932 | linep = line; |
---|
933 | } |
---|
934 | strcpy(linep, buf); |
---|
935 | linep += n; |
---|
936 | } |
---|
937 | #endif |
---|
938 | |
---|
939 | /* |
---|
940 | * print_string - print a readable representation of a string using |
---|
941 | * printer. |
---|
942 | */ |
---|
943 | /*#define print_string(p, len, printer, arg) */ |
---|
944 | |
---|
945 | void print_string(p, len, printer, arg) |
---|
946 | char *p; |
---|
947 | int len; |
---|
948 | void (*printer) __P((void *, char *,...)); |
---|
949 | void *arg; |
---|
950 | { |
---|
951 | int c; |
---|
952 | |
---|
953 | printer(arg, "\""); |
---|
954 | for (; len > 0; --len) { |
---|
955 | c = *p++; |
---|
956 | if (' ' <= c && c <= '~') { |
---|
957 | if (c == '\\' || c == '"') |
---|
958 | printer(arg, "\\"); |
---|
959 | printer(arg, "%c", c); |
---|
960 | } else { |
---|
961 | switch (c) { |
---|
962 | case '\n': |
---|
963 | printer(arg, "\\n"); |
---|
964 | break; |
---|
965 | case '\r': |
---|
966 | printer(arg, "\\r"); |
---|
967 | break; |
---|
968 | case '\t': |
---|
969 | printer(arg, "\\t"); |
---|
970 | break; |
---|
971 | default: |
---|
972 | printer(arg, "\\%.3o", c); |
---|
973 | } |
---|
974 | } |
---|
975 | } |
---|
976 | printer(arg, "\""); |
---|
977 | } |
---|
978 | |
---|
979 | |
---|
980 | /* |
---|
981 | * novm - log an error message saying we ran out of memory, and die. |
---|
982 | */ |
---|
983 | void novm(msg) |
---|
984 | char *msg; |
---|
985 | { |
---|
986 | /* syslog(LOG_ERR, "Virtual memory exhausted allocating %s\n", msg); |
---|
987 | */ die(1); |
---|
988 | } |
---|
989 | |
---|
990 | /* |
---|
991 | * fmtmsg - format a message into a buffer. Like sprintf except we |
---|
992 | * also specify the length of the output buffer, and we handle |
---|
993 | * %r (recursive format), %m (error message) and %I (IP address) formats. |
---|
994 | * Doesn't do floating-point formats. |
---|
995 | * Returns the number of chars put into buf. |
---|
996 | */ |
---|
997 | int |
---|
998 | fmtmsg __V((char *buf, int buflen, char *fmt,...)) |
---|
999 | { |
---|
1000 | va_list args; |
---|
1001 | int n; |
---|
1002 | |
---|
1003 | #if __STDC__ |
---|
1004 | va_start(args, fmt); |
---|
1005 | #else |
---|
1006 | char *buf; |
---|
1007 | int buflen; |
---|
1008 | char *fmt; |
---|
1009 | va_start(args); |
---|
1010 | buf = va_arg(args, char *); |
---|
1011 | buflen = va_arg(args, int); |
---|
1012 | fmt = va_arg(args, char *); |
---|
1013 | #endif |
---|
1014 | n = vfmtmsg(buf, buflen, fmt, args); |
---|
1015 | va_end(args); |
---|
1016 | return n; |
---|
1017 | } |
---|
1018 | |
---|
1019 | /* |
---|
1020 | * vfmtmsg - like fmtmsg, takes a va_list instead of a list of args. |
---|
1021 | */ |
---|
1022 | #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0) |
---|
1023 | /*#define vfmtmsg(buf, buflen, fmt, args) */ |
---|
1024 | |
---|
1025 | int vfmtmsg(buf, buflen, fmt, args) |
---|
1026 | char *buf; |
---|
1027 | int buflen; |
---|
1028 | char *fmt; |
---|
1029 | va_list args; |
---|
1030 | { |
---|
1031 | /* int c, i, n; |
---|
1032 | int width, prec, fillch; |
---|
1033 | int base, len, neg, quoted; |
---|
1034 | unsigned long val = 0; |
---|
1035 | char *str, *f, *buf0; |
---|
1036 | unsigned char *p; |
---|
1037 | char num[32]; |
---|
1038 | time_t t; |
---|
1039 | static char hexchars[] = "0123456789abcdef"; |
---|
1040 | |
---|
1041 | buf0 = buf; |
---|
1042 | --buflen; |
---|
1043 | while (buflen > 0) { |
---|
1044 | for (f = fmt; *f != '%' && *f != 0; ++f) |
---|
1045 | ; |
---|
1046 | if (f > fmt) { |
---|
1047 | len = f - fmt; |
---|
1048 | if (len > buflen) |
---|
1049 | len = buflen; |
---|
1050 | memcpy(buf, fmt, len); |
---|
1051 | buf += len; |
---|
1052 | buflen -= len; |
---|
1053 | fmt = f; |
---|
1054 | } |
---|
1055 | if (*fmt == 0) |
---|
1056 | break; |
---|
1057 | c = *++fmt; |
---|
1058 | width = prec = 0; |
---|
1059 | fillch = ' '; |
---|
1060 | if (c == '0') { |
---|
1061 | fillch = '0'; |
---|
1062 | c = *++fmt; |
---|
1063 | } |
---|
1064 | if (c == '*') { |
---|
1065 | width = va_arg(args, int); |
---|
1066 | c = *++fmt; |
---|
1067 | } else { |
---|
1068 | while (isdigit(c)) { |
---|
1069 | width = width * 10 + c - '0'; |
---|
1070 | c = *++fmt; |
---|
1071 | } |
---|
1072 | } |
---|
1073 | if (c == '.') { |
---|
1074 | c = *++fmt; |
---|
1075 | if (c == '*') { |
---|
1076 | prec = va_arg(args, int); |
---|
1077 | c = *++fmt; |
---|
1078 | } else { |
---|
1079 | while (isdigit(c)) { |
---|
1080 | prec = prec * 10 + c - '0'; |
---|
1081 | c = *++fmt; |
---|
1082 | } |
---|
1083 | } |
---|
1084 | } |
---|
1085 | str = 0; |
---|
1086 | base = 0; |
---|
1087 | neg = 0; |
---|
1088 | ++fmt; |
---|
1089 | switch (c) { |
---|
1090 | case 'd': |
---|
1091 | i = va_arg(args, int); |
---|
1092 | if (i < 0) { |
---|
1093 | neg = 1; |
---|
1094 | val = -i; |
---|
1095 | } else |
---|
1096 | val = i; |
---|
1097 | base = 10; |
---|
1098 | break; |
---|
1099 | case 'o': |
---|
1100 | val = va_arg(args, unsigned int); |
---|
1101 | base = 8; |
---|
1102 | break; |
---|
1103 | case 'x': |
---|
1104 | val = va_arg(args, unsigned int); |
---|
1105 | base = 16; |
---|
1106 | break; |
---|
1107 | case 'p': |
---|
1108 | val = (unsigned long) va_arg(args, void *); |
---|
1109 | base = 16; |
---|
1110 | neg = 2; |
---|
1111 | break; |
---|
1112 | case 's': |
---|
1113 | str = va_arg(args, char *); |
---|
1114 | break; |
---|
1115 | case 'c': |
---|
1116 | num[0] = va_arg(args, int); |
---|
1117 | num[1] = 0; |
---|
1118 | str = num; |
---|
1119 | break; |
---|
1120 | case 'm': |
---|
1121 | str = strerror(errno); |
---|
1122 | break; |
---|
1123 | case 'I': |
---|
1124 | str = ip_ntoa(va_arg(args, u_int32_t)); |
---|
1125 | break; |
---|
1126 | case 'r': |
---|
1127 | f = va_arg(args, char *); |
---|
1128 | #ifndef __powerpc__ |
---|
1129 | n = vfmtmsg(buf, buflen + 1, f, va_arg(args, va_list)); |
---|
1130 | #else |
---|
1131 | |
---|
1132 | n = vfmtmsg(buf, buflen + 1, f, va_arg(args, void *)); |
---|
1133 | #endif |
---|
1134 | buf += n; |
---|
1135 | buflen -= n; |
---|
1136 | continue; |
---|
1137 | case 't': |
---|
1138 | break; |
---|
1139 | case 'v': |
---|
1140 | case 'q': |
---|
1141 | quoted = c == 'q'; |
---|
1142 | p = va_arg(args, unsigned char *); |
---|
1143 | if (fillch == '0' && prec > 0) { |
---|
1144 | n = prec; |
---|
1145 | } else { |
---|
1146 | n = strlen((char *)p); |
---|
1147 | if (prec > 0 && prec < n) |
---|
1148 | n = prec; |
---|
1149 | } |
---|
1150 | while (n > 0 && buflen > 0) { |
---|
1151 | c = *p++; |
---|
1152 | --n; |
---|
1153 | if (!quoted && c >= 0x80) { |
---|
1154 | OUTCHAR('M'); |
---|
1155 | OUTCHAR('-'); |
---|
1156 | c -= 0x80; |
---|
1157 | } |
---|
1158 | if (quoted && (c == '"' || c == '\\')) |
---|
1159 | OUTCHAR('\\'); |
---|
1160 | if (c < 0x20 || (0x7f <= c && c < 0xa0)) { |
---|
1161 | if (quoted) { |
---|
1162 | OUTCHAR('\\'); |
---|
1163 | switch (c) { |
---|
1164 | case '\t': OUTCHAR('t'); break; |
---|
1165 | case '\n': OUTCHAR('n'); break; |
---|
1166 | case '\b': OUTCHAR('b'); break; |
---|
1167 | case '\f': OUTCHAR('f'); break; |
---|
1168 | default: |
---|
1169 | OUTCHAR('x'); |
---|
1170 | OUTCHAR(hexchars[c >> 4]); |
---|
1171 | OUTCHAR(hexchars[c & 0xf]); |
---|
1172 | } |
---|
1173 | } else { |
---|
1174 | if (c == '\t') |
---|
1175 | OUTCHAR(c); |
---|
1176 | else { |
---|
1177 | OUTCHAR('^'); |
---|
1178 | OUTCHAR(c ^ 0x40); |
---|
1179 | } |
---|
1180 | } |
---|
1181 | } else |
---|
1182 | OUTCHAR(c); |
---|
1183 | } |
---|
1184 | continue; |
---|
1185 | default: |
---|
1186 | *buf++ = '%'; |
---|
1187 | if (c != '%') |
---|
1188 | --fmt; |
---|
1189 | --buflen; |
---|
1190 | continue; |
---|
1191 | } |
---|
1192 | if (base != 0) { |
---|
1193 | str = num + sizeof(num); |
---|
1194 | *--str = 0; |
---|
1195 | while (str > num + neg) { |
---|
1196 | *--str = hexchars[val % base]; |
---|
1197 | val = val / base; |
---|
1198 | if (--prec <= 0 && val == 0) |
---|
1199 | break; |
---|
1200 | } |
---|
1201 | switch (neg) { |
---|
1202 | case 1: |
---|
1203 | *--str = '-'; |
---|
1204 | break; |
---|
1205 | case 2: |
---|
1206 | *--str = 'x'; |
---|
1207 | *--str = '0'; |
---|
1208 | break; |
---|
1209 | } |
---|
1210 | len = num + sizeof(num) - 1 - str; |
---|
1211 | } else { |
---|
1212 | len = strlen(str); |
---|
1213 | if (prec > 0 && len > prec) |
---|
1214 | len = prec; |
---|
1215 | } |
---|
1216 | if (width > 0) { |
---|
1217 | if (width > buflen) |
---|
1218 | width = buflen; |
---|
1219 | if ((n = width - len) > 0) { |
---|
1220 | buflen -= n; |
---|
1221 | for (; n > 0; --n) |
---|
1222 | *buf++ = fillch; |
---|
1223 | } |
---|
1224 | } |
---|
1225 | if (len > buflen) |
---|
1226 | len = buflen; |
---|
1227 | memcpy(buf, str, len); |
---|
1228 | buf += len; |
---|
1229 | buflen -= len; |
---|
1230 | } |
---|
1231 | *buf = 0; |
---|
1232 | return buf - buf0; |
---|
1233 | */ |
---|
1234 | return 0; |
---|
1235 | } |
---|
1236 | |
---|
1237 | |
---|
1238 | /* |
---|
1239 | * script_setenv - set an environment variable value to be used |
---|
1240 | * for scripts that we run (e.g. ip-up, auth-up, etc.) |
---|
1241 | */ |
---|
1242 | #define script_setenv(var, value) |
---|
1243 | /* |
---|
1244 | void |
---|
1245 | script_setenv(var, value) |
---|
1246 | char *var, *value; |
---|
1247 | { |
---|
1248 | int vl = strlen(var); |
---|
1249 | int i; |
---|
1250 | char *p, *newstring; |
---|
1251 | |
---|
1252 | newstring = (char *) malloc(vl + strlen(value) + 2); |
---|
1253 | if (newstring == 0) |
---|
1254 | return; |
---|
1255 | strcpy(newstring, var); |
---|
1256 | newstring[vl] = '='; |
---|
1257 | strcpy(newstring+vl+1, value); |
---|
1258 | |
---|
1259 | if (script_env != 0) { |
---|
1260 | for (i = 0; (p = script_env[i]) != 0; ++i) { |
---|
1261 | if (strncmp(p, var, vl) == 0 && p[vl] == '=') { |
---|
1262 | free(p); |
---|
1263 | script_env[i] = newstring; |
---|
1264 | return; |
---|
1265 | } |
---|
1266 | } |
---|
1267 | } else { |
---|
1268 | i = 0; |
---|
1269 | script_env = (char **) malloc(16 * sizeof(char *)); |
---|
1270 | if (script_env == 0) |
---|
1271 | return; |
---|
1272 | s_env_nalloc = 16; |
---|
1273 | } |
---|
1274 | |
---|
1275 | if (i + 1 >= s_env_nalloc) { |
---|
1276 | int new_n = i + 17; |
---|
1277 | char **newenv = (char **) realloc((void *)script_env, |
---|
1278 | new_n * sizeof(char *)); |
---|
1279 | if (newenv == 0) |
---|
1280 | return; |
---|
1281 | script_env = newenv; |
---|
1282 | s_env_nalloc = new_n; |
---|
1283 | } |
---|
1284 | |
---|
1285 | script_env[i] = newstring; |
---|
1286 | script_env[i+1] = 0; |
---|
1287 | } |
---|
1288 | |
---|
1289 | *//* |
---|
1290 | * script_unsetenv - remove a variable from the environment |
---|
1291 | * for scripts. |
---|
1292 | */ |
---|
1293 | #define script_unsetenv(var) |
---|
1294 | /* |
---|
1295 | void |
---|
1296 | script_unsetenv(var) |
---|
1297 | char *var; |
---|
1298 | { |
---|
1299 | int vl = strlen(var); |
---|
1300 | int i; |
---|
1301 | char *p; |
---|
1302 | |
---|
1303 | if (script_env == 0) |
---|
1304 | return; |
---|
1305 | for (i = 0; (p = script_env[i]) != 0; ++i) { |
---|
1306 | if (strncmp(p, var, vl) == 0 && p[vl] == '=') { |
---|
1307 | free(p); |
---|
1308 | while ((script_env[i] = script_env[i+1]) != 0) |
---|
1309 | ++i; |
---|
1310 | break; |
---|
1311 | } |
---|
1312 | } |
---|
1313 | } |
---|
1314 | */ |
---|