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