diff options
Diffstat (limited to 'src/pppd/pppd.c')
-rw-r--r-- | src/pppd/pppd.c | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/src/pppd/pppd.c b/src/pppd/pppd.c new file mode 100644 index 000000000..20d65d0f1 --- /dev/null +++ b/src/pppd/pppd.c @@ -0,0 +1,161 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2014 Tom Gundersen <teg@jklm.no> + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <sys/ioctl.h> +#include <linux/ppp-ioctl.h> +#include <net/if.h> + +#include "sd-ppp.h" +#include "sd-pppoe.h" +#include "sd-daemon.h" + +#include "event-util.h" + +#include "util.h" +#include "log.h" +#include "ppp-machine.h" + +typedef struct Manager { + sd_event *event; + sd_pppoe *pppoe; + sd_ppp *ppp; +} Manager; + +static void pppoe_handler(sd_pppoe *pppoe, int event, void *userdata) { + Manager *m = userdata; + int channel, r; + + assert(pppoe); + assert(m); + + switch (event) { + case PPPOE_EVENT_RUNNING: + r = sd_pppoe_get_channel(pppoe, &channel); + if (r < 0) { + log_warning("pppoe: could not get fd: %s", strerror(-r)); + return; + } + + log_info("pppoe: running"); + + r = sd_ppp_lower_up(m->ppp, channel); + if (r < 0) + log_warning("Could not start PPP: %s", strerror(-r)); + break; + + case PPPOE_EVENT_STOPPED: + sd_ppp_lower_down(m->ppp); + sd_pppoe_stop(m->pppoe); + sd_event_exit(m->event, 0); + log_info("pppoe: stopped"); + break; + + default: + assert_not_reached("pppoe: invalid event"); + } +} + +int main(int argc, char *argv[]) { + Manager m = {}; + const char *ifname; + int r, ifindex; + + log_set_target(LOG_TARGET_AUTO); + log_parse_environment(); + log_open(); + + if (argc != 2) { + log_error("This program takes one arguments."); + return EXIT_FAILURE; + } + + ifname = argv[1]; + ifindex = if_nametoindex(ifname); + + r = sd_pppoe_new(&m.pppoe); + if (r < 0) { + log_error("Could not create PPPoE: %s", + strerror(-r)); + return EXIT_FAILURE; + } + + r = sd_event_new(&m.event); + if (r < 0) { + log_error("Could not create event: %s", + strerror(-r)); + return EXIT_FAILURE; + } + + r = sd_ppp_new(&m.ppp, m.event, 0); + if (r < 0) { + log_error("Could not create PPP: %s", + strerror(-r)); + return EXIT_FAILURE; + } + + r = sd_pppoe_attach_event(m.pppoe, m.event, 0); + if (r < 0) { + log_error("Could not attach default event to PPPoE: %s", + strerror(-r)); + return EXIT_FAILURE; + } + + r = sd_pppoe_set_index(m.pppoe, ifindex); + if (r < 0) { + log_error("Could not set ifindex: %s", + strerror(-r)); + return EXIT_FAILURE; + } + + r = sd_pppoe_set_ifname(m.pppoe, ifname); + if (r < 0) { + log_error("Could not set ifname: %s", + strerror(-r)); + return EXIT_FAILURE; + } + + r = sd_pppoe_set_callback(m.pppoe, pppoe_handler, &m); + if (r < 0) { + log_error("Could not set callback: %s", + strerror(-r)); + return EXIT_FAILURE; + } + + r = sd_pppoe_start(m.pppoe); + if (r < 0) { + log_error("Could not start PPPoE: %s", + strerror(-r)); + return EXIT_FAILURE; + } + + r = sd_event_loop(m.event); + if (r < 0) { + log_error("Could not run mainloop: %s", + strerror(-r)); + return EXIT_FAILURE; + } + + sd_pppoe_unref(m.pppoe); + sd_ppp_unref(m.ppp); + sd_event_unref(m.event); + + return EXIT_SUCCESS; +} |