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