1 1.1 christos /* NetBSD: status.c,v 1.3 2008/05/04 13:30:54 martin Exp */ 2 1.1 christos 3 1.1 christos /* 4 1.1 christos * status.c - pppd plugin to implement an `lcpstatus' option. 5 1.1 christos * This is intended as more of an example than perfected feature, 6 1.1 christos * but this code has been in use on my local network for a year and 7 1.1 christos * is quite useful as is (with a bit of external help, at any rate). 8 1.1 christos * 9 1.1 christos * Written January 2003 by John F. Woods, jfw (at) funhouse.com 10 1.1 christos */ 11 1.1 christos 12 1.1 christos /* 13 1.1 christos * Copyright (c) 2004 The NetBSD Foundation, Inc. 14 1.1 christos * All rights reserved. 15 1.1 christos * 16 1.1 christos * This code is derived from software contributed to The NetBSD Foundation 17 1.1 christos * by John F. Woods, jfw (at) funhouse.com. 18 1.1 christos * 19 1.1 christos * Redistribution and use in source and binary forms, with or without 20 1.1 christos * modification, are permitted provided that the following conditions 21 1.1 christos * are met: 22 1.1 christos * 1. Redistributions of source code must retain the above copyright 23 1.1 christos * notice, this list of conditions and the following disclaimer. 24 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 25 1.1 christos * notice, this list of conditions and the following disclaimer in the 26 1.1 christos * documentation and/or other materials provided with the distribution. 27 1.1 christos * 28 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 29 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 32 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 1.1 christos * POSSIBILITY OF SUCH DAMAGE. 39 1.1 christos */ 40 1.1 christos #include <stddef.h> 41 1.1 christos #include <time.h> 42 1.1 christos #include <stdio.h> 43 1.1 christos #include "pppd.h" 44 1.1 christos 45 1.1 christos /* this version string will be checked against pppd's version string when 46 1.1 christos * this plugin is loaded. 47 1.1 christos */ 48 1.1 christos char pppd_version[] = VERSION; 49 1.1 christos 50 1.1 christos /* 51 1.1 christos * Usage: to use this status plug as-is, add the lines: 52 1.1 christos plugin status 53 1.1 christos lcpstatus /var/run/ppp0.status 54 1.1 christos * to your PPP options file (or add the appropriate options to your 55 1.1 christos * pppd command line), where /var/run/ppp0.status can vary to taste. 56 1.1 christos * This plugin will then proceed to create the lcp status file and 57 1.1 christos * write one of four strings to the file based on the most recent LCP 58 1.1 christos * event: "up", "down", "?", or "!", representing LCP up, LCP down, 59 1.1 christos * LCP echo not received, and received-our-own-LCP-echo (probably 60 1.1 christos * indicating a loopback or a disconnected modem echoing back characters). 61 1.1 christos * On my system, I have a separate program which reads that file every 62 1.1 christos * second and sends a UDP broadcast packet on my ethernet with the contents 63 1.1 christos * of the file; each of the other computers on my ethernet (all Macs) have 64 1.1 christos * a tiny little program which listens for that UDP broadcast packet and 65 1.1 christos * updates a menubar status indicator; the end result is that when PPP 66 1.1 christos * shuts down, users on my LAN immediately know without having to go look 67 1.1 christos * at the modem. (Or without demanding that *I* go look at the modem...) 68 1.1 christos * 69 1.1 christos * If you want to modify this plugin, other ways you could use and display 70 1.1 christos * the data generated by the transitions would include: 71 1.1 christos * + directly broadcasting the results from inside the pppd task (rather 72 1.1 christos * than having a separate process do it) 73 1.1 christos * + store the ppp state in an SNMP database so it could be displayed with 74 1.1 christos * a standard form of client rather than a goofy little Mac OS X menubar 75 1.1 christos * widget. 76 1.1 christos */ 77 1.1 christos 78 1.1 christos static char *statusfilename = 0; 79 1.1 christos static char *laststatus = 0; 80 1.1 christos static char UP[] = "up"; 81 1.1 christos static char DOWN[] = "down"; 82 1.1 christos static char MISS[] = "?"; 83 1.1 christos static char MINE[] = "!"; 84 1.1 christos 85 1.1 christos static option_t status_options[] = { 86 1.1 christos { "lcpstatus", o_string, &statusfilename, 87 1.1 christos "Name of file to which LCP status string will be written" }, 88 1.1 christos { NULL } 89 1.1 christos }; 90 1.1 christos 91 1.1 christos /* status should be one of the canned constants above. */ 92 1.1 christos static void writestatus(char *status) 93 1.1 christos { 94 1.1 christos FILE *statusfile; 95 1.1 christos if (status == laststatus) return; /* we knew that already */ 96 1.1 christos statusfile = fopen(statusfilename, "w"); 97 1.1 christos if (!statusfile) { 98 1.1 christos warn("can't write %s to log LCP status", statusfilename); 99 1.1 christos free(statusfilename); 100 1.1 christos statusfilename = 0; 101 1.1 christos return; 102 1.1 christos } 103 1.1 christos fprintf(statusfile, "%s\n", status); 104 1.1 christos fclose(statusfile); 105 1.1 christos laststatus = status; 106 1.1 christos } 107 1.1 christos 108 1.1 christos static void status_lcp_up(void) 109 1.1 christos { 110 1.1 christos if (!statusfilename) return; /* not enabled */ 111 1.1 christos writestatus(UP); 112 1.1 christos } 113 1.1 christos 114 1.1 christos static void status_lcp_down(void) 115 1.1 christos { 116 1.1 christos if (!statusfilename) return; /* not enabled */ 117 1.1 christos writestatus(DOWN); 118 1.1 christos } 119 1.1 christos 120 1.1 christos static void status_lcp_echo(int pending) 121 1.1 christos { 122 1.1 christos if (!statusfilename) return; /* not enabled */ 123 1.1 christos if (pending == 0) 124 1.1 christos writestatus(UP); 125 1.1 christos else if (laststatus != MINE) 126 1.1 christos writestatus(MISS); 127 1.1 christos } 128 1.1 christos 129 1.1 christos static void status_lcp_echoreply(int mine) 130 1.1 christos { 131 1.1 christos if (!statusfilename) return; /* not enabled */ 132 1.1 christos if (mine == 0) 133 1.1 christos writestatus(UP); 134 1.1 christos else 135 1.1 christos writestatus(MINE); 136 1.1 christos } 137 1.1 christos 138 1.1 christos void plugin_init(void) 139 1.1 christos { 140 1.1 christos info("Initialize PPP status plugin."); 141 1.1 christos add_options(status_options); 142 1.1 christos lcp_up_hook = status_lcp_up; 143 1.1 christos lcp_down_hook = status_lcp_down; 144 1.1 christos lcp_echo_hook = status_lcp_echo; 145 1.1 christos lcp_echoreply_hook = status_lcp_echoreply; 146 1.1 christos } 147