From 4e1b9faba7503f99ee2fbcd7458f66ade42fa309 Mon Sep 17 00:00:00 2001 From: Niklas Date: Thu, 1 Sep 2011 09:31:39 +0200 Subject: tried to clean the git. deleted old unused files and folders. moved customdhcpcd and LogReceiver to the fbgui folder --- LogReceiver/LogReceiver | Bin 0 -> 122376 bytes LogReceiver/LogReceiver.pro | 19 + LogReceiver/Makefile | 278 +++++ LogReceiver/Makefile.Debug | 178 +++ LogReceiver/Makefile.Release | 178 +++ LogReceiver/abortbootdialog.cpp | 98 ++ LogReceiver/abortbootdialog.h | 46 + LogReceiver/chooseinterfacedialog.cpp | 98 ++ LogReceiver/chooseinterfacedialog.h | 44 + LogReceiver/logreceiver.cpp | 448 ++++++++ LogReceiver/logreceiver.h | 74 ++ LogReceiver/logreceiver.ui | 25 + LogReceiver/main.cpp | 12 + LogReceiver/ndgui.cpp | 184 ++++ LogReceiver/ndgui.h | 67 ++ LogReceiver/ndgui.ui | 19 + customdhcpcd/Debug/makefile | 44 + customdhcpcd/Debug/objects.mk | 7 + customdhcpcd/Debug/sources.mk | 17 + customdhcpcd/Debug/src/arp.d | 21 + customdhcpcd/Debug/src/client.d | 37 + customdhcpcd/Debug/src/common.d | 6 + customdhcpcd/Debug/src/configure.d | 28 + customdhcpcd/Debug/src/customdhcpcd.d | 1 + customdhcpcd/Debug/src/dhcp.d | 17 + customdhcpcd/Debug/src/dhcpcd.d | 26 + customdhcpcd/Debug/src/discover.d | 1 + customdhcpcd/Debug/src/duid.d | 12 + customdhcpcd/Debug/src/info.d | 17 + customdhcpcd/Debug/src/interface.d | 15 + customdhcpcd/Debug/src/ipv4ll.d | 17 + customdhcpcd/Debug/src/logger.d | 10 + customdhcpcd/Debug/src/logwriter.d | 19 + customdhcpcd/Debug/src/signal.d | 8 + customdhcpcd/Debug/src/socket.d | 17 + customdhcpcd/Debug/src/subdir.mk | 85 ++ customdhcpcd/src/.gitignore | 5 + customdhcpcd/src/Makefile | 34 + customdhcpcd/src/README | 45 + customdhcpcd/src/arp.c | 284 +++++ customdhcpcd/src/arp.h | 39 + customdhcpcd/src/client.c | 1150 ++++++++++++++++++++ customdhcpcd/src/client.h | 35 + customdhcpcd/src/common.c | 249 +++++ customdhcpcd/src/common.h | 68 ++ customdhcpcd/src/config.h | 133 +++ customdhcpcd/src/configure.c | 813 ++++++++++++++ customdhcpcd/src/configure.h | 38 + customdhcpcd/src/customdhcpcd | Bin 0 -> 173171 bytes customdhcpcd/src/customdhcpcd.c | 17 + customdhcpcd/src/dhcp.c | 933 ++++++++++++++++ customdhcpcd/src/dhcp.h | 215 ++++ customdhcpcd/src/dhcpcd.c | 673 ++++++++++++ customdhcpcd/src/dhcpcd.h | 105 ++ customdhcpcd/src/dhcpcd.sh | 46 + customdhcpcd/src/discover.c | 7 + customdhcpcd/src/discover.h | 12 + customdhcpcd/src/duid.c | 118 ++ customdhcpcd/src/duid.h | 42 + customdhcpcd/src/info.c | 472 ++++++++ customdhcpcd/src/info.h | 42 + customdhcpcd/src/interface.c | 1060 ++++++++++++++++++ customdhcpcd/src/interface.h | 173 +++ customdhcpcd/src/ipv4ll.c | 70 ++ customdhcpcd/src/ipv4ll.h | 39 + customdhcpcd/src/logger.c | 128 +++ customdhcpcd/src/logger.h | 48 + customdhcpcd/src/logwriter.c | 149 +++ customdhcpcd/src/logwriter.h | 37 + customdhcpcd/src/mk/cc.mk | 23 + customdhcpcd/src/mk/depend.mk | 11 + customdhcpcd/src/mk/dist.mk | 11 + customdhcpcd/src/mk/man.mk | 14 + customdhcpcd/src/mk/os.mk | 77 ++ customdhcpcd/src/mk/prog.mk | 34 + customdhcpcd/src/signal.c | 183 ++++ customdhcpcd/src/signal.h | 40 + customdhcpcd/src/socket.c | 647 +++++++++++ customdhcpcd/src/socket.h | 46 + customdhcpcd/src/status.h | 19 + workspace/LogReceiver/LogReceiver | Bin 122376 -> 0 bytes workspace/LogReceiver/LogReceiver.pro | 19 - workspace/LogReceiver/Makefile | 278 ----- workspace/LogReceiver/Makefile.Debug | 178 --- workspace/LogReceiver/Makefile.Release | 178 --- workspace/LogReceiver/abortbootdialog.cpp | 98 -- workspace/LogReceiver/abortbootdialog.h | 46 - workspace/LogReceiver/chooseinterfacedialog.cpp | 98 -- workspace/LogReceiver/chooseinterfacedialog.h | 44 - workspace/LogReceiver/logreceiver.cpp | 448 -------- workspace/LogReceiver/logreceiver.h | 74 -- workspace/LogReceiver/logreceiver.ui | 25 - workspace/LogReceiver/main.cpp | 12 - workspace/LogReceiver/ndgui.cpp | 184 ---- workspace/LogReceiver/ndgui.h | 67 -- workspace/LogReceiver/ndgui.ui | 19 - workspace/LogWriter/LogWriter | Bin 35093 -> 0 bytes workspace/LogWriter/LogWriter.pro | 11 - workspace/LogWriter/logwriter.cpp | 120 -- workspace/LogWriter/logwriter.h | 50 - workspace/LogWriter/logwriter.ui | 19 - workspace/LogWriter/main.cpp | 12 - workspace/customdhcpcd/Debug/makefile | 44 - workspace/customdhcpcd/Debug/objects.mk | 7 - workspace/customdhcpcd/Debug/sources.mk | 17 - workspace/customdhcpcd/Debug/src/arp.d | 21 - workspace/customdhcpcd/Debug/src/client.d | 37 - workspace/customdhcpcd/Debug/src/common.d | 6 - workspace/customdhcpcd/Debug/src/configure.d | 28 - workspace/customdhcpcd/Debug/src/customdhcpcd.d | 1 - workspace/customdhcpcd/Debug/src/dhcp.d | 17 - workspace/customdhcpcd/Debug/src/dhcpcd.d | 26 - workspace/customdhcpcd/Debug/src/discover.d | 1 - workspace/customdhcpcd/Debug/src/duid.d | 12 - workspace/customdhcpcd/Debug/src/info.d | 17 - workspace/customdhcpcd/Debug/src/interface.d | 15 - workspace/customdhcpcd/Debug/src/ipv4ll.d | 17 - workspace/customdhcpcd/Debug/src/logger.d | 10 - workspace/customdhcpcd/Debug/src/logwriter.d | 19 - workspace/customdhcpcd/Debug/src/signal.d | 8 - workspace/customdhcpcd/Debug/src/socket.d | 17 - workspace/customdhcpcd/Debug/src/subdir.mk | 85 -- workspace/customdhcpcd/src/.gitignore | 5 - workspace/customdhcpcd/src/Makefile | 34 - workspace/customdhcpcd/src/README | 45 - workspace/customdhcpcd/src/arp.c | 284 ----- workspace/customdhcpcd/src/arp.h | 39 - workspace/customdhcpcd/src/client.c | 1150 -------------------- workspace/customdhcpcd/src/client.h | 35 - workspace/customdhcpcd/src/common.c | 249 ----- workspace/customdhcpcd/src/common.h | 68 -- workspace/customdhcpcd/src/config.h | 133 --- workspace/customdhcpcd/src/configure.c | 813 -------------- workspace/customdhcpcd/src/configure.h | 38 - workspace/customdhcpcd/src/customdhcpcd | Bin 173171 -> 0 bytes workspace/customdhcpcd/src/customdhcpcd.c | 17 - workspace/customdhcpcd/src/dhcp.c | 933 ---------------- workspace/customdhcpcd/src/dhcp.h | 215 ---- workspace/customdhcpcd/src/dhcpcd.c | 673 ------------ workspace/customdhcpcd/src/dhcpcd.h | 105 -- workspace/customdhcpcd/src/dhcpcd.sh | 46 - workspace/customdhcpcd/src/discover.c | 7 - workspace/customdhcpcd/src/discover.h | 12 - workspace/customdhcpcd/src/duid.c | 118 -- workspace/customdhcpcd/src/duid.h | 42 - workspace/customdhcpcd/src/info.c | 472 -------- workspace/customdhcpcd/src/info.h | 42 - workspace/customdhcpcd/src/interface.c | 1060 ------------------ workspace/customdhcpcd/src/interface.h | 173 --- workspace/customdhcpcd/src/ipv4ll.c | 70 -- workspace/customdhcpcd/src/ipv4ll.h | 39 - workspace/customdhcpcd/src/logger.c | 128 --- workspace/customdhcpcd/src/logger.h | 48 - workspace/customdhcpcd/src/logwriter.c | 149 --- workspace/customdhcpcd/src/logwriter.h | 37 - workspace/customdhcpcd/src/mk/cc.mk | 23 - workspace/customdhcpcd/src/mk/depend.mk | 11 - workspace/customdhcpcd/src/mk/dist.mk | 11 - workspace/customdhcpcd/src/mk/man.mk | 14 - workspace/customdhcpcd/src/mk/os.mk | 77 -- workspace/customdhcpcd/src/mk/prog.mk | 34 - workspace/customdhcpcd/src/signal.c | 183 ---- workspace/customdhcpcd/src/signal.h | 40 - workspace/customdhcpcd/src/socket.c | 647 ----------- workspace/customdhcpcd/src/socket.h | 46 - workspace/customdhcpcd/src/status.h | 19 - .../org.eclipse.ltk.core.refactoring.prefs | 3 - workspace/networkDiscovery/dhcpcd/.gitignore | 5 - workspace/networkDiscovery/dhcpcd/README | 45 - workspace/networkDiscovery/dhcpcd/arp.c | 284 ----- workspace/networkDiscovery/dhcpcd/arp.h | 39 - workspace/networkDiscovery/dhcpcd/client.c | 1136 ------------------- workspace/networkDiscovery/dhcpcd/client.h | 35 - workspace/networkDiscovery/dhcpcd/common.c | 249 ----- workspace/networkDiscovery/dhcpcd/common.h | 68 -- workspace/networkDiscovery/dhcpcd/config.h | 133 --- workspace/networkDiscovery/dhcpcd/configure.c | 809 -------------- workspace/networkDiscovery/dhcpcd/configure.h | 38 - workspace/networkDiscovery/dhcpcd/dhcp.c | 933 ---------------- workspace/networkDiscovery/dhcpcd/dhcp.h | 215 ---- workspace/networkDiscovery/dhcpcd/dhcpcd.c | 366 ------- workspace/networkDiscovery/dhcpcd/dhcpcd.h | 96 -- workspace/networkDiscovery/dhcpcd/dhcpcd.sh | 46 - workspace/networkDiscovery/dhcpcd/duid.c | 118 -- workspace/networkDiscovery/dhcpcd/duid.h | 42 - workspace/networkDiscovery/dhcpcd/info.c | 472 -------- workspace/networkDiscovery/dhcpcd/info.h | 42 - workspace/networkDiscovery/dhcpcd/interface.c | 1060 ------------------ workspace/networkDiscovery/dhcpcd/interface.h | 173 --- workspace/networkDiscovery/dhcpcd/ipv4ll.c | 70 -- workspace/networkDiscovery/dhcpcd/ipv4ll.h | 39 - workspace/networkDiscovery/dhcpcd/logger.c | 158 --- workspace/networkDiscovery/dhcpcd/logger.h | 53 - workspace/networkDiscovery/dhcpcd/signal.c | 183 ---- workspace/networkDiscovery/dhcpcd/signal.h | 40 - workspace/networkDiscovery/dhcpcd/socket.c | 647 ----------- workspace/networkDiscovery/dhcpcd/socket.h | 46 - workspace/networkDiscovery/main.cpp | 12 - workspace/networkDiscovery/networkDiscovery.pro | 42 - workspace/networkDiscovery/networkdiscovery.cpp | 125 --- workspace/networkDiscovery/networkdiscovery.h | 35 - workspace/networkDiscovery/networkdiscovery.ui | 19 - 202 files changed, 10557 insertions(+), 18645 deletions(-) create mode 100755 LogReceiver/LogReceiver create mode 100644 LogReceiver/LogReceiver.pro create mode 100644 LogReceiver/Makefile create mode 100644 LogReceiver/Makefile.Debug create mode 100644 LogReceiver/Makefile.Release create mode 100644 LogReceiver/abortbootdialog.cpp create mode 100644 LogReceiver/abortbootdialog.h create mode 100644 LogReceiver/chooseinterfacedialog.cpp create mode 100644 LogReceiver/chooseinterfacedialog.h create mode 100644 LogReceiver/logreceiver.cpp create mode 100644 LogReceiver/logreceiver.h create mode 100644 LogReceiver/logreceiver.ui create mode 100644 LogReceiver/main.cpp create mode 100644 LogReceiver/ndgui.cpp create mode 100644 LogReceiver/ndgui.h create mode 100644 LogReceiver/ndgui.ui create mode 100644 customdhcpcd/Debug/makefile create mode 100644 customdhcpcd/Debug/objects.mk create mode 100644 customdhcpcd/Debug/sources.mk create mode 100644 customdhcpcd/Debug/src/arp.d create mode 100644 customdhcpcd/Debug/src/client.d create mode 100644 customdhcpcd/Debug/src/common.d create mode 100644 customdhcpcd/Debug/src/configure.d create mode 100644 customdhcpcd/Debug/src/customdhcpcd.d create mode 100644 customdhcpcd/Debug/src/dhcp.d create mode 100644 customdhcpcd/Debug/src/dhcpcd.d create mode 100644 customdhcpcd/Debug/src/discover.d create mode 100644 customdhcpcd/Debug/src/duid.d create mode 100644 customdhcpcd/Debug/src/info.d create mode 100644 customdhcpcd/Debug/src/interface.d create mode 100644 customdhcpcd/Debug/src/ipv4ll.d create mode 100644 customdhcpcd/Debug/src/logger.d create mode 100644 customdhcpcd/Debug/src/logwriter.d create mode 100644 customdhcpcd/Debug/src/signal.d create mode 100644 customdhcpcd/Debug/src/socket.d create mode 100644 customdhcpcd/Debug/src/subdir.mk create mode 100644 customdhcpcd/src/.gitignore create mode 100644 customdhcpcd/src/Makefile create mode 100644 customdhcpcd/src/README create mode 100644 customdhcpcd/src/arp.c create mode 100644 customdhcpcd/src/arp.h create mode 100644 customdhcpcd/src/client.c create mode 100644 customdhcpcd/src/client.h create mode 100644 customdhcpcd/src/common.c create mode 100644 customdhcpcd/src/common.h create mode 100644 customdhcpcd/src/config.h create mode 100644 customdhcpcd/src/configure.c create mode 100644 customdhcpcd/src/configure.h create mode 100755 customdhcpcd/src/customdhcpcd create mode 100644 customdhcpcd/src/customdhcpcd.c create mode 100644 customdhcpcd/src/dhcp.c create mode 100644 customdhcpcd/src/dhcp.h create mode 100644 customdhcpcd/src/dhcpcd.c create mode 100644 customdhcpcd/src/dhcpcd.h create mode 100644 customdhcpcd/src/dhcpcd.sh create mode 100644 customdhcpcd/src/discover.c create mode 100644 customdhcpcd/src/discover.h create mode 100644 customdhcpcd/src/duid.c create mode 100644 customdhcpcd/src/duid.h create mode 100644 customdhcpcd/src/info.c create mode 100644 customdhcpcd/src/info.h create mode 100644 customdhcpcd/src/interface.c create mode 100644 customdhcpcd/src/interface.h create mode 100644 customdhcpcd/src/ipv4ll.c create mode 100644 customdhcpcd/src/ipv4ll.h create mode 100644 customdhcpcd/src/logger.c create mode 100644 customdhcpcd/src/logger.h create mode 100644 customdhcpcd/src/logwriter.c create mode 100644 customdhcpcd/src/logwriter.h create mode 100644 customdhcpcd/src/mk/cc.mk create mode 100644 customdhcpcd/src/mk/depend.mk create mode 100644 customdhcpcd/src/mk/dist.mk create mode 100644 customdhcpcd/src/mk/man.mk create mode 100644 customdhcpcd/src/mk/os.mk create mode 100644 customdhcpcd/src/mk/prog.mk create mode 100644 customdhcpcd/src/signal.c create mode 100644 customdhcpcd/src/signal.h create mode 100644 customdhcpcd/src/socket.c create mode 100644 customdhcpcd/src/socket.h create mode 100644 customdhcpcd/src/status.h delete mode 100755 workspace/LogReceiver/LogReceiver delete mode 100644 workspace/LogReceiver/LogReceiver.pro delete mode 100644 workspace/LogReceiver/Makefile delete mode 100644 workspace/LogReceiver/Makefile.Debug delete mode 100644 workspace/LogReceiver/Makefile.Release delete mode 100644 workspace/LogReceiver/abortbootdialog.cpp delete mode 100644 workspace/LogReceiver/abortbootdialog.h delete mode 100644 workspace/LogReceiver/chooseinterfacedialog.cpp delete mode 100644 workspace/LogReceiver/chooseinterfacedialog.h delete mode 100644 workspace/LogReceiver/logreceiver.cpp delete mode 100644 workspace/LogReceiver/logreceiver.h delete mode 100644 workspace/LogReceiver/logreceiver.ui delete mode 100644 workspace/LogReceiver/main.cpp delete mode 100644 workspace/LogReceiver/ndgui.cpp delete mode 100644 workspace/LogReceiver/ndgui.h delete mode 100644 workspace/LogReceiver/ndgui.ui delete mode 100755 workspace/LogWriter/LogWriter delete mode 100644 workspace/LogWriter/LogWriter.pro delete mode 100644 workspace/LogWriter/logwriter.cpp delete mode 100644 workspace/LogWriter/logwriter.h delete mode 100644 workspace/LogWriter/logwriter.ui delete mode 100644 workspace/LogWriter/main.cpp delete mode 100644 workspace/customdhcpcd/Debug/makefile delete mode 100644 workspace/customdhcpcd/Debug/objects.mk delete mode 100644 workspace/customdhcpcd/Debug/sources.mk delete mode 100644 workspace/customdhcpcd/Debug/src/arp.d delete mode 100644 workspace/customdhcpcd/Debug/src/client.d delete mode 100644 workspace/customdhcpcd/Debug/src/common.d delete mode 100644 workspace/customdhcpcd/Debug/src/configure.d delete mode 100644 workspace/customdhcpcd/Debug/src/customdhcpcd.d delete mode 100644 workspace/customdhcpcd/Debug/src/dhcp.d delete mode 100644 workspace/customdhcpcd/Debug/src/dhcpcd.d delete mode 100644 workspace/customdhcpcd/Debug/src/discover.d delete mode 100644 workspace/customdhcpcd/Debug/src/duid.d delete mode 100644 workspace/customdhcpcd/Debug/src/info.d delete mode 100644 workspace/customdhcpcd/Debug/src/interface.d delete mode 100644 workspace/customdhcpcd/Debug/src/ipv4ll.d delete mode 100644 workspace/customdhcpcd/Debug/src/logger.d delete mode 100644 workspace/customdhcpcd/Debug/src/logwriter.d delete mode 100644 workspace/customdhcpcd/Debug/src/signal.d delete mode 100644 workspace/customdhcpcd/Debug/src/socket.d delete mode 100644 workspace/customdhcpcd/Debug/src/subdir.mk delete mode 100644 workspace/customdhcpcd/src/.gitignore delete mode 100644 workspace/customdhcpcd/src/Makefile delete mode 100644 workspace/customdhcpcd/src/README delete mode 100644 workspace/customdhcpcd/src/arp.c delete mode 100644 workspace/customdhcpcd/src/arp.h delete mode 100644 workspace/customdhcpcd/src/client.c delete mode 100644 workspace/customdhcpcd/src/client.h delete mode 100644 workspace/customdhcpcd/src/common.c delete mode 100644 workspace/customdhcpcd/src/common.h delete mode 100644 workspace/customdhcpcd/src/config.h delete mode 100644 workspace/customdhcpcd/src/configure.c delete mode 100644 workspace/customdhcpcd/src/configure.h delete mode 100755 workspace/customdhcpcd/src/customdhcpcd delete mode 100644 workspace/customdhcpcd/src/customdhcpcd.c delete mode 100644 workspace/customdhcpcd/src/dhcp.c delete mode 100644 workspace/customdhcpcd/src/dhcp.h delete mode 100644 workspace/customdhcpcd/src/dhcpcd.c delete mode 100644 workspace/customdhcpcd/src/dhcpcd.h delete mode 100644 workspace/customdhcpcd/src/dhcpcd.sh delete mode 100644 workspace/customdhcpcd/src/discover.c delete mode 100644 workspace/customdhcpcd/src/discover.h delete mode 100644 workspace/customdhcpcd/src/duid.c delete mode 100644 workspace/customdhcpcd/src/duid.h delete mode 100644 workspace/customdhcpcd/src/info.c delete mode 100644 workspace/customdhcpcd/src/info.h delete mode 100644 workspace/customdhcpcd/src/interface.c delete mode 100644 workspace/customdhcpcd/src/interface.h delete mode 100644 workspace/customdhcpcd/src/ipv4ll.c delete mode 100644 workspace/customdhcpcd/src/ipv4ll.h delete mode 100644 workspace/customdhcpcd/src/logger.c delete mode 100644 workspace/customdhcpcd/src/logger.h delete mode 100644 workspace/customdhcpcd/src/logwriter.c delete mode 100644 workspace/customdhcpcd/src/logwriter.h delete mode 100644 workspace/customdhcpcd/src/mk/cc.mk delete mode 100644 workspace/customdhcpcd/src/mk/depend.mk delete mode 100644 workspace/customdhcpcd/src/mk/dist.mk delete mode 100644 workspace/customdhcpcd/src/mk/man.mk delete mode 100644 workspace/customdhcpcd/src/mk/os.mk delete mode 100644 workspace/customdhcpcd/src/mk/prog.mk delete mode 100644 workspace/customdhcpcd/src/signal.c delete mode 100644 workspace/customdhcpcd/src/signal.h delete mode 100644 workspace/customdhcpcd/src/socket.c delete mode 100644 workspace/customdhcpcd/src/socket.h delete mode 100644 workspace/customdhcpcd/src/status.h delete mode 100644 workspace/networkDiscovery/.settings/org.eclipse.ltk.core.refactoring.prefs delete mode 100644 workspace/networkDiscovery/dhcpcd/.gitignore delete mode 100644 workspace/networkDiscovery/dhcpcd/README delete mode 100644 workspace/networkDiscovery/dhcpcd/arp.c delete mode 100644 workspace/networkDiscovery/dhcpcd/arp.h delete mode 100644 workspace/networkDiscovery/dhcpcd/client.c delete mode 100644 workspace/networkDiscovery/dhcpcd/client.h delete mode 100644 workspace/networkDiscovery/dhcpcd/common.c delete mode 100644 workspace/networkDiscovery/dhcpcd/common.h delete mode 100644 workspace/networkDiscovery/dhcpcd/config.h delete mode 100644 workspace/networkDiscovery/dhcpcd/configure.c delete mode 100644 workspace/networkDiscovery/dhcpcd/configure.h delete mode 100644 workspace/networkDiscovery/dhcpcd/dhcp.c delete mode 100644 workspace/networkDiscovery/dhcpcd/dhcp.h delete mode 100644 workspace/networkDiscovery/dhcpcd/dhcpcd.c delete mode 100644 workspace/networkDiscovery/dhcpcd/dhcpcd.h delete mode 100644 workspace/networkDiscovery/dhcpcd/dhcpcd.sh delete mode 100644 workspace/networkDiscovery/dhcpcd/duid.c delete mode 100644 workspace/networkDiscovery/dhcpcd/duid.h delete mode 100644 workspace/networkDiscovery/dhcpcd/info.c delete mode 100644 workspace/networkDiscovery/dhcpcd/info.h delete mode 100644 workspace/networkDiscovery/dhcpcd/interface.c delete mode 100644 workspace/networkDiscovery/dhcpcd/interface.h delete mode 100644 workspace/networkDiscovery/dhcpcd/ipv4ll.c delete mode 100644 workspace/networkDiscovery/dhcpcd/ipv4ll.h delete mode 100644 workspace/networkDiscovery/dhcpcd/logger.c delete mode 100644 workspace/networkDiscovery/dhcpcd/logger.h delete mode 100644 workspace/networkDiscovery/dhcpcd/signal.c delete mode 100644 workspace/networkDiscovery/dhcpcd/signal.h delete mode 100644 workspace/networkDiscovery/dhcpcd/socket.c delete mode 100644 workspace/networkDiscovery/dhcpcd/socket.h delete mode 100644 workspace/networkDiscovery/main.cpp delete mode 100644 workspace/networkDiscovery/networkDiscovery.pro delete mode 100644 workspace/networkDiscovery/networkdiscovery.cpp delete mode 100644 workspace/networkDiscovery/networkdiscovery.h delete mode 100644 workspace/networkDiscovery/networkdiscovery.ui diff --git a/LogReceiver/LogReceiver b/LogReceiver/LogReceiver new file mode 100755 index 0000000..a16897b Binary files /dev/null and b/LogReceiver/LogReceiver differ diff --git a/LogReceiver/LogReceiver.pro b/LogReceiver/LogReceiver.pro new file mode 100644 index 0000000..19300fa --- /dev/null +++ b/LogReceiver/LogReceiver.pro @@ -0,0 +1,19 @@ +TEMPLATE = app +TARGET = LogReceiver +QT += core \ + gui \ + network +LIBS += -lsysfs +INCLUDEPATH += ../customdhcpcd/src +HEADERS += ndgui.h \ + logreceiver.h \ + abortbootdialog.h \ + chooseinterfacedialog.h +SOURCES += ndgui.cpp \ + main.cpp \ + logreceiver.cpp \ + abortbootdialog.cpp \ + chooseinterfacedialog.cpp +FORMS += ndgui.ui \ + logreceiver.ui +RESOURCES += diff --git a/LogReceiver/Makefile b/LogReceiver/Makefile new file mode 100644 index 0000000..cd541af --- /dev/null +++ b/LogReceiver/Makefile @@ -0,0 +1,278 @@ +############################################################################# +# Makefile for building: LogReceiver +# Generated by qmake (2.01a) (Qt 4.7.2) on: Wed Aug 24 10:11:58 2011 +# Project: LogReceiver.pro +# Template: app +# Command: /usr/local/Trolltech/QtEmbedded-4.7.2/bin/qmake -o Makefile LogReceiver.pro +############################################################################# + +####### Compiler, tools and options + +CC = gcc +CXX = g++ +DEFINES = -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED +CFLAGS = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES) +CXXFLAGS = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES) +INCPATH = -I/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/qws/linux-x86-g++ -I. -I/usr/local/Trolltech/QtEmbedded-4.7.2/include/QtCore -I/usr/local/Trolltech/QtEmbedded-4.7.2/include/QtNetwork -I/usr/local/Trolltech/QtEmbedded-4.7.2/include/QtGui -I/usr/local/Trolltech/QtEmbedded-4.7.2/include -I../customdhcpcd/src -I. -I. +LINK = g++ +LFLAGS = -Wl,-O1 -Wl,-rpath,/usr/local/Trolltech/QtEmbedded-4.7.2/lib +LIBS = $(SUBLIBS) -L/usr/local/Trolltech/QtEmbedded-4.7.2/lib -lsysfs -lQtGui -L/usr/local/Trolltech/QtEmbedded-4.7.2/lib -lQtNetwork -lQtCore -lpthread +AR = ar cqs +RANLIB = +QMAKE = /usr/local/Trolltech/QtEmbedded-4.7.2/bin/qmake +TAR = tar -cf +COMPRESS = gzip -9f +COPY = cp -f +SED = sed +COPY_FILE = $(COPY) +COPY_DIR = $(COPY) -r +STRIP = strip +INSTALL_FILE = install -m 644 -p +INSTALL_DIR = $(COPY_DIR) +INSTALL_PROGRAM = install -m 755 -p +DEL_FILE = rm -f +SYMLINK = ln -f -s +DEL_DIR = rmdir +MOVE = mv -f +CHK_DIR_EXISTS= test -d +MKDIR = mkdir -p + +####### Output directory + +OBJECTS_DIR = ./ + +####### Files + +SOURCES = ndgui.cpp \ + main.cpp \ + logreceiver.cpp \ + abortbootdialog.cpp \ + chooseinterfacedialog.cpp moc_ndgui.cpp \ + moc_logreceiver.cpp \ + moc_abortbootdialog.cpp \ + moc_chooseinterfacedialog.cpp +OBJECTS = ndgui.o \ + main.o \ + logreceiver.o \ + abortbootdialog.o \ + chooseinterfacedialog.o \ + moc_ndgui.o \ + moc_logreceiver.o \ + moc_abortbootdialog.o \ + moc_chooseinterfacedialog.o +DIST = /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/g++.conf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/unix.conf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/linux.conf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/qws.conf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/qconfig.pri \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/modules/qt_webkit_version.pri \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt_functions.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt_config.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/exclusive_builds.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/default_pre.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/release.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/default_post.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/warn_on.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/unix/thread.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/moc.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/resources.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/uic.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/yacc.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/lex.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/include_source_dir.prf \ + LogReceiver.pro +QMAKE_TARGET = LogReceiver +DESTDIR = +TARGET = LogReceiver + +first: all +####### Implicit rules + +.SUFFIXES: .o .c .cpp .cc .cxx .C + +.cpp.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.cc.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.cxx.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.C.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.c.o: + $(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<" + +####### Build rules + +all: Makefile $(TARGET) + +$(TARGET): ui_ndgui.h ui_logreceiver.h $(OBJECTS) + $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS) + +Makefile: LogReceiver.pro /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/qws/linux-x86-g++/qmake.conf /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/g++.conf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/unix.conf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/linux.conf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/qws.conf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/qconfig.pri \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/modules/qt_webkit_version.pri \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt_functions.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt_config.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/exclusive_builds.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/default_pre.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/release.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/default_post.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/warn_on.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/unix/thread.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/moc.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/resources.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/uic.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/yacc.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/lex.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/include_source_dir.prf \ + /usr/local/Trolltech/QtEmbedded-4.7.2/lib/libQtGui.prl \ + /usr/local/Trolltech/QtEmbedded-4.7.2/lib/libQtNetwork.prl \ + /usr/local/Trolltech/QtEmbedded-4.7.2/lib/libQtCore.prl + $(QMAKE) -o Makefile LogReceiver.pro +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/g++.conf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/unix.conf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/linux.conf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/qws.conf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/qconfig.pri: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/modules/qt_webkit_version.pri: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt_functions.prf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt_config.prf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/exclusive_builds.prf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/default_pre.prf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/release.prf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/default_post.prf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/warn_on.prf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt.prf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/unix/thread.prf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/moc.prf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/resources.prf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/uic.prf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/yacc.prf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/lex.prf: +/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/include_source_dir.prf: +/usr/local/Trolltech/QtEmbedded-4.7.2/lib/libQtGui.prl: +/usr/local/Trolltech/QtEmbedded-4.7.2/lib/libQtNetwork.prl: +/usr/local/Trolltech/QtEmbedded-4.7.2/lib/libQtCore.prl: +qmake: FORCE + @$(QMAKE) -o Makefile LogReceiver.pro + +dist: + @$(CHK_DIR_EXISTS) .tmp/LogReceiver1.0.0 || $(MKDIR) .tmp/LogReceiver1.0.0 + $(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/LogReceiver1.0.0/ && $(COPY_FILE) --parents ndgui.h logreceiver.h abortbootdialog.h chooseinterfacedialog.h .tmp/LogReceiver1.0.0/ && $(COPY_FILE) --parents ndgui.cpp main.cpp logreceiver.cpp abortbootdialog.cpp chooseinterfacedialog.cpp .tmp/LogReceiver1.0.0/ && $(COPY_FILE) --parents ndgui.ui logreceiver.ui .tmp/LogReceiver1.0.0/ && (cd `dirname .tmp/LogReceiver1.0.0` && $(TAR) LogReceiver1.0.0.tar LogReceiver1.0.0 && $(COMPRESS) LogReceiver1.0.0.tar) && $(MOVE) `dirname .tmp/LogReceiver1.0.0`/LogReceiver1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/LogReceiver1.0.0 + + +clean:compiler_clean + -$(DEL_FILE) $(OBJECTS) + -$(DEL_FILE) *~ core *.core + + +####### Sub-libraries + +distclean: clean + -$(DEL_FILE) $(TARGET) + -$(DEL_FILE) Makefile + + +check: first + +mocclean: compiler_moc_header_clean compiler_moc_source_clean + +mocables: compiler_moc_header_make_all compiler_moc_source_make_all + +compiler_moc_header_make_all: moc_ndgui.cpp moc_logreceiver.cpp moc_abortbootdialog.cpp moc_chooseinterfacedialog.cpp +compiler_moc_header_clean: + -$(DEL_FILE) moc_ndgui.cpp moc_logreceiver.cpp moc_abortbootdialog.cpp moc_chooseinterfacedialog.cpp +moc_ndgui.cpp: ui_ndgui.h \ + logreceiver.h \ + chooseinterfacedialog.h \ + abortbootdialog.h \ + ndgui.h + /usr/local/Trolltech/QtEmbedded-4.7.2/bin/moc $(DEFINES) $(INCPATH) ndgui.h -o moc_ndgui.cpp + +moc_logreceiver.cpp: logreceiver.h + /usr/local/Trolltech/QtEmbedded-4.7.2/bin/moc $(DEFINES) $(INCPATH) logreceiver.h -o moc_logreceiver.cpp + +moc_abortbootdialog.cpp: abortbootdialog.h + /usr/local/Trolltech/QtEmbedded-4.7.2/bin/moc $(DEFINES) $(INCPATH) abortbootdialog.h -o moc_abortbootdialog.cpp + +moc_chooseinterfacedialog.cpp: chooseinterfacedialog.h + /usr/local/Trolltech/QtEmbedded-4.7.2/bin/moc $(DEFINES) $(INCPATH) chooseinterfacedialog.h -o moc_chooseinterfacedialog.cpp + +compiler_rcc_make_all: +compiler_rcc_clean: +compiler_image_collection_make_all: qmake_image_collection.cpp +compiler_image_collection_clean: + -$(DEL_FILE) qmake_image_collection.cpp +compiler_moc_source_make_all: +compiler_moc_source_clean: +compiler_uic_make_all: ui_ndgui.h ui_logreceiver.h +compiler_uic_clean: + -$(DEL_FILE) ui_ndgui.h ui_logreceiver.h +ui_ndgui.h: ndgui.ui + /usr/local/Trolltech/QtEmbedded-4.7.2/bin/uic ndgui.ui -o ui_ndgui.h + +ui_logreceiver.h: logreceiver.ui + /usr/local/Trolltech/QtEmbedded-4.7.2/bin/uic logreceiver.ui -o ui_logreceiver.h + +compiler_yacc_decl_make_all: +compiler_yacc_decl_clean: +compiler_yacc_impl_make_all: +compiler_yacc_impl_clean: +compiler_lex_make_all: +compiler_lex_clean: +compiler_clean: compiler_moc_header_clean compiler_uic_clean + +####### Compile + +ndgui.o: ndgui.cpp ndgui.h \ + ui_ndgui.h \ + logreceiver.h \ + chooseinterfacedialog.h \ + abortbootdialog.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o ndgui.o ndgui.cpp + +main.o: main.cpp ndgui.h \ + ui_ndgui.h \ + logreceiver.h \ + chooseinterfacedialog.h \ + abortbootdialog.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o main.o main.cpp + +logreceiver.o: logreceiver.cpp logreceiver.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o logreceiver.o logreceiver.cpp + +abortbootdialog.o: abortbootdialog.cpp abortbootdialog.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o abortbootdialog.o abortbootdialog.cpp + +chooseinterfacedialog.o: chooseinterfacedialog.cpp chooseinterfacedialog.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o chooseinterfacedialog.o chooseinterfacedialog.cpp + +moc_ndgui.o: moc_ndgui.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_ndgui.o moc_ndgui.cpp + +moc_logreceiver.o: moc_logreceiver.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_logreceiver.o moc_logreceiver.cpp + +moc_abortbootdialog.o: moc_abortbootdialog.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_abortbootdialog.o moc_abortbootdialog.cpp + +moc_chooseinterfacedialog.o: moc_chooseinterfacedialog.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_chooseinterfacedialog.o moc_chooseinterfacedialog.cpp + +####### Install + +install: FORCE + +uninstall: FORCE + +FORCE: + diff --git a/LogReceiver/Makefile.Debug b/LogReceiver/Makefile.Debug new file mode 100644 index 0000000..29db2ad --- /dev/null +++ b/LogReceiver/Makefile.Debug @@ -0,0 +1,178 @@ +############################################################################# +# Makefile for building: LogReceiver +# Generated by qmake (2.01a) (Qt 4.7.2) on: Wed Jul 6 13:39:02 2011 +# Project: LogReceiver.pro +# Template: app +############################################################################# + +####### Compiler, tools and options + +CC = gcc +CXX = g++ +DEFINES = -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED +CFLAGS = -pipe -g -Wall -W -D_REENTRANT $(DEFINES) +CXXFLAGS = -pipe -g -Wall -W -D_REENTRANT $(DEFINES) +INCPATH = -I/usr/local/Trolltech/Qt-4.7.2/mkspecs/linux-g++ -I. -I/usr/local/Trolltech/Qt-4.7.2/include/QtCore -I/usr/local/Trolltech/Qt-4.7.2/include/QtNetwork -I/usr/local/Trolltech/Qt-4.7.2/include/QtGui -I/usr/local/Trolltech/Qt-4.7.2/include -Idebug -I. +LINK = g++ +LFLAGS = -Wl,-rpath,/usr/local/Trolltech/Qt-4.7.2/lib +LIBS = $(SUBLIBS) -L/usr/local/Trolltech/Qt-4.7.2/lib -lQtGui -L/usr/local/Trolltech/Qt-4.7.2/lib -L/usr/X11R6/lib -lQtNetwork -lQtCore -lpthread +AR = ar cqs +RANLIB = +QMAKE = /usr/local/Trolltech/Qt-4.7.2/bin/qmake +TAR = tar -cf +COMPRESS = gzip -9f +COPY = cp -f +SED = sed +COPY_FILE = $(COPY) +COPY_DIR = $(COPY) -r +STRIP = strip +INSTALL_FILE = install -m 644 -p +INSTALL_DIR = $(COPY_DIR) +INSTALL_PROGRAM = install -m 755 -p +DEL_FILE = rm -f +SYMLINK = ln -f -s +DEL_DIR = rmdir +MOVE = mv -f +CHK_DIR_EXISTS= test -d +MKDIR = mkdir -p + +####### Output directory + +OBJECTS_DIR = debug/ + +####### Files + +SOURCES = main.cpp \ + logreceiver.cpp debug/moc_logreceiver.cpp +OBJECTS = debug/main.o \ + debug/logreceiver.o \ + debug/moc_logreceiver.o +DIST = /usr/local/Trolltech/Qt-4.7.2/mkspecs/common/g++.conf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/common/unix.conf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/common/linux.conf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/qconfig.pri \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/modules/qt_webkit_version.pri \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/qt_functions.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/qt_config.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/exclusive_builds.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/default_pre.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/debug.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/debug_and_release.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/default_post.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/build_pass.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/warn_on.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/qt.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/unix/thread.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/moc.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/resources.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/uic.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/yacc.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/lex.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/include_source_dir.prf \ + LogReceiver.pro +QMAKE_TARGET = LogReceiver +DESTDIR = +TARGET = LogReceiver + +first: all +####### Implicit rules + +.SUFFIXES: .o .c .cpp .cc .cxx .C + +.cpp.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.cc.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.cxx.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.C.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.c.o: + $(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<" + +####### Build rules + +all: Makefile.Debug $(TARGET) + +$(TARGET): ui_logreceiver.h $(OBJECTS) + $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS) + +qmake: FORCE + @$(QMAKE) CONFIG+=debug_and_release -o Makefile.Debug LogReceiver.pro + +dist: + @$(CHK_DIR_EXISTS) debug/LogReceiver1.0.0 || $(MKDIR) debug/LogReceiver1.0.0 + $(COPY_FILE) --parents $(SOURCES) $(DIST) debug/LogReceiver1.0.0/ && $(COPY_FILE) --parents logreceiver.h debug/LogReceiver1.0.0/ && $(COPY_FILE) --parents main.cpp logreceiver.cpp debug/LogReceiver1.0.0/ && $(COPY_FILE) --parents logreceiver.ui debug/LogReceiver1.0.0/ && (cd `dirname debug/LogReceiver1.0.0` && $(TAR) LogReceiver1.0.0.tar LogReceiver1.0.0 && $(COMPRESS) LogReceiver1.0.0.tar) && $(MOVE) `dirname debug/LogReceiver1.0.0`/LogReceiver1.0.0.tar.gz . && $(DEL_FILE) -r debug/LogReceiver1.0.0 + + +clean:compiler_clean + -$(DEL_FILE) $(OBJECTS) + -$(DEL_FILE) *~ core *.core + + +####### Sub-libraries + +distclean: clean + -$(DEL_FILE) $(TARGET) + -$(DEL_FILE) Makefile.Debug + + +check: first + +mocclean: compiler_moc_header_clean compiler_moc_source_clean + +mocables: compiler_moc_header_make_all compiler_moc_source_make_all + +compiler_moc_header_make_all: debug/moc_logreceiver.cpp +compiler_moc_header_clean: + -$(DEL_FILE) debug/moc_logreceiver.cpp +debug/moc_logreceiver.cpp: ui_logreceiver.h \ + logreceiver.h + /usr/local/Trolltech/Qt-4.7.2/bin/moc $(DEFINES) $(INCPATH) logreceiver.h -o debug/moc_logreceiver.cpp + +compiler_rcc_make_all: +compiler_rcc_clean: +compiler_image_collection_make_all: qmake_image_collection.cpp +compiler_image_collection_clean: + -$(DEL_FILE) qmake_image_collection.cpp +compiler_moc_source_make_all: +compiler_moc_source_clean: +compiler_uic_make_all: ui_logreceiver.h +compiler_uic_clean: + -$(DEL_FILE) ui_logreceiver.h +ui_logreceiver.h: logreceiver.ui + /usr/local/Trolltech/Qt-4.7.2/bin/uic logreceiver.ui -o ui_logreceiver.h + +compiler_yacc_decl_make_all: +compiler_yacc_decl_clean: +compiler_yacc_impl_make_all: +compiler_yacc_impl_clean: +compiler_lex_make_all: +compiler_lex_clean: +compiler_clean: compiler_moc_header_clean compiler_uic_clean + +####### Compile + +debug/main.o: main.cpp logreceiver.h \ + ui_logreceiver.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o debug/main.o main.cpp + +debug/logreceiver.o: logreceiver.cpp logreceiver.h \ + ui_logreceiver.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o debug/logreceiver.o logreceiver.cpp + +debug/moc_logreceiver.o: debug/moc_logreceiver.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o debug/moc_logreceiver.o debug/moc_logreceiver.cpp + +####### Install + +install: FORCE + +uninstall: FORCE + +FORCE: + diff --git a/LogReceiver/Makefile.Release b/LogReceiver/Makefile.Release new file mode 100644 index 0000000..64e6a9f --- /dev/null +++ b/LogReceiver/Makefile.Release @@ -0,0 +1,178 @@ +############################################################################# +# Makefile for building: LogReceiver +# Generated by qmake (2.01a) (Qt 4.7.2) on: Wed Jul 6 13:39:02 2011 +# Project: LogReceiver.pro +# Template: app +############################################################################# + +####### Compiler, tools and options + +CC = gcc +CXX = g++ +DEFINES = -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED +CFLAGS = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES) +CXXFLAGS = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES) +INCPATH = -I/usr/local/Trolltech/Qt-4.7.2/mkspecs/linux-g++ -I. -I/usr/local/Trolltech/Qt-4.7.2/include/QtCore -I/usr/local/Trolltech/Qt-4.7.2/include/QtNetwork -I/usr/local/Trolltech/Qt-4.7.2/include/QtGui -I/usr/local/Trolltech/Qt-4.7.2/include -Irelease -I. +LINK = g++ +LFLAGS = -Wl,-O1 -Wl,-rpath,/usr/local/Trolltech/Qt-4.7.2/lib +LIBS = $(SUBLIBS) -L/usr/local/Trolltech/Qt-4.7.2/lib -lQtGui -L/usr/local/Trolltech/Qt-4.7.2/lib -L/usr/X11R6/lib -lQtNetwork -lQtCore -lpthread +AR = ar cqs +RANLIB = +QMAKE = /usr/local/Trolltech/Qt-4.7.2/bin/qmake +TAR = tar -cf +COMPRESS = gzip -9f +COPY = cp -f +SED = sed +COPY_FILE = $(COPY) +COPY_DIR = $(COPY) -r +STRIP = strip +INSTALL_FILE = install -m 644 -p +INSTALL_DIR = $(COPY_DIR) +INSTALL_PROGRAM = install -m 755 -p +DEL_FILE = rm -f +SYMLINK = ln -f -s +DEL_DIR = rmdir +MOVE = mv -f +CHK_DIR_EXISTS= test -d +MKDIR = mkdir -p + +####### Output directory + +OBJECTS_DIR = release/ + +####### Files + +SOURCES = main.cpp \ + logreceiver.cpp release/moc_logreceiver.cpp +OBJECTS = release/main.o \ + release/logreceiver.o \ + release/moc_logreceiver.o +DIST = /usr/local/Trolltech/Qt-4.7.2/mkspecs/common/g++.conf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/common/unix.conf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/common/linux.conf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/qconfig.pri \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/modules/qt_webkit_version.pri \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/qt_functions.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/qt_config.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/exclusive_builds.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/default_pre.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/release.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/debug_and_release.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/default_post.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/build_pass.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/warn_on.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/qt.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/unix/thread.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/moc.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/resources.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/uic.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/yacc.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/lex.prf \ + /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/include_source_dir.prf \ + LogReceiver.pro +QMAKE_TARGET = LogReceiver +DESTDIR = +TARGET = LogReceiver + +first: all +####### Implicit rules + +.SUFFIXES: .o .c .cpp .cc .cxx .C + +.cpp.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.cc.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.cxx.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.C.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.c.o: + $(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<" + +####### Build rules + +all: Makefile.Release $(TARGET) + +$(TARGET): ui_logreceiver.h $(OBJECTS) + $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS) + +qmake: FORCE + @$(QMAKE) CONFIG+=debug_and_release -o Makefile.Release LogReceiver.pro + +dist: + @$(CHK_DIR_EXISTS) release/LogReceiver1.0.0 || $(MKDIR) release/LogReceiver1.0.0 + $(COPY_FILE) --parents $(SOURCES) $(DIST) release/LogReceiver1.0.0/ && $(COPY_FILE) --parents logreceiver.h release/LogReceiver1.0.0/ && $(COPY_FILE) --parents main.cpp logreceiver.cpp release/LogReceiver1.0.0/ && $(COPY_FILE) --parents logreceiver.ui release/LogReceiver1.0.0/ && (cd `dirname release/LogReceiver1.0.0` && $(TAR) LogReceiver1.0.0.tar LogReceiver1.0.0 && $(COMPRESS) LogReceiver1.0.0.tar) && $(MOVE) `dirname release/LogReceiver1.0.0`/LogReceiver1.0.0.tar.gz . && $(DEL_FILE) -r release/LogReceiver1.0.0 + + +clean:compiler_clean + -$(DEL_FILE) $(OBJECTS) + -$(DEL_FILE) *~ core *.core + + +####### Sub-libraries + +distclean: clean + -$(DEL_FILE) $(TARGET) + -$(DEL_FILE) Makefile.Release + + +check: first + +mocclean: compiler_moc_header_clean compiler_moc_source_clean + +mocables: compiler_moc_header_make_all compiler_moc_source_make_all + +compiler_moc_header_make_all: release/moc_logreceiver.cpp +compiler_moc_header_clean: + -$(DEL_FILE) release/moc_logreceiver.cpp +release/moc_logreceiver.cpp: ui_logreceiver.h \ + logreceiver.h + /usr/local/Trolltech/Qt-4.7.2/bin/moc $(DEFINES) $(INCPATH) logreceiver.h -o release/moc_logreceiver.cpp + +compiler_rcc_make_all: +compiler_rcc_clean: +compiler_image_collection_make_all: qmake_image_collection.cpp +compiler_image_collection_clean: + -$(DEL_FILE) qmake_image_collection.cpp +compiler_moc_source_make_all: +compiler_moc_source_clean: +compiler_uic_make_all: ui_logreceiver.h +compiler_uic_clean: + -$(DEL_FILE) ui_logreceiver.h +ui_logreceiver.h: logreceiver.ui + /usr/local/Trolltech/Qt-4.7.2/bin/uic logreceiver.ui -o ui_logreceiver.h + +compiler_yacc_decl_make_all: +compiler_yacc_decl_clean: +compiler_yacc_impl_make_all: +compiler_yacc_impl_clean: +compiler_lex_make_all: +compiler_lex_clean: +compiler_clean: compiler_moc_header_clean compiler_uic_clean + +####### Compile + +release/main.o: main.cpp logreceiver.h \ + ui_logreceiver.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o release/main.o main.cpp + +release/logreceiver.o: logreceiver.cpp logreceiver.h \ + ui_logreceiver.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o release/logreceiver.o logreceiver.cpp + +release/moc_logreceiver.o: release/moc_logreceiver.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o release/moc_logreceiver.o release/moc_logreceiver.cpp + +####### Install + +install: FORCE + +uninstall: FORCE + +FORCE: + diff --git a/LogReceiver/abortbootdialog.cpp b/LogReceiver/abortbootdialog.cpp new file mode 100644 index 0000000..1614f6b --- /dev/null +++ b/LogReceiver/abortbootdialog.cpp @@ -0,0 +1,98 @@ +#include + +#include "abortbootdialog.h" + +AbortBootDialog::AbortBootDialog(QWidget *parent) : + QDialog(parent) +{ + oneMinuteCountdown = 60; + + createContentGroupBox(); + createButtonGroupBox(); + + createTimer(); + + mainLayout = new QVBoxLayout; + mainLayout->addWidget(contentGroupBox); + mainLayout->addWidget(buttonGroupBox); + + setLayout(mainLayout); + setWindowTitle(tr("Abort Boot")); +} +void AbortBootDialog::createContentGroupBox() +{ + contentGroupBox = new QGroupBox; + timerLabel = new QLabel(QString::number(oneMinuteCountdown)); + QHBoxLayout *layout = new QHBoxLayout; + + + layout->addWidget(new QLabel(tr("test"))); + layout->addWidget(timerLabel); + contentGroupBox->setLayout(layout); +} + +void AbortBootDialog::createButtonGroupBox() +{ + buttonGroupBox = new QGroupBox; + QHBoxLayout *layout = new QHBoxLayout; + + QPushButton *shutDownButton = new QPushButton(tr("Shut Down")); + shutDownButton->setDefault(true); + QPushButton *restartButton = new QPushButton(tr("Restart")); + restartButton->setAutoDefault(false); + QPushButton *showLogButton = new QPushButton(tr("Show Log")); + showLogButton->setAutoDefault(false); + + connect(showLogButton, SIGNAL(clicked()),this, SLOT(showLogButtonClicked())); + connect(shutDownButton, SIGNAL(clicked()), this, SLOT(shutDownButtonClicked())); + connect(restartButton, SIGNAL(clicked()), this, SLOT(restartButtonClicked())); + + layout->addWidget(showLogButton); + layout->addWidget(restartButton); + layout->addWidget(shutDownButton); + buttonGroupBox->setLayout(layout); +} + +void AbortBootDialog::createTimer() +{ + timer = new QTimer(this); + connect(timer, SIGNAL(timeout()),this, SLOT(timerLabelUpdate())); + timer->start(1000); +} + +void AbortBootDialog::showLogButtonClicked() +{ + qDebug() << "show log button clicked"; + emit showLogSignal(); +} + +void AbortBootDialog::shutDownButtonClicked() +{ + qDebug() << "shut down button clicked"; + emit shutDownSignal(); +} + +void AbortBootDialog::restartButtonClicked() +{ + qDebug() << "restart button clicked"; + emit restartSignal(); +} + +void AbortBootDialog::timerLabelUpdate() +{ + oneMinuteCountdown = oneMinuteCountdown -1; + timerLabel->setText(QString::number(oneMinuteCountdown)); + if(oneMinuteCountdown <= 0) + { + timer->stop(); + emit shutDownSignal(); + } + +} + +void AbortBootDialog::closeDialog() +{ + this->hide(); + this->killTimer(timer->timerId()); + this->close(); +} diff --git a/LogReceiver/abortbootdialog.h b/LogReceiver/abortbootdialog.h new file mode 100644 index 0000000..157331b --- /dev/null +++ b/LogReceiver/abortbootdialog.h @@ -0,0 +1,46 @@ +#ifndef ABORTBOOTDIALOG_H +#define ABORTBOOTDIALOG_H + +#include +#include "qboxlayout.h" +#include "qgroupbox.h" +#include "qcombobox.h" +#include "qlabel.h" + + +class AbortBootDialog : public QDialog +{ + Q_OBJECT +public: + explicit AbortBootDialog(QWidget *parent = 0); + + void closeDialog(); +private slots: + void showLogButtonClicked(); + void shutDownButtonClicked(); + void restartButtonClicked(); + void timerLabelUpdate(); + +private: + QVBoxLayout *mainLayout; + QGroupBox *contentGroupBox; + QGroupBox *buttonGroupBox; + QLabel *timerLabel; + QTimer *timer; + int oneMinuteCountdown; + + void createContentGroupBox(); + void createButtonGroupBox(); + void createTimer(); + + +signals: + void showLogSignal(); + void shutDownSignal(); + void restartSignal(); + +public slots: + +}; + +#endif // ABORTBOOTDIALOG_H diff --git a/LogReceiver/chooseinterfacedialog.cpp b/LogReceiver/chooseinterfacedialog.cpp new file mode 100644 index 0000000..b7a37e8 --- /dev/null +++ b/LogReceiver/chooseinterfacedialog.cpp @@ -0,0 +1,98 @@ +#include + +#include "chooseinterfacedialog.h" + +ChooseInterfaceDialog::ChooseInterfaceDialog(QStringList &interfaces, QWidget *parent) : + QDialog(parent) +{ + oneMinuteCountdown = 60; + + createContentGroupBox(interfaces); + createButtonGroupBox(); + + createTimer(); + + mainLayout = new QVBoxLayout; + mainLayout->addWidget(contentGroupBox); + mainLayout->addWidget(buttonGroupBox); + + setLayout(mainLayout); + setWindowTitle(tr("Choose Interface")); +} + +void ChooseInterfaceDialog::createContentGroupBox(QStringList &interfaces) +{ + contentGroupBox = new QGroupBox; + timerLabel = new QLabel(QString::number(oneMinuteCountdown)); + QVBoxLayout *groupBoxLayout = new QVBoxLayout; + QHBoxLayout *layout = new QHBoxLayout; + + comboBox = new QComboBox; + comboBox->addItems(interfaces); + + layout->addWidget(new QLabel(tr("test"))); + layout->addWidget(comboBox); + + groupBoxLayout->addWidget(timerLabel); + groupBoxLayout->addLayout(layout); + contentGroupBox->setLayout(groupBoxLayout); +} + +void ChooseInterfaceDialog::createButtonGroupBox() +{ + buttonGroupBox = new QGroupBox; + QHBoxLayout *layout = new QHBoxLayout; + + QPushButton *continueButton = new QPushButton(tr("Continue")); + continueButton->setDefault(true); + QPushButton *shutDownButton = new QPushButton(tr("Shut Down")); + shutDownButton->setAutoDefault(false); + QPushButton *restartButton = new QPushButton(tr("Restart")); + restartButton->setAutoDefault(false); + + connect(continueButton, SIGNAL(clicked()),this, SLOT(continueButtonClicked())); + connect(shutDownButton, SIGNAL(clicked()), this, SLOT(shutDownButtonClicked())); + connect(restartButton, SIGNAL(clicked()), this, SLOT(restartButtonClicked())); + + layout->addWidget(restartButton); + layout->addWidget(shutDownButton); + layout->addWidget(continueButton); + buttonGroupBox->setLayout(layout); +} + +void ChooseInterfaceDialog::createTimer() +{ + timer = new QTimer(this); + connect(timer, SIGNAL(timeout()),this, SLOT(timerLabelUpdate())); + timer->start(1000); +} + +void ChooseInterfaceDialog::continueButtonClicked() +{ + qDebug() << "continue button clicked"; + emit continueSignal(comboBox->currentText()); +} + +void ChooseInterfaceDialog::shutDownButtonClicked() +{ + qDebug() << "shut down button clicked"; + emit shutDownSignal(); +} + +void ChooseInterfaceDialog::restartButtonClicked() +{ + qDebug() << "restart button clicked"; + emit restartSignal(); +} + +void ChooseInterfaceDialog::timerLabelUpdate() +{ + oneMinuteCountdown = oneMinuteCountdown -1; + timerLabel->setText(QString::number(oneMinuteCountdown)); + if(oneMinuteCountdown <= 0) + { + timer->stop(); + emit continueSignal(comboBox->currentText()); + } + +} diff --git a/LogReceiver/chooseinterfacedialog.h b/LogReceiver/chooseinterfacedialog.h new file mode 100644 index 0000000..48ee038 --- /dev/null +++ b/LogReceiver/chooseinterfacedialog.h @@ -0,0 +1,44 @@ +#ifndef CHOOSEINTERFACEDIALOG_H +#define CHOOSEINTERFACEDIALOG_H + +#include +#include "qboxlayout.h" +#include "qgroupbox.h" +#include "qcombobox.h" +#include "qlabel.h" + +class ChooseInterfaceDialog : public QDialog +{ + Q_OBJECT +public: + explicit ChooseInterfaceDialog(QStringList &interfaces, QWidget *parent = 0); +private slots: + void continueButtonClicked(); + void shutDownButtonClicked(); + void restartButtonClicked(); + void timerLabelUpdate(); + +private: + QVBoxLayout *mainLayout; + QGroupBox *contentGroupBox; + QGroupBox *buttonGroupBox; + QComboBox *comboBox; + QLabel *timerLabel; + QTimer *timer; + int oneMinuteCountdown; + + void createContentGroupBox(QStringList &interfaces); + void createButtonGroupBox(); + void createTimer(); + +signals: + void continueSignal(QString ifName); + void shutDownSignal(); + void restartSignal(); + + +public slots: + +}; + +#endif // CHOOSEINTERFACEDIALOG_H diff --git a/LogReceiver/logreceiver.cpp b/LogReceiver/logreceiver.cpp new file mode 100644 index 0000000..802f442 --- /dev/null +++ b/LogReceiver/logreceiver.cpp @@ -0,0 +1,448 @@ +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "logreceiver.h" +#include +#include +#include "status.h" +#include "dhcp.h" + +LogReceiver::LogReceiver() { + + server = new QLocalServer(this); +} + +LogReceiver::~LogReceiver() { + +} + +void LogReceiver::initAndRun(QString serverPath, QString pathToExe, + QStringList* args) { + + if (serverPath != DEFAULT_QTSOCKETADDRESS) { + dhcpcdArguments.append("-q"); + dhcpcdArguments.append(serverPath); + } + if (!server->listen(serverPath)) { + /* + QMessageBox::critical(this, tr("LogReceiver"), tr( + "Unable to start the server: %1.") .arg(server->errorString())); + close(); + */ + // emit signal to the gui that a critial error occoured + QString errorInfo("Unable to start server: "); + qDebug() << "--- \t [LogReceiver::initAndRun] " + errorInfo + << server->errorString(); + emit + abortBoot(errorInfo + server->errorString()); + return; + } + + connect(server, SIGNAL(newConnection()), this, SLOT(handleNewConnection())); + + pathToDhcpcdExe = pathToExe; + // check if the path to the customdhcpcd file is correct + QFileInfo fInfo(pathToDhcpcdExe); + if (!fInfo.exists()) { + qDebug() + << "couldn't find customdhcpcd exe. Please check the path to this file."; + emit + abortBoot( + "couldn't find customdhcpcd exe. Please check the path to this file."); + return; + } + + if (args != NULL && !args->isEmpty()) { + qDebug() << "--- \t [LogReceiver::initAndRun] added additional args"; + dhcpcdArguments.append(*args); + } + + numberOfProcesses = 0; + + // start the main work: + + QList list = getListOfNetworkInterfaces(); + + if (list.size() > 0) { + + //list = checkCarrierState(list); + + + //dhcpcdArguments.append("-d"); + QString ifName("eth0"); + numberOfProcesses = list.size(); + runDHCPCD(list); + } else { + qDebug() << "list is empty. Haven't found usable interface."; + emit + abortBoot("Haven't found usable interface"); + return; + } + +} + +QList LogReceiver::getListOfNetworkInterfaces() { + QList nIList = QNetworkInterface::allInterfaces(); + QList result; + + if (nIList.size() > 0) { + foreach(QNetworkInterface nI, nIList) + { + if (((!(nI.flags() & QNetworkInterface::CanBroadcast) + || nI.flags() & QNetworkInterface::IsLoopBack) + || nI.flags() & QNetworkInterface::IsPointToPoint) + || checkBlackList(nI.humanReadableName())) { + continue; + } + if (!checkCarrierState(nI.humanReadableName())) { + continue; + } + result.append(nI.humanReadableName()); + emit addNewInterface(nI.humanReadableName()); + } + } else { + qDebug() << "no interfaces found!"; + } + return result; +} + +QList LogReceiver::checkCarrierState(QList &interfaces) { + QList result; + foreach(QString nI, interfaces) + { + if (checkCarrierState(nI)) { + // everything is fine, cable is plugged, + // go on with the next interface + //continue; + result.append(nI); + } + } + return result; +} + +bool LogReceiver::checkCarrierState(QString interface) { + + qDebug() << "check carrier state for interface " << interface; + QByteArray ba = interface.toAscii(); + const char * iface = ba.data(); + + struct sysfs_class_device *class_device = sysfs_open_class_device("net", + iface); + struct dlist *attrlist = sysfs_get_classdev_attributes(class_device); + if (attrlist != NULL) { + struct sysfs_attribute *attr = NULL; + dlist_for_each_data(attrlist, attr, struct sysfs_attribute) { + if (strcmp("carrier", attr->name) == 0) { + QString value(attr->value); + bool ok = false; + bool * pok = &ok; + int v = value.toInt(pok); + if (*pok) { + if (v == 1) { + qDebug() + << "carrier is 1. Cable is plugged. return true"; + return true; + } else { + qDebug() + << "carrier is 0. Cable is unplugged. return false"; + return false; + } + } else { + qDebug() << "conversion error"; + } + } + } + } else { + qDebug() << "attrlist is Null"; + } + sysfs_close_class_device(class_device); + + return true; +} + +void LogReceiver::runDHCPCD(QList &interfaces) { + foreach(QString nI, interfaces) + { + runDHCPCD(nI); + } +} + +void LogReceiver::runDHCPCD(QString interface) { + emit updateStatusLabel(interface, "start DHCP"); + dhcpcdArguments.append(interface); + QProcess * p = new QProcess(this); + + qDebug() << dhcpcdArguments; + + clientProcessToIfNameMap.insert(p, interface); + qDebug() << clientProcessToIfNameMap; + p->start(pathToDhcpcdExe, dhcpcdArguments); + connect(p, SIGNAL(started()), this, SLOT(handleProcessStarted())); + connect(p, SIGNAL(finished(int, QProcess::ExitStatus)), this, + SLOT(handleProcessFinished(int, QProcess::ExitStatus))); + dhcpcdArguments.removeLast(); +} + +void LogReceiver::checkInternetConnection(QString ifName) { + QString command("ping"); + QStringList argList; + QString timeout("1"); + QString total("2"); + int exitCode = -1; + QString destination("www.google.de"); + argList << "-I" << "ifName" << "-W" << timeout << "-c" << total + << destination; + argList.replace(1, ifName); + QProcess * p = new QProcess(this); + p->start(command, argList); + p->waitForFinished(); + exitCode = p->exitCode(); + if (exitCode > 0) { + qDebug() << "no internet connection with interface" << ifName; + //remove interface from list and inform user via debug console + emit updateStatusLabel(ifName, "connection not possible"); + } else if (exitCode == 0) { + qDebug() << "internet: check passed! for interface" << ifName; + emit + updateStatusLabel(ifName, "connection possible"); + emit connectionEstablished(ifName); + } +} + +void LogReceiver::checkInternetConnection(QList &interfaces) { + foreach(QString nI, interfaces) + { + checkInternetConnection(nI); + } +} +void LogReceiver::checkInternetConnectionViaTCP(QString ifName) { + + const bool canStartIAP = (configurationManager.capabilities() + & QNetworkConfigurationManager::CanStartAndStopInterfaces); + QList configs = + configurationManager.allConfigurations(); + QNetworkConfiguration cfg; + foreach(QNetworkConfiguration nC, configs) + { + if (nC.name() == ifName) { + qDebug() << "found config" << nC.name(); + cfg = nC; + break; + } + } + if (!cfg.isValid() || (!canStartIAP && cfg.state() + != QNetworkConfiguration::Active)) { + qDebug() << "config is not valid" << cfg.name(); + return; + } + QNetworkSession *session = new QNetworkSession(cfg, this); + session->open(); + if (session->waitForOpened(-1)) { + + qDebug () << "used interface for connectivity check:" <interface().humanReadableName(); + QTcpSocket *tcpSocket = new QTcpSocket(this); + tcpSocket->connectToHost(QString("209.85.148.105"), 80); + if (!tcpSocket->waitForConnected(2000)) { + qDebug() << tcpSocket->errorString(); + emit updateStatusLabel(ifName, "connection not possible"); + } else { + emit updateStatusLabel(ifName, "connection possible"); + emit connectionEstablished(ifName); + } + } else { + qDebug() << "couldn't open session"; + } + session->close(); +} + +void LogReceiver::handleNewConnection() { + qDebug() << "New Connection arrived"; + + /*QLocalSocket **/ + client = server ->nextPendingConnection(); + clients.insert(client, client); + connect(client, SIGNAL(disconnected()), this, + SLOT(handleClientDisconnect())); + connect(client, SIGNAL(readyRead()), this, SLOT(handleNewInput())); +} + +void LogReceiver::handleClientDisconnect() { + QLocalSocket* socket = qobject_cast (QObject::sender()); + + QLocalSocket * client = clients.value(socket); + + qDebug() << "disconnect client"; + handleNewInput(client); + client->deleteLater(); +} + +void LogReceiver::handleNewInput(QLocalSocket * client) { + qDebug() << "last read before exit"; + while (client->canReadLine()) { + QString data(client->readLine()); + + data = data.trimmed(); + qDebug() << data; + QStringList lines = data.split("\n"); + + for (int i = 0; i < lines.length(); i++) { + handleNewInputLine(client, lines.at(i)); + } + } +} + +void LogReceiver::handleNewInput() { + QLocalSocket* socket = qobject_cast (QObject::sender()); + + QLocalSocket * client = clients.value(socket); + + while(!client->atEnd()) { + QString data(client->readLine()); + + data = data.trimmed(); + //qDebug() << data; + QStringList lines = data.split("\n"); + + for (int i = 0; i < lines.length(); i++) { + handleNewInputLine(client, lines.at(i)); + } + } +/* + QString data(client->readAll()); + + data = data.trimmed(); + qDebug() << data; + QStringList lines = data.split("\n"); + + for (int i = 0; i < lines.length(); i++) { + handleNewInputLine(client, lines.at(i)); + } + */ +} + +void LogReceiver::handleNewInputLine(QLocalSocket * client, QString data) { + + QString logMsg(data); + QString interface = logMsg.section(";", 0, 0); + QString s_state = logMsg.section(";", 1, 1); + QString s_subState = logMsg.section(";", 2, 2); + QString msg = logMsg.section(";", 3, 3); + int st = s_state.trimmed().toInt(); + int sst = s_subState.trimmed().toInt(); + //qDebug() << logMsg; + + if (ifNameToClient.size() < numberOfProcesses && !ifNameToClient.contains( + interface)) { + ifNameToClient.insert(interface, client); + } + + switch (st) { + case LOG_INFO: + switch (sst) { + case DHCP_DISCOVER: + emit changeProgressBarValue(interface, 10); + break; + case DHCP_OFFER: + emit changeProgressBarValue(interface, 20); + break; + case DHCP_REQUEST: + emit changeProgressBarValue(interface, 30); + break; + case DHCP_ACK: + emit changeProgressBarValue(interface, 40); + break; + case DHCP_NAK: + emit changeProgressBarValue(interface, 40); + break; + case DHCPCD_ARP_TEST: + emit changeProgressBarValue(interface, 50); + break; + case DHCP_DECLINE: + emit changeProgressBarValue(interface, 60); + break; + case DHCP_RELEASE: + + break; + case DHCP_INFORM: + break; + case DHCPCD_CONFIGURE: + emit changeProgressBarValue(interface, 70); + break; + case DHCPCD_WRITE: + emit changeProgressBarValue(interface, 80); + break; + case DHCPCD_EXIT: + //emit changeProgressBarValue(interface, 100); + break; + case DHCPCD_LOG: + + default: + break; + } + break; + case LOG_ERR: + qDebug() << "received stat_error"; + break; + default: + //qDebug() << logMsg; + break; + } +} + +void LogReceiver::handleProcessFinished(int exitCode, + QProcess::ExitStatus exitStatus) { + QProcess* p = qobject_cast (QObject::sender()); + QString ifName = clientProcessToIfNameMap.value(p, "ifName"); + + if (ifName.compare("ifName") == 0) { + qDebug() + << "--- \t [LogReceiver::handleProcessFinished] haven't found process!"; + } else { + qDebug() << "process for interface" << ifName << "finished" << exitCode + << exitStatus; + if (exitCode > 0) { + qDebug() << "process exited unexpected"; + emit updateStatusLabel(ifName, "process exited unexpected"); + } else { + qDebug() << "process normal exit"; + qDebug() << "check internet connction"; + emit + changeProgressBarValue(ifName, 100); + emit + updateStatusLabel(ifName, "check connectivity"); + //checkInternetConnection(ifName); + checkInternetConnectionViaTCP(ifName); + } + } + QLocalSocket *client = ifNameToClient.value(ifName, 0); + if(client != 0) { + handleNewInput(client); + } + numberOfProcesses = numberOfProcesses - 1; + if (numberOfProcesses <= 0) { + emit allProcessesFinished(); + } +} + +void LogReceiver::handleProcessStarted() { + QProcess* p = qobject_cast (QObject::sender()); + QString ifName = clientProcessToIfNameMap.value(p, "ifName"); + qDebug() << "process started for interface:" << ifName; +} + +bool LogReceiver::checkBlackList(QString i) { + if (i.startsWith("v", Qt::CaseInsensitive)) { + return true; + } else if (i.startsWith("d", Qt::CaseInsensitive)) { + return true; + } else { + return false; + } +} diff --git a/LogReceiver/logreceiver.h b/LogReceiver/logreceiver.h new file mode 100644 index 0000000..ce6d070 --- /dev/null +++ b/LogReceiver/logreceiver.h @@ -0,0 +1,74 @@ +#ifndef LOGRECEIVER_H +#define LOGRECEIVER_H + +#include +#include +#include +#include +#include +#include +#include + +class QLocalServer; +class QLocalSocket; + +class LogReceiver: public QObject { +Q_OBJECT + +#define DEFAULT_QTSOCKETADDRESS "/var/tmp/qt_c_socket_default" +#define DEFAULT_PATHTODHCPCDEXE "/home/niklas/fbgui/workspace/customdhcpcd/src/customdhcpcd" + +public: + LogReceiver(); + ~LogReceiver(); + + void initAndRun(QString serverPath = DEFAULT_QTSOCKETADDRESS, + QString pathToExe = DEFAULT_PATHTODHCPCDEXE, + QStringList* args = NULL); + +private slots: + void handleNewConnection(); + void handleNewInput(); + void handleNewInputLine(QLocalSocket * client, QString data); + void handleProcessFinished(int exitCode, QProcess::ExitStatus exitStatus); + void handleProcessStarted(); + void handleClientDisconnect(); + +signals: + void addNewInterface(QString ifName); + void changeProgressBarValue(QString ifName, int newValue); + void connectionEstablished(QString ifName); + void abortBoot(QString msg); + void updateStatusLabel(QString ifName, QString status); + void allProcessesFinished(); + +private: + QLocalServer *server; + quint16 blockSize; + //QMap interfacesMap; + //QMap indexToIfaceNameMap; + QMap clients; + QLocalSocket * client; + QMap ifNameToClient; + QMap clientProcessToIfNameMap; + QString pathToDhcpcdExe; + QStringList dhcpcdArguments; + QNetworkConfigurationManager configurationManager; + QNetworkAccessManager *accessManager; + int numberOfProcesses; + + void handleNewInput(QLocalSocket * client); + + void runDHCPCD(QList &interfaces); + void runDHCPCD(QString interface); + QListcheckCarrierState(QList &interfaces); + bool checkCarrierState(QString interface); + void checkInternetConnection(QString ifName); + void checkInternetConnection(QList &interfaces); + void checkInternetConnectionViaTCP(QString ifName); + QList getListOfNetworkInterfaces(); + bool checkBlackList(QString i); + +}; + +#endif // LOGRECEIVER_H diff --git a/LogReceiver/logreceiver.ui b/LogReceiver/logreceiver.ui new file mode 100644 index 0000000..0010c64 --- /dev/null +++ b/LogReceiver/logreceiver.ui @@ -0,0 +1,25 @@ + + + LogReceiverClass + + + + 0 + 0 + 572 + 385 + + + + LogReceiver + + + #LogReceiverClass{ +background:grey; +} + + + + + + diff --git a/LogReceiver/main.cpp b/LogReceiver/main.cpp new file mode 100644 index 0000000..a8cd960 --- /dev/null +++ b/LogReceiver/main.cpp @@ -0,0 +1,12 @@ +#include "ndgui.h" + +#include +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + ndgui w; + w.show(); + return a.exec(); +} diff --git a/LogReceiver/ndgui.cpp b/LogReceiver/ndgui.cpp new file mode 100644 index 0000000..a839aef --- /dev/null +++ b/LogReceiver/ndgui.cpp @@ -0,0 +1,184 @@ +#include "ndgui.h" +#include "chooseinterfacedialog.h" +#include "abortbootdialog.h" + +ndgui::ndgui(QWidget *parent) + : QWidget(parent) +{ + ui.setupUi(this); + + connect(&logReceiver, SIGNAL(addNewInterface(QString)), this, SLOT(addNewInterface(QString))); + connect(&logReceiver, SIGNAL(changeProgressBarValue(QString , int )), this, SLOT(handleProgress(QString, int))); + connect(&logReceiver, SIGNAL(connectionEstablished(QString)), this, SLOT(handleConnectionEstablished(QString))); + connect(&logReceiver, SIGNAL(abortBoot(QString)), this, SLOT(handleAbortBoot(QString))); + connect(&logReceiver, SIGNAL(updateStatusLabel(QString,QString)), this, SLOT(handleUpdateStatusLabel(QString, QString))); + connect(&logReceiver, SIGNAL(allProcessesFinished()), this, SLOT(handleAllProcessesFinished())); + + buildGui(); + + logReceiver.initAndRun("/var/tmp/qt_c_socket_custom"); + numberOfInterfaces = 0; + + + setWindowTitle(tr("NetD")); +} + +ndgui::~ndgui() +{ + +} + +void ndgui::buildGui() { + + ndStatusLabel = new QLabel(tr("test")); + ndStatusLabel->setSizePolicy(QSizePolicy::Expanding, + QSizePolicy::Expanding); + ndStatusLabel->setAlignment(Qt::AlignCenter); + ndStatusLabel->setMinimumSize(100, 20); + + // create interface group box + createInterfaceGroupBox(); + + + + mainLayout = new QVBoxLayout; + mainLayout->addWidget(ndStatusLabel); + mainLayout->addWidget(interfaceGroupBox); + + setLayout(mainLayout); +} + +void ndgui::createInterfaceGroupBox(){ + interfaceGroupBox = new QGroupBox(tr("Interfaces")); + + interfaceGroupBoxLayout = new QVBoxLayout; + /* add interfaces via addInterfacesToGroupBox()*/ + + interfaceGroupBox->setLayout(interfaceGroupBoxLayout); +} + +void ndgui::addNewInterface(QString ifName) { + qDebug() << "receive interface to add:" << ifName; + QHBoxLayout *hBoxLayout = new QHBoxLayout; + QLabel *label = new QLabel(ifName); + QLabel *labelStatus = new QLabel("waiting"); + QProgressBar *pBar = new QProgressBar(this); + pBar->setRange(1, 100); + pBar->setMaximumSize(200, 20); + + statusLabels.insert(ifName, labelStatus); + progressBars.insert(ifName, pBar); + + hBoxLayout->addWidget(label, Qt::AlignLeft); + hBoxLayout->addWidget(labelStatus, Qt::AlignCenter); + hBoxLayout->addWidget(pBar, Qt::AlignRight); + + numberOfInterfaces++; + + interfaceGroupBoxLayout->addLayout(hBoxLayout, 2); +} + +void ndgui::handleProgress(QString ifName, int newValue) { + qDebug() << "<[---]> SLOT handleProgress activated with: " << ifName << newValue; + QProgressBar * pBar = progressBars.value(ifName); + if(newValue >= pBar->value()) { + pBar->setValue(newValue); + } + else { + qDebug() << "Error: new value is smaller than the old value!"; + } +} + +void ndgui::handleConnectionEstablished(QString ifName) { + finalUsableInterfaces.append(ifName); +} + +void ndgui::handleAbortBoot(QString msg) { + qDebug() << "abort boot. reason:" << msg; + showAbortBootDialog(); +} + +void ndgui::handleUpdateStatusLabel(QString ifName, QString status) { + QLabel* label = statusLabels.value(ifName); + label->setText(status); +} + +void ndgui::handleAllProcessesFinished() { + qDebug() << "all Processes finished"; + + if (finalUsableInterfaces.size() > 0) { + showChooseInterfaceDialog(); + } else { + showAbortBootDialog(); + } +} + +void ndgui::showAbortBootDialog() { + aBD = new AbortBootDialog(this); + connect(aBD, SIGNAL(showLogSignal()), this, SLOT(showLog())); + connect(aBD, SIGNAL(restartSignal()), this, SLOT(restartSystem())); + connect(aBD, SIGNAL(shutDownSignal()), this, SLOT(shutDownSystem())); + aBD->setModal(true); + aBD->show(); +} + +void ndgui::showChooseInterfaceDialog() { + cID = new ChooseInterfaceDialog(finalUsableInterfaces, this); + connect(cID, SIGNAL(continueSignal(QString)), this, + SLOT(continueBoot(QString))); + connect(cID, SIGNAL(restartSignal()), this, SLOT(restartSystem())); + connect(cID, SIGNAL(shutDownSignal()), this, SLOT(shutDownSystem())); + cID->setModal(true); + cID->show(); +} + +void ndgui::restartSystem() +{ + + if(qobject_cast(QObject::sender())>0) + { + qDebug() << "received Signal restart abd"; + aBD->closeDialog(); + } + else if(qobject_cast(QObject::sender())>0) + { + qDebug() << "received Signal restart cid"; + cID->close(); + } + else + { + qDebug() << "unknown sender" << QObject::sender(); + } + + +} + +void ndgui::shutDownSystem() +{ + if(qobject_cast(QObject::sender())>0) + { + + aBD->closeDialog(); + } + else if(qobject_cast(QObject::sender())>0) + { + + cID->close(); + } + else + { + qDebug() << "unknown sender" << QObject::sender(); + } + +} + +void ndgui::continueBoot(QString ifName) +{ + QString text = "continue with interface: " + ifName; + cID->close(); +} + +void ndgui::showLog() +{ + qDebug() << "show log"; +} diff --git a/LogReceiver/ndgui.h b/LogReceiver/ndgui.h new file mode 100644 index 0000000..e8b0b85 --- /dev/null +++ b/LogReceiver/ndgui.h @@ -0,0 +1,67 @@ +#ifndef NDGUI_H +#define NDGUI_H + +#include +#include "qprogressbar.h" +#include "qlabel.h" +#include "qgroupbox.h" +#include "qboxlayout.h" +#include "ui_ndgui.h" +#include "logreceiver.h" +#include "chooseinterfacedialog.h" +#include "abortbootdialog.h" + +class ndgui: public QWidget { +Q_OBJECT + +public: + ndgui(QWidget *parent = 0); + ~ndgui(); + +public slots: + void handleProgress(QString ifName, int newValue); + void addNewInterface(QString ifName); + void handleConnectionEstablished(QString ifName); + void handleAbortBoot(QString msg); + void handleUpdateStatusLabel(QString ifName, QString status); + void handleAllProcessesFinished(); + + void restartSystem(); + void shutDownSystem(); + void continueBoot(QString ifName); + void showLog(); + + void showAbortBootDialog(); + void showChooseInterfaceDialog(); + +private: + Ui::ndguiClass ui; + + LogReceiver logReceiver; + + QStringList finalUsableInterfaces; + + int numberOfInterfaces; + + ChooseInterfaceDialog *cID; + AbortBootDialog *aBD; + + /*gui elements*/ + QMap progressBars; + QMap statusLabels; + QLabel *ndStatusLabel; + QGroupBox *interfaceGroupBox; + QVBoxLayout *mainLayout; + QVBoxLayout *interfaceGroupBoxLayout; + /**/ + + + + + /*gui functions*/ + void buildGui(); + void createInterfaceGroupBox(); + +}; + +#endif // NDGUI_H diff --git a/LogReceiver/ndgui.ui b/LogReceiver/ndgui.ui new file mode 100644 index 0000000..4a4c466 --- /dev/null +++ b/LogReceiver/ndgui.ui @@ -0,0 +1,19 @@ + + ndguiClass + + + + 0 + 0 + 400 + 300 + + + + ndgui + + + + + + diff --git a/customdhcpcd/Debug/makefile b/customdhcpcd/Debug/makefile new file mode 100644 index 0000000..0c6307d --- /dev/null +++ b/customdhcpcd/Debug/makefile @@ -0,0 +1,44 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +-include ../makefile.init + +RM := rm -rf + +# All of the sources participating in the build are defined here +-include sources.mk +-include subdir.mk +-include src/subdir.mk +-include objects.mk + +ifneq ($(MAKECMDGOALS),clean) +ifneq ($(strip $(C_DEPS)),) +-include $(C_DEPS) +endif +endif + +-include ../makefile.defs + +# Add inputs and outputs from these tool invocations to the build variables + +# All Target +all: customdhcpcd + +# Tool invocations +customdhcpcd: $(OBJS) $(USER_OBJS) + @echo 'Building target: $@' + @echo 'Invoking: GCC C Linker' + gcc -o"customdhcpcd" $(OBJS) $(USER_OBJS) $(LIBS) + @echo 'Finished building target: $@' + @echo ' ' + +# Other Targets +clean: + -$(RM) $(OBJS)$(C_DEPS)$(EXECUTABLES) customdhcpcd + -@echo ' ' + +.PHONY: all clean dependents +.SECONDARY: + +-include ../makefile.targets diff --git a/customdhcpcd/Debug/objects.mk b/customdhcpcd/Debug/objects.mk new file mode 100644 index 0000000..224ef68 --- /dev/null +++ b/customdhcpcd/Debug/objects.mk @@ -0,0 +1,7 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +USER_OBJS := + +LIBS := diff --git a/customdhcpcd/Debug/sources.mk b/customdhcpcd/Debug/sources.mk new file mode 100644 index 0000000..57cf3c3 --- /dev/null +++ b/customdhcpcd/Debug/sources.mk @@ -0,0 +1,17 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +O_SRCS := +C_SRCS := +S_UPPER_SRCS := +OBJ_SRCS := +ASM_SRCS := +OBJS := +C_DEPS := +EXECUTABLES := + +# Every subdirectory with source files must be described here +SUBDIRS := \ +src \ + diff --git a/customdhcpcd/Debug/src/arp.d b/customdhcpcd/Debug/src/arp.d new file mode 100644 index 0000000..e639f20 --- /dev/null +++ b/customdhcpcd/Debug/src/arp.d @@ -0,0 +1,21 @@ +src/arp.d src/arp.o: ../src/arp.c ../src/config.h ../src/common.h \ + ../src/arp.h ../src/interface.h ../src/logger.h ../src/signal.h \ + ../src/socket.h ../src/dhcp.h ../src/dhcpcd.h + +../src/config.h: + +../src/common.h: + +../src/arp.h: + +../src/interface.h: + +../src/logger.h: + +../src/signal.h: + +../src/socket.h: + +../src/dhcp.h: + +../src/dhcpcd.h: diff --git a/customdhcpcd/Debug/src/client.d b/customdhcpcd/Debug/src/client.d new file mode 100644 index 0000000..585c535 --- /dev/null +++ b/customdhcpcd/Debug/src/client.d @@ -0,0 +1,37 @@ +src/client.d src/client.o: ../src/client.c ../src/config.h \ + ../src/common.h ../src/arp.h ../src/interface.h ../src/client.h \ + ../src/dhcpcd.h ../src/configure.h ../src/dhcp.h ../src/info.h \ + ../src/ipv4ll.h ../src/logger.h ../src/signal.h ../src/socket.h \ + ../src/logwriter.h ../src/status.h ../src/duid.h + +../src/config.h: + +../src/common.h: + +../src/arp.h: + +../src/interface.h: + +../src/client.h: + +../src/dhcpcd.h: + +../src/configure.h: + +../src/dhcp.h: + +../src/info.h: + +../src/ipv4ll.h: + +../src/logger.h: + +../src/signal.h: + +../src/socket.h: + +../src/logwriter.h: + +../src/status.h: + +../src/duid.h: diff --git a/customdhcpcd/Debug/src/common.d b/customdhcpcd/Debug/src/common.d new file mode 100644 index 0000000..235fc5e --- /dev/null +++ b/customdhcpcd/Debug/src/common.d @@ -0,0 +1,6 @@ +src/common.d src/common.o: ../src/common.c ../src/common.h \ + ../src/logger.h + +../src/common.h: + +../src/logger.h: diff --git a/customdhcpcd/Debug/src/configure.d b/customdhcpcd/Debug/src/configure.d new file mode 100644 index 0000000..16416f6 --- /dev/null +++ b/customdhcpcd/Debug/src/configure.d @@ -0,0 +1,28 @@ +src/configure.d src/configure.o: ../src/configure.c ../src/config.h \ + ../src/common.h ../src/configure.h ../src/dhcpcd.h ../src/interface.h \ + ../src/dhcp.h ../src/info.h ../src/logger.h ../src/signal.h \ + ../src/socket.h ../src/status.h ../src/logwriter.h + +../src/config.h: + +../src/common.h: + +../src/configure.h: + +../src/dhcpcd.h: + +../src/interface.h: + +../src/dhcp.h: + +../src/info.h: + +../src/logger.h: + +../src/signal.h: + +../src/socket.h: + +../src/status.h: + +../src/logwriter.h: diff --git a/customdhcpcd/Debug/src/customdhcpcd.d b/customdhcpcd/Debug/src/customdhcpcd.d new file mode 100644 index 0000000..1dd84f9 --- /dev/null +++ b/customdhcpcd/Debug/src/customdhcpcd.d @@ -0,0 +1 @@ +src/customdhcpcd.d src/customdhcpcd.o: ../src/customdhcpcd.c diff --git a/customdhcpcd/Debug/src/dhcp.d b/customdhcpcd/Debug/src/dhcp.d new file mode 100644 index 0000000..82e54dd --- /dev/null +++ b/customdhcpcd/Debug/src/dhcp.d @@ -0,0 +1,17 @@ +src/dhcp.d src/dhcp.o: ../src/dhcp.c ../src/config.h ../src/common.h \ + ../src/dhcpcd.h ../src/dhcp.h ../src/interface.h ../src/logger.h \ + ../src/socket.h + +../src/config.h: + +../src/common.h: + +../src/dhcpcd.h: + +../src/dhcp.h: + +../src/interface.h: + +../src/logger.h: + +../src/socket.h: diff --git a/customdhcpcd/Debug/src/dhcpcd.d b/customdhcpcd/Debug/src/dhcpcd.d new file mode 100644 index 0000000..05c8d7e --- /dev/null +++ b/customdhcpcd/Debug/src/dhcpcd.d @@ -0,0 +1,26 @@ +src/dhcpcd.d src/dhcpcd.o: ../src/dhcpcd.c ../src/config.h \ + ../src/client.h ../src/dhcpcd.h ../src/common.h ../src/dhcp.h \ + ../src/interface.h ../src/logger.h ../src/socket.h ../src/version.h \ + ../src/logwriter.h ../src/status.h + +../src/config.h: + +../src/client.h: + +../src/dhcpcd.h: + +../src/common.h: + +../src/dhcp.h: + +../src/interface.h: + +../src/logger.h: + +../src/socket.h: + +../src/version.h: + +../src/logwriter.h: + +../src/status.h: diff --git a/customdhcpcd/Debug/src/discover.d b/customdhcpcd/Debug/src/discover.d new file mode 100644 index 0000000..9188993 --- /dev/null +++ b/customdhcpcd/Debug/src/discover.d @@ -0,0 +1 @@ +src/discover.d src/discover.o: ../src/discover.c diff --git a/customdhcpcd/Debug/src/duid.d b/customdhcpcd/Debug/src/duid.d new file mode 100644 index 0000000..1434db7 --- /dev/null +++ b/customdhcpcd/Debug/src/duid.d @@ -0,0 +1,12 @@ +src/duid.d src/duid.o: ../src/duid.c ../src/config.h ../src/common.h \ + ../src/duid.h ../src/interface.h ../src/logger.h + +../src/config.h: + +../src/common.h: + +../src/duid.h: + +../src/interface.h: + +../src/logger.h: diff --git a/customdhcpcd/Debug/src/info.d b/customdhcpcd/Debug/src/info.d new file mode 100644 index 0000000..0a3490d --- /dev/null +++ b/customdhcpcd/Debug/src/info.d @@ -0,0 +1,17 @@ +src/info.d src/info.o: ../src/info.c ../src/config.h ../src/common.h \ + ../src/dhcp.h ../src/dhcpcd.h ../src/interface.h ../src/logger.h \ + ../src/info.h + +../src/config.h: + +../src/common.h: + +../src/dhcp.h: + +../src/dhcpcd.h: + +../src/interface.h: + +../src/logger.h: + +../src/info.h: diff --git a/customdhcpcd/Debug/src/interface.d b/customdhcpcd/Debug/src/interface.d new file mode 100644 index 0000000..581c831 --- /dev/null +++ b/customdhcpcd/Debug/src/interface.d @@ -0,0 +1,15 @@ +src/interface.d src/interface.o: ../src/interface.c ../src/config.h \ + ../src/common.h ../src/dhcp.h ../src/dhcpcd.h ../src/interface.h \ + ../src/logger.h + +../src/config.h: + +../src/common.h: + +../src/dhcp.h: + +../src/dhcpcd.h: + +../src/interface.h: + +../src/logger.h: diff --git a/customdhcpcd/Debug/src/ipv4ll.d b/customdhcpcd/Debug/src/ipv4ll.d new file mode 100644 index 0000000..7d02ead --- /dev/null +++ b/customdhcpcd/Debug/src/ipv4ll.d @@ -0,0 +1,17 @@ +src/ipv4ll.d src/ipv4ll.o: ../src/ipv4ll.c ../src/config.h ../src/arp.h \ + ../src/interface.h ../src/ipv4ll.h ../src/dhcp.h ../src/dhcpcd.h \ + ../src/common.h + +../src/config.h: + +../src/arp.h: + +../src/interface.h: + +../src/ipv4ll.h: + +../src/dhcp.h: + +../src/dhcpcd.h: + +../src/common.h: diff --git a/customdhcpcd/Debug/src/logger.d b/customdhcpcd/Debug/src/logger.d new file mode 100644 index 0000000..bbf1cf8 --- /dev/null +++ b/customdhcpcd/Debug/src/logger.d @@ -0,0 +1,10 @@ +src/logger.d src/logger.o: ../src/logger.c ../src/common.h \ + ../src/logger.h ../src/logwriter.h ../src/dhcpcd.h + +../src/common.h: + +../src/logger.h: + +../src/logwriter.h: + +../src/dhcpcd.h: diff --git a/customdhcpcd/Debug/src/logwriter.d b/customdhcpcd/Debug/src/logwriter.d new file mode 100644 index 0000000..83dc4b5 --- /dev/null +++ b/customdhcpcd/Debug/src/logwriter.d @@ -0,0 +1,19 @@ +src/logwriter.d src/logwriter.o: ../src/logwriter.c ../src/common.h \ + ../src/dhcp.h ../src/dhcpcd.h ../src/interface.h ../src/config.h \ + ../src/logger.h ../src/logwriter.h ../src/status.h + +../src/common.h: + +../src/dhcp.h: + +../src/dhcpcd.h: + +../src/interface.h: + +../src/config.h: + +../src/logger.h: + +../src/logwriter.h: + +../src/status.h: diff --git a/customdhcpcd/Debug/src/signal.d b/customdhcpcd/Debug/src/signal.d new file mode 100644 index 0000000..0185fc3 --- /dev/null +++ b/customdhcpcd/Debug/src/signal.d @@ -0,0 +1,8 @@ +src/signal.d src/signal.o: ../src/signal.c ../src/common.h \ + ../src/logger.h ../src/signal.h + +../src/common.h: + +../src/logger.h: + +../src/signal.h: diff --git a/customdhcpcd/Debug/src/socket.d b/customdhcpcd/Debug/src/socket.d new file mode 100644 index 0000000..785c170 --- /dev/null +++ b/customdhcpcd/Debug/src/socket.d @@ -0,0 +1,17 @@ +src/socket.d src/socket.o: ../src/socket.c ../src/config.h ../src/dhcp.h \ + ../src/dhcpcd.h ../src/common.h ../src/interface.h ../src/logger.h \ + ../src/socket.h + +../src/config.h: + +../src/dhcp.h: + +../src/dhcpcd.h: + +../src/common.h: + +../src/interface.h: + +../src/logger.h: + +../src/socket.h: diff --git a/customdhcpcd/Debug/src/subdir.mk b/customdhcpcd/Debug/src/subdir.mk new file mode 100644 index 0000000..38890b3 --- /dev/null +++ b/customdhcpcd/Debug/src/subdir.mk @@ -0,0 +1,85 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +O_SRCS += \ +../src/arp.o \ +../src/client.o \ +../src/common.o \ +../src/configure.o \ +../src/dhcp.o \ +../src/dhcpcd.o \ +../src/duid.o \ +../src/info.o \ +../src/interface.o \ +../src/ipv4ll.o \ +../src/logger.o \ +../src/logwriter.o \ +../src/signal.o \ +../src/socket.o + +C_SRCS += \ +../src/arp.c \ +../src/client.c \ +../src/common.c \ +../src/configure.c \ +../src/customdhcpcd.c \ +../src/dhcp.c \ +../src/dhcpcd.c \ +../src/discover.c \ +../src/duid.c \ +../src/info.c \ +../src/interface.c \ +../src/ipv4ll.c \ +../src/logger.c \ +../src/logwriter.c \ +../src/signal.c \ +../src/socket.c + +OBJS += \ +./src/arp.o \ +./src/client.o \ +./src/common.o \ +./src/configure.o \ +./src/customdhcpcd.o \ +./src/dhcp.o \ +./src/dhcpcd.o \ +./src/discover.o \ +./src/duid.o \ +./src/info.o \ +./src/interface.o \ +./src/ipv4ll.o \ +./src/logger.o \ +./src/logwriter.o \ +./src/signal.o \ +./src/socket.o + +C_DEPS += \ +./src/arp.d \ +./src/client.d \ +./src/common.d \ +./src/configure.d \ +./src/customdhcpcd.d \ +./src/dhcp.d \ +./src/dhcpcd.d \ +./src/discover.d \ +./src/duid.d \ +./src/info.d \ +./src/interface.d \ +./src/ipv4ll.d \ +./src/logger.d \ +./src/logwriter.d \ +./src/signal.d \ +./src/socket.d + + +# Each subdirectory must supply rules for building sources it contributes +src/%.o: ../src/%.c + @echo 'Building file: $<' + @echo 'Invoking: GCC C Compiler' + gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + diff --git a/customdhcpcd/src/.gitignore b/customdhcpcd/src/.gitignore new file mode 100644 index 0000000..6eb1591 --- /dev/null +++ b/customdhcpcd/src/.gitignore @@ -0,0 +1,5 @@ +*.o +version.h +dhcpcd +dhcpcd.8 +dhcpcd-*.bz2 diff --git a/customdhcpcd/src/Makefile b/customdhcpcd/src/Makefile new file mode 100644 index 0000000..7f90db4 --- /dev/null +++ b/customdhcpcd/src/Makefile @@ -0,0 +1,34 @@ +# Makefile based on BSD's pmake. +# Our mk stubs also work with GNU make. +# Copyright 2008 Roy Marples + +PROG= customdhcpcd +SRCS= arp.c client.c common.c configure.c dhcp.c dhcpcd.c duid.c \ + info.c interface.c ipv4ll.c logger.c logwriter.c signal.c socket.c +MAN= dhcpcd.8 + +VERSION= 3.2.3 +CLEANFILES= version.h dhcpcd.8 + +BINDIR= ${PREFIX}/sbin + +.SUFFIXES: .in + +MK= mk +include ${MK}/os.mk +include ${MK}/cc.mk +include ${MK}/prog.mk + +# os.mk should define this, but heh +INFOD?= /var/db + +LDADD+= ${LIBRESOLV} ${LIBRT} +CFLAGS+= -g -Wall -DINFODIR=\"${INFOD}\" ${FORK} ${RC} + +# As version.h is generated by us, hardcode the depend correctly. +${SRCS}: version.h +version.h: + echo "#define VERSION \"${VERSION}\""> version.h + +.in: + sed 's:@PREFIX@:${PREFIX}:g; s:@INFODIR@:${INFOD}:g' $< > $@ diff --git a/customdhcpcd/src/README b/customdhcpcd/src/README new file mode 100644 index 0000000..9089ec6 --- /dev/null +++ b/customdhcpcd/src/README @@ -0,0 +1,45 @@ +dhcpcd-3 - DHCP client daemon +Copyright 2006-2008 Roy Marples + + +Installation +------------ +Edit config.h to match your building requirements. + +Take special note of ENABLE_DUID and unset it if the target media is +volatile, like say a LiveCD. + +Then just make; make install + +man dhcpcd for command line options + + +Notes +----- +If you're cross compiling you may need to send HAVE_FORK=yes or HAVE_FORK=no +to the make command to avoid to automatic test. + +We try and detect how to restart ntp and ypbind, you can override this with +HAVE_INIT=no or force one of these values +OPENRC (OpenRC as used by Gentoo (forked from baselayout)) +BSDRC (BSD RC system - /etc/rc.d/ntpd restart ) +SERVICE (RedHat service command - service ntpd restart) +SLACKRC (Slackware RC system - /etc/rc.d/rc.ntpd restart) +SYSV (SYSV style - /etc/init.d/ntpd restart) + +You can change the default dir where dhcpcd stores it's .info files with +INFODIR=/var/db + +We now default to using -std=c99. For 64-bit linux, this always works, but +for 32-bit linux it requires either gnu99 or a patch to asm/types.h. +Most distros patch linux headers so this should work fine. +linux-2.6.24 finally ships with a working 32-bit header. +If your linux headers are older, or your distro hasn't patched them you can +set CSTD=gnu99 to work around this. + + +ChangeLog +--------- +We no longer supply a ChangeLog. +However, you're more than welcome to read the git commit comments at +http://git.marples.name/?p=dhcpcd/.git;a=summary diff --git a/customdhcpcd/src/arp.c b/customdhcpcd/src/arp.c new file mode 100644 index 0000000..794850c --- /dev/null +++ b/customdhcpcd/src/arp.c @@ -0,0 +1,284 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#ifdef __linux__ +#include +#include +#endif +#include +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" +#include "common.h" +#include "arp.h" +#include "interface.h" +#include "logger.h" +#include "signal.h" +#include "socket.h" + +/* These are really for IPV4LL */ +#define NPROBES 3 +#define PROBE_INTERVAL 200 +#define NCLAIMS 2 +#define CLAIM_INTERVAL 200 + +/* Linux does not seem to define these handy macros */ +#ifndef ar_sha +#define ar_sha(ap) (((caddr_t) ((ap) + 1)) + 0) +#define ar_spa(ap) (((caddr_t) ((ap) + 1)) + (ap)->ar_hln) +#define ar_tha(ap) (((caddr_t) ((ap) + 1)) + (ap)->ar_hln + (ap)->ar_pln) +#define ar_tpa(ap) (((caddr_t) ((ap) + 1)) + 2 * (ap)->ar_hln + (ap)->ar_pln) +#endif + +#ifndef arphdr_len +#define arphdr_len2(ar_hln, ar_pln) (sizeof (struct arphdr) + \ + 2 * (ar_hln) + 2 * (ar_pln)) +#define arphdr_len(ap) (arphdr_len2 ((ap)->ar_hln, (ap)->ar_pln)) +#endif + +#ifdef ENABLE_ARP + +static int send_arp (const interface_t *iface, int op, struct in_addr sip, + const unsigned char *taddr, struct in_addr tip) +{ + struct arphdr *arp; + size_t arpsize = arphdr_len2 (iface->hwlen, sizeof (sip)); + caddr_t tha; + int retval; + + arp = xzalloc (arpsize); + arp->ar_hrd = htons (iface->family); + arp->ar_pro = htons (ETHERTYPE_IP); + arp->ar_hln = iface->hwlen; + arp->ar_pln = sizeof (sip); + arp->ar_op = htons (op); + memcpy (ar_sha (arp), iface->hwaddr, (size_t) arp->ar_hln); + memcpy (ar_spa (arp), &sip, (size_t) arp->ar_pln); + if (taddr) { + /* NetBSD can return NULL from ar_tha, which is probably wrong + * but we still need to deal with it */ + if (! (tha = ar_tha (arp))) { + free (arp); + errno = EINVAL; + return (-1); + } + memcpy (tha, taddr, (size_t) arp->ar_hln); + } + memcpy (ar_tpa (arp), &tip, (size_t) arp->ar_pln); + + retval = send_packet (iface, ETHERTYPE_ARP, + (unsigned char *) arp, arphdr_len (arp)); + free (arp); + return (retval); +} + +int arp_claim (interface_t *iface, struct in_addr address) +{ + struct arphdr *reply = NULL; + long timeout = 0; + unsigned char *buffer; + int retval = -1; + int nprobes = 0; + int nclaims = 0; + struct in_addr null_address; + struct pollfd fds[] = { + { -1, POLLIN, 0 }, + { -1, POLLIN, 0 } + }; + + if (! iface) + return (-1); + + if (! iface->arpable) { + logger (LOG_DEBUG, "interface `%s' is not ARPable", iface->name); + return (0); + } + + if (! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr)) && + ! IN_LINKLOCAL (ntohl (address.s_addr))) + logger (LOG_INFO, + "checking %s is available on attached networks", + inet_ntoa (address)); + + if (! open_socket (iface, ETHERTYPE_ARP)) + return (-1); + + fds[0].fd = signal_fd (); + fds[1].fd = iface->fd; + + memset (&null_address, 0, sizeof (null_address)); + + buffer = xmalloc (iface->buffer_length); + reply = xmalloc (iface->buffer_length); + + for (;;) { + size_t bufpos = 0; + size_t buflen = iface->buffer_length; + int bytes; + int s = 0; + struct timeval stopat; + struct timeval now; + + /* Only poll if we have a timeout */ + if (timeout > 0) { + s = poll (fds, 2, timeout); + if (s == -1) { + if (errno == EINTR) { + if (signal_exists (NULL) == -1) { + errno = 0; + continue; + } else + break; + } + + logger (LOG_ERR, "poll: `%s'", + strerror (errno)); + break; + } + } + + /* Timed out */ + if (s == 0) { + if (nprobes < NPROBES) { + nprobes ++; + timeout = PROBE_INTERVAL; + logger (LOG_DEBUG, "sending ARP probe #%d", + nprobes); + if (send_arp (iface, ARPOP_REQUEST, + null_address, NULL, + address) == -1) + break; + + /* IEEE1394 cannot set ARP target address + * according to RFC2734 */ + if (nprobes >= NPROBES && + iface->family == ARPHRD_IEEE1394) + nclaims = NCLAIMS; + } else if (nclaims < NCLAIMS) { + nclaims ++; + timeout = CLAIM_INTERVAL; + logger (LOG_DEBUG, "sending ARP claim #%d", + nclaims); + if (send_arp (iface, ARPOP_REQUEST, + address, iface->hwaddr, + address) == -1) + break; + } else { + /* No replies, so done */ + retval = 0; + break; + } + + /* Setup our stop time */ + if (get_time (&stopat) != 0) + break; + stopat.tv_usec += timeout; + + continue; + } + + /* We maybe ARP flooded, so check our time */ + if (get_time (&now) != 0) + break; + if (timercmp (&now, &stopat, >)) { + timeout = 0; + continue; + } + + if (! fds[1].revents & POLLIN) + continue; + + memset (buffer, 0, buflen); + do { + union { + unsigned char *c; + struct in_addr *a; + } rp; + union { + unsigned char *c; + struct ether_addr *a; + } rh; + + memset (reply, 0, iface->buffer_length); + if ((bytes = get_packet (iface, (unsigned char *) reply, + buffer, + &buflen, &bufpos)) == -1) + break; + + /* Only these types are recognised */ + if (reply->ar_op != htons (ARPOP_REPLY)) + continue; + + /* Protocol must be IP. */ + if (reply->ar_pro != htons (ETHERTYPE_IP)) + continue; + if (reply->ar_pln != sizeof (address)) + continue; + if ((unsigned) bytes < sizeof (reply) + + 2 * (4 + reply->ar_hln)) + continue; + + rp.c = (unsigned char *) ar_spa (reply); + rh.c = (unsigned char *) ar_sha (reply); + + /* Ensure the ARP reply is for the our address */ + if (rp.a->s_addr != address.s_addr) + continue; + + /* Some systems send a reply back from our hwaddress, + * which is wierd */ + if (reply->ar_hln == iface->hwlen && + memcmp (rh.c, iface->hwaddr, iface->hwlen) == 0) + continue; + + logger (LOG_ERR, "ARPOP_REPLY received from %s (%s)", + inet_ntoa (*rp.a), + hwaddr_ntoa (rh.c, (size_t) reply->ar_hln)); + retval = -1; + goto eexit; + } while (bufpos != 0); + } + +eexit: + close (iface->fd); + iface->fd = -1; + free (buffer); + free (reply); + return (retval); +} +#endif diff --git a/customdhcpcd/src/arp.h b/customdhcpcd/src/arp.h new file mode 100644 index 0000000..3b7e8ef --- /dev/null +++ b/customdhcpcd/src/arp.h @@ -0,0 +1,39 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef ARP_H +#define ARP_H + +#ifdef ENABLE_ARP +#include + +#include "interface.h" + +int arp_claim (interface_t *iface, struct in_addr address); +#endif + +#endif diff --git a/customdhcpcd/src/client.c b/customdhcpcd/src/client.c new file mode 100644 index 0000000..ba71ba4 --- /dev/null +++ b/customdhcpcd/src/client.c @@ -0,0 +1,1150 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#ifdef __linux__ +# include +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" +#include "common.h" +#ifdef ENABLE_ARP +# include "arp.h" +#endif +#include "client.h" +#include "configure.h" +#include "dhcp.h" +#include "dhcpcd.h" +#include "info.h" +#include "interface.h" +#ifdef ENABLE_IPV4LL +# include "ipv4ll.h" +#endif +#include "logger.h" +#include "signal.h" +#include "socket.h" + +#include "logwriter.h" +#include "status.h" + +#ifdef ENABLE_DUID +# include "duid.h" +#endif + +#ifdef ENABLE_INFO +# include "info.h" +#endif + +#ifdef THERE_IS_NO_FORK +# ifndef ENABLE_INFO + # error "Non MMU requires ENABLE_INFO to work" +# endif +#endif + +/* Some platforms don't define INFTIM */ +#ifndef INFTIM +# define INFTIM -1 +#endif + +/* This is out mini timeout. + * Basically we resend the last request every TIMEOUT_MINI seconds. */ +#define TIMEOUT_MINI 3 +/* Except for an infinite timeout. We keep adding TIMEOUT_MINI to + * ourself until TIMEOUT_MINI_INF is reached. */ +#define TIMEOUT_MINI_INF 60 + +#define STATE_INIT 0 +#define STATE_REQUESTING 1 +#define STATE_BOUND 2 +#define STATE_RENEWING 3 +#define STATE_REBINDING 4 +#define STATE_REBOOT 5 +#define STATE_RENEW_REQUESTED 6 +#define STATE_RELEASED 7 + +/* We should define a maximum for the NAK exponential backoff */ +#define NAKOFF_MAX 60 + +#define SOCKET_CLOSED 0 +#define SOCKET_OPEN 1 + +/* Indexes for pollfds */ +#define POLLFD_SIGNAL 0 +#define POLLFD_IFACE 1 + +typedef struct _state { + int *pidfd; + bool forked; + int state; + uint32_t xid; + dhcp_t *dhcp; + int socket; + interface_t *interface; + time_t start; + time_t last_sent; + time_t last_type; + long timeout; + time_t nakoff; + bool daemonised; + bool persistent; + unsigned char *buffer; + size_t buffer_len; + size_t buffer_pos; +} state_t; + +static pid_t daemonise (int *pidfd) +{ + pid_t pid; + sigset_t full; + sigset_t old; +#ifdef THERE_IS_NO_FORK + char **argv; + int i; +#endif + + sigfillset (&full); + sigprocmask (SIG_SETMASK, &full, &old); + +#ifndef THERE_IS_NO_FORK + logger (LOG_DEBUG, "forking to background"); + switch (pid = fork()) { + case -1: + logger (LOG_ERR, "fork: %s", strerror (errno)); + exit (EXIT_FAILURE); + /* NOT REACHED */ + case 0: + setsid (); + close_fds (); + break; + default: + /* Reset our signals as we're the parent about to exit. */ + signal_reset (); + break; + } +#else + logger (LOG_INFO, "forking to background"); + + /* We need to add --daemonise to our options */ + argv = xmalloc (sizeof (char *) * (dhcpcd_argc + 4)); + argv[0] = dhcpcd; + for (i = 1; i < dhcpcd_argc; i++) + argv[i] = dhcpcd_argv[i]; + argv[i] = (char *) "--daemonised"; + if (dhcpcd_skiproutes) { + argv[++i] = (char *) "--skiproutes"; + argv[++i] = dhcpcd_skiproutes; + } + argv[i + 1] = NULL; + + switch (pid = vfork ()) { + case -1: + logger (LOG_ERR, "vfork: %s", strerror (errno)); + _exit (EXIT_FAILURE); + case 0: + signal_reset (); + sigprocmask (SIG_SETMASK, &old, NULL); + execvp (dhcpcd, argv); + logger (LOG_ERR, "execl `%s': %s", dhcpcd, + strerror (errno)); + _exit (EXIT_FAILURE); + } + + free (argv); +#endif + + /* Done with the fd now */ + if (pid != 0) { + writepid (*pidfd, pid); + close (*pidfd); + *pidfd = -1; + + } + + sigprocmask (SIG_SETMASK, &old, NULL); + return (pid); +} + +#ifdef ENABLE_INFO +static bool get_old_lease (state_t *state, const options_t *options) +{ + interface_t *iface = state->interface; + dhcp_t *dhcp = state->dhcp; + struct timeval tv; + unsigned int offset = 0; + + if (! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr))) + logger (LOG_INFO, "trying to use old lease in `%s'", + iface->infofile); + if (! read_info (iface, dhcp)) + return (false); + + /* Vitaly important we remove the server information here */ + memset (&dhcp->serveraddress, 0, sizeof (dhcp->serveraddress)); + memset (dhcp->servername, 0, sizeof (dhcp->servername)); + +#ifdef ENABLE_ARP + /* Check that no-one is using the address */ + if ((options->dolastlease || + (IN_LINKLOCAL (ntohl (dhcp->address.s_addr)) && + (! options->doipv4ll || + arp_claim (iface, dhcp->address))))) + { + memset (&dhcp->address, 0, sizeof (dhcp->address)); + memset (&dhcp->netmask, 0, sizeof (dhcp->netmask)); + memset (&dhcp->broadcast, 0, sizeof (dhcp->broadcast)); + return (false); + } + + /* Ok, lets use this */ + if (IN_LINKLOCAL (dhcp->address.s_addr)) + return (true); +#endif + + /* Ensure that we can still use the lease */ + if (gettimeofday (&tv, NULL) == -1) { + logger (LOG_ERR, "gettimeofday: %s", strerror (errno)); + return (false); + } + + offset = tv.tv_sec - dhcp->leasedfrom; + if (dhcp->leasedfrom && + tv.tv_sec - dhcp->leasedfrom > dhcp->leasetime) + { + logger (LOG_ERR, "lease expired %u seconds ago", + offset + dhcp->leasetime); + return (false); + } + + if (dhcp->leasedfrom == 0) + offset = 0; + state->timeout = dhcp->renewaltime - offset; + iface->start_uptime = uptime (); + return (true); +} +#endif + +#ifdef THERE_IS_NO_FORK +static void remove_skiproutes (dhcp_t *dhcp, interface_t *iface) +{ + int i = -1; + route_t *route; + route_t *newroute; + + free_route (iface->previous_routes); + iface->previous_routes = NULL; + + NSTAILQ_FOREACH (route, dhcp->routes, entries) { + i++; + + /* Check that we did add this route or not */ + if (dhcpcd_skiproutes) { + char *sk = xstrdup (dhcpcd_skiproutes); + char *skp = sk; + char *token; + bool found = false; + + while ((token = strsep (&skp, ","))) { + if (isdigit (*token) && atoi (token) == i) { + found = true; + break; + } + } + free (sk); + if (found) + continue; + } + + if (! iface->previous_routes) { + iface->previous_routes = xmalloc (sizeof (*iface->previous_routes)); + STAILQ_INIT (iface->previous_routes); + } + + newroute = xmalloc (sizeof (*newroute)); + memcpy (newroute, route, sizeof (*newroute)); + STAILQ_INSERT_TAIL (iface->previous_routes, newroute, entries); + } + + /* We no longer need this argument */ + free (dhcpcd_skiproutes); + dhcpcd_skiproutes = NULL; +} +#endif + +static bool client_setup (state_t *state, const options_t *options) +{ + dhcp_t *dhcp = state->dhcp; + interface_t *iface = state->interface; + + state->state = STATE_INIT; + state->last_type = DHCP_DISCOVER; + state->nakoff = 1; + state->daemonised = options->daemonised; + state->persistent = options->persistent; + + if (options->request_address.s_addr == 0 && + (options->doinform || options->dorequest || options->daemonised)) + { +#ifdef ENABLE_INFO + if (! get_old_lease (state, options)) +#endif + { + free (dhcp); + return (false); + } + state->timeout = 0; + + if (! options->daemonised && + IN_LINKLOCAL (ntohl (dhcp->address.s_addr))) + { + logger (LOG_ERR, "cannot request a link local address"); + return (false); + } +#ifdef THERE_IS_NO_FORK + if (options->daemonised) { + state->state = STATE_BOUND; + state->timeout = dhcp->renewaltime; + iface->previous_address = dhcp->address; + iface->previous_netmask = dhcp->netmask; + remove_skiproutes (dhcp, iface); + } +#endif + + } else { + dhcp->address = options->request_address; + dhcp->netmask = options->request_netmask; + if (dhcp->netmask.s_addr == 0) + dhcp->netmask.s_addr = get_netmask (dhcp->address.s_addr); + dhcp->broadcast.s_addr = dhcp->address.s_addr | + ~dhcp->netmask.s_addr; + } + + /* Remove all existing addresses. + * After all, we ARE a DHCP client whose job it is to configure the + * interface. We only do this on start, so persistent addresses + * can be added afterwards by the user if needed. */ + if (! options->test && ! options->daemonised) { + if (! options->doinform) { + flush_addresses (iface->name); + } else { + /* The inform address HAS to be configured for it to + * work with most DHCP servers */ + if (options->doinform && + has_address (iface->name, dhcp->address) < 1) + { + add_address (iface->name, dhcp->address, + dhcp->netmask, dhcp->broadcast); + iface->previous_address = dhcp->address; + iface->previous_netmask = dhcp->netmask; + } + } + } + + if (*options->clientid) { + /* Attempt to see if the ClientID is a hardware address */ + iface->clientid_len = hwaddr_aton (NULL, options->clientid); + if (iface->clientid_len) { + iface->clientid = xmalloc (iface->clientid_len); + hwaddr_aton (iface->clientid, options->clientid); + } else { + /* Nope, so mark it as-is */ + iface->clientid_len = strlen (options->clientid) + 1; + iface->clientid = xmalloc (iface->clientid_len); + *iface->clientid = '\0'; + memcpy (iface->clientid + 1, + options->clientid, iface->clientid_len - 1); + } + } else { +#ifdef ENABLE_DUID + unsigned char *duid = NULL; + size_t duid_len = 0; + + if (options->doduid) { + duid = xmalloc (DUID_LEN); + duid_len = get_duid (duid, iface); + } + + if (duid_len > 0) { + logger (LOG_INFO, "DUID = %s", hwaddr_ntoa (duid, duid_len)); + + iface->clientid_len = duid_len + 5; + iface->clientid = xmalloc (iface->clientid_len); + *iface->clientid = 255; /* RFC 4361 */ + + /* IAID is 4 bytes, so if the iface name is 4 bytes use it */ + if (strlen (iface->name) == 4) { + memcpy (iface->clientid + 1, iface->name, 4); + } else { + /* Name isn't 4 bytes, so use the index */ + uint32_t ul = htonl (if_nametoindex (iface->name)); + memcpy (iface->clientid + 1, &ul, 4); + } + + memcpy (iface->clientid + 5, duid, duid_len); + free (duid); + } else { +#else + { +#endif + iface->clientid_len = iface->hwlen + 1; + iface->clientid = xmalloc (iface->clientid_len); + *iface->clientid = iface->family; + memcpy (iface->clientid + 1, iface->hwaddr, iface->hwlen); + } + } + + return (true); +} + +static bool do_socket (state_t *state, int mode) +{ + if (state->interface->fd >= 0) + close (state->interface->fd); +#ifdef __linux + if (mode == SOCKET_CLOSED && state->interface->listen_fd >= 0) { + close (state->interface->listen_fd); + state->interface->listen_fd = -1; + } +#endif + + state->interface->fd = -1; + if (mode == SOCKET_OPEN) + if (open_socket (state->interface, ETHERTYPE_IP) == -1) + return (false); + state->socket = mode; + return (true); +} + +static bool _send_message (state_t *state, int type, const options_t *options) +{ + ssize_t retval; + + state->last_type = type; + state->last_sent = uptime (); + logSendToQt(type); + retval = send_message (state->interface, state->dhcp, state->xid, + type, options); + return (retval == -1 ? false : true); +} + +static void drop_config (state_t *state, const options_t *options) +{ + if (! state->persistent) + configure (options, state->interface, state->dhcp, false); + + free_dhcp (state->dhcp); + memset (state->dhcp, 0, sizeof (*state->dhcp)); +} + +static int wait_for_packet (struct pollfd *fds, state_t *state, + const options_t *options) +{ + dhcp_t *dhcp = state->dhcp; + interface_t *iface = state->interface; + int timeout = 0; + int retval = 0; + + if (! (state->timeout > 0 || + (options->timeout == 0 && + (state->state != STATE_INIT || state->xid)))) { + /* We need to zero our signal fd, otherwise we will block + * trying to read a signal. */ + fds[POLLFD_SIGNAL].revents = 0; + return (0); + } + + fds[POLLFD_IFACE].fd = iface->fd; + + if ((options->timeout == 0 && state->xid) || + (dhcp->leasetime == (unsigned) -1 && + state->state == STATE_BOUND)) + { + logger (LOG_DEBUG, "waiting for infinity"); + while (retval == 0) { + if (iface->fd == -1) + retval = poll (fds, 1, INFTIM); + else { + /* Slow down our requests */ + if (timeout < TIMEOUT_MINI_INF) + timeout += TIMEOUT_MINI; + else if (timeout > TIMEOUT_MINI_INF) + timeout = TIMEOUT_MINI_INF; + + retval = poll (fds, 2, timeout * 1000); + if (retval == -1 && errno == EINTR) { + /* If interupted, continue as normal as + * the signal will be delivered down + * the pipe */ + retval = 0; + continue; + } + if (retval == 0) + _send_message (state, state->last_type, + options); + } + } + + return (retval); + } + + /* Resend our message if we're getting loads of packets. + * As we use BPF or LPF, we shouldn't hit this as much, but it's + * still nice to have. */ + if (iface->fd > -1 && uptime () - state->last_sent >= TIMEOUT_MINI) + _send_message (state, state->last_type, options); + + logger (LOG_DEBUG, "waiting for %ld seconds", + (unsigned long) state->timeout); + /* If we're waiting for a reply, then we re-send the last + * DHCP request periodically in-case of a bad line */ + retval = 0; + while (state->timeout > 0 && retval == 0) { + if (iface->fd == -1) + timeout = (int) state->timeout; + else { + timeout = TIMEOUT_MINI; + if (state->timeout < timeout) + timeout = (int) state->timeout; + } + timeout *= 1000; + state->start = uptime (); + retval = poll (fds, iface->fd == -1 ? 1 : 2, timeout); + state->timeout -= uptime () - state->start; + if (retval == -1 && errno == EINTR) { + /* If interupted, continue as normal as the signal + * will be delivered down the pipe */ + retval = 0; + continue; + } + if (retval == 0 && iface->fd != -1 && state->timeout > 0) + _send_message (state, state->last_type, options); + } + + return (retval); +} + +static bool handle_signal (int sig, state_t *state, const options_t *options) +{ + switch (sig) { + case SIGINT: + logger (LOG_INFO, "received SIGINT, stopping"); + return (false); + case SIGTERM: + logger (LOG_INFO, "received SIGTERM, stopping"); + return (false); + + case SIGALRM: + logger (LOG_INFO, "received SIGALRM, renewing lease"); + switch (state->state) { + case STATE_BOUND: + case STATE_RENEWING: + case STATE_REBINDING: + state->state = STATE_RENEW_REQUESTED; + break; + case STATE_RENEW_REQUESTED: + case STATE_REQUESTING: + case STATE_RELEASED: + state->state = STATE_INIT; + break; + } + state->timeout = 0; + state->xid = 0; + return (true); + + case SIGHUP: + if (state->state != STATE_BOUND && + state->state != STATE_RENEWING && + state->state != STATE_REBINDING) + { + logger (LOG_ERR, + "received SIGHUP, but we no have lease to release"); + return (false); + } + + logger (LOG_INFO, "received SIGHUP, releasing lease"); + if (! IN_LINKLOCAL (ntohl (state->dhcp->address.s_addr))) { + do_socket (state, SOCKET_OPEN); + state->xid = (uint32_t) random (); + if ((open_socket (state->interface, false)) >= 0) { + _send_message (state, DHCP_RELEASE, options); + } + do_socket (state, SOCKET_CLOSED); + } + unlink (state->interface->infofile); + return (false); + + default: + logger (LOG_ERR, + "received signal %d, but don't know what to do with it", + sig); + } + + return (false); +} + +static int handle_timeout (state_t *state, const options_t *options) +{ + dhcp_t *dhcp = state->dhcp; + interface_t *iface = state->interface; + + /* No NAK, so reset the backoff */ + state->nakoff = 1; + + if (state->state == STATE_INIT && state->xid != 0) { + if (iface->previous_address.s_addr != 0 && + ! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr)) && + ! options->doinform) + { + logger (LOG_ERR, "lost lease"); + if (! options->persistent) + drop_config (state, options); + } else if (! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr))) + logger (LOG_ERR, "timed out"); + + do_socket (state, SOCKET_CLOSED); + free_dhcp (dhcp); + memset (dhcp, 0, sizeof (*dhcp)); + +#ifdef ENABLE_INFO + if (! options->test && + (options->doipv4ll || options->dolastlease)) + { + errno = 0; + if (! get_old_lease (state, options)) + { + if (errno == EINTR) + return (0); + if (options->dolastlease) + return (-1); + free_dhcp (dhcp); + memset (dhcp, 0, sizeof (*dhcp)); + } else if (errno == EINTR) + return (0); + } +#endif + +#ifdef ENABLE_IPV4LL + if (! options->test && options->doipv4ll && + (! dhcp->address.s_addr || + (! IN_LINKLOCAL (ntohl (dhcp->address.s_addr)) && + ! options->dolastlease))) + { + logger (LOG_INFO, "probing for an IPV4LL address"); + free_dhcp (dhcp); + memset (dhcp, 0, sizeof (*dhcp)); + if (ipv4ll_get_address (iface, dhcp) == -1) { + if (! state->daemonised) + return (-1); + + /* start over */ + state->xid = 0; + return (0); + } + state->timeout = dhcp->renewaltime; + } +#endif + +#if defined (ENABLE_INFO) || defined (ENABLE_IPV4LL) + if (dhcp->address.s_addr) { + if (! state->daemonised && + IN_LINKLOCAL (ntohl (dhcp->address.s_addr))) + logger (LOG_WARNING, "using IPV4LL address %s", + inet_ntoa (dhcp->address)); + if (configure (options, iface, dhcp, true) == -1 && + ! state->daemonised) + return (-1); + + state->state = STATE_BOUND; + if (! state->daemonised && options->daemonise) { + switch (daemonise (state->pidfd)) { + case -1: + return (-1); + case 0: + state->daemonised = true; + return (0); + default: + state->persistent = true; + state->forked = true; + return (-1); + } + } + + state->timeout = dhcp->renewaltime; + state->xid = 0; + return (0); + } +#endif + + if (! state->daemonised) + return (-1); + } + + switch (state->state) { + case STATE_INIT: + state->xid = (uint32_t) random (); + do_socket (state, SOCKET_OPEN); + state->timeout = options->timeout; + iface->start_uptime = uptime (); + if (dhcp->address.s_addr == 0) { + if (! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr))) + logger (LOG_INFO, "broadcasting for a lease"); + _send_message (state, DHCP_DISCOVER, options); + } else if (options->doinform) { + logger (LOG_INFO, "broadcasting inform for %s", + inet_ntoa (dhcp->address)); + _send_message (state, DHCP_INFORM, options); + state->state = STATE_REQUESTING; + } else { + logger (LOG_INFO, "broadcasting for a lease of %s", + inet_ntoa (dhcp->address)); + _send_message (state, DHCP_REQUEST, options); + state->state = STATE_REQUESTING; + } + + break; + case STATE_BOUND: + case STATE_RENEW_REQUESTED: + if (IN_LINKLOCAL (ntohl (dhcp->address.s_addr))) { + memset (&dhcp->address, 0, sizeof (dhcp->address)); + state->state = STATE_INIT; + state->xid = 0; + break; + } + state->state = STATE_RENEWING; + state->xid = (uint32_t) random (); + /* FALLTHROUGH */ + case STATE_RENEWING: + iface->start_uptime = uptime (); + logger (LOG_INFO, "renewing lease of %s", inet_ntoa + (dhcp->address)); + do_socket (state, SOCKET_OPEN); + _send_message (state, DHCP_REQUEST, options); + state->timeout = dhcp->rebindtime - dhcp->renewaltime; + state->state = STATE_REBINDING; + break; + case STATE_REBINDING: + logger (LOG_ERR, "lost lease, attemping to rebind"); + memset (&dhcp->address, 0, sizeof (dhcp->address)); + do_socket (state, SOCKET_OPEN); + if (state->xid == 0) + state->xid = (uint32_t) random (); + dhcp->serveraddress.s_addr = 0; + _send_message (state, DHCP_REQUEST, options); + state->timeout = dhcp->leasetime - dhcp->rebindtime; + state->state = STATE_REQUESTING; + break; + case STATE_REQUESTING: + state->state = STATE_INIT; + do_socket (state, SOCKET_CLOSED); + state->timeout = 0; + break; + + case STATE_RELEASED: + dhcp->leasetime = 0; + break; + } + + return (0); +} + + +static int handle_dhcp (state_t *state, int type, const options_t *options) +{ + struct timespec ts; + interface_t *iface = state->interface; + dhcp_t *dhcp = state->dhcp; + + /* We should restart on a NAK */ + if (type == DHCP_NAK) { + logger (LOG_INFO, "received NAK: %s", dhcp->message); + logToQt(LOG_INFO, DHCP_NAK, ""); + state->state = STATE_INIT; + state->timeout = 0; + state->xid = 0; + free_dhcp (dhcp); + memset (dhcp, 0, sizeof (*dhcp)); + + /* If we constantly get NAKS then we should slowly back off */ + if (state->nakoff > 0) { + logger (LOG_DEBUG, "sleeping for %ld seconds", + (long) state->nakoff); + ts.tv_sec = state->nakoff; + ts.tv_nsec = 0; + state->nakoff *= 2; + if (state->nakoff > NAKOFF_MAX) + state->nakoff = NAKOFF_MAX; + nanosleep (&ts, NULL); + } + + return (0); + } + + /* No NAK, so reset the backoff */ + state->nakoff = 1; + + if (type == DHCP_OFFER && state->state == STATE_INIT) { + char *addr = strdup (inet_ntoa (dhcp->address)); + if (dhcp->servername[0]) + logger (LOG_INFO, "offered %s from %s `%s'", + addr, inet_ntoa (dhcp->serveraddress), + dhcp->servername); + else + logger (LOG_INFO, "offered %s from %s", + addr, inet_ntoa (dhcp->serveraddress)); + free (addr); + + logToQt(LOG_INFO, DHCP_OFFER, ""); + +#ifdef ENABLE_INFO + if (options->test) { + write_info (iface, dhcp, options, false); + errno = 0; + return (-1); + } +#endif + + _send_message (state, DHCP_REQUEST, options); + state->state = STATE_REQUESTING; + + return (0); + } + + if (type == DHCP_OFFER) { + logger (LOG_INFO, "got subsequent offer of %s, ignoring ", + inet_ntoa (dhcp->address)); + return (0); + } + + /* We should only be dealing with acks */ + if (type != DHCP_ACK) { + logger (LOG_ERR, "%d not an ACK or OFFER", type); + return (0); + } + + /* if we are here, than we received an ACK and can go on with configuration */ + logToQt(LOG_INFO, DHCP_ACK, ""); + + switch (state->state) { + case STATE_RENEW_REQUESTED: + case STATE_REQUESTING: + case STATE_RENEWING: + case STATE_REBINDING: + break; + default: + logger (LOG_ERR, "wrong state %d", state->state); + } + + do_socket (state, SOCKET_CLOSED); + +#ifdef ENABLE_ARP + if (options->doarp && iface->previous_address.s_addr != + dhcp->address.s_addr) + { + errno = 0; + logToQt(LOG_INFO, DHCPCD_ARP_TEST, ""); + if (arp_claim (iface, dhcp->address)) { + do_socket (state, SOCKET_OPEN); + _send_message (state, DHCP_DECLINE, options); + do_socket (state, SOCKET_CLOSED); + + free_dhcp (dhcp); + memset (dhcp, 0, sizeof (*dhcp)); + state->xid = 0; + state->timeout = 0; + state->state = STATE_INIT; + + /* RFC 2131 says that we should wait for 10 seconds + * before doing anything else */ + logger (LOG_INFO, "sleeping for 10 seconds"); + ts.tv_sec = 10; + ts.tv_nsec = 0; + nanosleep (&ts, NULL); + return (0); + } else if (errno == EINTR) + return (0); + } +#endif + + if (options->doinform) { + if (options->request_address.s_addr != 0) + dhcp->address = options->request_address; + else + dhcp->address = iface->previous_address; + + logger (LOG_INFO, "received approval for %s", + inet_ntoa (dhcp->address)); + if (iface->previous_netmask.s_addr != dhcp->netmask.s_addr) { + add_address (iface->name, dhcp->address, + dhcp->netmask, dhcp->broadcast); + iface->previous_netmask.s_addr = dhcp->netmask.s_addr; + } + state->timeout = options->leasetime; + if (state->timeout == 0) + state->timeout = DEFAULT_LEASETIME; + state->state = STATE_INIT; + } else if (dhcp->leasetime == (unsigned) -1) { + dhcp->renewaltime = dhcp->rebindtime = dhcp->leasetime; + state->timeout = 1; /* So we wait for infinity */ + logger (LOG_INFO, "leased %s for infinity", + inet_ntoa (dhcp->address)); + state->state = STATE_BOUND; + } else { + if (! dhcp->leasetime) { + dhcp->leasetime = DEFAULT_LEASETIME; + logger(LOG_INFO, + "no lease time supplied, assuming %d seconds", + dhcp->leasetime); + } + logger (LOG_INFO, "leased %s for %u seconds", + inet_ntoa (dhcp->address), dhcp->leasetime); + + if (dhcp->rebindtime >= dhcp->leasetime) { + dhcp->rebindtime = (dhcp->leasetime * 0.875); + logger (LOG_ERR, + "rebind time greater than lease " + "time, forcing to %u seconds", + dhcp->rebindtime); + } + + if (dhcp->renewaltime > dhcp->rebindtime) { + dhcp->renewaltime = (dhcp->leasetime * 0.5); + logger (LOG_ERR, + "renewal time greater than rebind time, " + "forcing to %u seconds", + dhcp->renewaltime); + } + + if (! dhcp->renewaltime) { + dhcp->renewaltime = (dhcp->leasetime * 0.5); + logger (LOG_INFO, + "no renewal time supplied, assuming %d seconds", + dhcp->renewaltime); + } else + logger (LOG_DEBUG, "renew in %u seconds", + dhcp->renewaltime); + + if (! dhcp->rebindtime) { + dhcp->rebindtime = (dhcp->leasetime * 0.875); + logger (LOG_INFO, + "no rebind time supplied, assuming %d seconds", + dhcp->rebindtime); + } else + logger (LOG_DEBUG, "rebind in %u seconds", + dhcp->rebindtime); + + state->timeout = dhcp->renewaltime; + state->state = STATE_BOUND; + } + + state->xid = 0; + + logToQt(LOG_INFO, DHCPCD_CONFIGURE, ""); + if (configure (options, iface, dhcp, true) == -1 && + ! state->daemonised) + return (-1); + + if (! state->daemonised && options->daemonise) { + switch (daemonise (state->pidfd)) { + case 0: + state->daemonised = true; + return (0); + case -1: + return (-1); + default: + state->persistent = true; + state->forked = true; + return (-1); + } + } + + return (0); +} + +static int handle_packet (state_t *state, const options_t *options) +{ + interface_t *iface = state->interface; + bool valid = false; + int type; + struct dhcp_t *new_dhcp; + dhcpmessage_t message; + + /* Allocate our buffer space for BPF. + * We cannot do this until we have opened our socket as we don't + * know how much of a buffer we need until then. */ + if (! state->buffer) + state->buffer = xmalloc (iface->buffer_length); + state->buffer_len = iface->buffer_length; + state->buffer_pos = 0; + + /* We loop through until our buffer is empty. + * The benefit is that if we get >1 DHCP packet in our buffer and + * the first one fails for any reason, we can use the next. */ + + memset (&message, 0, sizeof (message)); + new_dhcp = xmalloc (sizeof (*new_dhcp)); + + do { + if (get_packet (iface, (unsigned char *) &message, + state->buffer, + &state->buffer_len, &state->buffer_pos) == -1) + break; + + if (state->xid != message.xid) { + logger (LOG_DEBUG, + "ignoring packet with xid 0x%x as it's not ours (0x%x)", + message.xid, state->xid); + continue; + } + + logger (LOG_DEBUG, "got a packet with xid 0x%x", message.xid); + memset (new_dhcp, 0, sizeof (*new_dhcp)); + type = parse_dhcpmessage (new_dhcp, &message); + if (type == -1) { + logger (LOG_ERR, "failed to parse packet"); + free_dhcp (new_dhcp); + /* We don't abort on this, so return zero */ + return (0); + } + + /* If we got here then the DHCP packet is valid and appears to + * be for us, so let's clear the buffer as we don't care about + * any more DHCP packets at this point. */ + valid = true; + break; + } while (state->buffer_pos != 0); + + /* No packets for us, so wait until we get one */ + if (! valid) { + free (new_dhcp); + return (0); + } + + /* new_dhcp is now our master DHCP message */ + free_dhcp (state->dhcp); + free (state->dhcp); + state->dhcp = new_dhcp; + new_dhcp = NULL; + + return (handle_dhcp (state, type, options)); +} + +int dhcp_run (const options_t *options, int *pidfd) +{ + interface_t *iface; + state_t *state = NULL; + struct pollfd fds[] = { + { -1, POLLIN, 0 }, + { -1, POLLIN, 0 } + }; + int retval = -1; + int sig; + + if (! options) + return (-1); + + /*read_interface : defined in interface.c*/ + iface = read_interface (options->interface, options->metric); + if (! iface) + goto eexit; + + state = xzalloc (sizeof (*state)); + state->dhcp = xzalloc (sizeof (*state->dhcp)); + state->pidfd = pidfd; + state->interface = iface; + + if (! client_setup (state, options)) + goto eexit; + + if (signal_init () == -1) + goto eexit; + if (signal_setup () == -1) + goto eexit; + + fds[POLLFD_SIGNAL].fd = signal_fd (); + + for (;;) { + retval = wait_for_packet (fds, state, options); + + /* We should always handle our signals first */ + if ((sig = (signal_read (&fds[POLLFD_SIGNAL]))) != -1) { + if (handle_signal (sig, state, options)) + retval = 0; + else + retval = -1; + } else if (retval == 0) + retval = handle_timeout (state, options); + else if (retval > 0 && + state->socket != SOCKET_CLOSED && + fds[POLLFD_IFACE].revents & POLLIN) + retval = handle_packet (state, options); + else if (retval == -1 && errno == EINTR) { + /* The interupt will be handled above */ + retval = 0; + } else { + logger (LOG_ERR, "poll: %s", strerror (errno)); + retval = -1; + } + + if (retval != 0) + break; + } + +eexit: + if (iface) { + do_socket (state, SOCKET_CLOSED); + drop_config (state, options); + free_route (iface->previous_routes); + free (iface->clientid); + free (iface); + } + + if (state) { + if (state->forked) + retval = 0; + + if (state->daemonised) + unlink (options->pidfile); + + free_dhcp (state->dhcp); + free (state->dhcp); + free (state->buffer); + free (state); + } + + return (retval); +} diff --git a/customdhcpcd/src/client.h b/customdhcpcd/src/client.h new file mode 100644 index 0000000..fa6ea9b --- /dev/null +++ b/customdhcpcd/src/client.h @@ -0,0 +1,35 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef CLIENT_H +#define CLIENT_H + +#include "dhcpcd.h" + +int dhcp_run (const options_t *options, int *pidfd); + +#endif diff --git a/customdhcpcd/src/common.c b/customdhcpcd/src/common.c new file mode 100644 index 0000000..99471bc --- /dev/null +++ b/customdhcpcd/src/common.c @@ -0,0 +1,249 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "common.h" +#include "logger.h" + +/* Handy routine to read very long lines in text files. + * This means we read the whole line and avoid any nasty buffer overflows. */ +char *get_line (FILE *fp) +{ + char *line = NULL; + char *p; + size_t len = 0; + size_t last = 0; + + if (feof (fp)) + return (NULL); + + do { + len += BUFSIZ; + line = xrealloc (line, sizeof (char) * len); + p = line + last; + memset (p, 0, BUFSIZ); + fgets (p, BUFSIZ, fp); + last += strlen (p); + } while (! feof (fp) && line[last - 1] != '\n'); + + /* Trim the trailing newline */ + if (*line && line[--last] == '\n') + line[last] = '\0'; + + return (line); +} + +/* OK, this should be in dhcpcd.c + * It's here to make dhcpcd more readable */ +#ifndef HAVE_SRANDOMDEV +void srandomdev (void) +{ + int fd; + unsigned long seed; + + fd = open ("/dev/urandom", 0); + if (fd == -1 || read (fd, &seed, sizeof (seed)) == -1) { + logger (LOG_WARNING, "Could not read from /dev/urandom: %s", + strerror (errno)); + seed = time (0); + } + if (fd >= 0) + close(fd); + + srandom (seed); +} +#endif + +/* strlcpy is nice, shame glibc does not define it */ +#ifndef HAVE_STRLCPY +size_t strlcpy (char *dst, const char *src, size_t size) +{ + const char *s = src; + size_t n = size; + + if (n && --n) + do { + if (! (*dst++ = *src++)) + break; + } while (--n); + + if (! n) { + if (size) + *dst = '\0'; + while (*src++); + } + + return (src - s - 1); +} +#endif + +/* Close our fd's */ +int close_fds (void) +{ + int fd; + + if ((fd = open ("/dev/null", O_RDWR)) == -1) { + logger (LOG_ERR, "open `/dev/null': %s", strerror (errno)); + return (-1); + } + + dup2 (fd, fileno (stdin)); + dup2 (fd, fileno (stdout)); + dup2 (fd, fileno (stderr)); + if (fd > 2) + close (fd); + return (0); +} + +int close_on_exec (int fd) +{ + int flags; + + if ((flags = fcntl (fd, F_GETFD, 0)) == -1 + || fcntl (fd, F_SETFD, flags | FD_CLOEXEC) == -1) + { + logger (LOG_ERR, "fcntl: %s", strerror (errno)); + return (-1); + } + return (0); +} + +/* Handy function to get the time. + * We only care about time advancements, not the actual time itself + * Which is why we use CLOCK_MONOTONIC, but it is not available on all + * platforms. + */ +int get_time (struct timeval *tp) +{ +#if defined(_POSIX_MONOTONIC_CLOCK) && defined(CLOCK_MONOTONIC) + struct timespec ts; + static clockid_t posix_clock; + static int posix_clock_set = 0; + + if (! posix_clock_set) { + if (sysconf (_SC_MONOTONIC_CLOCK) >= 0) + posix_clock = CLOCK_MONOTONIC; + else + posix_clock = CLOCK_REALTIME; + posix_clock_set = 1; + } + + if (clock_gettime (posix_clock, &ts) == -1) { + logger (LOG_ERR, "clock_gettime: %s", strerror (errno)); + return (-1); + } + + tp->tv_sec = ts.tv_sec; + tp->tv_usec = ts.tv_nsec / 1000; + return (0); +#else + if (gettimeofday (tp, NULL) == -1) { + logger (LOG_ERR, "gettimeofday: %s", strerror (errno)); + return (-1); + } + return (0); +#endif +} + +time_t uptime (void) +{ + struct timeval tp; + + if (get_time (&tp) == -1) + return (-1); + + return (tp.tv_sec); +} + +void writepid (int fd, pid_t pid) +{ + char spid[16]; + if (ftruncate (fd, (off_t) 0) == -1) { + logger (LOG_ERR, "ftruncate: %s", strerror (errno)); + } else { + ssize_t len; + snprintf (spid, sizeof (spid), "%u", pid); + len = pwrite (fd, spid, strlen (spid), (off_t) 0); + if (len != (ssize_t) strlen (spid)) + logger (LOG_ERR, "pwrite: %s", strerror (errno)); + } +} + +void *xmalloc (size_t s) +{ + void *value = malloc (s); + + if (value) + return (value); + + logger (LOG_ERR, "memory exhausted"); + + exit (EXIT_FAILURE); + /* NOTREACHED */ +} + +void *xzalloc (size_t s) +{ + void *value = xmalloc (s); + memset (value, 0, s); + return (value); +} + +void *xrealloc (void *ptr, size_t s) +{ + void *value = realloc (ptr, s); + + if (value) + return (value); + + logger (LOG_ERR, "memory exhausted"); + exit (EXIT_FAILURE); + /* NOTREACHED */ +} + +char *xstrdup (const char *str) +{ + char *value; + + if (! str) + return (NULL); + + if ((value = strdup (str))) + return (value); + + logger (LOG_ERR, "memory exhausted"); + exit (EXIT_FAILURE); + /* NOTREACHED */ +} diff --git a/customdhcpcd/src/common.h b/customdhcpcd/src/common.h new file mode 100644 index 0000000..46f1886 --- /dev/null +++ b/customdhcpcd/src/common.h @@ -0,0 +1,68 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef COMMON_H +#define COMMON_H + +/* string.h pulls in features.h so the below define checks work */ +#include +#include +#include + +#if __GNUC__ > 2 || defined(__INTEL_COMPILER) +# define _unused __attribute__((__unused__)) +#else +# define _unused +#endif + +#define HAVE_STRLCPY +/* Only GLIBC doesn't support strlcpy */ +#ifdef __GLIBC__ +# if ! defined(__UCLIBC__) && ! defined (__dietlibc__) +# undef HAVE_STRLCPY +size_t strlcpy (char *dst, const char *src, size_t size); +# endif +#endif + +#define HAVE_SRANDOMDEV +#if defined(__GLIBC__) || defined(__NetBSD__) +# undef HAVE_SRANDOMDEV +void srandomdev (void); +#endif + +int close_fds (void); +int close_on_exec (int fd); +char *get_line (FILE *fp); +int get_time (struct timeval *tp); +time_t uptime (void); +void writepid (int fd, pid_t pid); +void *xrealloc (void *ptr, size_t size); +void *xmalloc (size_t size); +void *xzalloc (size_t size); +char *xstrdup (const char *str); + +#endif diff --git a/customdhcpcd/src/config.h b/customdhcpcd/src/config.h new file mode 100644 index 0000000..2c0991b --- /dev/null +++ b/customdhcpcd/src/config.h @@ -0,0 +1,133 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef CONFIG_H +#define CONFIG_H + +/* You can enable/disable various chunks of optional code here. + * You would only do this to try and shrink the end binary if dhcpcd + * was running on a low memory device */ + +#define ENABLE_ARP +#define ENABLE_NTP +#define ENABLE_NIS +#define ENABLE_INFO +/* Define this to enable some compatability with 1.x and 2.x info files */ +/* #define ENABLE_INFO_COMPAT */ + +/* IPV4LL, aka ZeroConf, aka APIPA, aka RFC 3927. + * Needs ARP. */ +#define ENABLE_IPV4LL + +/* We will auto create a DUID_LLT file if it doesn't exist. + * You can always create your own DUID file that just contains the + * hex string that represents the DUID. + * See RFC 3315 for details on this. */ +#define ENABLE_DUID + +/* resolvconf is framework for multiple interfaces to manage resolv.conf */ +#define ENABLE_RESOLVCONF + +/* Some systems do not have a working fork. + * The Makefile will attempt to work it out, but if it fails to feel free to + * define it here. */ +/* #define THERE_IS_NO_FORK */ + +/* Packname name and pathname definitions. */ + +#define PACKAGE "dhcpcd" + +#define ETCDIR "/etc" +#define RESOLVFILE ETCDIR "/resolv.conf" + +#define NISFILE ETCDIR "/yp.conf" + +#define NTPFILE ETCDIR "/ntp.conf" +#define NTPDRIFTFILE ETCDIR "/ntp.drift" +#define NTPLOGFILE "/var/log/ntp.log" + +#define OPENNTPFILE ETCDIR "/ntpd.conf" + +#define DEFAULT_SCRIPT ETCDIR "/" PACKAGE ".sh" + +#define STATEDIR "/var" +#define PIDFILE STATEDIR "/run/" PACKAGE "-%s.pid" + +#ifndef INFODIR +# define INFODIR "/var/lib/dhcpcd" +#endif +#define INFOFILE INFODIR "/" PACKAGE "-%s.info" +#define DUIDFILE INFODIR "/" PACKAGE ".duid" + +/* OPENRC is Open Run Control, forked from Gentoo's baselayout package + * BSDRC is BSD style Run Control + * SLACKRC is Slackware Run Control + * SERVICE is RedHat service command + * SYSV should cover everthing else */ +#ifdef ENABLE_OPENRC +# define SERVICE "OPENRC" +# define NISSERVICE ETCDIR "/init.d/ypbind" +# define NISRESTARTARGS "--nodeps", "--quiet", "conditionalrestart" +# define NTPSERVICE ETCDIR "/init.d/ntpd" +# define NTPRESTARTARGS "--nodeps", "--quiet", "conditionalrestart" +#endif +#if ENABLE_BSDRC +# define SERVICE "BSDRC" +# define NISSERVICE ETCDIR "/rc.d/ypbind" +# define NISRESTARTARGS "restart" +# define NTPSERVICE ETCDIR "/rc.d/ntpd" +# define NTPRESTARTARGS "restart" +#endif +#if ENABLE_SLACKRC +# define SERVICE "SLACKRC" +# define NISSERVICE ETCDIR "/rc.d/rc.ypbind" +# define NISRESTARTARGS "restart" +# define NTPSERVICE ETCDIR "/rc.d/rc.ntpd" +# define NTPRESTARTARGS "restart" +#endif +#if ENABLE_SERVICE +# define SERVICE "SERVICE" +# define NISSERVICE "service" +# define NISRESTARTARGS "ypbind", "restart" +# define NTPSERVICE "service" +# define NTPRESTARTARGS "ntpd", "restart" +#endif +#if ENABLE_SYSV +# define SERVICE "SYSV" +# define NISSERVICE ETCDIR "/init.d/ypbind" +# define NISRESTARTARGS "restart" +# define NTPSERVICE ETCDIR "/init.d/ntpd" +# define NTPRESTARTARGS "restart" +#endif + +#ifndef NISSERVICE +# undef ENABLE_NIS +#endif +#ifndef NTPSERVICE +# undef ENABLE_NTP +#endif + +#endif diff --git a/customdhcpcd/src/configure.c b/customdhcpcd/src/configure.c new file mode 100644 index 0000000..91e3c9f --- /dev/null +++ b/customdhcpcd/src/configure.c @@ -0,0 +1,813 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include + +#include + +#include +#ifdef __linux__ +# include +#endif +#include +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" +#include "common.h" +#include "configure.h" +#include "dhcp.h" +#ifdef ENABLE_INFO +# include "info.h" +#endif +#include "interface.h" +#include "dhcpcd.h" +#include "logger.h" +#include "signal.h" +#include "socket.h" + +#include "status.h" +#include "logwriter.h" + +static int file_in_path (const char *file) +{ + char *p = getenv ("PATH"); + char *path; + char *token; + struct stat s; + char mypath[PATH_MAX]; + int retval = -1; + + if (! p) { + errno = ENOENT; + return (-1); + } + + path = strdup (p); + p = path; + while ((token = strsep (&p, ":"))) { + snprintf (mypath, PATH_MAX, "%s/%s", token, file); + if (stat (mypath, &s) == 0) { + retval = 0; + break; + } + } + free (path); + return (retval); +} + +/* IMPORTANT: Ensure that the last parameter is NULL when calling */ +static int exec_cmd (const char *cmd, const char *args, ...) +{ + va_list va; + char **argv; + int n = 1; + int ret = 0; + pid_t pid; + sigset_t full; + sigset_t old; + + va_start (va, args); + while (va_arg (va, char *) != NULL) + n++; + va_end (va); + argv = xmalloc (sizeof (char *) * (n + 2)); + + va_start (va, args); + n = 2; + argv[0] = (char *) cmd; + argv[1] = (char *) args; + while ((argv[n] = va_arg (va, char *)) != NULL) + n++; + va_end (va); + + /* OK, we need to block signals */ + sigfillset (&full); + sigprocmask (SIG_SETMASK, &full, &old); + +#ifdef THERE_IS_NO_FORK + signal_reset (); + pid = vfork (); +#else + pid = fork(); +#endif + + switch (pid) { + case -1: + logger (LOG_ERR, "vfork: %s", strerror (errno)); + ret = -1; + break; + case 0: +#ifndef THERE_IS_NO_FORK + signal_reset (); +#endif + sigprocmask (SIG_SETMASK, &old, NULL); + if (execvp (cmd, argv) && errno != ENOENT) + logger (LOG_ERR, "error executing \"%s\": %s", + cmd, strerror (errno)); + _exit (111); + /* NOTREACHED */ + } + +#ifdef THERE_IS_NO_FORK + signal_setup (); +#endif + + /* Restore our signals */ + sigprocmask (SIG_SETMASK, &old, NULL); + + free (argv); + return (ret); +} + +static void exec_script (const char *script, const char *infofile, + const char *arg) +{ + struct stat buf; + + if (! script || ! infofile || ! arg) + return; + + if (stat (script, &buf) == -1) { + if (strcmp (script, DEFAULT_SCRIPT) != 0) + logger (LOG_ERR, "`%s': %s", script, strerror (ENOENT)); + return; + } + +#ifdef ENABLE_INFO + logger (LOG_DEBUG, "exec \"%s\" \"%s\" \"%s\"", script, infofile, arg); + exec_cmd (script, infofile, arg, (char *) NULL); +#else + logger (LOG_DEBUG, "exec \"%s\" \"\" \"%s\"", script, arg); + exec_cmd (script, "", arg, (char *) NULL); +#endif +} + +static int make_resolv (const char *ifname, const dhcp_t *dhcp) +{ + FILE *f = NULL; + address_t *address; + +#ifdef ENABLE_RESOLVCONF + char *resolvconf = NULL; + + if (file_in_path ("resolvconf") == 0) { + size_t len = strlen ("resolvconf -a ") + strlen (ifname) + 1; + resolvconf = xmalloc (sizeof (char) * len); + snprintf (resolvconf, len, "resolvconf -a %s", ifname); + if ((f = popen (resolvconf , "w"))) + logger (LOG_DEBUG, + "sending DNS information to resolvconf"); + else if (errno == EEXIST) + logger (LOG_ERR, "popen: %s", strerror (errno)); + + if (ferror (f)) + logger (LOG_ERR, "ferror"); + free (resolvconf); + } +#endif + if (! f) { + logger (LOG_DEBUG, "writing "RESOLVFILE); + if (! (f = fopen(RESOLVFILE, "w"))) + logger (LOG_ERR, "fopen `%s': %s", RESOLVFILE, strerror (errno)); + } + + if (! f) + return (-1); + + fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname); + if (dhcp->dnssearch) + fprintf (f, "search %s\n", dhcp->dnssearch); + else if (dhcp->dnsdomain) { + fprintf (f, "search %s\n", dhcp->dnsdomain); + } + + STAILQ_FOREACH (address, dhcp->dnsservers, entries) + fprintf (f, "nameserver %s\n", inet_ntoa (address->address)); + +#ifdef ENABLE_RESOLVCONF + if (resolvconf) + pclose (f); + else +#endif + fclose (f); + + /* Refresh the local resolver */ + res_init (); + return (0); +} + +static void restore_resolv (const char *ifname) +{ +#ifdef ENABLE_RESOLVCONF + if (file_in_path ("resolvconf") == 0) { + logger (LOG_DEBUG, "removing information from resolvconf"); + exec_cmd("resolvconf", "-d", ifname, (char *) NULL); + } +#endif +} + +static bool in_addresses (const struct address_head *addresses, + struct in_addr address) +{ + const address_t *addr; + + STAILQ_FOREACH (addr, addresses, entries) + if (addr->address.s_addr == address.s_addr) + return (true); + + return (false); +} + +static bool in_routes (const struct route_head *routes, route_t *route) +{ + const route_t *r; + + if (! routes) + return (false); + + STAILQ_FOREACH (r, routes, entries) + if (r->destination.s_addr == route->destination.s_addr && + r->netmask.s_addr == route->netmask.s_addr && + r->gateway.s_addr == route->gateway.s_addr) + return (true); + + return (false); +} + +#ifdef ENABLE_NTP +static int _make_ntp (const char *file, const char *ifname, const dhcp_t *dhcp) +{ + FILE *f; + address_t *address; + char *a; + char *line; + int tomatch = 0; + char *token; + bool ntp = false; + + STAILQ_FOREACH (address, dhcp->ntpservers, entries) + tomatch++; + + /* Check that we really need to update the servers. + * We do this because ntp has to be restarted to + * work with a changed config. */ + if (! (f = fopen (file, "r"))) { + if (errno != ENOENT) { + logger (LOG_ERR, "fopen `%s': %s", + file, strerror (errno)); + return (-1); + } + } else { + while (tomatch != 0 && (line = get_line (f))) { + struct in_addr addr; + + a = line; + token = strsep (&a, " "); + if (! token || strcmp (token, "server") != 0) + goto next; + + if ((token = strsep (&a, " \n")) == NULL) + goto next; + + if (inet_aton (token, &addr) == 1 && + in_addresses (dhcp->ntpservers, addr)) + tomatch--; + +next: + free (line); + } + fclose (f); + + /* File has the same name servers that we do, + * so no need to restart ntp */ + if (tomatch == 0) { + logger (LOG_DEBUG, "%s already configured, skipping", + file); + return (0); + } + } + + logger (LOG_DEBUG, "writing %s", file); + if (! (f = fopen (file, "w"))) { + logger (LOG_ERR, "fopen `%s': %s", file, strerror (errno)); + return (-1); + } + + fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname); +#ifdef NTPFILE + if (strcmp (file, NTPFILE) == 0) { + ntp = true; + fprintf (f, "restrict default noquery notrust nomodify\n"); + fprintf (f, "restrict 127.0.0.1\n"); + } +#endif + + STAILQ_FOREACH (address, dhcp->ntpservers, entries) { + a = inet_ntoa (address->address); + if (ntp) + fprintf (f, "restrict %s nomodify notrap noquery\n", a); + fprintf (f, "server %s\n", a); + } + fclose (f); + + return (1); +} + +static int make_ntp (const char *ifname, const dhcp_t *dhcp) +{ + /* On some systems we have only have one ntp service, but we don't + * know which configuration file we're using. So we need to write + * to both and restart accordingly. */ + + bool restart_ntp = false; + bool restart_openntp = false; + int retval = 0; + +#ifdef NTPFILE + if (_make_ntp (NTPFILE, ifname, dhcp) > 0) + restart_ntp = true; +#endif + +#ifdef OPENNTPFILE + if (_make_ntp (OPENNTPFILE, ifname, dhcp) > 0) + restart_openntp = true; +#endif + +#ifdef NTPSERVICE + if (restart_ntp) { +#ifdef NTPCHECK + if (system (NTPCHECK) == 0) +#endif + retval += exec_cmd (NTPSERVICE, NTPRESTARTARGS, + (char *) NULL); + } +#endif + +#if defined (NTPSERVICE) && defined (OPENNTPSERVICE) + if (restart_openntp && + (strcmp (NTPSERVICE, OPENNTPSERVICE) != 0 || ! restart_ntp)) + { +#ifdef OPENNTPCHECK + if (system (OPENNTPCHECK) == 0) +#endif + retval += exec_cmd (OPENNTPSERVICE, + OPENNTPRESTARTARGS, (char *) NULL); + } +#elif defined (OPENNTPSERVICE) && ! defined (NTPSERVICE) + if (restart_openntp) { +#ifdef OPENNTPCHECK + if (system (OPENNTPCHECK) == 0) +#endif + retval += exec_cmd (OPENNTPSERVICE, + OPENNTPRESTARTARGS, (char *) NULL); + } +#endif + + return (retval); +} +#endif + +#ifdef ENABLE_NIS +#define PREFIXSIZE 256 +static int make_nis (const char *ifname, const dhcp_t *dhcp) +{ + FILE *f; + address_t *address; + char *prefix; + + logger (LOG_DEBUG, "writing "NISFILE); + if (! (f = fopen(NISFILE, "w"))) { + logger (LOG_ERR, "fopen `%s': %s", NISFILE, strerror (errno)); + return (-1); + } + + prefix = xmalloc (sizeof (char) * PREFIXSIZE); + *prefix = '\0'; + fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname); + + if (dhcp->nisdomain) { + setdomainname (dhcp->nisdomain, (int) strlen (dhcp->nisdomain)); + + if (dhcp->nisservers) + snprintf (prefix, PREFIXSIZE, "domain %s server", + dhcp->nisdomain); + else + fprintf (f, "domain %s broadcast\n", dhcp->nisdomain); + } + else + snprintf (prefix, PREFIXSIZE, "%s", "ypserver"); + + NSTAILQ_FOREACH (address, dhcp->nisservers, entries) + fprintf (f, "%s %s\n", prefix, inet_ntoa (address->address)); + + free (prefix); + fclose (f); + +#ifdef NISCHECK + if (system (NISCHECK) == 0) +#endif + exec_cmd (NISSERVICE, NISRESTARTARGS, (char *) NULL); + return (0); +} +#endif + +static char *lookuphostname (char *hostname, const dhcp_t *dhcp, + const options_t *options) +{ + union { + struct sockaddr sa; + struct sockaddr_in sin; + } su; + socklen_t salen; + char *addr; + struct addrinfo hints; + struct addrinfo *res = NULL; + int result; + char *p; + + logger (LOG_DEBUG, "Looking up hostname via DNS"); + addr = xmalloc (sizeof (char) * NI_MAXHOST); + salen = sizeof (su.sa); + memset (&su.sa, 0, salen); + su.sin.sin_family = AF_INET; + memcpy (&su.sin.sin_addr, &dhcp->address, sizeof (su.sin.sin_addr)); + + if ((result = getnameinfo (&su.sa, salen, addr, NI_MAXHOST, + NULL, 0, NI_NAMEREQD)) != 0) { + logger (LOG_ERR, + "Failed to lookup hostname via DNS: %s", + gai_strerror (result)); + free (addr); + return (NULL); + } + + /* Check for a malicious PTR record */ + memset (&hints, 0, sizeof (hints)); + hints.ai_socktype = SOCK_DGRAM; + hints.ai_flags = AI_NUMERICHOST; + result = getaddrinfo (addr, "0", &hints, &res); + if (res) + freeaddrinfo (res); + if (result == 0) + logger (LOG_ERR, "malicious PTR record detected"); + if (result == 0 || ! *addr) { + free (addr); + return (NULL); + } + + p = strchr (addr, '.'); + if (p) { + switch (options->dohostname) { + case 1: /* -H */ + case 4: /* -HHHH */ + break; + case 2: /* -HH */ + case 5: /* -HHHHH */ + /* Strip out the domain if it matches */ + p++; + if (*p && dhcp->dnssearch) { + char *s = xstrdup (dhcp->dnssearch); + char *sp = s; + char *t; + + while ((t = strsep (&sp, " "))) + if (strcmp (t, p) == 0) { + *--p = '\0'; + break; + } + free (s); + } else if (dhcp->dnsdomain) { + if (strcmp (dhcp->dnsdomain, p) == 0) + *--p = '\0'; + } + break; + case 3: /* -HHH */ + case 6: /* -HHHHHH */ + /* Just strip the domain */ + *p = '\0'; + break; + default: /* Too many H! */ + break; + } + } + + strlcpy (hostname, addr, MAXHOSTNAMELEN); + free (addr); + return (hostname); +} + +int configure (const options_t *options, interface_t *iface, + const dhcp_t *dhcp, bool up) +{ + route_t *route = NULL; + struct route_head *new_routes = NULL; + route_t *new_route = NULL; + char *newhostname = NULL; + char *curhostname = NULL; + int remember; +#ifdef ENABLE_IPV4LL + bool haslinklocal = false; +#endif +#ifdef THERE_IS_NO_FORK + int skip = 0; + size_t skiplen; + char *skipp; +#endif + + if (! options || ! iface || ! dhcp) + return (-1); + + if (dhcp->address.s_addr == 0) + up = 0; + + /* Remove old routes. + * Always do this as the interface may have >1 address not added by us + * so the routes we added may still exist. */ + NSTAILQ_FOREACH (route, iface->previous_routes, entries) + if ((route->destination.s_addr || options->dogateway) && + (! up || ! in_routes (dhcp->routes, route))) + del_route (iface->name, route->destination, + route->netmask, route->gateway, + options->metric); + /* If we aren't up, then reset the interface as much as we can */ + if (! up) { + if (iface->previous_routes) { + free_route (iface->previous_routes); + iface->previous_routes = NULL; + } + + /* Restore the original MTU value */ + if (iface->mtu && iface->previous_mtu != iface->mtu) { + set_mtu (iface->name, iface->mtu); + iface->previous_mtu = iface->mtu; + } + +#ifdef ENABLE_INFO + /* If we haven't created an info file, do so now */ + if (! dhcp->frominfo) + write_info (iface, dhcp, options, false); +#endif + + /* Only reset things if we had set them before */ + if (iface->previous_address.s_addr != 0) { + if (! options->keep_address) { + del_address (iface->name, + iface->previous_address, + iface->previous_netmask); + memset (&iface->previous_address, + 0, sizeof (iface->previous_address)); + memset (&iface->previous_netmask, + 0, sizeof (iface->previous_netmask)); + } + } + + restore_resolv (iface->name); + exec_script (options->script, iface->infofile, "down"); + + return (0); + } + + /* Set the MTU requested. + * If the DHCP server no longer sends one OR it's invalid then + * we restore the original MTU */ + if (options->domtu) { + unsigned short mtu = iface->mtu; + if (dhcp->mtu) + mtu = dhcp->mtu; + + if (mtu != iface->previous_mtu) { + if (set_mtu (iface->name, mtu) == 0) + iface->previous_mtu = mtu; + } + } + + /* This also changes netmask */ + if (! options->doinform || ! has_address (iface->name, dhcp->address)) + if (add_address (iface->name, dhcp->address, dhcp->netmask, + dhcp->broadcast) == -1 && errno != EEXIST) + return (false); + + /* Now delete the old address if different */ + if (iface->previous_address.s_addr != dhcp->address.s_addr && + iface->previous_address.s_addr != 0 && + ! options->keep_address) + del_address (iface->name, + iface->previous_address, iface->previous_netmask); + +#ifdef __linux__ + /* On linux, we need to change the subnet route to have our metric. */ + if (iface->previous_address.s_addr != dhcp->address.s_addr && + options->metric > 0 && + dhcp->netmask.s_addr != INADDR_BROADCAST) + { + struct in_addr td; + struct in_addr tg; + memset (&td, 0, sizeof (td)); + memset (&tg, 0, sizeof (tg)); + td.s_addr = dhcp->address.s_addr & dhcp->netmask.s_addr; + add_route (iface->name, td, dhcp->netmask, tg, options->metric); + del_route (iface->name, td, dhcp->netmask, tg, 0); + } +#endif + +#ifdef THERE_IS_NO_FORK + free (dhcpcd_skiproutes); + /* We can never have more than 255 routes. So we need space + * for 255 3 digit numbers and commas */ + skiplen = 255 * 4 + 1; + skipp = dhcpcd_skiproutes = xmalloc (sizeof (char) * skiplen); + *skipp = '\0'; +#endif + + /* Remember added routes */ + NSTAILQ_FOREACH (route, dhcp->routes, entries) { +#ifdef ENABLE_IPV4LL + /* Check if we have already got a link locale route dished + * out by the DHCP server */ + if (route->destination.s_addr == htonl (LINKLOCAL_ADDR) && + route->netmask.s_addr == htonl (LINKLOCAL_MASK)) + haslinklocal = true; +#endif + /* Don't set default routes if not asked to */ + if (route->destination.s_addr == 0 && + route->netmask.s_addr == 0 && + ! options->dogateway) + continue; + + remember = add_route (iface->name, route->destination, + route->netmask, route->gateway, + options->metric); + /* If we failed to add the route, we may have already added it + ourselves. If so, remember it again. */ + if (remember < 0 && in_routes (iface->previous_routes, route)) + remember = 1; + + if (remember >= 0) { + if (! new_routes) { + new_routes = xmalloc (sizeof (*new_routes)); + STAILQ_INIT (new_routes); + } + new_route = xmalloc (sizeof (route_t)); + memcpy (new_route, route, sizeof (*new_route)); + STAILQ_INSERT_TAIL (new_routes, new_route, entries); + } +#ifdef THERE_IS_NO_FORK + /* If we have daemonised yet we need to record which routes + * we failed to add so we can skip them */ + else if (! options->daemonised) { + /* We can never have more than 255 / 4 routes, + * so 3 chars is plently */ + if (*skipp) + *skipp++ = ','; + skipp += snprintf (skipp, + dhcpcd_skiproutes + skiplen - skipp, + "%d", skip); + } + skip++; +#endif + } + +#ifdef THERE_IS_NO_FORK + if (*dhcpcd_skiproutes) + *skipp = '\0'; + else { + free (dhcpcd_skiproutes); + dhcpcd_skiproutes = NULL; + } +#endif + +#ifdef ENABLE_IPV4LL + /* Ensure we always add the link local route if we got a private + * address and isn't link local itself */ + if (options->doipv4ll && + ! haslinklocal && + IN_PRIVATE (ntohl (dhcp->address.s_addr))) + { + struct in_addr dest; + struct in_addr mask; + struct in_addr gate; + + dest.s_addr = htonl (LINKLOCAL_ADDR); + mask.s_addr = htonl (LINKLOCAL_MASK); + gate.s_addr = 0; + remember = add_route (iface->name, dest, mask, gate, + options->metric); + + if (remember >= 0) { + if (! new_routes) { + new_routes = xmalloc (sizeof (*new_routes)); + STAILQ_INIT (new_routes); + } + new_route = xmalloc (sizeof (*new_route)); + new_route->destination.s_addr = dest.s_addr; + new_route->netmask.s_addr = mask.s_addr; + new_route->gateway.s_addr = gate.s_addr; + STAILQ_INSERT_TAIL (new_routes, new_route, entries); + } + } +#endif + + if (iface->previous_routes) + free_route (iface->previous_routes); + iface->previous_routes = new_routes; + + logToQt(LOG_INFO, DHCPCD_WRITE, ""); + if (options->dodns && dhcp->dnsservers) + make_resolv(iface->name, dhcp); + else + logger (LOG_DEBUG, "no dns information to write"); + +#ifdef ENABLE_NTP + if (options->dontp && dhcp->ntpservers) + make_ntp(iface->name, dhcp); +#endif + +#ifdef ENABLE_NIS + if (options->donis && (dhcp->nisservers || dhcp->nisdomain)) + make_nis(iface->name, dhcp); +#endif + + curhostname = xmalloc (sizeof (char) * MAXHOSTNAMELEN); + *curhostname = '\0'; + + gethostname (curhostname, MAXHOSTNAMELEN); + if (options->dohostname || + strlen (curhostname) == 0 || + strcmp (curhostname, "(none)") == 0 || + strcmp (curhostname, "localhost") == 0) + { + newhostname = xmalloc (sizeof (char) * MAXHOSTNAMELEN); + + if (dhcp->hostname) + strlcpy (newhostname, dhcp->hostname, MAXHOSTNAMELEN); + else + *newhostname = '\0'; + + /* Now we have made a resolv.conf we can obtain a hostname + * if we need it */ + if (! *newhostname || options->dohostname > 3) + lookuphostname (newhostname, dhcp, options); + + if (*newhostname) { + logger (LOG_INFO, "setting hostname to `%s'", + newhostname); + sethostname (newhostname, (int) strlen (newhostname)); + } + + free (newhostname); + } + + free (curhostname); + +#ifdef ENABLE_INFO + if (! dhcp->frominfo) + write_info (iface, dhcp, options, true); +#endif + + if (iface->previous_address.s_addr != dhcp->address.s_addr || + iface->previous_netmask.s_addr != dhcp->netmask.s_addr) + { + memcpy (&iface->previous_address, + &dhcp->address, sizeof (iface->previous_address)); + memcpy (&iface->previous_netmask, + &dhcp->netmask, sizeof (iface->previous_netmask)); + exec_script (options->script, iface->infofile, "new"); + } else + exec_script (options->script, iface->infofile, "up"); + + return (0); +} diff --git a/customdhcpcd/src/configure.h b/customdhcpcd/src/configure.h new file mode 100644 index 0000000..3166947 --- /dev/null +++ b/customdhcpcd/src/configure.h @@ -0,0 +1,38 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef DHCPCONFIG_H +#define DHCPCONFIG_H + +#include "dhcpcd.h" +#include "interface.h" +#include "dhcp.h" + +int configure (const options_t *options, interface_t *iface, + const dhcp_t *dhcp, bool up); + +#endif diff --git a/customdhcpcd/src/customdhcpcd b/customdhcpcd/src/customdhcpcd new file mode 100755 index 0000000..5ef34d0 Binary files /dev/null and b/customdhcpcd/src/customdhcpcd differ diff --git a/customdhcpcd/src/customdhcpcd.c b/customdhcpcd/src/customdhcpcd.c new file mode 100644 index 0000000..7c5e219 --- /dev/null +++ b/customdhcpcd/src/customdhcpcd.c @@ -0,0 +1,17 @@ +/* + ============================================================================ + Name : customdhcpcd.c + Author : Niklas Goby + Version : + Copyright : Your copyright notice + Description : Hello World in C, Ansi-style + ============================================================================ + */ + +#include +#include + +int main(void) { + puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */ + return EXIT_SUCCESS; +} diff --git a/customdhcpcd/src/dhcp.c b/customdhcpcd/src/dhcp.c new file mode 100644 index 0000000..f625e8f --- /dev/null +++ b/customdhcpcd/src/dhcp.c @@ -0,0 +1,933 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include + +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" + +#include "common.h" +#include "dhcpcd.h" +#include "dhcp.h" +#include "interface.h" +#include "logger.h" +#include "socket.h" + +#ifndef STAILQ_CONCAT +#define STAILQ_CONCAT(head1, head2) do { \ + if (!STAILQ_EMPTY((head2))) { \ + *(head1)->stqh_last = (head2)->stqh_first; \ + (head1)->stqh_last = (head2)->stqh_last; \ + STAILQ_INIT((head2)); \ + } \ +} while (0) +#endif + +typedef struct message { + int value; + const char *name; +} dhcp_message_t; + +static dhcp_message_t dhcp_messages[] = { + { DHCP_DISCOVER, "DHCP_DISCOVER" }, + { DHCP_OFFER, "DHCP_OFFER" }, + { DHCP_REQUEST, "DHCP_REQUEST" }, + { DHCP_DECLINE, "DHCP_DECLINE" }, + { DHCP_ACK, "DHCP_ACK" }, + { DHCP_NAK, "DHCP_NAK" }, + { DHCP_RELEASE, "DHCP_RELEASE" }, + { DHCP_INFORM, "DHCP_INFORM" }, + { -1, NULL } +}; + +static const char *dhcp_message (int type) +{ + dhcp_message_t *d; + for (d = dhcp_messages; d->name; d++) + if (d->value == type) + return (d->name); + + return (NULL); +} + +ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, + uint32_t xid, char type, const options_t *options) +{ + struct udp_dhcp_packet *packet; + dhcpmessage_t *message; + unsigned char *m; + unsigned char *p; + unsigned char *n_params = NULL; + size_t l; + struct in_addr from; + struct in_addr to; + time_t up = uptime() - iface->start_uptime; + uint32_t ul; + uint16_t sz; + size_t message_length; + ssize_t retval; + + if (!iface || !options || !dhcp) + return -1; + + memset (&from, 0, sizeof (from)); + memset (&to, 0, sizeof (to)); + + if (type == DHCP_RELEASE) + to.s_addr = dhcp->serveraddress.s_addr; + + message = xzalloc (sizeof (*message)); + m = (unsigned char *) message; + p = (unsigned char *) &message->options; + + if ((type == DHCP_INFORM || + type == DHCP_RELEASE || + type == DHCP_REQUEST) && + ! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr))) + { + message->ciaddr = iface->previous_address.s_addr; + from.s_addr = iface->previous_address.s_addr; + + /* Just incase we haven't actually configured the address yet */ + if (type == DHCP_INFORM && iface->previous_address.s_addr == 0) + message->ciaddr = dhcp->address.s_addr; + + /* Zero the address if we're currently on a different subnet */ + if (type == DHCP_REQUEST && + iface->previous_netmask.s_addr != dhcp->netmask.s_addr) + message->ciaddr = from.s_addr = 0; + + if (from.s_addr != 0) + to.s_addr = dhcp->serveraddress.s_addr; + } + + message->op = DHCP_BOOTREQUEST; + message->hwtype = iface->family; + switch (iface->family) { + case ARPHRD_ETHER: + case ARPHRD_IEEE802: + message->hwlen = ETHER_ADDR_LEN; + memcpy (&message->chaddr, &iface->hwaddr, + ETHER_ADDR_LEN); + break; + case ARPHRD_IEEE1394: + case ARPHRD_INFINIBAND: + message->hwlen = 0; + if (message->ciaddr == 0) + message->flags = htons (BROADCAST_FLAG); + break; + default: + logger (LOG_ERR, "dhcp: unknown hardware type %d", + iface->family); + } + + if (up < 0 || up > (time_t) UINT16_MAX) + message->secs = htons ((uint16_t) UINT16_MAX); + else + message->secs = htons (up); + message->xid = xid; + message->cookie = htonl (MAGIC_COOKIE); + + *p++ = DHCP_MESSAGETYPE; + *p++ = 1; + *p++ = type; + + if (type == DHCP_REQUEST) { + *p++ = DHCP_MAXMESSAGESIZE; + *p++ = 2; + sz = get_mtu (iface->name); + if (sz < MTU_MIN) { + if (set_mtu (iface->name, MTU_MIN) == 0) + sz = MTU_MIN; + } + sz = htons (sz); + memcpy (p, &sz, 2); + p += 2; + } + + *p++ = DHCP_CLIENTID; + *p++ = iface->clientid_len; + memcpy (p, iface->clientid, iface->clientid_len); + p+= iface->clientid_len; + + if (type != DHCP_DECLINE && type != DHCP_RELEASE) { + if (options->userclass_len > 0) { + *p++ = DHCP_USERCLASS; + *p++ = options->userclass_len; + memcpy (p, &options->userclass, options->userclass_len); + p += options->userclass_len; + } + + if (*options->classid > 0) { + *p++ = DHCP_CLASSID; + *p++ = l = strlen (options->classid); + memcpy (p, options->classid, l); + p += l; + } + } + + if (type == DHCP_DISCOVER || type == DHCP_REQUEST) { +#define PUTADDR(_type, _val) { \ + *p++ = _type; \ + *p++ = 4; \ + memcpy (p, &_val.s_addr, 4); \ + p += 4; \ +} + if (IN_LINKLOCAL (ntohl (dhcp->address.s_addr))) + logger (LOG_ERR, + "cannot request a link local address"); + else { + if (dhcp->address.s_addr && + dhcp->address.s_addr != + iface->previous_address.s_addr) + { + PUTADDR (DHCP_ADDRESS, dhcp->address); + if (dhcp->serveraddress.s_addr) + PUTADDR (DHCP_SERVERIDENTIFIER, + dhcp->serveraddress); + } + } +#undef PUTADDR + + if (options->leasetime != 0) { + *p++ = DHCP_LEASETIME; + *p++ = 4; + ul = htonl (options->leasetime); + memcpy (p, &ul, 4); + p += 4; + } + } + + if (type == DHCP_DISCOVER || + type == DHCP_INFORM || + type == DHCP_REQUEST) + { + if (options->hostname[0]) { + if (options->fqdn == FQDN_DISABLE) { + *p++ = DHCP_HOSTNAME; + *p++ = l = strlen (options->hostname); + memcpy (p, options->hostname, l); + p += l; + } else { + /* Draft IETF DHC-FQDN option (81) */ + *p++ = DHCP_FQDN; + *p++ = (l = strlen (options->hostname)) + 3; + /* Flags: 0000NEOS + * S: 1 => Client requests Server to update + * a RR in DNS as well as PTR + * O: 1 => Server indicates to client that + * DNS has been updated + * E: 1 => Name data is DNS format + * N: 1 => Client requests Server to not + * update DNS + */ + *p++ = options->fqdn & 0x9; + *p++ = 0; /* from server for PTR RR */ + *p++ = 0; /* from server for A RR if S=1 */ + memcpy (p, options->hostname, l); + p += l; + } + } + + *p++ = DHCP_PARAMETERREQUESTLIST; + n_params = p; + *p++ = 0; + /* Only request DNSSERVER in discover to keep the packets small. + * RFC2131 Section 3.5 states that the REQUEST must include the + * list from the DISCOVER message, so I think this is ok. */ + + if (type == DHCP_DISCOVER && ! options->test) + *p++ = DHCP_DNSSERVER; + else { + if (type != DHCP_INFORM) { + *p++ = DHCP_RENEWALTIME; + *p++ = DHCP_REBINDTIME; + } + *p++ = DHCP_NETMASK; + *p++ = DHCP_BROADCAST; + + /* -S means request CSR and MSCSR + * -SS means only request MSCSR incase DHCP message + * is too big */ + if (options->domscsr < 2) + *p++ = DHCP_CSR; + if (options->domscsr > 0) + *p++ = DHCP_MSCSR; + /* RFC 3442 states classless static routes should be + * before routers and static routes as classless static + * routes override them both */ + *p++ = DHCP_STATICROUTE; + *p++ = DHCP_ROUTERS; + *p++ = DHCP_HOSTNAME; + *p++ = DHCP_DNSSEARCH; + *p++ = DHCP_DNSDOMAIN; + *p++ = DHCP_DNSSERVER; +#ifdef ENABLE_NIS + *p++ = DHCP_NISDOMAIN; + *p++ = DHCP_NISSERVER; +#endif +#ifdef ENABLE_NTP + *p++ = DHCP_NTPSERVER; +#endif + *p++ = DHCP_MTU; +#ifdef ENABLE_INFO + *p++ = DHCP_ROOTPATH; + *p++ = DHCP_SIPSERVER; +#endif + } + + *n_params = p - n_params - 1; + } + *p++ = DHCP_END; + +#ifdef BOOTP_MESSAGE_LENTH_MIN + /* Some crappy DHCP servers think they have to obey the BOOTP minimum + * message length. + * They are wrong, but we should still cater for them. */ + while (p - m < BOOTP_MESSAGE_LENTH_MIN) + *p++ = DHCP_PAD; +#endif + + message_length = p - m; + + packet = xzalloc (sizeof (*packet)); + make_dhcp_packet (packet, (unsigned char *) message, message_length, + from, to); + free (message); + + logger (LOG_DEBUG, "sending %s with xid 0x%x", + dhcp_message (type), xid); + retval = send_packet (iface, ETHERTYPE_IP, (unsigned char *) packet, + message_length + + sizeof (packet->ip) + sizeof (packet->udp)); + free (packet); + return (retval); +} + +/* Decode an RFC3397 DNS search order option into a space + * seperated string. Returns length of string (including + * terminating zero) or zero on error. out may be NULL + * to just determine output length. */ +static unsigned int decode_search (const unsigned char *p, int len, char *out) +{ + const unsigned char *r, *q = p; + unsigned int count = 0, l, hops; + + while (q - p < len) { + r = NULL; + hops = 0; + while ((l = *q++)) { + unsigned int label_type = l & 0xc0; + if (label_type == 0x80 || label_type == 0x40) + return 0; + else if (label_type == 0xc0) { /* pointer */ + l = (l & 0x3f) << 8; + l |= *q++; + + /* save source of first jump. */ + if (!r) + r = q; + + hops++; + if (hops > 255) + return 0; + + q = p + l; + if (q - p >= len) + return 0; + } else { + /* straightforward name segment, add with '.' */ + count += l + 1; + if (out) { + memcpy (out, q, l); + out += l; + *out++ = '.'; + } + q += l; + } + } + + /* change last dot to space */ + if (out) + *(out - 1) = ' '; + + if (r) + q = r; + } + + /* change last space to zero terminator */ + if (out) + *(out - 1) = 0; + + return count; +} + +/* Add our classless static routes to the routes variable + * and return the last route set */ +static struct route_head *decode_CSR (const unsigned char *p, int len) +{ + const unsigned char *q = p; + unsigned int cidr; + unsigned int ocets; + struct route_head *routes = NULL; + route_t *route; + + /* Minimum is 5 -first is CIDR and a router length of 4 */ + if (len < 5) + return NULL; + + while (q - p < len) { + if (! routes) { + routes = xmalloc (sizeof (*routes)); + STAILQ_INIT (routes); + } + + route = xzalloc (sizeof (*route)); + + cidr = *q++; + if (cidr > 32) { + logger (LOG_ERR, + "invalid CIDR of %d in classless static route", + cidr); + free_route (routes); + return (NULL); + } + ocets = (cidr + 7) / 8; + + if (ocets > 0) { + memcpy (&route->destination.s_addr, q, (size_t) ocets); + q += ocets; + } + + /* Now enter the netmask */ + if (ocets > 0) { + memset (&route->netmask.s_addr, 255, (size_t) ocets - 1); + memset ((unsigned char *) &route->netmask.s_addr + + (ocets - 1), + (256 - (1 << (32 - cidr) % 8)), 1); + } + + /* Finally, snag the router */ + memcpy (&route->gateway.s_addr, q, 4); + q += 4; + + STAILQ_INSERT_TAIL (routes, route, entries); + } + + return (routes); +} + +void free_dhcp (dhcp_t *dhcp) +{ + if (! dhcp) + return; + + free_route (dhcp->routes); + free (dhcp->hostname); + free_address (dhcp->dnsservers); + free (dhcp->dnsdomain); + free (dhcp->dnssearch); + free_address (dhcp->ntpservers); + free (dhcp->nisdomain); + free_address (dhcp->nisservers); + free (dhcp->rootpath); + free (dhcp->sipservers); + if (dhcp->fqdn) { + free (dhcp->fqdn->name); + free (dhcp->fqdn); + } +} + + +static bool dhcp_add_address (struct address_head **addresses, + const unsigned char *data, + int length) +{ + int i; + address_t *address; + + for (i = 0; i < length; i += 4) { + /* Sanity check */ + if (i + 4 > length) { + logger (LOG_ERR, "invalid address length"); + return (false); + } + + if (*addresses == NULL) { + *addresses = xmalloc (sizeof (**addresses)); + STAILQ_INIT (*addresses); + } + address = xzalloc (sizeof (*address)); + memcpy (&address->address.s_addr, data + i, 4); + STAILQ_INSERT_TAIL (*addresses, address, entries); + } + + return (true); +} + +#ifdef ENABLE_INFO +static char *decode_sipservers (const unsigned char *data, int length) +{ + char *sip = NULL; + char *p; + const char encoding = *data++; + struct in_addr addr; + size_t len; + + length--; + + switch (encoding) { + case 0: + if ((len = decode_search (data, length, NULL)) > 0) { + sip = xmalloc (len); + decode_search (data, length, sip); + } + break; + + case 1: + if (length == 0 || length % 4 != 0) { + logger (LOG_ERR, + "invalid length %d for option 120", + length + 1); + break; + } + len = ((length / 4) * (4 * 4)) + 1; + sip = p = xmalloc (len); + while (length != 0) { + memcpy (&addr.s_addr, data, 4); + data += 4; + p += snprintf (p, len - (p - sip), + "%s ", inet_ntoa (addr)); + length -= 4; + } + *--p = '\0'; + break; + + default: + logger (LOG_ERR, "unknown sip encoding %d", encoding); + break; + } + + return (sip); +} +#endif + +/* This calculates the netmask that we should use for static routes. + * This IS different from the calculation used to calculate the netmask + * for an interface address. */ +static uint32_t route_netmask (uint32_t ip_in) +{ + /* used to be unsigned long - check if error */ + uint32_t p = ntohl (ip_in); + uint32_t t; + + if (IN_CLASSA (p)) + t = ~IN_CLASSA_NET; + else { + if (IN_CLASSB (p)) + t = ~IN_CLASSB_NET; + else { + if (IN_CLASSC (p)) + t = ~IN_CLASSC_NET; + else + t = 0; + } + } + + while (t & p) + t >>= 1; + + return (htonl (~t)); +} + +static struct route_head *decode_routes (const unsigned char *data, int length) +{ + int i; + struct route_head *head = NULL; + route_t *route; + + for (i = 0; i < length; i += 8) { + if (! head) { + head = xmalloc (sizeof (*head)); + STAILQ_INIT (head); + } + route = xzalloc (sizeof (*route)); + memcpy (&route->destination.s_addr, data + i, 4); + memcpy (&route->gateway.s_addr, data + i + 4, 4); + route->netmask.s_addr = + route_netmask (route->destination.s_addr); + STAILQ_INSERT_TAIL (head, route, entries); + } + + return (head); +} + +static struct route_head *decode_routers (const unsigned char *data, int length) +{ + int i; + struct route_head *head = NULL; + route_t *route = NULL; + + for (i = 0; i < length; i += 4) { + if (! head) { + head = xmalloc (sizeof (*head)); + STAILQ_INIT (head); + } + route = xzalloc (sizeof (*route)); + memcpy (&route->gateway.s_addr, data + i, 4); + STAILQ_INSERT_TAIL (head, route, entries); + } + + return (head); +} + +int parse_dhcpmessage (dhcp_t *dhcp, const dhcpmessage_t *message) +{ + const unsigned char *p = message->options; + const unsigned char *end = p; /* Add size later for gcc-3 issue */ + unsigned char option; + unsigned char length; + unsigned int len = 0; + int retval = -1; + struct timeval tv; + struct route_head *routers = NULL; + struct route_head *routes = NULL; + struct route_head *csr = NULL; + struct route_head *mscsr = NULL; + bool in_overload = false; + bool parse_sname = false; + bool parse_file = false; + + end += sizeof (message->options); + + if (gettimeofday (&tv, NULL) == -1) { + logger (LOG_ERR, "gettimeofday: %s", strerror (errno)); + return (-1); + } + + dhcp->address.s_addr = message->yiaddr; + dhcp->leasedfrom = tv.tv_sec; + dhcp->frominfo = false; + dhcp->address.s_addr = message->yiaddr; + strlcpy (dhcp->servername, (char *) message->servername, + sizeof (dhcp->servername)); + +#define LEN_ERR \ + { \ + logger (LOG_ERR, "invalid length %d for option %d", \ + length, option); \ + p += length; \ + continue; \ + } + +parse_start: + while (p < end) { + option = *p++; + if (! option) + continue; + + if (option == DHCP_END) + goto eexit; + + length = *p++; + + if (option != DHCP_PAD && length == 0) { + logger (LOG_ERR, "option %d has zero length", option); + retval = -1; + goto eexit; + } + + if (p + length >= end) { + logger (LOG_ERR, "dhcp option exceeds message length"); + retval = -1; + goto eexit; + } + + switch (option) { + case DHCP_MESSAGETYPE: + retval = (int) *p; + p += length; + continue; + + default: + if (length == 0) { + logger (LOG_DEBUG, + "option %d has zero length, skipping", + option); + continue; + } + } + +#define LENGTH(_length) \ + if (length != _length) \ + LEN_ERR; +#define MIN_LENGTH(_length) \ + if (length < _length) \ + LEN_ERR; +#define MULT_LENGTH(_mult) \ + if (length % _mult != 0) \ + LEN_ERR; +#define GET_UINT8(_val) \ + LENGTH (sizeof (uint8_t)); \ + memcpy (&_val, p, sizeof (uint8_t)); +#define GET_UINT16(_val) \ + LENGTH (sizeof (uint16_t)); \ + memcpy (&_val, p, sizeof (uint16_t)); +#define GET_UINT32(_val) \ + LENGTH (sizeof (uint32_t)); \ + memcpy (&_val, p, sizeof (uint32_t)); +#define GET_UINT16_H(_val) \ + GET_UINT16 (_val); \ + _val = ntohs (_val); +#define GET_UINT32_H(_val) \ + GET_UINT32 (_val); \ + _val = ntohl (_val); + + switch (option) { + case DHCP_ADDRESS: + GET_UINT32 (dhcp->address.s_addr); + break; + case DHCP_NETMASK: + GET_UINT32 (dhcp->netmask.s_addr); + break; + case DHCP_BROADCAST: + GET_UINT32 (dhcp->broadcast.s_addr); + break; + case DHCP_SERVERIDENTIFIER: + GET_UINT32 (dhcp->serveraddress.s_addr); + break; + case DHCP_LEASETIME: + GET_UINT32_H (dhcp->leasetime); + break; + case DHCP_RENEWALTIME: + GET_UINT32_H (dhcp->renewaltime); + break; + case DHCP_REBINDTIME: + GET_UINT32_H (dhcp->rebindtime); + break; + case DHCP_MTU: + GET_UINT16_H (dhcp->mtu); + /* Minimum legal mtu is 68 accoridng to + * RFC 2132. In practise it's 576 which is the + * minimum maximum message size. */ + if (dhcp->mtu < MTU_MIN) { + logger (LOG_DEBUG, + "MTU %d is too low, minimum is %d; ignoring", + dhcp->mtu, MTU_MIN); + dhcp->mtu = 0; + } + break; + +#undef GET_UINT32_H +#undef GET_UINT32 +#undef GET_UINT16_H +#undef GET_UINT16 +#undef GET_UINT8 + +#define GETSTR(_var) { \ + MIN_LENGTH (sizeof (char)); \ + if (_var) free (_var); \ + _var = xmalloc ((size_t) length + 1); \ + memcpy (_var, p, (size_t) length); \ + memset (_var + length, 0, 1); \ +} + case DHCP_HOSTNAME: + GETSTR (dhcp->hostname); + break; + case DHCP_DNSDOMAIN: + GETSTR (dhcp->dnsdomain); + break; + case DHCP_MESSAGE: + GETSTR (dhcp->message); + break; +#ifdef ENABLE_INFO + case DHCP_ROOTPATH: + GETSTR (dhcp->rootpath); + break; +#endif +#ifdef ENABLE_NIS + case DHCP_NISDOMAIN: + GETSTR (dhcp->nisdomain); + break; +#endif +#undef GETSTR + +#define GETADDR(_var) \ + MULT_LENGTH (4); \ + if (! dhcp_add_address (&_var, p, length)) \ + { \ + retval = -1; \ + goto eexit; \ + } + case DHCP_DNSSERVER: + GETADDR (dhcp->dnsservers); + break; +#ifdef ENABLE_NTP + case DHCP_NTPSERVER: + GETADDR (dhcp->ntpservers); + break; +#endif +#ifdef ENABLE_NIS + case DHCP_NISSERVER: + GETADDR (dhcp->nisservers); + break; +#endif +#undef GETADDR + + case DHCP_DNSSEARCH: + MIN_LENGTH (1); + free (dhcp->dnssearch); + len = decode_search (p, length, NULL); + if (len > 0) { + dhcp->dnssearch = xmalloc (len); + decode_search (p, length, + dhcp->dnssearch); + } + break; + + case DHCP_CSR: + MIN_LENGTH (5); + free_route (csr); + csr = decode_CSR (p, length); + break; + + case DHCP_MSCSR: + MIN_LENGTH (5); + free_route (mscsr); + mscsr = decode_CSR (p, length); + break; + +#ifdef ENABLE_INFO + case DHCP_SIPSERVER: + free (dhcp->sipservers); + dhcp->sipservers = decode_sipservers (p,length); + break; +#endif + + case DHCP_STATICROUTE: + MULT_LENGTH (8); + free_route (routes); + routes = decode_routes (p, length); + break; + + case DHCP_ROUTERS: + MULT_LENGTH (4); + free_route (routers); + routers = decode_routers (p, length); + break; + + case DHCP_OPTIONSOVERLOADED: + LENGTH (1); + /* The overloaded option in an overloaded option + * should be ignored, overwise we may get an + * infinite loop */ + if (! in_overload) { + if (*p & 1) + parse_file = true; + if (*p & 2) + parse_sname = true; + } + break; + + case DHCP_FQDN: + /* We ignore replies about FQDN */ + break; + +#undef LENGTH +#undef MIN_LENGTH +#undef MULT_LENGTH + + default: + logger (LOG_DEBUG, + "no facility to parse DHCP code %u", + option); + break; + } + + p += length; + } + +eexit: + /* We may have options overloaded, so go back and grab them */ + if (parse_file) { + parse_file = false; + p = message->bootfile; + end = p + sizeof (message->bootfile); + in_overload = true; + goto parse_start; + } else if (parse_sname) { + parse_sname = false; + p = message->servername; + end = p + sizeof (message->servername); + memset (dhcp->servername, 0, sizeof (dhcp->servername)); + in_overload = true; + goto parse_start; + } + + /* Fill in any missing fields */ + if (! dhcp->netmask.s_addr) + dhcp->netmask.s_addr = get_netmask (dhcp->address.s_addr); + if (! dhcp->broadcast.s_addr) + dhcp->broadcast.s_addr = dhcp->address.s_addr | + ~dhcp->netmask.s_addr; + + /* If we have classess static routes then we discard + * static routes and routers according to RFC 3442 */ + if (csr) { + dhcp->routes = csr; + free_route (mscsr); + free_route (routers); + free_route (routes); + } else if (mscsr) { + dhcp->routes = mscsr; + free_route (routers); + free_route (routes); + } else { + /* Ensure that we apply static routes before routers */ + if (! routes) + routes = routers; + else if (routers) + STAILQ_CONCAT (routes, routers); + dhcp->routes = routes; + } + + return (retval); +} diff --git a/customdhcpcd/src/dhcp.h b/customdhcpcd/src/dhcp.h new file mode 100644 index 0000000..ef97b75 --- /dev/null +++ b/customdhcpcd/src/dhcp.h @@ -0,0 +1,215 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef DHCP_H +#define DHCP_H + +#include +#include +#include +#include +#include + +#include "dhcpcd.h" +#include "interface.h" + +/* Max MTU - defines dhcp option length */ +#define MTU_MAX 1500 +#define MTU_MIN 576 + +/* UDP port numbers for DHCP */ +#define DHCP_SERVER_PORT 67 +#define DHCP_CLIENT_PORT 68 + +#define MAGIC_COOKIE 0x63825363 +#define BROADCAST_FLAG 0x8000 + +/* DHCP message OP code */ +#define DHCP_BOOTREQUEST 1 +#define DHCP_BOOTREPLY 2 + +/* DHCP message type */ +#define DHCP_DISCOVER 1 +#define DHCP_OFFER 2 +#define DHCP_REQUEST 3 +#define DHCP_DECLINE 4 +#define DHCP_ACK 5 +#define DHCP_NAK 6 +#define DHCP_RELEASE 7 +#define DHCP_INFORM 8 + +/* DHCP options */ +enum DHCP_OPTIONS +{ + DHCP_PAD = 0, + DHCP_NETMASK = 1, + DHCP_TIMEROFFSET = 2, + DHCP_ROUTERS = 3, + DHCP_TIMESERVER = 4, + DHCP_NAMESERVER = 5, + DHCP_DNSSERVER = 6, + DHCP_LOGSERVER = 7, + DHCP_COOKIESERVER = 8, + DHCP_HOSTNAME = 12, + DHCP_DNSDOMAIN = 15, + DHCP_ROOTPATH = 17, + DHCP_DEFAULTIPTTL = 23, + DHCP_MTU = 26, + DHCP_BROADCAST = 28, + DHCP_MASKDISCOVERY = 29, + DHCP_ROUTERDISCOVERY = 31, + DHCP_STATICROUTE = 33, + DHCP_NISDOMAIN = 40, + DHCP_NISSERVER = 41, + DHCP_NTPSERVER = 42, + DHCP_ADDRESS = 50, + DHCP_LEASETIME = 51, + DHCP_OPTIONSOVERLOADED = 52, + DHCP_MESSAGETYPE = 53, + DHCP_SERVERIDENTIFIER = 54, + DHCP_PARAMETERREQUESTLIST = 55, + DHCP_MESSAGE = 56, + DHCP_MAXMESSAGESIZE = 57, + DHCP_RENEWALTIME = 58, + DHCP_REBINDTIME = 59, + DHCP_CLASSID = 60, + DHCP_CLIENTID = 61, + DHCP_USERCLASS = 77, /* RFC 3004 */ + DHCP_FQDN = 81, + DHCP_DNSSEARCH = 119, /* RFC 3397 */ + DHCP_SIPSERVER = 120, /* RFC 3361 */ + DHCP_CSR = 121, /* RFC 3442 */ + DHCP_MSCSR = 249, /* MS code for RFC 3442 */ + DHCP_END = 255 +}; + +/* SetFQDNHostName values - lsnybble used in flags + * byte (see buildmsg.c), hsnybble to create order + * and to allow 0x00 to mean disable + */ +enum FQQN { + FQDN_DISABLE = 0x00, + FQDN_NONE = 0x18, + FQDN_PTR = 0x20, + FQDN_BOTH = 0x31 +}; + +typedef struct fqdn_t +{ + uint8_t flags; + uint8_t r1; + uint8_t r2; + char *name; +} fqdn_t; + +typedef struct dhcp_t +{ + char version[11]; + + struct in_addr serveraddress; + char serverhw[IF_NAMESIZE]; + char servername[64]; + + struct in_addr address; + struct in_addr netmask; + struct in_addr broadcast; + unsigned short mtu; + + uint32_t leasedfrom; + uint32_t leasetime; + uint32_t renewaltime; + uint32_t rebindtime; + + struct route_head *routes; + + char *hostname; + fqdn_t *fqdn; + + struct address_head *dnsservers; + char *dnsdomain; + char *dnssearch; + + struct address_head *ntpservers; + + struct address_head *nisservers; + char *nisdomain; + + char *sipservers; + + char *message; + char *rootpath; + + bool frominfo; +} dhcp_t; + +/* Sizes for DHCP options */ +#define DHCP_CHADDR_LEN 16 +#define SERVERNAME_LEN 64 +#define BOOTFILE_LEN 128 +#define DHCP_UDP_LEN (20 + 8) +#define DHCP_BASE_LEN (4 + 4 + 2 + 2 + 4 + 4 + 4 + 4 + 4) +#define DHCP_RESERVE_LEN (4 + 4 + 4 + 4 + 2) +#define DHCP_FIXED_LEN (DHCP_BASE_LEN + DHCP_CHADDR_LEN + \ + + SERVERNAME_LEN + BOOTFILE_LEN) +#define DHCP_OPTION_LEN (MTU_MAX - DHCP_FIXED_LEN - DHCP_UDP_LEN \ + - DHCP_RESERVE_LEN) + +/* Some crappy DHCP servers require the BOOTP minimum length */ +#define BOOTP_MESSAGE_LENTH_MIN 300 + +typedef struct dhcpmessage_t +{ + unsigned char op; /* message type */ + unsigned char hwtype; /* hardware address type */ + unsigned char hwlen; /* hardware address length */ + unsigned char hwopcount; /* should be zero in client message */ + uint32_t xid; /* transaction id */ + uint16_t secs; /* elapsed time in sec. from boot */ + uint16_t flags; + uint32_t ciaddr; /* (previously allocated) client IP */ + uint32_t yiaddr; /* 'your' client IP address */ + uint32_t siaddr; /* should be zero in client's messages */ + uint32_t giaddr; /* should be zero in client's messages */ + unsigned char chaddr[DHCP_CHADDR_LEN]; /* client's hardware address */ + unsigned char servername[SERVERNAME_LEN]; /* server host name */ + unsigned char bootfile[BOOTFILE_LEN]; /* boot file name */ + uint32_t cookie; + unsigned char options[DHCP_OPTION_LEN]; /* message options - cookie */ +} dhcpmessage_t; + +struct udp_dhcp_packet +{ + struct ip ip; + struct udphdr udp; + dhcpmessage_t dhcp; +}; + +ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, + uint32_t xid, char type, const options_t *options); +void free_dhcp (dhcp_t *dhcp); +int parse_dhcpmessage (dhcp_t *dhcp, const dhcpmessage_t *message); +#endif diff --git a/customdhcpcd/src/dhcpcd.c b/customdhcpcd/src/dhcpcd.c new file mode 100644 index 0000000..9f91239 --- /dev/null +++ b/customdhcpcd/src/dhcpcd.c @@ -0,0 +1,673 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +const char copyright[] = "Copyright (c) 2006-2008 Roy Marples"; + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" +#include "client.h" +#include "dhcpcd.h" +#include "dhcp.h" +#include "interface.h" +#include "logger.h" +#include "socket.h" +#include "version.h" + +#include "logwriter.h" +#include "status.h" + +static int doversion = 0; +static int dohelp = 0; +#define EXTRA_OPTS +static const struct option longopts[] = { + {"arp", no_argument, NULL, 'a'}, + {"script", required_argument, NULL, 'c'}, + {"debug", no_argument, NULL, 'd'}, + {"hostname", optional_argument, NULL, 'h'}, + {"classid", optional_argument, NULL, 'i'}, + {"release", no_argument, NULL, 'k'}, + {"leasetime", required_argument, NULL, 'l'}, + {"metric", required_argument, NULL, 'm'}, + {"renew", no_argument, NULL, 'n'}, + {"persistent", no_argument, NULL, 'p'}, + {"qtsocketaddress", required_argument, NULL, 'q'}, + {"inform", optional_argument, NULL, 's'}, + {"request", optional_argument, NULL, 'r'}, + {"timeout", required_argument, NULL, 't'}, + {"userclass", required_argument, NULL, 'u'}, + {"exit", no_argument, NULL, 'x'}, + {"lastlease", no_argument, NULL, 'E'}, + {"fqdn", required_argument, NULL, 'F'}, + {"nogateway", no_argument, NULL, 'G'}, + {"sethostname", no_argument, NULL, 'H'}, + {"clientid", optional_argument, NULL, 'I'}, + {"noipv4ll", no_argument, NULL, 'L'}, + {"nomtu", no_argument, NULL, 'M'}, + {"nontp", no_argument, NULL, 'N'}, + {"nodns", no_argument, NULL, 'R'}, + {"msscr", no_argument, NULL, 'S'}, + {"test", no_argument, NULL, 'T'}, + {"nonis", no_argument, NULL, 'Y'}, + {"help", no_argument, &dohelp, 1}, + {"version", no_argument, &doversion, 1}, +#ifdef THERE_IS_NO_FORK + {"daemonised", no_argument, NULL, 'f'}, + {"skiproutes", required_argument, NULL, 'g'}, +#endif + {NULL, 0, NULL, 0} +}; + +#ifdef THERE_IS_NO_FORK +char dhcpcd[PATH_MAX]; +char **dhcpcd_argv = NULL; +int dhcpcd_argc = 0; +char *dhcpcd_skiproutes = NULL; +#undef EXTRA_OPTS +#define EXTRA_OPTS "fg:" +#endif + +static int atoint (const char *s) +{ + char *t; + long n; + + errno = 0; + n = strtol (s, &t, 0); + if ((errno != 0 && n == 0) || s == t || + (errno == ERANGE && (n == LONG_MAX || n == LONG_MIN))) + { + logger (LOG_ERR, "`%s' out of range", s); + return (-1); + } + + return ((int) n); +} + +static pid_t read_pid (const char *pidfile) +{ + FILE *fp; + pid_t pid = 0; + + if ((fp = fopen (pidfile, "r")) == NULL) { + errno = ENOENT; + return 0; + } + + fscanf (fp, "%d", &pid); + fclose (fp); + + + return (pid); +} + +static void usage (void) +{ + printf ("usage: "PACKAGE" [-adknpEGHMNRSTY] [-c script] [-h hostname] [-i classID]\n" + " [-l leasetime] [-m metric] [-r ipaddress] [-s ipaddress]\n" + " [-t timeout] [-u userclass] [-F none | ptr | both]\n" + " [-I clientID] [-q qtsocketaddress] \n"); +} + + +int main (int argc, char **argv) +{ + options_t *options; + int userclasses = 0; + int opt; + int option_index = 0; + char *prefix; + pid_t pid; + int debug = 0; + int i; + int pidfd = -1; + int sig = 0; + int retval = EXIT_FAILURE; + + /* Close any un-needed fd's */ + for (i = getdtablesize() - 1; i >= 3; --i) + close (i); + + openlog (PACKAGE, LOG_PID, LOG_LOCAL0); + + options = xzalloc (sizeof (*options)); + options->script = (char *) DEFAULT_SCRIPT; + snprintf (options->classid, CLASS_ID_MAX_LEN, "%s %s", + PACKAGE, VERSION); + + options->doarp = true; + options->dodns = true; + options->domtu = true; + options->donis = true; + options->dontp = true; + options->dogateway = true; + options->daemonise = true; + options->doinform = false; + options->doipv4ll = true; + options->doduid = true; + options->timeout = DEFAULT_TIMEOUT; + /* added by Niklas Goby, additional field, storing the socket address path for + * communicating with Qt "server" + * defined in dhcpcd.h */ + strcpy(options->qtsocketaddress, DEFAULT_QTSOCKETADDRESS); + + gethostname (options->hostname, sizeof (options->hostname)); + if (strcmp (options->hostname, "(none)") == 0 || + strcmp (options->hostname, "localhost") == 0) + memset (options->hostname, 0, sizeof (options->hostname)); + + + /* Don't set any optional arguments here so we retain POSIX + * compatibility with getopt */ + while ((opt = getopt_long(argc, argv, EXTRA_OPTS + "c:dh:i:kl:m:npq:r:s:t:u:xAEF:GHI:LMNRSTY", + longopts, &option_index)) != -1) + { + switch (opt) { + case 0: + if (longopts[option_index].flag) + break; + logger (LOG_ERR, + "option `%s' should set a flag", + longopts[option_index].name); + goto abort; + case 'c': + options->script = optarg; + break; + case 'd': + debug++; + switch (debug) { + case 1: + setloglevel (LOG_DEBUG); + break; + case 2: + options->daemonise = false; + break; + } + break; + #ifdef THERE_IS_NO_FORK + case 'f': + options->daemonised = true; + close_fds (); + break; + case 'g': + dhcpcd_skiproutes = xstrdup (optarg); + break; + #endif + case 'h': + if (! optarg) + *options->hostname = '\0'; + else if (strlen (optarg) > MAXHOSTNAMELEN) { + logger (LOG_ERR, + "`%s' too long for HostName string, max is %d", + optarg, MAXHOSTNAMELEN); + goto abort; + } else + strlcpy (options->hostname, optarg, + sizeof (options->hostname)); + break; + case 'i': + if (! optarg) { + *options->classid = '\0'; + } else if (strlen (optarg) > CLASS_ID_MAX_LEN) { + logger (LOG_ERR, + "`%s' too long for ClassID string, max is %d", + optarg, CLASS_ID_MAX_LEN); + goto abort; + } else + strlcpy (options->classid, optarg, + sizeof (options->classid)); + break; + case 'k': + sig = SIGHUP; + break; + case 'l': + if (*optarg == '-') { + logger (LOG_ERR, + "leasetime must be a positive value"); + goto abort; + } + errno = 0; + options->leasetime = (uint32_t) strtol (optarg, NULL, 0); + if (errno == EINVAL || errno == ERANGE) { + logger (LOG_ERR, "`%s' out of range", optarg); + goto abort; + } + break; + case 'm': + options->metric = atoint (optarg); + if (options->metric < 0) { + logger (LOG_ERR, + "metric must be a positive value"); + goto abort; + } + break; + case 'n': + sig = SIGALRM; + break; + case 'p': + options->persistent = true; + break; + case 'q': + if (strlen(optarg) > QTSOCKETADDRESSLENGTH) { + logger(LOG_ERR, "`%s' too long for an socket address path (max=%d)", + optarg, QTSOCKETADDRESSLENGTH); + goto abort; + } + strlcpy(options->qtsocketaddress, optarg, sizeof(options->qtsocketaddress)); + break; + case 's': + options->doinform = true; + options->doarp = false; + if (! optarg || strlen (optarg) == 0) { + options->request_address.s_addr = 0; + break; + } else { + char *slash = strchr (optarg, '/'); + if (slash) { + int cidr; + /* nullify the slash, so the -r option can read the + * address */ + *slash++ = '\0'; + if (sscanf (slash, "%d", &cidr) != 1 || + inet_cidrtoaddr (cidr, &options->request_netmask) != 0) { + logger (LOG_ERR, "`%s' is not a valid CIDR", slash); + goto abort; + } + } + } + /* FALLTHROUGH */ + case 'r': + if (! options->doinform) + options->dorequest = true; + if (strlen (optarg) > 0 && + ! inet_aton (optarg, &options->request_address)) + { + logger (LOG_ERR, "`%s' is not a valid IP address", optarg); + goto abort; + } + break; + case 't': + options->timeout = atoint (optarg); + if (options->timeout < 0) { + logger (LOG_ERR, "timeout must be a positive value"); + goto abort; + } + break; + case 'u': + { + int offset = 0; + for (i = 0; i < userclasses; i++) + offset += (int) options->userclass[offset] + 1; + if (offset + 1 + strlen (optarg) > USERCLASS_MAX_LEN) { + logger (LOG_ERR, "userclass overrun, max is %d", + USERCLASS_MAX_LEN); + goto abort; + } + userclasses++; + memcpy (options->userclass + offset + 1 , optarg, strlen (optarg)); + options->userclass[offset] = strlen (optarg); + options->userclass_len += (strlen (optarg)) + 1; + } + break; + case 'x': + sig = SIGTERM; + break; + case 'A': + #ifndef ENABLE_ARP + logger (LOG_ERR, + "arp not compiled into dhcpcd"); + goto abort; + #endif + options->doarp = false; + break; + case 'E': + #ifndef ENABLE_INFO + logger (LOG_ERR, + "info not compiled into dhcpcd"); + goto abort; + #endif + options->dolastlease = true; + break; + case 'F': + if (strncmp (optarg, "none", strlen (optarg)) == 0) + options->fqdn = FQDN_NONE; + else if (strncmp (optarg, "ptr", strlen (optarg)) == 0) + options->fqdn = FQDN_PTR; + else if (strncmp (optarg, "both", strlen (optarg)) == 0) + options->fqdn = FQDN_BOTH; + else { + logger (LOG_ERR, "invalid value `%s' for FQDN", optarg); + goto abort; + } + break; + case 'G': + options->dogateway = false; + break; + case 'H': + options->dohostname++; + break; + case 'I': + if (optarg) { + if (strlen (optarg) > CLIENT_ID_MAX_LEN) { + logger (LOG_ERR, "`%s' is too long for ClientID, max is %d", + optarg, CLIENT_ID_MAX_LEN); + goto abort; + } + if (strlcpy (options->clientid, optarg, + sizeof (options->clientid)) == 0) + /* empty string disabled duid */ + options->doduid = false; + + } else { + memset (options->clientid, 0, sizeof (options->clientid)); + options->doduid = false; + } + break; + case 'L': + options->doipv4ll = false; + break; + case 'M': + options->domtu = false; + break; + case 'N': + options->dontp = false; + break; + case 'R': + options->dodns = false; + break; + case 'S': + options->domscsr++; + break; + case 'T': + #ifndef ENABLE_INFO + logger (LOG_ERR, "info support not compiled into dhcpcd"); + goto abort; + #endif + options->test = true; + options->persistent = true; + break; + case 'Y': + options->donis = false; + break; + case '?': + usage (); + goto abort; + default: + usage (); + goto abort; + } + } + if (doversion) { + printf (""PACKAGE" "VERSION"\n"); + printf ("Compile time options:" + #ifdef ENABLE_ARP + " ARP" + #endif + #ifdef ENABLE_DUID + " DUID" + #endif + #ifdef ENABLE_INFO + " INFO" + #endif + #ifdef ENABLE_INFO_COMPAT + " INFO_COMPAT" + #endif + #ifdef ENABLE_IPV4LL + " IPV4LL" + #endif + #ifdef ENABLE_NIS + " NIS" + #endif + #ifdef ENABLE_NTP + " NTP" + #endif + #ifdef SERVICE + " " SERVICE + #endif + #ifdef ENABLE_RESOLVCONF + " RESOLVCONF" + #endif + #ifdef THERE_IS_NO_FORK + " THERE_IS_NO_FORK" + #endif + "\n"); + } + + if (dohelp) + usage (); + + +#ifdef THERE_IS_NO_FORK + dhcpcd_argv = argv; + dhcpcd_argc = argc; + if (! realpath (argv[0], dhcpcd)) { + logger (LOG_ERR, "unable to resolve the path `%s': %s", + argv[0], strerror (errno)); + goto abort; + } +#endif + + /* initializations for the ipc connection to qt*/ + setSocketName(options->qtsocketaddress); + if (initQtLoggerSocket() < 0) { + logger(LOG_ERR, "initialization Qt Logger failed: %s ", strerror (errno)); + goto abort; + } + + + if (optind < argc) { + if (strlen(argv[optind]) > IF_NAMESIZE) { + logger(LOG_ERR, "`%s' too long for an interface name (max=%d)", + argv[optind], IF_NAMESIZE); + goto abort; + } + strlcpy(options->interface, argv[optind], sizeof(options->interface)); + setInterfaceName(options->interface); + } else { + /* If only version was requested then exit now */ + if (doversion || dohelp) { + retval = 0; + goto abort; + } + + logger(LOG_ERR, "no interface specified"); + setInterfaceName("no_if"); + goto abort; + } + + if (strchr(options->hostname, '.')) { + if (options->fqdn == FQDN_DISABLE) + options->fqdn = FQDN_BOTH; + } else + options->fqdn = FQDN_DISABLE; + + if (options->request_address.s_addr == 0 && options->doinform) { + if ((options->request_address.s_addr = get_address(options->interface)) + != 0) + options->keep_address = true; + } + + if (IN_LINKLOCAL (ntohl (options->request_address.s_addr))) { + logger (LOG_ERR, + "you are not allowed to request a link local address"); + logToQt(LOG_ERR, -1, "you are not allowed to request a link local address"); + goto abort; + } + + if (geteuid ()) { + logger (LOG_WARNING, PACKAGE " will not work correctly unless" + " run as root"); + } + + prefix = xmalloc (sizeof (char) * (IF_NAMESIZE + 3)); + snprintf (prefix, IF_NAMESIZE, "%s: ", options->interface); + setlogprefix (prefix); + snprintf (options->pidfile, sizeof (options->pidfile), PIDFILE, + options->interface); + free (prefix); + + chdir ("/"); + umask (022); + + if (mkdir (INFODIR, S_IRUSR | S_IWUSR |S_IXUSR | S_IRGRP | S_IXGRP + | S_IROTH | S_IXOTH) && errno != EEXIST) + { + logger (LOG_ERR, + "mkdir(\"%s\",0): %s\n", INFODIR, strerror (errno)); + goto abort; + } + + if (mkdir (ETCDIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP + | S_IROTH | S_IXOTH) && errno != EEXIST) + { + logger (LOG_ERR, + "mkdir(\"%s\",0): %s\n", ETCDIR, strerror (errno)); + goto abort; + } + + if (options->test) { + if (options->dorequest || options->doinform) { + logger (LOG_ERR, + "cannot test with --inform or --request"); + goto abort; + } + + if (options->dolastlease) { + logger (LOG_ERR, "cannot test with --lastlease"); + goto abort; + } + + if (sig != 0) { + logger (LOG_ERR, + "cannot test with --release or --renew"); + goto abort; + } + } + + if (sig != 0) { + int killed = -1; + pid = read_pid (options->pidfile); + if (pid != 0) + logger (LOG_INFO, "sending signal %d to pid %d", + sig, pid); + + if (! pid || (killed = kill (pid, sig))) + logger (sig == SIGALRM ? LOG_INFO : LOG_ERR, + ""PACKAGE" not running"); + + if (pid != 0 && (sig != SIGALRM || killed != 0)) + unlink (options->pidfile); + + if (killed == 0) { + retval = EXIT_SUCCESS; + goto abort; + } + + if (sig != SIGALRM) + goto abort; + } + + if (! options->test && ! options->daemonised) { + if ((pid = read_pid (options->pidfile)) > 0 && + kill (pid, 0) == 0) + { + logger (LOG_ERR, ""PACKAGE + " already running on pid %d (%s)", + pid, options->pidfile); + goto abort; + } + + pidfd = open (options->pidfile, + O_WRONLY | O_CREAT | O_NONBLOCK, 0664); + if (pidfd == -1) { + logger (LOG_ERR, "open `%s': %s", + options->pidfile, strerror (errno)); + goto abort; + } + + /* Lock the file so that only one instance of dhcpcd runs + * on an interface */ + if (flock (pidfd, LOCK_EX | LOCK_NB) == -1) { + logger (LOG_ERR, "flock `%s': %s", + options->pidfile, strerror (errno)); + goto abort; + } + + /* dhcpcd.sh should not interhit this fd */ + if ((i = fcntl (pidfd, F_GETFD, 0)) == -1 || + fcntl (pidfd, F_SETFD, i | FD_CLOEXEC) == -1) + logger (LOG_ERR, "fcntl: %s", strerror (errno)); + + writepid (pidfd, getpid ()); + logger (LOG_INFO, PACKAGE " " VERSION " starting"); + } + + /* Seed random */ + srandomdev (); + + /* Massage our filters per platform */ + setup_packet_filters (); + + /* dhcp_run : defined in client.c */ + if (dhcp_run (options, &pidfd) == 0) + retval = EXIT_SUCCESS; + +abort: + /* If we didn't daemonise then we need to punt the pidfile now */ + if (pidfd > -1) { + close (pidfd); + unlink (options->pidfile); + } + + free (options); + +#ifdef THERE_IS_NO_FORK + /* There may have been an error before the dhcp_run function + * clears this, so just do it here to be safe */ + free (dhcpcd_skiproutes); +#endif + + logger (LOG_INFO, "exiting"); + logToQt(LOG_INFO, DHCPCD_EXIT, "exiting due abort"); + closeQtLoggerSocket(); + exit (retval); + /* NOTREACHED */ +} diff --git a/customdhcpcd/src/dhcpcd.h b/customdhcpcd/src/dhcpcd.h new file mode 100644 index 0000000..7deb5b7 --- /dev/null +++ b/customdhcpcd/src/dhcpcd.h @@ -0,0 +1,105 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef DHCPCD_H +#define DHCPCD_H + +#include +#include +#include +#include +#include +#include + +#include "common.h" + +#define DEFAULT_TIMEOUT 20 +#define DEFAULT_LEASETIME 3600 /* 1 hour */ + +/* added by Niklas Goby, additional field, storing the socket address path for + * communicating with Qt "server" */ +#define DEFAULT_QTSOCKETADDRESS "/var/tmp/qt_c_socket_default" +#define QTSOCKETADDRESSLENGTH 255 + +#define CLASS_ID_MAX_LEN 48 +#define CLIENT_ID_MAX_LEN 48 +#define USERCLASS_MAX_LEN 255 + +#ifdef THERE_IS_NO_FORK +extern char dhcpcd[PATH_MAX]; +extern char **dhcpcd_argv; +extern int dhcpcd_argc; +extern char *dhcpcd_skiproutes; +#endif + +typedef struct options_t { + /* added by Niklas Goby, additional field, storing the socket address path for + * communicating with Qt "server" */ + char qtsocketaddress[QTSOCKETADDRESSLENGTH]; + /*----*/ + char interface[IF_NAMESIZE]; + char hostname[MAXHOSTNAMELEN]; + int fqdn; + char classid[CLASS_ID_MAX_LEN]; + char clientid[CLIENT_ID_MAX_LEN]; + char userclass[USERCLASS_MAX_LEN]; + size_t userclass_len; + uint32_t leasetime; + time_t timeout; + int metric; + + bool doarp; + bool dodns; + bool dodomainname; + bool dogateway; + int dohostname; + bool domtu; + bool donis; + bool dontp; + bool dolastlease; + bool doinform; + bool dorequest; + bool doipv4ll; + bool doduid; + int domscsr; + + struct in_addr request_address; + struct in_addr request_netmask; + + bool persistent; + bool keep_address; + bool daemonise; + bool daemonised; + bool test; + + char *script; + char pidfile[PATH_MAX]; +} options_t; + +int nd_main (char *ifname); + +#endif diff --git a/customdhcpcd/src/dhcpcd.sh b/customdhcpcd/src/dhcpcd.sh new file mode 100644 index 0000000..8c86aac --- /dev/null +++ b/customdhcpcd/src/dhcpcd.sh @@ -0,0 +1,46 @@ +#!/bin/sh +# +# This is a sample /etc/dhcpcd.sh script. +# /etc/dhcpcd.sh script is executed by dhcpcd daemon +# any time it configures or shuts down interface. +# The following parameters are passed to dhcpcd.exe script: +# $1 = HostInfoFilePath, e.g "/var/lib/dhcpcd/dhcpcd-eth0.info" +# $2 = "up" if interface has been configured with the same +# IP address as before reboot; +# $2 = "down" if interface has been shut down; +# $2 = "new" if interface has been configured with new IP address; +# +# Sanity checks + +if [ $# -lt 2 ]; then + logger -s -p local0.err -t dhcpcd.sh "wrong usage" + exit 1 +fi + +hostinfo="$1" +state="$2" + +# Reading HostInfo file for configuration parameters +[ -e "${hostinfo}" ] && . "${hostinfo}" + +case "${state}" in + up) + logger -s -p local0.info -t dhcpcd.sh \ + "interface ${INTERFACE} has been configured with old IP=${IPADDR}" + # Put your code here for when the interface has been brought up with an + # old IP address here + ;; + + new) + logger -s -p local0.info -t dhcpcd.sh \ + "interface ${INTERFACE} has been configured with new IP=${IPADDR}" + # Put your code here for when the interface has been brought up with a + # new IP address + ;; + + down) logger -s -p local0.info -t dhcpcd.sh \ + "interface ${INTERFACE} has been brought down" + # Put your code here for the when the interface has been shut down + ;; +esac +exit 0 diff --git a/customdhcpcd/src/discover.c b/customdhcpcd/src/discover.c new file mode 100644 index 0000000..eae7b0a --- /dev/null +++ b/customdhcpcd/src/discover.c @@ -0,0 +1,7 @@ +/* + * discover.c + * + * Created on: Jul 7, 2011 + * Author: niklas + */ + diff --git a/customdhcpcd/src/discover.h b/customdhcpcd/src/discover.h new file mode 100644 index 0000000..1f4918d --- /dev/null +++ b/customdhcpcd/src/discover.h @@ -0,0 +1,12 @@ +/* + * discover.h + * + * Created on: Jul 7, 2011 + * Author: niklas + */ + +#ifndef DISCOVER_H_ +#define DISCOVER_H_ + + +#endif /* DISCOVER_H_ */ diff --git a/customdhcpcd/src/duid.c b/customdhcpcd/src/duid.c new file mode 100644 index 0000000..e4dd83b --- /dev/null +++ b/customdhcpcd/src/duid.c @@ -0,0 +1,118 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" +#include "common.h" +#include "duid.h" +#include "logger.h" + +#ifdef ENABLE_DUID + +#define THIRTY_YEARS_IN_SECONDS 946707779 + +size_t get_duid (unsigned char *duid, const interface_t *iface) +{ + FILE *f; + uint16_t type = 0; + uint16_t hw = 0; + uint32_t ul; + time_t t; + int x = 0; + unsigned char *p = duid; + size_t len = 0; + + if (! iface) + return (0); + + /* If we already have a DUID then use it as it's never supposed + * to change once we have one even if the interfaces do */ + if ((f = fopen (DUIDFILE, "r"))) { + char *line = get_line (f); + if (line) { + len = hwaddr_aton (NULL, line); + if (len && len <= DUID_LEN) + hwaddr_aton (duid, line); + free (line); + } + fclose (f); + if (len) + return (len); + } else { + if (errno != ENOENT) { + logger (LOG_ERR, "fopen `%s': %s", + DUIDFILE, strerror (errno)); + return (0); + } + } + + /* No file? OK, lets make one based on our interface */ + type = htons (1); /* DUI-D-LLT */ + memcpy (p, &type, 2); + p += 2; + + hw = htons (iface->family); + memcpy (p, &hw, 2); + p += 2; + + /* time returns seconds from jan 1 1970, but DUID-LLT is + * seconds from jan 1 2000 modulo 2^32 */ + t = time (NULL) - THIRTY_YEARS_IN_SECONDS; + ul = htonl (t & 0xffffffff); + memcpy (p, &ul, 4); + p += 4; + + /* Finally, add the MAC address of the interface */ + memcpy (p, iface->hwaddr, iface->hwlen); + p += iface->hwlen; + + len = p - duid; + + if (! (f = fopen (DUIDFILE, "w"))) + logger (LOG_ERR, "fopen `%s': %s", DUIDFILE, strerror (errno)); + else { + x = fprintf (f, "%s\n", hwaddr_ntoa (duid, len)); + fclose (f); + } + + /* Failed to write the duid? scrub it, we cannot use it */ + if (x < 1) { + len = 0; + unlink (DUIDFILE); + } + + return (len); +} +#endif diff --git a/customdhcpcd/src/duid.h b/customdhcpcd/src/duid.h new file mode 100644 index 0000000..1492990 --- /dev/null +++ b/customdhcpcd/src/duid.h @@ -0,0 +1,42 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef DUID_H +#define DUID_H + +#include "config.h" + +#ifdef ENABLE_DUID +#ifndef DUID_LEN +# define DUID_LEN 128 + 2 +#endif + +#include "interface.h" + +size_t get_duid (unsigned char *duid, const interface_t *iface); +#endif +#endif diff --git a/customdhcpcd/src/info.c b/customdhcpcd/src/info.c new file mode 100644 index 0000000..8369b43 --- /dev/null +++ b/customdhcpcd/src/info.c @@ -0,0 +1,472 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include + +#include +#include +#include +#include +#include + +#include "config.h" +#include "common.h" +#include "dhcp.h" +#include "interface.h" +#include "logger.h" +#include "info.h" + +#ifdef ENABLE_INFO + +/* Create a malloced string of cstr, changing ' to '\'' + * so the contents work in a shell */ +static char *cleanmetas (const char *cstr) +{ + const char *p = cstr; + char *new; + char *n; + size_t len; + + if (cstr == NULL || (len = strlen (cstr)) == 0) + return (xstrdup ("")); + + n = new = xmalloc (sizeof (char) * len + 2); + do + if (*p == '\'') { + size_t pos = n - new; + len += 4; + new = xrealloc (new, sizeof (char) * len + 1); + n = new + pos; + *n++ = '\''; + *n++ = '\\'; + *n++ = '\''; + *n++ = '\''; + } else + *n++ = *p; + while (*p++); + + /* Terminate the sucker */ + *n = '\0'; + + return (new); +} + + +static void print_addresses (FILE *f, const struct address_head *addresses) +{ + const address_t *addr; + + STAILQ_FOREACH (addr, addresses, entries) { + fprintf (f, "%s", inet_ntoa (addr->address)); + if (STAILQ_NEXT (addr, entries)) + fprintf (f, " "); + } +} + +static void print_clean (FILE *f, const char *name, const char *value) +{ + char *clean; + + if (! value) + return; + + clean = cleanmetas (value); + fprintf (f, "%s='%s'\n", name, clean); + free (clean); +} + +bool write_info(const interface_t *iface, const dhcp_t *dhcp, + const options_t *options, bool overwrite) +{ + FILE *f; + route_t *route; + struct stat sb; + + if (options->test) + f = stdout; + else { + if (! overwrite && stat (iface->infofile, &sb) == 0) + return (true); + + logger (LOG_DEBUG, "writing %s", iface->infofile); + if ((f = fopen (iface->infofile, "w")) == NULL) { + logger (LOG_ERR, "fopen `%s': %s", + iface->infofile, strerror (errno)); + return (false); + } + } + + if (dhcp->address.s_addr) { + struct in_addr n; + n.s_addr = dhcp->address.s_addr & dhcp->netmask.s_addr; + fprintf (f, "IPADDR='%s'\n", inet_ntoa (dhcp->address)); + fprintf (f, "NETMASK='%s'\n", inet_ntoa (dhcp->netmask)); + fprintf (f, "NETWORK='%s'\n", inet_ntoa (n)); + fprintf (f, "BROADCAST='%s'\n", inet_ntoa (dhcp->broadcast)); + } + if (dhcp->mtu > 0) + fprintf (f, "MTU='%d'\n", dhcp->mtu); + + if (dhcp->routes) { + bool doneone = false; + fprintf (f, "ROUTES='"); + STAILQ_FOREACH (route, dhcp->routes, entries) { + if (route->destination.s_addr != 0) { + if (doneone) + fprintf (f, " "); + fprintf (f, "%s", inet_ntoa (route->destination)); + fprintf (f, ",%s", inet_ntoa (route->netmask)); + fprintf (f, ",%s", inet_ntoa (route->gateway)); + doneone = true; + } + } + fprintf (f, "'\n"); + + doneone = false; + fprintf (f, "GATEWAYS='"); + STAILQ_FOREACH (route, dhcp->routes, entries) { + if (route->destination.s_addr == 0) { + if (doneone) + fprintf (f, " "); + fprintf (f, "%s", inet_ntoa (route->gateway)); + doneone = true; + } + } + fprintf (f, "'\n"); + } + + print_clean (f, "HOSTNAME", dhcp->hostname); + print_clean (f, "DNSDOMAIN", dhcp->dnsdomain); + print_clean (f, "DNSSEARCH", dhcp->dnssearch); + + if (dhcp->dnsservers) { + fprintf (f, "DNSSERVERS='"); + print_addresses (f, dhcp->dnsservers); + fprintf (f, "'\n"); + } + + if (dhcp->fqdn) { + fprintf (f, "FQDNFLAGS='%u'\n", dhcp->fqdn->flags); + fprintf (f, "FQDNRCODE1='%u'\n", dhcp->fqdn->r1); + fprintf (f, "FQDNRCODE2='%u'\n", dhcp->fqdn->r2); + print_clean (f, "FQDNHOSTNAME", dhcp->fqdn->name); + } + + if (dhcp->ntpservers) { + fprintf (f, "NTPSERVERS='"); + print_addresses (f, dhcp->ntpservers); + fprintf (f, "'\n"); + } + + print_clean (f, "NISDOMAIN", dhcp->nisdomain); + if (dhcp->nisservers) { + fprintf (f, "NISSERVERS='"); + print_addresses (f, dhcp->nisservers); + fprintf (f, "'\n"); + } + + print_clean (f, "ROOTPATH", dhcp->rootpath); + print_clean (f, "SIPSERVERS", dhcp->sipservers); + + if (dhcp->serveraddress.s_addr) + fprintf (f, "DHCPSID='%s'\n", inet_ntoa (dhcp->serveraddress)); + if (dhcp->servername[0]) + print_clean (f, "DHCPSNAME", dhcp->servername); + + if (! options->doinform && dhcp->address.s_addr) { + if (! options->test) + fprintf (f, "LEASEDFROM='%u'\n", dhcp->leasedfrom); + fprintf (f, "LEASETIME='%u'\n", dhcp->leasetime); + fprintf (f, "RENEWALTIME='%u'\n", dhcp->renewaltime); + fprintf (f, "REBINDTIME='%u'\n", dhcp->rebindtime); + } + print_clean (f, "INTERFACE", iface->name); + print_clean (f, "CLASSID", options->classid); + if (iface->clientid_len > 0) { + fprintf (f, "CLIENTID='%s'\n", + hwaddr_ntoa (iface->clientid, iface->clientid_len)); + } + fprintf (f, "DHCPCHADDR='%s'\n", hwaddr_ntoa (iface->hwaddr, + iface->hwlen)); + +#ifdef ENABLE_INFO_COMPAT + /* Support the old .info settings if we need to */ + fprintf (f, "\n# dhcpcd-1.x and 2.x compatible variables\n"); + if (dhcp->dnsservers) { + address_t *addr; + + fprintf (f, "DNS='"); + STAILQ_FOREACH (addr, dhcp->dnsservers, entries) { + fprintf (f, "%s", inet_ntoa (addr->address)); + if (STAILQ_NEXT (addr, entries)) + fprintf (f, ","); + } + fprintf (f, "'\n"); + } + + if (dhcp->routes) { + bool doneone = false; + fprintf (f, "GATEWAY='"); + STAILQ_FOREACH (route, dhcp->routes, entries) { + if (route->destination.s_addr == 0) { + if (doneone) + fprintf (f, ","); + fprintf (f, "%s", inet_ntoa (route->gateway)); + doneone = true; + } + } + fprintf (f, "'\n"); + } +#endif + + if (! options->test) + fclose (f); + return (true); +} + +static bool parse_address (struct in_addr *addr, + const char *value, const char *var) +{ + if (inet_aton (value, addr) == 0) { + logger (LOG_ERR, "%s `%s': %s", var, value, + strerror (errno)); + return (false); + } + return (true); +} + +static bool parse_uint (unsigned int *i, + const char *value, const char *var) +{ + if (sscanf (value, "%u", i) != 1) { + logger (LOG_ERR, "%s `%s': not a valid number", + var, value); + return (false); + } + return (true); +} + +static bool parse_ushort (unsigned short *s, + const char *value, const char *var) +{ + if (sscanf (value, "%hu", s) != 1) { + logger (LOG_ERR, "%s `%s': not a valid number", + var, value); + return (false); + } + return (true); +} + +static struct address_head *parse_addresses (char *value, const char *var) +{ + char *token; + char *p = value; + struct address_head *head = NULL; + + while ((token = strsep (&p, " "))) { + address_t *a = xzalloc (sizeof (*a)); + + if (inet_aton (token, &a->address) == 0) { + logger (LOG_ERR, "%s: invalid address `%s'", var, token); + free_address (head); + free (a); + return (NULL); + } + + if (! head) { + head = xmalloc (sizeof (*head)); + STAILQ_INIT (head); + } + STAILQ_INSERT_TAIL (head, a, entries); + } + + return (head); +} + +bool read_info (const interface_t *iface, dhcp_t *dhcp) +{ + FILE *fp; + char *line; + char *var; + char *value; + char *p; + struct stat sb; + + if (stat (iface->infofile, &sb) != 0) { + logger (LOG_ERR, "lease information file `%s' does not exist", + iface->infofile); + return (false); + } + + if (! (fp = fopen (iface->infofile, "r"))) { + logger (LOG_ERR, "fopen `%s': %s", + iface->infofile, strerror (errno)); + return (false); + } + + dhcp->frominfo = true; + + while ((line = get_line (fp))) { + var = line; + + /* Strip leading spaces/tabs */ + while ((*var == ' ') || (*var == '\t')) + var++; + + /* Trim trailing \n */ + p = var + strlen (var) - 1; + if (*p == '\n') + *p = 0; + + /* Skip comments */ + if (*var == '#') + goto next; + + /* If we don't have an equals sign then skip it */ + if (! (p = strchr (var, '='))) + goto next; + + /* Terminate the = so we have two strings */ + *p = 0; + + value = p + 1; + /* Strip leading and trailing quotes if present */ + if (*value == '\'' || *value == '"') + value++; + p = value + strlen (value) - 1; + if (*p == '\'' || *p == '"') + *p = 0; + + /* Don't process null vars or values */ + if (! *var || ! *value) + goto next; + + if (strcmp (var, "IPADDR") == 0) + parse_address (&dhcp->address, value, "IPADDR"); + else if (strcmp (var, "NETMASK") == 0) + parse_address (&dhcp->netmask, value, "NETMASK"); + else if (strcmp (var, "BROADCAST") == 0) + parse_address (&dhcp->broadcast, value, "BROADCAST"); + else if (strcmp (var, "MTU") == 0) + parse_ushort (&dhcp->mtu, value, "MTU"); + else if (strcmp (var, "ROUTES") == 0) { + p = value; + while ((value = strsep (&p, " "))) { + char *pp = value; + char *dest = strsep (&pp, ","); + char *net = strsep (&pp, ","); + char *gate = strsep (&pp, ","); + route_t *route; + + if (! dest || ! net || ! gate) { + logger (LOG_ERR, "read_info ROUTES `%s,%s,%s': invalid route", + dest, net, gate); + goto next; + } + + /* See if we can create a route */ + route = xzalloc (sizeof (*route)); + if (inet_aton (dest, &route->destination) == 0) { + logger (LOG_ERR, "read_info ROUTES `%s': not a valid destination address", + dest); + free (route); + goto next; + } + if (inet_aton (dest, &route->netmask) == 0) { + logger (LOG_ERR, "read_info ROUTES `%s': not a valid netmask address", + net); + free (route); + goto next; + } + if (inet_aton (dest, &route->gateway) == 0) { + logger (LOG_ERR, "read_info ROUTES `%s': not a valid gateway address", + gate); + free (route); + goto next; + } + + /* OK, now add our route */ + if (! dhcp->routes) { + dhcp->routes = xmalloc (sizeof (*dhcp->routes)); + STAILQ_INIT (dhcp->routes); + } + STAILQ_INSERT_TAIL (dhcp->routes, route, entries); + } + } else if (strcmp (var, "GATEWAYS") == 0) { + p = value; + while ((value = strsep (&p, " "))) { + route_t *route = xzalloc (sizeof (*route)); + if (parse_address (&route->gateway, value, "GATEWAYS")) { + if (! dhcp->routes) { + dhcp->routes = xmalloc (sizeof (*dhcp->routes)); + STAILQ_INIT (dhcp->routes); + } + STAILQ_INSERT_TAIL (dhcp->routes, route, entries); + } else + free (route); + } + } else if (strcmp (var, "HOSTNAME") == 0) + dhcp->hostname = xstrdup (value); + else if (strcmp (var, "DNSDOMAIN") == 0) + dhcp->dnsdomain = xstrdup (value); + else if (strcmp (var, "DNSSEARCH") == 0) + dhcp->dnssearch = xstrdup (value); + else if (strcmp (var, "DNSSERVERS") == 0) + dhcp->dnsservers = parse_addresses (value, "DNSSERVERS"); + else if (strcmp (var, "NTPSERVERS") == 0) + dhcp->ntpservers = parse_addresses (value, "NTPSERVERS"); + else if (strcmp (var, "NISDOMAIN") == 0) + dhcp->nisdomain = xstrdup (value); + else if (strcmp (var, "NISSERVERS") == 0) + dhcp->nisservers = parse_addresses (value, "NISSERVERS"); + else if (strcmp (var, "ROOTPATH") == 0) + dhcp->rootpath = xstrdup (value); + else if (strcmp (var, "DHCPSID") == 0) + parse_address (&dhcp->serveraddress, value, "DHCPSID"); + else if (strcmp (var, "DHCPSNAME") == 0) + strlcpy (dhcp->servername, value, sizeof (dhcp->servername)); + else if (strcmp (var, "LEASEDFROM") == 0) + parse_uint (&dhcp->leasedfrom, value, "LEASEDFROM"); + else if (strcmp (var, "LEASETIME") == 0) + parse_uint (&dhcp->leasetime, value, "LEASETIME"); + else if (strcmp (var, "RENEWALTIME") == 0) + parse_uint (&dhcp->renewaltime, value, "RENEWALTIME"); + else if (strcmp (var, "REBINDTIME") == 0) + parse_uint (&dhcp->rebindtime, value, "REBINDTIME"); + +next: + free (line); + } + + fclose (fp); + return (true); +} + +#endif + diff --git a/customdhcpcd/src/info.h b/customdhcpcd/src/info.h new file mode 100644 index 0000000..22966db --- /dev/null +++ b/customdhcpcd/src/info.h @@ -0,0 +1,42 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef INFO_H +#define INFO_H + +#include "dhcpcd.h" +#include "interface.h" +#include "dhcp.h" + +#ifdef ENABLE_INFO +bool write_info (const interface_t *iface, const dhcp_t *dhcp, + const options_t *options, bool overwrite); + +bool read_info (const interface_t *iface, dhcp_t *dhcp); +#endif + +#endif diff --git a/customdhcpcd/src/interface.c b/customdhcpcd/src/interface.c new file mode 100644 index 0000000..d2ff8d6 --- /dev/null +++ b/customdhcpcd/src/interface.c @@ -0,0 +1,1060 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include + +#include + +/* Netlink suff */ +#ifdef __linux__ +#include /* Needed for 2.4 kernels */ +#include +#include +#include +#include +#else +#include +#include +#include +#include +#endif + +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" +#include "common.h" +#include "dhcp.h" +#include "interface.h" +#include "logger.h" + +void free_address (struct address_head *addresses) +{ + address_t *p; + address_t *n; + + if (! addresses) + return; + + p = STAILQ_FIRST (addresses); + while (p) { + n = STAILQ_NEXT (p, entries); + free (p); + p = n; + } + free (addresses); +} + +void free_route (struct route_head *routes) +{ + route_t *p; + route_t *n; + + if (! routes) + return; + + p = STAILQ_FIRST (routes); + while (p) { + n = STAILQ_NEXT (p, entries); + free (p); + p = n; + } + free (routes); +} + +int inet_ntocidr (struct in_addr address) +{ + int cidr = 0; + uint32_t mask = htonl (address.s_addr); + + while (mask) { + cidr++; + mask <<= 1; + } + + return (cidr); +} + +int inet_cidrtoaddr (int cidr, struct in_addr *addr) { + int ocets; + + if (cidr < 0 || cidr > 32) { + errno = EINVAL; + return (-1); + } + ocets = (cidr + 7) / 8; + + memset (addr, 0, sizeof (*addr)); + if (ocets > 0) { + memset (&addr->s_addr, 255, (size_t) ocets - 1); + memset ((unsigned char *) &addr->s_addr + (ocets - 1), + (256 - (1 << (32 - cidr) % 8)), 1); + } + + return (0); +} + +uint32_t get_netmask (uint32_t addr) +{ + uint32_t dst; + + if (addr == 0) + return (0); + + dst = htonl (addr); + if (IN_CLASSA (dst)) + return (ntohl (IN_CLASSA_NET)); + if (IN_CLASSB (dst)) + return (ntohl (IN_CLASSB_NET)); + if (IN_CLASSC (dst)) + return (ntohl (IN_CLASSC_NET)); + + return (0); +} + +char *hwaddr_ntoa (const unsigned char *hwaddr, size_t hwlen) +{ + static char buffer[(HWADDR_LEN * 3) + 1]; + char *p = buffer; + size_t i; + + for (i = 0; i < hwlen && i < HWADDR_LEN; i++) { + if (i > 0) + *p ++= ':'; + p += snprintf (p, 3, "%.2x", hwaddr[i]); + } + + *p ++= '\0'; + + return (buffer); +} + +size_t hwaddr_aton (unsigned char *buffer, const char *addr) +{ + char c[3]; + const char *p = addr; + unsigned char *bp = buffer; + size_t len = 0; + + c[2] = '\0'; + while (*p) { + c[0] = *p++; + c[1] = *p++; + /* Ensure that next data is EOL or a seperator with data */ + if (! (*p == '\0' || (*p == ':' && *(p + 1) != '\0'))) { + errno = EINVAL; + return (0); + } + /* Ensure that digits are hex */ + if (isxdigit ((int) c[0]) == 0 || isxdigit ((int) c[1]) == 0) { + errno = EINVAL; + return (0); + } + p++; + if (bp) + *bp++ = (unsigned char) strtol (c, NULL, 16); + else + len++; + } + + if (bp) + return (bp - buffer); + return (len); +} + +static int _do_interface (const char *ifname, + _unused unsigned char *hwaddr, _unused size_t *hwlen, + struct in_addr *addr, + bool flush, bool get) +{ + int s; + struct ifconf ifc; + int retval = 0; + int len = 10 * sizeof (struct ifreq); + int lastlen = 0; + char *p; + + if ((s = socket (AF_INET, SOCK_DGRAM, 0)) == -1) { + logger (LOG_ERR, "socket: %s", strerror (errno)); + return -1; + } + + /* Not all implementations return the needed buffer size for + * SIOGIFCONF so we loop like so for all until it works */ + memset (&ifc, 0, sizeof (ifc)); + for (;;) { + ifc.ifc_len = len; + ifc.ifc_buf = xmalloc ((size_t) len); + if (ioctl (s, SIOCGIFCONF, &ifc) == -1) { + if (errno != EINVAL || lastlen != 0) { + logger (LOG_ERR, "ioctl SIOCGIFCONF: %s", + strerror (errno)); + close (s); + free (ifc.ifc_buf); + return -1; + } + } else { + if (ifc.ifc_len == lastlen) + break; + lastlen = ifc.ifc_len; + } + + free (ifc.ifc_buf); + ifc.ifc_buf = NULL; + len *= 2; + } + + for (p = ifc.ifc_buf; p < ifc.ifc_buf + ifc.ifc_len;) { + union { + char *buffer; + struct ifreq *ifr; + } ifreqs; + struct sockaddr_in address; + struct ifreq *ifr; + + /* Cast the ifc buffer to an ifreq cleanly */ + ifreqs.buffer = p; + ifr = ifreqs.ifr; + +#ifdef __linux__ + p += sizeof (*ifr); +#else + p += offsetof (struct ifreq, ifr_ifru) + ifr->ifr_addr.sa_len; +#endif + + if (strcmp (ifname, ifr->ifr_name) != 0) + continue; + +#ifdef AF_LINK + if (hwaddr && hwlen && ifr->ifr_addr.sa_family == AF_LINK) { + struct sockaddr_dl sdl; + + memcpy (&sdl, &ifr->ifr_addr, sizeof (sdl)); + *hwlen = sdl.sdl_alen; + memcpy (hwaddr, sdl.sdl_data + sdl.sdl_nlen, + (size_t) sdl.sdl_alen); + retval = 1; + break; + } +#endif + + if (ifr->ifr_addr.sa_family == AF_INET) { + memcpy (&address, &ifr->ifr_addr, sizeof (address)); + if (flush) { + struct sockaddr_in netmask; + + if (ioctl (s, SIOCGIFNETMASK, ifr) == -1) { + logger (LOG_ERR, + "ioctl SIOCGIFNETMASK: %s", + strerror (errno)); + continue; + } + memcpy (&netmask, &ifr->ifr_addr, + sizeof (netmask)); + + if (del_address (ifname, + address.sin_addr, + netmask.sin_addr) == -1) + retval = -1; + } else if (get) { + addr->s_addr = address.sin_addr.s_addr; + retval = 1; + break; + } else if (address.sin_addr.s_addr == addr->s_addr) { + retval = 1; + break; + } + } + + } + + close (s); + free (ifc.ifc_buf); + return retval; +} + +interface_t *read_interface (const char *ifname, _unused int metric) +{ + int s; + struct ifreq ifr; + interface_t *iface = NULL; + unsigned char *hwaddr = NULL; + size_t hwlen = 0; + sa_family_t family = 0; + unsigned short mtu; +#ifdef __linux__ + char *p; +#endif + + if (! ifname) + return NULL; + + memset (&ifr, 0, sizeof (ifr)); + strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); + + if ((s = socket (AF_INET, SOCK_DGRAM, 0)) == -1) { + logger (LOG_ERR, "socket: %s", strerror (errno)); + return NULL; + } + +#ifdef __linux__ + strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); + if (ioctl (s, SIOCGIFHWADDR, &ifr) == -1) { + logger (LOG_ERR, "ioctl SIOCGIFHWADDR: %s", strerror (errno)); + goto exit; + } + + switch (ifr.ifr_hwaddr.sa_family) { + case ARPHRD_ETHER: + case ARPHRD_IEEE802: + hwlen = ETHER_ADDR_LEN; + break; + case ARPHRD_IEEE1394: + hwlen = EUI64_ADDR_LEN; + case ARPHRD_INFINIBAND: + hwlen = INFINIBAND_ADDR_LEN; + break; + default: + logger (LOG_ERR, + "interface is not Ethernet, FireWire, " \ + "InfiniBand or Token Ring"); + goto exit; + } + + hwaddr = xmalloc (sizeof (unsigned char) * HWADDR_LEN); + memcpy (hwaddr, ifr.ifr_hwaddr.sa_data, hwlen); + family = ifr.ifr_hwaddr.sa_family; +#else + ifr.ifr_metric = metric; + strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); + if (ioctl (s, SIOCSIFMETRIC, &ifr) == -1) { + logger (LOG_ERR, "ioctl SIOCSIFMETRIC: %s", strerror (errno)); + goto exit; + } + + hwaddr = xmalloc (sizeof (unsigned char) * HWADDR_LEN); + if (_do_interface (ifname, hwaddr, &hwlen, NULL, false, false) != 1) { + logger (LOG_ERR, "could not find interface %s", ifname); + goto exit; + } + + family = ARPHRD_ETHER; +#endif + + strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); + if (ioctl (s, SIOCGIFMTU, &ifr) == -1) { + logger (LOG_ERR, "ioctl SIOCGIFMTU: %s", strerror (errno)); + goto exit; + } + + if (ifr.ifr_mtu < MTU_MIN) { + logger (LOG_DEBUG, "MTU of %d is too low, setting to %d", + ifr.ifr_mtu, MTU_MIN); + ifr.ifr_mtu = MTU_MIN; + strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); + if (ioctl (s, SIOCSIFMTU, &ifr) == -1) { + logger (LOG_ERR, "ioctl SIOCSIFMTU,: %s", + strerror (errno)); + goto exit; + } + } + mtu = ifr.ifr_mtu; + + strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); +#ifdef __linux__ + /* We can only bring the real interface up */ + if ((p = strchr (ifr.ifr_name, ':'))) + *p = '\0'; +#endif + if (ioctl (s, SIOCGIFFLAGS, &ifr) == -1) { + logger (LOG_ERR, "ioctl SIOCGIFFLAGS: %s", strerror (errno)); + goto exit; + } + + if (! (ifr.ifr_flags & IFF_UP) || ! (ifr.ifr_flags & IFF_RUNNING)) { + ifr.ifr_flags |= IFF_UP | IFF_RUNNING; + if (ioctl (s, SIOCSIFFLAGS, &ifr) != 0) { + logger (LOG_ERR, "ioctl SIOCSIFFLAGS: %s", + strerror (errno)); + goto exit; + } + } + + iface = xzalloc (sizeof (*iface)); + strlcpy (iface->name, ifname, IF_NAMESIZE); +#ifdef ENABLE_INFO + snprintf (iface->infofile, PATH_MAX, INFOFILE, ifname); +#endif + memcpy (&iface->hwaddr, hwaddr, hwlen); + iface->hwlen = hwlen; + + iface->family = family; + iface->arpable = ! (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)); + iface->mtu = iface->previous_mtu = mtu; + + logger (LOG_INFO, "hardware address = %s", + hwaddr_ntoa (iface->hwaddr, iface->hwlen)); + + /* 0 is a valid fd, so init to -1 */ + iface->fd = -1; +#ifdef __linux__ + iface->listen_fd = -1; +#endif + +exit: + close (s); + free (hwaddr); + return iface; +} + +int get_mtu (const char *ifname) +{ + struct ifreq ifr; + int r; + int s; + + if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { + logger (LOG_ERR, "socket: %s", strerror (errno)); + return (-1); + } + + memset (&ifr, 0, sizeof (ifr)); + strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); + r = ioctl (s, SIOCGIFMTU, &ifr); + close (s); + + if (r == -1) { + logger (LOG_ERR, "ioctl SIOCGIFMTU: %s", strerror (errno)); + return (-1); + } + + return (ifr.ifr_mtu); +} + +int set_mtu (const char *ifname, short int mtu) +{ + struct ifreq ifr; + int r; + int s; + + if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { + logger (LOG_ERR, "socket: %s", strerror (errno)); + return (-1); + } + + memset (&ifr, 0, sizeof (ifr)); + logger (LOG_DEBUG, "setting MTU to %d", mtu); + strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); + ifr.ifr_mtu = mtu; + r = ioctl (s, SIOCSIFMTU, &ifr); + close (s); + + if (r == -1) + logger (LOG_ERR, "ioctl SIOCSIFMTU: %s", strerror (errno)); + + return (r == 0 ? 0 : -1); +} + +static void log_route (struct in_addr destination, + struct in_addr netmask, + struct in_addr gateway, + _unused int metric, + int change, int del) +{ + char *dstd = xstrdup (inet_ntoa (destination)); + +#ifdef __linux__ +#define METRIC " metric %d" +#else +#define METRIC "" +#endif + + if (gateway.s_addr == destination.s_addr || + gateway.s_addr == INADDR_ANY) + logger (LOG_INFO, "%s route to %s/%d" METRIC, + change ? "changing" : del ? "removing" : "adding", + dstd, inet_ntocidr (netmask) +#ifdef __linux__ + , metric +#endif + ); + else if (destination.s_addr == INADDR_ANY) + logger (LOG_INFO, "%s default route via %s" METRIC, + change ? "changing" : del ? "removing" : "adding", + inet_ntoa (gateway) + +#ifdef __linux__ + , metric +#endif + ); + else + logger (LOG_INFO, "%s route to %s/%d via %s" METRIC, + change ? "changing" : del ? "removing" : "adding", + dstd, inet_ntocidr (netmask), inet_ntoa (gateway) +#ifdef __linux__ + , metric +#endif + ); + + free (dstd); +} + +#if defined(BSD) || defined(__FreeBSD_kernel__) + +/* Darwin doesn't define this for some very odd reason */ +#ifndef SA_SIZE +# define SA_SIZE(sa) \ + ( (!(sa) || ((struct sockaddr *)(sa))->sa_len == 0) ? \ + sizeof(long) : \ + 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(long) - 1) ) ) +#endif + +static int do_address (const char *ifname, struct in_addr address, + struct in_addr netmask, struct in_addr broadcast, + int del) +{ + int s; + struct ifaliasreq ifa; + + if (! ifname) + return -1; + + if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { + logger (LOG_ERR, "socket: %s", strerror (errno)); + return -1; + } + + memset (&ifa, 0, sizeof (ifa)); + strlcpy (ifa.ifra_name, ifname, sizeof (ifa.ifra_name)); + +#define ADDADDR(_var, _addr) { \ + union { struct sockaddr *sa; struct sockaddr_in *sin; } _s; \ + _s.sa = &_var; \ + _s.sin->sin_family = AF_INET; \ + _s.sin->sin_len = sizeof (*_s.sin); \ + memcpy (&_s.sin->sin_addr, &_addr, sizeof (_s.sin->sin_addr)); \ +} + + ADDADDR (ifa.ifra_addr, address); + ADDADDR (ifa.ifra_mask, netmask); +if (! del) + ADDADDR (ifa.ifra_broadaddr, broadcast); + +#undef ADDADDR + + if (ioctl (s, del ? SIOCDIFADDR : SIOCAIFADDR, &ifa) == -1) { + logger (LOG_ERR, "ioctl %s: %s", + del ? "SIOCDIFADDR" : "SIOCAIFADDR", + strerror (errno)); + close (s); + return -1; + } + +close (s); +return 0; +} + +static int do_route (const char *ifname, + struct in_addr destination, + struct in_addr netmask, + struct in_addr gateway, + int metric, + int change, int del) +{ + int s; + static int seq; + union sockunion { + struct sockaddr sa; + struct sockaddr_in sin; +#ifdef INET6 + struct sockaddr_in6 sin6; +#endif + struct sockaddr_dl sdl; + struct sockaddr_storage ss; + } su; + struct rtm + { + struct rt_msghdr hdr; + char buffer[sizeof (su) * 3]; + } rtm; + char *bp = rtm.buffer; + size_t l; + + if (! ifname) + return -1; + + log_route (destination, netmask, gateway, metric, change, del); + + if ((s = socket (PF_ROUTE, SOCK_RAW, 0)) == -1) { + logger (LOG_ERR, "socket: %s", strerror (errno)); + return -1; + } + + memset (&rtm, 0, sizeof (rtm)); + + rtm.hdr.rtm_version = RTM_VERSION; + rtm.hdr.rtm_seq = ++seq; + rtm.hdr.rtm_type = change ? RTM_CHANGE : del ? RTM_DELETE : RTM_ADD; + rtm.hdr.rtm_flags = RTF_UP | RTF_STATIC; + + /* This order is important */ + rtm.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK; + +#define ADDADDR(_addr) \ + memset (&su, 0, sizeof (su)); \ + su.sin.sin_family = AF_INET; \ + su.sin.sin_len = sizeof (su.sin); \ + memcpy (&su.sin.sin_addr, &_addr, sizeof (su.sin.sin_addr)); \ + l = SA_SIZE (&(su.sa)); \ + memcpy (bp, &(su), l); \ + bp += l; + + ADDADDR (destination); + + if (netmask.s_addr == INADDR_BROADCAST || + gateway.s_addr == INADDR_ANY) + { + /* Make us a link layer socket */ + unsigned char *hwaddr; + size_t hwlen = 0; + + if (netmask.s_addr == INADDR_BROADCAST) + rtm.hdr.rtm_flags |= RTF_HOST; + + hwaddr = xmalloc (sizeof (unsigned char) * HWADDR_LEN); + _do_interface (ifname, hwaddr, &hwlen, NULL, false, false); + memset (&su, 0, sizeof (su)); + su.sdl.sdl_len = sizeof (su.sdl); + su.sdl.sdl_family = AF_LINK; + su.sdl.sdl_nlen = strlen (ifname); + memcpy (&su.sdl.sdl_data, ifname, (size_t) su.sdl.sdl_nlen); + su.sdl.sdl_alen = hwlen; + memcpy (((unsigned char *) &su.sdl.sdl_data) + su.sdl.sdl_nlen, + hwaddr, (size_t) su.sdl.sdl_alen); + + l = SA_SIZE (&(su.sa)); + memcpy (bp, &su, l); + bp += l; + free (hwaddr); + } else { + rtm.hdr.rtm_flags |= RTF_GATEWAY; + ADDADDR (gateway); + } + + ADDADDR (netmask); +#undef ADDADDR + + rtm.hdr.rtm_msglen = l = bp - (char *)&rtm; + if (write (s, &rtm, l) == -1) { + /* Don't report error about routes already existing */ + if (errno != EEXIST) + logger (LOG_ERR, "write: %s", strerror (errno)); + close (s); + return -1; + } + + close (s); + return 0; +} + +#elif __linux__ +/* This netlink stuff is overly compex IMO. + * The BSD implementation is much cleaner and a lot less code. + * send_netlink handles the actual transmission so we can work out + * if there was an error or not. */ +#define BUFFERLEN 256 +int send_netlink (struct nlmsghdr *hdr, netlink_callback callback, void *arg) +{ + int s; + pid_t mypid = getpid (); + struct sockaddr_nl nl; + struct iovec iov; + struct msghdr msg; + static unsigned int seq; + char *buffer; + ssize_t bytes; + union + { + char *buffer; + struct nlmsghdr *nlm; + } h; + + if ((s = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1) { + logger (LOG_ERR, "socket: %s", strerror (errno)); + return -1; + } + + memset (&nl, 0, sizeof (nl)); + nl.nl_family = AF_NETLINK; + if (bind (s, (struct sockaddr *) &nl, sizeof (nl)) == -1) { + logger (LOG_ERR, "bind: %s", strerror (errno)); + close (s); + return -1; + } + + memset (&iov, 0, sizeof (iov)); + iov.iov_base = hdr; + iov.iov_len = hdr->nlmsg_len; + + memset (&msg, 0, sizeof (msg)); + msg.msg_name = &nl; + msg.msg_namelen = sizeof (nl); + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + + /* Request a reply */ + hdr->nlmsg_flags |= NLM_F_ACK; + hdr->nlmsg_seq = ++seq; + + if (sendmsg (s, &msg, 0) == -1) { + logger (LOG_ERR, "write: %s", strerror (errno)); + close (s); + return -1; + } + + buffer = xzalloc (sizeof (char) * BUFFERLEN); + iov.iov_base = buffer; + + for (;;) { + iov.iov_len = BUFFERLEN; + bytes = recvmsg (s, &msg, 0); + + if (bytes == -1) { + if (errno != EINTR) + logger (LOG_ERR, "recvmsg: %s", + strerror (errno)); + continue; + } + + if (bytes == 0) { + logger (LOG_ERR, "netlink: EOF"); + goto eexit; + } + + if (msg.msg_namelen != sizeof (nl)) { + logger (LOG_ERR, + "netlink: sender address length mismatch"); + goto eexit; + } + + for (h.buffer = buffer; bytes >= (signed) sizeof (*h.nlm); ) { + int len = h.nlm->nlmsg_len; + int l = len - sizeof (*h.nlm); + struct nlmsgerr *err = (struct nlmsgerr *) NLMSG_DATA (h.nlm); + + if (l < 0 || len > bytes) { + if (msg.msg_flags & MSG_TRUNC) + logger (LOG_ERR, "netlink: truncated message"); + else + logger (LOG_ERR, "netlink: malformed message"); + goto eexit; + } + + /* Ensure it's our message */ + if (nl.nl_pid != 0 || + (pid_t) h.nlm->nlmsg_pid != mypid || + h.nlm->nlmsg_seq != seq) + { + /* Next Message */ + bytes -= NLMSG_ALIGN (len); + h.buffer += NLMSG_ALIGN (len); + continue; + } + + /* We get an NLMSG_ERROR back with a code of zero for success */ + if (h.nlm->nlmsg_type != NLMSG_ERROR) { + logger (LOG_ERR, "netlink: unexpected reply %d", + h.nlm->nlmsg_type); + goto eexit; + } + + if ((unsigned) l < sizeof (*err)) { + logger (LOG_ERR, "netlink: error truncated"); + goto eexit; + } + + if (err->error == 0) { + int retval = 0; + + close (s); + if (callback) { + if ((retval = callback (hdr, arg)) == -1) + logger (LOG_ERR, "netlink: callback failed"); + } + free (buffer); + return (retval); + } + + errno = -err->error; + /* Don't report on something already existing */ + if (errno != EEXIST) + logger (LOG_ERR, "netlink: %s", + strerror (errno)); + goto eexit; + } + } + +eexit: + close (s); + free (buffer); + return -1; +} + +#define NLMSG_TAIL(nmsg) \ + ((struct rtattr *) (((ptrdiff_t) (nmsg)) + NLMSG_ALIGN ((nmsg)->nlmsg_len))) + +static int add_attr_l(struct nlmsghdr *n, unsigned int maxlen, int type, + const void *data, int alen) +{ + int len = RTA_LENGTH(alen); + struct rtattr *rta; + + if (NLMSG_ALIGN (n->nlmsg_len) + RTA_ALIGN (len) > maxlen) { + logger (LOG_ERR, "add_attr_l: message exceeded bound of %d\n", + maxlen); + return -1; + } + + rta = NLMSG_TAIL (n); + rta->rta_type = type; + rta->rta_len = len; + memcpy (RTA_DATA (rta), data, alen); + n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + RTA_ALIGN (len); + + return 0; +} + +static int add_attr_32(struct nlmsghdr *n, unsigned int maxlen, int type, + uint32_t data) +{ + int len = RTA_LENGTH (sizeof (data)); + struct rtattr *rta; + + if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen) { + logger (LOG_ERR, "add_attr32: message exceeded bound of %d\n", + maxlen); + return -1; + } + + rta = NLMSG_TAIL (n); + rta->rta_type = type; + rta->rta_len = len; + memcpy (RTA_DATA (rta), &data, sizeof (data)); + n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len; + + return 0; +} + +struct nlma +{ + struct nlmsghdr hdr; + struct ifaddrmsg ifa; + char buffer[64]; +}; + +struct nlmr +{ + struct nlmsghdr hdr; + struct rtmsg rt; + char buffer[256]; +}; + +static int do_address(const char *ifname, + struct in_addr address, struct in_addr netmask, + struct in_addr broadcast, int del) +{ + struct nlma *nlm; + int retval; + + if (!ifname) + return -1; + + nlm = xzalloc (sizeof (*nlm)); + nlm->hdr.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg)); + nlm->hdr.nlmsg_flags = NLM_F_REQUEST; + if (! del) + nlm->hdr.nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE; + nlm->hdr.nlmsg_type = del ? RTM_DELADDR : RTM_NEWADDR; + if (! (nlm->ifa.ifa_index = if_nametoindex (ifname))) { + logger (LOG_ERR, "if_nametoindex: no index for interface `%s'", + ifname); + free (nlm); + return -1; + } + nlm->ifa.ifa_family = AF_INET; + + nlm->ifa.ifa_prefixlen = inet_ntocidr (netmask); + + /* This creates the aliased interface */ + add_attr_l (&nlm->hdr, sizeof (*nlm), IFA_LABEL, + ifname, strlen (ifname) + 1); + + add_attr_l (&nlm->hdr, sizeof (*nlm), IFA_LOCAL, + &address.s_addr, sizeof (address.s_addr)); + if (! del) + add_attr_l (&nlm->hdr, sizeof (*nlm), IFA_BROADCAST, + &broadcast.s_addr, sizeof (broadcast.s_addr)); + + retval = send_netlink (&nlm->hdr, NULL, NULL); + free (nlm); + return retval; +} + +static int do_route (const char *ifname, + struct in_addr destination, + struct in_addr netmask, + struct in_addr gateway, + int metric, int change, int del) +{ + struct nlmr *nlm; + unsigned int ifindex; + int retval; + + if (! ifname) + return -1; + + log_route (destination, netmask, gateway, metric, change, del); + + if (! (ifindex = if_nametoindex (ifname))) { + logger (LOG_ERR, "if_nametoindex: no index for interface `%s'", + ifname); + return -1; + } + + nlm = xzalloc (sizeof (*nlm)); + nlm->hdr.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg)); + if (change) + nlm->hdr.nlmsg_flags = NLM_F_REPLACE; + else if (! del) + nlm->hdr.nlmsg_flags = NLM_F_CREATE | NLM_F_EXCL; + nlm->hdr.nlmsg_flags |= NLM_F_REQUEST; + nlm->hdr.nlmsg_type = del ? RTM_DELROUTE : RTM_NEWROUTE; + nlm->rt.rtm_family = AF_INET; + nlm->rt.rtm_table = RT_TABLE_MAIN; + + if (del) + nlm->rt.rtm_scope = RT_SCOPE_NOWHERE; + else { + nlm->hdr.nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL; + nlm->rt.rtm_protocol = RTPROT_BOOT; + if (netmask.s_addr == INADDR_BROADCAST || + gateway.s_addr == INADDR_ANY) + nlm->rt.rtm_scope = RT_SCOPE_LINK; + else + nlm->rt.rtm_scope = RT_SCOPE_UNIVERSE; + nlm->rt.rtm_type = RTN_UNICAST; + } + + nlm->rt.rtm_dst_len = inet_ntocidr (netmask); + add_attr_l (&nlm->hdr, sizeof (*nlm), RTA_DST, + &destination.s_addr, sizeof (destination.s_addr)); + if (netmask.s_addr != INADDR_BROADCAST && + destination.s_addr != gateway.s_addr) + add_attr_l (&nlm->hdr, sizeof (*nlm), RTA_GATEWAY, + &gateway.s_addr, sizeof (gateway.s_addr)); + + add_attr_32 (&nlm->hdr, sizeof (*nlm), RTA_OIF, ifindex); + add_attr_32 (&nlm->hdr, sizeof (*nlm), RTA_PRIORITY, metric); + + retval = send_netlink (&nlm->hdr, NULL, NULL); + free (nlm); + return retval; +} + +#else + #error "Platform not supported!" + #error "We currently support BPF and Linux sockets." + #error "Other platforms may work using BPF. If yours does, please let me know" + #error "so I can add it to our list." +#endif + +int add_address (const char *ifname, struct in_addr address, + struct in_addr netmask, struct in_addr broadcast) +{ + logger (LOG_INFO, "adding IP address %s/%d", + inet_ntoa (address), inet_ntocidr (netmask)); + + return (do_address (ifname, address, netmask, broadcast, 0)); +} + +int del_address (const char *ifname, + struct in_addr address, struct in_addr netmask) +{ + struct in_addr t; + + logger (LOG_INFO, "removing IP address %s/%d", + inet_ntoa (address), inet_ntocidr (netmask)); + + memset (&t, 0, sizeof (t)); + return (do_address (ifname, address, netmask, t, 1)); +} + +int add_route (const char *ifname, struct in_addr destination, + struct in_addr netmask, struct in_addr gateway, int metric) +{ + return (do_route (ifname, destination, netmask, gateway, metric, 0, 0)); +} + +int change_route (const char *ifname, struct in_addr destination, + struct in_addr netmask, struct in_addr gateway, int metric) +{ + return (do_route (ifname, destination, netmask, gateway, metric, 1, 0)); +} + +int del_route (const char *ifname, struct in_addr destination, + struct in_addr netmask, struct in_addr gateway, int metric) +{ + return (do_route (ifname, destination, netmask, gateway, metric, 0, 1)); +} + + +int flush_addresses (const char *ifname) +{ + return (_do_interface (ifname, NULL, NULL, NULL, true, false)); +} + +in_addr_t get_address (const char *ifname) +{ + struct in_addr address; + if (_do_interface (ifname, NULL, NULL, &address, false, true) > 0) + return (address.s_addr); + return (0); +} + +int has_address (const char *ifname, struct in_addr address) +{ + return (_do_interface (ifname, NULL, NULL, &address, false, false)); +} diff --git a/customdhcpcd/src/interface.h b/customdhcpcd/src/interface.h new file mode 100644 index 0000000..8215d48 --- /dev/null +++ b/customdhcpcd/src/interface.h @@ -0,0 +1,173 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef INTERFACE_H +#define INTERFACE_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" + +#ifdef __linux__ +# include +#endif + +#ifdef ENABLE_DUID +#ifndef DUID_LEN +# define DUID_LEN 128 + 2 +#endif +#endif + +#define EUI64_ADDR_LEN 8 +#define INFINIBAND_ADDR_LEN 20 + +/* Linux 2.4 doesn't define this */ +#ifndef ARPHRD_IEEE1394 +# define ARPHRD_IEEE1394 24 +#endif + +/* The BSD's don't define this yet */ +#ifndef ARPHRD_INFINIBAND +# define ARPHRD_INFINIBAND 32 +#endif + +#define HWADDR_LEN 20 + +/* Work out if we have a private address or not + * 10/8 + * 172.16/12 + * 192.168/16 + */ +#ifndef IN_PRIVATE +# define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) || \ + ((addr & 0xfff00000) == 0xac100000) || \ + ((addr & IN_CLASSB_NET) == 0xc0a80000)) +#endif + +#define LINKLOCAL_ADDR 0xa9fe0000 +#define LINKLOCAL_MASK 0xffff0000 +#define LINKLOCAL_BRDC 0xa9feffff + +#ifndef IN_LINKLOCAL +# define IN_LINKLOCAL(addr) ((addr & IN_CLASSB_NET) == LINKLOCAL_ADDR) +#endif + +#ifndef STAILQ_ENTRY +# error "your sys/queue.h is too old and lacks STAILQ" +#endif + +#define NSTAILQ_FOREACH(var, head, field) \ + if (head) STAILQ_FOREACH (var, head, field) + +typedef struct route_t +{ + struct in_addr destination; + struct in_addr netmask; + struct in_addr gateway; + STAILQ_ENTRY (route_t) entries; +} route_t; +STAILQ_HEAD (route_head, route_t); + +typedef struct address_t +{ + struct in_addr address; + STAILQ_ENTRY (address_t) entries; +} address_t; +STAILQ_HEAD (address_head, address_t); + +typedef struct interface_t +{ + char name[IF_NAMESIZE]; + sa_family_t family; + unsigned char hwaddr[HWADDR_LEN]; + size_t hwlen; + bool arpable; + unsigned short mtu; + + int fd; + size_t buffer_length; + +#ifdef __linux__ + int listen_fd; + int socket_protocol; +#endif + + char infofile[PATH_MAX]; + + unsigned short previous_mtu; + struct in_addr previous_address; + struct in_addr previous_netmask; + struct route_head *previous_routes; + + time_t start_uptime; + + unsigned char *clientid; + size_t clientid_len; +} interface_t; + +void free_address (struct address_head *addresses); +void free_route (struct route_head *routes); +uint32_t get_netmask (uint32_t addr); +char *hwaddr_ntoa (const unsigned char *hwaddr, size_t hwlen); +size_t hwaddr_aton (unsigned char *hwaddr, const char *addr); + +interface_t *read_interface (const char *ifname, int metric); +int get_mtu (const char *ifname); +int set_mtu (const char *ifname, short int mtu); + +int add_address (const char *ifname, struct in_addr address, + struct in_addr netmask, struct in_addr broadcast); +int del_address (const char *ifname, struct in_addr address, + struct in_addr netmask); + +int flush_addresses (const char *ifname); +in_addr_t get_address (const char *ifname); +int has_address (const char *ifname, struct in_addr address); + +int add_route (const char *ifname, struct in_addr destination, + struct in_addr netmask, struct in_addr gateway, int metric); +int change_route (const char *ifname, struct in_addr destination, + struct in_addr netmask, struct in_addr gateway, int metric); +int del_route (const char *ifname, struct in_addr destination, + struct in_addr netmask, struct in_addr gateway, int metric); + +int inet_ntocidr (struct in_addr address); +int inet_cidrtoaddr (int cidr, struct in_addr *addr); + +#ifdef __linux__ +typedef int (*netlink_callback) (struct nlmsghdr *hdr, void *arg); +int send_netlink (struct nlmsghdr *hdr, netlink_callback callback, void *arg); +#endif +#endif diff --git a/customdhcpcd/src/ipv4ll.c b/customdhcpcd/src/ipv4ll.c new file mode 100644 index 0000000..9742b9a --- /dev/null +++ b/customdhcpcd/src/ipv4ll.c @@ -0,0 +1,70 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include + +#include "config.h" +#include "arp.h" +#include "ipv4ll.h" + +#ifdef ENABLE_IPV4LL + +#ifndef ENABLE_ARP + # error IPV4LL requires ARP +#endif + +#define IPV4LL_LEASETIME 20 + +int ipv4ll_get_address (interface_t *iface, dhcp_t *dhcp) { + struct in_addr addr; + + for (;;) { + addr.s_addr = htonl (LINKLOCAL_ADDR | + (((uint32_t) abs ((int) random ()) + % 0xFD00) + 0x0100)); + errno = 0; + if (! arp_claim (iface, addr)) + break; + /* Our ARP may have been interrupted */ + if (errno) + return (-1); + } + + dhcp->address.s_addr = addr.s_addr; + dhcp->netmask.s_addr = htonl (LINKLOCAL_MASK); + dhcp->broadcast.s_addr = htonl (LINKLOCAL_BRDC); + + /* Finally configure some DHCP like lease times */ + dhcp->leasetime = IPV4LL_LEASETIME; + dhcp->renewaltime = (dhcp->leasetime * 0.5); + dhcp->rebindtime = (dhcp->leasetime * 0.875); + + return (0); +} + +#endif diff --git a/customdhcpcd/src/ipv4ll.h b/customdhcpcd/src/ipv4ll.h new file mode 100644 index 0000000..4fa8943 --- /dev/null +++ b/customdhcpcd/src/ipv4ll.h @@ -0,0 +1,39 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef IPV4LL_H +#define IPV4LL_H + +#ifdef ENABLE_IPV4LL + +#include "dhcp.h" +#include "interface.h" + +int ipv4ll_get_address (interface_t *iface, dhcp_t *dhcp); + +#endif +#endif diff --git a/customdhcpcd/src/logger.c b/customdhcpcd/src/logger.c new file mode 100644 index 0000000..91d6cc0 --- /dev/null +++ b/customdhcpcd/src/logger.c @@ -0,0 +1,128 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#define SYSLOG_NAMES + +#define COM_CH "/var/tmp/com.socket" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "common.h" +#include "logger.h" +#include "logwriter.h" + +static int loglevel = LOG_WARNING; +static char logprefix[12] = { 0 }; + +int logtolevel(const char *priority) { + CODE *c; + + if (isdigit ((int) *priority)) + return (atoi(priority)); + + for (c = prioritynames; c->c_name; c++) + if (!strcasecmp(priority, c->c_name)) + return (c->c_val); + + return (-1); +} + +static const char *leveltolog(int level) { + CODE *c; + + for (c = prioritynames; c->c_name; c++) + if (c->c_val == level) + return (c->c_name); + + return (NULL); +} + +void setloglevel(int level) { + loglevel = level; +} + +void setlogprefix(const char *prefix) { + snprintf(logprefix, sizeof(logprefix), "%s", prefix); +} + +void logger(int level, const char *fmt, ...) { + va_list p; + va_list p2; + FILE *f = stderr; + + va_start (p, fmt); + va_copy (p2, p); + + if (level <= LOG_ERR || level <= loglevel) { + + /* new function by Niklas Goby + * send the log message also to our Qt programm. + * implemented in logwriter.c + * */ + logLoggerToQt(level, fmt, p); + + if (level == LOG_DEBUG || level == LOG_INFO) + f = stdout; + fprintf(f, "%s, %s", leveltolog(level), logprefix); + vfprintf(f, fmt, p); + fputc('\n', f); + + /* stdout, stderr may be re-directed to some kind of buffer. + * So we always flush to ensure it's written. */ + fflush(f); + } + + if (level < LOG_DEBUG || level <= loglevel) { + size_t len = strlen(logprefix); + size_t fmt2len = strlen(fmt) + len + 1; + char *fmt2 = malloc(sizeof(char) * fmt2len); + char *pf = fmt2; + if (fmt2) { + memcpy(pf, logprefix, len); + pf += len; + strlcpy(pf, fmt, fmt2len - len); + vsyslog(level, fmt2, p2); + free(fmt2); + } else { + vsyslog(level, fmt, p2); + syslog(LOG_ERR, "logger: memory exhausted"); + exit(EXIT_FAILURE); + } + } + + va_end (p2); + va_end (p); +} + diff --git a/customdhcpcd/src/logger.h b/customdhcpcd/src/logger.h new file mode 100644 index 0000000..70e2ed5 --- /dev/null +++ b/customdhcpcd/src/logger.h @@ -0,0 +1,48 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef LOGGER_H +#define LOGGER_H + +#if defined(__GNUC__) +# define _PRINTF_LIKE(_one, _two) __attribute__ ((__format__ (__printf__, _one, _two))) +#else +# define _PRINTF_LIKE(_one, _two) +#endif + +#include + + + +int logtolevel (const char *priority); +void setloglevel (int level); +void setlogprefix (const char *prefix); +void logger (int level, const char *fmt, ...) _PRINTF_LIKE (2, 3); + + + +#endif diff --git a/customdhcpcd/src/logwriter.c b/customdhcpcd/src/logwriter.c new file mode 100644 index 0000000..74c0181 --- /dev/null +++ b/customdhcpcd/src/logwriter.c @@ -0,0 +1,149 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "common.h" +#include "dhcp.h" +#include "dhcpcd.h" +#include "logger.h" +#include "logwriter.h" +#include "status.h" + +/*sockets for the logger and the qt-reader */ +int sockfd, ns; +int retval = -1; +char socketName[QTSOCKETADDRESSLENGTH]; +char interfaceName[IF_NAMESIZE]; +char mesg[1024]; + +void setSocketName(const char * sn) { + snprintf(socketName, sizeof(socketName), "%s", sn); +} + +void setInterfaceName(const char * in){ + snprintf(interfaceName, sizeof(interfaceName), "%s", in); +} + +int initQtLoggerSocket() { + /** + * new code. seems to be right. + */ + + struct sockaddr_un serv_addr; + fprintf(stdout,"start init \n"); + sockfd = socket(AF_UNIX, SOCK_STREAM, 0); + if (sockfd < 0) { + fprintf(stdout,"ERROR opening socket \n"); + retval = sockfd; + return sockfd; + } + serv_addr.sun_family = AF_UNIX; + strcpy(serv_addr.sun_path, socketName); + + retval = connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); + if ( retval < 0) + fprintf(stdout,"ERROR connecting \n"); + fprintf(stdout,"init Qt Logger Socket done \n"); + return retval; +} + +void closeQtLoggerSocket (){ + close(sockfd); +} + +/* +void logToQt(char * status, char * substatus, char * msg) { + char * m = malloc(strlen(interfaceName) + strlen(status) + strlen(substatus) + strlen(msg) +4 ); + strcpy(m,interfaceName); + strcat(m,";"); + strcat(m,status); + strcat(m,";"); + strcat(m,substatus); + strcat(m,";"); + strcat(m,msg); + sendToQt(m); + + free(m); +} +*/ + + +void sendToQt(log_msg * msg) { + int n = -1; + const char *tpl = "%s;%d;%d;%s\n"; + char *outbuf; + size_t outbuf_size = + sizeof(char)*4 + // ";" *3 + newline + sizeof(int)*2 + // status, substatus + sizeof(msg->device) + // devicename + sizeof(msg->msg); // msg + outbuf = malloc(outbuf_size); + memset(outbuf,0,outbuf_size); + snprintf(outbuf, sizeof(char)*3 + sizeof(int)*2 + sizeof(msg->device) + sizeof(msg->msg), tpl, msg->device, msg->status, msg->substatus, msg->msg); + if (outbuf != NULL){ + n = write(sockfd, outbuf, outbuf_size); + } + free(outbuf); + syslog (LOG_INFO, "[fbgui] INFO writing to socket: [%d:%d] %s (%s)", msg->status, msg->substatus, msg->msg, msg->device); +// fflush(sockfd); + if (n < 0) { + syslog (LOG_ERR, "[fbgui] ERROR writing to socket: [%d:%d] %s (%s)", msg->status, msg->substatus, msg->msg, msg->device); +// fprintf(stdout, "ERROR writing to socket: %s", msg); + } + //usleep(500); +} + +void logToQt(int status, int substatus, const char * msg) { + if (retval >= 0) { + log_msg lm; + lm.status = status; + lm.substatus = substatus; + snprintf(lm.msg, sizeof(lm.msg), "%s", msg); + snprintf(lm.device, sizeof(lm.device), "%s", interfaceName); + sendToQt(&lm); + } +} + +void logSendToQt(int type) { + switch(type) { + case DHCP_DISCOVER: + logToQt(LOG_INFO, DHCP_DISCOVER, "send discover"); + break; + case DHCP_OFFER: + logToQt(LOG_INFO, DHCP_OFFER, "send offer"); + break; + case DHCP_REQUEST: + logToQt(LOG_INFO, DHCP_REQUEST, "send request"); + break; + case DHCP_DECLINE: + logToQt(LOG_INFO, DHCP_DECLINE, "send decline"); + break; + case DHCP_ACK: + logToQt(LOG_INFO, DHCP_ACK, "send ack"); + break; + case DHCP_NAK: + logToQt(LOG_INFO, DHCP_NAK, "send nak"); + break; + case DHCP_RELEASE: + logToQt(LOG_INFO, DHCP_RELEASE, "send release"); + break; + case DHCP_INFORM: + logToQt(LOG_INFO, DHCP_INFORM, "send inform"); + break; + default : + break; + } +} + +void logLoggerToQt(int level, const char *fmt, va_list args) { + vsnprintf(mesg, sizeof(mesg), fmt, args); + strcat(mesg, "\n"); + logToQt(level, DHCPCD_LOG, mesg); +} diff --git a/customdhcpcd/src/logwriter.h b/customdhcpcd/src/logwriter.h new file mode 100644 index 0000000..1a755fc --- /dev/null +++ b/customdhcpcd/src/logwriter.h @@ -0,0 +1,37 @@ +/* + * logwriter.h + * + * Created on: Jul 7, 2011 + * Author: niklas + */ + +#ifndef LOGWRITER_H_ +#define LOGWRITER_H_ + +#include "dhcpcd.h" + +#define LOG_MSG_SIZE 1024 + +typedef struct _log_msg log_msg; +struct _log_msg { + int status; + int substatus; + char device[IF_NAMESIZE]; + char msg[LOG_MSG_SIZE]; +}; + +/** + * new functions for communicating with Qt + */ +void setSocketName(const char * sn); +void setInterfaceName(const char * in); +int initQtLoggerSocket (); +void closeQtLoggerSocket (); +void sendToQt (); +void logToQt(int status, int substatus, const char * msg); +void logSendToQt(int type); +void logLoggerToQt(int level, const char *fmt, va_list args); +//void logToQt(char * status, char * substatus, char * msg); + + +#endif /* LOGWRITER_H_ */ diff --git a/customdhcpcd/src/mk/cc.mk b/customdhcpcd/src/mk/cc.mk new file mode 100644 index 0000000..d52597b --- /dev/null +++ b/customdhcpcd/src/mk/cc.mk @@ -0,0 +1,23 @@ +# Copyright 2008 Roy Marples + +# Setup some good default CFLAGS +CFLAGS?= -O2 + +# Default to using the C99 standard +CSTD?= c99 +_CSTD_SH= if test -n "${CSTD}"; then echo "-std=${CSTD}"; else echo ""; fi +_CSTD!= ${_CSTD_SH} +CFLAGS+= ${_CSTD}$(shell ${_CSTD_SH}) + +# Try and use some good cc flags +_CC_FLAGS= -pedantic -Wall -Wunused -Wimplicit -Wshadow -Wformat=2 \ + -Wmissing-declarations -Wno-missing-prototypes -Wwrite-strings \ + -Wbad-function-cast -Wnested-externs -Wcomment -Winline \ + -Wchar-subscripts -Wcast-align -Wno-format-nonliteral \ + -Wdeclaration-after-statement -Wsequence-point -Wextra +_CC_FLAGS_SH= for f in ${_CC_FLAGS}; do \ + if ${CC} $$f -S -o /dev/null -xc /dev/null >/dev/null 2>&1; \ + then printf "%s" "$$f "; fi \ + done +_CC_FLAGS!= ${_CC_FLAGS_SH} +CFLAGS+= ${_CC_FLAGS}$(shell ${CC_FLAGS_SH}) diff --git a/customdhcpcd/src/mk/depend.mk b/customdhcpcd/src/mk/depend.mk new file mode 100644 index 0000000..a4d717a --- /dev/null +++ b/customdhcpcd/src/mk/depend.mk @@ -0,0 +1,11 @@ +# This only works for make implementations that always include a .depend if +# it exists. Only GNU make does not do this. + +# Copyright 2008 Roy Marples + +CLEANFILES+= .depend + +.depend: ${SRCS} + ${CC} ${CFLAGS} -MM ${SRCS} > .depend + +depend: .depend diff --git a/customdhcpcd/src/mk/dist.mk b/customdhcpcd/src/mk/dist.mk new file mode 100644 index 0000000..1d3669d --- /dev/null +++ b/customdhcpcd/src/mk/dist.mk @@ -0,0 +1,11 @@ +# rules to make a distribution tarball from a git repo +# Copyright 2008 Roy Marples + +GITREF?= HEAD +DISTPREFIX?= ${PROG}-${VERSION} +DISTFILE?= ${DISTPREFIX}.tar.bz2 + +CLEANFILES+= ${DISTFILE} + +dist: + git archive --prefix=${DISTPREFIX}/ ${GITREF} | bzip2 > ${DISTFILE} diff --git a/customdhcpcd/src/mk/man.mk b/customdhcpcd/src/mk/man.mk new file mode 100644 index 0000000..5d9bf26 --- /dev/null +++ b/customdhcpcd/src/mk/man.mk @@ -0,0 +1,14 @@ +# rules to install manpages +# Copyright 2008 Roy Marples + +MANPREFIX?= /usr/share +MANDIR?= ${MANPREFIX}/man/man +MANMODE?= 0444 +MINSTALL?= ${INSTALL} -m ${MANMODE} + +man: ${MAN} + +# We cheat as all our pages go into section 8 +maninstall: man + ${INSTALL} -d ${DESTDIR}${MANDIR}8 + for man in ${MAN}; do ${MINSTALL} $$man ${DESTDIR}${MANDIR}8; done diff --git a/customdhcpcd/src/mk/os.mk b/customdhcpcd/src/mk/os.mk new file mode 100644 index 0000000..af173fc --- /dev/null +++ b/customdhcpcd/src/mk/os.mk @@ -0,0 +1,77 @@ +# Setup OS specific variables +# Copyright 2008 Roy Marples + +# Work out if we need -lresolv or not +_LIBRESOLV_SH= printf '\#include \n\#include \nint main (void) { return (res_init ()); }\n' > .res_init.c; \ + if ${CC} .res_init.c -o .res_init >/dev/null 2>&1; then \ + echo ""; \ + elif ${CC} .res_init.c -lresolv -o .res_init >/dev/null 2>&1; then \ + echo "-lresolv"; \ + else \ + echo "Cannot work out how to get res_init to link" >&2; \ + rm -f .res_init.c .res_init; \ + exit 1; \ + fi; \ + rm -f .res_init.c .res_init +_LIBRESOLV!= ${_LIBRESOLV_SH} +LIBRESOLV= ${_LIBRESOLV}$(shell ${_LIBRESOLV_SH}) + +# Work out if we need -lrt or not +_LIBRT_SH= printf '\#include \n\#include \n\nint main (void) { struct timespec ts;\n\#if defined(_POSIX_MONOTONIC_CLOCK) && defined(CLOCK_MONOTONIC)\nreturn (clock_gettime (CLOCK_MONOTONIC, &ts));\n\#else\nreturn -1;\n\#endif\n}\n' > .clock_gettime.c; \ + if ${CC} .clock_gettime.c -o .clock_gettime >/dev/null 2>&1; then \ + echo ""; \ + elif ${CC} .clock_gettime.c -lrt -o .clock_gettime >/dev/null 2>&1; then \ + echo "-lrt"; \ + else \ + echo ""; \ + fi; \ + rm -f .clock_gettime.c .clock_gettime +_LIBRT!= ${_LIBRT_SH} +LIBRT= ${_LIBRT}$(shell ${_LIBRT_SH}) + +# Work out if our fork() works or not +_HAVE_FORK_SH= if test "${HAVE_FORK}" = "yes"; then \ + echo ""; \ + elif test -n "${HAVE_FORK}"; then \ + echo "-DTHERE_IS_NO_FORK"; \ + else \ + printf '\#include \n\#include \nint main (void) { pid_t pid = fork(); if (pid == -1) exit (-1); exit (0); }\n' > .fork.c; \ + ${CC} .fork.c -o .fork >/dev/null 2>&1; \ + if ./.fork; then \ + echo ""; \ + else \ + echo "-DTHERE_IS_NO_FORK"; \ + fi; \ + rm -f .fork.c .fork; \ + fi; +_HAVE_FORK!= ${_HAVE_FORK_SH} +FORK= ${_HAVE_FORK}$(shell ${_HAVE_FORK_SH}) + +# info dir defaults to /var/lib/dhcpcd on Linux and /var/db elsewhere +_INFODIR_SH= if test -n "${INFODIR}"; then \ + echo "${INFODIR}"; \ + else \ + case `uname -s` in \ + Linux) echo "/var/lib/dhcpcd";; \ + *) echo "/var/db";; \ + esac \ + fi +_INFODIR!= ${_INFODIR_SH} +INFOD?= ${_INFODIR}$(shell ${_INFODIR_SH}) + +# Work out how to restart services +_RC_SH= if test -n "${HAVE_INIT}"; then \ + test "${HAVE_INIT}" = "no" || echo "-DENABLE_${HAVE_INIT}"; \ + elif test -x /sbin/runscript; then echo "-DENABLE_OPENRC"; \ + elif test -x /sbin/service; then echo "-DENABLE_SERVICE"; \ + elif test -x /etc/rc.d/rc.S -a -x /etc/rc.d/rc.M; then echo "-DENABLE_SLACKRC"; \ + elif test -d /etc/rc.d; then echo "-DENABLE_BSDRC"; \ + elif test -d /etc/init.d; then echo "-DENABLE_SYSV"; \ + fi +_RC!= ${_RC_SH} +RC= ${_RC}$(shell ${_RC_SH}) + +# glibc requires _BSD_SOURCE and _XOPEN_SOURCE +_DEF_SH= case `uname -s` in Linux) echo "-D_BSD_SOURCE -D_XOPEN_SOURCE=600";; *) echo;; esac +_DEF!= ${_DEF_SH} +CFLAGS+= ${_DEF}$(shell ${_DEF_SH}) diff --git a/customdhcpcd/src/mk/prog.mk b/customdhcpcd/src/mk/prog.mk new file mode 100644 index 0000000..6f2560c --- /dev/null +++ b/customdhcpcd/src/mk/prog.mk @@ -0,0 +1,34 @@ +# rules to build a program +# based on FreeBSD's bsd.prog.mk + +# Copyright 2008 Roy Marples + +BINDIR?= ${PREFIX}/usr/bin +BINMODE?= 0755 +OBJS+= ${SRCS:.c=.o} + +INSTALL?= install + +all: ${PROG} ${MAN} + +${PROG}: ${SCRIPTS} ${OBJS} + ${CC} ${LDFLAGS} -o $@ ${OBJS} ${LDADD} + +_proginstall: ${PROG} + ${INSTALL} -d ${DESTDIR}${BINDIR} + ${INSTALL} -m ${BINMODE} ${PROG} ${DESTDIR}${BINDIR} + +include ${MK}/depend.mk +include ${MK}/man.mk +include ${MK}/dist.mk + +install: _proginstall maninstall + +clean: + rm -f ${OBJS} ${PROG} ${CLEANFILES} + +LINTFLAGS?= -hx +LINTFLAGS+= -X 159,247,352 + +lint: ${SRCS:.c=.c} + ${LINT} ${LINTFLAGS} ${CFLAGS:M-[DIU]*} $^ ${.ALLSRC} diff --git a/customdhcpcd/src/signal.c b/customdhcpcd/src/signal.c new file mode 100644 index 0000000..9055c9f --- /dev/null +++ b/customdhcpcd/src/signal.c @@ -0,0 +1,183 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "common.h" +#include "logger.h" +#include "signal.h" + +static int signal_pipe[2]; +static int signals[5]; + +static const int handle_sigs[] = { + SIGHUP, + SIGALRM, + SIGTERM, + SIGINT +}; + +static void signal_handler (int sig) +{ + unsigned int i = 0; + int serrno = errno; + + /* Add a signal to our stack */ + while (signals[i]) + i++; + if (i > sizeof (signals) / sizeof (signals[0])) + logger (LOG_ERR, "signal buffer overrun"); + else + signals[i] = sig; + + if (write (signal_pipe[1], &sig, sizeof (sig)) == -1) + logger (LOG_ERR, "Could not send signal: %s", strerror (errno)); + + /* Restore errno */ + errno = serrno; +} + +int signal_fd (void) +{ + return (signal_pipe[0]); +} + +/* Check if we have a signal or not */ +int signal_exists (const struct pollfd *fd) +{ + if (signals[0] || (fd && fd->revents & POLLIN)) + return (0); + return (-1); +} + +/* Read a signal from the signal pipe. Returns 0 if there is + * no signal, -1 on error (and sets errno appropriately), and + * your signal on success */ +int signal_read (struct pollfd *fd) +{ + int sig = -1; + + /* Pop a signal off the our stack */ + if (signals[0]) { + unsigned int i = 0; + sig = signals[0]; + while (i < (sizeof (signals) / sizeof (signals[0])) - 1) { + signals[i] = signals[i + 1]; + if (! signals[++i]) + break; + } + } + + if (fd && fd->revents & POLLIN) { + char buf[16]; + size_t bytes; + + memset (buf, 0, sizeof (buf)); + bytes = read (signal_pipe[0], buf, sizeof (buf)); + + if (bytes >= sizeof (sig)) + memcpy (&sig, buf, sizeof (sig)); + + /* We need to clear us from rset if nothing left in the buffer + * in case we are called many times */ + if (bytes == sizeof (sig)) + fd->revents = 0; + } + + return (sig); +} + +/* Call this before doing anything else. Sets up the socket pair + * and installs the signal handler */ +int signal_init (void) +{ + struct sigaction sa; + + if (pipe (signal_pipe) == -1) { + logger (LOG_ERR, "pipe: %s", strerror (errno)); + return (-1); + } + + /* Stop any scripts from inheriting us */ + close_on_exec (signal_pipe[0]); + close_on_exec (signal_pipe[1]); + + /* Ignore child signals and don't make zombies. + * Because we do this, we don't need to be in signal_setup */ + memset (&sa, 0, sizeof (sa)); + sa.sa_handler = SIG_DFL; + sa.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT; + if (sigaction (SIGCHLD, &sa, NULL) == -1) { + logger (LOG_ERR, "sigaction: %s", strerror (errno)); + return (-1); + } + + memset (signals, 0, sizeof (signals)); + return (0); +} + +int signal_setup (void) +{ + unsigned int i; + struct sigaction sa; + + memset (&sa, 0, sizeof (sa)); + sa.sa_handler = signal_handler; + sigemptyset (&sa.sa_mask); + + for (i = 0; i < sizeof (handle_sigs) / sizeof (handle_sigs[0]); i++) + if (sigaction (handle_sigs[i], &sa, NULL) == -1) { + logger (LOG_ERR, "sigaction: %s", strerror (errno)); + return (-1); + } + + return (0); +} + +int signal_reset (void) +{ + struct sigaction sa; + unsigned int i; + + memset (&sa, 0, sizeof (sa)); + sa.sa_handler = SIG_DFL; + sigemptyset (&sa.sa_mask); + + for (i = 0; i < sizeof (handle_sigs) / sizeof (handle_sigs[0]); i++) + if (sigaction (handle_sigs[i], &sa, NULL) == -1) { + logger (LOG_ERR, "sigaction: %s", strerror (errno)); + return (-1); + } + + return (0); +} diff --git a/customdhcpcd/src/signal.h b/customdhcpcd/src/signal.h new file mode 100644 index 0000000..63a5906 --- /dev/null +++ b/customdhcpcd/src/signal.h @@ -0,0 +1,40 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef SIGNAL_H +#define SIGNAL_H + +#include + +int signal_init (void); +int signal_setup (void); +int signal_reset (void); +int signal_fd (void); +int signal_exists (const struct pollfd *fd); +int signal_read (struct pollfd *fd); + +#endif diff --git a/customdhcpcd/src/socket.c b/customdhcpcd/src/socket.c new file mode 100644 index 0000000..58ad6c5 --- /dev/null +++ b/customdhcpcd/src/socket.c @@ -0,0 +1,647 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#define __FAVOR_BSD /* Nasty hack so we can use BSD semantics for UDP */ +#include +#undef __FAVOR_BSD +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(BSD) || defined(__FreeBSD_kernel__) +# include +#elif __linux__ +# include +# include +# define bpf_insn sock_filter +#endif + +#include "config.h" +#include "dhcp.h" +#include "interface.h" +#include "logger.h" +#include "socket.h" + +/* A suitably large buffer for all transactions. + * BPF buffer size is set by the kernel, so no define. */ +#ifdef __linux__ +# define BUFFER_LENGTH 4096 +#endif + +/* Broadcast address for IPoIB */ +static const uint8_t ipv4_bcast_addr[] = { + 0x00, 0xff, 0xff, 0xff, + 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff +}; + +/* Credit where credit is due :) + * The below BPF filter is taken from ISC DHCP */ +static struct bpf_insn dhcp_bpf_filter [] = { + /* Make sure this is an IP packet... */ + BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 12), + BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8), + + /* Make sure it's a UDP packet... */ + BPF_STMT (BPF_LD + BPF_B + BPF_ABS, 23), + BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6), + + /* Make sure this isn't a fragment... */ + BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 20), + BPF_JUMP (BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0), + + /* Get the IP header length... */ + BPF_STMT (BPF_LDX + BPF_B + BPF_MSH, 14), + + /* Make sure it's to the right port... */ + BPF_STMT (BPF_LD + BPF_H + BPF_IND, 16), + BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, DHCP_CLIENT_PORT, 0, 1), + + /* If we passed all the tests, ask for the whole packet. */ + BPF_STMT (BPF_RET + BPF_K, ~0U), + + /* Otherwise, drop it. */ + BPF_STMT (BPF_RET + BPF_K, 0), +}; + +static struct bpf_insn arp_bpf_filter [] = { + /* Make sure this is an ARP packet... */ + BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 12), + BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_ARP, 0, 3), + + /* Make sure this is an ARP REPLY... */ + BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 20), + BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REPLY, 0, 1), + + /* If we passed all the tests, ask for the whole packet. */ + BPF_STMT (BPF_RET + BPF_K, ~0U), + + /* Otherwise, drop it. */ + BPF_STMT (BPF_RET + BPF_K, 0), +}; + +void setup_packet_filters (void) +{ +#ifdef __linux__ + /* We need to massage the filters for Linux cooked packets */ + dhcp_bpf_filter[1].jf = 0; /* skip the IP packet type check */ + dhcp_bpf_filter[2].k -= ETH_HLEN; + dhcp_bpf_filter[4].k -= ETH_HLEN; + dhcp_bpf_filter[6].k -= ETH_HLEN; + dhcp_bpf_filter[7].k -= ETH_HLEN; + + arp_bpf_filter[1].jf = 0; /* skip the IP packet type check */ + arp_bpf_filter[2].k -= ETH_HLEN; +#endif +} + +static uint16_t checksum (unsigned char *addr, uint16_t len) +{ + uint32_t sum = 0; + union + { + unsigned char *addr; + uint16_t *i; + } p; + uint16_t nleft = len; + + p.addr = addr; + while (nleft > 1) { + sum += *p.i++; + nleft -= 2; + } + + if (nleft == 1) { + uint8_t a = 0; + memcpy (&a, p.i, 1); + sum += ntohs (a) << 8; + } + + sum = (sum >> 16) + (sum & 0xffff); + sum += (sum >> 16); + + return ~sum; +} + +void make_dhcp_packet(struct udp_dhcp_packet *packet, + const unsigned char *data, size_t length, + struct in_addr source, struct in_addr dest) +{ + struct ip *ip = &packet->ip; + struct udphdr *udp = &packet->udp; + + /* OK, this is important :) + * We copy the data to our packet and then create a small part of the + * ip structure and an invalid ip_len (basically udp length). + * We then fill the udp structure and put the checksum + * of the whole packet into the udp checksum. + * Finally we complete the ip structure and ip checksum. + * If we don't do the ordering like so then the udp checksum will be + * broken, so find another way of doing it! */ + + memcpy (&packet->dhcp, data, length); + + ip->ip_p = IPPROTO_UDP; + ip->ip_src.s_addr = source.s_addr; + if (dest.s_addr == 0) + ip->ip_dst.s_addr = INADDR_BROADCAST; + else + ip->ip_dst.s_addr = dest.s_addr; + + udp->uh_sport = htons (DHCP_CLIENT_PORT); + udp->uh_dport = htons (DHCP_SERVER_PORT); + udp->uh_ulen = htons (sizeof (*udp) + length); + ip->ip_len = udp->uh_ulen; + udp->uh_sum = checksum ((unsigned char *) packet, sizeof (*packet)); + + ip->ip_v = IPVERSION; + ip->ip_hl = 5; + ip->ip_id = 0; + ip->ip_tos = IPTOS_LOWDELAY; + ip->ip_len = htons (sizeof (*ip) + sizeof (*udp) + length); + ip->ip_id = 0; + ip->ip_off = htons (IP_DF); /* Don't fragment */ + ip->ip_ttl = IPDEFTTL; + + ip->ip_sum = checksum ((unsigned char *) ip, sizeof (*ip)); +} + +static int valid_dhcp_packet (unsigned char *data) +{ + union + { + unsigned char *data; + struct udp_dhcp_packet *packet; + } d; + uint16_t bytes; + uint16_t ipsum; + uint16_t iplen; + uint16_t udpsum; + struct in_addr source; + struct in_addr dest; + int retval = 0; + + d.data = data; + bytes = ntohs (d.packet->ip.ip_len); + ipsum = d.packet->ip.ip_sum; + iplen = d.packet->ip.ip_len; + udpsum = d.packet->udp.uh_sum; + + d.data = data; + d.packet->ip.ip_sum = 0; + if (ipsum != checksum ((unsigned char *) &d.packet->ip, + sizeof (d.packet->ip))) + { + logger (LOG_DEBUG, "bad IP header checksum, ignoring"); + retval = -1; + goto eexit; + } + + memcpy (&source, &d.packet->ip.ip_src, sizeof (d.packet->ip.ip_src)); + memcpy (&dest, &d.packet->ip.ip_dst, sizeof (d.packet->ip.ip_dst)); + memset (&d.packet->ip, 0, sizeof (d.packet->ip)); + d.packet->udp.uh_sum = 0; + + d.packet->ip.ip_p = IPPROTO_UDP; + memcpy (&d.packet->ip.ip_src, &source, sizeof (d.packet->ip.ip_src)); + memcpy (&d.packet->ip.ip_dst, &dest, sizeof (d.packet->ip.ip_dst)); + d.packet->ip.ip_len = d.packet->udp.uh_ulen; + if (udpsum && udpsum != checksum (d.data, bytes)) { + logger (LOG_ERR, "bad UDP checksum, ignoring"); + retval = -1; + } + +eexit: + d.packet->ip.ip_sum = ipsum; + d.packet->ip.ip_len = iplen; + d.packet->udp.uh_sum = udpsum; + + return retval; +} + +#if defined(BSD) || defined(__FreeBSD_kernel__) +int open_socket (interface_t *iface, int protocol) +{ + int n = 0; + int fd = -1; + char *device; + int flags; + struct ifreq ifr; + int buf = 0; + struct bpf_program pf; + + device = xmalloc (sizeof (char) * PATH_MAX); + do { + snprintf (device, PATH_MAX, "/dev/bpf%d", n++); + fd = open (device, O_RDWR); + } while (fd == -1 && errno == EBUSY); + free (device); + + if (fd == -1) { + logger (LOG_ERR, "unable to open a BPF device"); + return -1; + } + + close_on_exec (fd); + + memset (&ifr, 0, sizeof (ifr)); + strlcpy (ifr.ifr_name, iface->name, sizeof (ifr.ifr_name)); + if (ioctl (fd, BIOCSETIF, &ifr) == -1) { + logger (LOG_ERR, + "cannot attach interface `%s' to bpf device `%s': %s", + iface->name, device, strerror (errno)); + close (fd); + return -1; + } + + /* Get the required BPF buffer length from the kernel. */ + if (ioctl (fd, BIOCGBLEN, &buf) == -1) { + logger (LOG_ERR, "ioctl BIOCGBLEN: %s", strerror (errno)); + close (fd); + return -1; + } + iface->buffer_length = buf; + + flags = 1; + if (ioctl (fd, BIOCIMMEDIATE, &flags) == -1) { + logger (LOG_ERR, "ioctl BIOCIMMEDIATE: %s", strerror (errno)); + close (fd); + return -1; + } + + /* Install the DHCP filter */ + if (protocol == ETHERTYPE_ARP) { + pf.bf_insns = arp_bpf_filter; + pf.bf_len = sizeof (arp_bpf_filter) + / sizeof (arp_bpf_filter[0]); + } else { + pf.bf_insns = dhcp_bpf_filter; + pf.bf_len = sizeof (dhcp_bpf_filter) + / sizeof (dhcp_bpf_filter[0]); + } + if (ioctl (fd, BIOCSETF, &pf) == -1) { + logger (LOG_ERR, "ioctl BIOCSETF: %s", strerror (errno)); + close (fd); + return -1; + } + + if (iface->fd > -1) + close (iface->fd); + iface->fd = fd; + + return fd; +} + +ssize_t send_packet (const interface_t *iface, int type, + const unsigned char *data, size_t len) +{ + ssize_t retval = -1; + struct iovec iov[2]; + + if (iface->family == ARPHRD_ETHER) { + struct ether_header hw; + memset (&hw, 0, sizeof (hw)); + memset (&hw.ether_dhost, 0xff, ETHER_ADDR_LEN); + hw.ether_type = htons (type); + + iov[0].iov_base = &hw; + iov[0].iov_len = sizeof (hw); + } else { + logger (LOG_ERR, "unsupported interace type %d", iface->family); + return -1; + } + iov[1].iov_base = (unsigned char *) data; + iov[1].iov_len = len; + + if ((retval = writev(iface->fd, iov, 2)) == -1) + logger (LOG_ERR, "writev: %s", strerror (errno)); + + return retval; +} + +/* BPF requires that we read the entire buffer. + * So we pass the buffer in the API so we can loop on >1 dhcp packet. */ +ssize_t get_packet (const interface_t *iface, unsigned char *data, + unsigned char *buffer, + size_t *buffer_len, size_t *buffer_pos) +{ + union + { + unsigned char *buffer; + struct bpf_hdr *packet; + } bpf; + + bpf.buffer = buffer; + + if (*buffer_pos < 1) { + memset (bpf.buffer, 0, iface->buffer_length); + *buffer_len = read (iface->fd, bpf.buffer, iface->buffer_length); + *buffer_pos = 0; + if (*buffer_len < 1) { + struct timespec ts; + logger (LOG_ERR, "read: %s", strerror (errno)); + ts.tv_sec = 3; + ts.tv_nsec = 0; + nanosleep (&ts, NULL); + return (-1); + } + } else + bpf.buffer += *buffer_pos; + + while (bpf.packet) { + size_t len = 0; + union + { + unsigned char *buffer; + struct ether_header *hw; + } hdr; + unsigned char *payload; + bool have_data = false; + + /* Ensure that the entire packet is in our buffer */ + if (*buffer_pos + bpf.packet->bh_hdrlen + bpf.packet->bh_caplen + > (unsigned) *buffer_len) + break; + + hdr.buffer = bpf.buffer + bpf.packet->bh_hdrlen; + payload = hdr.buffer + sizeof (*hdr.hw); + + /* If it's an ARP reply, then just send it back */ + if (hdr.hw->ether_type == htons (ETHERTYPE_ARP)) { + len = bpf.packet->bh_caplen - + sizeof (*hdr.hw); + memcpy (data, payload, len); + have_data = true; + } else { + if (valid_dhcp_packet (payload) >= 0) { + union + { + unsigned char *buffer; + struct udp_dhcp_packet *packet; + } pay; + pay.buffer = payload; + len = ntohs (pay.packet->ip.ip_len) - + sizeof (pay.packet->ip) - + sizeof (pay.packet->udp); + memcpy (data, &pay.packet->dhcp, len); + have_data = true; + } + } + + /* Update the buffer_pos pointer */ + bpf.buffer += BPF_WORDALIGN (bpf.packet->bh_hdrlen + + bpf.packet->bh_caplen); + if ((unsigned) (bpf.buffer - buffer) < *buffer_len) + *buffer_pos = bpf.buffer - buffer; + else + *buffer_pos = 0; + + if (have_data) + return len; + + if (*buffer_pos == 0) + break; + } + + /* No valid packets left, so return */ + *buffer_pos = 0; + return -1; +} + +#elif __linux__ + +int open_socket (interface_t *iface, int protocol) +{ + int fd; + union sockunion { + struct sockaddr sa; + struct sockaddr_in sin; + struct sockaddr_ll sll; + struct sockaddr_storage ss; + } su; + struct sock_fprog pf; + struct ifreq ifr; + int n = 1; + + /* We need to bind to a port, otherwise Linux generate ICMP messages + * that cannot contect the port when we have an address. + * We don't actually use this fd at all, instead using our packet + * filter socket. */ + if (iface->listen_fd == -1 && protocol == ETHERTYPE_IP) { + if ((fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { + logger (LOG_ERR, "socket: %s", strerror (errno)); + } else { + memset (&su, 0, sizeof (su)); + su.sin.sin_family = AF_INET; + su.sin.sin_port = htons (DHCP_CLIENT_PORT); + if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, + &n, sizeof (n)) == -1) + logger (LOG_ERR, "SO_REUSEADDR: %s", + strerror (errno)); + if (setsockopt (fd, SOL_SOCKET, SO_RCVBUF, + &n, sizeof (n)) == -1) + logger (LOG_ERR, "SO_RCVBUF: %s", + strerror (errno)); + memset (&ifr, 0, sizeof (ifr)); + strncpy (ifr.ifr_name, iface->name, + sizeof (ifr.ifr_name)); + if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE, + &ifr, sizeof (ifr)) == -1) + logger (LOG_ERR, "SO_SOBINDTODEVICE: %s", + strerror (errno)); + if (bind (fd, &su.sa, sizeof (su)) == -1) { + logger (LOG_ERR, "bind: %s", strerror (errno)); + close (fd); + } else { + iface->listen_fd = fd; + close_on_exec (fd); + } + } + } + + if ((fd = socket (PF_PACKET, SOCK_DGRAM, htons (protocol))) == -1) { + logger (LOG_ERR, "socket: %s", strerror (errno)); + return (-1); + } + close_on_exec (fd); + + memset (&su, 0, sizeof (su)); + su.sll.sll_family = PF_PACKET; + su.sll.sll_protocol = htons (protocol); + if (! (su.sll.sll_ifindex = if_nametoindex (iface->name))) { + logger (LOG_ERR, + "if_nametoindex: no index for interface `%s'", + iface->name); + close (fd); + return (-1); + } + + /* Install the DHCP filter */ + memset (&pf, 0, sizeof (pf)); + if (protocol == ETHERTYPE_ARP) { + pf.filter = arp_bpf_filter; + pf.len = sizeof (arp_bpf_filter) / sizeof (arp_bpf_filter[0]); + } else { + pf.filter = dhcp_bpf_filter; + pf.len = sizeof (dhcp_bpf_filter) / sizeof (dhcp_bpf_filter[0]); + } + if (setsockopt (fd, SOL_SOCKET, SO_ATTACH_FILTER, + &pf, sizeof (pf)) != 0) + { + logger (LOG_ERR, "SO_ATTACH_FILTER: %s", strerror (errno)); + close (fd); + return (-1); + } + + if (bind (fd, &su.sa, sizeof (su)) == -1) { + logger (LOG_ERR, "bind: %s", strerror (errno)); + close (fd); + return (-1); + } + + if (iface->fd > -1) + close (iface->fd); + iface->fd = fd; + iface->socket_protocol = protocol; + iface->buffer_length = BUFFER_LENGTH; + + return (fd); +} + +ssize_t send_packet (const interface_t *iface, int type, + const unsigned char *data, size_t len) +{ + union sockunion { + struct sockaddr sa; + struct sockaddr_ll sll; + struct sockaddr_storage ss; + } su; + ssize_t retval; + + if (! iface) + return (-1); + + memset (&su, 0, sizeof (su)); + su.sll.sll_family = AF_PACKET; + su.sll.sll_protocol = htons (type); + + if (! (su.sll.sll_ifindex = if_nametoindex (iface->name))) { + logger (LOG_ERR, "if_nametoindex: no index for interface `%s'", + iface->name); + return (-1); + } + + su.sll.sll_hatype = htons (iface->family); + su.sll.sll_halen = iface->hwlen; + if (iface->family == ARPHRD_INFINIBAND) + memcpy (&su.sll.sll_addr, + &ipv4_bcast_addr, sizeof (ipv4_bcast_addr)); + else + memset (&su.sll.sll_addr, 0xff, iface->hwlen); + + if ((retval = sendto (iface->fd, data, len, 0, &su.sa, + sizeof (su))) == -1) + + logger (LOG_ERR, "sendto: %s", strerror (errno)); + return (retval); +} + +/* Linux has no need for the buffer as we can read as much as we want. + * We only have the buffer listed to keep the same API. */ +ssize_t get_packet (const interface_t *iface, unsigned char *data, + unsigned char *buffer, + size_t *buffer_len, size_t *buffer_pos) +{ + ssize_t bytes; + union + { + unsigned char *buffer; + struct udp_dhcp_packet *packet; + } pay; + + /* We don't use the given buffer, but we need to rewind the position */ + *buffer_pos = 0; + + memset (buffer, 0, iface->buffer_length); + bytes = read (iface->fd, buffer, iface->buffer_length); + + if (bytes == -1) { + struct timespec ts; + logger (LOG_ERR, "read: %s", strerror (errno)); + ts.tv_sec = 3; + ts.tv_nsec = 0; + nanosleep (&ts, NULL); + return (-1); + } + + *buffer_len = bytes; + /* If it's an ARP reply, then just send it back */ + if (iface->socket_protocol == ETHERTYPE_ARP) { + memcpy (data, buffer, bytes); + return (bytes); + } + + if ((unsigned) bytes < (sizeof (pay.packet->ip) + + sizeof (pay.packet->udp))) + { + logger (LOG_DEBUG, "message too short, ignoring"); + return (-1); + } + + pay.buffer = buffer; + if (bytes < ntohs (pay.packet->ip.ip_len)) { + logger (LOG_DEBUG, "truncated packet, ignoring"); + return (-1); + } + + if (valid_dhcp_packet (buffer) == -1) + return (-1); + + bytes = ntohs (pay.packet->ip.ip_len) - + (sizeof (pay.packet->ip) + sizeof (pay.packet->udp)); + memcpy (data, &pay.packet->dhcp, bytes); + return (bytes); +} + +#else + #error "Platform not supported!" + #error "We currently support BPF and Linux sockets." + #error "Other platforms may work using BPF. If yours does, please let me know" + #error "so I can add it to our list." +#endif diff --git a/customdhcpcd/src/socket.h b/customdhcpcd/src/socket.h new file mode 100644 index 0000000..bdf26d0 --- /dev/null +++ b/customdhcpcd/src/socket.h @@ -0,0 +1,46 @@ +/* + * dhcpcd - DHCP client daemon + * Copyright 2006-2008 Roy Marples + * All rights reserved + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef SOCKET_H +#define SOCKET_H + +#include + +#include "dhcp.h" +#include "interface.h" + +void setup_packet_filters (void); +void make_dhcp_packet(struct udp_dhcp_packet *packet, + const unsigned char *data, size_t length, + struct in_addr source, struct in_addr dest); + +int open_socket (interface_t *iface, int protocol); +ssize_t send_packet (const interface_t *iface, int type, + const unsigned char *data, size_t len); +ssize_t get_packet (const interface_t *iface, unsigned char *data, + unsigned char *buffer, size_t *buffer_len, size_t *buffer_pos); +#endif diff --git a/customdhcpcd/src/status.h b/customdhcpcd/src/status.h new file mode 100644 index 0000000..0a1dc8a --- /dev/null +++ b/customdhcpcd/src/status.h @@ -0,0 +1,19 @@ +/* + * status.h + * + * Created on: Jul 11, 2011 + * Author: niklas + */ + +#ifndef STATUS_H_ +#define STATUS_H_ + +#define DHCPCD_EXIT 9 +#define DHCPCD_ARP_TEST 10 +#define DHCPCD_CONFIGURE 11 +#define DHCPCD_WRITE 12 +#define DHCPCD_LOG 13 + + + +#endif /* STATUS_H_ */ diff --git a/workspace/LogReceiver/LogReceiver b/workspace/LogReceiver/LogReceiver deleted file mode 100755 index a16897b..0000000 Binary files a/workspace/LogReceiver/LogReceiver and /dev/null differ diff --git a/workspace/LogReceiver/LogReceiver.pro b/workspace/LogReceiver/LogReceiver.pro deleted file mode 100644 index 19300fa..0000000 --- a/workspace/LogReceiver/LogReceiver.pro +++ /dev/null @@ -1,19 +0,0 @@ -TEMPLATE = app -TARGET = LogReceiver -QT += core \ - gui \ - network -LIBS += -lsysfs -INCLUDEPATH += ../customdhcpcd/src -HEADERS += ndgui.h \ - logreceiver.h \ - abortbootdialog.h \ - chooseinterfacedialog.h -SOURCES += ndgui.cpp \ - main.cpp \ - logreceiver.cpp \ - abortbootdialog.cpp \ - chooseinterfacedialog.cpp -FORMS += ndgui.ui \ - logreceiver.ui -RESOURCES += diff --git a/workspace/LogReceiver/Makefile b/workspace/LogReceiver/Makefile deleted file mode 100644 index cd541af..0000000 --- a/workspace/LogReceiver/Makefile +++ /dev/null @@ -1,278 +0,0 @@ -############################################################################# -# Makefile for building: LogReceiver -# Generated by qmake (2.01a) (Qt 4.7.2) on: Wed Aug 24 10:11:58 2011 -# Project: LogReceiver.pro -# Template: app -# Command: /usr/local/Trolltech/QtEmbedded-4.7.2/bin/qmake -o Makefile LogReceiver.pro -############################################################################# - -####### Compiler, tools and options - -CC = gcc -CXX = g++ -DEFINES = -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED -CFLAGS = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES) -CXXFLAGS = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES) -INCPATH = -I/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/qws/linux-x86-g++ -I. -I/usr/local/Trolltech/QtEmbedded-4.7.2/include/QtCore -I/usr/local/Trolltech/QtEmbedded-4.7.2/include/QtNetwork -I/usr/local/Trolltech/QtEmbedded-4.7.2/include/QtGui -I/usr/local/Trolltech/QtEmbedded-4.7.2/include -I../customdhcpcd/src -I. -I. -LINK = g++ -LFLAGS = -Wl,-O1 -Wl,-rpath,/usr/local/Trolltech/QtEmbedded-4.7.2/lib -LIBS = $(SUBLIBS) -L/usr/local/Trolltech/QtEmbedded-4.7.2/lib -lsysfs -lQtGui -L/usr/local/Trolltech/QtEmbedded-4.7.2/lib -lQtNetwork -lQtCore -lpthread -AR = ar cqs -RANLIB = -QMAKE = /usr/local/Trolltech/QtEmbedded-4.7.2/bin/qmake -TAR = tar -cf -COMPRESS = gzip -9f -COPY = cp -f -SED = sed -COPY_FILE = $(COPY) -COPY_DIR = $(COPY) -r -STRIP = strip -INSTALL_FILE = install -m 644 -p -INSTALL_DIR = $(COPY_DIR) -INSTALL_PROGRAM = install -m 755 -p -DEL_FILE = rm -f -SYMLINK = ln -f -s -DEL_DIR = rmdir -MOVE = mv -f -CHK_DIR_EXISTS= test -d -MKDIR = mkdir -p - -####### Output directory - -OBJECTS_DIR = ./ - -####### Files - -SOURCES = ndgui.cpp \ - main.cpp \ - logreceiver.cpp \ - abortbootdialog.cpp \ - chooseinterfacedialog.cpp moc_ndgui.cpp \ - moc_logreceiver.cpp \ - moc_abortbootdialog.cpp \ - moc_chooseinterfacedialog.cpp -OBJECTS = ndgui.o \ - main.o \ - logreceiver.o \ - abortbootdialog.o \ - chooseinterfacedialog.o \ - moc_ndgui.o \ - moc_logreceiver.o \ - moc_abortbootdialog.o \ - moc_chooseinterfacedialog.o -DIST = /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/g++.conf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/unix.conf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/linux.conf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/qws.conf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/qconfig.pri \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/modules/qt_webkit_version.pri \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt_functions.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt_config.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/exclusive_builds.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/default_pre.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/release.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/default_post.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/warn_on.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/unix/thread.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/moc.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/resources.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/uic.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/yacc.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/lex.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/include_source_dir.prf \ - LogReceiver.pro -QMAKE_TARGET = LogReceiver -DESTDIR = -TARGET = LogReceiver - -first: all -####### Implicit rules - -.SUFFIXES: .o .c .cpp .cc .cxx .C - -.cpp.o: - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" - -.cc.o: - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" - -.cxx.o: - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" - -.C.o: - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" - -.c.o: - $(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<" - -####### Build rules - -all: Makefile $(TARGET) - -$(TARGET): ui_ndgui.h ui_logreceiver.h $(OBJECTS) - $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS) - -Makefile: LogReceiver.pro /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/qws/linux-x86-g++/qmake.conf /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/g++.conf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/unix.conf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/linux.conf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/qws.conf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/qconfig.pri \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/modules/qt_webkit_version.pri \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt_functions.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt_config.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/exclusive_builds.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/default_pre.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/release.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/default_post.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/warn_on.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/unix/thread.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/moc.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/resources.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/uic.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/yacc.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/lex.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/include_source_dir.prf \ - /usr/local/Trolltech/QtEmbedded-4.7.2/lib/libQtGui.prl \ - /usr/local/Trolltech/QtEmbedded-4.7.2/lib/libQtNetwork.prl \ - /usr/local/Trolltech/QtEmbedded-4.7.2/lib/libQtCore.prl - $(QMAKE) -o Makefile LogReceiver.pro -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/g++.conf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/unix.conf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/linux.conf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/common/qws.conf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/qconfig.pri: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/modules/qt_webkit_version.pri: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt_functions.prf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt_config.prf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/exclusive_builds.prf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/default_pre.prf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/release.prf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/default_post.prf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/warn_on.prf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/qt.prf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/unix/thread.prf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/moc.prf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/resources.prf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/uic.prf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/yacc.prf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/lex.prf: -/usr/local/Trolltech/QtEmbedded-4.7.2/mkspecs/features/include_source_dir.prf: -/usr/local/Trolltech/QtEmbedded-4.7.2/lib/libQtGui.prl: -/usr/local/Trolltech/QtEmbedded-4.7.2/lib/libQtNetwork.prl: -/usr/local/Trolltech/QtEmbedded-4.7.2/lib/libQtCore.prl: -qmake: FORCE - @$(QMAKE) -o Makefile LogReceiver.pro - -dist: - @$(CHK_DIR_EXISTS) .tmp/LogReceiver1.0.0 || $(MKDIR) .tmp/LogReceiver1.0.0 - $(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/LogReceiver1.0.0/ && $(COPY_FILE) --parents ndgui.h logreceiver.h abortbootdialog.h chooseinterfacedialog.h .tmp/LogReceiver1.0.0/ && $(COPY_FILE) --parents ndgui.cpp main.cpp logreceiver.cpp abortbootdialog.cpp chooseinterfacedialog.cpp .tmp/LogReceiver1.0.0/ && $(COPY_FILE) --parents ndgui.ui logreceiver.ui .tmp/LogReceiver1.0.0/ && (cd `dirname .tmp/LogReceiver1.0.0` && $(TAR) LogReceiver1.0.0.tar LogReceiver1.0.0 && $(COMPRESS) LogReceiver1.0.0.tar) && $(MOVE) `dirname .tmp/LogReceiver1.0.0`/LogReceiver1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/LogReceiver1.0.0 - - -clean:compiler_clean - -$(DEL_FILE) $(OBJECTS) - -$(DEL_FILE) *~ core *.core - - -####### Sub-libraries - -distclean: clean - -$(DEL_FILE) $(TARGET) - -$(DEL_FILE) Makefile - - -check: first - -mocclean: compiler_moc_header_clean compiler_moc_source_clean - -mocables: compiler_moc_header_make_all compiler_moc_source_make_all - -compiler_moc_header_make_all: moc_ndgui.cpp moc_logreceiver.cpp moc_abortbootdialog.cpp moc_chooseinterfacedialog.cpp -compiler_moc_header_clean: - -$(DEL_FILE) moc_ndgui.cpp moc_logreceiver.cpp moc_abortbootdialog.cpp moc_chooseinterfacedialog.cpp -moc_ndgui.cpp: ui_ndgui.h \ - logreceiver.h \ - chooseinterfacedialog.h \ - abortbootdialog.h \ - ndgui.h - /usr/local/Trolltech/QtEmbedded-4.7.2/bin/moc $(DEFINES) $(INCPATH) ndgui.h -o moc_ndgui.cpp - -moc_logreceiver.cpp: logreceiver.h - /usr/local/Trolltech/QtEmbedded-4.7.2/bin/moc $(DEFINES) $(INCPATH) logreceiver.h -o moc_logreceiver.cpp - -moc_abortbootdialog.cpp: abortbootdialog.h - /usr/local/Trolltech/QtEmbedded-4.7.2/bin/moc $(DEFINES) $(INCPATH) abortbootdialog.h -o moc_abortbootdialog.cpp - -moc_chooseinterfacedialog.cpp: chooseinterfacedialog.h - /usr/local/Trolltech/QtEmbedded-4.7.2/bin/moc $(DEFINES) $(INCPATH) chooseinterfacedialog.h -o moc_chooseinterfacedialog.cpp - -compiler_rcc_make_all: -compiler_rcc_clean: -compiler_image_collection_make_all: qmake_image_collection.cpp -compiler_image_collection_clean: - -$(DEL_FILE) qmake_image_collection.cpp -compiler_moc_source_make_all: -compiler_moc_source_clean: -compiler_uic_make_all: ui_ndgui.h ui_logreceiver.h -compiler_uic_clean: - -$(DEL_FILE) ui_ndgui.h ui_logreceiver.h -ui_ndgui.h: ndgui.ui - /usr/local/Trolltech/QtEmbedded-4.7.2/bin/uic ndgui.ui -o ui_ndgui.h - -ui_logreceiver.h: logreceiver.ui - /usr/local/Trolltech/QtEmbedded-4.7.2/bin/uic logreceiver.ui -o ui_logreceiver.h - -compiler_yacc_decl_make_all: -compiler_yacc_decl_clean: -compiler_yacc_impl_make_all: -compiler_yacc_impl_clean: -compiler_lex_make_all: -compiler_lex_clean: -compiler_clean: compiler_moc_header_clean compiler_uic_clean - -####### Compile - -ndgui.o: ndgui.cpp ndgui.h \ - ui_ndgui.h \ - logreceiver.h \ - chooseinterfacedialog.h \ - abortbootdialog.h - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o ndgui.o ndgui.cpp - -main.o: main.cpp ndgui.h \ - ui_ndgui.h \ - logreceiver.h \ - chooseinterfacedialog.h \ - abortbootdialog.h - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o main.o main.cpp - -logreceiver.o: logreceiver.cpp logreceiver.h - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o logreceiver.o logreceiver.cpp - -abortbootdialog.o: abortbootdialog.cpp abortbootdialog.h - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o abortbootdialog.o abortbootdialog.cpp - -chooseinterfacedialog.o: chooseinterfacedialog.cpp chooseinterfacedialog.h - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o chooseinterfacedialog.o chooseinterfacedialog.cpp - -moc_ndgui.o: moc_ndgui.cpp - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_ndgui.o moc_ndgui.cpp - -moc_logreceiver.o: moc_logreceiver.cpp - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_logreceiver.o moc_logreceiver.cpp - -moc_abortbootdialog.o: moc_abortbootdialog.cpp - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_abortbootdialog.o moc_abortbootdialog.cpp - -moc_chooseinterfacedialog.o: moc_chooseinterfacedialog.cpp - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_chooseinterfacedialog.o moc_chooseinterfacedialog.cpp - -####### Install - -install: FORCE - -uninstall: FORCE - -FORCE: - diff --git a/workspace/LogReceiver/Makefile.Debug b/workspace/LogReceiver/Makefile.Debug deleted file mode 100644 index 29db2ad..0000000 --- a/workspace/LogReceiver/Makefile.Debug +++ /dev/null @@ -1,178 +0,0 @@ -############################################################################# -# Makefile for building: LogReceiver -# Generated by qmake (2.01a) (Qt 4.7.2) on: Wed Jul 6 13:39:02 2011 -# Project: LogReceiver.pro -# Template: app -############################################################################# - -####### Compiler, tools and options - -CC = gcc -CXX = g++ -DEFINES = -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED -CFLAGS = -pipe -g -Wall -W -D_REENTRANT $(DEFINES) -CXXFLAGS = -pipe -g -Wall -W -D_REENTRANT $(DEFINES) -INCPATH = -I/usr/local/Trolltech/Qt-4.7.2/mkspecs/linux-g++ -I. -I/usr/local/Trolltech/Qt-4.7.2/include/QtCore -I/usr/local/Trolltech/Qt-4.7.2/include/QtNetwork -I/usr/local/Trolltech/Qt-4.7.2/include/QtGui -I/usr/local/Trolltech/Qt-4.7.2/include -Idebug -I. -LINK = g++ -LFLAGS = -Wl,-rpath,/usr/local/Trolltech/Qt-4.7.2/lib -LIBS = $(SUBLIBS) -L/usr/local/Trolltech/Qt-4.7.2/lib -lQtGui -L/usr/local/Trolltech/Qt-4.7.2/lib -L/usr/X11R6/lib -lQtNetwork -lQtCore -lpthread -AR = ar cqs -RANLIB = -QMAKE = /usr/local/Trolltech/Qt-4.7.2/bin/qmake -TAR = tar -cf -COMPRESS = gzip -9f -COPY = cp -f -SED = sed -COPY_FILE = $(COPY) -COPY_DIR = $(COPY) -r -STRIP = strip -INSTALL_FILE = install -m 644 -p -INSTALL_DIR = $(COPY_DIR) -INSTALL_PROGRAM = install -m 755 -p -DEL_FILE = rm -f -SYMLINK = ln -f -s -DEL_DIR = rmdir -MOVE = mv -f -CHK_DIR_EXISTS= test -d -MKDIR = mkdir -p - -####### Output directory - -OBJECTS_DIR = debug/ - -####### Files - -SOURCES = main.cpp \ - logreceiver.cpp debug/moc_logreceiver.cpp -OBJECTS = debug/main.o \ - debug/logreceiver.o \ - debug/moc_logreceiver.o -DIST = /usr/local/Trolltech/Qt-4.7.2/mkspecs/common/g++.conf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/common/unix.conf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/common/linux.conf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/qconfig.pri \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/modules/qt_webkit_version.pri \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/qt_functions.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/qt_config.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/exclusive_builds.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/default_pre.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/debug.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/debug_and_release.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/default_post.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/build_pass.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/warn_on.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/qt.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/unix/thread.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/moc.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/resources.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/uic.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/yacc.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/lex.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/include_source_dir.prf \ - LogReceiver.pro -QMAKE_TARGET = LogReceiver -DESTDIR = -TARGET = LogReceiver - -first: all -####### Implicit rules - -.SUFFIXES: .o .c .cpp .cc .cxx .C - -.cpp.o: - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" - -.cc.o: - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" - -.cxx.o: - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" - -.C.o: - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" - -.c.o: - $(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<" - -####### Build rules - -all: Makefile.Debug $(TARGET) - -$(TARGET): ui_logreceiver.h $(OBJECTS) - $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS) - -qmake: FORCE - @$(QMAKE) CONFIG+=debug_and_release -o Makefile.Debug LogReceiver.pro - -dist: - @$(CHK_DIR_EXISTS) debug/LogReceiver1.0.0 || $(MKDIR) debug/LogReceiver1.0.0 - $(COPY_FILE) --parents $(SOURCES) $(DIST) debug/LogReceiver1.0.0/ && $(COPY_FILE) --parents logreceiver.h debug/LogReceiver1.0.0/ && $(COPY_FILE) --parents main.cpp logreceiver.cpp debug/LogReceiver1.0.0/ && $(COPY_FILE) --parents logreceiver.ui debug/LogReceiver1.0.0/ && (cd `dirname debug/LogReceiver1.0.0` && $(TAR) LogReceiver1.0.0.tar LogReceiver1.0.0 && $(COMPRESS) LogReceiver1.0.0.tar) && $(MOVE) `dirname debug/LogReceiver1.0.0`/LogReceiver1.0.0.tar.gz . && $(DEL_FILE) -r debug/LogReceiver1.0.0 - - -clean:compiler_clean - -$(DEL_FILE) $(OBJECTS) - -$(DEL_FILE) *~ core *.core - - -####### Sub-libraries - -distclean: clean - -$(DEL_FILE) $(TARGET) - -$(DEL_FILE) Makefile.Debug - - -check: first - -mocclean: compiler_moc_header_clean compiler_moc_source_clean - -mocables: compiler_moc_header_make_all compiler_moc_source_make_all - -compiler_moc_header_make_all: debug/moc_logreceiver.cpp -compiler_moc_header_clean: - -$(DEL_FILE) debug/moc_logreceiver.cpp -debug/moc_logreceiver.cpp: ui_logreceiver.h \ - logreceiver.h - /usr/local/Trolltech/Qt-4.7.2/bin/moc $(DEFINES) $(INCPATH) logreceiver.h -o debug/moc_logreceiver.cpp - -compiler_rcc_make_all: -compiler_rcc_clean: -compiler_image_collection_make_all: qmake_image_collection.cpp -compiler_image_collection_clean: - -$(DEL_FILE) qmake_image_collection.cpp -compiler_moc_source_make_all: -compiler_moc_source_clean: -compiler_uic_make_all: ui_logreceiver.h -compiler_uic_clean: - -$(DEL_FILE) ui_logreceiver.h -ui_logreceiver.h: logreceiver.ui - /usr/local/Trolltech/Qt-4.7.2/bin/uic logreceiver.ui -o ui_logreceiver.h - -compiler_yacc_decl_make_all: -compiler_yacc_decl_clean: -compiler_yacc_impl_make_all: -compiler_yacc_impl_clean: -compiler_lex_make_all: -compiler_lex_clean: -compiler_clean: compiler_moc_header_clean compiler_uic_clean - -####### Compile - -debug/main.o: main.cpp logreceiver.h \ - ui_logreceiver.h - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o debug/main.o main.cpp - -debug/logreceiver.o: logreceiver.cpp logreceiver.h \ - ui_logreceiver.h - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o debug/logreceiver.o logreceiver.cpp - -debug/moc_logreceiver.o: debug/moc_logreceiver.cpp - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o debug/moc_logreceiver.o debug/moc_logreceiver.cpp - -####### Install - -install: FORCE - -uninstall: FORCE - -FORCE: - diff --git a/workspace/LogReceiver/Makefile.Release b/workspace/LogReceiver/Makefile.Release deleted file mode 100644 index 64e6a9f..0000000 --- a/workspace/LogReceiver/Makefile.Release +++ /dev/null @@ -1,178 +0,0 @@ -############################################################################# -# Makefile for building: LogReceiver -# Generated by qmake (2.01a) (Qt 4.7.2) on: Wed Jul 6 13:39:02 2011 -# Project: LogReceiver.pro -# Template: app -############################################################################# - -####### Compiler, tools and options - -CC = gcc -CXX = g++ -DEFINES = -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED -CFLAGS = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES) -CXXFLAGS = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES) -INCPATH = -I/usr/local/Trolltech/Qt-4.7.2/mkspecs/linux-g++ -I. -I/usr/local/Trolltech/Qt-4.7.2/include/QtCore -I/usr/local/Trolltech/Qt-4.7.2/include/QtNetwork -I/usr/local/Trolltech/Qt-4.7.2/include/QtGui -I/usr/local/Trolltech/Qt-4.7.2/include -Irelease -I. -LINK = g++ -LFLAGS = -Wl,-O1 -Wl,-rpath,/usr/local/Trolltech/Qt-4.7.2/lib -LIBS = $(SUBLIBS) -L/usr/local/Trolltech/Qt-4.7.2/lib -lQtGui -L/usr/local/Trolltech/Qt-4.7.2/lib -L/usr/X11R6/lib -lQtNetwork -lQtCore -lpthread -AR = ar cqs -RANLIB = -QMAKE = /usr/local/Trolltech/Qt-4.7.2/bin/qmake -TAR = tar -cf -COMPRESS = gzip -9f -COPY = cp -f -SED = sed -COPY_FILE = $(COPY) -COPY_DIR = $(COPY) -r -STRIP = strip -INSTALL_FILE = install -m 644 -p -INSTALL_DIR = $(COPY_DIR) -INSTALL_PROGRAM = install -m 755 -p -DEL_FILE = rm -f -SYMLINK = ln -f -s -DEL_DIR = rmdir -MOVE = mv -f -CHK_DIR_EXISTS= test -d -MKDIR = mkdir -p - -####### Output directory - -OBJECTS_DIR = release/ - -####### Files - -SOURCES = main.cpp \ - logreceiver.cpp release/moc_logreceiver.cpp -OBJECTS = release/main.o \ - release/logreceiver.o \ - release/moc_logreceiver.o -DIST = /usr/local/Trolltech/Qt-4.7.2/mkspecs/common/g++.conf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/common/unix.conf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/common/linux.conf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/qconfig.pri \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/modules/qt_webkit_version.pri \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/qt_functions.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/qt_config.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/exclusive_builds.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/default_pre.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/release.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/debug_and_release.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/default_post.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/build_pass.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/warn_on.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/qt.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/unix/thread.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/moc.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/resources.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/uic.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/yacc.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/lex.prf \ - /usr/local/Trolltech/Qt-4.7.2/mkspecs/features/include_source_dir.prf \ - LogReceiver.pro -QMAKE_TARGET = LogReceiver -DESTDIR = -TARGET = LogReceiver - -first: all -####### Implicit rules - -.SUFFIXES: .o .c .cpp .cc .cxx .C - -.cpp.o: - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" - -.cc.o: - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" - -.cxx.o: - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" - -.C.o: - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" - -.c.o: - $(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<" - -####### Build rules - -all: Makefile.Release $(TARGET) - -$(TARGET): ui_logreceiver.h $(OBJECTS) - $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS) - -qmake: FORCE - @$(QMAKE) CONFIG+=debug_and_release -o Makefile.Release LogReceiver.pro - -dist: - @$(CHK_DIR_EXISTS) release/LogReceiver1.0.0 || $(MKDIR) release/LogReceiver1.0.0 - $(COPY_FILE) --parents $(SOURCES) $(DIST) release/LogReceiver1.0.0/ && $(COPY_FILE) --parents logreceiver.h release/LogReceiver1.0.0/ && $(COPY_FILE) --parents main.cpp logreceiver.cpp release/LogReceiver1.0.0/ && $(COPY_FILE) --parents logreceiver.ui release/LogReceiver1.0.0/ && (cd `dirname release/LogReceiver1.0.0` && $(TAR) LogReceiver1.0.0.tar LogReceiver1.0.0 && $(COMPRESS) LogReceiver1.0.0.tar) && $(MOVE) `dirname release/LogReceiver1.0.0`/LogReceiver1.0.0.tar.gz . && $(DEL_FILE) -r release/LogReceiver1.0.0 - - -clean:compiler_clean - -$(DEL_FILE) $(OBJECTS) - -$(DEL_FILE) *~ core *.core - - -####### Sub-libraries - -distclean: clean - -$(DEL_FILE) $(TARGET) - -$(DEL_FILE) Makefile.Release - - -check: first - -mocclean: compiler_moc_header_clean compiler_moc_source_clean - -mocables: compiler_moc_header_make_all compiler_moc_source_make_all - -compiler_moc_header_make_all: release/moc_logreceiver.cpp -compiler_moc_header_clean: - -$(DEL_FILE) release/moc_logreceiver.cpp -release/moc_logreceiver.cpp: ui_logreceiver.h \ - logreceiver.h - /usr/local/Trolltech/Qt-4.7.2/bin/moc $(DEFINES) $(INCPATH) logreceiver.h -o release/moc_logreceiver.cpp - -compiler_rcc_make_all: -compiler_rcc_clean: -compiler_image_collection_make_all: qmake_image_collection.cpp -compiler_image_collection_clean: - -$(DEL_FILE) qmake_image_collection.cpp -compiler_moc_source_make_all: -compiler_moc_source_clean: -compiler_uic_make_all: ui_logreceiver.h -compiler_uic_clean: - -$(DEL_FILE) ui_logreceiver.h -ui_logreceiver.h: logreceiver.ui - /usr/local/Trolltech/Qt-4.7.2/bin/uic logreceiver.ui -o ui_logreceiver.h - -compiler_yacc_decl_make_all: -compiler_yacc_decl_clean: -compiler_yacc_impl_make_all: -compiler_yacc_impl_clean: -compiler_lex_make_all: -compiler_lex_clean: -compiler_clean: compiler_moc_header_clean compiler_uic_clean - -####### Compile - -release/main.o: main.cpp logreceiver.h \ - ui_logreceiver.h - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o release/main.o main.cpp - -release/logreceiver.o: logreceiver.cpp logreceiver.h \ - ui_logreceiver.h - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o release/logreceiver.o logreceiver.cpp - -release/moc_logreceiver.o: release/moc_logreceiver.cpp - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o release/moc_logreceiver.o release/moc_logreceiver.cpp - -####### Install - -install: FORCE - -uninstall: FORCE - -FORCE: - diff --git a/workspace/LogReceiver/abortbootdialog.cpp b/workspace/LogReceiver/abortbootdialog.cpp deleted file mode 100644 index 1614f6b..0000000 --- a/workspace/LogReceiver/abortbootdialog.cpp +++ /dev/null @@ -1,98 +0,0 @@ -#include - -#include "abortbootdialog.h" - -AbortBootDialog::AbortBootDialog(QWidget *parent) : - QDialog(parent) -{ - oneMinuteCountdown = 60; - - createContentGroupBox(); - createButtonGroupBox(); - - createTimer(); - - mainLayout = new QVBoxLayout; - mainLayout->addWidget(contentGroupBox); - mainLayout->addWidget(buttonGroupBox); - - setLayout(mainLayout); - setWindowTitle(tr("Abort Boot")); -} -void AbortBootDialog::createContentGroupBox() -{ - contentGroupBox = new QGroupBox; - timerLabel = new QLabel(QString::number(oneMinuteCountdown)); - QHBoxLayout *layout = new QHBoxLayout; - - - layout->addWidget(new QLabel(tr("test"))); - layout->addWidget(timerLabel); - contentGroupBox->setLayout(layout); -} - -void AbortBootDialog::createButtonGroupBox() -{ - buttonGroupBox = new QGroupBox; - QHBoxLayout *layout = new QHBoxLayout; - - QPushButton *shutDownButton = new QPushButton(tr("Shut Down")); - shutDownButton->setDefault(true); - QPushButton *restartButton = new QPushButton(tr("Restart")); - restartButton->setAutoDefault(false); - QPushButton *showLogButton = new QPushButton(tr("Show Log")); - showLogButton->setAutoDefault(false); - - connect(showLogButton, SIGNAL(clicked()),this, SLOT(showLogButtonClicked())); - connect(shutDownButton, SIGNAL(clicked()), this, SLOT(shutDownButtonClicked())); - connect(restartButton, SIGNAL(clicked()), this, SLOT(restartButtonClicked())); - - layout->addWidget(showLogButton); - layout->addWidget(restartButton); - layout->addWidget(shutDownButton); - buttonGroupBox->setLayout(layout); -} - -void AbortBootDialog::createTimer() -{ - timer = new QTimer(this); - connect(timer, SIGNAL(timeout()),this, SLOT(timerLabelUpdate())); - timer->start(1000); -} - -void AbortBootDialog::showLogButtonClicked() -{ - qDebug() << "show log button clicked"; - emit showLogSignal(); -} - -void AbortBootDialog::shutDownButtonClicked() -{ - qDebug() << "shut down button clicked"; - emit shutDownSignal(); -} - -void AbortBootDialog::restartButtonClicked() -{ - qDebug() << "restart button clicked"; - emit restartSignal(); -} - -void AbortBootDialog::timerLabelUpdate() -{ - oneMinuteCountdown = oneMinuteCountdown -1; - timerLabel->setText(QString::number(oneMinuteCountdown)); - if(oneMinuteCountdown <= 0) - { - timer->stop(); - emit shutDownSignal(); - } - -} - -void AbortBootDialog::closeDialog() -{ - this->hide(); - this->killTimer(timer->timerId()); - this->close(); -} diff --git a/workspace/LogReceiver/abortbootdialog.h b/workspace/LogReceiver/abortbootdialog.h deleted file mode 100644 index 157331b..0000000 --- a/workspace/LogReceiver/abortbootdialog.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef ABORTBOOTDIALOG_H -#define ABORTBOOTDIALOG_H - -#include -#include "qboxlayout.h" -#include "qgroupbox.h" -#include "qcombobox.h" -#include "qlabel.h" - - -class AbortBootDialog : public QDialog -{ - Q_OBJECT -public: - explicit AbortBootDialog(QWidget *parent = 0); - - void closeDialog(); -private slots: - void showLogButtonClicked(); - void shutDownButtonClicked(); - void restartButtonClicked(); - void timerLabelUpdate(); - -private: - QVBoxLayout *mainLayout; - QGroupBox *contentGroupBox; - QGroupBox *buttonGroupBox; - QLabel *timerLabel; - QTimer *timer; - int oneMinuteCountdown; - - void createContentGroupBox(); - void createButtonGroupBox(); - void createTimer(); - - -signals: - void showLogSignal(); - void shutDownSignal(); - void restartSignal(); - -public slots: - -}; - -#endif // ABORTBOOTDIALOG_H diff --git a/workspace/LogReceiver/chooseinterfacedialog.cpp b/workspace/LogReceiver/chooseinterfacedialog.cpp deleted file mode 100644 index b7a37e8..0000000 --- a/workspace/LogReceiver/chooseinterfacedialog.cpp +++ /dev/null @@ -1,98 +0,0 @@ -#include - -#include "chooseinterfacedialog.h" - -ChooseInterfaceDialog::ChooseInterfaceDialog(QStringList &interfaces, QWidget *parent) : - QDialog(parent) -{ - oneMinuteCountdown = 60; - - createContentGroupBox(interfaces); - createButtonGroupBox(); - - createTimer(); - - mainLayout = new QVBoxLayout; - mainLayout->addWidget(contentGroupBox); - mainLayout->addWidget(buttonGroupBox); - - setLayout(mainLayout); - setWindowTitle(tr("Choose Interface")); -} - -void ChooseInterfaceDialog::createContentGroupBox(QStringList &interfaces) -{ - contentGroupBox = new QGroupBox; - timerLabel = new QLabel(QString::number(oneMinuteCountdown)); - QVBoxLayout *groupBoxLayout = new QVBoxLayout; - QHBoxLayout *layout = new QHBoxLayout; - - comboBox = new QComboBox; - comboBox->addItems(interfaces); - - layout->addWidget(new QLabel(tr("test"))); - layout->addWidget(comboBox); - - groupBoxLayout->addWidget(timerLabel); - groupBoxLayout->addLayout(layout); - contentGroupBox->setLayout(groupBoxLayout); -} - -void ChooseInterfaceDialog::createButtonGroupBox() -{ - buttonGroupBox = new QGroupBox; - QHBoxLayout *layout = new QHBoxLayout; - - QPushButton *continueButton = new QPushButton(tr("Continue")); - continueButton->setDefault(true); - QPushButton *shutDownButton = new QPushButton(tr("Shut Down")); - shutDownButton->setAutoDefault(false); - QPushButton *restartButton = new QPushButton(tr("Restart")); - restartButton->setAutoDefault(false); - - connect(continueButton, SIGNAL(clicked()),this, SLOT(continueButtonClicked())); - connect(shutDownButton, SIGNAL(clicked()), this, SLOT(shutDownButtonClicked())); - connect(restartButton, SIGNAL(clicked()), this, SLOT(restartButtonClicked())); - - layout->addWidget(restartButton); - layout->addWidget(shutDownButton); - layout->addWidget(continueButton); - buttonGroupBox->setLayout(layout); -} - -void ChooseInterfaceDialog::createTimer() -{ - timer = new QTimer(this); - connect(timer, SIGNAL(timeout()),this, SLOT(timerLabelUpdate())); - timer->start(1000); -} - -void ChooseInterfaceDialog::continueButtonClicked() -{ - qDebug() << "continue button clicked"; - emit continueSignal(comboBox->currentText()); -} - -void ChooseInterfaceDialog::shutDownButtonClicked() -{ - qDebug() << "shut down button clicked"; - emit shutDownSignal(); -} - -void ChooseInterfaceDialog::restartButtonClicked() -{ - qDebug() << "restart button clicked"; - emit restartSignal(); -} - -void ChooseInterfaceDialog::timerLabelUpdate() -{ - oneMinuteCountdown = oneMinuteCountdown -1; - timerLabel->setText(QString::number(oneMinuteCountdown)); - if(oneMinuteCountdown <= 0) - { - timer->stop(); - emit continueSignal(comboBox->currentText()); - } - -} diff --git a/workspace/LogReceiver/chooseinterfacedialog.h b/workspace/LogReceiver/chooseinterfacedialog.h deleted file mode 100644 index 48ee038..0000000 --- a/workspace/LogReceiver/chooseinterfacedialog.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef CHOOSEINTERFACEDIALOG_H -#define CHOOSEINTERFACEDIALOG_H - -#include -#include "qboxlayout.h" -#include "qgroupbox.h" -#include "qcombobox.h" -#include "qlabel.h" - -class ChooseInterfaceDialog : public QDialog -{ - Q_OBJECT -public: - explicit ChooseInterfaceDialog(QStringList &interfaces, QWidget *parent = 0); -private slots: - void continueButtonClicked(); - void shutDownButtonClicked(); - void restartButtonClicked(); - void timerLabelUpdate(); - -private: - QVBoxLayout *mainLayout; - QGroupBox *contentGroupBox; - QGroupBox *buttonGroupBox; - QComboBox *comboBox; - QLabel *timerLabel; - QTimer *timer; - int oneMinuteCountdown; - - void createContentGroupBox(QStringList &interfaces); - void createButtonGroupBox(); - void createTimer(); - -signals: - void continueSignal(QString ifName); - void shutDownSignal(); - void restartSignal(); - - -public slots: - -}; - -#endif // CHOOSEINTERFACEDIALOG_H diff --git a/workspace/LogReceiver/logreceiver.cpp b/workspace/LogReceiver/logreceiver.cpp deleted file mode 100644 index 802f442..0000000 --- a/workspace/LogReceiver/logreceiver.cpp +++ /dev/null @@ -1,448 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "logreceiver.h" -#include -#include -#include "status.h" -#include "dhcp.h" - -LogReceiver::LogReceiver() { - - server = new QLocalServer(this); -} - -LogReceiver::~LogReceiver() { - -} - -void LogReceiver::initAndRun(QString serverPath, QString pathToExe, - QStringList* args) { - - if (serverPath != DEFAULT_QTSOCKETADDRESS) { - dhcpcdArguments.append("-q"); - dhcpcdArguments.append(serverPath); - } - if (!server->listen(serverPath)) { - /* - QMessageBox::critical(this, tr("LogReceiver"), tr( - "Unable to start the server: %1.") .arg(server->errorString())); - close(); - */ - // emit signal to the gui that a critial error occoured - QString errorInfo("Unable to start server: "); - qDebug() << "--- \t [LogReceiver::initAndRun] " + errorInfo - << server->errorString(); - emit - abortBoot(errorInfo + server->errorString()); - return; - } - - connect(server, SIGNAL(newConnection()), this, SLOT(handleNewConnection())); - - pathToDhcpcdExe = pathToExe; - // check if the path to the customdhcpcd file is correct - QFileInfo fInfo(pathToDhcpcdExe); - if (!fInfo.exists()) { - qDebug() - << "couldn't find customdhcpcd exe. Please check the path to this file."; - emit - abortBoot( - "couldn't find customdhcpcd exe. Please check the path to this file."); - return; - } - - if (args != NULL && !args->isEmpty()) { - qDebug() << "--- \t [LogReceiver::initAndRun] added additional args"; - dhcpcdArguments.append(*args); - } - - numberOfProcesses = 0; - - // start the main work: - - QList list = getListOfNetworkInterfaces(); - - if (list.size() > 0) { - - //list = checkCarrierState(list); - - - //dhcpcdArguments.append("-d"); - QString ifName("eth0"); - numberOfProcesses = list.size(); - runDHCPCD(list); - } else { - qDebug() << "list is empty. Haven't found usable interface."; - emit - abortBoot("Haven't found usable interface"); - return; - } - -} - -QList LogReceiver::getListOfNetworkInterfaces() { - QList nIList = QNetworkInterface::allInterfaces(); - QList result; - - if (nIList.size() > 0) { - foreach(QNetworkInterface nI, nIList) - { - if (((!(nI.flags() & QNetworkInterface::CanBroadcast) - || nI.flags() & QNetworkInterface::IsLoopBack) - || nI.flags() & QNetworkInterface::IsPointToPoint) - || checkBlackList(nI.humanReadableName())) { - continue; - } - if (!checkCarrierState(nI.humanReadableName())) { - continue; - } - result.append(nI.humanReadableName()); - emit addNewInterface(nI.humanReadableName()); - } - } else { - qDebug() << "no interfaces found!"; - } - return result; -} - -QList LogReceiver::checkCarrierState(QList &interfaces) { - QList result; - foreach(QString nI, interfaces) - { - if (checkCarrierState(nI)) { - // everything is fine, cable is plugged, - // go on with the next interface - //continue; - result.append(nI); - } - } - return result; -} - -bool LogReceiver::checkCarrierState(QString interface) { - - qDebug() << "check carrier state for interface " << interface; - QByteArray ba = interface.toAscii(); - const char * iface = ba.data(); - - struct sysfs_class_device *class_device = sysfs_open_class_device("net", - iface); - struct dlist *attrlist = sysfs_get_classdev_attributes(class_device); - if (attrlist != NULL) { - struct sysfs_attribute *attr = NULL; - dlist_for_each_data(attrlist, attr, struct sysfs_attribute) { - if (strcmp("carrier", attr->name) == 0) { - QString value(attr->value); - bool ok = false; - bool * pok = &ok; - int v = value.toInt(pok); - if (*pok) { - if (v == 1) { - qDebug() - << "carrier is 1. Cable is plugged. return true"; - return true; - } else { - qDebug() - << "carrier is 0. Cable is unplugged. return false"; - return false; - } - } else { - qDebug() << "conversion error"; - } - } - } - } else { - qDebug() << "attrlist is Null"; - } - sysfs_close_class_device(class_device); - - return true; -} - -void LogReceiver::runDHCPCD(QList &interfaces) { - foreach(QString nI, interfaces) - { - runDHCPCD(nI); - } -} - -void LogReceiver::runDHCPCD(QString interface) { - emit updateStatusLabel(interface, "start DHCP"); - dhcpcdArguments.append(interface); - QProcess * p = new QProcess(this); - - qDebug() << dhcpcdArguments; - - clientProcessToIfNameMap.insert(p, interface); - qDebug() << clientProcessToIfNameMap; - p->start(pathToDhcpcdExe, dhcpcdArguments); - connect(p, SIGNAL(started()), this, SLOT(handleProcessStarted())); - connect(p, SIGNAL(finished(int, QProcess::ExitStatus)), this, - SLOT(handleProcessFinished(int, QProcess::ExitStatus))); - dhcpcdArguments.removeLast(); -} - -void LogReceiver::checkInternetConnection(QString ifName) { - QString command("ping"); - QStringList argList; - QString timeout("1"); - QString total("2"); - int exitCode = -1; - QString destination("www.google.de"); - argList << "-I" << "ifName" << "-W" << timeout << "-c" << total - << destination; - argList.replace(1, ifName); - QProcess * p = new QProcess(this); - p->start(command, argList); - p->waitForFinished(); - exitCode = p->exitCode(); - if (exitCode > 0) { - qDebug() << "no internet connection with interface" << ifName; - //remove interface from list and inform user via debug console - emit updateStatusLabel(ifName, "connection not possible"); - } else if (exitCode == 0) { - qDebug() << "internet: check passed! for interface" << ifName; - emit - updateStatusLabel(ifName, "connection possible"); - emit connectionEstablished(ifName); - } -} - -void LogReceiver::checkInternetConnection(QList &interfaces) { - foreach(QString nI, interfaces) - { - checkInternetConnection(nI); - } -} -void LogReceiver::checkInternetConnectionViaTCP(QString ifName) { - - const bool canStartIAP = (configurationManager.capabilities() - & QNetworkConfigurationManager::CanStartAndStopInterfaces); - QList configs = - configurationManager.allConfigurations(); - QNetworkConfiguration cfg; - foreach(QNetworkConfiguration nC, configs) - { - if (nC.name() == ifName) { - qDebug() << "found config" << nC.name(); - cfg = nC; - break; - } - } - if (!cfg.isValid() || (!canStartIAP && cfg.state() - != QNetworkConfiguration::Active)) { - qDebug() << "config is not valid" << cfg.name(); - return; - } - QNetworkSession *session = new QNetworkSession(cfg, this); - session->open(); - if (session->waitForOpened(-1)) { - - qDebug () << "used interface for connectivity check:" <interface().humanReadableName(); - QTcpSocket *tcpSocket = new QTcpSocket(this); - tcpSocket->connectToHost(QString("209.85.148.105"), 80); - if (!tcpSocket->waitForConnected(2000)) { - qDebug() << tcpSocket->errorString(); - emit updateStatusLabel(ifName, "connection not possible"); - } else { - emit updateStatusLabel(ifName, "connection possible"); - emit connectionEstablished(ifName); - } - } else { - qDebug() << "couldn't open session"; - } - session->close(); -} - -void LogReceiver::handleNewConnection() { - qDebug() << "New Connection arrived"; - - /*QLocalSocket **/ - client = server ->nextPendingConnection(); - clients.insert(client, client); - connect(client, SIGNAL(disconnected()), this, - SLOT(handleClientDisconnect())); - connect(client, SIGNAL(readyRead()), this, SLOT(handleNewInput())); -} - -void LogReceiver::handleClientDisconnect() { - QLocalSocket* socket = qobject_cast (QObject::sender()); - - QLocalSocket * client = clients.value(socket); - - qDebug() << "disconnect client"; - handleNewInput(client); - client->deleteLater(); -} - -void LogReceiver::handleNewInput(QLocalSocket * client) { - qDebug() << "last read before exit"; - while (client->canReadLine()) { - QString data(client->readLine()); - - data = data.trimmed(); - qDebug() << data; - QStringList lines = data.split("\n"); - - for (int i = 0; i < lines.length(); i++) { - handleNewInputLine(client, lines.at(i)); - } - } -} - -void LogReceiver::handleNewInput() { - QLocalSocket* socket = qobject_cast (QObject::sender()); - - QLocalSocket * client = clients.value(socket); - - while(!client->atEnd()) { - QString data(client->readLine()); - - data = data.trimmed(); - //qDebug() << data; - QStringList lines = data.split("\n"); - - for (int i = 0; i < lines.length(); i++) { - handleNewInputLine(client, lines.at(i)); - } - } -/* - QString data(client->readAll()); - - data = data.trimmed(); - qDebug() << data; - QStringList lines = data.split("\n"); - - for (int i = 0; i < lines.length(); i++) { - handleNewInputLine(client, lines.at(i)); - } - */ -} - -void LogReceiver::handleNewInputLine(QLocalSocket * client, QString data) { - - QString logMsg(data); - QString interface = logMsg.section(";", 0, 0); - QString s_state = logMsg.section(";", 1, 1); - QString s_subState = logMsg.section(";", 2, 2); - QString msg = logMsg.section(";", 3, 3); - int st = s_state.trimmed().toInt(); - int sst = s_subState.trimmed().toInt(); - //qDebug() << logMsg; - - if (ifNameToClient.size() < numberOfProcesses && !ifNameToClient.contains( - interface)) { - ifNameToClient.insert(interface, client); - } - - switch (st) { - case LOG_INFO: - switch (sst) { - case DHCP_DISCOVER: - emit changeProgressBarValue(interface, 10); - break; - case DHCP_OFFER: - emit changeProgressBarValue(interface, 20); - break; - case DHCP_REQUEST: - emit changeProgressBarValue(interface, 30); - break; - case DHCP_ACK: - emit changeProgressBarValue(interface, 40); - break; - case DHCP_NAK: - emit changeProgressBarValue(interface, 40); - break; - case DHCPCD_ARP_TEST: - emit changeProgressBarValue(interface, 50); - break; - case DHCP_DECLINE: - emit changeProgressBarValue(interface, 60); - break; - case DHCP_RELEASE: - - break; - case DHCP_INFORM: - break; - case DHCPCD_CONFIGURE: - emit changeProgressBarValue(interface, 70); - break; - case DHCPCD_WRITE: - emit changeProgressBarValue(interface, 80); - break; - case DHCPCD_EXIT: - //emit changeProgressBarValue(interface, 100); - break; - case DHCPCD_LOG: - - default: - break; - } - break; - case LOG_ERR: - qDebug() << "received stat_error"; - break; - default: - //qDebug() << logMsg; - break; - } -} - -void LogReceiver::handleProcessFinished(int exitCode, - QProcess::ExitStatus exitStatus) { - QProcess* p = qobject_cast (QObject::sender()); - QString ifName = clientProcessToIfNameMap.value(p, "ifName"); - - if (ifName.compare("ifName") == 0) { - qDebug() - << "--- \t [LogReceiver::handleProcessFinished] haven't found process!"; - } else { - qDebug() << "process for interface" << ifName << "finished" << exitCode - << exitStatus; - if (exitCode > 0) { - qDebug() << "process exited unexpected"; - emit updateStatusLabel(ifName, "process exited unexpected"); - } else { - qDebug() << "process normal exit"; - qDebug() << "check internet connction"; - emit - changeProgressBarValue(ifName, 100); - emit - updateStatusLabel(ifName, "check connectivity"); - //checkInternetConnection(ifName); - checkInternetConnectionViaTCP(ifName); - } - } - QLocalSocket *client = ifNameToClient.value(ifName, 0); - if(client != 0) { - handleNewInput(client); - } - numberOfProcesses = numberOfProcesses - 1; - if (numberOfProcesses <= 0) { - emit allProcessesFinished(); - } -} - -void LogReceiver::handleProcessStarted() { - QProcess* p = qobject_cast (QObject::sender()); - QString ifName = clientProcessToIfNameMap.value(p, "ifName"); - qDebug() << "process started for interface:" << ifName; -} - -bool LogReceiver::checkBlackList(QString i) { - if (i.startsWith("v", Qt::CaseInsensitive)) { - return true; - } else if (i.startsWith("d", Qt::CaseInsensitive)) { - return true; - } else { - return false; - } -} diff --git a/workspace/LogReceiver/logreceiver.h b/workspace/LogReceiver/logreceiver.h deleted file mode 100644 index ce6d070..0000000 --- a/workspace/LogReceiver/logreceiver.h +++ /dev/null @@ -1,74 +0,0 @@ -#ifndef LOGRECEIVER_H -#define LOGRECEIVER_H - -#include -#include -#include -#include -#include -#include -#include - -class QLocalServer; -class QLocalSocket; - -class LogReceiver: public QObject { -Q_OBJECT - -#define DEFAULT_QTSOCKETADDRESS "/var/tmp/qt_c_socket_default" -#define DEFAULT_PATHTODHCPCDEXE "/home/niklas/fbgui/workspace/customdhcpcd/src/customdhcpcd" - -public: - LogReceiver(); - ~LogReceiver(); - - void initAndRun(QString serverPath = DEFAULT_QTSOCKETADDRESS, - QString pathToExe = DEFAULT_PATHTODHCPCDEXE, - QStringList* args = NULL); - -private slots: - void handleNewConnection(); - void handleNewInput(); - void handleNewInputLine(QLocalSocket * client, QString data); - void handleProcessFinished(int exitCode, QProcess::ExitStatus exitStatus); - void handleProcessStarted(); - void handleClientDisconnect(); - -signals: - void addNewInterface(QString ifName); - void changeProgressBarValue(QString ifName, int newValue); - void connectionEstablished(QString ifName); - void abortBoot(QString msg); - void updateStatusLabel(QString ifName, QString status); - void allProcessesFinished(); - -private: - QLocalServer *server; - quint16 blockSize; - //QMap interfacesMap; - //QMap indexToIfaceNameMap; - QMap clients; - QLocalSocket * client; - QMap ifNameToClient; - QMap clientProcessToIfNameMap; - QString pathToDhcpcdExe; - QStringList dhcpcdArguments; - QNetworkConfigurationManager configurationManager; - QNetworkAccessManager *accessManager; - int numberOfProcesses; - - void handleNewInput(QLocalSocket * client); - - void runDHCPCD(QList &interfaces); - void runDHCPCD(QString interface); - QListcheckCarrierState(QList &interfaces); - bool checkCarrierState(QString interface); - void checkInternetConnection(QString ifName); - void checkInternetConnection(QList &interfaces); - void checkInternetConnectionViaTCP(QString ifName); - QList getListOfNetworkInterfaces(); - bool checkBlackList(QString i); - -}; - -#endif // LOGRECEIVER_H diff --git a/workspace/LogReceiver/logreceiver.ui b/workspace/LogReceiver/logreceiver.ui deleted file mode 100644 index 0010c64..0000000 --- a/workspace/LogReceiver/logreceiver.ui +++ /dev/null @@ -1,25 +0,0 @@ - - - LogReceiverClass - - - - 0 - 0 - 572 - 385 - - - - LogReceiver - - - #LogReceiverClass{ -background:grey; -} - - - - - - diff --git a/workspace/LogReceiver/main.cpp b/workspace/LogReceiver/main.cpp deleted file mode 100644 index a8cd960..0000000 --- a/workspace/LogReceiver/main.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "ndgui.h" - -#include -#include - -int main(int argc, char *argv[]) -{ - QApplication a(argc, argv); - ndgui w; - w.show(); - return a.exec(); -} diff --git a/workspace/LogReceiver/ndgui.cpp b/workspace/LogReceiver/ndgui.cpp deleted file mode 100644 index a839aef..0000000 --- a/workspace/LogReceiver/ndgui.cpp +++ /dev/null @@ -1,184 +0,0 @@ -#include "ndgui.h" -#include "chooseinterfacedialog.h" -#include "abortbootdialog.h" - -ndgui::ndgui(QWidget *parent) - : QWidget(parent) -{ - ui.setupUi(this); - - connect(&logReceiver, SIGNAL(addNewInterface(QString)), this, SLOT(addNewInterface(QString))); - connect(&logReceiver, SIGNAL(changeProgressBarValue(QString , int )), this, SLOT(handleProgress(QString, int))); - connect(&logReceiver, SIGNAL(connectionEstablished(QString)), this, SLOT(handleConnectionEstablished(QString))); - connect(&logReceiver, SIGNAL(abortBoot(QString)), this, SLOT(handleAbortBoot(QString))); - connect(&logReceiver, SIGNAL(updateStatusLabel(QString,QString)), this, SLOT(handleUpdateStatusLabel(QString, QString))); - connect(&logReceiver, SIGNAL(allProcessesFinished()), this, SLOT(handleAllProcessesFinished())); - - buildGui(); - - logReceiver.initAndRun("/var/tmp/qt_c_socket_custom"); - numberOfInterfaces = 0; - - - setWindowTitle(tr("NetD")); -} - -ndgui::~ndgui() -{ - -} - -void ndgui::buildGui() { - - ndStatusLabel = new QLabel(tr("test")); - ndStatusLabel->setSizePolicy(QSizePolicy::Expanding, - QSizePolicy::Expanding); - ndStatusLabel->setAlignment(Qt::AlignCenter); - ndStatusLabel->setMinimumSize(100, 20); - - // create interface group box - createInterfaceGroupBox(); - - - - mainLayout = new QVBoxLayout; - mainLayout->addWidget(ndStatusLabel); - mainLayout->addWidget(interfaceGroupBox); - - setLayout(mainLayout); -} - -void ndgui::createInterfaceGroupBox(){ - interfaceGroupBox = new QGroupBox(tr("Interfaces")); - - interfaceGroupBoxLayout = new QVBoxLayout; - /* add interfaces via addInterfacesToGroupBox()*/ - - interfaceGroupBox->setLayout(interfaceGroupBoxLayout); -} - -void ndgui::addNewInterface(QString ifName) { - qDebug() << "receive interface to add:" << ifName; - QHBoxLayout *hBoxLayout = new QHBoxLayout; - QLabel *label = new QLabel(ifName); - QLabel *labelStatus = new QLabel("waiting"); - QProgressBar *pBar = new QProgressBar(this); - pBar->setRange(1, 100); - pBar->setMaximumSize(200, 20); - - statusLabels.insert(ifName, labelStatus); - progressBars.insert(ifName, pBar); - - hBoxLayout->addWidget(label, Qt::AlignLeft); - hBoxLayout->addWidget(labelStatus, Qt::AlignCenter); - hBoxLayout->addWidget(pBar, Qt::AlignRight); - - numberOfInterfaces++; - - interfaceGroupBoxLayout->addLayout(hBoxLayout, 2); -} - -void ndgui::handleProgress(QString ifName, int newValue) { - qDebug() << "<[---]> SLOT handleProgress activated with: " << ifName << newValue; - QProgressBar * pBar = progressBars.value(ifName); - if(newValue >= pBar->value()) { - pBar->setValue(newValue); - } - else { - qDebug() << "Error: new value is smaller than the old value!"; - } -} - -void ndgui::handleConnectionEstablished(QString ifName) { - finalUsableInterfaces.append(ifName); -} - -void ndgui::handleAbortBoot(QString msg) { - qDebug() << "abort boot. reason:" << msg; - showAbortBootDialog(); -} - -void ndgui::handleUpdateStatusLabel(QString ifName, QString status) { - QLabel* label = statusLabels.value(ifName); - label->setText(status); -} - -void ndgui::handleAllProcessesFinished() { - qDebug() << "all Processes finished"; - - if (finalUsableInterfaces.size() > 0) { - showChooseInterfaceDialog(); - } else { - showAbortBootDialog(); - } -} - -void ndgui::showAbortBootDialog() { - aBD = new AbortBootDialog(this); - connect(aBD, SIGNAL(showLogSignal()), this, SLOT(showLog())); - connect(aBD, SIGNAL(restartSignal()), this, SLOT(restartSystem())); - connect(aBD, SIGNAL(shutDownSignal()), this, SLOT(shutDownSystem())); - aBD->setModal(true); - aBD->show(); -} - -void ndgui::showChooseInterfaceDialog() { - cID = new ChooseInterfaceDialog(finalUsableInterfaces, this); - connect(cID, SIGNAL(continueSignal(QString)), this, - SLOT(continueBoot(QString))); - connect(cID, SIGNAL(restartSignal()), this, SLOT(restartSystem())); - connect(cID, SIGNAL(shutDownSignal()), this, SLOT(shutDownSystem())); - cID->setModal(true); - cID->show(); -} - -void ndgui::restartSystem() -{ - - if(qobject_cast(QObject::sender())>0) - { - qDebug() << "received Signal restart abd"; - aBD->closeDialog(); - } - else if(qobject_cast(QObject::sender())>0) - { - qDebug() << "received Signal restart cid"; - cID->close(); - } - else - { - qDebug() << "unknown sender" << QObject::sender(); - } - - -} - -void ndgui::shutDownSystem() -{ - if(qobject_cast(QObject::sender())>0) - { - - aBD->closeDialog(); - } - else if(qobject_cast(QObject::sender())>0) - { - - cID->close(); - } - else - { - qDebug() << "unknown sender" << QObject::sender(); - } - -} - -void ndgui::continueBoot(QString ifName) -{ - QString text = "continue with interface: " + ifName; - cID->close(); -} - -void ndgui::showLog() -{ - qDebug() << "show log"; -} diff --git a/workspace/LogReceiver/ndgui.h b/workspace/LogReceiver/ndgui.h deleted file mode 100644 index e8b0b85..0000000 --- a/workspace/LogReceiver/ndgui.h +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef NDGUI_H -#define NDGUI_H - -#include -#include "qprogressbar.h" -#include "qlabel.h" -#include "qgroupbox.h" -#include "qboxlayout.h" -#include "ui_ndgui.h" -#include "logreceiver.h" -#include "chooseinterfacedialog.h" -#include "abortbootdialog.h" - -class ndgui: public QWidget { -Q_OBJECT - -public: - ndgui(QWidget *parent = 0); - ~ndgui(); - -public slots: - void handleProgress(QString ifName, int newValue); - void addNewInterface(QString ifName); - void handleConnectionEstablished(QString ifName); - void handleAbortBoot(QString msg); - void handleUpdateStatusLabel(QString ifName, QString status); - void handleAllProcessesFinished(); - - void restartSystem(); - void shutDownSystem(); - void continueBoot(QString ifName); - void showLog(); - - void showAbortBootDialog(); - void showChooseInterfaceDialog(); - -private: - Ui::ndguiClass ui; - - LogReceiver logReceiver; - - QStringList finalUsableInterfaces; - - int numberOfInterfaces; - - ChooseInterfaceDialog *cID; - AbortBootDialog *aBD; - - /*gui elements*/ - QMap progressBars; - QMap statusLabels; - QLabel *ndStatusLabel; - QGroupBox *interfaceGroupBox; - QVBoxLayout *mainLayout; - QVBoxLayout *interfaceGroupBoxLayout; - /**/ - - - - - /*gui functions*/ - void buildGui(); - void createInterfaceGroupBox(); - -}; - -#endif // NDGUI_H diff --git a/workspace/LogReceiver/ndgui.ui b/workspace/LogReceiver/ndgui.ui deleted file mode 100644 index 4a4c466..0000000 --- a/workspace/LogReceiver/ndgui.ui +++ /dev/null @@ -1,19 +0,0 @@ - - ndguiClass - - - - 0 - 0 - 400 - 300 - - - - ndgui - - - - - - diff --git a/workspace/LogWriter/LogWriter b/workspace/LogWriter/LogWriter deleted file mode 100755 index 54d4ab8..0000000 Binary files a/workspace/LogWriter/LogWriter and /dev/null differ diff --git a/workspace/LogWriter/LogWriter.pro b/workspace/LogWriter/LogWriter.pro deleted file mode 100644 index ba5df47..0000000 --- a/workspace/LogWriter/LogWriter.pro +++ /dev/null @@ -1,11 +0,0 @@ -TEMPLATE = app -TARGET = LogWriter -QT += core \ - gui \ - network -HEADERS += status.h \ - logwriter.h -SOURCES += main.cpp \ - logwriter.cpp -FORMS += logwriter.ui -RESOURCES += diff --git a/workspace/LogWriter/logwriter.cpp b/workspace/LogWriter/logwriter.cpp deleted file mode 100644 index 94155ed..0000000 --- a/workspace/LogWriter/logwriter.cpp +++ /dev/null @@ -1,120 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "logwriter.h" -#include "status.h" - -LogWriter::LogWriter(QWidget *parent) : - QDialog(parent) { - hostLabel = new QLabel(tr("Message:")); - hostLineEdit = new QLineEdit("fortune"); - - serverName = "/var/tmp/qt_c_socket_test"; - - hostLabel->setBuddy(hostLineEdit); - - statusLabel = new QLabel(tr("This examples requires that you run the " - "LogReceiver example as well.")); - - connectToLogReceiver = new QPushButton(tr("Connect")); - connectToLogReceiver->setDefault(true); - - quitButton = new QPushButton(tr("Quit")); - - writeButton = new QPushButton(tr("Write")); - - buttonBox = new QDialogButtonBox; - buttonBox->addButton(connectToLogReceiver, QDialogButtonBox::ActionRole); - buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); - buttonBox->addButton(writeButton, QDialogButtonBox::ActionRole); - - connect(hostLineEdit, SIGNAL(textChanged(QString)), this, - SLOT(enableWriteButton())); - connect(connectToLogReceiver, SIGNAL(clicked()), this, - SLOT(requestNewFortune())); - connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); - connect(writeButton, SIGNAL(clicked()), this, SLOT(writeToLogReceiver())); - - QGridLayout *mainLayout = new QGridLayout; - mainLayout->addWidget(hostLabel, 0, 0); - mainLayout->addWidget(hostLineEdit, 0, 1); - mainLayout->addWidget(statusLabel, 2, 0, 1, 2); - mainLayout->addWidget(buttonBox, 3, 0, 1, 2); - setLayout(mainLayout); - - setWindowTitle(tr("Fortune Client")); - hostLineEdit->setFocus(); -} - -LogWriter::~LogWriter() { - -} - -void LogWriter::requestNewFortune() { - struct sockaddr_un serv_addr; - - connectToLogReceiver->setEnabled(false); - - - sockfd = socket(AF_UNIX, SOCK_STREAM, 0); - if (sockfd < 0) - qDebug() << "ERROR opening socket" << strerror(errno); - serv_addr.sun_family = AF_UNIX; - strcpy(serv_addr.sun_path, "/var/tmp/qt_c_socket_test"); - - - if (::connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) - qDebug() << "ERROR connecting" << strerror(errno); -} - -void LogWriter::readFortune() { - -} - -void LogWriter::displayError(QLocalSocket::LocalSocketError socketError) { - -} - -void LogWriter::enableWriteButton() { - writeButton->setEnabled(!hostLineEdit->text().isEmpty()); -} - -void LogWriter::writeToLogReceiver() { - writeToLogReceiver(STAT_OK,DISCOVER); -} - -void LogWriter::writeToLogReceiver(int stat, int sub_stat) { - char * st = (char *) malloc(sizeof(int)*4+1); - char * sst = (char *) malloc(sizeof(int)*4+1); - sprintf(st,"%d", stat); - sprintf(sst,"%d",sub_stat); - char * msg = "msg test"; - int msize = strlen(st) + strlen(sst) + strlen(msg) + 3; - char * m = (char *) malloc(msize); - strcpy(m, ""); - strcat(m, st); - strcat(m, ";"); - strcat(m, sst); - strcat(m, ";"); - strcat(m, msg); - - //char buffer[256]; - //char *b = "test"; - qDebug() << strlen(m); - qDebug() << m; - int n = ::write(sockfd, m, strlen(m)); - if (n < 0) - qDebug() << "ERROR writing to socket"; - - free(st); - free(sst); - free(m); - -} diff --git a/workspace/LogWriter/logwriter.h b/workspace/LogWriter/logwriter.h deleted file mode 100644 index a4ddea3..0000000 --- a/workspace/LogWriter/logwriter.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef LOGWRITER_H -#define LOGWRITER_H - -#include -#include -#include "ui_logwriter.h" -#include "status.h" - -class QDialogButtonBox; -class QLabel; -class QLineEdit; -class QPushButton; -class QLocalSocket; - -class LogWriter: public QDialog { -Q_OBJECT - -public: - LogWriter(QWidget *parent = 0); - ~LogWriter(); - -private slots: - void requestNewFortune(); - void readFortune(); - void displayError(QLocalSocket::LocalSocketError socketError); - void enableWriteButton(); - void writeToLogReceiver(); - -private: - // enum STATUS stat, enum SUBSTATUS sub_stat - void writeToLogReceiver(int stat, int sub_stat); - Ui::LogWriterClass ui; - QLabel *hostLabel; - QLineEdit *hostLineEdit; - QLabel *statusLabel; - QPushButton *connectToLogReceiver; - QPushButton *quitButton; - QPushButton *writeButton; - QDialogButtonBox *buttonBox; - - QString currentFortune; - quint16 blockSize; - QString serverName; - - int sockfd; - -}; - -#endif // LOGWRITER_H - diff --git a/workspace/LogWriter/logwriter.ui b/workspace/LogWriter/logwriter.ui deleted file mode 100644 index aebeb33..0000000 --- a/workspace/LogWriter/logwriter.ui +++ /dev/null @@ -1,19 +0,0 @@ - - LogWriterClass - - - - 0 - 0 - 400 - 300 - - - - LogWriter - - - - - - diff --git a/workspace/LogWriter/main.cpp b/workspace/LogWriter/main.cpp deleted file mode 100644 index 5695d76..0000000 --- a/workspace/LogWriter/main.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "logwriter.h" - -#include -#include - -int main(int argc, char *argv[]) -{ - QApplication a(argc, argv); - LogWriter w; - w.show(); - return a.exec(); -} diff --git a/workspace/customdhcpcd/Debug/makefile b/workspace/customdhcpcd/Debug/makefile deleted file mode 100644 index 0c6307d..0000000 --- a/workspace/customdhcpcd/Debug/makefile +++ /dev/null @@ -1,44 +0,0 @@ -################################################################################ -# Automatically-generated file. Do not edit! -################################################################################ - --include ../makefile.init - -RM := rm -rf - -# All of the sources participating in the build are defined here --include sources.mk --include subdir.mk --include src/subdir.mk --include objects.mk - -ifneq ($(MAKECMDGOALS),clean) -ifneq ($(strip $(C_DEPS)),) --include $(C_DEPS) -endif -endif - --include ../makefile.defs - -# Add inputs and outputs from these tool invocations to the build variables - -# All Target -all: customdhcpcd - -# Tool invocations -customdhcpcd: $(OBJS) $(USER_OBJS) - @echo 'Building target: $@' - @echo 'Invoking: GCC C Linker' - gcc -o"customdhcpcd" $(OBJS) $(USER_OBJS) $(LIBS) - @echo 'Finished building target: $@' - @echo ' ' - -# Other Targets -clean: - -$(RM) $(OBJS)$(C_DEPS)$(EXECUTABLES) customdhcpcd - -@echo ' ' - -.PHONY: all clean dependents -.SECONDARY: - --include ../makefile.targets diff --git a/workspace/customdhcpcd/Debug/objects.mk b/workspace/customdhcpcd/Debug/objects.mk deleted file mode 100644 index 224ef68..0000000 --- a/workspace/customdhcpcd/Debug/objects.mk +++ /dev/null @@ -1,7 +0,0 @@ -################################################################################ -# Automatically-generated file. Do not edit! -################################################################################ - -USER_OBJS := - -LIBS := diff --git a/workspace/customdhcpcd/Debug/sources.mk b/workspace/customdhcpcd/Debug/sources.mk deleted file mode 100644 index 57cf3c3..0000000 --- a/workspace/customdhcpcd/Debug/sources.mk +++ /dev/null @@ -1,17 +0,0 @@ -################################################################################ -# Automatically-generated file. Do not edit! -################################################################################ - -O_SRCS := -C_SRCS := -S_UPPER_SRCS := -OBJ_SRCS := -ASM_SRCS := -OBJS := -C_DEPS := -EXECUTABLES := - -# Every subdirectory with source files must be described here -SUBDIRS := \ -src \ - diff --git a/workspace/customdhcpcd/Debug/src/arp.d b/workspace/customdhcpcd/Debug/src/arp.d deleted file mode 100644 index e639f20..0000000 --- a/workspace/customdhcpcd/Debug/src/arp.d +++ /dev/null @@ -1,21 +0,0 @@ -src/arp.d src/arp.o: ../src/arp.c ../src/config.h ../src/common.h \ - ../src/arp.h ../src/interface.h ../src/logger.h ../src/signal.h \ - ../src/socket.h ../src/dhcp.h ../src/dhcpcd.h - -../src/config.h: - -../src/common.h: - -../src/arp.h: - -../src/interface.h: - -../src/logger.h: - -../src/signal.h: - -../src/socket.h: - -../src/dhcp.h: - -../src/dhcpcd.h: diff --git a/workspace/customdhcpcd/Debug/src/client.d b/workspace/customdhcpcd/Debug/src/client.d deleted file mode 100644 index 585c535..0000000 --- a/workspace/customdhcpcd/Debug/src/client.d +++ /dev/null @@ -1,37 +0,0 @@ -src/client.d src/client.o: ../src/client.c ../src/config.h \ - ../src/common.h ../src/arp.h ../src/interface.h ../src/client.h \ - ../src/dhcpcd.h ../src/configure.h ../src/dhcp.h ../src/info.h \ - ../src/ipv4ll.h ../src/logger.h ../src/signal.h ../src/socket.h \ - ../src/logwriter.h ../src/status.h ../src/duid.h - -../src/config.h: - -../src/common.h: - -../src/arp.h: - -../src/interface.h: - -../src/client.h: - -../src/dhcpcd.h: - -../src/configure.h: - -../src/dhcp.h: - -../src/info.h: - -../src/ipv4ll.h: - -../src/logger.h: - -../src/signal.h: - -../src/socket.h: - -../src/logwriter.h: - -../src/status.h: - -../src/duid.h: diff --git a/workspace/customdhcpcd/Debug/src/common.d b/workspace/customdhcpcd/Debug/src/common.d deleted file mode 100644 index 235fc5e..0000000 --- a/workspace/customdhcpcd/Debug/src/common.d +++ /dev/null @@ -1,6 +0,0 @@ -src/common.d src/common.o: ../src/common.c ../src/common.h \ - ../src/logger.h - -../src/common.h: - -../src/logger.h: diff --git a/workspace/customdhcpcd/Debug/src/configure.d b/workspace/customdhcpcd/Debug/src/configure.d deleted file mode 100644 index 16416f6..0000000 --- a/workspace/customdhcpcd/Debug/src/configure.d +++ /dev/null @@ -1,28 +0,0 @@ -src/configure.d src/configure.o: ../src/configure.c ../src/config.h \ - ../src/common.h ../src/configure.h ../src/dhcpcd.h ../src/interface.h \ - ../src/dhcp.h ../src/info.h ../src/logger.h ../src/signal.h \ - ../src/socket.h ../src/status.h ../src/logwriter.h - -../src/config.h: - -../src/common.h: - -../src/configure.h: - -../src/dhcpcd.h: - -../src/interface.h: - -../src/dhcp.h: - -../src/info.h: - -../src/logger.h: - -../src/signal.h: - -../src/socket.h: - -../src/status.h: - -../src/logwriter.h: diff --git a/workspace/customdhcpcd/Debug/src/customdhcpcd.d b/workspace/customdhcpcd/Debug/src/customdhcpcd.d deleted file mode 100644 index 1dd84f9..0000000 --- a/workspace/customdhcpcd/Debug/src/customdhcpcd.d +++ /dev/null @@ -1 +0,0 @@ -src/customdhcpcd.d src/customdhcpcd.o: ../src/customdhcpcd.c diff --git a/workspace/customdhcpcd/Debug/src/dhcp.d b/workspace/customdhcpcd/Debug/src/dhcp.d deleted file mode 100644 index 82e54dd..0000000 --- a/workspace/customdhcpcd/Debug/src/dhcp.d +++ /dev/null @@ -1,17 +0,0 @@ -src/dhcp.d src/dhcp.o: ../src/dhcp.c ../src/config.h ../src/common.h \ - ../src/dhcpcd.h ../src/dhcp.h ../src/interface.h ../src/logger.h \ - ../src/socket.h - -../src/config.h: - -../src/common.h: - -../src/dhcpcd.h: - -../src/dhcp.h: - -../src/interface.h: - -../src/logger.h: - -../src/socket.h: diff --git a/workspace/customdhcpcd/Debug/src/dhcpcd.d b/workspace/customdhcpcd/Debug/src/dhcpcd.d deleted file mode 100644 index 05c8d7e..0000000 --- a/workspace/customdhcpcd/Debug/src/dhcpcd.d +++ /dev/null @@ -1,26 +0,0 @@ -src/dhcpcd.d src/dhcpcd.o: ../src/dhcpcd.c ../src/config.h \ - ../src/client.h ../src/dhcpcd.h ../src/common.h ../src/dhcp.h \ - ../src/interface.h ../src/logger.h ../src/socket.h ../src/version.h \ - ../src/logwriter.h ../src/status.h - -../src/config.h: - -../src/client.h: - -../src/dhcpcd.h: - -../src/common.h: - -../src/dhcp.h: - -../src/interface.h: - -../src/logger.h: - -../src/socket.h: - -../src/version.h: - -../src/logwriter.h: - -../src/status.h: diff --git a/workspace/customdhcpcd/Debug/src/discover.d b/workspace/customdhcpcd/Debug/src/discover.d deleted file mode 100644 index 9188993..0000000 --- a/workspace/customdhcpcd/Debug/src/discover.d +++ /dev/null @@ -1 +0,0 @@ -src/discover.d src/discover.o: ../src/discover.c diff --git a/workspace/customdhcpcd/Debug/src/duid.d b/workspace/customdhcpcd/Debug/src/duid.d deleted file mode 100644 index 1434db7..0000000 --- a/workspace/customdhcpcd/Debug/src/duid.d +++ /dev/null @@ -1,12 +0,0 @@ -src/duid.d src/duid.o: ../src/duid.c ../src/config.h ../src/common.h \ - ../src/duid.h ../src/interface.h ../src/logger.h - -../src/config.h: - -../src/common.h: - -../src/duid.h: - -../src/interface.h: - -../src/logger.h: diff --git a/workspace/customdhcpcd/Debug/src/info.d b/workspace/customdhcpcd/Debug/src/info.d deleted file mode 100644 index 0a3490d..0000000 --- a/workspace/customdhcpcd/Debug/src/info.d +++ /dev/null @@ -1,17 +0,0 @@ -src/info.d src/info.o: ../src/info.c ../src/config.h ../src/common.h \ - ../src/dhcp.h ../src/dhcpcd.h ../src/interface.h ../src/logger.h \ - ../src/info.h - -../src/config.h: - -../src/common.h: - -../src/dhcp.h: - -../src/dhcpcd.h: - -../src/interface.h: - -../src/logger.h: - -../src/info.h: diff --git a/workspace/customdhcpcd/Debug/src/interface.d b/workspace/customdhcpcd/Debug/src/interface.d deleted file mode 100644 index 581c831..0000000 --- a/workspace/customdhcpcd/Debug/src/interface.d +++ /dev/null @@ -1,15 +0,0 @@ -src/interface.d src/interface.o: ../src/interface.c ../src/config.h \ - ../src/common.h ../src/dhcp.h ../src/dhcpcd.h ../src/interface.h \ - ../src/logger.h - -../src/config.h: - -../src/common.h: - -../src/dhcp.h: - -../src/dhcpcd.h: - -../src/interface.h: - -../src/logger.h: diff --git a/workspace/customdhcpcd/Debug/src/ipv4ll.d b/workspace/customdhcpcd/Debug/src/ipv4ll.d deleted file mode 100644 index 7d02ead..0000000 --- a/workspace/customdhcpcd/Debug/src/ipv4ll.d +++ /dev/null @@ -1,17 +0,0 @@ -src/ipv4ll.d src/ipv4ll.o: ../src/ipv4ll.c ../src/config.h ../src/arp.h \ - ../src/interface.h ../src/ipv4ll.h ../src/dhcp.h ../src/dhcpcd.h \ - ../src/common.h - -../src/config.h: - -../src/arp.h: - -../src/interface.h: - -../src/ipv4ll.h: - -../src/dhcp.h: - -../src/dhcpcd.h: - -../src/common.h: diff --git a/workspace/customdhcpcd/Debug/src/logger.d b/workspace/customdhcpcd/Debug/src/logger.d deleted file mode 100644 index bbf1cf8..0000000 --- a/workspace/customdhcpcd/Debug/src/logger.d +++ /dev/null @@ -1,10 +0,0 @@ -src/logger.d src/logger.o: ../src/logger.c ../src/common.h \ - ../src/logger.h ../src/logwriter.h ../src/dhcpcd.h - -../src/common.h: - -../src/logger.h: - -../src/logwriter.h: - -../src/dhcpcd.h: diff --git a/workspace/customdhcpcd/Debug/src/logwriter.d b/workspace/customdhcpcd/Debug/src/logwriter.d deleted file mode 100644 index 83dc4b5..0000000 --- a/workspace/customdhcpcd/Debug/src/logwriter.d +++ /dev/null @@ -1,19 +0,0 @@ -src/logwriter.d src/logwriter.o: ../src/logwriter.c ../src/common.h \ - ../src/dhcp.h ../src/dhcpcd.h ../src/interface.h ../src/config.h \ - ../src/logger.h ../src/logwriter.h ../src/status.h - -../src/common.h: - -../src/dhcp.h: - -../src/dhcpcd.h: - -../src/interface.h: - -../src/config.h: - -../src/logger.h: - -../src/logwriter.h: - -../src/status.h: diff --git a/workspace/customdhcpcd/Debug/src/signal.d b/workspace/customdhcpcd/Debug/src/signal.d deleted file mode 100644 index 0185fc3..0000000 --- a/workspace/customdhcpcd/Debug/src/signal.d +++ /dev/null @@ -1,8 +0,0 @@ -src/signal.d src/signal.o: ../src/signal.c ../src/common.h \ - ../src/logger.h ../src/signal.h - -../src/common.h: - -../src/logger.h: - -../src/signal.h: diff --git a/workspace/customdhcpcd/Debug/src/socket.d b/workspace/customdhcpcd/Debug/src/socket.d deleted file mode 100644 index 785c170..0000000 --- a/workspace/customdhcpcd/Debug/src/socket.d +++ /dev/null @@ -1,17 +0,0 @@ -src/socket.d src/socket.o: ../src/socket.c ../src/config.h ../src/dhcp.h \ - ../src/dhcpcd.h ../src/common.h ../src/interface.h ../src/logger.h \ - ../src/socket.h - -../src/config.h: - -../src/dhcp.h: - -../src/dhcpcd.h: - -../src/common.h: - -../src/interface.h: - -../src/logger.h: - -../src/socket.h: diff --git a/workspace/customdhcpcd/Debug/src/subdir.mk b/workspace/customdhcpcd/Debug/src/subdir.mk deleted file mode 100644 index 38890b3..0000000 --- a/workspace/customdhcpcd/Debug/src/subdir.mk +++ /dev/null @@ -1,85 +0,0 @@ -################################################################################ -# Automatically-generated file. Do not edit! -################################################################################ - -# Add inputs and outputs from these tool invocations to the build variables -O_SRCS += \ -../src/arp.o \ -../src/client.o \ -../src/common.o \ -../src/configure.o \ -../src/dhcp.o \ -../src/dhcpcd.o \ -../src/duid.o \ -../src/info.o \ -../src/interface.o \ -../src/ipv4ll.o \ -../src/logger.o \ -../src/logwriter.o \ -../src/signal.o \ -../src/socket.o - -C_SRCS += \ -../src/arp.c \ -../src/client.c \ -../src/common.c \ -../src/configure.c \ -../src/customdhcpcd.c \ -../src/dhcp.c \ -../src/dhcpcd.c \ -../src/discover.c \ -../src/duid.c \ -../src/info.c \ -../src/interface.c \ -../src/ipv4ll.c \ -../src/logger.c \ -../src/logwriter.c \ -../src/signal.c \ -../src/socket.c - -OBJS += \ -./src/arp.o \ -./src/client.o \ -./src/common.o \ -./src/configure.o \ -./src/customdhcpcd.o \ -./src/dhcp.o \ -./src/dhcpcd.o \ -./src/discover.o \ -./src/duid.o \ -./src/info.o \ -./src/interface.o \ -./src/ipv4ll.o \ -./src/logger.o \ -./src/logwriter.o \ -./src/signal.o \ -./src/socket.o - -C_DEPS += \ -./src/arp.d \ -./src/client.d \ -./src/common.d \ -./src/configure.d \ -./src/customdhcpcd.d \ -./src/dhcp.d \ -./src/dhcpcd.d \ -./src/discover.d \ -./src/duid.d \ -./src/info.d \ -./src/interface.d \ -./src/ipv4ll.d \ -./src/logger.d \ -./src/logwriter.d \ -./src/signal.d \ -./src/socket.d - - -# Each subdirectory must supply rules for building sources it contributes -src/%.o: ../src/%.c - @echo 'Building file: $<' - @echo 'Invoking: GCC C Compiler' - gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<" - @echo 'Finished building: $<' - @echo ' ' - - diff --git a/workspace/customdhcpcd/src/.gitignore b/workspace/customdhcpcd/src/.gitignore deleted file mode 100644 index 6eb1591..0000000 --- a/workspace/customdhcpcd/src/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -*.o -version.h -dhcpcd -dhcpcd.8 -dhcpcd-*.bz2 diff --git a/workspace/customdhcpcd/src/Makefile b/workspace/customdhcpcd/src/Makefile deleted file mode 100644 index 7f90db4..0000000 --- a/workspace/customdhcpcd/src/Makefile +++ /dev/null @@ -1,34 +0,0 @@ -# Makefile based on BSD's pmake. -# Our mk stubs also work with GNU make. -# Copyright 2008 Roy Marples - -PROG= customdhcpcd -SRCS= arp.c client.c common.c configure.c dhcp.c dhcpcd.c duid.c \ - info.c interface.c ipv4ll.c logger.c logwriter.c signal.c socket.c -MAN= dhcpcd.8 - -VERSION= 3.2.3 -CLEANFILES= version.h dhcpcd.8 - -BINDIR= ${PREFIX}/sbin - -.SUFFIXES: .in - -MK= mk -include ${MK}/os.mk -include ${MK}/cc.mk -include ${MK}/prog.mk - -# os.mk should define this, but heh -INFOD?= /var/db - -LDADD+= ${LIBRESOLV} ${LIBRT} -CFLAGS+= -g -Wall -DINFODIR=\"${INFOD}\" ${FORK} ${RC} - -# As version.h is generated by us, hardcode the depend correctly. -${SRCS}: version.h -version.h: - echo "#define VERSION \"${VERSION}\""> version.h - -.in: - sed 's:@PREFIX@:${PREFIX}:g; s:@INFODIR@:${INFOD}:g' $< > $@ diff --git a/workspace/customdhcpcd/src/README b/workspace/customdhcpcd/src/README deleted file mode 100644 index 9089ec6..0000000 --- a/workspace/customdhcpcd/src/README +++ /dev/null @@ -1,45 +0,0 @@ -dhcpcd-3 - DHCP client daemon -Copyright 2006-2008 Roy Marples - - -Installation ------------- -Edit config.h to match your building requirements. - -Take special note of ENABLE_DUID and unset it if the target media is -volatile, like say a LiveCD. - -Then just make; make install - -man dhcpcd for command line options - - -Notes ------ -If you're cross compiling you may need to send HAVE_FORK=yes or HAVE_FORK=no -to the make command to avoid to automatic test. - -We try and detect how to restart ntp and ypbind, you can override this with -HAVE_INIT=no or force one of these values -OPENRC (OpenRC as used by Gentoo (forked from baselayout)) -BSDRC (BSD RC system - /etc/rc.d/ntpd restart ) -SERVICE (RedHat service command - service ntpd restart) -SLACKRC (Slackware RC system - /etc/rc.d/rc.ntpd restart) -SYSV (SYSV style - /etc/init.d/ntpd restart) - -You can change the default dir where dhcpcd stores it's .info files with -INFODIR=/var/db - -We now default to using -std=c99. For 64-bit linux, this always works, but -for 32-bit linux it requires either gnu99 or a patch to asm/types.h. -Most distros patch linux headers so this should work fine. -linux-2.6.24 finally ships with a working 32-bit header. -If your linux headers are older, or your distro hasn't patched them you can -set CSTD=gnu99 to work around this. - - -ChangeLog ---------- -We no longer supply a ChangeLog. -However, you're more than welcome to read the git commit comments at -http://git.marples.name/?p=dhcpcd/.git;a=summary diff --git a/workspace/customdhcpcd/src/arp.c b/workspace/customdhcpcd/src/arp.c deleted file mode 100644 index 794850c..0000000 --- a/workspace/customdhcpcd/src/arp.c +++ /dev/null @@ -1,284 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#ifdef __linux__ -#include -#include -#endif -#include -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "common.h" -#include "arp.h" -#include "interface.h" -#include "logger.h" -#include "signal.h" -#include "socket.h" - -/* These are really for IPV4LL */ -#define NPROBES 3 -#define PROBE_INTERVAL 200 -#define NCLAIMS 2 -#define CLAIM_INTERVAL 200 - -/* Linux does not seem to define these handy macros */ -#ifndef ar_sha -#define ar_sha(ap) (((caddr_t) ((ap) + 1)) + 0) -#define ar_spa(ap) (((caddr_t) ((ap) + 1)) + (ap)->ar_hln) -#define ar_tha(ap) (((caddr_t) ((ap) + 1)) + (ap)->ar_hln + (ap)->ar_pln) -#define ar_tpa(ap) (((caddr_t) ((ap) + 1)) + 2 * (ap)->ar_hln + (ap)->ar_pln) -#endif - -#ifndef arphdr_len -#define arphdr_len2(ar_hln, ar_pln) (sizeof (struct arphdr) + \ - 2 * (ar_hln) + 2 * (ar_pln)) -#define arphdr_len(ap) (arphdr_len2 ((ap)->ar_hln, (ap)->ar_pln)) -#endif - -#ifdef ENABLE_ARP - -static int send_arp (const interface_t *iface, int op, struct in_addr sip, - const unsigned char *taddr, struct in_addr tip) -{ - struct arphdr *arp; - size_t arpsize = arphdr_len2 (iface->hwlen, sizeof (sip)); - caddr_t tha; - int retval; - - arp = xzalloc (arpsize); - arp->ar_hrd = htons (iface->family); - arp->ar_pro = htons (ETHERTYPE_IP); - arp->ar_hln = iface->hwlen; - arp->ar_pln = sizeof (sip); - arp->ar_op = htons (op); - memcpy (ar_sha (arp), iface->hwaddr, (size_t) arp->ar_hln); - memcpy (ar_spa (arp), &sip, (size_t) arp->ar_pln); - if (taddr) { - /* NetBSD can return NULL from ar_tha, which is probably wrong - * but we still need to deal with it */ - if (! (tha = ar_tha (arp))) { - free (arp); - errno = EINVAL; - return (-1); - } - memcpy (tha, taddr, (size_t) arp->ar_hln); - } - memcpy (ar_tpa (arp), &tip, (size_t) arp->ar_pln); - - retval = send_packet (iface, ETHERTYPE_ARP, - (unsigned char *) arp, arphdr_len (arp)); - free (arp); - return (retval); -} - -int arp_claim (interface_t *iface, struct in_addr address) -{ - struct arphdr *reply = NULL; - long timeout = 0; - unsigned char *buffer; - int retval = -1; - int nprobes = 0; - int nclaims = 0; - struct in_addr null_address; - struct pollfd fds[] = { - { -1, POLLIN, 0 }, - { -1, POLLIN, 0 } - }; - - if (! iface) - return (-1); - - if (! iface->arpable) { - logger (LOG_DEBUG, "interface `%s' is not ARPable", iface->name); - return (0); - } - - if (! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr)) && - ! IN_LINKLOCAL (ntohl (address.s_addr))) - logger (LOG_INFO, - "checking %s is available on attached networks", - inet_ntoa (address)); - - if (! open_socket (iface, ETHERTYPE_ARP)) - return (-1); - - fds[0].fd = signal_fd (); - fds[1].fd = iface->fd; - - memset (&null_address, 0, sizeof (null_address)); - - buffer = xmalloc (iface->buffer_length); - reply = xmalloc (iface->buffer_length); - - for (;;) { - size_t bufpos = 0; - size_t buflen = iface->buffer_length; - int bytes; - int s = 0; - struct timeval stopat; - struct timeval now; - - /* Only poll if we have a timeout */ - if (timeout > 0) { - s = poll (fds, 2, timeout); - if (s == -1) { - if (errno == EINTR) { - if (signal_exists (NULL) == -1) { - errno = 0; - continue; - } else - break; - } - - logger (LOG_ERR, "poll: `%s'", - strerror (errno)); - break; - } - } - - /* Timed out */ - if (s == 0) { - if (nprobes < NPROBES) { - nprobes ++; - timeout = PROBE_INTERVAL; - logger (LOG_DEBUG, "sending ARP probe #%d", - nprobes); - if (send_arp (iface, ARPOP_REQUEST, - null_address, NULL, - address) == -1) - break; - - /* IEEE1394 cannot set ARP target address - * according to RFC2734 */ - if (nprobes >= NPROBES && - iface->family == ARPHRD_IEEE1394) - nclaims = NCLAIMS; - } else if (nclaims < NCLAIMS) { - nclaims ++; - timeout = CLAIM_INTERVAL; - logger (LOG_DEBUG, "sending ARP claim #%d", - nclaims); - if (send_arp (iface, ARPOP_REQUEST, - address, iface->hwaddr, - address) == -1) - break; - } else { - /* No replies, so done */ - retval = 0; - break; - } - - /* Setup our stop time */ - if (get_time (&stopat) != 0) - break; - stopat.tv_usec += timeout; - - continue; - } - - /* We maybe ARP flooded, so check our time */ - if (get_time (&now) != 0) - break; - if (timercmp (&now, &stopat, >)) { - timeout = 0; - continue; - } - - if (! fds[1].revents & POLLIN) - continue; - - memset (buffer, 0, buflen); - do { - union { - unsigned char *c; - struct in_addr *a; - } rp; - union { - unsigned char *c; - struct ether_addr *a; - } rh; - - memset (reply, 0, iface->buffer_length); - if ((bytes = get_packet (iface, (unsigned char *) reply, - buffer, - &buflen, &bufpos)) == -1) - break; - - /* Only these types are recognised */ - if (reply->ar_op != htons (ARPOP_REPLY)) - continue; - - /* Protocol must be IP. */ - if (reply->ar_pro != htons (ETHERTYPE_IP)) - continue; - if (reply->ar_pln != sizeof (address)) - continue; - if ((unsigned) bytes < sizeof (reply) + - 2 * (4 + reply->ar_hln)) - continue; - - rp.c = (unsigned char *) ar_spa (reply); - rh.c = (unsigned char *) ar_sha (reply); - - /* Ensure the ARP reply is for the our address */ - if (rp.a->s_addr != address.s_addr) - continue; - - /* Some systems send a reply back from our hwaddress, - * which is wierd */ - if (reply->ar_hln == iface->hwlen && - memcmp (rh.c, iface->hwaddr, iface->hwlen) == 0) - continue; - - logger (LOG_ERR, "ARPOP_REPLY received from %s (%s)", - inet_ntoa (*rp.a), - hwaddr_ntoa (rh.c, (size_t) reply->ar_hln)); - retval = -1; - goto eexit; - } while (bufpos != 0); - } - -eexit: - close (iface->fd); - iface->fd = -1; - free (buffer); - free (reply); - return (retval); -} -#endif diff --git a/workspace/customdhcpcd/src/arp.h b/workspace/customdhcpcd/src/arp.h deleted file mode 100644 index 3b7e8ef..0000000 --- a/workspace/customdhcpcd/src/arp.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef ARP_H -#define ARP_H - -#ifdef ENABLE_ARP -#include - -#include "interface.h" - -int arp_claim (interface_t *iface, struct in_addr address); -#endif - -#endif diff --git a/workspace/customdhcpcd/src/client.c b/workspace/customdhcpcd/src/client.c deleted file mode 100644 index ba71ba4..0000000 --- a/workspace/customdhcpcd/src/client.c +++ /dev/null @@ -1,1150 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#ifdef __linux__ -# include -#endif -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "common.h" -#ifdef ENABLE_ARP -# include "arp.h" -#endif -#include "client.h" -#include "configure.h" -#include "dhcp.h" -#include "dhcpcd.h" -#include "info.h" -#include "interface.h" -#ifdef ENABLE_IPV4LL -# include "ipv4ll.h" -#endif -#include "logger.h" -#include "signal.h" -#include "socket.h" - -#include "logwriter.h" -#include "status.h" - -#ifdef ENABLE_DUID -# include "duid.h" -#endif - -#ifdef ENABLE_INFO -# include "info.h" -#endif - -#ifdef THERE_IS_NO_FORK -# ifndef ENABLE_INFO - # error "Non MMU requires ENABLE_INFO to work" -# endif -#endif - -/* Some platforms don't define INFTIM */ -#ifndef INFTIM -# define INFTIM -1 -#endif - -/* This is out mini timeout. - * Basically we resend the last request every TIMEOUT_MINI seconds. */ -#define TIMEOUT_MINI 3 -/* Except for an infinite timeout. We keep adding TIMEOUT_MINI to - * ourself until TIMEOUT_MINI_INF is reached. */ -#define TIMEOUT_MINI_INF 60 - -#define STATE_INIT 0 -#define STATE_REQUESTING 1 -#define STATE_BOUND 2 -#define STATE_RENEWING 3 -#define STATE_REBINDING 4 -#define STATE_REBOOT 5 -#define STATE_RENEW_REQUESTED 6 -#define STATE_RELEASED 7 - -/* We should define a maximum for the NAK exponential backoff */ -#define NAKOFF_MAX 60 - -#define SOCKET_CLOSED 0 -#define SOCKET_OPEN 1 - -/* Indexes for pollfds */ -#define POLLFD_SIGNAL 0 -#define POLLFD_IFACE 1 - -typedef struct _state { - int *pidfd; - bool forked; - int state; - uint32_t xid; - dhcp_t *dhcp; - int socket; - interface_t *interface; - time_t start; - time_t last_sent; - time_t last_type; - long timeout; - time_t nakoff; - bool daemonised; - bool persistent; - unsigned char *buffer; - size_t buffer_len; - size_t buffer_pos; -} state_t; - -static pid_t daemonise (int *pidfd) -{ - pid_t pid; - sigset_t full; - sigset_t old; -#ifdef THERE_IS_NO_FORK - char **argv; - int i; -#endif - - sigfillset (&full); - sigprocmask (SIG_SETMASK, &full, &old); - -#ifndef THERE_IS_NO_FORK - logger (LOG_DEBUG, "forking to background"); - switch (pid = fork()) { - case -1: - logger (LOG_ERR, "fork: %s", strerror (errno)); - exit (EXIT_FAILURE); - /* NOT REACHED */ - case 0: - setsid (); - close_fds (); - break; - default: - /* Reset our signals as we're the parent about to exit. */ - signal_reset (); - break; - } -#else - logger (LOG_INFO, "forking to background"); - - /* We need to add --daemonise to our options */ - argv = xmalloc (sizeof (char *) * (dhcpcd_argc + 4)); - argv[0] = dhcpcd; - for (i = 1; i < dhcpcd_argc; i++) - argv[i] = dhcpcd_argv[i]; - argv[i] = (char *) "--daemonised"; - if (dhcpcd_skiproutes) { - argv[++i] = (char *) "--skiproutes"; - argv[++i] = dhcpcd_skiproutes; - } - argv[i + 1] = NULL; - - switch (pid = vfork ()) { - case -1: - logger (LOG_ERR, "vfork: %s", strerror (errno)); - _exit (EXIT_FAILURE); - case 0: - signal_reset (); - sigprocmask (SIG_SETMASK, &old, NULL); - execvp (dhcpcd, argv); - logger (LOG_ERR, "execl `%s': %s", dhcpcd, - strerror (errno)); - _exit (EXIT_FAILURE); - } - - free (argv); -#endif - - /* Done with the fd now */ - if (pid != 0) { - writepid (*pidfd, pid); - close (*pidfd); - *pidfd = -1; - - } - - sigprocmask (SIG_SETMASK, &old, NULL); - return (pid); -} - -#ifdef ENABLE_INFO -static bool get_old_lease (state_t *state, const options_t *options) -{ - interface_t *iface = state->interface; - dhcp_t *dhcp = state->dhcp; - struct timeval tv; - unsigned int offset = 0; - - if (! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr))) - logger (LOG_INFO, "trying to use old lease in `%s'", - iface->infofile); - if (! read_info (iface, dhcp)) - return (false); - - /* Vitaly important we remove the server information here */ - memset (&dhcp->serveraddress, 0, sizeof (dhcp->serveraddress)); - memset (dhcp->servername, 0, sizeof (dhcp->servername)); - -#ifdef ENABLE_ARP - /* Check that no-one is using the address */ - if ((options->dolastlease || - (IN_LINKLOCAL (ntohl (dhcp->address.s_addr)) && - (! options->doipv4ll || - arp_claim (iface, dhcp->address))))) - { - memset (&dhcp->address, 0, sizeof (dhcp->address)); - memset (&dhcp->netmask, 0, sizeof (dhcp->netmask)); - memset (&dhcp->broadcast, 0, sizeof (dhcp->broadcast)); - return (false); - } - - /* Ok, lets use this */ - if (IN_LINKLOCAL (dhcp->address.s_addr)) - return (true); -#endif - - /* Ensure that we can still use the lease */ - if (gettimeofday (&tv, NULL) == -1) { - logger (LOG_ERR, "gettimeofday: %s", strerror (errno)); - return (false); - } - - offset = tv.tv_sec - dhcp->leasedfrom; - if (dhcp->leasedfrom && - tv.tv_sec - dhcp->leasedfrom > dhcp->leasetime) - { - logger (LOG_ERR, "lease expired %u seconds ago", - offset + dhcp->leasetime); - return (false); - } - - if (dhcp->leasedfrom == 0) - offset = 0; - state->timeout = dhcp->renewaltime - offset; - iface->start_uptime = uptime (); - return (true); -} -#endif - -#ifdef THERE_IS_NO_FORK -static void remove_skiproutes (dhcp_t *dhcp, interface_t *iface) -{ - int i = -1; - route_t *route; - route_t *newroute; - - free_route (iface->previous_routes); - iface->previous_routes = NULL; - - NSTAILQ_FOREACH (route, dhcp->routes, entries) { - i++; - - /* Check that we did add this route or not */ - if (dhcpcd_skiproutes) { - char *sk = xstrdup (dhcpcd_skiproutes); - char *skp = sk; - char *token; - bool found = false; - - while ((token = strsep (&skp, ","))) { - if (isdigit (*token) && atoi (token) == i) { - found = true; - break; - } - } - free (sk); - if (found) - continue; - } - - if (! iface->previous_routes) { - iface->previous_routes = xmalloc (sizeof (*iface->previous_routes)); - STAILQ_INIT (iface->previous_routes); - } - - newroute = xmalloc (sizeof (*newroute)); - memcpy (newroute, route, sizeof (*newroute)); - STAILQ_INSERT_TAIL (iface->previous_routes, newroute, entries); - } - - /* We no longer need this argument */ - free (dhcpcd_skiproutes); - dhcpcd_skiproutes = NULL; -} -#endif - -static bool client_setup (state_t *state, const options_t *options) -{ - dhcp_t *dhcp = state->dhcp; - interface_t *iface = state->interface; - - state->state = STATE_INIT; - state->last_type = DHCP_DISCOVER; - state->nakoff = 1; - state->daemonised = options->daemonised; - state->persistent = options->persistent; - - if (options->request_address.s_addr == 0 && - (options->doinform || options->dorequest || options->daemonised)) - { -#ifdef ENABLE_INFO - if (! get_old_lease (state, options)) -#endif - { - free (dhcp); - return (false); - } - state->timeout = 0; - - if (! options->daemonised && - IN_LINKLOCAL (ntohl (dhcp->address.s_addr))) - { - logger (LOG_ERR, "cannot request a link local address"); - return (false); - } -#ifdef THERE_IS_NO_FORK - if (options->daemonised) { - state->state = STATE_BOUND; - state->timeout = dhcp->renewaltime; - iface->previous_address = dhcp->address; - iface->previous_netmask = dhcp->netmask; - remove_skiproutes (dhcp, iface); - } -#endif - - } else { - dhcp->address = options->request_address; - dhcp->netmask = options->request_netmask; - if (dhcp->netmask.s_addr == 0) - dhcp->netmask.s_addr = get_netmask (dhcp->address.s_addr); - dhcp->broadcast.s_addr = dhcp->address.s_addr | - ~dhcp->netmask.s_addr; - } - - /* Remove all existing addresses. - * After all, we ARE a DHCP client whose job it is to configure the - * interface. We only do this on start, so persistent addresses - * can be added afterwards by the user if needed. */ - if (! options->test && ! options->daemonised) { - if (! options->doinform) { - flush_addresses (iface->name); - } else { - /* The inform address HAS to be configured for it to - * work with most DHCP servers */ - if (options->doinform && - has_address (iface->name, dhcp->address) < 1) - { - add_address (iface->name, dhcp->address, - dhcp->netmask, dhcp->broadcast); - iface->previous_address = dhcp->address; - iface->previous_netmask = dhcp->netmask; - } - } - } - - if (*options->clientid) { - /* Attempt to see if the ClientID is a hardware address */ - iface->clientid_len = hwaddr_aton (NULL, options->clientid); - if (iface->clientid_len) { - iface->clientid = xmalloc (iface->clientid_len); - hwaddr_aton (iface->clientid, options->clientid); - } else { - /* Nope, so mark it as-is */ - iface->clientid_len = strlen (options->clientid) + 1; - iface->clientid = xmalloc (iface->clientid_len); - *iface->clientid = '\0'; - memcpy (iface->clientid + 1, - options->clientid, iface->clientid_len - 1); - } - } else { -#ifdef ENABLE_DUID - unsigned char *duid = NULL; - size_t duid_len = 0; - - if (options->doduid) { - duid = xmalloc (DUID_LEN); - duid_len = get_duid (duid, iface); - } - - if (duid_len > 0) { - logger (LOG_INFO, "DUID = %s", hwaddr_ntoa (duid, duid_len)); - - iface->clientid_len = duid_len + 5; - iface->clientid = xmalloc (iface->clientid_len); - *iface->clientid = 255; /* RFC 4361 */ - - /* IAID is 4 bytes, so if the iface name is 4 bytes use it */ - if (strlen (iface->name) == 4) { - memcpy (iface->clientid + 1, iface->name, 4); - } else { - /* Name isn't 4 bytes, so use the index */ - uint32_t ul = htonl (if_nametoindex (iface->name)); - memcpy (iface->clientid + 1, &ul, 4); - } - - memcpy (iface->clientid + 5, duid, duid_len); - free (duid); - } else { -#else - { -#endif - iface->clientid_len = iface->hwlen + 1; - iface->clientid = xmalloc (iface->clientid_len); - *iface->clientid = iface->family; - memcpy (iface->clientid + 1, iface->hwaddr, iface->hwlen); - } - } - - return (true); -} - -static bool do_socket (state_t *state, int mode) -{ - if (state->interface->fd >= 0) - close (state->interface->fd); -#ifdef __linux - if (mode == SOCKET_CLOSED && state->interface->listen_fd >= 0) { - close (state->interface->listen_fd); - state->interface->listen_fd = -1; - } -#endif - - state->interface->fd = -1; - if (mode == SOCKET_OPEN) - if (open_socket (state->interface, ETHERTYPE_IP) == -1) - return (false); - state->socket = mode; - return (true); -} - -static bool _send_message (state_t *state, int type, const options_t *options) -{ - ssize_t retval; - - state->last_type = type; - state->last_sent = uptime (); - logSendToQt(type); - retval = send_message (state->interface, state->dhcp, state->xid, - type, options); - return (retval == -1 ? false : true); -} - -static void drop_config (state_t *state, const options_t *options) -{ - if (! state->persistent) - configure (options, state->interface, state->dhcp, false); - - free_dhcp (state->dhcp); - memset (state->dhcp, 0, sizeof (*state->dhcp)); -} - -static int wait_for_packet (struct pollfd *fds, state_t *state, - const options_t *options) -{ - dhcp_t *dhcp = state->dhcp; - interface_t *iface = state->interface; - int timeout = 0; - int retval = 0; - - if (! (state->timeout > 0 || - (options->timeout == 0 && - (state->state != STATE_INIT || state->xid)))) { - /* We need to zero our signal fd, otherwise we will block - * trying to read a signal. */ - fds[POLLFD_SIGNAL].revents = 0; - return (0); - } - - fds[POLLFD_IFACE].fd = iface->fd; - - if ((options->timeout == 0 && state->xid) || - (dhcp->leasetime == (unsigned) -1 && - state->state == STATE_BOUND)) - { - logger (LOG_DEBUG, "waiting for infinity"); - while (retval == 0) { - if (iface->fd == -1) - retval = poll (fds, 1, INFTIM); - else { - /* Slow down our requests */ - if (timeout < TIMEOUT_MINI_INF) - timeout += TIMEOUT_MINI; - else if (timeout > TIMEOUT_MINI_INF) - timeout = TIMEOUT_MINI_INF; - - retval = poll (fds, 2, timeout * 1000); - if (retval == -1 && errno == EINTR) { - /* If interupted, continue as normal as - * the signal will be delivered down - * the pipe */ - retval = 0; - continue; - } - if (retval == 0) - _send_message (state, state->last_type, - options); - } - } - - return (retval); - } - - /* Resend our message if we're getting loads of packets. - * As we use BPF or LPF, we shouldn't hit this as much, but it's - * still nice to have. */ - if (iface->fd > -1 && uptime () - state->last_sent >= TIMEOUT_MINI) - _send_message (state, state->last_type, options); - - logger (LOG_DEBUG, "waiting for %ld seconds", - (unsigned long) state->timeout); - /* If we're waiting for a reply, then we re-send the last - * DHCP request periodically in-case of a bad line */ - retval = 0; - while (state->timeout > 0 && retval == 0) { - if (iface->fd == -1) - timeout = (int) state->timeout; - else { - timeout = TIMEOUT_MINI; - if (state->timeout < timeout) - timeout = (int) state->timeout; - } - timeout *= 1000; - state->start = uptime (); - retval = poll (fds, iface->fd == -1 ? 1 : 2, timeout); - state->timeout -= uptime () - state->start; - if (retval == -1 && errno == EINTR) { - /* If interupted, continue as normal as the signal - * will be delivered down the pipe */ - retval = 0; - continue; - } - if (retval == 0 && iface->fd != -1 && state->timeout > 0) - _send_message (state, state->last_type, options); - } - - return (retval); -} - -static bool handle_signal (int sig, state_t *state, const options_t *options) -{ - switch (sig) { - case SIGINT: - logger (LOG_INFO, "received SIGINT, stopping"); - return (false); - case SIGTERM: - logger (LOG_INFO, "received SIGTERM, stopping"); - return (false); - - case SIGALRM: - logger (LOG_INFO, "received SIGALRM, renewing lease"); - switch (state->state) { - case STATE_BOUND: - case STATE_RENEWING: - case STATE_REBINDING: - state->state = STATE_RENEW_REQUESTED; - break; - case STATE_RENEW_REQUESTED: - case STATE_REQUESTING: - case STATE_RELEASED: - state->state = STATE_INIT; - break; - } - state->timeout = 0; - state->xid = 0; - return (true); - - case SIGHUP: - if (state->state != STATE_BOUND && - state->state != STATE_RENEWING && - state->state != STATE_REBINDING) - { - logger (LOG_ERR, - "received SIGHUP, but we no have lease to release"); - return (false); - } - - logger (LOG_INFO, "received SIGHUP, releasing lease"); - if (! IN_LINKLOCAL (ntohl (state->dhcp->address.s_addr))) { - do_socket (state, SOCKET_OPEN); - state->xid = (uint32_t) random (); - if ((open_socket (state->interface, false)) >= 0) { - _send_message (state, DHCP_RELEASE, options); - } - do_socket (state, SOCKET_CLOSED); - } - unlink (state->interface->infofile); - return (false); - - default: - logger (LOG_ERR, - "received signal %d, but don't know what to do with it", - sig); - } - - return (false); -} - -static int handle_timeout (state_t *state, const options_t *options) -{ - dhcp_t *dhcp = state->dhcp; - interface_t *iface = state->interface; - - /* No NAK, so reset the backoff */ - state->nakoff = 1; - - if (state->state == STATE_INIT && state->xid != 0) { - if (iface->previous_address.s_addr != 0 && - ! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr)) && - ! options->doinform) - { - logger (LOG_ERR, "lost lease"); - if (! options->persistent) - drop_config (state, options); - } else if (! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr))) - logger (LOG_ERR, "timed out"); - - do_socket (state, SOCKET_CLOSED); - free_dhcp (dhcp); - memset (dhcp, 0, sizeof (*dhcp)); - -#ifdef ENABLE_INFO - if (! options->test && - (options->doipv4ll || options->dolastlease)) - { - errno = 0; - if (! get_old_lease (state, options)) - { - if (errno == EINTR) - return (0); - if (options->dolastlease) - return (-1); - free_dhcp (dhcp); - memset (dhcp, 0, sizeof (*dhcp)); - } else if (errno == EINTR) - return (0); - } -#endif - -#ifdef ENABLE_IPV4LL - if (! options->test && options->doipv4ll && - (! dhcp->address.s_addr || - (! IN_LINKLOCAL (ntohl (dhcp->address.s_addr)) && - ! options->dolastlease))) - { - logger (LOG_INFO, "probing for an IPV4LL address"); - free_dhcp (dhcp); - memset (dhcp, 0, sizeof (*dhcp)); - if (ipv4ll_get_address (iface, dhcp) == -1) { - if (! state->daemonised) - return (-1); - - /* start over */ - state->xid = 0; - return (0); - } - state->timeout = dhcp->renewaltime; - } -#endif - -#if defined (ENABLE_INFO) || defined (ENABLE_IPV4LL) - if (dhcp->address.s_addr) { - if (! state->daemonised && - IN_LINKLOCAL (ntohl (dhcp->address.s_addr))) - logger (LOG_WARNING, "using IPV4LL address %s", - inet_ntoa (dhcp->address)); - if (configure (options, iface, dhcp, true) == -1 && - ! state->daemonised) - return (-1); - - state->state = STATE_BOUND; - if (! state->daemonised && options->daemonise) { - switch (daemonise (state->pidfd)) { - case -1: - return (-1); - case 0: - state->daemonised = true; - return (0); - default: - state->persistent = true; - state->forked = true; - return (-1); - } - } - - state->timeout = dhcp->renewaltime; - state->xid = 0; - return (0); - } -#endif - - if (! state->daemonised) - return (-1); - } - - switch (state->state) { - case STATE_INIT: - state->xid = (uint32_t) random (); - do_socket (state, SOCKET_OPEN); - state->timeout = options->timeout; - iface->start_uptime = uptime (); - if (dhcp->address.s_addr == 0) { - if (! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr))) - logger (LOG_INFO, "broadcasting for a lease"); - _send_message (state, DHCP_DISCOVER, options); - } else if (options->doinform) { - logger (LOG_INFO, "broadcasting inform for %s", - inet_ntoa (dhcp->address)); - _send_message (state, DHCP_INFORM, options); - state->state = STATE_REQUESTING; - } else { - logger (LOG_INFO, "broadcasting for a lease of %s", - inet_ntoa (dhcp->address)); - _send_message (state, DHCP_REQUEST, options); - state->state = STATE_REQUESTING; - } - - break; - case STATE_BOUND: - case STATE_RENEW_REQUESTED: - if (IN_LINKLOCAL (ntohl (dhcp->address.s_addr))) { - memset (&dhcp->address, 0, sizeof (dhcp->address)); - state->state = STATE_INIT; - state->xid = 0; - break; - } - state->state = STATE_RENEWING; - state->xid = (uint32_t) random (); - /* FALLTHROUGH */ - case STATE_RENEWING: - iface->start_uptime = uptime (); - logger (LOG_INFO, "renewing lease of %s", inet_ntoa - (dhcp->address)); - do_socket (state, SOCKET_OPEN); - _send_message (state, DHCP_REQUEST, options); - state->timeout = dhcp->rebindtime - dhcp->renewaltime; - state->state = STATE_REBINDING; - break; - case STATE_REBINDING: - logger (LOG_ERR, "lost lease, attemping to rebind"); - memset (&dhcp->address, 0, sizeof (dhcp->address)); - do_socket (state, SOCKET_OPEN); - if (state->xid == 0) - state->xid = (uint32_t) random (); - dhcp->serveraddress.s_addr = 0; - _send_message (state, DHCP_REQUEST, options); - state->timeout = dhcp->leasetime - dhcp->rebindtime; - state->state = STATE_REQUESTING; - break; - case STATE_REQUESTING: - state->state = STATE_INIT; - do_socket (state, SOCKET_CLOSED); - state->timeout = 0; - break; - - case STATE_RELEASED: - dhcp->leasetime = 0; - break; - } - - return (0); -} - - -static int handle_dhcp (state_t *state, int type, const options_t *options) -{ - struct timespec ts; - interface_t *iface = state->interface; - dhcp_t *dhcp = state->dhcp; - - /* We should restart on a NAK */ - if (type == DHCP_NAK) { - logger (LOG_INFO, "received NAK: %s", dhcp->message); - logToQt(LOG_INFO, DHCP_NAK, ""); - state->state = STATE_INIT; - state->timeout = 0; - state->xid = 0; - free_dhcp (dhcp); - memset (dhcp, 0, sizeof (*dhcp)); - - /* If we constantly get NAKS then we should slowly back off */ - if (state->nakoff > 0) { - logger (LOG_DEBUG, "sleeping for %ld seconds", - (long) state->nakoff); - ts.tv_sec = state->nakoff; - ts.tv_nsec = 0; - state->nakoff *= 2; - if (state->nakoff > NAKOFF_MAX) - state->nakoff = NAKOFF_MAX; - nanosleep (&ts, NULL); - } - - return (0); - } - - /* No NAK, so reset the backoff */ - state->nakoff = 1; - - if (type == DHCP_OFFER && state->state == STATE_INIT) { - char *addr = strdup (inet_ntoa (dhcp->address)); - if (dhcp->servername[0]) - logger (LOG_INFO, "offered %s from %s `%s'", - addr, inet_ntoa (dhcp->serveraddress), - dhcp->servername); - else - logger (LOG_INFO, "offered %s from %s", - addr, inet_ntoa (dhcp->serveraddress)); - free (addr); - - logToQt(LOG_INFO, DHCP_OFFER, ""); - -#ifdef ENABLE_INFO - if (options->test) { - write_info (iface, dhcp, options, false); - errno = 0; - return (-1); - } -#endif - - _send_message (state, DHCP_REQUEST, options); - state->state = STATE_REQUESTING; - - return (0); - } - - if (type == DHCP_OFFER) { - logger (LOG_INFO, "got subsequent offer of %s, ignoring ", - inet_ntoa (dhcp->address)); - return (0); - } - - /* We should only be dealing with acks */ - if (type != DHCP_ACK) { - logger (LOG_ERR, "%d not an ACK or OFFER", type); - return (0); - } - - /* if we are here, than we received an ACK and can go on with configuration */ - logToQt(LOG_INFO, DHCP_ACK, ""); - - switch (state->state) { - case STATE_RENEW_REQUESTED: - case STATE_REQUESTING: - case STATE_RENEWING: - case STATE_REBINDING: - break; - default: - logger (LOG_ERR, "wrong state %d", state->state); - } - - do_socket (state, SOCKET_CLOSED); - -#ifdef ENABLE_ARP - if (options->doarp && iface->previous_address.s_addr != - dhcp->address.s_addr) - { - errno = 0; - logToQt(LOG_INFO, DHCPCD_ARP_TEST, ""); - if (arp_claim (iface, dhcp->address)) { - do_socket (state, SOCKET_OPEN); - _send_message (state, DHCP_DECLINE, options); - do_socket (state, SOCKET_CLOSED); - - free_dhcp (dhcp); - memset (dhcp, 0, sizeof (*dhcp)); - state->xid = 0; - state->timeout = 0; - state->state = STATE_INIT; - - /* RFC 2131 says that we should wait for 10 seconds - * before doing anything else */ - logger (LOG_INFO, "sleeping for 10 seconds"); - ts.tv_sec = 10; - ts.tv_nsec = 0; - nanosleep (&ts, NULL); - return (0); - } else if (errno == EINTR) - return (0); - } -#endif - - if (options->doinform) { - if (options->request_address.s_addr != 0) - dhcp->address = options->request_address; - else - dhcp->address = iface->previous_address; - - logger (LOG_INFO, "received approval for %s", - inet_ntoa (dhcp->address)); - if (iface->previous_netmask.s_addr != dhcp->netmask.s_addr) { - add_address (iface->name, dhcp->address, - dhcp->netmask, dhcp->broadcast); - iface->previous_netmask.s_addr = dhcp->netmask.s_addr; - } - state->timeout = options->leasetime; - if (state->timeout == 0) - state->timeout = DEFAULT_LEASETIME; - state->state = STATE_INIT; - } else if (dhcp->leasetime == (unsigned) -1) { - dhcp->renewaltime = dhcp->rebindtime = dhcp->leasetime; - state->timeout = 1; /* So we wait for infinity */ - logger (LOG_INFO, "leased %s for infinity", - inet_ntoa (dhcp->address)); - state->state = STATE_BOUND; - } else { - if (! dhcp->leasetime) { - dhcp->leasetime = DEFAULT_LEASETIME; - logger(LOG_INFO, - "no lease time supplied, assuming %d seconds", - dhcp->leasetime); - } - logger (LOG_INFO, "leased %s for %u seconds", - inet_ntoa (dhcp->address), dhcp->leasetime); - - if (dhcp->rebindtime >= dhcp->leasetime) { - dhcp->rebindtime = (dhcp->leasetime * 0.875); - logger (LOG_ERR, - "rebind time greater than lease " - "time, forcing to %u seconds", - dhcp->rebindtime); - } - - if (dhcp->renewaltime > dhcp->rebindtime) { - dhcp->renewaltime = (dhcp->leasetime * 0.5); - logger (LOG_ERR, - "renewal time greater than rebind time, " - "forcing to %u seconds", - dhcp->renewaltime); - } - - if (! dhcp->renewaltime) { - dhcp->renewaltime = (dhcp->leasetime * 0.5); - logger (LOG_INFO, - "no renewal time supplied, assuming %d seconds", - dhcp->renewaltime); - } else - logger (LOG_DEBUG, "renew in %u seconds", - dhcp->renewaltime); - - if (! dhcp->rebindtime) { - dhcp->rebindtime = (dhcp->leasetime * 0.875); - logger (LOG_INFO, - "no rebind time supplied, assuming %d seconds", - dhcp->rebindtime); - } else - logger (LOG_DEBUG, "rebind in %u seconds", - dhcp->rebindtime); - - state->timeout = dhcp->renewaltime; - state->state = STATE_BOUND; - } - - state->xid = 0; - - logToQt(LOG_INFO, DHCPCD_CONFIGURE, ""); - if (configure (options, iface, dhcp, true) == -1 && - ! state->daemonised) - return (-1); - - if (! state->daemonised && options->daemonise) { - switch (daemonise (state->pidfd)) { - case 0: - state->daemonised = true; - return (0); - case -1: - return (-1); - default: - state->persistent = true; - state->forked = true; - return (-1); - } - } - - return (0); -} - -static int handle_packet (state_t *state, const options_t *options) -{ - interface_t *iface = state->interface; - bool valid = false; - int type; - struct dhcp_t *new_dhcp; - dhcpmessage_t message; - - /* Allocate our buffer space for BPF. - * We cannot do this until we have opened our socket as we don't - * know how much of a buffer we need until then. */ - if (! state->buffer) - state->buffer = xmalloc (iface->buffer_length); - state->buffer_len = iface->buffer_length; - state->buffer_pos = 0; - - /* We loop through until our buffer is empty. - * The benefit is that if we get >1 DHCP packet in our buffer and - * the first one fails for any reason, we can use the next. */ - - memset (&message, 0, sizeof (message)); - new_dhcp = xmalloc (sizeof (*new_dhcp)); - - do { - if (get_packet (iface, (unsigned char *) &message, - state->buffer, - &state->buffer_len, &state->buffer_pos) == -1) - break; - - if (state->xid != message.xid) { - logger (LOG_DEBUG, - "ignoring packet with xid 0x%x as it's not ours (0x%x)", - message.xid, state->xid); - continue; - } - - logger (LOG_DEBUG, "got a packet with xid 0x%x", message.xid); - memset (new_dhcp, 0, sizeof (*new_dhcp)); - type = parse_dhcpmessage (new_dhcp, &message); - if (type == -1) { - logger (LOG_ERR, "failed to parse packet"); - free_dhcp (new_dhcp); - /* We don't abort on this, so return zero */ - return (0); - } - - /* If we got here then the DHCP packet is valid and appears to - * be for us, so let's clear the buffer as we don't care about - * any more DHCP packets at this point. */ - valid = true; - break; - } while (state->buffer_pos != 0); - - /* No packets for us, so wait until we get one */ - if (! valid) { - free (new_dhcp); - return (0); - } - - /* new_dhcp is now our master DHCP message */ - free_dhcp (state->dhcp); - free (state->dhcp); - state->dhcp = new_dhcp; - new_dhcp = NULL; - - return (handle_dhcp (state, type, options)); -} - -int dhcp_run (const options_t *options, int *pidfd) -{ - interface_t *iface; - state_t *state = NULL; - struct pollfd fds[] = { - { -1, POLLIN, 0 }, - { -1, POLLIN, 0 } - }; - int retval = -1; - int sig; - - if (! options) - return (-1); - - /*read_interface : defined in interface.c*/ - iface = read_interface (options->interface, options->metric); - if (! iface) - goto eexit; - - state = xzalloc (sizeof (*state)); - state->dhcp = xzalloc (sizeof (*state->dhcp)); - state->pidfd = pidfd; - state->interface = iface; - - if (! client_setup (state, options)) - goto eexit; - - if (signal_init () == -1) - goto eexit; - if (signal_setup () == -1) - goto eexit; - - fds[POLLFD_SIGNAL].fd = signal_fd (); - - for (;;) { - retval = wait_for_packet (fds, state, options); - - /* We should always handle our signals first */ - if ((sig = (signal_read (&fds[POLLFD_SIGNAL]))) != -1) { - if (handle_signal (sig, state, options)) - retval = 0; - else - retval = -1; - } else if (retval == 0) - retval = handle_timeout (state, options); - else if (retval > 0 && - state->socket != SOCKET_CLOSED && - fds[POLLFD_IFACE].revents & POLLIN) - retval = handle_packet (state, options); - else if (retval == -1 && errno == EINTR) { - /* The interupt will be handled above */ - retval = 0; - } else { - logger (LOG_ERR, "poll: %s", strerror (errno)); - retval = -1; - } - - if (retval != 0) - break; - } - -eexit: - if (iface) { - do_socket (state, SOCKET_CLOSED); - drop_config (state, options); - free_route (iface->previous_routes); - free (iface->clientid); - free (iface); - } - - if (state) { - if (state->forked) - retval = 0; - - if (state->daemonised) - unlink (options->pidfile); - - free_dhcp (state->dhcp); - free (state->dhcp); - free (state->buffer); - free (state); - } - - return (retval); -} diff --git a/workspace/customdhcpcd/src/client.h b/workspace/customdhcpcd/src/client.h deleted file mode 100644 index fa6ea9b..0000000 --- a/workspace/customdhcpcd/src/client.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef CLIENT_H -#define CLIENT_H - -#include "dhcpcd.h" - -int dhcp_run (const options_t *options, int *pidfd); - -#endif diff --git a/workspace/customdhcpcd/src/common.c b/workspace/customdhcpcd/src/common.c deleted file mode 100644 index 99471bc..0000000 --- a/workspace/customdhcpcd/src/common.c +++ /dev/null @@ -1,249 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "common.h" -#include "logger.h" - -/* Handy routine to read very long lines in text files. - * This means we read the whole line and avoid any nasty buffer overflows. */ -char *get_line (FILE *fp) -{ - char *line = NULL; - char *p; - size_t len = 0; - size_t last = 0; - - if (feof (fp)) - return (NULL); - - do { - len += BUFSIZ; - line = xrealloc (line, sizeof (char) * len); - p = line + last; - memset (p, 0, BUFSIZ); - fgets (p, BUFSIZ, fp); - last += strlen (p); - } while (! feof (fp) && line[last - 1] != '\n'); - - /* Trim the trailing newline */ - if (*line && line[--last] == '\n') - line[last] = '\0'; - - return (line); -} - -/* OK, this should be in dhcpcd.c - * It's here to make dhcpcd more readable */ -#ifndef HAVE_SRANDOMDEV -void srandomdev (void) -{ - int fd; - unsigned long seed; - - fd = open ("/dev/urandom", 0); - if (fd == -1 || read (fd, &seed, sizeof (seed)) == -1) { - logger (LOG_WARNING, "Could not read from /dev/urandom: %s", - strerror (errno)); - seed = time (0); - } - if (fd >= 0) - close(fd); - - srandom (seed); -} -#endif - -/* strlcpy is nice, shame glibc does not define it */ -#ifndef HAVE_STRLCPY -size_t strlcpy (char *dst, const char *src, size_t size) -{ - const char *s = src; - size_t n = size; - - if (n && --n) - do { - if (! (*dst++ = *src++)) - break; - } while (--n); - - if (! n) { - if (size) - *dst = '\0'; - while (*src++); - } - - return (src - s - 1); -} -#endif - -/* Close our fd's */ -int close_fds (void) -{ - int fd; - - if ((fd = open ("/dev/null", O_RDWR)) == -1) { - logger (LOG_ERR, "open `/dev/null': %s", strerror (errno)); - return (-1); - } - - dup2 (fd, fileno (stdin)); - dup2 (fd, fileno (stdout)); - dup2 (fd, fileno (stderr)); - if (fd > 2) - close (fd); - return (0); -} - -int close_on_exec (int fd) -{ - int flags; - - if ((flags = fcntl (fd, F_GETFD, 0)) == -1 - || fcntl (fd, F_SETFD, flags | FD_CLOEXEC) == -1) - { - logger (LOG_ERR, "fcntl: %s", strerror (errno)); - return (-1); - } - return (0); -} - -/* Handy function to get the time. - * We only care about time advancements, not the actual time itself - * Which is why we use CLOCK_MONOTONIC, but it is not available on all - * platforms. - */ -int get_time (struct timeval *tp) -{ -#if defined(_POSIX_MONOTONIC_CLOCK) && defined(CLOCK_MONOTONIC) - struct timespec ts; - static clockid_t posix_clock; - static int posix_clock_set = 0; - - if (! posix_clock_set) { - if (sysconf (_SC_MONOTONIC_CLOCK) >= 0) - posix_clock = CLOCK_MONOTONIC; - else - posix_clock = CLOCK_REALTIME; - posix_clock_set = 1; - } - - if (clock_gettime (posix_clock, &ts) == -1) { - logger (LOG_ERR, "clock_gettime: %s", strerror (errno)); - return (-1); - } - - tp->tv_sec = ts.tv_sec; - tp->tv_usec = ts.tv_nsec / 1000; - return (0); -#else - if (gettimeofday (tp, NULL) == -1) { - logger (LOG_ERR, "gettimeofday: %s", strerror (errno)); - return (-1); - } - return (0); -#endif -} - -time_t uptime (void) -{ - struct timeval tp; - - if (get_time (&tp) == -1) - return (-1); - - return (tp.tv_sec); -} - -void writepid (int fd, pid_t pid) -{ - char spid[16]; - if (ftruncate (fd, (off_t) 0) == -1) { - logger (LOG_ERR, "ftruncate: %s", strerror (errno)); - } else { - ssize_t len; - snprintf (spid, sizeof (spid), "%u", pid); - len = pwrite (fd, spid, strlen (spid), (off_t) 0); - if (len != (ssize_t) strlen (spid)) - logger (LOG_ERR, "pwrite: %s", strerror (errno)); - } -} - -void *xmalloc (size_t s) -{ - void *value = malloc (s); - - if (value) - return (value); - - logger (LOG_ERR, "memory exhausted"); - - exit (EXIT_FAILURE); - /* NOTREACHED */ -} - -void *xzalloc (size_t s) -{ - void *value = xmalloc (s); - memset (value, 0, s); - return (value); -} - -void *xrealloc (void *ptr, size_t s) -{ - void *value = realloc (ptr, s); - - if (value) - return (value); - - logger (LOG_ERR, "memory exhausted"); - exit (EXIT_FAILURE); - /* NOTREACHED */ -} - -char *xstrdup (const char *str) -{ - char *value; - - if (! str) - return (NULL); - - if ((value = strdup (str))) - return (value); - - logger (LOG_ERR, "memory exhausted"); - exit (EXIT_FAILURE); - /* NOTREACHED */ -} diff --git a/workspace/customdhcpcd/src/common.h b/workspace/customdhcpcd/src/common.h deleted file mode 100644 index 46f1886..0000000 --- a/workspace/customdhcpcd/src/common.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef COMMON_H -#define COMMON_H - -/* string.h pulls in features.h so the below define checks work */ -#include -#include -#include - -#if __GNUC__ > 2 || defined(__INTEL_COMPILER) -# define _unused __attribute__((__unused__)) -#else -# define _unused -#endif - -#define HAVE_STRLCPY -/* Only GLIBC doesn't support strlcpy */ -#ifdef __GLIBC__ -# if ! defined(__UCLIBC__) && ! defined (__dietlibc__) -# undef HAVE_STRLCPY -size_t strlcpy (char *dst, const char *src, size_t size); -# endif -#endif - -#define HAVE_SRANDOMDEV -#if defined(__GLIBC__) || defined(__NetBSD__) -# undef HAVE_SRANDOMDEV -void srandomdev (void); -#endif - -int close_fds (void); -int close_on_exec (int fd); -char *get_line (FILE *fp); -int get_time (struct timeval *tp); -time_t uptime (void); -void writepid (int fd, pid_t pid); -void *xrealloc (void *ptr, size_t size); -void *xmalloc (size_t size); -void *xzalloc (size_t size); -char *xstrdup (const char *str); - -#endif diff --git a/workspace/customdhcpcd/src/config.h b/workspace/customdhcpcd/src/config.h deleted file mode 100644 index 2c0991b..0000000 --- a/workspace/customdhcpcd/src/config.h +++ /dev/null @@ -1,133 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef CONFIG_H -#define CONFIG_H - -/* You can enable/disable various chunks of optional code here. - * You would only do this to try and shrink the end binary if dhcpcd - * was running on a low memory device */ - -#define ENABLE_ARP -#define ENABLE_NTP -#define ENABLE_NIS -#define ENABLE_INFO -/* Define this to enable some compatability with 1.x and 2.x info files */ -/* #define ENABLE_INFO_COMPAT */ - -/* IPV4LL, aka ZeroConf, aka APIPA, aka RFC 3927. - * Needs ARP. */ -#define ENABLE_IPV4LL - -/* We will auto create a DUID_LLT file if it doesn't exist. - * You can always create your own DUID file that just contains the - * hex string that represents the DUID. - * See RFC 3315 for details on this. */ -#define ENABLE_DUID - -/* resolvconf is framework for multiple interfaces to manage resolv.conf */ -#define ENABLE_RESOLVCONF - -/* Some systems do not have a working fork. - * The Makefile will attempt to work it out, but if it fails to feel free to - * define it here. */ -/* #define THERE_IS_NO_FORK */ - -/* Packname name and pathname definitions. */ - -#define PACKAGE "dhcpcd" - -#define ETCDIR "/etc" -#define RESOLVFILE ETCDIR "/resolv.conf" - -#define NISFILE ETCDIR "/yp.conf" - -#define NTPFILE ETCDIR "/ntp.conf" -#define NTPDRIFTFILE ETCDIR "/ntp.drift" -#define NTPLOGFILE "/var/log/ntp.log" - -#define OPENNTPFILE ETCDIR "/ntpd.conf" - -#define DEFAULT_SCRIPT ETCDIR "/" PACKAGE ".sh" - -#define STATEDIR "/var" -#define PIDFILE STATEDIR "/run/" PACKAGE "-%s.pid" - -#ifndef INFODIR -# define INFODIR "/var/lib/dhcpcd" -#endif -#define INFOFILE INFODIR "/" PACKAGE "-%s.info" -#define DUIDFILE INFODIR "/" PACKAGE ".duid" - -/* OPENRC is Open Run Control, forked from Gentoo's baselayout package - * BSDRC is BSD style Run Control - * SLACKRC is Slackware Run Control - * SERVICE is RedHat service command - * SYSV should cover everthing else */ -#ifdef ENABLE_OPENRC -# define SERVICE "OPENRC" -# define NISSERVICE ETCDIR "/init.d/ypbind" -# define NISRESTARTARGS "--nodeps", "--quiet", "conditionalrestart" -# define NTPSERVICE ETCDIR "/init.d/ntpd" -# define NTPRESTARTARGS "--nodeps", "--quiet", "conditionalrestart" -#endif -#if ENABLE_BSDRC -# define SERVICE "BSDRC" -# define NISSERVICE ETCDIR "/rc.d/ypbind" -# define NISRESTARTARGS "restart" -# define NTPSERVICE ETCDIR "/rc.d/ntpd" -# define NTPRESTARTARGS "restart" -#endif -#if ENABLE_SLACKRC -# define SERVICE "SLACKRC" -# define NISSERVICE ETCDIR "/rc.d/rc.ypbind" -# define NISRESTARTARGS "restart" -# define NTPSERVICE ETCDIR "/rc.d/rc.ntpd" -# define NTPRESTARTARGS "restart" -#endif -#if ENABLE_SERVICE -# define SERVICE "SERVICE" -# define NISSERVICE "service" -# define NISRESTARTARGS "ypbind", "restart" -# define NTPSERVICE "service" -# define NTPRESTARTARGS "ntpd", "restart" -#endif -#if ENABLE_SYSV -# define SERVICE "SYSV" -# define NISSERVICE ETCDIR "/init.d/ypbind" -# define NISRESTARTARGS "restart" -# define NTPSERVICE ETCDIR "/init.d/ntpd" -# define NTPRESTARTARGS "restart" -#endif - -#ifndef NISSERVICE -# undef ENABLE_NIS -#endif -#ifndef NTPSERVICE -# undef ENABLE_NTP -#endif - -#endif diff --git a/workspace/customdhcpcd/src/configure.c b/workspace/customdhcpcd/src/configure.c deleted file mode 100644 index 91e3c9f..0000000 --- a/workspace/customdhcpcd/src/configure.c +++ /dev/null @@ -1,813 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include - -#include - -#include -#ifdef __linux__ -# include -#endif -#include -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "common.h" -#include "configure.h" -#include "dhcp.h" -#ifdef ENABLE_INFO -# include "info.h" -#endif -#include "interface.h" -#include "dhcpcd.h" -#include "logger.h" -#include "signal.h" -#include "socket.h" - -#include "status.h" -#include "logwriter.h" - -static int file_in_path (const char *file) -{ - char *p = getenv ("PATH"); - char *path; - char *token; - struct stat s; - char mypath[PATH_MAX]; - int retval = -1; - - if (! p) { - errno = ENOENT; - return (-1); - } - - path = strdup (p); - p = path; - while ((token = strsep (&p, ":"))) { - snprintf (mypath, PATH_MAX, "%s/%s", token, file); - if (stat (mypath, &s) == 0) { - retval = 0; - break; - } - } - free (path); - return (retval); -} - -/* IMPORTANT: Ensure that the last parameter is NULL when calling */ -static int exec_cmd (const char *cmd, const char *args, ...) -{ - va_list va; - char **argv; - int n = 1; - int ret = 0; - pid_t pid; - sigset_t full; - sigset_t old; - - va_start (va, args); - while (va_arg (va, char *) != NULL) - n++; - va_end (va); - argv = xmalloc (sizeof (char *) * (n + 2)); - - va_start (va, args); - n = 2; - argv[0] = (char *) cmd; - argv[1] = (char *) args; - while ((argv[n] = va_arg (va, char *)) != NULL) - n++; - va_end (va); - - /* OK, we need to block signals */ - sigfillset (&full); - sigprocmask (SIG_SETMASK, &full, &old); - -#ifdef THERE_IS_NO_FORK - signal_reset (); - pid = vfork (); -#else - pid = fork(); -#endif - - switch (pid) { - case -1: - logger (LOG_ERR, "vfork: %s", strerror (errno)); - ret = -1; - break; - case 0: -#ifndef THERE_IS_NO_FORK - signal_reset (); -#endif - sigprocmask (SIG_SETMASK, &old, NULL); - if (execvp (cmd, argv) && errno != ENOENT) - logger (LOG_ERR, "error executing \"%s\": %s", - cmd, strerror (errno)); - _exit (111); - /* NOTREACHED */ - } - -#ifdef THERE_IS_NO_FORK - signal_setup (); -#endif - - /* Restore our signals */ - sigprocmask (SIG_SETMASK, &old, NULL); - - free (argv); - return (ret); -} - -static void exec_script (const char *script, const char *infofile, - const char *arg) -{ - struct stat buf; - - if (! script || ! infofile || ! arg) - return; - - if (stat (script, &buf) == -1) { - if (strcmp (script, DEFAULT_SCRIPT) != 0) - logger (LOG_ERR, "`%s': %s", script, strerror (ENOENT)); - return; - } - -#ifdef ENABLE_INFO - logger (LOG_DEBUG, "exec \"%s\" \"%s\" \"%s\"", script, infofile, arg); - exec_cmd (script, infofile, arg, (char *) NULL); -#else - logger (LOG_DEBUG, "exec \"%s\" \"\" \"%s\"", script, arg); - exec_cmd (script, "", arg, (char *) NULL); -#endif -} - -static int make_resolv (const char *ifname, const dhcp_t *dhcp) -{ - FILE *f = NULL; - address_t *address; - -#ifdef ENABLE_RESOLVCONF - char *resolvconf = NULL; - - if (file_in_path ("resolvconf") == 0) { - size_t len = strlen ("resolvconf -a ") + strlen (ifname) + 1; - resolvconf = xmalloc (sizeof (char) * len); - snprintf (resolvconf, len, "resolvconf -a %s", ifname); - if ((f = popen (resolvconf , "w"))) - logger (LOG_DEBUG, - "sending DNS information to resolvconf"); - else if (errno == EEXIST) - logger (LOG_ERR, "popen: %s", strerror (errno)); - - if (ferror (f)) - logger (LOG_ERR, "ferror"); - free (resolvconf); - } -#endif - if (! f) { - logger (LOG_DEBUG, "writing "RESOLVFILE); - if (! (f = fopen(RESOLVFILE, "w"))) - logger (LOG_ERR, "fopen `%s': %s", RESOLVFILE, strerror (errno)); - } - - if (! f) - return (-1); - - fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname); - if (dhcp->dnssearch) - fprintf (f, "search %s\n", dhcp->dnssearch); - else if (dhcp->dnsdomain) { - fprintf (f, "search %s\n", dhcp->dnsdomain); - } - - STAILQ_FOREACH (address, dhcp->dnsservers, entries) - fprintf (f, "nameserver %s\n", inet_ntoa (address->address)); - -#ifdef ENABLE_RESOLVCONF - if (resolvconf) - pclose (f); - else -#endif - fclose (f); - - /* Refresh the local resolver */ - res_init (); - return (0); -} - -static void restore_resolv (const char *ifname) -{ -#ifdef ENABLE_RESOLVCONF - if (file_in_path ("resolvconf") == 0) { - logger (LOG_DEBUG, "removing information from resolvconf"); - exec_cmd("resolvconf", "-d", ifname, (char *) NULL); - } -#endif -} - -static bool in_addresses (const struct address_head *addresses, - struct in_addr address) -{ - const address_t *addr; - - STAILQ_FOREACH (addr, addresses, entries) - if (addr->address.s_addr == address.s_addr) - return (true); - - return (false); -} - -static bool in_routes (const struct route_head *routes, route_t *route) -{ - const route_t *r; - - if (! routes) - return (false); - - STAILQ_FOREACH (r, routes, entries) - if (r->destination.s_addr == route->destination.s_addr && - r->netmask.s_addr == route->netmask.s_addr && - r->gateway.s_addr == route->gateway.s_addr) - return (true); - - return (false); -} - -#ifdef ENABLE_NTP -static int _make_ntp (const char *file, const char *ifname, const dhcp_t *dhcp) -{ - FILE *f; - address_t *address; - char *a; - char *line; - int tomatch = 0; - char *token; - bool ntp = false; - - STAILQ_FOREACH (address, dhcp->ntpservers, entries) - tomatch++; - - /* Check that we really need to update the servers. - * We do this because ntp has to be restarted to - * work with a changed config. */ - if (! (f = fopen (file, "r"))) { - if (errno != ENOENT) { - logger (LOG_ERR, "fopen `%s': %s", - file, strerror (errno)); - return (-1); - } - } else { - while (tomatch != 0 && (line = get_line (f))) { - struct in_addr addr; - - a = line; - token = strsep (&a, " "); - if (! token || strcmp (token, "server") != 0) - goto next; - - if ((token = strsep (&a, " \n")) == NULL) - goto next; - - if (inet_aton (token, &addr) == 1 && - in_addresses (dhcp->ntpservers, addr)) - tomatch--; - -next: - free (line); - } - fclose (f); - - /* File has the same name servers that we do, - * so no need to restart ntp */ - if (tomatch == 0) { - logger (LOG_DEBUG, "%s already configured, skipping", - file); - return (0); - } - } - - logger (LOG_DEBUG, "writing %s", file); - if (! (f = fopen (file, "w"))) { - logger (LOG_ERR, "fopen `%s': %s", file, strerror (errno)); - return (-1); - } - - fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname); -#ifdef NTPFILE - if (strcmp (file, NTPFILE) == 0) { - ntp = true; - fprintf (f, "restrict default noquery notrust nomodify\n"); - fprintf (f, "restrict 127.0.0.1\n"); - } -#endif - - STAILQ_FOREACH (address, dhcp->ntpservers, entries) { - a = inet_ntoa (address->address); - if (ntp) - fprintf (f, "restrict %s nomodify notrap noquery\n", a); - fprintf (f, "server %s\n", a); - } - fclose (f); - - return (1); -} - -static int make_ntp (const char *ifname, const dhcp_t *dhcp) -{ - /* On some systems we have only have one ntp service, but we don't - * know which configuration file we're using. So we need to write - * to both and restart accordingly. */ - - bool restart_ntp = false; - bool restart_openntp = false; - int retval = 0; - -#ifdef NTPFILE - if (_make_ntp (NTPFILE, ifname, dhcp) > 0) - restart_ntp = true; -#endif - -#ifdef OPENNTPFILE - if (_make_ntp (OPENNTPFILE, ifname, dhcp) > 0) - restart_openntp = true; -#endif - -#ifdef NTPSERVICE - if (restart_ntp) { -#ifdef NTPCHECK - if (system (NTPCHECK) == 0) -#endif - retval += exec_cmd (NTPSERVICE, NTPRESTARTARGS, - (char *) NULL); - } -#endif - -#if defined (NTPSERVICE) && defined (OPENNTPSERVICE) - if (restart_openntp && - (strcmp (NTPSERVICE, OPENNTPSERVICE) != 0 || ! restart_ntp)) - { -#ifdef OPENNTPCHECK - if (system (OPENNTPCHECK) == 0) -#endif - retval += exec_cmd (OPENNTPSERVICE, - OPENNTPRESTARTARGS, (char *) NULL); - } -#elif defined (OPENNTPSERVICE) && ! defined (NTPSERVICE) - if (restart_openntp) { -#ifdef OPENNTPCHECK - if (system (OPENNTPCHECK) == 0) -#endif - retval += exec_cmd (OPENNTPSERVICE, - OPENNTPRESTARTARGS, (char *) NULL); - } -#endif - - return (retval); -} -#endif - -#ifdef ENABLE_NIS -#define PREFIXSIZE 256 -static int make_nis (const char *ifname, const dhcp_t *dhcp) -{ - FILE *f; - address_t *address; - char *prefix; - - logger (LOG_DEBUG, "writing "NISFILE); - if (! (f = fopen(NISFILE, "w"))) { - logger (LOG_ERR, "fopen `%s': %s", NISFILE, strerror (errno)); - return (-1); - } - - prefix = xmalloc (sizeof (char) * PREFIXSIZE); - *prefix = '\0'; - fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname); - - if (dhcp->nisdomain) { - setdomainname (dhcp->nisdomain, (int) strlen (dhcp->nisdomain)); - - if (dhcp->nisservers) - snprintf (prefix, PREFIXSIZE, "domain %s server", - dhcp->nisdomain); - else - fprintf (f, "domain %s broadcast\n", dhcp->nisdomain); - } - else - snprintf (prefix, PREFIXSIZE, "%s", "ypserver"); - - NSTAILQ_FOREACH (address, dhcp->nisservers, entries) - fprintf (f, "%s %s\n", prefix, inet_ntoa (address->address)); - - free (prefix); - fclose (f); - -#ifdef NISCHECK - if (system (NISCHECK) == 0) -#endif - exec_cmd (NISSERVICE, NISRESTARTARGS, (char *) NULL); - return (0); -} -#endif - -static char *lookuphostname (char *hostname, const dhcp_t *dhcp, - const options_t *options) -{ - union { - struct sockaddr sa; - struct sockaddr_in sin; - } su; - socklen_t salen; - char *addr; - struct addrinfo hints; - struct addrinfo *res = NULL; - int result; - char *p; - - logger (LOG_DEBUG, "Looking up hostname via DNS"); - addr = xmalloc (sizeof (char) * NI_MAXHOST); - salen = sizeof (su.sa); - memset (&su.sa, 0, salen); - su.sin.sin_family = AF_INET; - memcpy (&su.sin.sin_addr, &dhcp->address, sizeof (su.sin.sin_addr)); - - if ((result = getnameinfo (&su.sa, salen, addr, NI_MAXHOST, - NULL, 0, NI_NAMEREQD)) != 0) { - logger (LOG_ERR, - "Failed to lookup hostname via DNS: %s", - gai_strerror (result)); - free (addr); - return (NULL); - } - - /* Check for a malicious PTR record */ - memset (&hints, 0, sizeof (hints)); - hints.ai_socktype = SOCK_DGRAM; - hints.ai_flags = AI_NUMERICHOST; - result = getaddrinfo (addr, "0", &hints, &res); - if (res) - freeaddrinfo (res); - if (result == 0) - logger (LOG_ERR, "malicious PTR record detected"); - if (result == 0 || ! *addr) { - free (addr); - return (NULL); - } - - p = strchr (addr, '.'); - if (p) { - switch (options->dohostname) { - case 1: /* -H */ - case 4: /* -HHHH */ - break; - case 2: /* -HH */ - case 5: /* -HHHHH */ - /* Strip out the domain if it matches */ - p++; - if (*p && dhcp->dnssearch) { - char *s = xstrdup (dhcp->dnssearch); - char *sp = s; - char *t; - - while ((t = strsep (&sp, " "))) - if (strcmp (t, p) == 0) { - *--p = '\0'; - break; - } - free (s); - } else if (dhcp->dnsdomain) { - if (strcmp (dhcp->dnsdomain, p) == 0) - *--p = '\0'; - } - break; - case 3: /* -HHH */ - case 6: /* -HHHHHH */ - /* Just strip the domain */ - *p = '\0'; - break; - default: /* Too many H! */ - break; - } - } - - strlcpy (hostname, addr, MAXHOSTNAMELEN); - free (addr); - return (hostname); -} - -int configure (const options_t *options, interface_t *iface, - const dhcp_t *dhcp, bool up) -{ - route_t *route = NULL; - struct route_head *new_routes = NULL; - route_t *new_route = NULL; - char *newhostname = NULL; - char *curhostname = NULL; - int remember; -#ifdef ENABLE_IPV4LL - bool haslinklocal = false; -#endif -#ifdef THERE_IS_NO_FORK - int skip = 0; - size_t skiplen; - char *skipp; -#endif - - if (! options || ! iface || ! dhcp) - return (-1); - - if (dhcp->address.s_addr == 0) - up = 0; - - /* Remove old routes. - * Always do this as the interface may have >1 address not added by us - * so the routes we added may still exist. */ - NSTAILQ_FOREACH (route, iface->previous_routes, entries) - if ((route->destination.s_addr || options->dogateway) && - (! up || ! in_routes (dhcp->routes, route))) - del_route (iface->name, route->destination, - route->netmask, route->gateway, - options->metric); - /* If we aren't up, then reset the interface as much as we can */ - if (! up) { - if (iface->previous_routes) { - free_route (iface->previous_routes); - iface->previous_routes = NULL; - } - - /* Restore the original MTU value */ - if (iface->mtu && iface->previous_mtu != iface->mtu) { - set_mtu (iface->name, iface->mtu); - iface->previous_mtu = iface->mtu; - } - -#ifdef ENABLE_INFO - /* If we haven't created an info file, do so now */ - if (! dhcp->frominfo) - write_info (iface, dhcp, options, false); -#endif - - /* Only reset things if we had set them before */ - if (iface->previous_address.s_addr != 0) { - if (! options->keep_address) { - del_address (iface->name, - iface->previous_address, - iface->previous_netmask); - memset (&iface->previous_address, - 0, sizeof (iface->previous_address)); - memset (&iface->previous_netmask, - 0, sizeof (iface->previous_netmask)); - } - } - - restore_resolv (iface->name); - exec_script (options->script, iface->infofile, "down"); - - return (0); - } - - /* Set the MTU requested. - * If the DHCP server no longer sends one OR it's invalid then - * we restore the original MTU */ - if (options->domtu) { - unsigned short mtu = iface->mtu; - if (dhcp->mtu) - mtu = dhcp->mtu; - - if (mtu != iface->previous_mtu) { - if (set_mtu (iface->name, mtu) == 0) - iface->previous_mtu = mtu; - } - } - - /* This also changes netmask */ - if (! options->doinform || ! has_address (iface->name, dhcp->address)) - if (add_address (iface->name, dhcp->address, dhcp->netmask, - dhcp->broadcast) == -1 && errno != EEXIST) - return (false); - - /* Now delete the old address if different */ - if (iface->previous_address.s_addr != dhcp->address.s_addr && - iface->previous_address.s_addr != 0 && - ! options->keep_address) - del_address (iface->name, - iface->previous_address, iface->previous_netmask); - -#ifdef __linux__ - /* On linux, we need to change the subnet route to have our metric. */ - if (iface->previous_address.s_addr != dhcp->address.s_addr && - options->metric > 0 && - dhcp->netmask.s_addr != INADDR_BROADCAST) - { - struct in_addr td; - struct in_addr tg; - memset (&td, 0, sizeof (td)); - memset (&tg, 0, sizeof (tg)); - td.s_addr = dhcp->address.s_addr & dhcp->netmask.s_addr; - add_route (iface->name, td, dhcp->netmask, tg, options->metric); - del_route (iface->name, td, dhcp->netmask, tg, 0); - } -#endif - -#ifdef THERE_IS_NO_FORK - free (dhcpcd_skiproutes); - /* We can never have more than 255 routes. So we need space - * for 255 3 digit numbers and commas */ - skiplen = 255 * 4 + 1; - skipp = dhcpcd_skiproutes = xmalloc (sizeof (char) * skiplen); - *skipp = '\0'; -#endif - - /* Remember added routes */ - NSTAILQ_FOREACH (route, dhcp->routes, entries) { -#ifdef ENABLE_IPV4LL - /* Check if we have already got a link locale route dished - * out by the DHCP server */ - if (route->destination.s_addr == htonl (LINKLOCAL_ADDR) && - route->netmask.s_addr == htonl (LINKLOCAL_MASK)) - haslinklocal = true; -#endif - /* Don't set default routes if not asked to */ - if (route->destination.s_addr == 0 && - route->netmask.s_addr == 0 && - ! options->dogateway) - continue; - - remember = add_route (iface->name, route->destination, - route->netmask, route->gateway, - options->metric); - /* If we failed to add the route, we may have already added it - ourselves. If so, remember it again. */ - if (remember < 0 && in_routes (iface->previous_routes, route)) - remember = 1; - - if (remember >= 0) { - if (! new_routes) { - new_routes = xmalloc (sizeof (*new_routes)); - STAILQ_INIT (new_routes); - } - new_route = xmalloc (sizeof (route_t)); - memcpy (new_route, route, sizeof (*new_route)); - STAILQ_INSERT_TAIL (new_routes, new_route, entries); - } -#ifdef THERE_IS_NO_FORK - /* If we have daemonised yet we need to record which routes - * we failed to add so we can skip them */ - else if (! options->daemonised) { - /* We can never have more than 255 / 4 routes, - * so 3 chars is plently */ - if (*skipp) - *skipp++ = ','; - skipp += snprintf (skipp, - dhcpcd_skiproutes + skiplen - skipp, - "%d", skip); - } - skip++; -#endif - } - -#ifdef THERE_IS_NO_FORK - if (*dhcpcd_skiproutes) - *skipp = '\0'; - else { - free (dhcpcd_skiproutes); - dhcpcd_skiproutes = NULL; - } -#endif - -#ifdef ENABLE_IPV4LL - /* Ensure we always add the link local route if we got a private - * address and isn't link local itself */ - if (options->doipv4ll && - ! haslinklocal && - IN_PRIVATE (ntohl (dhcp->address.s_addr))) - { - struct in_addr dest; - struct in_addr mask; - struct in_addr gate; - - dest.s_addr = htonl (LINKLOCAL_ADDR); - mask.s_addr = htonl (LINKLOCAL_MASK); - gate.s_addr = 0; - remember = add_route (iface->name, dest, mask, gate, - options->metric); - - if (remember >= 0) { - if (! new_routes) { - new_routes = xmalloc (sizeof (*new_routes)); - STAILQ_INIT (new_routes); - } - new_route = xmalloc (sizeof (*new_route)); - new_route->destination.s_addr = dest.s_addr; - new_route->netmask.s_addr = mask.s_addr; - new_route->gateway.s_addr = gate.s_addr; - STAILQ_INSERT_TAIL (new_routes, new_route, entries); - } - } -#endif - - if (iface->previous_routes) - free_route (iface->previous_routes); - iface->previous_routes = new_routes; - - logToQt(LOG_INFO, DHCPCD_WRITE, ""); - if (options->dodns && dhcp->dnsservers) - make_resolv(iface->name, dhcp); - else - logger (LOG_DEBUG, "no dns information to write"); - -#ifdef ENABLE_NTP - if (options->dontp && dhcp->ntpservers) - make_ntp(iface->name, dhcp); -#endif - -#ifdef ENABLE_NIS - if (options->donis && (dhcp->nisservers || dhcp->nisdomain)) - make_nis(iface->name, dhcp); -#endif - - curhostname = xmalloc (sizeof (char) * MAXHOSTNAMELEN); - *curhostname = '\0'; - - gethostname (curhostname, MAXHOSTNAMELEN); - if (options->dohostname || - strlen (curhostname) == 0 || - strcmp (curhostname, "(none)") == 0 || - strcmp (curhostname, "localhost") == 0) - { - newhostname = xmalloc (sizeof (char) * MAXHOSTNAMELEN); - - if (dhcp->hostname) - strlcpy (newhostname, dhcp->hostname, MAXHOSTNAMELEN); - else - *newhostname = '\0'; - - /* Now we have made a resolv.conf we can obtain a hostname - * if we need it */ - if (! *newhostname || options->dohostname > 3) - lookuphostname (newhostname, dhcp, options); - - if (*newhostname) { - logger (LOG_INFO, "setting hostname to `%s'", - newhostname); - sethostname (newhostname, (int) strlen (newhostname)); - } - - free (newhostname); - } - - free (curhostname); - -#ifdef ENABLE_INFO - if (! dhcp->frominfo) - write_info (iface, dhcp, options, true); -#endif - - if (iface->previous_address.s_addr != dhcp->address.s_addr || - iface->previous_netmask.s_addr != dhcp->netmask.s_addr) - { - memcpy (&iface->previous_address, - &dhcp->address, sizeof (iface->previous_address)); - memcpy (&iface->previous_netmask, - &dhcp->netmask, sizeof (iface->previous_netmask)); - exec_script (options->script, iface->infofile, "new"); - } else - exec_script (options->script, iface->infofile, "up"); - - return (0); -} diff --git a/workspace/customdhcpcd/src/configure.h b/workspace/customdhcpcd/src/configure.h deleted file mode 100644 index 3166947..0000000 --- a/workspace/customdhcpcd/src/configure.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef DHCPCONFIG_H -#define DHCPCONFIG_H - -#include "dhcpcd.h" -#include "interface.h" -#include "dhcp.h" - -int configure (const options_t *options, interface_t *iface, - const dhcp_t *dhcp, bool up); - -#endif diff --git a/workspace/customdhcpcd/src/customdhcpcd b/workspace/customdhcpcd/src/customdhcpcd deleted file mode 100755 index 5ef34d0..0000000 Binary files a/workspace/customdhcpcd/src/customdhcpcd and /dev/null differ diff --git a/workspace/customdhcpcd/src/customdhcpcd.c b/workspace/customdhcpcd/src/customdhcpcd.c deleted file mode 100644 index 7c5e219..0000000 --- a/workspace/customdhcpcd/src/customdhcpcd.c +++ /dev/null @@ -1,17 +0,0 @@ -/* - ============================================================================ - Name : customdhcpcd.c - Author : Niklas Goby - Version : - Copyright : Your copyright notice - Description : Hello World in C, Ansi-style - ============================================================================ - */ - -#include -#include - -int main(void) { - puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */ - return EXIT_SUCCESS; -} diff --git a/workspace/customdhcpcd/src/dhcp.c b/workspace/customdhcpcd/src/dhcp.c deleted file mode 100644 index f625e8f..0000000 --- a/workspace/customdhcpcd/src/dhcp.c +++ /dev/null @@ -1,933 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include - -#include -#include - -#include - -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" - -#include "common.h" -#include "dhcpcd.h" -#include "dhcp.h" -#include "interface.h" -#include "logger.h" -#include "socket.h" - -#ifndef STAILQ_CONCAT -#define STAILQ_CONCAT(head1, head2) do { \ - if (!STAILQ_EMPTY((head2))) { \ - *(head1)->stqh_last = (head2)->stqh_first; \ - (head1)->stqh_last = (head2)->stqh_last; \ - STAILQ_INIT((head2)); \ - } \ -} while (0) -#endif - -typedef struct message { - int value; - const char *name; -} dhcp_message_t; - -static dhcp_message_t dhcp_messages[] = { - { DHCP_DISCOVER, "DHCP_DISCOVER" }, - { DHCP_OFFER, "DHCP_OFFER" }, - { DHCP_REQUEST, "DHCP_REQUEST" }, - { DHCP_DECLINE, "DHCP_DECLINE" }, - { DHCP_ACK, "DHCP_ACK" }, - { DHCP_NAK, "DHCP_NAK" }, - { DHCP_RELEASE, "DHCP_RELEASE" }, - { DHCP_INFORM, "DHCP_INFORM" }, - { -1, NULL } -}; - -static const char *dhcp_message (int type) -{ - dhcp_message_t *d; - for (d = dhcp_messages; d->name; d++) - if (d->value == type) - return (d->name); - - return (NULL); -} - -ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, - uint32_t xid, char type, const options_t *options) -{ - struct udp_dhcp_packet *packet; - dhcpmessage_t *message; - unsigned char *m; - unsigned char *p; - unsigned char *n_params = NULL; - size_t l; - struct in_addr from; - struct in_addr to; - time_t up = uptime() - iface->start_uptime; - uint32_t ul; - uint16_t sz; - size_t message_length; - ssize_t retval; - - if (!iface || !options || !dhcp) - return -1; - - memset (&from, 0, sizeof (from)); - memset (&to, 0, sizeof (to)); - - if (type == DHCP_RELEASE) - to.s_addr = dhcp->serveraddress.s_addr; - - message = xzalloc (sizeof (*message)); - m = (unsigned char *) message; - p = (unsigned char *) &message->options; - - if ((type == DHCP_INFORM || - type == DHCP_RELEASE || - type == DHCP_REQUEST) && - ! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr))) - { - message->ciaddr = iface->previous_address.s_addr; - from.s_addr = iface->previous_address.s_addr; - - /* Just incase we haven't actually configured the address yet */ - if (type == DHCP_INFORM && iface->previous_address.s_addr == 0) - message->ciaddr = dhcp->address.s_addr; - - /* Zero the address if we're currently on a different subnet */ - if (type == DHCP_REQUEST && - iface->previous_netmask.s_addr != dhcp->netmask.s_addr) - message->ciaddr = from.s_addr = 0; - - if (from.s_addr != 0) - to.s_addr = dhcp->serveraddress.s_addr; - } - - message->op = DHCP_BOOTREQUEST; - message->hwtype = iface->family; - switch (iface->family) { - case ARPHRD_ETHER: - case ARPHRD_IEEE802: - message->hwlen = ETHER_ADDR_LEN; - memcpy (&message->chaddr, &iface->hwaddr, - ETHER_ADDR_LEN); - break; - case ARPHRD_IEEE1394: - case ARPHRD_INFINIBAND: - message->hwlen = 0; - if (message->ciaddr == 0) - message->flags = htons (BROADCAST_FLAG); - break; - default: - logger (LOG_ERR, "dhcp: unknown hardware type %d", - iface->family); - } - - if (up < 0 || up > (time_t) UINT16_MAX) - message->secs = htons ((uint16_t) UINT16_MAX); - else - message->secs = htons (up); - message->xid = xid; - message->cookie = htonl (MAGIC_COOKIE); - - *p++ = DHCP_MESSAGETYPE; - *p++ = 1; - *p++ = type; - - if (type == DHCP_REQUEST) { - *p++ = DHCP_MAXMESSAGESIZE; - *p++ = 2; - sz = get_mtu (iface->name); - if (sz < MTU_MIN) { - if (set_mtu (iface->name, MTU_MIN) == 0) - sz = MTU_MIN; - } - sz = htons (sz); - memcpy (p, &sz, 2); - p += 2; - } - - *p++ = DHCP_CLIENTID; - *p++ = iface->clientid_len; - memcpy (p, iface->clientid, iface->clientid_len); - p+= iface->clientid_len; - - if (type != DHCP_DECLINE && type != DHCP_RELEASE) { - if (options->userclass_len > 0) { - *p++ = DHCP_USERCLASS; - *p++ = options->userclass_len; - memcpy (p, &options->userclass, options->userclass_len); - p += options->userclass_len; - } - - if (*options->classid > 0) { - *p++ = DHCP_CLASSID; - *p++ = l = strlen (options->classid); - memcpy (p, options->classid, l); - p += l; - } - } - - if (type == DHCP_DISCOVER || type == DHCP_REQUEST) { -#define PUTADDR(_type, _val) { \ - *p++ = _type; \ - *p++ = 4; \ - memcpy (p, &_val.s_addr, 4); \ - p += 4; \ -} - if (IN_LINKLOCAL (ntohl (dhcp->address.s_addr))) - logger (LOG_ERR, - "cannot request a link local address"); - else { - if (dhcp->address.s_addr && - dhcp->address.s_addr != - iface->previous_address.s_addr) - { - PUTADDR (DHCP_ADDRESS, dhcp->address); - if (dhcp->serveraddress.s_addr) - PUTADDR (DHCP_SERVERIDENTIFIER, - dhcp->serveraddress); - } - } -#undef PUTADDR - - if (options->leasetime != 0) { - *p++ = DHCP_LEASETIME; - *p++ = 4; - ul = htonl (options->leasetime); - memcpy (p, &ul, 4); - p += 4; - } - } - - if (type == DHCP_DISCOVER || - type == DHCP_INFORM || - type == DHCP_REQUEST) - { - if (options->hostname[0]) { - if (options->fqdn == FQDN_DISABLE) { - *p++ = DHCP_HOSTNAME; - *p++ = l = strlen (options->hostname); - memcpy (p, options->hostname, l); - p += l; - } else { - /* Draft IETF DHC-FQDN option (81) */ - *p++ = DHCP_FQDN; - *p++ = (l = strlen (options->hostname)) + 3; - /* Flags: 0000NEOS - * S: 1 => Client requests Server to update - * a RR in DNS as well as PTR - * O: 1 => Server indicates to client that - * DNS has been updated - * E: 1 => Name data is DNS format - * N: 1 => Client requests Server to not - * update DNS - */ - *p++ = options->fqdn & 0x9; - *p++ = 0; /* from server for PTR RR */ - *p++ = 0; /* from server for A RR if S=1 */ - memcpy (p, options->hostname, l); - p += l; - } - } - - *p++ = DHCP_PARAMETERREQUESTLIST; - n_params = p; - *p++ = 0; - /* Only request DNSSERVER in discover to keep the packets small. - * RFC2131 Section 3.5 states that the REQUEST must include the - * list from the DISCOVER message, so I think this is ok. */ - - if (type == DHCP_DISCOVER && ! options->test) - *p++ = DHCP_DNSSERVER; - else { - if (type != DHCP_INFORM) { - *p++ = DHCP_RENEWALTIME; - *p++ = DHCP_REBINDTIME; - } - *p++ = DHCP_NETMASK; - *p++ = DHCP_BROADCAST; - - /* -S means request CSR and MSCSR - * -SS means only request MSCSR incase DHCP message - * is too big */ - if (options->domscsr < 2) - *p++ = DHCP_CSR; - if (options->domscsr > 0) - *p++ = DHCP_MSCSR; - /* RFC 3442 states classless static routes should be - * before routers and static routes as classless static - * routes override them both */ - *p++ = DHCP_STATICROUTE; - *p++ = DHCP_ROUTERS; - *p++ = DHCP_HOSTNAME; - *p++ = DHCP_DNSSEARCH; - *p++ = DHCP_DNSDOMAIN; - *p++ = DHCP_DNSSERVER; -#ifdef ENABLE_NIS - *p++ = DHCP_NISDOMAIN; - *p++ = DHCP_NISSERVER; -#endif -#ifdef ENABLE_NTP - *p++ = DHCP_NTPSERVER; -#endif - *p++ = DHCP_MTU; -#ifdef ENABLE_INFO - *p++ = DHCP_ROOTPATH; - *p++ = DHCP_SIPSERVER; -#endif - } - - *n_params = p - n_params - 1; - } - *p++ = DHCP_END; - -#ifdef BOOTP_MESSAGE_LENTH_MIN - /* Some crappy DHCP servers think they have to obey the BOOTP minimum - * message length. - * They are wrong, but we should still cater for them. */ - while (p - m < BOOTP_MESSAGE_LENTH_MIN) - *p++ = DHCP_PAD; -#endif - - message_length = p - m; - - packet = xzalloc (sizeof (*packet)); - make_dhcp_packet (packet, (unsigned char *) message, message_length, - from, to); - free (message); - - logger (LOG_DEBUG, "sending %s with xid 0x%x", - dhcp_message (type), xid); - retval = send_packet (iface, ETHERTYPE_IP, (unsigned char *) packet, - message_length + - sizeof (packet->ip) + sizeof (packet->udp)); - free (packet); - return (retval); -} - -/* Decode an RFC3397 DNS search order option into a space - * seperated string. Returns length of string (including - * terminating zero) or zero on error. out may be NULL - * to just determine output length. */ -static unsigned int decode_search (const unsigned char *p, int len, char *out) -{ - const unsigned char *r, *q = p; - unsigned int count = 0, l, hops; - - while (q - p < len) { - r = NULL; - hops = 0; - while ((l = *q++)) { - unsigned int label_type = l & 0xc0; - if (label_type == 0x80 || label_type == 0x40) - return 0; - else if (label_type == 0xc0) { /* pointer */ - l = (l & 0x3f) << 8; - l |= *q++; - - /* save source of first jump. */ - if (!r) - r = q; - - hops++; - if (hops > 255) - return 0; - - q = p + l; - if (q - p >= len) - return 0; - } else { - /* straightforward name segment, add with '.' */ - count += l + 1; - if (out) { - memcpy (out, q, l); - out += l; - *out++ = '.'; - } - q += l; - } - } - - /* change last dot to space */ - if (out) - *(out - 1) = ' '; - - if (r) - q = r; - } - - /* change last space to zero terminator */ - if (out) - *(out - 1) = 0; - - return count; -} - -/* Add our classless static routes to the routes variable - * and return the last route set */ -static struct route_head *decode_CSR (const unsigned char *p, int len) -{ - const unsigned char *q = p; - unsigned int cidr; - unsigned int ocets; - struct route_head *routes = NULL; - route_t *route; - - /* Minimum is 5 -first is CIDR and a router length of 4 */ - if (len < 5) - return NULL; - - while (q - p < len) { - if (! routes) { - routes = xmalloc (sizeof (*routes)); - STAILQ_INIT (routes); - } - - route = xzalloc (sizeof (*route)); - - cidr = *q++; - if (cidr > 32) { - logger (LOG_ERR, - "invalid CIDR of %d in classless static route", - cidr); - free_route (routes); - return (NULL); - } - ocets = (cidr + 7) / 8; - - if (ocets > 0) { - memcpy (&route->destination.s_addr, q, (size_t) ocets); - q += ocets; - } - - /* Now enter the netmask */ - if (ocets > 0) { - memset (&route->netmask.s_addr, 255, (size_t) ocets - 1); - memset ((unsigned char *) &route->netmask.s_addr + - (ocets - 1), - (256 - (1 << (32 - cidr) % 8)), 1); - } - - /* Finally, snag the router */ - memcpy (&route->gateway.s_addr, q, 4); - q += 4; - - STAILQ_INSERT_TAIL (routes, route, entries); - } - - return (routes); -} - -void free_dhcp (dhcp_t *dhcp) -{ - if (! dhcp) - return; - - free_route (dhcp->routes); - free (dhcp->hostname); - free_address (dhcp->dnsservers); - free (dhcp->dnsdomain); - free (dhcp->dnssearch); - free_address (dhcp->ntpservers); - free (dhcp->nisdomain); - free_address (dhcp->nisservers); - free (dhcp->rootpath); - free (dhcp->sipservers); - if (dhcp->fqdn) { - free (dhcp->fqdn->name); - free (dhcp->fqdn); - } -} - - -static bool dhcp_add_address (struct address_head **addresses, - const unsigned char *data, - int length) -{ - int i; - address_t *address; - - for (i = 0; i < length; i += 4) { - /* Sanity check */ - if (i + 4 > length) { - logger (LOG_ERR, "invalid address length"); - return (false); - } - - if (*addresses == NULL) { - *addresses = xmalloc (sizeof (**addresses)); - STAILQ_INIT (*addresses); - } - address = xzalloc (sizeof (*address)); - memcpy (&address->address.s_addr, data + i, 4); - STAILQ_INSERT_TAIL (*addresses, address, entries); - } - - return (true); -} - -#ifdef ENABLE_INFO -static char *decode_sipservers (const unsigned char *data, int length) -{ - char *sip = NULL; - char *p; - const char encoding = *data++; - struct in_addr addr; - size_t len; - - length--; - - switch (encoding) { - case 0: - if ((len = decode_search (data, length, NULL)) > 0) { - sip = xmalloc (len); - decode_search (data, length, sip); - } - break; - - case 1: - if (length == 0 || length % 4 != 0) { - logger (LOG_ERR, - "invalid length %d for option 120", - length + 1); - break; - } - len = ((length / 4) * (4 * 4)) + 1; - sip = p = xmalloc (len); - while (length != 0) { - memcpy (&addr.s_addr, data, 4); - data += 4; - p += snprintf (p, len - (p - sip), - "%s ", inet_ntoa (addr)); - length -= 4; - } - *--p = '\0'; - break; - - default: - logger (LOG_ERR, "unknown sip encoding %d", encoding); - break; - } - - return (sip); -} -#endif - -/* This calculates the netmask that we should use for static routes. - * This IS different from the calculation used to calculate the netmask - * for an interface address. */ -static uint32_t route_netmask (uint32_t ip_in) -{ - /* used to be unsigned long - check if error */ - uint32_t p = ntohl (ip_in); - uint32_t t; - - if (IN_CLASSA (p)) - t = ~IN_CLASSA_NET; - else { - if (IN_CLASSB (p)) - t = ~IN_CLASSB_NET; - else { - if (IN_CLASSC (p)) - t = ~IN_CLASSC_NET; - else - t = 0; - } - } - - while (t & p) - t >>= 1; - - return (htonl (~t)); -} - -static struct route_head *decode_routes (const unsigned char *data, int length) -{ - int i; - struct route_head *head = NULL; - route_t *route; - - for (i = 0; i < length; i += 8) { - if (! head) { - head = xmalloc (sizeof (*head)); - STAILQ_INIT (head); - } - route = xzalloc (sizeof (*route)); - memcpy (&route->destination.s_addr, data + i, 4); - memcpy (&route->gateway.s_addr, data + i + 4, 4); - route->netmask.s_addr = - route_netmask (route->destination.s_addr); - STAILQ_INSERT_TAIL (head, route, entries); - } - - return (head); -} - -static struct route_head *decode_routers (const unsigned char *data, int length) -{ - int i; - struct route_head *head = NULL; - route_t *route = NULL; - - for (i = 0; i < length; i += 4) { - if (! head) { - head = xmalloc (sizeof (*head)); - STAILQ_INIT (head); - } - route = xzalloc (sizeof (*route)); - memcpy (&route->gateway.s_addr, data + i, 4); - STAILQ_INSERT_TAIL (head, route, entries); - } - - return (head); -} - -int parse_dhcpmessage (dhcp_t *dhcp, const dhcpmessage_t *message) -{ - const unsigned char *p = message->options; - const unsigned char *end = p; /* Add size later for gcc-3 issue */ - unsigned char option; - unsigned char length; - unsigned int len = 0; - int retval = -1; - struct timeval tv; - struct route_head *routers = NULL; - struct route_head *routes = NULL; - struct route_head *csr = NULL; - struct route_head *mscsr = NULL; - bool in_overload = false; - bool parse_sname = false; - bool parse_file = false; - - end += sizeof (message->options); - - if (gettimeofday (&tv, NULL) == -1) { - logger (LOG_ERR, "gettimeofday: %s", strerror (errno)); - return (-1); - } - - dhcp->address.s_addr = message->yiaddr; - dhcp->leasedfrom = tv.tv_sec; - dhcp->frominfo = false; - dhcp->address.s_addr = message->yiaddr; - strlcpy (dhcp->servername, (char *) message->servername, - sizeof (dhcp->servername)); - -#define LEN_ERR \ - { \ - logger (LOG_ERR, "invalid length %d for option %d", \ - length, option); \ - p += length; \ - continue; \ - } - -parse_start: - while (p < end) { - option = *p++; - if (! option) - continue; - - if (option == DHCP_END) - goto eexit; - - length = *p++; - - if (option != DHCP_PAD && length == 0) { - logger (LOG_ERR, "option %d has zero length", option); - retval = -1; - goto eexit; - } - - if (p + length >= end) { - logger (LOG_ERR, "dhcp option exceeds message length"); - retval = -1; - goto eexit; - } - - switch (option) { - case DHCP_MESSAGETYPE: - retval = (int) *p; - p += length; - continue; - - default: - if (length == 0) { - logger (LOG_DEBUG, - "option %d has zero length, skipping", - option); - continue; - } - } - -#define LENGTH(_length) \ - if (length != _length) \ - LEN_ERR; -#define MIN_LENGTH(_length) \ - if (length < _length) \ - LEN_ERR; -#define MULT_LENGTH(_mult) \ - if (length % _mult != 0) \ - LEN_ERR; -#define GET_UINT8(_val) \ - LENGTH (sizeof (uint8_t)); \ - memcpy (&_val, p, sizeof (uint8_t)); -#define GET_UINT16(_val) \ - LENGTH (sizeof (uint16_t)); \ - memcpy (&_val, p, sizeof (uint16_t)); -#define GET_UINT32(_val) \ - LENGTH (sizeof (uint32_t)); \ - memcpy (&_val, p, sizeof (uint32_t)); -#define GET_UINT16_H(_val) \ - GET_UINT16 (_val); \ - _val = ntohs (_val); -#define GET_UINT32_H(_val) \ - GET_UINT32 (_val); \ - _val = ntohl (_val); - - switch (option) { - case DHCP_ADDRESS: - GET_UINT32 (dhcp->address.s_addr); - break; - case DHCP_NETMASK: - GET_UINT32 (dhcp->netmask.s_addr); - break; - case DHCP_BROADCAST: - GET_UINT32 (dhcp->broadcast.s_addr); - break; - case DHCP_SERVERIDENTIFIER: - GET_UINT32 (dhcp->serveraddress.s_addr); - break; - case DHCP_LEASETIME: - GET_UINT32_H (dhcp->leasetime); - break; - case DHCP_RENEWALTIME: - GET_UINT32_H (dhcp->renewaltime); - break; - case DHCP_REBINDTIME: - GET_UINT32_H (dhcp->rebindtime); - break; - case DHCP_MTU: - GET_UINT16_H (dhcp->mtu); - /* Minimum legal mtu is 68 accoridng to - * RFC 2132. In practise it's 576 which is the - * minimum maximum message size. */ - if (dhcp->mtu < MTU_MIN) { - logger (LOG_DEBUG, - "MTU %d is too low, minimum is %d; ignoring", - dhcp->mtu, MTU_MIN); - dhcp->mtu = 0; - } - break; - -#undef GET_UINT32_H -#undef GET_UINT32 -#undef GET_UINT16_H -#undef GET_UINT16 -#undef GET_UINT8 - -#define GETSTR(_var) { \ - MIN_LENGTH (sizeof (char)); \ - if (_var) free (_var); \ - _var = xmalloc ((size_t) length + 1); \ - memcpy (_var, p, (size_t) length); \ - memset (_var + length, 0, 1); \ -} - case DHCP_HOSTNAME: - GETSTR (dhcp->hostname); - break; - case DHCP_DNSDOMAIN: - GETSTR (dhcp->dnsdomain); - break; - case DHCP_MESSAGE: - GETSTR (dhcp->message); - break; -#ifdef ENABLE_INFO - case DHCP_ROOTPATH: - GETSTR (dhcp->rootpath); - break; -#endif -#ifdef ENABLE_NIS - case DHCP_NISDOMAIN: - GETSTR (dhcp->nisdomain); - break; -#endif -#undef GETSTR - -#define GETADDR(_var) \ - MULT_LENGTH (4); \ - if (! dhcp_add_address (&_var, p, length)) \ - { \ - retval = -1; \ - goto eexit; \ - } - case DHCP_DNSSERVER: - GETADDR (dhcp->dnsservers); - break; -#ifdef ENABLE_NTP - case DHCP_NTPSERVER: - GETADDR (dhcp->ntpservers); - break; -#endif -#ifdef ENABLE_NIS - case DHCP_NISSERVER: - GETADDR (dhcp->nisservers); - break; -#endif -#undef GETADDR - - case DHCP_DNSSEARCH: - MIN_LENGTH (1); - free (dhcp->dnssearch); - len = decode_search (p, length, NULL); - if (len > 0) { - dhcp->dnssearch = xmalloc (len); - decode_search (p, length, - dhcp->dnssearch); - } - break; - - case DHCP_CSR: - MIN_LENGTH (5); - free_route (csr); - csr = decode_CSR (p, length); - break; - - case DHCP_MSCSR: - MIN_LENGTH (5); - free_route (mscsr); - mscsr = decode_CSR (p, length); - break; - -#ifdef ENABLE_INFO - case DHCP_SIPSERVER: - free (dhcp->sipservers); - dhcp->sipservers = decode_sipservers (p,length); - break; -#endif - - case DHCP_STATICROUTE: - MULT_LENGTH (8); - free_route (routes); - routes = decode_routes (p, length); - break; - - case DHCP_ROUTERS: - MULT_LENGTH (4); - free_route (routers); - routers = decode_routers (p, length); - break; - - case DHCP_OPTIONSOVERLOADED: - LENGTH (1); - /* The overloaded option in an overloaded option - * should be ignored, overwise we may get an - * infinite loop */ - if (! in_overload) { - if (*p & 1) - parse_file = true; - if (*p & 2) - parse_sname = true; - } - break; - - case DHCP_FQDN: - /* We ignore replies about FQDN */ - break; - -#undef LENGTH -#undef MIN_LENGTH -#undef MULT_LENGTH - - default: - logger (LOG_DEBUG, - "no facility to parse DHCP code %u", - option); - break; - } - - p += length; - } - -eexit: - /* We may have options overloaded, so go back and grab them */ - if (parse_file) { - parse_file = false; - p = message->bootfile; - end = p + sizeof (message->bootfile); - in_overload = true; - goto parse_start; - } else if (parse_sname) { - parse_sname = false; - p = message->servername; - end = p + sizeof (message->servername); - memset (dhcp->servername, 0, sizeof (dhcp->servername)); - in_overload = true; - goto parse_start; - } - - /* Fill in any missing fields */ - if (! dhcp->netmask.s_addr) - dhcp->netmask.s_addr = get_netmask (dhcp->address.s_addr); - if (! dhcp->broadcast.s_addr) - dhcp->broadcast.s_addr = dhcp->address.s_addr | - ~dhcp->netmask.s_addr; - - /* If we have classess static routes then we discard - * static routes and routers according to RFC 3442 */ - if (csr) { - dhcp->routes = csr; - free_route (mscsr); - free_route (routers); - free_route (routes); - } else if (mscsr) { - dhcp->routes = mscsr; - free_route (routers); - free_route (routes); - } else { - /* Ensure that we apply static routes before routers */ - if (! routes) - routes = routers; - else if (routers) - STAILQ_CONCAT (routes, routers); - dhcp->routes = routes; - } - - return (retval); -} diff --git a/workspace/customdhcpcd/src/dhcp.h b/workspace/customdhcpcd/src/dhcp.h deleted file mode 100644 index ef97b75..0000000 --- a/workspace/customdhcpcd/src/dhcp.h +++ /dev/null @@ -1,215 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef DHCP_H -#define DHCP_H - -#include -#include -#include -#include -#include - -#include "dhcpcd.h" -#include "interface.h" - -/* Max MTU - defines dhcp option length */ -#define MTU_MAX 1500 -#define MTU_MIN 576 - -/* UDP port numbers for DHCP */ -#define DHCP_SERVER_PORT 67 -#define DHCP_CLIENT_PORT 68 - -#define MAGIC_COOKIE 0x63825363 -#define BROADCAST_FLAG 0x8000 - -/* DHCP message OP code */ -#define DHCP_BOOTREQUEST 1 -#define DHCP_BOOTREPLY 2 - -/* DHCP message type */ -#define DHCP_DISCOVER 1 -#define DHCP_OFFER 2 -#define DHCP_REQUEST 3 -#define DHCP_DECLINE 4 -#define DHCP_ACK 5 -#define DHCP_NAK 6 -#define DHCP_RELEASE 7 -#define DHCP_INFORM 8 - -/* DHCP options */ -enum DHCP_OPTIONS -{ - DHCP_PAD = 0, - DHCP_NETMASK = 1, - DHCP_TIMEROFFSET = 2, - DHCP_ROUTERS = 3, - DHCP_TIMESERVER = 4, - DHCP_NAMESERVER = 5, - DHCP_DNSSERVER = 6, - DHCP_LOGSERVER = 7, - DHCP_COOKIESERVER = 8, - DHCP_HOSTNAME = 12, - DHCP_DNSDOMAIN = 15, - DHCP_ROOTPATH = 17, - DHCP_DEFAULTIPTTL = 23, - DHCP_MTU = 26, - DHCP_BROADCAST = 28, - DHCP_MASKDISCOVERY = 29, - DHCP_ROUTERDISCOVERY = 31, - DHCP_STATICROUTE = 33, - DHCP_NISDOMAIN = 40, - DHCP_NISSERVER = 41, - DHCP_NTPSERVER = 42, - DHCP_ADDRESS = 50, - DHCP_LEASETIME = 51, - DHCP_OPTIONSOVERLOADED = 52, - DHCP_MESSAGETYPE = 53, - DHCP_SERVERIDENTIFIER = 54, - DHCP_PARAMETERREQUESTLIST = 55, - DHCP_MESSAGE = 56, - DHCP_MAXMESSAGESIZE = 57, - DHCP_RENEWALTIME = 58, - DHCP_REBINDTIME = 59, - DHCP_CLASSID = 60, - DHCP_CLIENTID = 61, - DHCP_USERCLASS = 77, /* RFC 3004 */ - DHCP_FQDN = 81, - DHCP_DNSSEARCH = 119, /* RFC 3397 */ - DHCP_SIPSERVER = 120, /* RFC 3361 */ - DHCP_CSR = 121, /* RFC 3442 */ - DHCP_MSCSR = 249, /* MS code for RFC 3442 */ - DHCP_END = 255 -}; - -/* SetFQDNHostName values - lsnybble used in flags - * byte (see buildmsg.c), hsnybble to create order - * and to allow 0x00 to mean disable - */ -enum FQQN { - FQDN_DISABLE = 0x00, - FQDN_NONE = 0x18, - FQDN_PTR = 0x20, - FQDN_BOTH = 0x31 -}; - -typedef struct fqdn_t -{ - uint8_t flags; - uint8_t r1; - uint8_t r2; - char *name; -} fqdn_t; - -typedef struct dhcp_t -{ - char version[11]; - - struct in_addr serveraddress; - char serverhw[IF_NAMESIZE]; - char servername[64]; - - struct in_addr address; - struct in_addr netmask; - struct in_addr broadcast; - unsigned short mtu; - - uint32_t leasedfrom; - uint32_t leasetime; - uint32_t renewaltime; - uint32_t rebindtime; - - struct route_head *routes; - - char *hostname; - fqdn_t *fqdn; - - struct address_head *dnsservers; - char *dnsdomain; - char *dnssearch; - - struct address_head *ntpservers; - - struct address_head *nisservers; - char *nisdomain; - - char *sipservers; - - char *message; - char *rootpath; - - bool frominfo; -} dhcp_t; - -/* Sizes for DHCP options */ -#define DHCP_CHADDR_LEN 16 -#define SERVERNAME_LEN 64 -#define BOOTFILE_LEN 128 -#define DHCP_UDP_LEN (20 + 8) -#define DHCP_BASE_LEN (4 + 4 + 2 + 2 + 4 + 4 + 4 + 4 + 4) -#define DHCP_RESERVE_LEN (4 + 4 + 4 + 4 + 2) -#define DHCP_FIXED_LEN (DHCP_BASE_LEN + DHCP_CHADDR_LEN + \ - + SERVERNAME_LEN + BOOTFILE_LEN) -#define DHCP_OPTION_LEN (MTU_MAX - DHCP_FIXED_LEN - DHCP_UDP_LEN \ - - DHCP_RESERVE_LEN) - -/* Some crappy DHCP servers require the BOOTP minimum length */ -#define BOOTP_MESSAGE_LENTH_MIN 300 - -typedef struct dhcpmessage_t -{ - unsigned char op; /* message type */ - unsigned char hwtype; /* hardware address type */ - unsigned char hwlen; /* hardware address length */ - unsigned char hwopcount; /* should be zero in client message */ - uint32_t xid; /* transaction id */ - uint16_t secs; /* elapsed time in sec. from boot */ - uint16_t flags; - uint32_t ciaddr; /* (previously allocated) client IP */ - uint32_t yiaddr; /* 'your' client IP address */ - uint32_t siaddr; /* should be zero in client's messages */ - uint32_t giaddr; /* should be zero in client's messages */ - unsigned char chaddr[DHCP_CHADDR_LEN]; /* client's hardware address */ - unsigned char servername[SERVERNAME_LEN]; /* server host name */ - unsigned char bootfile[BOOTFILE_LEN]; /* boot file name */ - uint32_t cookie; - unsigned char options[DHCP_OPTION_LEN]; /* message options - cookie */ -} dhcpmessage_t; - -struct udp_dhcp_packet -{ - struct ip ip; - struct udphdr udp; - dhcpmessage_t dhcp; -}; - -ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, - uint32_t xid, char type, const options_t *options); -void free_dhcp (dhcp_t *dhcp); -int parse_dhcpmessage (dhcp_t *dhcp, const dhcpmessage_t *message); -#endif diff --git a/workspace/customdhcpcd/src/dhcpcd.c b/workspace/customdhcpcd/src/dhcpcd.c deleted file mode 100644 index 9f91239..0000000 --- a/workspace/customdhcpcd/src/dhcpcd.c +++ /dev/null @@ -1,673 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -const char copyright[] = "Copyright (c) 2006-2008 Roy Marples"; - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "client.h" -#include "dhcpcd.h" -#include "dhcp.h" -#include "interface.h" -#include "logger.h" -#include "socket.h" -#include "version.h" - -#include "logwriter.h" -#include "status.h" - -static int doversion = 0; -static int dohelp = 0; -#define EXTRA_OPTS -static const struct option longopts[] = { - {"arp", no_argument, NULL, 'a'}, - {"script", required_argument, NULL, 'c'}, - {"debug", no_argument, NULL, 'd'}, - {"hostname", optional_argument, NULL, 'h'}, - {"classid", optional_argument, NULL, 'i'}, - {"release", no_argument, NULL, 'k'}, - {"leasetime", required_argument, NULL, 'l'}, - {"metric", required_argument, NULL, 'm'}, - {"renew", no_argument, NULL, 'n'}, - {"persistent", no_argument, NULL, 'p'}, - {"qtsocketaddress", required_argument, NULL, 'q'}, - {"inform", optional_argument, NULL, 's'}, - {"request", optional_argument, NULL, 'r'}, - {"timeout", required_argument, NULL, 't'}, - {"userclass", required_argument, NULL, 'u'}, - {"exit", no_argument, NULL, 'x'}, - {"lastlease", no_argument, NULL, 'E'}, - {"fqdn", required_argument, NULL, 'F'}, - {"nogateway", no_argument, NULL, 'G'}, - {"sethostname", no_argument, NULL, 'H'}, - {"clientid", optional_argument, NULL, 'I'}, - {"noipv4ll", no_argument, NULL, 'L'}, - {"nomtu", no_argument, NULL, 'M'}, - {"nontp", no_argument, NULL, 'N'}, - {"nodns", no_argument, NULL, 'R'}, - {"msscr", no_argument, NULL, 'S'}, - {"test", no_argument, NULL, 'T'}, - {"nonis", no_argument, NULL, 'Y'}, - {"help", no_argument, &dohelp, 1}, - {"version", no_argument, &doversion, 1}, -#ifdef THERE_IS_NO_FORK - {"daemonised", no_argument, NULL, 'f'}, - {"skiproutes", required_argument, NULL, 'g'}, -#endif - {NULL, 0, NULL, 0} -}; - -#ifdef THERE_IS_NO_FORK -char dhcpcd[PATH_MAX]; -char **dhcpcd_argv = NULL; -int dhcpcd_argc = 0; -char *dhcpcd_skiproutes = NULL; -#undef EXTRA_OPTS -#define EXTRA_OPTS "fg:" -#endif - -static int atoint (const char *s) -{ - char *t; - long n; - - errno = 0; - n = strtol (s, &t, 0); - if ((errno != 0 && n == 0) || s == t || - (errno == ERANGE && (n == LONG_MAX || n == LONG_MIN))) - { - logger (LOG_ERR, "`%s' out of range", s); - return (-1); - } - - return ((int) n); -} - -static pid_t read_pid (const char *pidfile) -{ - FILE *fp; - pid_t pid = 0; - - if ((fp = fopen (pidfile, "r")) == NULL) { - errno = ENOENT; - return 0; - } - - fscanf (fp, "%d", &pid); - fclose (fp); - - - return (pid); -} - -static void usage (void) -{ - printf ("usage: "PACKAGE" [-adknpEGHMNRSTY] [-c script] [-h hostname] [-i classID]\n" - " [-l leasetime] [-m metric] [-r ipaddress] [-s ipaddress]\n" - " [-t timeout] [-u userclass] [-F none | ptr | both]\n" - " [-I clientID] [-q qtsocketaddress] \n"); -} - - -int main (int argc, char **argv) -{ - options_t *options; - int userclasses = 0; - int opt; - int option_index = 0; - char *prefix; - pid_t pid; - int debug = 0; - int i; - int pidfd = -1; - int sig = 0; - int retval = EXIT_FAILURE; - - /* Close any un-needed fd's */ - for (i = getdtablesize() - 1; i >= 3; --i) - close (i); - - openlog (PACKAGE, LOG_PID, LOG_LOCAL0); - - options = xzalloc (sizeof (*options)); - options->script = (char *) DEFAULT_SCRIPT; - snprintf (options->classid, CLASS_ID_MAX_LEN, "%s %s", - PACKAGE, VERSION); - - options->doarp = true; - options->dodns = true; - options->domtu = true; - options->donis = true; - options->dontp = true; - options->dogateway = true; - options->daemonise = true; - options->doinform = false; - options->doipv4ll = true; - options->doduid = true; - options->timeout = DEFAULT_TIMEOUT; - /* added by Niklas Goby, additional field, storing the socket address path for - * communicating with Qt "server" - * defined in dhcpcd.h */ - strcpy(options->qtsocketaddress, DEFAULT_QTSOCKETADDRESS); - - gethostname (options->hostname, sizeof (options->hostname)); - if (strcmp (options->hostname, "(none)") == 0 || - strcmp (options->hostname, "localhost") == 0) - memset (options->hostname, 0, sizeof (options->hostname)); - - - /* Don't set any optional arguments here so we retain POSIX - * compatibility with getopt */ - while ((opt = getopt_long(argc, argv, EXTRA_OPTS - "c:dh:i:kl:m:npq:r:s:t:u:xAEF:GHI:LMNRSTY", - longopts, &option_index)) != -1) - { - switch (opt) { - case 0: - if (longopts[option_index].flag) - break; - logger (LOG_ERR, - "option `%s' should set a flag", - longopts[option_index].name); - goto abort; - case 'c': - options->script = optarg; - break; - case 'd': - debug++; - switch (debug) { - case 1: - setloglevel (LOG_DEBUG); - break; - case 2: - options->daemonise = false; - break; - } - break; - #ifdef THERE_IS_NO_FORK - case 'f': - options->daemonised = true; - close_fds (); - break; - case 'g': - dhcpcd_skiproutes = xstrdup (optarg); - break; - #endif - case 'h': - if (! optarg) - *options->hostname = '\0'; - else if (strlen (optarg) > MAXHOSTNAMELEN) { - logger (LOG_ERR, - "`%s' too long for HostName string, max is %d", - optarg, MAXHOSTNAMELEN); - goto abort; - } else - strlcpy (options->hostname, optarg, - sizeof (options->hostname)); - break; - case 'i': - if (! optarg) { - *options->classid = '\0'; - } else if (strlen (optarg) > CLASS_ID_MAX_LEN) { - logger (LOG_ERR, - "`%s' too long for ClassID string, max is %d", - optarg, CLASS_ID_MAX_LEN); - goto abort; - } else - strlcpy (options->classid, optarg, - sizeof (options->classid)); - break; - case 'k': - sig = SIGHUP; - break; - case 'l': - if (*optarg == '-') { - logger (LOG_ERR, - "leasetime must be a positive value"); - goto abort; - } - errno = 0; - options->leasetime = (uint32_t) strtol (optarg, NULL, 0); - if (errno == EINVAL || errno == ERANGE) { - logger (LOG_ERR, "`%s' out of range", optarg); - goto abort; - } - break; - case 'm': - options->metric = atoint (optarg); - if (options->metric < 0) { - logger (LOG_ERR, - "metric must be a positive value"); - goto abort; - } - break; - case 'n': - sig = SIGALRM; - break; - case 'p': - options->persistent = true; - break; - case 'q': - if (strlen(optarg) > QTSOCKETADDRESSLENGTH) { - logger(LOG_ERR, "`%s' too long for an socket address path (max=%d)", - optarg, QTSOCKETADDRESSLENGTH); - goto abort; - } - strlcpy(options->qtsocketaddress, optarg, sizeof(options->qtsocketaddress)); - break; - case 's': - options->doinform = true; - options->doarp = false; - if (! optarg || strlen (optarg) == 0) { - options->request_address.s_addr = 0; - break; - } else { - char *slash = strchr (optarg, '/'); - if (slash) { - int cidr; - /* nullify the slash, so the -r option can read the - * address */ - *slash++ = '\0'; - if (sscanf (slash, "%d", &cidr) != 1 || - inet_cidrtoaddr (cidr, &options->request_netmask) != 0) { - logger (LOG_ERR, "`%s' is not a valid CIDR", slash); - goto abort; - } - } - } - /* FALLTHROUGH */ - case 'r': - if (! options->doinform) - options->dorequest = true; - if (strlen (optarg) > 0 && - ! inet_aton (optarg, &options->request_address)) - { - logger (LOG_ERR, "`%s' is not a valid IP address", optarg); - goto abort; - } - break; - case 't': - options->timeout = atoint (optarg); - if (options->timeout < 0) { - logger (LOG_ERR, "timeout must be a positive value"); - goto abort; - } - break; - case 'u': - { - int offset = 0; - for (i = 0; i < userclasses; i++) - offset += (int) options->userclass[offset] + 1; - if (offset + 1 + strlen (optarg) > USERCLASS_MAX_LEN) { - logger (LOG_ERR, "userclass overrun, max is %d", - USERCLASS_MAX_LEN); - goto abort; - } - userclasses++; - memcpy (options->userclass + offset + 1 , optarg, strlen (optarg)); - options->userclass[offset] = strlen (optarg); - options->userclass_len += (strlen (optarg)) + 1; - } - break; - case 'x': - sig = SIGTERM; - break; - case 'A': - #ifndef ENABLE_ARP - logger (LOG_ERR, - "arp not compiled into dhcpcd"); - goto abort; - #endif - options->doarp = false; - break; - case 'E': - #ifndef ENABLE_INFO - logger (LOG_ERR, - "info not compiled into dhcpcd"); - goto abort; - #endif - options->dolastlease = true; - break; - case 'F': - if (strncmp (optarg, "none", strlen (optarg)) == 0) - options->fqdn = FQDN_NONE; - else if (strncmp (optarg, "ptr", strlen (optarg)) == 0) - options->fqdn = FQDN_PTR; - else if (strncmp (optarg, "both", strlen (optarg)) == 0) - options->fqdn = FQDN_BOTH; - else { - logger (LOG_ERR, "invalid value `%s' for FQDN", optarg); - goto abort; - } - break; - case 'G': - options->dogateway = false; - break; - case 'H': - options->dohostname++; - break; - case 'I': - if (optarg) { - if (strlen (optarg) > CLIENT_ID_MAX_LEN) { - logger (LOG_ERR, "`%s' is too long for ClientID, max is %d", - optarg, CLIENT_ID_MAX_LEN); - goto abort; - } - if (strlcpy (options->clientid, optarg, - sizeof (options->clientid)) == 0) - /* empty string disabled duid */ - options->doduid = false; - - } else { - memset (options->clientid, 0, sizeof (options->clientid)); - options->doduid = false; - } - break; - case 'L': - options->doipv4ll = false; - break; - case 'M': - options->domtu = false; - break; - case 'N': - options->dontp = false; - break; - case 'R': - options->dodns = false; - break; - case 'S': - options->domscsr++; - break; - case 'T': - #ifndef ENABLE_INFO - logger (LOG_ERR, "info support not compiled into dhcpcd"); - goto abort; - #endif - options->test = true; - options->persistent = true; - break; - case 'Y': - options->donis = false; - break; - case '?': - usage (); - goto abort; - default: - usage (); - goto abort; - } - } - if (doversion) { - printf (""PACKAGE" "VERSION"\n"); - printf ("Compile time options:" - #ifdef ENABLE_ARP - " ARP" - #endif - #ifdef ENABLE_DUID - " DUID" - #endif - #ifdef ENABLE_INFO - " INFO" - #endif - #ifdef ENABLE_INFO_COMPAT - " INFO_COMPAT" - #endif - #ifdef ENABLE_IPV4LL - " IPV4LL" - #endif - #ifdef ENABLE_NIS - " NIS" - #endif - #ifdef ENABLE_NTP - " NTP" - #endif - #ifdef SERVICE - " " SERVICE - #endif - #ifdef ENABLE_RESOLVCONF - " RESOLVCONF" - #endif - #ifdef THERE_IS_NO_FORK - " THERE_IS_NO_FORK" - #endif - "\n"); - } - - if (dohelp) - usage (); - - -#ifdef THERE_IS_NO_FORK - dhcpcd_argv = argv; - dhcpcd_argc = argc; - if (! realpath (argv[0], dhcpcd)) { - logger (LOG_ERR, "unable to resolve the path `%s': %s", - argv[0], strerror (errno)); - goto abort; - } -#endif - - /* initializations for the ipc connection to qt*/ - setSocketName(options->qtsocketaddress); - if (initQtLoggerSocket() < 0) { - logger(LOG_ERR, "initialization Qt Logger failed: %s ", strerror (errno)); - goto abort; - } - - - if (optind < argc) { - if (strlen(argv[optind]) > IF_NAMESIZE) { - logger(LOG_ERR, "`%s' too long for an interface name (max=%d)", - argv[optind], IF_NAMESIZE); - goto abort; - } - strlcpy(options->interface, argv[optind], sizeof(options->interface)); - setInterfaceName(options->interface); - } else { - /* If only version was requested then exit now */ - if (doversion || dohelp) { - retval = 0; - goto abort; - } - - logger(LOG_ERR, "no interface specified"); - setInterfaceName("no_if"); - goto abort; - } - - if (strchr(options->hostname, '.')) { - if (options->fqdn == FQDN_DISABLE) - options->fqdn = FQDN_BOTH; - } else - options->fqdn = FQDN_DISABLE; - - if (options->request_address.s_addr == 0 && options->doinform) { - if ((options->request_address.s_addr = get_address(options->interface)) - != 0) - options->keep_address = true; - } - - if (IN_LINKLOCAL (ntohl (options->request_address.s_addr))) { - logger (LOG_ERR, - "you are not allowed to request a link local address"); - logToQt(LOG_ERR, -1, "you are not allowed to request a link local address"); - goto abort; - } - - if (geteuid ()) { - logger (LOG_WARNING, PACKAGE " will not work correctly unless" - " run as root"); - } - - prefix = xmalloc (sizeof (char) * (IF_NAMESIZE + 3)); - snprintf (prefix, IF_NAMESIZE, "%s: ", options->interface); - setlogprefix (prefix); - snprintf (options->pidfile, sizeof (options->pidfile), PIDFILE, - options->interface); - free (prefix); - - chdir ("/"); - umask (022); - - if (mkdir (INFODIR, S_IRUSR | S_IWUSR |S_IXUSR | S_IRGRP | S_IXGRP - | S_IROTH | S_IXOTH) && errno != EEXIST) - { - logger (LOG_ERR, - "mkdir(\"%s\",0): %s\n", INFODIR, strerror (errno)); - goto abort; - } - - if (mkdir (ETCDIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP - | S_IROTH | S_IXOTH) && errno != EEXIST) - { - logger (LOG_ERR, - "mkdir(\"%s\",0): %s\n", ETCDIR, strerror (errno)); - goto abort; - } - - if (options->test) { - if (options->dorequest || options->doinform) { - logger (LOG_ERR, - "cannot test with --inform or --request"); - goto abort; - } - - if (options->dolastlease) { - logger (LOG_ERR, "cannot test with --lastlease"); - goto abort; - } - - if (sig != 0) { - logger (LOG_ERR, - "cannot test with --release or --renew"); - goto abort; - } - } - - if (sig != 0) { - int killed = -1; - pid = read_pid (options->pidfile); - if (pid != 0) - logger (LOG_INFO, "sending signal %d to pid %d", - sig, pid); - - if (! pid || (killed = kill (pid, sig))) - logger (sig == SIGALRM ? LOG_INFO : LOG_ERR, - ""PACKAGE" not running"); - - if (pid != 0 && (sig != SIGALRM || killed != 0)) - unlink (options->pidfile); - - if (killed == 0) { - retval = EXIT_SUCCESS; - goto abort; - } - - if (sig != SIGALRM) - goto abort; - } - - if (! options->test && ! options->daemonised) { - if ((pid = read_pid (options->pidfile)) > 0 && - kill (pid, 0) == 0) - { - logger (LOG_ERR, ""PACKAGE - " already running on pid %d (%s)", - pid, options->pidfile); - goto abort; - } - - pidfd = open (options->pidfile, - O_WRONLY | O_CREAT | O_NONBLOCK, 0664); - if (pidfd == -1) { - logger (LOG_ERR, "open `%s': %s", - options->pidfile, strerror (errno)); - goto abort; - } - - /* Lock the file so that only one instance of dhcpcd runs - * on an interface */ - if (flock (pidfd, LOCK_EX | LOCK_NB) == -1) { - logger (LOG_ERR, "flock `%s': %s", - options->pidfile, strerror (errno)); - goto abort; - } - - /* dhcpcd.sh should not interhit this fd */ - if ((i = fcntl (pidfd, F_GETFD, 0)) == -1 || - fcntl (pidfd, F_SETFD, i | FD_CLOEXEC) == -1) - logger (LOG_ERR, "fcntl: %s", strerror (errno)); - - writepid (pidfd, getpid ()); - logger (LOG_INFO, PACKAGE " " VERSION " starting"); - } - - /* Seed random */ - srandomdev (); - - /* Massage our filters per platform */ - setup_packet_filters (); - - /* dhcp_run : defined in client.c */ - if (dhcp_run (options, &pidfd) == 0) - retval = EXIT_SUCCESS; - -abort: - /* If we didn't daemonise then we need to punt the pidfile now */ - if (pidfd > -1) { - close (pidfd); - unlink (options->pidfile); - } - - free (options); - -#ifdef THERE_IS_NO_FORK - /* There may have been an error before the dhcp_run function - * clears this, so just do it here to be safe */ - free (dhcpcd_skiproutes); -#endif - - logger (LOG_INFO, "exiting"); - logToQt(LOG_INFO, DHCPCD_EXIT, "exiting due abort"); - closeQtLoggerSocket(); - exit (retval); - /* NOTREACHED */ -} diff --git a/workspace/customdhcpcd/src/dhcpcd.h b/workspace/customdhcpcd/src/dhcpcd.h deleted file mode 100644 index 7deb5b7..0000000 --- a/workspace/customdhcpcd/src/dhcpcd.h +++ /dev/null @@ -1,105 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef DHCPCD_H -#define DHCPCD_H - -#include -#include -#include -#include -#include -#include - -#include "common.h" - -#define DEFAULT_TIMEOUT 20 -#define DEFAULT_LEASETIME 3600 /* 1 hour */ - -/* added by Niklas Goby, additional field, storing the socket address path for - * communicating with Qt "server" */ -#define DEFAULT_QTSOCKETADDRESS "/var/tmp/qt_c_socket_default" -#define QTSOCKETADDRESSLENGTH 255 - -#define CLASS_ID_MAX_LEN 48 -#define CLIENT_ID_MAX_LEN 48 -#define USERCLASS_MAX_LEN 255 - -#ifdef THERE_IS_NO_FORK -extern char dhcpcd[PATH_MAX]; -extern char **dhcpcd_argv; -extern int dhcpcd_argc; -extern char *dhcpcd_skiproutes; -#endif - -typedef struct options_t { - /* added by Niklas Goby, additional field, storing the socket address path for - * communicating with Qt "server" */ - char qtsocketaddress[QTSOCKETADDRESSLENGTH]; - /*----*/ - char interface[IF_NAMESIZE]; - char hostname[MAXHOSTNAMELEN]; - int fqdn; - char classid[CLASS_ID_MAX_LEN]; - char clientid[CLIENT_ID_MAX_LEN]; - char userclass[USERCLASS_MAX_LEN]; - size_t userclass_len; - uint32_t leasetime; - time_t timeout; - int metric; - - bool doarp; - bool dodns; - bool dodomainname; - bool dogateway; - int dohostname; - bool domtu; - bool donis; - bool dontp; - bool dolastlease; - bool doinform; - bool dorequest; - bool doipv4ll; - bool doduid; - int domscsr; - - struct in_addr request_address; - struct in_addr request_netmask; - - bool persistent; - bool keep_address; - bool daemonise; - bool daemonised; - bool test; - - char *script; - char pidfile[PATH_MAX]; -} options_t; - -int nd_main (char *ifname); - -#endif diff --git a/workspace/customdhcpcd/src/dhcpcd.sh b/workspace/customdhcpcd/src/dhcpcd.sh deleted file mode 100644 index 8c86aac..0000000 --- a/workspace/customdhcpcd/src/dhcpcd.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/sh -# -# This is a sample /etc/dhcpcd.sh script. -# /etc/dhcpcd.sh script is executed by dhcpcd daemon -# any time it configures or shuts down interface. -# The following parameters are passed to dhcpcd.exe script: -# $1 = HostInfoFilePath, e.g "/var/lib/dhcpcd/dhcpcd-eth0.info" -# $2 = "up" if interface has been configured with the same -# IP address as before reboot; -# $2 = "down" if interface has been shut down; -# $2 = "new" if interface has been configured with new IP address; -# -# Sanity checks - -if [ $# -lt 2 ]; then - logger -s -p local0.err -t dhcpcd.sh "wrong usage" - exit 1 -fi - -hostinfo="$1" -state="$2" - -# Reading HostInfo file for configuration parameters -[ -e "${hostinfo}" ] && . "${hostinfo}" - -case "${state}" in - up) - logger -s -p local0.info -t dhcpcd.sh \ - "interface ${INTERFACE} has been configured with old IP=${IPADDR}" - # Put your code here for when the interface has been brought up with an - # old IP address here - ;; - - new) - logger -s -p local0.info -t dhcpcd.sh \ - "interface ${INTERFACE} has been configured with new IP=${IPADDR}" - # Put your code here for when the interface has been brought up with a - # new IP address - ;; - - down) logger -s -p local0.info -t dhcpcd.sh \ - "interface ${INTERFACE} has been brought down" - # Put your code here for the when the interface has been shut down - ;; -esac -exit 0 diff --git a/workspace/customdhcpcd/src/discover.c b/workspace/customdhcpcd/src/discover.c deleted file mode 100644 index eae7b0a..0000000 --- a/workspace/customdhcpcd/src/discover.c +++ /dev/null @@ -1,7 +0,0 @@ -/* - * discover.c - * - * Created on: Jul 7, 2011 - * Author: niklas - */ - diff --git a/workspace/customdhcpcd/src/discover.h b/workspace/customdhcpcd/src/discover.h deleted file mode 100644 index 1f4918d..0000000 --- a/workspace/customdhcpcd/src/discover.h +++ /dev/null @@ -1,12 +0,0 @@ -/* - * discover.h - * - * Created on: Jul 7, 2011 - * Author: niklas - */ - -#ifndef DISCOVER_H_ -#define DISCOVER_H_ - - -#endif /* DISCOVER_H_ */ diff --git a/workspace/customdhcpcd/src/duid.c b/workspace/customdhcpcd/src/duid.c deleted file mode 100644 index e4dd83b..0000000 --- a/workspace/customdhcpcd/src/duid.c +++ /dev/null @@ -1,118 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "common.h" -#include "duid.h" -#include "logger.h" - -#ifdef ENABLE_DUID - -#define THIRTY_YEARS_IN_SECONDS 946707779 - -size_t get_duid (unsigned char *duid, const interface_t *iface) -{ - FILE *f; - uint16_t type = 0; - uint16_t hw = 0; - uint32_t ul; - time_t t; - int x = 0; - unsigned char *p = duid; - size_t len = 0; - - if (! iface) - return (0); - - /* If we already have a DUID then use it as it's never supposed - * to change once we have one even if the interfaces do */ - if ((f = fopen (DUIDFILE, "r"))) { - char *line = get_line (f); - if (line) { - len = hwaddr_aton (NULL, line); - if (len && len <= DUID_LEN) - hwaddr_aton (duid, line); - free (line); - } - fclose (f); - if (len) - return (len); - } else { - if (errno != ENOENT) { - logger (LOG_ERR, "fopen `%s': %s", - DUIDFILE, strerror (errno)); - return (0); - } - } - - /* No file? OK, lets make one based on our interface */ - type = htons (1); /* DUI-D-LLT */ - memcpy (p, &type, 2); - p += 2; - - hw = htons (iface->family); - memcpy (p, &hw, 2); - p += 2; - - /* time returns seconds from jan 1 1970, but DUID-LLT is - * seconds from jan 1 2000 modulo 2^32 */ - t = time (NULL) - THIRTY_YEARS_IN_SECONDS; - ul = htonl (t & 0xffffffff); - memcpy (p, &ul, 4); - p += 4; - - /* Finally, add the MAC address of the interface */ - memcpy (p, iface->hwaddr, iface->hwlen); - p += iface->hwlen; - - len = p - duid; - - if (! (f = fopen (DUIDFILE, "w"))) - logger (LOG_ERR, "fopen `%s': %s", DUIDFILE, strerror (errno)); - else { - x = fprintf (f, "%s\n", hwaddr_ntoa (duid, len)); - fclose (f); - } - - /* Failed to write the duid? scrub it, we cannot use it */ - if (x < 1) { - len = 0; - unlink (DUIDFILE); - } - - return (len); -} -#endif diff --git a/workspace/customdhcpcd/src/duid.h b/workspace/customdhcpcd/src/duid.h deleted file mode 100644 index 1492990..0000000 --- a/workspace/customdhcpcd/src/duid.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef DUID_H -#define DUID_H - -#include "config.h" - -#ifdef ENABLE_DUID -#ifndef DUID_LEN -# define DUID_LEN 128 + 2 -#endif - -#include "interface.h" - -size_t get_duid (unsigned char *duid, const interface_t *iface); -#endif -#endif diff --git a/workspace/customdhcpcd/src/info.c b/workspace/customdhcpcd/src/info.c deleted file mode 100644 index 8369b43..0000000 --- a/workspace/customdhcpcd/src/info.c +++ /dev/null @@ -1,472 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - -#include - -#include -#include -#include -#include -#include - -#include "config.h" -#include "common.h" -#include "dhcp.h" -#include "interface.h" -#include "logger.h" -#include "info.h" - -#ifdef ENABLE_INFO - -/* Create a malloced string of cstr, changing ' to '\'' - * so the contents work in a shell */ -static char *cleanmetas (const char *cstr) -{ - const char *p = cstr; - char *new; - char *n; - size_t len; - - if (cstr == NULL || (len = strlen (cstr)) == 0) - return (xstrdup ("")); - - n = new = xmalloc (sizeof (char) * len + 2); - do - if (*p == '\'') { - size_t pos = n - new; - len += 4; - new = xrealloc (new, sizeof (char) * len + 1); - n = new + pos; - *n++ = '\''; - *n++ = '\\'; - *n++ = '\''; - *n++ = '\''; - } else - *n++ = *p; - while (*p++); - - /* Terminate the sucker */ - *n = '\0'; - - return (new); -} - - -static void print_addresses (FILE *f, const struct address_head *addresses) -{ - const address_t *addr; - - STAILQ_FOREACH (addr, addresses, entries) { - fprintf (f, "%s", inet_ntoa (addr->address)); - if (STAILQ_NEXT (addr, entries)) - fprintf (f, " "); - } -} - -static void print_clean (FILE *f, const char *name, const char *value) -{ - char *clean; - - if (! value) - return; - - clean = cleanmetas (value); - fprintf (f, "%s='%s'\n", name, clean); - free (clean); -} - -bool write_info(const interface_t *iface, const dhcp_t *dhcp, - const options_t *options, bool overwrite) -{ - FILE *f; - route_t *route; - struct stat sb; - - if (options->test) - f = stdout; - else { - if (! overwrite && stat (iface->infofile, &sb) == 0) - return (true); - - logger (LOG_DEBUG, "writing %s", iface->infofile); - if ((f = fopen (iface->infofile, "w")) == NULL) { - logger (LOG_ERR, "fopen `%s': %s", - iface->infofile, strerror (errno)); - return (false); - } - } - - if (dhcp->address.s_addr) { - struct in_addr n; - n.s_addr = dhcp->address.s_addr & dhcp->netmask.s_addr; - fprintf (f, "IPADDR='%s'\n", inet_ntoa (dhcp->address)); - fprintf (f, "NETMASK='%s'\n", inet_ntoa (dhcp->netmask)); - fprintf (f, "NETWORK='%s'\n", inet_ntoa (n)); - fprintf (f, "BROADCAST='%s'\n", inet_ntoa (dhcp->broadcast)); - } - if (dhcp->mtu > 0) - fprintf (f, "MTU='%d'\n", dhcp->mtu); - - if (dhcp->routes) { - bool doneone = false; - fprintf (f, "ROUTES='"); - STAILQ_FOREACH (route, dhcp->routes, entries) { - if (route->destination.s_addr != 0) { - if (doneone) - fprintf (f, " "); - fprintf (f, "%s", inet_ntoa (route->destination)); - fprintf (f, ",%s", inet_ntoa (route->netmask)); - fprintf (f, ",%s", inet_ntoa (route->gateway)); - doneone = true; - } - } - fprintf (f, "'\n"); - - doneone = false; - fprintf (f, "GATEWAYS='"); - STAILQ_FOREACH (route, dhcp->routes, entries) { - if (route->destination.s_addr == 0) { - if (doneone) - fprintf (f, " "); - fprintf (f, "%s", inet_ntoa (route->gateway)); - doneone = true; - } - } - fprintf (f, "'\n"); - } - - print_clean (f, "HOSTNAME", dhcp->hostname); - print_clean (f, "DNSDOMAIN", dhcp->dnsdomain); - print_clean (f, "DNSSEARCH", dhcp->dnssearch); - - if (dhcp->dnsservers) { - fprintf (f, "DNSSERVERS='"); - print_addresses (f, dhcp->dnsservers); - fprintf (f, "'\n"); - } - - if (dhcp->fqdn) { - fprintf (f, "FQDNFLAGS='%u'\n", dhcp->fqdn->flags); - fprintf (f, "FQDNRCODE1='%u'\n", dhcp->fqdn->r1); - fprintf (f, "FQDNRCODE2='%u'\n", dhcp->fqdn->r2); - print_clean (f, "FQDNHOSTNAME", dhcp->fqdn->name); - } - - if (dhcp->ntpservers) { - fprintf (f, "NTPSERVERS='"); - print_addresses (f, dhcp->ntpservers); - fprintf (f, "'\n"); - } - - print_clean (f, "NISDOMAIN", dhcp->nisdomain); - if (dhcp->nisservers) { - fprintf (f, "NISSERVERS='"); - print_addresses (f, dhcp->nisservers); - fprintf (f, "'\n"); - } - - print_clean (f, "ROOTPATH", dhcp->rootpath); - print_clean (f, "SIPSERVERS", dhcp->sipservers); - - if (dhcp->serveraddress.s_addr) - fprintf (f, "DHCPSID='%s'\n", inet_ntoa (dhcp->serveraddress)); - if (dhcp->servername[0]) - print_clean (f, "DHCPSNAME", dhcp->servername); - - if (! options->doinform && dhcp->address.s_addr) { - if (! options->test) - fprintf (f, "LEASEDFROM='%u'\n", dhcp->leasedfrom); - fprintf (f, "LEASETIME='%u'\n", dhcp->leasetime); - fprintf (f, "RENEWALTIME='%u'\n", dhcp->renewaltime); - fprintf (f, "REBINDTIME='%u'\n", dhcp->rebindtime); - } - print_clean (f, "INTERFACE", iface->name); - print_clean (f, "CLASSID", options->classid); - if (iface->clientid_len > 0) { - fprintf (f, "CLIENTID='%s'\n", - hwaddr_ntoa (iface->clientid, iface->clientid_len)); - } - fprintf (f, "DHCPCHADDR='%s'\n", hwaddr_ntoa (iface->hwaddr, - iface->hwlen)); - -#ifdef ENABLE_INFO_COMPAT - /* Support the old .info settings if we need to */ - fprintf (f, "\n# dhcpcd-1.x and 2.x compatible variables\n"); - if (dhcp->dnsservers) { - address_t *addr; - - fprintf (f, "DNS='"); - STAILQ_FOREACH (addr, dhcp->dnsservers, entries) { - fprintf (f, "%s", inet_ntoa (addr->address)); - if (STAILQ_NEXT (addr, entries)) - fprintf (f, ","); - } - fprintf (f, "'\n"); - } - - if (dhcp->routes) { - bool doneone = false; - fprintf (f, "GATEWAY='"); - STAILQ_FOREACH (route, dhcp->routes, entries) { - if (route->destination.s_addr == 0) { - if (doneone) - fprintf (f, ","); - fprintf (f, "%s", inet_ntoa (route->gateway)); - doneone = true; - } - } - fprintf (f, "'\n"); - } -#endif - - if (! options->test) - fclose (f); - return (true); -} - -static bool parse_address (struct in_addr *addr, - const char *value, const char *var) -{ - if (inet_aton (value, addr) == 0) { - logger (LOG_ERR, "%s `%s': %s", var, value, - strerror (errno)); - return (false); - } - return (true); -} - -static bool parse_uint (unsigned int *i, - const char *value, const char *var) -{ - if (sscanf (value, "%u", i) != 1) { - logger (LOG_ERR, "%s `%s': not a valid number", - var, value); - return (false); - } - return (true); -} - -static bool parse_ushort (unsigned short *s, - const char *value, const char *var) -{ - if (sscanf (value, "%hu", s) != 1) { - logger (LOG_ERR, "%s `%s': not a valid number", - var, value); - return (false); - } - return (true); -} - -static struct address_head *parse_addresses (char *value, const char *var) -{ - char *token; - char *p = value; - struct address_head *head = NULL; - - while ((token = strsep (&p, " "))) { - address_t *a = xzalloc (sizeof (*a)); - - if (inet_aton (token, &a->address) == 0) { - logger (LOG_ERR, "%s: invalid address `%s'", var, token); - free_address (head); - free (a); - return (NULL); - } - - if (! head) { - head = xmalloc (sizeof (*head)); - STAILQ_INIT (head); - } - STAILQ_INSERT_TAIL (head, a, entries); - } - - return (head); -} - -bool read_info (const interface_t *iface, dhcp_t *dhcp) -{ - FILE *fp; - char *line; - char *var; - char *value; - char *p; - struct stat sb; - - if (stat (iface->infofile, &sb) != 0) { - logger (LOG_ERR, "lease information file `%s' does not exist", - iface->infofile); - return (false); - } - - if (! (fp = fopen (iface->infofile, "r"))) { - logger (LOG_ERR, "fopen `%s': %s", - iface->infofile, strerror (errno)); - return (false); - } - - dhcp->frominfo = true; - - while ((line = get_line (fp))) { - var = line; - - /* Strip leading spaces/tabs */ - while ((*var == ' ') || (*var == '\t')) - var++; - - /* Trim trailing \n */ - p = var + strlen (var) - 1; - if (*p == '\n') - *p = 0; - - /* Skip comments */ - if (*var == '#') - goto next; - - /* If we don't have an equals sign then skip it */ - if (! (p = strchr (var, '='))) - goto next; - - /* Terminate the = so we have two strings */ - *p = 0; - - value = p + 1; - /* Strip leading and trailing quotes if present */ - if (*value == '\'' || *value == '"') - value++; - p = value + strlen (value) - 1; - if (*p == '\'' || *p == '"') - *p = 0; - - /* Don't process null vars or values */ - if (! *var || ! *value) - goto next; - - if (strcmp (var, "IPADDR") == 0) - parse_address (&dhcp->address, value, "IPADDR"); - else if (strcmp (var, "NETMASK") == 0) - parse_address (&dhcp->netmask, value, "NETMASK"); - else if (strcmp (var, "BROADCAST") == 0) - parse_address (&dhcp->broadcast, value, "BROADCAST"); - else if (strcmp (var, "MTU") == 0) - parse_ushort (&dhcp->mtu, value, "MTU"); - else if (strcmp (var, "ROUTES") == 0) { - p = value; - while ((value = strsep (&p, " "))) { - char *pp = value; - char *dest = strsep (&pp, ","); - char *net = strsep (&pp, ","); - char *gate = strsep (&pp, ","); - route_t *route; - - if (! dest || ! net || ! gate) { - logger (LOG_ERR, "read_info ROUTES `%s,%s,%s': invalid route", - dest, net, gate); - goto next; - } - - /* See if we can create a route */ - route = xzalloc (sizeof (*route)); - if (inet_aton (dest, &route->destination) == 0) { - logger (LOG_ERR, "read_info ROUTES `%s': not a valid destination address", - dest); - free (route); - goto next; - } - if (inet_aton (dest, &route->netmask) == 0) { - logger (LOG_ERR, "read_info ROUTES `%s': not a valid netmask address", - net); - free (route); - goto next; - } - if (inet_aton (dest, &route->gateway) == 0) { - logger (LOG_ERR, "read_info ROUTES `%s': not a valid gateway address", - gate); - free (route); - goto next; - } - - /* OK, now add our route */ - if (! dhcp->routes) { - dhcp->routes = xmalloc (sizeof (*dhcp->routes)); - STAILQ_INIT (dhcp->routes); - } - STAILQ_INSERT_TAIL (dhcp->routes, route, entries); - } - } else if (strcmp (var, "GATEWAYS") == 0) { - p = value; - while ((value = strsep (&p, " "))) { - route_t *route = xzalloc (sizeof (*route)); - if (parse_address (&route->gateway, value, "GATEWAYS")) { - if (! dhcp->routes) { - dhcp->routes = xmalloc (sizeof (*dhcp->routes)); - STAILQ_INIT (dhcp->routes); - } - STAILQ_INSERT_TAIL (dhcp->routes, route, entries); - } else - free (route); - } - } else if (strcmp (var, "HOSTNAME") == 0) - dhcp->hostname = xstrdup (value); - else if (strcmp (var, "DNSDOMAIN") == 0) - dhcp->dnsdomain = xstrdup (value); - else if (strcmp (var, "DNSSEARCH") == 0) - dhcp->dnssearch = xstrdup (value); - else if (strcmp (var, "DNSSERVERS") == 0) - dhcp->dnsservers = parse_addresses (value, "DNSSERVERS"); - else if (strcmp (var, "NTPSERVERS") == 0) - dhcp->ntpservers = parse_addresses (value, "NTPSERVERS"); - else if (strcmp (var, "NISDOMAIN") == 0) - dhcp->nisdomain = xstrdup (value); - else if (strcmp (var, "NISSERVERS") == 0) - dhcp->nisservers = parse_addresses (value, "NISSERVERS"); - else if (strcmp (var, "ROOTPATH") == 0) - dhcp->rootpath = xstrdup (value); - else if (strcmp (var, "DHCPSID") == 0) - parse_address (&dhcp->serveraddress, value, "DHCPSID"); - else if (strcmp (var, "DHCPSNAME") == 0) - strlcpy (dhcp->servername, value, sizeof (dhcp->servername)); - else if (strcmp (var, "LEASEDFROM") == 0) - parse_uint (&dhcp->leasedfrom, value, "LEASEDFROM"); - else if (strcmp (var, "LEASETIME") == 0) - parse_uint (&dhcp->leasetime, value, "LEASETIME"); - else if (strcmp (var, "RENEWALTIME") == 0) - parse_uint (&dhcp->renewaltime, value, "RENEWALTIME"); - else if (strcmp (var, "REBINDTIME") == 0) - parse_uint (&dhcp->rebindtime, value, "REBINDTIME"); - -next: - free (line); - } - - fclose (fp); - return (true); -} - -#endif - diff --git a/workspace/customdhcpcd/src/info.h b/workspace/customdhcpcd/src/info.h deleted file mode 100644 index 22966db..0000000 --- a/workspace/customdhcpcd/src/info.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef INFO_H -#define INFO_H - -#include "dhcpcd.h" -#include "interface.h" -#include "dhcp.h" - -#ifdef ENABLE_INFO -bool write_info (const interface_t *iface, const dhcp_t *dhcp, - const options_t *options, bool overwrite); - -bool read_info (const interface_t *iface, dhcp_t *dhcp); -#endif - -#endif diff --git a/workspace/customdhcpcd/src/interface.c b/workspace/customdhcpcd/src/interface.c deleted file mode 100644 index d2ff8d6..0000000 --- a/workspace/customdhcpcd/src/interface.c +++ /dev/null @@ -1,1060 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include - -#include - -/* Netlink suff */ -#ifdef __linux__ -#include /* Needed for 2.4 kernels */ -#include -#include -#include -#include -#else -#include -#include -#include -#include -#endif - -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "common.h" -#include "dhcp.h" -#include "interface.h" -#include "logger.h" - -void free_address (struct address_head *addresses) -{ - address_t *p; - address_t *n; - - if (! addresses) - return; - - p = STAILQ_FIRST (addresses); - while (p) { - n = STAILQ_NEXT (p, entries); - free (p); - p = n; - } - free (addresses); -} - -void free_route (struct route_head *routes) -{ - route_t *p; - route_t *n; - - if (! routes) - return; - - p = STAILQ_FIRST (routes); - while (p) { - n = STAILQ_NEXT (p, entries); - free (p); - p = n; - } - free (routes); -} - -int inet_ntocidr (struct in_addr address) -{ - int cidr = 0; - uint32_t mask = htonl (address.s_addr); - - while (mask) { - cidr++; - mask <<= 1; - } - - return (cidr); -} - -int inet_cidrtoaddr (int cidr, struct in_addr *addr) { - int ocets; - - if (cidr < 0 || cidr > 32) { - errno = EINVAL; - return (-1); - } - ocets = (cidr + 7) / 8; - - memset (addr, 0, sizeof (*addr)); - if (ocets > 0) { - memset (&addr->s_addr, 255, (size_t) ocets - 1); - memset ((unsigned char *) &addr->s_addr + (ocets - 1), - (256 - (1 << (32 - cidr) % 8)), 1); - } - - return (0); -} - -uint32_t get_netmask (uint32_t addr) -{ - uint32_t dst; - - if (addr == 0) - return (0); - - dst = htonl (addr); - if (IN_CLASSA (dst)) - return (ntohl (IN_CLASSA_NET)); - if (IN_CLASSB (dst)) - return (ntohl (IN_CLASSB_NET)); - if (IN_CLASSC (dst)) - return (ntohl (IN_CLASSC_NET)); - - return (0); -} - -char *hwaddr_ntoa (const unsigned char *hwaddr, size_t hwlen) -{ - static char buffer[(HWADDR_LEN * 3) + 1]; - char *p = buffer; - size_t i; - - for (i = 0; i < hwlen && i < HWADDR_LEN; i++) { - if (i > 0) - *p ++= ':'; - p += snprintf (p, 3, "%.2x", hwaddr[i]); - } - - *p ++= '\0'; - - return (buffer); -} - -size_t hwaddr_aton (unsigned char *buffer, const char *addr) -{ - char c[3]; - const char *p = addr; - unsigned char *bp = buffer; - size_t len = 0; - - c[2] = '\0'; - while (*p) { - c[0] = *p++; - c[1] = *p++; - /* Ensure that next data is EOL or a seperator with data */ - if (! (*p == '\0' || (*p == ':' && *(p + 1) != '\0'))) { - errno = EINVAL; - return (0); - } - /* Ensure that digits are hex */ - if (isxdigit ((int) c[0]) == 0 || isxdigit ((int) c[1]) == 0) { - errno = EINVAL; - return (0); - } - p++; - if (bp) - *bp++ = (unsigned char) strtol (c, NULL, 16); - else - len++; - } - - if (bp) - return (bp - buffer); - return (len); -} - -static int _do_interface (const char *ifname, - _unused unsigned char *hwaddr, _unused size_t *hwlen, - struct in_addr *addr, - bool flush, bool get) -{ - int s; - struct ifconf ifc; - int retval = 0; - int len = 10 * sizeof (struct ifreq); - int lastlen = 0; - char *p; - - if ((s = socket (AF_INET, SOCK_DGRAM, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return -1; - } - - /* Not all implementations return the needed buffer size for - * SIOGIFCONF so we loop like so for all until it works */ - memset (&ifc, 0, sizeof (ifc)); - for (;;) { - ifc.ifc_len = len; - ifc.ifc_buf = xmalloc ((size_t) len); - if (ioctl (s, SIOCGIFCONF, &ifc) == -1) { - if (errno != EINVAL || lastlen != 0) { - logger (LOG_ERR, "ioctl SIOCGIFCONF: %s", - strerror (errno)); - close (s); - free (ifc.ifc_buf); - return -1; - } - } else { - if (ifc.ifc_len == lastlen) - break; - lastlen = ifc.ifc_len; - } - - free (ifc.ifc_buf); - ifc.ifc_buf = NULL; - len *= 2; - } - - for (p = ifc.ifc_buf; p < ifc.ifc_buf + ifc.ifc_len;) { - union { - char *buffer; - struct ifreq *ifr; - } ifreqs; - struct sockaddr_in address; - struct ifreq *ifr; - - /* Cast the ifc buffer to an ifreq cleanly */ - ifreqs.buffer = p; - ifr = ifreqs.ifr; - -#ifdef __linux__ - p += sizeof (*ifr); -#else - p += offsetof (struct ifreq, ifr_ifru) + ifr->ifr_addr.sa_len; -#endif - - if (strcmp (ifname, ifr->ifr_name) != 0) - continue; - -#ifdef AF_LINK - if (hwaddr && hwlen && ifr->ifr_addr.sa_family == AF_LINK) { - struct sockaddr_dl sdl; - - memcpy (&sdl, &ifr->ifr_addr, sizeof (sdl)); - *hwlen = sdl.sdl_alen; - memcpy (hwaddr, sdl.sdl_data + sdl.sdl_nlen, - (size_t) sdl.sdl_alen); - retval = 1; - break; - } -#endif - - if (ifr->ifr_addr.sa_family == AF_INET) { - memcpy (&address, &ifr->ifr_addr, sizeof (address)); - if (flush) { - struct sockaddr_in netmask; - - if (ioctl (s, SIOCGIFNETMASK, ifr) == -1) { - logger (LOG_ERR, - "ioctl SIOCGIFNETMASK: %s", - strerror (errno)); - continue; - } - memcpy (&netmask, &ifr->ifr_addr, - sizeof (netmask)); - - if (del_address (ifname, - address.sin_addr, - netmask.sin_addr) == -1) - retval = -1; - } else if (get) { - addr->s_addr = address.sin_addr.s_addr; - retval = 1; - break; - } else if (address.sin_addr.s_addr == addr->s_addr) { - retval = 1; - break; - } - } - - } - - close (s); - free (ifc.ifc_buf); - return retval; -} - -interface_t *read_interface (const char *ifname, _unused int metric) -{ - int s; - struct ifreq ifr; - interface_t *iface = NULL; - unsigned char *hwaddr = NULL; - size_t hwlen = 0; - sa_family_t family = 0; - unsigned short mtu; -#ifdef __linux__ - char *p; -#endif - - if (! ifname) - return NULL; - - memset (&ifr, 0, sizeof (ifr)); - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - - if ((s = socket (AF_INET, SOCK_DGRAM, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return NULL; - } - -#ifdef __linux__ - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - if (ioctl (s, SIOCGIFHWADDR, &ifr) == -1) { - logger (LOG_ERR, "ioctl SIOCGIFHWADDR: %s", strerror (errno)); - goto exit; - } - - switch (ifr.ifr_hwaddr.sa_family) { - case ARPHRD_ETHER: - case ARPHRD_IEEE802: - hwlen = ETHER_ADDR_LEN; - break; - case ARPHRD_IEEE1394: - hwlen = EUI64_ADDR_LEN; - case ARPHRD_INFINIBAND: - hwlen = INFINIBAND_ADDR_LEN; - break; - default: - logger (LOG_ERR, - "interface is not Ethernet, FireWire, " \ - "InfiniBand or Token Ring"); - goto exit; - } - - hwaddr = xmalloc (sizeof (unsigned char) * HWADDR_LEN); - memcpy (hwaddr, ifr.ifr_hwaddr.sa_data, hwlen); - family = ifr.ifr_hwaddr.sa_family; -#else - ifr.ifr_metric = metric; - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - if (ioctl (s, SIOCSIFMETRIC, &ifr) == -1) { - logger (LOG_ERR, "ioctl SIOCSIFMETRIC: %s", strerror (errno)); - goto exit; - } - - hwaddr = xmalloc (sizeof (unsigned char) * HWADDR_LEN); - if (_do_interface (ifname, hwaddr, &hwlen, NULL, false, false) != 1) { - logger (LOG_ERR, "could not find interface %s", ifname); - goto exit; - } - - family = ARPHRD_ETHER; -#endif - - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - if (ioctl (s, SIOCGIFMTU, &ifr) == -1) { - logger (LOG_ERR, "ioctl SIOCGIFMTU: %s", strerror (errno)); - goto exit; - } - - if (ifr.ifr_mtu < MTU_MIN) { - logger (LOG_DEBUG, "MTU of %d is too low, setting to %d", - ifr.ifr_mtu, MTU_MIN); - ifr.ifr_mtu = MTU_MIN; - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - if (ioctl (s, SIOCSIFMTU, &ifr) == -1) { - logger (LOG_ERR, "ioctl SIOCSIFMTU,: %s", - strerror (errno)); - goto exit; - } - } - mtu = ifr.ifr_mtu; - - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); -#ifdef __linux__ - /* We can only bring the real interface up */ - if ((p = strchr (ifr.ifr_name, ':'))) - *p = '\0'; -#endif - if (ioctl (s, SIOCGIFFLAGS, &ifr) == -1) { - logger (LOG_ERR, "ioctl SIOCGIFFLAGS: %s", strerror (errno)); - goto exit; - } - - if (! (ifr.ifr_flags & IFF_UP) || ! (ifr.ifr_flags & IFF_RUNNING)) { - ifr.ifr_flags |= IFF_UP | IFF_RUNNING; - if (ioctl (s, SIOCSIFFLAGS, &ifr) != 0) { - logger (LOG_ERR, "ioctl SIOCSIFFLAGS: %s", - strerror (errno)); - goto exit; - } - } - - iface = xzalloc (sizeof (*iface)); - strlcpy (iface->name, ifname, IF_NAMESIZE); -#ifdef ENABLE_INFO - snprintf (iface->infofile, PATH_MAX, INFOFILE, ifname); -#endif - memcpy (&iface->hwaddr, hwaddr, hwlen); - iface->hwlen = hwlen; - - iface->family = family; - iface->arpable = ! (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)); - iface->mtu = iface->previous_mtu = mtu; - - logger (LOG_INFO, "hardware address = %s", - hwaddr_ntoa (iface->hwaddr, iface->hwlen)); - - /* 0 is a valid fd, so init to -1 */ - iface->fd = -1; -#ifdef __linux__ - iface->listen_fd = -1; -#endif - -exit: - close (s); - free (hwaddr); - return iface; -} - -int get_mtu (const char *ifname) -{ - struct ifreq ifr; - int r; - int s; - - if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return (-1); - } - - memset (&ifr, 0, sizeof (ifr)); - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - r = ioctl (s, SIOCGIFMTU, &ifr); - close (s); - - if (r == -1) { - logger (LOG_ERR, "ioctl SIOCGIFMTU: %s", strerror (errno)); - return (-1); - } - - return (ifr.ifr_mtu); -} - -int set_mtu (const char *ifname, short int mtu) -{ - struct ifreq ifr; - int r; - int s; - - if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return (-1); - } - - memset (&ifr, 0, sizeof (ifr)); - logger (LOG_DEBUG, "setting MTU to %d", mtu); - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - ifr.ifr_mtu = mtu; - r = ioctl (s, SIOCSIFMTU, &ifr); - close (s); - - if (r == -1) - logger (LOG_ERR, "ioctl SIOCSIFMTU: %s", strerror (errno)); - - return (r == 0 ? 0 : -1); -} - -static void log_route (struct in_addr destination, - struct in_addr netmask, - struct in_addr gateway, - _unused int metric, - int change, int del) -{ - char *dstd = xstrdup (inet_ntoa (destination)); - -#ifdef __linux__ -#define METRIC " metric %d" -#else -#define METRIC "" -#endif - - if (gateway.s_addr == destination.s_addr || - gateway.s_addr == INADDR_ANY) - logger (LOG_INFO, "%s route to %s/%d" METRIC, - change ? "changing" : del ? "removing" : "adding", - dstd, inet_ntocidr (netmask) -#ifdef __linux__ - , metric -#endif - ); - else if (destination.s_addr == INADDR_ANY) - logger (LOG_INFO, "%s default route via %s" METRIC, - change ? "changing" : del ? "removing" : "adding", - inet_ntoa (gateway) - -#ifdef __linux__ - , metric -#endif - ); - else - logger (LOG_INFO, "%s route to %s/%d via %s" METRIC, - change ? "changing" : del ? "removing" : "adding", - dstd, inet_ntocidr (netmask), inet_ntoa (gateway) -#ifdef __linux__ - , metric -#endif - ); - - free (dstd); -} - -#if defined(BSD) || defined(__FreeBSD_kernel__) - -/* Darwin doesn't define this for some very odd reason */ -#ifndef SA_SIZE -# define SA_SIZE(sa) \ - ( (!(sa) || ((struct sockaddr *)(sa))->sa_len == 0) ? \ - sizeof(long) : \ - 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(long) - 1) ) ) -#endif - -static int do_address (const char *ifname, struct in_addr address, - struct in_addr netmask, struct in_addr broadcast, - int del) -{ - int s; - struct ifaliasreq ifa; - - if (! ifname) - return -1; - - if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return -1; - } - - memset (&ifa, 0, sizeof (ifa)); - strlcpy (ifa.ifra_name, ifname, sizeof (ifa.ifra_name)); - -#define ADDADDR(_var, _addr) { \ - union { struct sockaddr *sa; struct sockaddr_in *sin; } _s; \ - _s.sa = &_var; \ - _s.sin->sin_family = AF_INET; \ - _s.sin->sin_len = sizeof (*_s.sin); \ - memcpy (&_s.sin->sin_addr, &_addr, sizeof (_s.sin->sin_addr)); \ -} - - ADDADDR (ifa.ifra_addr, address); - ADDADDR (ifa.ifra_mask, netmask); -if (! del) - ADDADDR (ifa.ifra_broadaddr, broadcast); - -#undef ADDADDR - - if (ioctl (s, del ? SIOCDIFADDR : SIOCAIFADDR, &ifa) == -1) { - logger (LOG_ERR, "ioctl %s: %s", - del ? "SIOCDIFADDR" : "SIOCAIFADDR", - strerror (errno)); - close (s); - return -1; - } - -close (s); -return 0; -} - -static int do_route (const char *ifname, - struct in_addr destination, - struct in_addr netmask, - struct in_addr gateway, - int metric, - int change, int del) -{ - int s; - static int seq; - union sockunion { - struct sockaddr sa; - struct sockaddr_in sin; -#ifdef INET6 - struct sockaddr_in6 sin6; -#endif - struct sockaddr_dl sdl; - struct sockaddr_storage ss; - } su; - struct rtm - { - struct rt_msghdr hdr; - char buffer[sizeof (su) * 3]; - } rtm; - char *bp = rtm.buffer; - size_t l; - - if (! ifname) - return -1; - - log_route (destination, netmask, gateway, metric, change, del); - - if ((s = socket (PF_ROUTE, SOCK_RAW, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return -1; - } - - memset (&rtm, 0, sizeof (rtm)); - - rtm.hdr.rtm_version = RTM_VERSION; - rtm.hdr.rtm_seq = ++seq; - rtm.hdr.rtm_type = change ? RTM_CHANGE : del ? RTM_DELETE : RTM_ADD; - rtm.hdr.rtm_flags = RTF_UP | RTF_STATIC; - - /* This order is important */ - rtm.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK; - -#define ADDADDR(_addr) \ - memset (&su, 0, sizeof (su)); \ - su.sin.sin_family = AF_INET; \ - su.sin.sin_len = sizeof (su.sin); \ - memcpy (&su.sin.sin_addr, &_addr, sizeof (su.sin.sin_addr)); \ - l = SA_SIZE (&(su.sa)); \ - memcpy (bp, &(su), l); \ - bp += l; - - ADDADDR (destination); - - if (netmask.s_addr == INADDR_BROADCAST || - gateway.s_addr == INADDR_ANY) - { - /* Make us a link layer socket */ - unsigned char *hwaddr; - size_t hwlen = 0; - - if (netmask.s_addr == INADDR_BROADCAST) - rtm.hdr.rtm_flags |= RTF_HOST; - - hwaddr = xmalloc (sizeof (unsigned char) * HWADDR_LEN); - _do_interface (ifname, hwaddr, &hwlen, NULL, false, false); - memset (&su, 0, sizeof (su)); - su.sdl.sdl_len = sizeof (su.sdl); - su.sdl.sdl_family = AF_LINK; - su.sdl.sdl_nlen = strlen (ifname); - memcpy (&su.sdl.sdl_data, ifname, (size_t) su.sdl.sdl_nlen); - su.sdl.sdl_alen = hwlen; - memcpy (((unsigned char *) &su.sdl.sdl_data) + su.sdl.sdl_nlen, - hwaddr, (size_t) su.sdl.sdl_alen); - - l = SA_SIZE (&(su.sa)); - memcpy (bp, &su, l); - bp += l; - free (hwaddr); - } else { - rtm.hdr.rtm_flags |= RTF_GATEWAY; - ADDADDR (gateway); - } - - ADDADDR (netmask); -#undef ADDADDR - - rtm.hdr.rtm_msglen = l = bp - (char *)&rtm; - if (write (s, &rtm, l) == -1) { - /* Don't report error about routes already existing */ - if (errno != EEXIST) - logger (LOG_ERR, "write: %s", strerror (errno)); - close (s); - return -1; - } - - close (s); - return 0; -} - -#elif __linux__ -/* This netlink stuff is overly compex IMO. - * The BSD implementation is much cleaner and a lot less code. - * send_netlink handles the actual transmission so we can work out - * if there was an error or not. */ -#define BUFFERLEN 256 -int send_netlink (struct nlmsghdr *hdr, netlink_callback callback, void *arg) -{ - int s; - pid_t mypid = getpid (); - struct sockaddr_nl nl; - struct iovec iov; - struct msghdr msg; - static unsigned int seq; - char *buffer; - ssize_t bytes; - union - { - char *buffer; - struct nlmsghdr *nlm; - } h; - - if ((s = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return -1; - } - - memset (&nl, 0, sizeof (nl)); - nl.nl_family = AF_NETLINK; - if (bind (s, (struct sockaddr *) &nl, sizeof (nl)) == -1) { - logger (LOG_ERR, "bind: %s", strerror (errno)); - close (s); - return -1; - } - - memset (&iov, 0, sizeof (iov)); - iov.iov_base = hdr; - iov.iov_len = hdr->nlmsg_len; - - memset (&msg, 0, sizeof (msg)); - msg.msg_name = &nl; - msg.msg_namelen = sizeof (nl); - msg.msg_iov = &iov; - msg.msg_iovlen = 1; - - /* Request a reply */ - hdr->nlmsg_flags |= NLM_F_ACK; - hdr->nlmsg_seq = ++seq; - - if (sendmsg (s, &msg, 0) == -1) { - logger (LOG_ERR, "write: %s", strerror (errno)); - close (s); - return -1; - } - - buffer = xzalloc (sizeof (char) * BUFFERLEN); - iov.iov_base = buffer; - - for (;;) { - iov.iov_len = BUFFERLEN; - bytes = recvmsg (s, &msg, 0); - - if (bytes == -1) { - if (errno != EINTR) - logger (LOG_ERR, "recvmsg: %s", - strerror (errno)); - continue; - } - - if (bytes == 0) { - logger (LOG_ERR, "netlink: EOF"); - goto eexit; - } - - if (msg.msg_namelen != sizeof (nl)) { - logger (LOG_ERR, - "netlink: sender address length mismatch"); - goto eexit; - } - - for (h.buffer = buffer; bytes >= (signed) sizeof (*h.nlm); ) { - int len = h.nlm->nlmsg_len; - int l = len - sizeof (*h.nlm); - struct nlmsgerr *err = (struct nlmsgerr *) NLMSG_DATA (h.nlm); - - if (l < 0 || len > bytes) { - if (msg.msg_flags & MSG_TRUNC) - logger (LOG_ERR, "netlink: truncated message"); - else - logger (LOG_ERR, "netlink: malformed message"); - goto eexit; - } - - /* Ensure it's our message */ - if (nl.nl_pid != 0 || - (pid_t) h.nlm->nlmsg_pid != mypid || - h.nlm->nlmsg_seq != seq) - { - /* Next Message */ - bytes -= NLMSG_ALIGN (len); - h.buffer += NLMSG_ALIGN (len); - continue; - } - - /* We get an NLMSG_ERROR back with a code of zero for success */ - if (h.nlm->nlmsg_type != NLMSG_ERROR) { - logger (LOG_ERR, "netlink: unexpected reply %d", - h.nlm->nlmsg_type); - goto eexit; - } - - if ((unsigned) l < sizeof (*err)) { - logger (LOG_ERR, "netlink: error truncated"); - goto eexit; - } - - if (err->error == 0) { - int retval = 0; - - close (s); - if (callback) { - if ((retval = callback (hdr, arg)) == -1) - logger (LOG_ERR, "netlink: callback failed"); - } - free (buffer); - return (retval); - } - - errno = -err->error; - /* Don't report on something already existing */ - if (errno != EEXIST) - logger (LOG_ERR, "netlink: %s", - strerror (errno)); - goto eexit; - } - } - -eexit: - close (s); - free (buffer); - return -1; -} - -#define NLMSG_TAIL(nmsg) \ - ((struct rtattr *) (((ptrdiff_t) (nmsg)) + NLMSG_ALIGN ((nmsg)->nlmsg_len))) - -static int add_attr_l(struct nlmsghdr *n, unsigned int maxlen, int type, - const void *data, int alen) -{ - int len = RTA_LENGTH(alen); - struct rtattr *rta; - - if (NLMSG_ALIGN (n->nlmsg_len) + RTA_ALIGN (len) > maxlen) { - logger (LOG_ERR, "add_attr_l: message exceeded bound of %d\n", - maxlen); - return -1; - } - - rta = NLMSG_TAIL (n); - rta->rta_type = type; - rta->rta_len = len; - memcpy (RTA_DATA (rta), data, alen); - n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + RTA_ALIGN (len); - - return 0; -} - -static int add_attr_32(struct nlmsghdr *n, unsigned int maxlen, int type, - uint32_t data) -{ - int len = RTA_LENGTH (sizeof (data)); - struct rtattr *rta; - - if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen) { - logger (LOG_ERR, "add_attr32: message exceeded bound of %d\n", - maxlen); - return -1; - } - - rta = NLMSG_TAIL (n); - rta->rta_type = type; - rta->rta_len = len; - memcpy (RTA_DATA (rta), &data, sizeof (data)); - n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len; - - return 0; -} - -struct nlma -{ - struct nlmsghdr hdr; - struct ifaddrmsg ifa; - char buffer[64]; -}; - -struct nlmr -{ - struct nlmsghdr hdr; - struct rtmsg rt; - char buffer[256]; -}; - -static int do_address(const char *ifname, - struct in_addr address, struct in_addr netmask, - struct in_addr broadcast, int del) -{ - struct nlma *nlm; - int retval; - - if (!ifname) - return -1; - - nlm = xzalloc (sizeof (*nlm)); - nlm->hdr.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg)); - nlm->hdr.nlmsg_flags = NLM_F_REQUEST; - if (! del) - nlm->hdr.nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE; - nlm->hdr.nlmsg_type = del ? RTM_DELADDR : RTM_NEWADDR; - if (! (nlm->ifa.ifa_index = if_nametoindex (ifname))) { - logger (LOG_ERR, "if_nametoindex: no index for interface `%s'", - ifname); - free (nlm); - return -1; - } - nlm->ifa.ifa_family = AF_INET; - - nlm->ifa.ifa_prefixlen = inet_ntocidr (netmask); - - /* This creates the aliased interface */ - add_attr_l (&nlm->hdr, sizeof (*nlm), IFA_LABEL, - ifname, strlen (ifname) + 1); - - add_attr_l (&nlm->hdr, sizeof (*nlm), IFA_LOCAL, - &address.s_addr, sizeof (address.s_addr)); - if (! del) - add_attr_l (&nlm->hdr, sizeof (*nlm), IFA_BROADCAST, - &broadcast.s_addr, sizeof (broadcast.s_addr)); - - retval = send_netlink (&nlm->hdr, NULL, NULL); - free (nlm); - return retval; -} - -static int do_route (const char *ifname, - struct in_addr destination, - struct in_addr netmask, - struct in_addr gateway, - int metric, int change, int del) -{ - struct nlmr *nlm; - unsigned int ifindex; - int retval; - - if (! ifname) - return -1; - - log_route (destination, netmask, gateway, metric, change, del); - - if (! (ifindex = if_nametoindex (ifname))) { - logger (LOG_ERR, "if_nametoindex: no index for interface `%s'", - ifname); - return -1; - } - - nlm = xzalloc (sizeof (*nlm)); - nlm->hdr.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg)); - if (change) - nlm->hdr.nlmsg_flags = NLM_F_REPLACE; - else if (! del) - nlm->hdr.nlmsg_flags = NLM_F_CREATE | NLM_F_EXCL; - nlm->hdr.nlmsg_flags |= NLM_F_REQUEST; - nlm->hdr.nlmsg_type = del ? RTM_DELROUTE : RTM_NEWROUTE; - nlm->rt.rtm_family = AF_INET; - nlm->rt.rtm_table = RT_TABLE_MAIN; - - if (del) - nlm->rt.rtm_scope = RT_SCOPE_NOWHERE; - else { - nlm->hdr.nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL; - nlm->rt.rtm_protocol = RTPROT_BOOT; - if (netmask.s_addr == INADDR_BROADCAST || - gateway.s_addr == INADDR_ANY) - nlm->rt.rtm_scope = RT_SCOPE_LINK; - else - nlm->rt.rtm_scope = RT_SCOPE_UNIVERSE; - nlm->rt.rtm_type = RTN_UNICAST; - } - - nlm->rt.rtm_dst_len = inet_ntocidr (netmask); - add_attr_l (&nlm->hdr, sizeof (*nlm), RTA_DST, - &destination.s_addr, sizeof (destination.s_addr)); - if (netmask.s_addr != INADDR_BROADCAST && - destination.s_addr != gateway.s_addr) - add_attr_l (&nlm->hdr, sizeof (*nlm), RTA_GATEWAY, - &gateway.s_addr, sizeof (gateway.s_addr)); - - add_attr_32 (&nlm->hdr, sizeof (*nlm), RTA_OIF, ifindex); - add_attr_32 (&nlm->hdr, sizeof (*nlm), RTA_PRIORITY, metric); - - retval = send_netlink (&nlm->hdr, NULL, NULL); - free (nlm); - return retval; -} - -#else - #error "Platform not supported!" - #error "We currently support BPF and Linux sockets." - #error "Other platforms may work using BPF. If yours does, please let me know" - #error "so I can add it to our list." -#endif - -int add_address (const char *ifname, struct in_addr address, - struct in_addr netmask, struct in_addr broadcast) -{ - logger (LOG_INFO, "adding IP address %s/%d", - inet_ntoa (address), inet_ntocidr (netmask)); - - return (do_address (ifname, address, netmask, broadcast, 0)); -} - -int del_address (const char *ifname, - struct in_addr address, struct in_addr netmask) -{ - struct in_addr t; - - logger (LOG_INFO, "removing IP address %s/%d", - inet_ntoa (address), inet_ntocidr (netmask)); - - memset (&t, 0, sizeof (t)); - return (do_address (ifname, address, netmask, t, 1)); -} - -int add_route (const char *ifname, struct in_addr destination, - struct in_addr netmask, struct in_addr gateway, int metric) -{ - return (do_route (ifname, destination, netmask, gateway, metric, 0, 0)); -} - -int change_route (const char *ifname, struct in_addr destination, - struct in_addr netmask, struct in_addr gateway, int metric) -{ - return (do_route (ifname, destination, netmask, gateway, metric, 1, 0)); -} - -int del_route (const char *ifname, struct in_addr destination, - struct in_addr netmask, struct in_addr gateway, int metric) -{ - return (do_route (ifname, destination, netmask, gateway, metric, 0, 1)); -} - - -int flush_addresses (const char *ifname) -{ - return (_do_interface (ifname, NULL, NULL, NULL, true, false)); -} - -in_addr_t get_address (const char *ifname) -{ - struct in_addr address; - if (_do_interface (ifname, NULL, NULL, &address, false, true) > 0) - return (address.s_addr); - return (0); -} - -int has_address (const char *ifname, struct in_addr address) -{ - return (_do_interface (ifname, NULL, NULL, &address, false, false)); -} diff --git a/workspace/customdhcpcd/src/interface.h b/workspace/customdhcpcd/src/interface.h deleted file mode 100644 index 8215d48..0000000 --- a/workspace/customdhcpcd/src/interface.h +++ /dev/null @@ -1,173 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef INTERFACE_H -#define INTERFACE_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" - -#ifdef __linux__ -# include -#endif - -#ifdef ENABLE_DUID -#ifndef DUID_LEN -# define DUID_LEN 128 + 2 -#endif -#endif - -#define EUI64_ADDR_LEN 8 -#define INFINIBAND_ADDR_LEN 20 - -/* Linux 2.4 doesn't define this */ -#ifndef ARPHRD_IEEE1394 -# define ARPHRD_IEEE1394 24 -#endif - -/* The BSD's don't define this yet */ -#ifndef ARPHRD_INFINIBAND -# define ARPHRD_INFINIBAND 32 -#endif - -#define HWADDR_LEN 20 - -/* Work out if we have a private address or not - * 10/8 - * 172.16/12 - * 192.168/16 - */ -#ifndef IN_PRIVATE -# define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) || \ - ((addr & 0xfff00000) == 0xac100000) || \ - ((addr & IN_CLASSB_NET) == 0xc0a80000)) -#endif - -#define LINKLOCAL_ADDR 0xa9fe0000 -#define LINKLOCAL_MASK 0xffff0000 -#define LINKLOCAL_BRDC 0xa9feffff - -#ifndef IN_LINKLOCAL -# define IN_LINKLOCAL(addr) ((addr & IN_CLASSB_NET) == LINKLOCAL_ADDR) -#endif - -#ifndef STAILQ_ENTRY -# error "your sys/queue.h is too old and lacks STAILQ" -#endif - -#define NSTAILQ_FOREACH(var, head, field) \ - if (head) STAILQ_FOREACH (var, head, field) - -typedef struct route_t -{ - struct in_addr destination; - struct in_addr netmask; - struct in_addr gateway; - STAILQ_ENTRY (route_t) entries; -} route_t; -STAILQ_HEAD (route_head, route_t); - -typedef struct address_t -{ - struct in_addr address; - STAILQ_ENTRY (address_t) entries; -} address_t; -STAILQ_HEAD (address_head, address_t); - -typedef struct interface_t -{ - char name[IF_NAMESIZE]; - sa_family_t family; - unsigned char hwaddr[HWADDR_LEN]; - size_t hwlen; - bool arpable; - unsigned short mtu; - - int fd; - size_t buffer_length; - -#ifdef __linux__ - int listen_fd; - int socket_protocol; -#endif - - char infofile[PATH_MAX]; - - unsigned short previous_mtu; - struct in_addr previous_address; - struct in_addr previous_netmask; - struct route_head *previous_routes; - - time_t start_uptime; - - unsigned char *clientid; - size_t clientid_len; -} interface_t; - -void free_address (struct address_head *addresses); -void free_route (struct route_head *routes); -uint32_t get_netmask (uint32_t addr); -char *hwaddr_ntoa (const unsigned char *hwaddr, size_t hwlen); -size_t hwaddr_aton (unsigned char *hwaddr, const char *addr); - -interface_t *read_interface (const char *ifname, int metric); -int get_mtu (const char *ifname); -int set_mtu (const char *ifname, short int mtu); - -int add_address (const char *ifname, struct in_addr address, - struct in_addr netmask, struct in_addr broadcast); -int del_address (const char *ifname, struct in_addr address, - struct in_addr netmask); - -int flush_addresses (const char *ifname); -in_addr_t get_address (const char *ifname); -int has_address (const char *ifname, struct in_addr address); - -int add_route (const char *ifname, struct in_addr destination, - struct in_addr netmask, struct in_addr gateway, int metric); -int change_route (const char *ifname, struct in_addr destination, - struct in_addr netmask, struct in_addr gateway, int metric); -int del_route (const char *ifname, struct in_addr destination, - struct in_addr netmask, struct in_addr gateway, int metric); - -int inet_ntocidr (struct in_addr address); -int inet_cidrtoaddr (int cidr, struct in_addr *addr); - -#ifdef __linux__ -typedef int (*netlink_callback) (struct nlmsghdr *hdr, void *arg); -int send_netlink (struct nlmsghdr *hdr, netlink_callback callback, void *arg); -#endif -#endif diff --git a/workspace/customdhcpcd/src/ipv4ll.c b/workspace/customdhcpcd/src/ipv4ll.c deleted file mode 100644 index 9742b9a..0000000 --- a/workspace/customdhcpcd/src/ipv4ll.c +++ /dev/null @@ -1,70 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include - -#include "config.h" -#include "arp.h" -#include "ipv4ll.h" - -#ifdef ENABLE_IPV4LL - -#ifndef ENABLE_ARP - # error IPV4LL requires ARP -#endif - -#define IPV4LL_LEASETIME 20 - -int ipv4ll_get_address (interface_t *iface, dhcp_t *dhcp) { - struct in_addr addr; - - for (;;) { - addr.s_addr = htonl (LINKLOCAL_ADDR | - (((uint32_t) abs ((int) random ()) - % 0xFD00) + 0x0100)); - errno = 0; - if (! arp_claim (iface, addr)) - break; - /* Our ARP may have been interrupted */ - if (errno) - return (-1); - } - - dhcp->address.s_addr = addr.s_addr; - dhcp->netmask.s_addr = htonl (LINKLOCAL_MASK); - dhcp->broadcast.s_addr = htonl (LINKLOCAL_BRDC); - - /* Finally configure some DHCP like lease times */ - dhcp->leasetime = IPV4LL_LEASETIME; - dhcp->renewaltime = (dhcp->leasetime * 0.5); - dhcp->rebindtime = (dhcp->leasetime * 0.875); - - return (0); -} - -#endif diff --git a/workspace/customdhcpcd/src/ipv4ll.h b/workspace/customdhcpcd/src/ipv4ll.h deleted file mode 100644 index 4fa8943..0000000 --- a/workspace/customdhcpcd/src/ipv4ll.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef IPV4LL_H -#define IPV4LL_H - -#ifdef ENABLE_IPV4LL - -#include "dhcp.h" -#include "interface.h" - -int ipv4ll_get_address (interface_t *iface, dhcp_t *dhcp); - -#endif -#endif diff --git a/workspace/customdhcpcd/src/logger.c b/workspace/customdhcpcd/src/logger.c deleted file mode 100644 index 91d6cc0..0000000 --- a/workspace/customdhcpcd/src/logger.c +++ /dev/null @@ -1,128 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#define SYSLOG_NAMES - -#define COM_CH "/var/tmp/com.socket" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "common.h" -#include "logger.h" -#include "logwriter.h" - -static int loglevel = LOG_WARNING; -static char logprefix[12] = { 0 }; - -int logtolevel(const char *priority) { - CODE *c; - - if (isdigit ((int) *priority)) - return (atoi(priority)); - - for (c = prioritynames; c->c_name; c++) - if (!strcasecmp(priority, c->c_name)) - return (c->c_val); - - return (-1); -} - -static const char *leveltolog(int level) { - CODE *c; - - for (c = prioritynames; c->c_name; c++) - if (c->c_val == level) - return (c->c_name); - - return (NULL); -} - -void setloglevel(int level) { - loglevel = level; -} - -void setlogprefix(const char *prefix) { - snprintf(logprefix, sizeof(logprefix), "%s", prefix); -} - -void logger(int level, const char *fmt, ...) { - va_list p; - va_list p2; - FILE *f = stderr; - - va_start (p, fmt); - va_copy (p2, p); - - if (level <= LOG_ERR || level <= loglevel) { - - /* new function by Niklas Goby - * send the log message also to our Qt programm. - * implemented in logwriter.c - * */ - logLoggerToQt(level, fmt, p); - - if (level == LOG_DEBUG || level == LOG_INFO) - f = stdout; - fprintf(f, "%s, %s", leveltolog(level), logprefix); - vfprintf(f, fmt, p); - fputc('\n', f); - - /* stdout, stderr may be re-directed to some kind of buffer. - * So we always flush to ensure it's written. */ - fflush(f); - } - - if (level < LOG_DEBUG || level <= loglevel) { - size_t len = strlen(logprefix); - size_t fmt2len = strlen(fmt) + len + 1; - char *fmt2 = malloc(sizeof(char) * fmt2len); - char *pf = fmt2; - if (fmt2) { - memcpy(pf, logprefix, len); - pf += len; - strlcpy(pf, fmt, fmt2len - len); - vsyslog(level, fmt2, p2); - free(fmt2); - } else { - vsyslog(level, fmt, p2); - syslog(LOG_ERR, "logger: memory exhausted"); - exit(EXIT_FAILURE); - } - } - - va_end (p2); - va_end (p); -} - diff --git a/workspace/customdhcpcd/src/logger.h b/workspace/customdhcpcd/src/logger.h deleted file mode 100644 index 70e2ed5..0000000 --- a/workspace/customdhcpcd/src/logger.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef LOGGER_H -#define LOGGER_H - -#if defined(__GNUC__) -# define _PRINTF_LIKE(_one, _two) __attribute__ ((__format__ (__printf__, _one, _two))) -#else -# define _PRINTF_LIKE(_one, _two) -#endif - -#include - - - -int logtolevel (const char *priority); -void setloglevel (int level); -void setlogprefix (const char *prefix); -void logger (int level, const char *fmt, ...) _PRINTF_LIKE (2, 3); - - - -#endif diff --git a/workspace/customdhcpcd/src/logwriter.c b/workspace/customdhcpcd/src/logwriter.c deleted file mode 100644 index 74c0181..0000000 --- a/workspace/customdhcpcd/src/logwriter.c +++ /dev/null @@ -1,149 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "common.h" -#include "dhcp.h" -#include "dhcpcd.h" -#include "logger.h" -#include "logwriter.h" -#include "status.h" - -/*sockets for the logger and the qt-reader */ -int sockfd, ns; -int retval = -1; -char socketName[QTSOCKETADDRESSLENGTH]; -char interfaceName[IF_NAMESIZE]; -char mesg[1024]; - -void setSocketName(const char * sn) { - snprintf(socketName, sizeof(socketName), "%s", sn); -} - -void setInterfaceName(const char * in){ - snprintf(interfaceName, sizeof(interfaceName), "%s", in); -} - -int initQtLoggerSocket() { - /** - * new code. seems to be right. - */ - - struct sockaddr_un serv_addr; - fprintf(stdout,"start init \n"); - sockfd = socket(AF_UNIX, SOCK_STREAM, 0); - if (sockfd < 0) { - fprintf(stdout,"ERROR opening socket \n"); - retval = sockfd; - return sockfd; - } - serv_addr.sun_family = AF_UNIX; - strcpy(serv_addr.sun_path, socketName); - - retval = connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); - if ( retval < 0) - fprintf(stdout,"ERROR connecting \n"); - fprintf(stdout,"init Qt Logger Socket done \n"); - return retval; -} - -void closeQtLoggerSocket (){ - close(sockfd); -} - -/* -void logToQt(char * status, char * substatus, char * msg) { - char * m = malloc(strlen(interfaceName) + strlen(status) + strlen(substatus) + strlen(msg) +4 ); - strcpy(m,interfaceName); - strcat(m,";"); - strcat(m,status); - strcat(m,";"); - strcat(m,substatus); - strcat(m,";"); - strcat(m,msg); - sendToQt(m); - - free(m); -} -*/ - - -void sendToQt(log_msg * msg) { - int n = -1; - const char *tpl = "%s;%d;%d;%s\n"; - char *outbuf; - size_t outbuf_size = - sizeof(char)*4 + // ";" *3 + newline - sizeof(int)*2 + // status, substatus - sizeof(msg->device) + // devicename - sizeof(msg->msg); // msg - outbuf = malloc(outbuf_size); - memset(outbuf,0,outbuf_size); - snprintf(outbuf, sizeof(char)*3 + sizeof(int)*2 + sizeof(msg->device) + sizeof(msg->msg), tpl, msg->device, msg->status, msg->substatus, msg->msg); - if (outbuf != NULL){ - n = write(sockfd, outbuf, outbuf_size); - } - free(outbuf); - syslog (LOG_INFO, "[fbgui] INFO writing to socket: [%d:%d] %s (%s)", msg->status, msg->substatus, msg->msg, msg->device); -// fflush(sockfd); - if (n < 0) { - syslog (LOG_ERR, "[fbgui] ERROR writing to socket: [%d:%d] %s (%s)", msg->status, msg->substatus, msg->msg, msg->device); -// fprintf(stdout, "ERROR writing to socket: %s", msg); - } - //usleep(500); -} - -void logToQt(int status, int substatus, const char * msg) { - if (retval >= 0) { - log_msg lm; - lm.status = status; - lm.substatus = substatus; - snprintf(lm.msg, sizeof(lm.msg), "%s", msg); - snprintf(lm.device, sizeof(lm.device), "%s", interfaceName); - sendToQt(&lm); - } -} - -void logSendToQt(int type) { - switch(type) { - case DHCP_DISCOVER: - logToQt(LOG_INFO, DHCP_DISCOVER, "send discover"); - break; - case DHCP_OFFER: - logToQt(LOG_INFO, DHCP_OFFER, "send offer"); - break; - case DHCP_REQUEST: - logToQt(LOG_INFO, DHCP_REQUEST, "send request"); - break; - case DHCP_DECLINE: - logToQt(LOG_INFO, DHCP_DECLINE, "send decline"); - break; - case DHCP_ACK: - logToQt(LOG_INFO, DHCP_ACK, "send ack"); - break; - case DHCP_NAK: - logToQt(LOG_INFO, DHCP_NAK, "send nak"); - break; - case DHCP_RELEASE: - logToQt(LOG_INFO, DHCP_RELEASE, "send release"); - break; - case DHCP_INFORM: - logToQt(LOG_INFO, DHCP_INFORM, "send inform"); - break; - default : - break; - } -} - -void logLoggerToQt(int level, const char *fmt, va_list args) { - vsnprintf(mesg, sizeof(mesg), fmt, args); - strcat(mesg, "\n"); - logToQt(level, DHCPCD_LOG, mesg); -} diff --git a/workspace/customdhcpcd/src/logwriter.h b/workspace/customdhcpcd/src/logwriter.h deleted file mode 100644 index 1a755fc..0000000 --- a/workspace/customdhcpcd/src/logwriter.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * logwriter.h - * - * Created on: Jul 7, 2011 - * Author: niklas - */ - -#ifndef LOGWRITER_H_ -#define LOGWRITER_H_ - -#include "dhcpcd.h" - -#define LOG_MSG_SIZE 1024 - -typedef struct _log_msg log_msg; -struct _log_msg { - int status; - int substatus; - char device[IF_NAMESIZE]; - char msg[LOG_MSG_SIZE]; -}; - -/** - * new functions for communicating with Qt - */ -void setSocketName(const char * sn); -void setInterfaceName(const char * in); -int initQtLoggerSocket (); -void closeQtLoggerSocket (); -void sendToQt (); -void logToQt(int status, int substatus, const char * msg); -void logSendToQt(int type); -void logLoggerToQt(int level, const char *fmt, va_list args); -//void logToQt(char * status, char * substatus, char * msg); - - -#endif /* LOGWRITER_H_ */ diff --git a/workspace/customdhcpcd/src/mk/cc.mk b/workspace/customdhcpcd/src/mk/cc.mk deleted file mode 100644 index d52597b..0000000 --- a/workspace/customdhcpcd/src/mk/cc.mk +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright 2008 Roy Marples - -# Setup some good default CFLAGS -CFLAGS?= -O2 - -# Default to using the C99 standard -CSTD?= c99 -_CSTD_SH= if test -n "${CSTD}"; then echo "-std=${CSTD}"; else echo ""; fi -_CSTD!= ${_CSTD_SH} -CFLAGS+= ${_CSTD}$(shell ${_CSTD_SH}) - -# Try and use some good cc flags -_CC_FLAGS= -pedantic -Wall -Wunused -Wimplicit -Wshadow -Wformat=2 \ - -Wmissing-declarations -Wno-missing-prototypes -Wwrite-strings \ - -Wbad-function-cast -Wnested-externs -Wcomment -Winline \ - -Wchar-subscripts -Wcast-align -Wno-format-nonliteral \ - -Wdeclaration-after-statement -Wsequence-point -Wextra -_CC_FLAGS_SH= for f in ${_CC_FLAGS}; do \ - if ${CC} $$f -S -o /dev/null -xc /dev/null >/dev/null 2>&1; \ - then printf "%s" "$$f "; fi \ - done -_CC_FLAGS!= ${_CC_FLAGS_SH} -CFLAGS+= ${_CC_FLAGS}$(shell ${CC_FLAGS_SH}) diff --git a/workspace/customdhcpcd/src/mk/depend.mk b/workspace/customdhcpcd/src/mk/depend.mk deleted file mode 100644 index a4d717a..0000000 --- a/workspace/customdhcpcd/src/mk/depend.mk +++ /dev/null @@ -1,11 +0,0 @@ -# This only works for make implementations that always include a .depend if -# it exists. Only GNU make does not do this. - -# Copyright 2008 Roy Marples - -CLEANFILES+= .depend - -.depend: ${SRCS} - ${CC} ${CFLAGS} -MM ${SRCS} > .depend - -depend: .depend diff --git a/workspace/customdhcpcd/src/mk/dist.mk b/workspace/customdhcpcd/src/mk/dist.mk deleted file mode 100644 index 1d3669d..0000000 --- a/workspace/customdhcpcd/src/mk/dist.mk +++ /dev/null @@ -1,11 +0,0 @@ -# rules to make a distribution tarball from a git repo -# Copyright 2008 Roy Marples - -GITREF?= HEAD -DISTPREFIX?= ${PROG}-${VERSION} -DISTFILE?= ${DISTPREFIX}.tar.bz2 - -CLEANFILES+= ${DISTFILE} - -dist: - git archive --prefix=${DISTPREFIX}/ ${GITREF} | bzip2 > ${DISTFILE} diff --git a/workspace/customdhcpcd/src/mk/man.mk b/workspace/customdhcpcd/src/mk/man.mk deleted file mode 100644 index 5d9bf26..0000000 --- a/workspace/customdhcpcd/src/mk/man.mk +++ /dev/null @@ -1,14 +0,0 @@ -# rules to install manpages -# Copyright 2008 Roy Marples - -MANPREFIX?= /usr/share -MANDIR?= ${MANPREFIX}/man/man -MANMODE?= 0444 -MINSTALL?= ${INSTALL} -m ${MANMODE} - -man: ${MAN} - -# We cheat as all our pages go into section 8 -maninstall: man - ${INSTALL} -d ${DESTDIR}${MANDIR}8 - for man in ${MAN}; do ${MINSTALL} $$man ${DESTDIR}${MANDIR}8; done diff --git a/workspace/customdhcpcd/src/mk/os.mk b/workspace/customdhcpcd/src/mk/os.mk deleted file mode 100644 index af173fc..0000000 --- a/workspace/customdhcpcd/src/mk/os.mk +++ /dev/null @@ -1,77 +0,0 @@ -# Setup OS specific variables -# Copyright 2008 Roy Marples - -# Work out if we need -lresolv or not -_LIBRESOLV_SH= printf '\#include \n\#include \nint main (void) { return (res_init ()); }\n' > .res_init.c; \ - if ${CC} .res_init.c -o .res_init >/dev/null 2>&1; then \ - echo ""; \ - elif ${CC} .res_init.c -lresolv -o .res_init >/dev/null 2>&1; then \ - echo "-lresolv"; \ - else \ - echo "Cannot work out how to get res_init to link" >&2; \ - rm -f .res_init.c .res_init; \ - exit 1; \ - fi; \ - rm -f .res_init.c .res_init -_LIBRESOLV!= ${_LIBRESOLV_SH} -LIBRESOLV= ${_LIBRESOLV}$(shell ${_LIBRESOLV_SH}) - -# Work out if we need -lrt or not -_LIBRT_SH= printf '\#include \n\#include \n\nint main (void) { struct timespec ts;\n\#if defined(_POSIX_MONOTONIC_CLOCK) && defined(CLOCK_MONOTONIC)\nreturn (clock_gettime (CLOCK_MONOTONIC, &ts));\n\#else\nreturn -1;\n\#endif\n}\n' > .clock_gettime.c; \ - if ${CC} .clock_gettime.c -o .clock_gettime >/dev/null 2>&1; then \ - echo ""; \ - elif ${CC} .clock_gettime.c -lrt -o .clock_gettime >/dev/null 2>&1; then \ - echo "-lrt"; \ - else \ - echo ""; \ - fi; \ - rm -f .clock_gettime.c .clock_gettime -_LIBRT!= ${_LIBRT_SH} -LIBRT= ${_LIBRT}$(shell ${_LIBRT_SH}) - -# Work out if our fork() works or not -_HAVE_FORK_SH= if test "${HAVE_FORK}" = "yes"; then \ - echo ""; \ - elif test -n "${HAVE_FORK}"; then \ - echo "-DTHERE_IS_NO_FORK"; \ - else \ - printf '\#include \n\#include \nint main (void) { pid_t pid = fork(); if (pid == -1) exit (-1); exit (0); }\n' > .fork.c; \ - ${CC} .fork.c -o .fork >/dev/null 2>&1; \ - if ./.fork; then \ - echo ""; \ - else \ - echo "-DTHERE_IS_NO_FORK"; \ - fi; \ - rm -f .fork.c .fork; \ - fi; -_HAVE_FORK!= ${_HAVE_FORK_SH} -FORK= ${_HAVE_FORK}$(shell ${_HAVE_FORK_SH}) - -# info dir defaults to /var/lib/dhcpcd on Linux and /var/db elsewhere -_INFODIR_SH= if test -n "${INFODIR}"; then \ - echo "${INFODIR}"; \ - else \ - case `uname -s` in \ - Linux) echo "/var/lib/dhcpcd";; \ - *) echo "/var/db";; \ - esac \ - fi -_INFODIR!= ${_INFODIR_SH} -INFOD?= ${_INFODIR}$(shell ${_INFODIR_SH}) - -# Work out how to restart services -_RC_SH= if test -n "${HAVE_INIT}"; then \ - test "${HAVE_INIT}" = "no" || echo "-DENABLE_${HAVE_INIT}"; \ - elif test -x /sbin/runscript; then echo "-DENABLE_OPENRC"; \ - elif test -x /sbin/service; then echo "-DENABLE_SERVICE"; \ - elif test -x /etc/rc.d/rc.S -a -x /etc/rc.d/rc.M; then echo "-DENABLE_SLACKRC"; \ - elif test -d /etc/rc.d; then echo "-DENABLE_BSDRC"; \ - elif test -d /etc/init.d; then echo "-DENABLE_SYSV"; \ - fi -_RC!= ${_RC_SH} -RC= ${_RC}$(shell ${_RC_SH}) - -# glibc requires _BSD_SOURCE and _XOPEN_SOURCE -_DEF_SH= case `uname -s` in Linux) echo "-D_BSD_SOURCE -D_XOPEN_SOURCE=600";; *) echo;; esac -_DEF!= ${_DEF_SH} -CFLAGS+= ${_DEF}$(shell ${_DEF_SH}) diff --git a/workspace/customdhcpcd/src/mk/prog.mk b/workspace/customdhcpcd/src/mk/prog.mk deleted file mode 100644 index 6f2560c..0000000 --- a/workspace/customdhcpcd/src/mk/prog.mk +++ /dev/null @@ -1,34 +0,0 @@ -# rules to build a program -# based on FreeBSD's bsd.prog.mk - -# Copyright 2008 Roy Marples - -BINDIR?= ${PREFIX}/usr/bin -BINMODE?= 0755 -OBJS+= ${SRCS:.c=.o} - -INSTALL?= install - -all: ${PROG} ${MAN} - -${PROG}: ${SCRIPTS} ${OBJS} - ${CC} ${LDFLAGS} -o $@ ${OBJS} ${LDADD} - -_proginstall: ${PROG} - ${INSTALL} -d ${DESTDIR}${BINDIR} - ${INSTALL} -m ${BINMODE} ${PROG} ${DESTDIR}${BINDIR} - -include ${MK}/depend.mk -include ${MK}/man.mk -include ${MK}/dist.mk - -install: _proginstall maninstall - -clean: - rm -f ${OBJS} ${PROG} ${CLEANFILES} - -LINTFLAGS?= -hx -LINTFLAGS+= -X 159,247,352 - -lint: ${SRCS:.c=.c} - ${LINT} ${LINTFLAGS} ${CFLAGS:M-[DIU]*} $^ ${.ALLSRC} diff --git a/workspace/customdhcpcd/src/signal.c b/workspace/customdhcpcd/src/signal.c deleted file mode 100644 index 9055c9f..0000000 --- a/workspace/customdhcpcd/src/signal.c +++ /dev/null @@ -1,183 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include - -#include "common.h" -#include "logger.h" -#include "signal.h" - -static int signal_pipe[2]; -static int signals[5]; - -static const int handle_sigs[] = { - SIGHUP, - SIGALRM, - SIGTERM, - SIGINT -}; - -static void signal_handler (int sig) -{ - unsigned int i = 0; - int serrno = errno; - - /* Add a signal to our stack */ - while (signals[i]) - i++; - if (i > sizeof (signals) / sizeof (signals[0])) - logger (LOG_ERR, "signal buffer overrun"); - else - signals[i] = sig; - - if (write (signal_pipe[1], &sig, sizeof (sig)) == -1) - logger (LOG_ERR, "Could not send signal: %s", strerror (errno)); - - /* Restore errno */ - errno = serrno; -} - -int signal_fd (void) -{ - return (signal_pipe[0]); -} - -/* Check if we have a signal or not */ -int signal_exists (const struct pollfd *fd) -{ - if (signals[0] || (fd && fd->revents & POLLIN)) - return (0); - return (-1); -} - -/* Read a signal from the signal pipe. Returns 0 if there is - * no signal, -1 on error (and sets errno appropriately), and - * your signal on success */ -int signal_read (struct pollfd *fd) -{ - int sig = -1; - - /* Pop a signal off the our stack */ - if (signals[0]) { - unsigned int i = 0; - sig = signals[0]; - while (i < (sizeof (signals) / sizeof (signals[0])) - 1) { - signals[i] = signals[i + 1]; - if (! signals[++i]) - break; - } - } - - if (fd && fd->revents & POLLIN) { - char buf[16]; - size_t bytes; - - memset (buf, 0, sizeof (buf)); - bytes = read (signal_pipe[0], buf, sizeof (buf)); - - if (bytes >= sizeof (sig)) - memcpy (&sig, buf, sizeof (sig)); - - /* We need to clear us from rset if nothing left in the buffer - * in case we are called many times */ - if (bytes == sizeof (sig)) - fd->revents = 0; - } - - return (sig); -} - -/* Call this before doing anything else. Sets up the socket pair - * and installs the signal handler */ -int signal_init (void) -{ - struct sigaction sa; - - if (pipe (signal_pipe) == -1) { - logger (LOG_ERR, "pipe: %s", strerror (errno)); - return (-1); - } - - /* Stop any scripts from inheriting us */ - close_on_exec (signal_pipe[0]); - close_on_exec (signal_pipe[1]); - - /* Ignore child signals and don't make zombies. - * Because we do this, we don't need to be in signal_setup */ - memset (&sa, 0, sizeof (sa)); - sa.sa_handler = SIG_DFL; - sa.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT; - if (sigaction (SIGCHLD, &sa, NULL) == -1) { - logger (LOG_ERR, "sigaction: %s", strerror (errno)); - return (-1); - } - - memset (signals, 0, sizeof (signals)); - return (0); -} - -int signal_setup (void) -{ - unsigned int i; - struct sigaction sa; - - memset (&sa, 0, sizeof (sa)); - sa.sa_handler = signal_handler; - sigemptyset (&sa.sa_mask); - - for (i = 0; i < sizeof (handle_sigs) / sizeof (handle_sigs[0]); i++) - if (sigaction (handle_sigs[i], &sa, NULL) == -1) { - logger (LOG_ERR, "sigaction: %s", strerror (errno)); - return (-1); - } - - return (0); -} - -int signal_reset (void) -{ - struct sigaction sa; - unsigned int i; - - memset (&sa, 0, sizeof (sa)); - sa.sa_handler = SIG_DFL; - sigemptyset (&sa.sa_mask); - - for (i = 0; i < sizeof (handle_sigs) / sizeof (handle_sigs[0]); i++) - if (sigaction (handle_sigs[i], &sa, NULL) == -1) { - logger (LOG_ERR, "sigaction: %s", strerror (errno)); - return (-1); - } - - return (0); -} diff --git a/workspace/customdhcpcd/src/signal.h b/workspace/customdhcpcd/src/signal.h deleted file mode 100644 index 63a5906..0000000 --- a/workspace/customdhcpcd/src/signal.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef SIGNAL_H -#define SIGNAL_H - -#include - -int signal_init (void); -int signal_setup (void); -int signal_reset (void); -int signal_fd (void); -int signal_exists (const struct pollfd *fd); -int signal_read (struct pollfd *fd); - -#endif diff --git a/workspace/customdhcpcd/src/socket.c b/workspace/customdhcpcd/src/socket.c deleted file mode 100644 index 58ad6c5..0000000 --- a/workspace/customdhcpcd/src/socket.c +++ /dev/null @@ -1,647 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#define __FAVOR_BSD /* Nasty hack so we can use BSD semantics for UDP */ -#include -#undef __FAVOR_BSD -#include -#include -#include -#include -#include -#include -#include -#include - -#if defined(BSD) || defined(__FreeBSD_kernel__) -# include -#elif __linux__ -# include -# include -# define bpf_insn sock_filter -#endif - -#include "config.h" -#include "dhcp.h" -#include "interface.h" -#include "logger.h" -#include "socket.h" - -/* A suitably large buffer for all transactions. - * BPF buffer size is set by the kernel, so no define. */ -#ifdef __linux__ -# define BUFFER_LENGTH 4096 -#endif - -/* Broadcast address for IPoIB */ -static const uint8_t ipv4_bcast_addr[] = { - 0x00, 0xff, 0xff, 0xff, - 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff -}; - -/* Credit where credit is due :) - * The below BPF filter is taken from ISC DHCP */ -static struct bpf_insn dhcp_bpf_filter [] = { - /* Make sure this is an IP packet... */ - BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 12), - BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8), - - /* Make sure it's a UDP packet... */ - BPF_STMT (BPF_LD + BPF_B + BPF_ABS, 23), - BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6), - - /* Make sure this isn't a fragment... */ - BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 20), - BPF_JUMP (BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0), - - /* Get the IP header length... */ - BPF_STMT (BPF_LDX + BPF_B + BPF_MSH, 14), - - /* Make sure it's to the right port... */ - BPF_STMT (BPF_LD + BPF_H + BPF_IND, 16), - BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, DHCP_CLIENT_PORT, 0, 1), - - /* If we passed all the tests, ask for the whole packet. */ - BPF_STMT (BPF_RET + BPF_K, ~0U), - - /* Otherwise, drop it. */ - BPF_STMT (BPF_RET + BPF_K, 0), -}; - -static struct bpf_insn arp_bpf_filter [] = { - /* Make sure this is an ARP packet... */ - BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 12), - BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_ARP, 0, 3), - - /* Make sure this is an ARP REPLY... */ - BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 20), - BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REPLY, 0, 1), - - /* If we passed all the tests, ask for the whole packet. */ - BPF_STMT (BPF_RET + BPF_K, ~0U), - - /* Otherwise, drop it. */ - BPF_STMT (BPF_RET + BPF_K, 0), -}; - -void setup_packet_filters (void) -{ -#ifdef __linux__ - /* We need to massage the filters for Linux cooked packets */ - dhcp_bpf_filter[1].jf = 0; /* skip the IP packet type check */ - dhcp_bpf_filter[2].k -= ETH_HLEN; - dhcp_bpf_filter[4].k -= ETH_HLEN; - dhcp_bpf_filter[6].k -= ETH_HLEN; - dhcp_bpf_filter[7].k -= ETH_HLEN; - - arp_bpf_filter[1].jf = 0; /* skip the IP packet type check */ - arp_bpf_filter[2].k -= ETH_HLEN; -#endif -} - -static uint16_t checksum (unsigned char *addr, uint16_t len) -{ - uint32_t sum = 0; - union - { - unsigned char *addr; - uint16_t *i; - } p; - uint16_t nleft = len; - - p.addr = addr; - while (nleft > 1) { - sum += *p.i++; - nleft -= 2; - } - - if (nleft == 1) { - uint8_t a = 0; - memcpy (&a, p.i, 1); - sum += ntohs (a) << 8; - } - - sum = (sum >> 16) + (sum & 0xffff); - sum += (sum >> 16); - - return ~sum; -} - -void make_dhcp_packet(struct udp_dhcp_packet *packet, - const unsigned char *data, size_t length, - struct in_addr source, struct in_addr dest) -{ - struct ip *ip = &packet->ip; - struct udphdr *udp = &packet->udp; - - /* OK, this is important :) - * We copy the data to our packet and then create a small part of the - * ip structure and an invalid ip_len (basically udp length). - * We then fill the udp structure and put the checksum - * of the whole packet into the udp checksum. - * Finally we complete the ip structure and ip checksum. - * If we don't do the ordering like so then the udp checksum will be - * broken, so find another way of doing it! */ - - memcpy (&packet->dhcp, data, length); - - ip->ip_p = IPPROTO_UDP; - ip->ip_src.s_addr = source.s_addr; - if (dest.s_addr == 0) - ip->ip_dst.s_addr = INADDR_BROADCAST; - else - ip->ip_dst.s_addr = dest.s_addr; - - udp->uh_sport = htons (DHCP_CLIENT_PORT); - udp->uh_dport = htons (DHCP_SERVER_PORT); - udp->uh_ulen = htons (sizeof (*udp) + length); - ip->ip_len = udp->uh_ulen; - udp->uh_sum = checksum ((unsigned char *) packet, sizeof (*packet)); - - ip->ip_v = IPVERSION; - ip->ip_hl = 5; - ip->ip_id = 0; - ip->ip_tos = IPTOS_LOWDELAY; - ip->ip_len = htons (sizeof (*ip) + sizeof (*udp) + length); - ip->ip_id = 0; - ip->ip_off = htons (IP_DF); /* Don't fragment */ - ip->ip_ttl = IPDEFTTL; - - ip->ip_sum = checksum ((unsigned char *) ip, sizeof (*ip)); -} - -static int valid_dhcp_packet (unsigned char *data) -{ - union - { - unsigned char *data; - struct udp_dhcp_packet *packet; - } d; - uint16_t bytes; - uint16_t ipsum; - uint16_t iplen; - uint16_t udpsum; - struct in_addr source; - struct in_addr dest; - int retval = 0; - - d.data = data; - bytes = ntohs (d.packet->ip.ip_len); - ipsum = d.packet->ip.ip_sum; - iplen = d.packet->ip.ip_len; - udpsum = d.packet->udp.uh_sum; - - d.data = data; - d.packet->ip.ip_sum = 0; - if (ipsum != checksum ((unsigned char *) &d.packet->ip, - sizeof (d.packet->ip))) - { - logger (LOG_DEBUG, "bad IP header checksum, ignoring"); - retval = -1; - goto eexit; - } - - memcpy (&source, &d.packet->ip.ip_src, sizeof (d.packet->ip.ip_src)); - memcpy (&dest, &d.packet->ip.ip_dst, sizeof (d.packet->ip.ip_dst)); - memset (&d.packet->ip, 0, sizeof (d.packet->ip)); - d.packet->udp.uh_sum = 0; - - d.packet->ip.ip_p = IPPROTO_UDP; - memcpy (&d.packet->ip.ip_src, &source, sizeof (d.packet->ip.ip_src)); - memcpy (&d.packet->ip.ip_dst, &dest, sizeof (d.packet->ip.ip_dst)); - d.packet->ip.ip_len = d.packet->udp.uh_ulen; - if (udpsum && udpsum != checksum (d.data, bytes)) { - logger (LOG_ERR, "bad UDP checksum, ignoring"); - retval = -1; - } - -eexit: - d.packet->ip.ip_sum = ipsum; - d.packet->ip.ip_len = iplen; - d.packet->udp.uh_sum = udpsum; - - return retval; -} - -#if defined(BSD) || defined(__FreeBSD_kernel__) -int open_socket (interface_t *iface, int protocol) -{ - int n = 0; - int fd = -1; - char *device; - int flags; - struct ifreq ifr; - int buf = 0; - struct bpf_program pf; - - device = xmalloc (sizeof (char) * PATH_MAX); - do { - snprintf (device, PATH_MAX, "/dev/bpf%d", n++); - fd = open (device, O_RDWR); - } while (fd == -1 && errno == EBUSY); - free (device); - - if (fd == -1) { - logger (LOG_ERR, "unable to open a BPF device"); - return -1; - } - - close_on_exec (fd); - - memset (&ifr, 0, sizeof (ifr)); - strlcpy (ifr.ifr_name, iface->name, sizeof (ifr.ifr_name)); - if (ioctl (fd, BIOCSETIF, &ifr) == -1) { - logger (LOG_ERR, - "cannot attach interface `%s' to bpf device `%s': %s", - iface->name, device, strerror (errno)); - close (fd); - return -1; - } - - /* Get the required BPF buffer length from the kernel. */ - if (ioctl (fd, BIOCGBLEN, &buf) == -1) { - logger (LOG_ERR, "ioctl BIOCGBLEN: %s", strerror (errno)); - close (fd); - return -1; - } - iface->buffer_length = buf; - - flags = 1; - if (ioctl (fd, BIOCIMMEDIATE, &flags) == -1) { - logger (LOG_ERR, "ioctl BIOCIMMEDIATE: %s", strerror (errno)); - close (fd); - return -1; - } - - /* Install the DHCP filter */ - if (protocol == ETHERTYPE_ARP) { - pf.bf_insns = arp_bpf_filter; - pf.bf_len = sizeof (arp_bpf_filter) - / sizeof (arp_bpf_filter[0]); - } else { - pf.bf_insns = dhcp_bpf_filter; - pf.bf_len = sizeof (dhcp_bpf_filter) - / sizeof (dhcp_bpf_filter[0]); - } - if (ioctl (fd, BIOCSETF, &pf) == -1) { - logger (LOG_ERR, "ioctl BIOCSETF: %s", strerror (errno)); - close (fd); - return -1; - } - - if (iface->fd > -1) - close (iface->fd); - iface->fd = fd; - - return fd; -} - -ssize_t send_packet (const interface_t *iface, int type, - const unsigned char *data, size_t len) -{ - ssize_t retval = -1; - struct iovec iov[2]; - - if (iface->family == ARPHRD_ETHER) { - struct ether_header hw; - memset (&hw, 0, sizeof (hw)); - memset (&hw.ether_dhost, 0xff, ETHER_ADDR_LEN); - hw.ether_type = htons (type); - - iov[0].iov_base = &hw; - iov[0].iov_len = sizeof (hw); - } else { - logger (LOG_ERR, "unsupported interace type %d", iface->family); - return -1; - } - iov[1].iov_base = (unsigned char *) data; - iov[1].iov_len = len; - - if ((retval = writev(iface->fd, iov, 2)) == -1) - logger (LOG_ERR, "writev: %s", strerror (errno)); - - return retval; -} - -/* BPF requires that we read the entire buffer. - * So we pass the buffer in the API so we can loop on >1 dhcp packet. */ -ssize_t get_packet (const interface_t *iface, unsigned char *data, - unsigned char *buffer, - size_t *buffer_len, size_t *buffer_pos) -{ - union - { - unsigned char *buffer; - struct bpf_hdr *packet; - } bpf; - - bpf.buffer = buffer; - - if (*buffer_pos < 1) { - memset (bpf.buffer, 0, iface->buffer_length); - *buffer_len = read (iface->fd, bpf.buffer, iface->buffer_length); - *buffer_pos = 0; - if (*buffer_len < 1) { - struct timespec ts; - logger (LOG_ERR, "read: %s", strerror (errno)); - ts.tv_sec = 3; - ts.tv_nsec = 0; - nanosleep (&ts, NULL); - return (-1); - } - } else - bpf.buffer += *buffer_pos; - - while (bpf.packet) { - size_t len = 0; - union - { - unsigned char *buffer; - struct ether_header *hw; - } hdr; - unsigned char *payload; - bool have_data = false; - - /* Ensure that the entire packet is in our buffer */ - if (*buffer_pos + bpf.packet->bh_hdrlen + bpf.packet->bh_caplen - > (unsigned) *buffer_len) - break; - - hdr.buffer = bpf.buffer + bpf.packet->bh_hdrlen; - payload = hdr.buffer + sizeof (*hdr.hw); - - /* If it's an ARP reply, then just send it back */ - if (hdr.hw->ether_type == htons (ETHERTYPE_ARP)) { - len = bpf.packet->bh_caplen - - sizeof (*hdr.hw); - memcpy (data, payload, len); - have_data = true; - } else { - if (valid_dhcp_packet (payload) >= 0) { - union - { - unsigned char *buffer; - struct udp_dhcp_packet *packet; - } pay; - pay.buffer = payload; - len = ntohs (pay.packet->ip.ip_len) - - sizeof (pay.packet->ip) - - sizeof (pay.packet->udp); - memcpy (data, &pay.packet->dhcp, len); - have_data = true; - } - } - - /* Update the buffer_pos pointer */ - bpf.buffer += BPF_WORDALIGN (bpf.packet->bh_hdrlen + - bpf.packet->bh_caplen); - if ((unsigned) (bpf.buffer - buffer) < *buffer_len) - *buffer_pos = bpf.buffer - buffer; - else - *buffer_pos = 0; - - if (have_data) - return len; - - if (*buffer_pos == 0) - break; - } - - /* No valid packets left, so return */ - *buffer_pos = 0; - return -1; -} - -#elif __linux__ - -int open_socket (interface_t *iface, int protocol) -{ - int fd; - union sockunion { - struct sockaddr sa; - struct sockaddr_in sin; - struct sockaddr_ll sll; - struct sockaddr_storage ss; - } su; - struct sock_fprog pf; - struct ifreq ifr; - int n = 1; - - /* We need to bind to a port, otherwise Linux generate ICMP messages - * that cannot contect the port when we have an address. - * We don't actually use this fd at all, instead using our packet - * filter socket. */ - if (iface->listen_fd == -1 && protocol == ETHERTYPE_IP) { - if ((fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - } else { - memset (&su, 0, sizeof (su)); - su.sin.sin_family = AF_INET; - su.sin.sin_port = htons (DHCP_CLIENT_PORT); - if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, - &n, sizeof (n)) == -1) - logger (LOG_ERR, "SO_REUSEADDR: %s", - strerror (errno)); - if (setsockopt (fd, SOL_SOCKET, SO_RCVBUF, - &n, sizeof (n)) == -1) - logger (LOG_ERR, "SO_RCVBUF: %s", - strerror (errno)); - memset (&ifr, 0, sizeof (ifr)); - strncpy (ifr.ifr_name, iface->name, - sizeof (ifr.ifr_name)); - if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE, - &ifr, sizeof (ifr)) == -1) - logger (LOG_ERR, "SO_SOBINDTODEVICE: %s", - strerror (errno)); - if (bind (fd, &su.sa, sizeof (su)) == -1) { - logger (LOG_ERR, "bind: %s", strerror (errno)); - close (fd); - } else { - iface->listen_fd = fd; - close_on_exec (fd); - } - } - } - - if ((fd = socket (PF_PACKET, SOCK_DGRAM, htons (protocol))) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return (-1); - } - close_on_exec (fd); - - memset (&su, 0, sizeof (su)); - su.sll.sll_family = PF_PACKET; - su.sll.sll_protocol = htons (protocol); - if (! (su.sll.sll_ifindex = if_nametoindex (iface->name))) { - logger (LOG_ERR, - "if_nametoindex: no index for interface `%s'", - iface->name); - close (fd); - return (-1); - } - - /* Install the DHCP filter */ - memset (&pf, 0, sizeof (pf)); - if (protocol == ETHERTYPE_ARP) { - pf.filter = arp_bpf_filter; - pf.len = sizeof (arp_bpf_filter) / sizeof (arp_bpf_filter[0]); - } else { - pf.filter = dhcp_bpf_filter; - pf.len = sizeof (dhcp_bpf_filter) / sizeof (dhcp_bpf_filter[0]); - } - if (setsockopt (fd, SOL_SOCKET, SO_ATTACH_FILTER, - &pf, sizeof (pf)) != 0) - { - logger (LOG_ERR, "SO_ATTACH_FILTER: %s", strerror (errno)); - close (fd); - return (-1); - } - - if (bind (fd, &su.sa, sizeof (su)) == -1) { - logger (LOG_ERR, "bind: %s", strerror (errno)); - close (fd); - return (-1); - } - - if (iface->fd > -1) - close (iface->fd); - iface->fd = fd; - iface->socket_protocol = protocol; - iface->buffer_length = BUFFER_LENGTH; - - return (fd); -} - -ssize_t send_packet (const interface_t *iface, int type, - const unsigned char *data, size_t len) -{ - union sockunion { - struct sockaddr sa; - struct sockaddr_ll sll; - struct sockaddr_storage ss; - } su; - ssize_t retval; - - if (! iface) - return (-1); - - memset (&su, 0, sizeof (su)); - su.sll.sll_family = AF_PACKET; - su.sll.sll_protocol = htons (type); - - if (! (su.sll.sll_ifindex = if_nametoindex (iface->name))) { - logger (LOG_ERR, "if_nametoindex: no index for interface `%s'", - iface->name); - return (-1); - } - - su.sll.sll_hatype = htons (iface->family); - su.sll.sll_halen = iface->hwlen; - if (iface->family == ARPHRD_INFINIBAND) - memcpy (&su.sll.sll_addr, - &ipv4_bcast_addr, sizeof (ipv4_bcast_addr)); - else - memset (&su.sll.sll_addr, 0xff, iface->hwlen); - - if ((retval = sendto (iface->fd, data, len, 0, &su.sa, - sizeof (su))) == -1) - - logger (LOG_ERR, "sendto: %s", strerror (errno)); - return (retval); -} - -/* Linux has no need for the buffer as we can read as much as we want. - * We only have the buffer listed to keep the same API. */ -ssize_t get_packet (const interface_t *iface, unsigned char *data, - unsigned char *buffer, - size_t *buffer_len, size_t *buffer_pos) -{ - ssize_t bytes; - union - { - unsigned char *buffer; - struct udp_dhcp_packet *packet; - } pay; - - /* We don't use the given buffer, but we need to rewind the position */ - *buffer_pos = 0; - - memset (buffer, 0, iface->buffer_length); - bytes = read (iface->fd, buffer, iface->buffer_length); - - if (bytes == -1) { - struct timespec ts; - logger (LOG_ERR, "read: %s", strerror (errno)); - ts.tv_sec = 3; - ts.tv_nsec = 0; - nanosleep (&ts, NULL); - return (-1); - } - - *buffer_len = bytes; - /* If it's an ARP reply, then just send it back */ - if (iface->socket_protocol == ETHERTYPE_ARP) { - memcpy (data, buffer, bytes); - return (bytes); - } - - if ((unsigned) bytes < (sizeof (pay.packet->ip) + - sizeof (pay.packet->udp))) - { - logger (LOG_DEBUG, "message too short, ignoring"); - return (-1); - } - - pay.buffer = buffer; - if (bytes < ntohs (pay.packet->ip.ip_len)) { - logger (LOG_DEBUG, "truncated packet, ignoring"); - return (-1); - } - - if (valid_dhcp_packet (buffer) == -1) - return (-1); - - bytes = ntohs (pay.packet->ip.ip_len) - - (sizeof (pay.packet->ip) + sizeof (pay.packet->udp)); - memcpy (data, &pay.packet->dhcp, bytes); - return (bytes); -} - -#else - #error "Platform not supported!" - #error "We currently support BPF and Linux sockets." - #error "Other platforms may work using BPF. If yours does, please let me know" - #error "so I can add it to our list." -#endif diff --git a/workspace/customdhcpcd/src/socket.h b/workspace/customdhcpcd/src/socket.h deleted file mode 100644 index bdf26d0..0000000 --- a/workspace/customdhcpcd/src/socket.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef SOCKET_H -#define SOCKET_H - -#include - -#include "dhcp.h" -#include "interface.h" - -void setup_packet_filters (void); -void make_dhcp_packet(struct udp_dhcp_packet *packet, - const unsigned char *data, size_t length, - struct in_addr source, struct in_addr dest); - -int open_socket (interface_t *iface, int protocol); -ssize_t send_packet (const interface_t *iface, int type, - const unsigned char *data, size_t len); -ssize_t get_packet (const interface_t *iface, unsigned char *data, - unsigned char *buffer, size_t *buffer_len, size_t *buffer_pos); -#endif diff --git a/workspace/customdhcpcd/src/status.h b/workspace/customdhcpcd/src/status.h deleted file mode 100644 index 0a1dc8a..0000000 --- a/workspace/customdhcpcd/src/status.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * status.h - * - * Created on: Jul 11, 2011 - * Author: niklas - */ - -#ifndef STATUS_H_ -#define STATUS_H_ - -#define DHCPCD_EXIT 9 -#define DHCPCD_ARP_TEST 10 -#define DHCPCD_CONFIGURE 11 -#define DHCPCD_WRITE 12 -#define DHCPCD_LOG 13 - - - -#endif /* STATUS_H_ */ diff --git a/workspace/networkDiscovery/.settings/org.eclipse.ltk.core.refactoring.prefs b/workspace/networkDiscovery/.settings/org.eclipse.ltk.core.refactoring.prefs deleted file mode 100644 index 0dba4ef..0000000 --- a/workspace/networkDiscovery/.settings/org.eclipse.ltk.core.refactoring.prefs +++ /dev/null @@ -1,3 +0,0 @@ -#Fri Jun 24 11:22:33 CEST 2011 -eclipse.preferences.version=1 -org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false diff --git a/workspace/networkDiscovery/dhcpcd/.gitignore b/workspace/networkDiscovery/dhcpcd/.gitignore deleted file mode 100644 index 6eb1591..0000000 --- a/workspace/networkDiscovery/dhcpcd/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -*.o -version.h -dhcpcd -dhcpcd.8 -dhcpcd-*.bz2 diff --git a/workspace/networkDiscovery/dhcpcd/README b/workspace/networkDiscovery/dhcpcd/README deleted file mode 100644 index 9089ec6..0000000 --- a/workspace/networkDiscovery/dhcpcd/README +++ /dev/null @@ -1,45 +0,0 @@ -dhcpcd-3 - DHCP client daemon -Copyright 2006-2008 Roy Marples - - -Installation ------------- -Edit config.h to match your building requirements. - -Take special note of ENABLE_DUID and unset it if the target media is -volatile, like say a LiveCD. - -Then just make; make install - -man dhcpcd for command line options - - -Notes ------ -If you're cross compiling you may need to send HAVE_FORK=yes or HAVE_FORK=no -to the make command to avoid to automatic test. - -We try and detect how to restart ntp and ypbind, you can override this with -HAVE_INIT=no or force one of these values -OPENRC (OpenRC as used by Gentoo (forked from baselayout)) -BSDRC (BSD RC system - /etc/rc.d/ntpd restart ) -SERVICE (RedHat service command - service ntpd restart) -SLACKRC (Slackware RC system - /etc/rc.d/rc.ntpd restart) -SYSV (SYSV style - /etc/init.d/ntpd restart) - -You can change the default dir where dhcpcd stores it's .info files with -INFODIR=/var/db - -We now default to using -std=c99. For 64-bit linux, this always works, but -for 32-bit linux it requires either gnu99 or a patch to asm/types.h. -Most distros patch linux headers so this should work fine. -linux-2.6.24 finally ships with a working 32-bit header. -If your linux headers are older, or your distro hasn't patched them you can -set CSTD=gnu99 to work around this. - - -ChangeLog ---------- -We no longer supply a ChangeLog. -However, you're more than welcome to read the git commit comments at -http://git.marples.name/?p=dhcpcd/.git;a=summary diff --git a/workspace/networkDiscovery/dhcpcd/arp.c b/workspace/networkDiscovery/dhcpcd/arp.c deleted file mode 100644 index 794850c..0000000 --- a/workspace/networkDiscovery/dhcpcd/arp.c +++ /dev/null @@ -1,284 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#ifdef __linux__ -#include -#include -#endif -#include -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "common.h" -#include "arp.h" -#include "interface.h" -#include "logger.h" -#include "signal.h" -#include "socket.h" - -/* These are really for IPV4LL */ -#define NPROBES 3 -#define PROBE_INTERVAL 200 -#define NCLAIMS 2 -#define CLAIM_INTERVAL 200 - -/* Linux does not seem to define these handy macros */ -#ifndef ar_sha -#define ar_sha(ap) (((caddr_t) ((ap) + 1)) + 0) -#define ar_spa(ap) (((caddr_t) ((ap) + 1)) + (ap)->ar_hln) -#define ar_tha(ap) (((caddr_t) ((ap) + 1)) + (ap)->ar_hln + (ap)->ar_pln) -#define ar_tpa(ap) (((caddr_t) ((ap) + 1)) + 2 * (ap)->ar_hln + (ap)->ar_pln) -#endif - -#ifndef arphdr_len -#define arphdr_len2(ar_hln, ar_pln) (sizeof (struct arphdr) + \ - 2 * (ar_hln) + 2 * (ar_pln)) -#define arphdr_len(ap) (arphdr_len2 ((ap)->ar_hln, (ap)->ar_pln)) -#endif - -#ifdef ENABLE_ARP - -static int send_arp (const interface_t *iface, int op, struct in_addr sip, - const unsigned char *taddr, struct in_addr tip) -{ - struct arphdr *arp; - size_t arpsize = arphdr_len2 (iface->hwlen, sizeof (sip)); - caddr_t tha; - int retval; - - arp = xzalloc (arpsize); - arp->ar_hrd = htons (iface->family); - arp->ar_pro = htons (ETHERTYPE_IP); - arp->ar_hln = iface->hwlen; - arp->ar_pln = sizeof (sip); - arp->ar_op = htons (op); - memcpy (ar_sha (arp), iface->hwaddr, (size_t) arp->ar_hln); - memcpy (ar_spa (arp), &sip, (size_t) arp->ar_pln); - if (taddr) { - /* NetBSD can return NULL from ar_tha, which is probably wrong - * but we still need to deal with it */ - if (! (tha = ar_tha (arp))) { - free (arp); - errno = EINVAL; - return (-1); - } - memcpy (tha, taddr, (size_t) arp->ar_hln); - } - memcpy (ar_tpa (arp), &tip, (size_t) arp->ar_pln); - - retval = send_packet (iface, ETHERTYPE_ARP, - (unsigned char *) arp, arphdr_len (arp)); - free (arp); - return (retval); -} - -int arp_claim (interface_t *iface, struct in_addr address) -{ - struct arphdr *reply = NULL; - long timeout = 0; - unsigned char *buffer; - int retval = -1; - int nprobes = 0; - int nclaims = 0; - struct in_addr null_address; - struct pollfd fds[] = { - { -1, POLLIN, 0 }, - { -1, POLLIN, 0 } - }; - - if (! iface) - return (-1); - - if (! iface->arpable) { - logger (LOG_DEBUG, "interface `%s' is not ARPable", iface->name); - return (0); - } - - if (! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr)) && - ! IN_LINKLOCAL (ntohl (address.s_addr))) - logger (LOG_INFO, - "checking %s is available on attached networks", - inet_ntoa (address)); - - if (! open_socket (iface, ETHERTYPE_ARP)) - return (-1); - - fds[0].fd = signal_fd (); - fds[1].fd = iface->fd; - - memset (&null_address, 0, sizeof (null_address)); - - buffer = xmalloc (iface->buffer_length); - reply = xmalloc (iface->buffer_length); - - for (;;) { - size_t bufpos = 0; - size_t buflen = iface->buffer_length; - int bytes; - int s = 0; - struct timeval stopat; - struct timeval now; - - /* Only poll if we have a timeout */ - if (timeout > 0) { - s = poll (fds, 2, timeout); - if (s == -1) { - if (errno == EINTR) { - if (signal_exists (NULL) == -1) { - errno = 0; - continue; - } else - break; - } - - logger (LOG_ERR, "poll: `%s'", - strerror (errno)); - break; - } - } - - /* Timed out */ - if (s == 0) { - if (nprobes < NPROBES) { - nprobes ++; - timeout = PROBE_INTERVAL; - logger (LOG_DEBUG, "sending ARP probe #%d", - nprobes); - if (send_arp (iface, ARPOP_REQUEST, - null_address, NULL, - address) == -1) - break; - - /* IEEE1394 cannot set ARP target address - * according to RFC2734 */ - if (nprobes >= NPROBES && - iface->family == ARPHRD_IEEE1394) - nclaims = NCLAIMS; - } else if (nclaims < NCLAIMS) { - nclaims ++; - timeout = CLAIM_INTERVAL; - logger (LOG_DEBUG, "sending ARP claim #%d", - nclaims); - if (send_arp (iface, ARPOP_REQUEST, - address, iface->hwaddr, - address) == -1) - break; - } else { - /* No replies, so done */ - retval = 0; - break; - } - - /* Setup our stop time */ - if (get_time (&stopat) != 0) - break; - stopat.tv_usec += timeout; - - continue; - } - - /* We maybe ARP flooded, so check our time */ - if (get_time (&now) != 0) - break; - if (timercmp (&now, &stopat, >)) { - timeout = 0; - continue; - } - - if (! fds[1].revents & POLLIN) - continue; - - memset (buffer, 0, buflen); - do { - union { - unsigned char *c; - struct in_addr *a; - } rp; - union { - unsigned char *c; - struct ether_addr *a; - } rh; - - memset (reply, 0, iface->buffer_length); - if ((bytes = get_packet (iface, (unsigned char *) reply, - buffer, - &buflen, &bufpos)) == -1) - break; - - /* Only these types are recognised */ - if (reply->ar_op != htons (ARPOP_REPLY)) - continue; - - /* Protocol must be IP. */ - if (reply->ar_pro != htons (ETHERTYPE_IP)) - continue; - if (reply->ar_pln != sizeof (address)) - continue; - if ((unsigned) bytes < sizeof (reply) + - 2 * (4 + reply->ar_hln)) - continue; - - rp.c = (unsigned char *) ar_spa (reply); - rh.c = (unsigned char *) ar_sha (reply); - - /* Ensure the ARP reply is for the our address */ - if (rp.a->s_addr != address.s_addr) - continue; - - /* Some systems send a reply back from our hwaddress, - * which is wierd */ - if (reply->ar_hln == iface->hwlen && - memcmp (rh.c, iface->hwaddr, iface->hwlen) == 0) - continue; - - logger (LOG_ERR, "ARPOP_REPLY received from %s (%s)", - inet_ntoa (*rp.a), - hwaddr_ntoa (rh.c, (size_t) reply->ar_hln)); - retval = -1; - goto eexit; - } while (bufpos != 0); - } - -eexit: - close (iface->fd); - iface->fd = -1; - free (buffer); - free (reply); - return (retval); -} -#endif diff --git a/workspace/networkDiscovery/dhcpcd/arp.h b/workspace/networkDiscovery/dhcpcd/arp.h deleted file mode 100644 index 3b7e8ef..0000000 --- a/workspace/networkDiscovery/dhcpcd/arp.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef ARP_H -#define ARP_H - -#ifdef ENABLE_ARP -#include - -#include "interface.h" - -int arp_claim (interface_t *iface, struct in_addr address); -#endif - -#endif diff --git a/workspace/networkDiscovery/dhcpcd/client.c b/workspace/networkDiscovery/dhcpcd/client.c deleted file mode 100644 index 8ce2721..0000000 --- a/workspace/networkDiscovery/dhcpcd/client.c +++ /dev/null @@ -1,1136 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#ifdef __linux__ -# include -#endif -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "common.h" -#ifdef ENABLE_ARP -# include "arp.h" -#endif -#include "client.h" -#include "configure.h" -#include "dhcp.h" -#include "dhcpcd.h" -#include "info.h" -#include "interface.h" -#ifdef ENABLE_IPV4LL -# include "ipv4ll.h" -#endif -#include "logger.h" -#include "signal.h" -#include "socket.h" - -#ifdef ENABLE_DUID -# include "duid.h" -#endif - -#ifdef ENABLE_INFO -# include "info.h" -#endif - -#ifdef THERE_IS_NO_FORK -# ifndef ENABLE_INFO - # error "Non MMU requires ENABLE_INFO to work" -# endif -#endif - -/* Some platforms don't define INFTIM */ -#ifndef INFTIM -# define INFTIM -1 -#endif - -/* This is out mini timeout. - * Basically we resend the last request every TIMEOUT_MINI seconds. */ -#define TIMEOUT_MINI 3 -/* Except for an infinite timeout. We keep adding TIMEOUT_MINI to - * ourself until TIMEOUT_MINI_INF is reached. */ -#define TIMEOUT_MINI_INF 60 - -#define STATE_INIT 0 -#define STATE_REQUESTING 1 -#define STATE_BOUND 2 -#define STATE_RENEWING 3 -#define STATE_REBINDING 4 -#define STATE_REBOOT 5 -#define STATE_RENEW_REQUESTED 6 -#define STATE_RELEASED 7 - -/* We should define a maximum for the NAK exponential backoff */ -#define NAKOFF_MAX 60 - -#define SOCKET_CLOSED 0 -#define SOCKET_OPEN 1 - -/* Indexes for pollfds */ -#define POLLFD_SIGNAL 0 -#define POLLFD_IFACE 1 - -typedef struct _state { - int *pidfd; - bool forked; - int state; - uint32_t xid; - dhcp_t *dhcp; - int socket; - interface_t *interface; - time_t start; - time_t last_sent; - time_t last_type; - long timeout; - time_t nakoff; - bool daemonised; - bool persistent; - unsigned char *buffer; - size_t buffer_len; - size_t buffer_pos; -} state_t; - -static pid_t daemonise (int *pidfd) -{ - pid_t pid; - sigset_t full; - sigset_t old; -#ifdef THERE_IS_NO_FORK - char **argv; - int i; -#endif - - sigfillset (&full); - sigprocmask (SIG_SETMASK, &full, &old); - -#ifndef THERE_IS_NO_FORK - logger (LOG_DEBUG, "forking to background"); - switch (pid = fork()) { - case -1: - logger (LOG_ERR, "fork: %s", strerror (errno)); - exit (EXIT_FAILURE); - /* NOT REACHED */ - case 0: - setsid (); - close_fds (); - break; - default: - /* Reset our signals as we're the parent about to exit. */ - signal_reset (); - break; - } -#else - logger (LOG_INFO, "forking to background"); - - /* We need to add --daemonise to our options */ - argv = xmalloc (sizeof (char *) * (dhcpcd_argc + 4)); - argv[0] = dhcpcd; - for (i = 1; i < dhcpcd_argc; i++) - argv[i] = dhcpcd_argv[i]; - argv[i] = (char *) "--daemonised"; - if (dhcpcd_skiproutes) { - argv[++i] = (char *) "--skiproutes"; - argv[++i] = dhcpcd_skiproutes; - } - argv[i + 1] = NULL; - - switch (pid = vfork ()) { - case -1: - logger (LOG_ERR, "vfork: %s", strerror (errno)); - _exit (EXIT_FAILURE); - case 0: - signal_reset (); - sigprocmask (SIG_SETMASK, &old, NULL); - execvp (dhcpcd, argv); - logger (LOG_ERR, "execl `%s': %s", dhcpcd, - strerror (errno)); - _exit (EXIT_FAILURE); - } - - free (argv); -#endif - - /* Done with the fd now */ - if (pid != 0) { - writepid (*pidfd, pid); - close (*pidfd); - *pidfd = -1; - - } - - sigprocmask (SIG_SETMASK, &old, NULL); - return (pid); -} - -#ifdef ENABLE_INFO -static bool get_old_lease (state_t *state, const options_t *options) -{ - interface_t *iface = state->interface; - dhcp_t *dhcp = state->dhcp; - struct timeval tv; - unsigned int offset = 0; - - if (! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr))) - logger (LOG_INFO, "trying to use old lease in `%s'", - iface->infofile); - if (! read_info (iface, dhcp)) - return (false); - - /* Vitaly important we remove the server information here */ - memset (&dhcp->serveraddress, 0, sizeof (dhcp->serveraddress)); - memset (dhcp->servername, 0, sizeof (dhcp->servername)); - -#ifdef ENABLE_ARP - /* Check that no-one is using the address */ - if ((options->dolastlease || - (IN_LINKLOCAL (ntohl (dhcp->address.s_addr)) && - (! options->doipv4ll || - arp_claim (iface, dhcp->address))))) - { - memset (&dhcp->address, 0, sizeof (dhcp->address)); - memset (&dhcp->netmask, 0, sizeof (dhcp->netmask)); - memset (&dhcp->broadcast, 0, sizeof (dhcp->broadcast)); - return (false); - } - - /* Ok, lets use this */ - if (IN_LINKLOCAL (dhcp->address.s_addr)) - return (true); -#endif - - /* Ensure that we can still use the lease */ - if (gettimeofday (&tv, NULL) == -1) { - logger (LOG_ERR, "gettimeofday: %s", strerror (errno)); - return (false); - } - - offset = tv.tv_sec - dhcp->leasedfrom; - if (dhcp->leasedfrom && - tv.tv_sec - dhcp->leasedfrom > dhcp->leasetime) - { - logger (LOG_ERR, "lease expired %u seconds ago", - offset + dhcp->leasetime); - return (false); - } - - if (dhcp->leasedfrom == 0) - offset = 0; - state->timeout = dhcp->renewaltime - offset; - iface->start_uptime = uptime (); - return (true); -} -#endif - -#ifdef THERE_IS_NO_FORK -static void remove_skiproutes (dhcp_t *dhcp, interface_t *iface) -{ - int i = -1; - route_t *route; - route_t *newroute; - - free_route (iface->previous_routes); - iface->previous_routes = NULL; - - NSTAILQ_FOREACH (route, dhcp->routes, entries) { - i++; - - /* Check that we did add this route or not */ - if (dhcpcd_skiproutes) { - char *sk = xstrdup (dhcpcd_skiproutes); - char *skp = sk; - char *token; - bool found = false; - - while ((token = strsep (&skp, ","))) { - if (isdigit (*token) && atoi (token) == i) { - found = true; - break; - } - } - free (sk); - if (found) - continue; - } - - if (! iface->previous_routes) { - iface->previous_routes = xmalloc (sizeof (*iface->previous_routes)); - STAILQ_INIT (iface->previous_routes); - } - - newroute = xmalloc (sizeof (*newroute)); - memcpy (newroute, route, sizeof (*newroute)); - STAILQ_INSERT_TAIL (iface->previous_routes, newroute, entries); - } - - /* We no longer need this argument */ - free (dhcpcd_skiproutes); - dhcpcd_skiproutes = NULL; -} -#endif - -static bool client_setup (state_t *state, const options_t *options) -{ - dhcp_t *dhcp = state->dhcp; - interface_t *iface = state->interface; - - state->state = STATE_INIT; - state->last_type = DHCP_DISCOVER; - state->nakoff = 1; - state->daemonised = options->daemonised; - state->persistent = options->persistent; - - if (options->request_address.s_addr == 0 && - (options->doinform || options->dorequest || options->daemonised)) - { -#ifdef ENABLE_INFO - if (! get_old_lease (state, options)) -#endif - { - free (dhcp); - return (false); - } - state->timeout = 0; - - if (! options->daemonised && - IN_LINKLOCAL (ntohl (dhcp->address.s_addr))) - { - logger (LOG_ERR, "cannot request a link local address"); - return (false); - } -#ifdef THERE_IS_NO_FORK - if (options->daemonised) { - state->state = STATE_BOUND; - state->timeout = dhcp->renewaltime; - iface->previous_address = dhcp->address; - iface->previous_netmask = dhcp->netmask; - remove_skiproutes (dhcp, iface); - } -#endif - - } else { - dhcp->address = options->request_address; - dhcp->netmask = options->request_netmask; - if (dhcp->netmask.s_addr == 0) - dhcp->netmask.s_addr = get_netmask (dhcp->address.s_addr); - dhcp->broadcast.s_addr = dhcp->address.s_addr | - ~dhcp->netmask.s_addr; - } - - /* Remove all existing addresses. - * After all, we ARE a DHCP client whose job it is to configure the - * interface. We only do this on start, so persistent addresses - * can be added afterwards by the user if needed. */ - if (! options->test && ! options->daemonised) { - if (! options->doinform) { - flush_addresses (iface->name); - } else { - /* The inform address HAS to be configured for it to - * work with most DHCP servers */ - if (options->doinform && - has_address (iface->name, dhcp->address) < 1) - { - add_address (iface->name, dhcp->address, - dhcp->netmask, dhcp->broadcast); - iface->previous_address = dhcp->address; - iface->previous_netmask = dhcp->netmask; - } - } - } - - if (*options->clientid) { - /* Attempt to see if the ClientID is a hardware address */ - iface->clientid_len = hwaddr_aton (NULL, options->clientid); - if (iface->clientid_len) { - iface->clientid = xmalloc (iface->clientid_len); - hwaddr_aton (iface->clientid, options->clientid); - } else { - /* Nope, so mark it as-is */ - iface->clientid_len = strlen (options->clientid) + 1; - iface->clientid = xmalloc (iface->clientid_len); - *iface->clientid = '\0'; - memcpy (iface->clientid + 1, - options->clientid, iface->clientid_len - 1); - } - } else { -#ifdef ENABLE_DUID - unsigned char *duid = NULL; - size_t duid_len = 0; - - if (options->doduid) { - duid = xmalloc (DUID_LEN); - duid_len = get_duid (duid, iface); - } - - if (duid_len > 0) { - logger (LOG_INFO, "DUID = %s", hwaddr_ntoa (duid, duid_len)); - - iface->clientid_len = duid_len + 5; - iface->clientid = xmalloc (iface->clientid_len); - *iface->clientid = 255; /* RFC 4361 */ - - /* IAID is 4 bytes, so if the iface name is 4 bytes use it */ - if (strlen (iface->name) == 4) { - memcpy (iface->clientid + 1, iface->name, 4); - } else { - /* Name isn't 4 bytes, so use the index */ - uint32_t ul = htonl (if_nametoindex (iface->name)); - memcpy (iface->clientid + 1, &ul, 4); - } - - memcpy (iface->clientid + 5, duid, duid_len); - free (duid); - } else { -#else - { -#endif - iface->clientid_len = iface->hwlen + 1; - iface->clientid = xmalloc (iface->clientid_len); - *iface->clientid = iface->family; - memcpy (iface->clientid + 1, iface->hwaddr, iface->hwlen); - } - } - - return (true); -} - -static bool do_socket (state_t *state, int mode) -{ - if (state->interface->fd >= 0) - close (state->interface->fd); -#ifdef __linux - if (mode == SOCKET_CLOSED && state->interface->listen_fd >= 0) { - close (state->interface->listen_fd); - state->interface->listen_fd = -1; - } -#endif - - state->interface->fd = -1; - if (mode == SOCKET_OPEN) - if (open_socket (state->interface, ETHERTYPE_IP) == -1) - return (false); - state->socket = mode; - return (true); -} - -static bool _send_message (state_t *state, int type, const options_t *options) -{ - ssize_t retval; - - state->last_type = type; - state->last_sent = uptime (); - retval = send_message (state->interface, state->dhcp, state->xid, - type, options); - return (retval == -1 ? false : true); -} - -static void drop_config (state_t *state, const options_t *options) -{ - if (! state->persistent) - configure (options, state->interface, state->dhcp, false); - - free_dhcp (state->dhcp); - memset (state->dhcp, 0, sizeof (*state->dhcp)); -} - -static int wait_for_packet (struct pollfd *fds, state_t *state, - const options_t *options) -{ - dhcp_t *dhcp = state->dhcp; - interface_t *iface = state->interface; - int timeout = 0; - int retval = 0; - - if (! (state->timeout > 0 || - (options->timeout == 0 && - (state->state != STATE_INIT || state->xid)))) { - /* We need to zero our signal fd, otherwise we will block - * trying to read a signal. */ - fds[POLLFD_SIGNAL].revents = 0; - return (0); - } - - fds[POLLFD_IFACE].fd = iface->fd; - - if ((options->timeout == 0 && state->xid) || - (dhcp->leasetime == (unsigned) -1 && - state->state == STATE_BOUND)) - { - logger (LOG_DEBUG, "waiting for infinity"); - while (retval == 0) { - if (iface->fd == -1) - retval = poll (fds, 1, INFTIM); - else { - /* Slow down our requests */ - if (timeout < TIMEOUT_MINI_INF) - timeout += TIMEOUT_MINI; - else if (timeout > TIMEOUT_MINI_INF) - timeout = TIMEOUT_MINI_INF; - - retval = poll (fds, 2, timeout * 1000); - if (retval == -1 && errno == EINTR) { - /* If interupted, continue as normal as - * the signal will be delivered down - * the pipe */ - retval = 0; - continue; - } - if (retval == 0) - _send_message (state, state->last_type, - options); - } - } - - return (retval); - } - - /* Resend our message if we're getting loads of packets. - * As we use BPF or LPF, we shouldn't hit this as much, but it's - * still nice to have. */ - if (iface->fd > -1 && uptime () - state->last_sent >= TIMEOUT_MINI) - _send_message (state, state->last_type, options); - - logger (LOG_DEBUG, "waiting for %ld seconds", - (unsigned long) state->timeout); - /* If we're waiting for a reply, then we re-send the last - * DHCP request periodically in-case of a bad line */ - retval = 0; - while (state->timeout > 0 && retval == 0) { - if (iface->fd == -1) - timeout = (int) state->timeout; - else { - timeout = TIMEOUT_MINI; - if (state->timeout < timeout) - timeout = (int) state->timeout; - } - timeout *= 1000; - state->start = uptime (); - retval = poll (fds, iface->fd == -1 ? 1 : 2, timeout); - state->timeout -= uptime () - state->start; - if (retval == -1 && errno == EINTR) { - /* If interupted, continue as normal as the signal - * will be delivered down the pipe */ - retval = 0; - continue; - } - if (retval == 0 && iface->fd != -1 && state->timeout > 0) - _send_message (state, state->last_type, options); - } - - return (retval); -} - -static bool handle_signal (int sig, state_t *state, const options_t *options) -{ - switch (sig) { - case SIGINT: - logger (LOG_INFO, "received SIGINT, stopping"); - return (false); - case SIGTERM: - logger (LOG_INFO, "received SIGTERM, stopping"); - return (false); - - case SIGALRM: - logger (LOG_INFO, "received SIGALRM, renewing lease"); - switch (state->state) { - case STATE_BOUND: - case STATE_RENEWING: - case STATE_REBINDING: - state->state = STATE_RENEW_REQUESTED; - break; - case STATE_RENEW_REQUESTED: - case STATE_REQUESTING: - case STATE_RELEASED: - state->state = STATE_INIT; - break; - } - state->timeout = 0; - state->xid = 0; - return (true); - - case SIGHUP: - if (state->state != STATE_BOUND && - state->state != STATE_RENEWING && - state->state != STATE_REBINDING) - { - logger (LOG_ERR, - "received SIGHUP, but we no have lease to release"); - return (false); - } - - logger (LOG_INFO, "received SIGHUP, releasing lease"); - if (! IN_LINKLOCAL (ntohl (state->dhcp->address.s_addr))) { - do_socket (state, SOCKET_OPEN); - state->xid = (uint32_t) random (); - if ((open_socket (state->interface, false)) >= 0) - _send_message (state, DHCP_RELEASE, options); - do_socket (state, SOCKET_CLOSED); - } - unlink (state->interface->infofile); - return (false); - - default: - logger (LOG_ERR, - "received signal %d, but don't know what to do with it", - sig); - } - - return (false); -} - -static int handle_timeout (state_t *state, const options_t *options) -{ - dhcp_t *dhcp = state->dhcp; - interface_t *iface = state->interface; - - /* No NAK, so reset the backoff */ - state->nakoff = 1; - - if (state->state == STATE_INIT && state->xid != 0) { - if (iface->previous_address.s_addr != 0 && - ! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr)) && - ! options->doinform) - { - logger (LOG_ERR, "lost lease"); - if (! options->persistent) - drop_config (state, options); - } else if (! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr))) - logger (LOG_ERR, "timed out"); - - do_socket (state, SOCKET_CLOSED); - free_dhcp (dhcp); - memset (dhcp, 0, sizeof (*dhcp)); - -#ifdef ENABLE_INFO - if (! options->test && - (options->doipv4ll || options->dolastlease)) - { - errno = 0; - if (! get_old_lease (state, options)) - { - if (errno == EINTR) - return (0); - if (options->dolastlease) - return (-1); - free_dhcp (dhcp); - memset (dhcp, 0, sizeof (*dhcp)); - } else if (errno == EINTR) - return (0); - } -#endif - -#ifdef ENABLE_IPV4LL - if (! options->test && options->doipv4ll && - (! dhcp->address.s_addr || - (! IN_LINKLOCAL (ntohl (dhcp->address.s_addr)) && - ! options->dolastlease))) - { - logger (LOG_INFO, "probing for an IPV4LL address"); - free_dhcp (dhcp); - memset (dhcp, 0, sizeof (*dhcp)); - if (ipv4ll_get_address (iface, dhcp) == -1) { - if (! state->daemonised) - return (-1); - - /* start over */ - state->xid = 0; - return (0); - } - state->timeout = dhcp->renewaltime; - } -#endif - -#if defined (ENABLE_INFO) || defined (ENABLE_IPV4LL) - if (dhcp->address.s_addr) { - if (! state->daemonised && - IN_LINKLOCAL (ntohl (dhcp->address.s_addr))) - logger (LOG_WARNING, "using IPV4LL address %s", - inet_ntoa (dhcp->address)); - if (configure (options, iface, dhcp, true) == -1 && - ! state->daemonised) - return (-1); - - state->state = STATE_BOUND; - if (! state->daemonised && options->daemonise) { - switch (daemonise (state->pidfd)) { - case -1: - return (-1); - case 0: - state->daemonised = true; - return (0); - default: - state->persistent = true; - state->forked = true; - return (-1); - } - } - - state->timeout = dhcp->renewaltime; - state->xid = 0; - return (0); - } -#endif - - if (! state->daemonised) - return (-1); - } - - switch (state->state) { - case STATE_INIT: - state->xid = (uint32_t) random (); - do_socket (state, SOCKET_OPEN); - state->timeout = options->timeout; - iface->start_uptime = uptime (); - if (dhcp->address.s_addr == 0) { - if (! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr))) - logger (LOG_INFO, "broadcasting for a lease"); - _send_message (state, DHCP_DISCOVER, options); - } else if (options->doinform) { - logger (LOG_INFO, "broadcasting inform for %s", - inet_ntoa (dhcp->address)); - _send_message (state, DHCP_INFORM, options); - state->state = STATE_REQUESTING; - } else { - logger (LOG_INFO, "broadcasting for a lease of %s", - inet_ntoa (dhcp->address)); - _send_message (state, DHCP_REQUEST, options); - state->state = STATE_REQUESTING; - } - - break; - case STATE_BOUND: - case STATE_RENEW_REQUESTED: - if (IN_LINKLOCAL (ntohl (dhcp->address.s_addr))) { - memset (&dhcp->address, 0, sizeof (dhcp->address)); - state->state = STATE_INIT; - state->xid = 0; - break; - } - state->state = STATE_RENEWING; - state->xid = (uint32_t) random (); - /* FALLTHROUGH */ - case STATE_RENEWING: - iface->start_uptime = uptime (); - logger (LOG_INFO, "renewing lease of %s", inet_ntoa - (dhcp->address)); - do_socket (state, SOCKET_OPEN); - _send_message (state, DHCP_REQUEST, options); - state->timeout = dhcp->rebindtime - dhcp->renewaltime; - state->state = STATE_REBINDING; - break; - case STATE_REBINDING: - logger (LOG_ERR, "lost lease, attemping to rebind"); - memset (&dhcp->address, 0, sizeof (dhcp->address)); - do_socket (state, SOCKET_OPEN); - if (state->xid == 0) - state->xid = (uint32_t) random (); - dhcp->serveraddress.s_addr = 0; - _send_message (state, DHCP_REQUEST, options); - state->timeout = dhcp->leasetime - dhcp->rebindtime; - state->state = STATE_REQUESTING; - break; - case STATE_REQUESTING: - state->state = STATE_INIT; - do_socket (state, SOCKET_CLOSED); - state->timeout = 0; - break; - - case STATE_RELEASED: - dhcp->leasetime = 0; - break; - } - - return (0); -} - - -static int handle_dhcp (state_t *state, int type, const options_t *options) -{ - struct timespec ts; - interface_t *iface = state->interface; - dhcp_t *dhcp = state->dhcp; - - /* We should restart on a NAK */ - if (type == DHCP_NAK) { - logger (LOG_INFO, "received NAK: %s", dhcp->message); - state->state = STATE_INIT; - state->timeout = 0; - state->xid = 0; - free_dhcp (dhcp); - memset (dhcp, 0, sizeof (*dhcp)); - - /* If we constantly get NAKS then we should slowly back off */ - if (state->nakoff > 0) { - logger (LOG_DEBUG, "sleeping for %ld seconds", - (long) state->nakoff); - ts.tv_sec = state->nakoff; - ts.tv_nsec = 0; - state->nakoff *= 2; - if (state->nakoff > NAKOFF_MAX) - state->nakoff = NAKOFF_MAX; - nanosleep (&ts, NULL); - } - - return (0); - } - - /* No NAK, so reset the backoff */ - state->nakoff = 1; - - if (type == DHCP_OFFER && state->state == STATE_INIT) { - char *addr = strdup (inet_ntoa (dhcp->address)); - if (dhcp->servername[0]) - logger (LOG_INFO, "offered %s from %s `%s'", - addr, inet_ntoa (dhcp->serveraddress), - dhcp->servername); - else - logger (LOG_INFO, "offered %s from %s", - addr, inet_ntoa (dhcp->serveraddress)); - free (addr); - -#ifdef ENABLE_INFO - if (options->test) { - write_info (iface, dhcp, options, false); - errno = 0; - return (-1); - } -#endif - - _send_message (state, DHCP_REQUEST, options); - state->state = STATE_REQUESTING; - - return (0); - } - - if (type == DHCP_OFFER) { - logger (LOG_INFO, "got subsequent offer of %s, ignoring ", - inet_ntoa (dhcp->address)); - return (0); - } - - /* We should only be dealing with acks */ - if (type != DHCP_ACK) { - logger (LOG_ERR, "%d not an ACK or OFFER", type); - return (0); - } - - switch (state->state) { - case STATE_RENEW_REQUESTED: - case STATE_REQUESTING: - case STATE_RENEWING: - case STATE_REBINDING: - break; - default: - logger (LOG_ERR, "wrong state %d", state->state); - } - - do_socket (state, SOCKET_CLOSED); - -#ifdef ENABLE_ARP - if (options->doarp && iface->previous_address.s_addr != - dhcp->address.s_addr) - { - errno = 0; - if (arp_claim (iface, dhcp->address)) { - do_socket (state, SOCKET_OPEN); - _send_message (state, DHCP_DECLINE, options); - do_socket (state, SOCKET_CLOSED); - - free_dhcp (dhcp); - memset (dhcp, 0, sizeof (*dhcp)); - state->xid = 0; - state->timeout = 0; - state->state = STATE_INIT; - - /* RFC 2131 says that we should wait for 10 seconds - * before doing anything else */ - logger (LOG_INFO, "sleeping for 10 seconds"); - ts.tv_sec = 10; - ts.tv_nsec = 0; - nanosleep (&ts, NULL); - return (0); - } else if (errno == EINTR) - return (0); - } -#endif - - if (options->doinform) { - if (options->request_address.s_addr != 0) - dhcp->address = options->request_address; - else - dhcp->address = iface->previous_address; - - logger (LOG_INFO, "received approval for %s", - inet_ntoa (dhcp->address)); - if (iface->previous_netmask.s_addr != dhcp->netmask.s_addr) { - add_address (iface->name, dhcp->address, - dhcp->netmask, dhcp->broadcast); - iface->previous_netmask.s_addr = dhcp->netmask.s_addr; - } - state->timeout = options->leasetime; - if (state->timeout == 0) - state->timeout = DEFAULT_LEASETIME; - state->state = STATE_INIT; - } else if (dhcp->leasetime == (unsigned) -1) { - dhcp->renewaltime = dhcp->rebindtime = dhcp->leasetime; - state->timeout = 1; /* So we wait for infinity */ - logger (LOG_INFO, "leased %s for infinity", - inet_ntoa (dhcp->address)); - state->state = STATE_BOUND; - } else { - if (! dhcp->leasetime) { - dhcp->leasetime = DEFAULT_LEASETIME; - logger(LOG_INFO, - "no lease time supplied, assuming %d seconds", - dhcp->leasetime); - } - logger (LOG_INFO, "leased %s for %u seconds", - inet_ntoa (dhcp->address), dhcp->leasetime); - - if (dhcp->rebindtime >= dhcp->leasetime) { - dhcp->rebindtime = (dhcp->leasetime * 0.875); - logger (LOG_ERR, - "rebind time greater than lease " - "time, forcing to %u seconds", - dhcp->rebindtime); - } - - if (dhcp->renewaltime > dhcp->rebindtime) { - dhcp->renewaltime = (dhcp->leasetime * 0.5); - logger (LOG_ERR, - "renewal time greater than rebind time, " - "forcing to %u seconds", - dhcp->renewaltime); - } - - if (! dhcp->renewaltime) { - dhcp->renewaltime = (dhcp->leasetime * 0.5); - logger (LOG_INFO, - "no renewal time supplied, assuming %d seconds", - dhcp->renewaltime); - } else - logger (LOG_DEBUG, "renew in %u seconds", - dhcp->renewaltime); - - if (! dhcp->rebindtime) { - dhcp->rebindtime = (dhcp->leasetime * 0.875); - logger (LOG_INFO, - "no rebind time supplied, assuming %d seconds", - dhcp->rebindtime); - } else - logger (LOG_DEBUG, "rebind in %u seconds", - dhcp->rebindtime); - - state->timeout = dhcp->renewaltime; - state->state = STATE_BOUND; - } - - state->xid = 0; - - if (configure (options, iface, dhcp, true) == -1 && - ! state->daemonised) - return (-1); - - if (! state->daemonised && options->daemonise) { - switch (daemonise (state->pidfd)) { - case 0: - state->daemonised = true; - return (0); - case -1: - return (-1); - default: - state->persistent = true; - state->forked = true; - return (-1); - } - } - - return (0); -} - -static int handle_packet (state_t *state, const options_t *options) -{ - interface_t *iface = state->interface; - bool valid = false; - int type; - struct dhcp_t *new_dhcp; - dhcpmessage_t message; - - /* Allocate our buffer space for BPF. - * We cannot do this until we have opened our socket as we don't - * know how much of a buffer we need until then. */ - if (! state->buffer) - state->buffer = xmalloc (iface->buffer_length); - state->buffer_len = iface->buffer_length; - state->buffer_pos = 0; - - /* We loop through until our buffer is empty. - * The benefit is that if we get >1 DHCP packet in our buffer and - * the first one fails for any reason, we can use the next. */ - - memset (&message, 0, sizeof (message)); - new_dhcp = xmalloc (sizeof (*new_dhcp)); - - do { - if (get_packet (iface, (unsigned char *) &message, - state->buffer, - &state->buffer_len, &state->buffer_pos) == -1) - break; - - if (state->xid != message.xid) { - logger (LOG_DEBUG, - "ignoring packet with xid 0x%x as it's not ours (0x%x)", - message.xid, state->xid); - continue; - } - - logger (LOG_DEBUG, "got a packet with xid 0x%x", message.xid); - memset (new_dhcp, 0, sizeof (*new_dhcp)); - type = parse_dhcpmessage (new_dhcp, &message); - if (type == -1) { - logger (LOG_ERR, "failed to parse packet"); - free_dhcp (new_dhcp); - /* We don't abort on this, so return zero */ - return (0); - } - - /* If we got here then the DHCP packet is valid and appears to - * be for us, so let's clear the buffer as we don't care about - * any more DHCP packets at this point. */ - valid = true; - break; - } while (state->buffer_pos != 0); - - /* No packets for us, so wait until we get one */ - if (! valid) { - free (new_dhcp); - return (0); - } - - /* new_dhcp is now our master DHCP message */ - free_dhcp (state->dhcp); - free (state->dhcp); - state->dhcp = new_dhcp; - new_dhcp = NULL; - - return (handle_dhcp (state, type, options)); -} - -int dhcp_run (const options_t *options, int *pidfd) -{ - interface_t *iface; - state_t *state = NULL; - struct pollfd fds[] = { - { -1, POLLIN, 0 }, - { -1, POLLIN, 0 } - }; - int retval = -1; - int sig; - - if (! options) - return (-1); - - iface = read_interface (options->interface, options->metric); - if (! iface) - goto eexit; - - state = xzalloc (sizeof (*state)); - state->dhcp = xzalloc (sizeof (*state->dhcp)); - state->pidfd = pidfd; - state->interface = iface; - - if (! client_setup (state, options)) - goto eexit; - - if (signal_init () == -1) - goto eexit; - if (signal_setup () == -1) - goto eexit; - - fds[POLLFD_SIGNAL].fd = signal_fd (); - - for (;;) { - retval = wait_for_packet (fds, state, options); - - /* We should always handle our signals first */ - if ((sig = (signal_read (&fds[POLLFD_SIGNAL]))) != -1) { - if (handle_signal (sig, state, options)) - retval = 0; - else - retval = -1; - } else if (retval == 0) - retval = handle_timeout (state, options); - else if (retval > 0 && - state->socket != SOCKET_CLOSED && - fds[POLLFD_IFACE].revents & POLLIN) - retval = handle_packet (state, options); - else if (retval == -1 && errno == EINTR) { - /* The interupt will be handled above */ - retval = 0; - } else { - logger (LOG_ERR, "poll: %s", strerror (errno)); - retval = -1; - } - - if (retval != 0) - break; - } - -eexit: - if (iface) { - do_socket (state, SOCKET_CLOSED); - drop_config (state, options); - free_route (iface->previous_routes); - free (iface->clientid); - free (iface); - } - - if (state) { - if (state->forked) - retval = 0; - - if (state->daemonised) - unlink (options->pidfile); - - free_dhcp (state->dhcp); - free (state->dhcp); - free (state->buffer); - free (state); - } - - return (retval); -} diff --git a/workspace/networkDiscovery/dhcpcd/client.h b/workspace/networkDiscovery/dhcpcd/client.h deleted file mode 100644 index fa6ea9b..0000000 --- a/workspace/networkDiscovery/dhcpcd/client.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef CLIENT_H -#define CLIENT_H - -#include "dhcpcd.h" - -int dhcp_run (const options_t *options, int *pidfd); - -#endif diff --git a/workspace/networkDiscovery/dhcpcd/common.c b/workspace/networkDiscovery/dhcpcd/common.c deleted file mode 100644 index 99471bc..0000000 --- a/workspace/networkDiscovery/dhcpcd/common.c +++ /dev/null @@ -1,249 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "common.h" -#include "logger.h" - -/* Handy routine to read very long lines in text files. - * This means we read the whole line and avoid any nasty buffer overflows. */ -char *get_line (FILE *fp) -{ - char *line = NULL; - char *p; - size_t len = 0; - size_t last = 0; - - if (feof (fp)) - return (NULL); - - do { - len += BUFSIZ; - line = xrealloc (line, sizeof (char) * len); - p = line + last; - memset (p, 0, BUFSIZ); - fgets (p, BUFSIZ, fp); - last += strlen (p); - } while (! feof (fp) && line[last - 1] != '\n'); - - /* Trim the trailing newline */ - if (*line && line[--last] == '\n') - line[last] = '\0'; - - return (line); -} - -/* OK, this should be in dhcpcd.c - * It's here to make dhcpcd more readable */ -#ifndef HAVE_SRANDOMDEV -void srandomdev (void) -{ - int fd; - unsigned long seed; - - fd = open ("/dev/urandom", 0); - if (fd == -1 || read (fd, &seed, sizeof (seed)) == -1) { - logger (LOG_WARNING, "Could not read from /dev/urandom: %s", - strerror (errno)); - seed = time (0); - } - if (fd >= 0) - close(fd); - - srandom (seed); -} -#endif - -/* strlcpy is nice, shame glibc does not define it */ -#ifndef HAVE_STRLCPY -size_t strlcpy (char *dst, const char *src, size_t size) -{ - const char *s = src; - size_t n = size; - - if (n && --n) - do { - if (! (*dst++ = *src++)) - break; - } while (--n); - - if (! n) { - if (size) - *dst = '\0'; - while (*src++); - } - - return (src - s - 1); -} -#endif - -/* Close our fd's */ -int close_fds (void) -{ - int fd; - - if ((fd = open ("/dev/null", O_RDWR)) == -1) { - logger (LOG_ERR, "open `/dev/null': %s", strerror (errno)); - return (-1); - } - - dup2 (fd, fileno (stdin)); - dup2 (fd, fileno (stdout)); - dup2 (fd, fileno (stderr)); - if (fd > 2) - close (fd); - return (0); -} - -int close_on_exec (int fd) -{ - int flags; - - if ((flags = fcntl (fd, F_GETFD, 0)) == -1 - || fcntl (fd, F_SETFD, flags | FD_CLOEXEC) == -1) - { - logger (LOG_ERR, "fcntl: %s", strerror (errno)); - return (-1); - } - return (0); -} - -/* Handy function to get the time. - * We only care about time advancements, not the actual time itself - * Which is why we use CLOCK_MONOTONIC, but it is not available on all - * platforms. - */ -int get_time (struct timeval *tp) -{ -#if defined(_POSIX_MONOTONIC_CLOCK) && defined(CLOCK_MONOTONIC) - struct timespec ts; - static clockid_t posix_clock; - static int posix_clock_set = 0; - - if (! posix_clock_set) { - if (sysconf (_SC_MONOTONIC_CLOCK) >= 0) - posix_clock = CLOCK_MONOTONIC; - else - posix_clock = CLOCK_REALTIME; - posix_clock_set = 1; - } - - if (clock_gettime (posix_clock, &ts) == -1) { - logger (LOG_ERR, "clock_gettime: %s", strerror (errno)); - return (-1); - } - - tp->tv_sec = ts.tv_sec; - tp->tv_usec = ts.tv_nsec / 1000; - return (0); -#else - if (gettimeofday (tp, NULL) == -1) { - logger (LOG_ERR, "gettimeofday: %s", strerror (errno)); - return (-1); - } - return (0); -#endif -} - -time_t uptime (void) -{ - struct timeval tp; - - if (get_time (&tp) == -1) - return (-1); - - return (tp.tv_sec); -} - -void writepid (int fd, pid_t pid) -{ - char spid[16]; - if (ftruncate (fd, (off_t) 0) == -1) { - logger (LOG_ERR, "ftruncate: %s", strerror (errno)); - } else { - ssize_t len; - snprintf (spid, sizeof (spid), "%u", pid); - len = pwrite (fd, spid, strlen (spid), (off_t) 0); - if (len != (ssize_t) strlen (spid)) - logger (LOG_ERR, "pwrite: %s", strerror (errno)); - } -} - -void *xmalloc (size_t s) -{ - void *value = malloc (s); - - if (value) - return (value); - - logger (LOG_ERR, "memory exhausted"); - - exit (EXIT_FAILURE); - /* NOTREACHED */ -} - -void *xzalloc (size_t s) -{ - void *value = xmalloc (s); - memset (value, 0, s); - return (value); -} - -void *xrealloc (void *ptr, size_t s) -{ - void *value = realloc (ptr, s); - - if (value) - return (value); - - logger (LOG_ERR, "memory exhausted"); - exit (EXIT_FAILURE); - /* NOTREACHED */ -} - -char *xstrdup (const char *str) -{ - char *value; - - if (! str) - return (NULL); - - if ((value = strdup (str))) - return (value); - - logger (LOG_ERR, "memory exhausted"); - exit (EXIT_FAILURE); - /* NOTREACHED */ -} diff --git a/workspace/networkDiscovery/dhcpcd/common.h b/workspace/networkDiscovery/dhcpcd/common.h deleted file mode 100644 index 46f1886..0000000 --- a/workspace/networkDiscovery/dhcpcd/common.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef COMMON_H -#define COMMON_H - -/* string.h pulls in features.h so the below define checks work */ -#include -#include -#include - -#if __GNUC__ > 2 || defined(__INTEL_COMPILER) -# define _unused __attribute__((__unused__)) -#else -# define _unused -#endif - -#define HAVE_STRLCPY -/* Only GLIBC doesn't support strlcpy */ -#ifdef __GLIBC__ -# if ! defined(__UCLIBC__) && ! defined (__dietlibc__) -# undef HAVE_STRLCPY -size_t strlcpy (char *dst, const char *src, size_t size); -# endif -#endif - -#define HAVE_SRANDOMDEV -#if defined(__GLIBC__) || defined(__NetBSD__) -# undef HAVE_SRANDOMDEV -void srandomdev (void); -#endif - -int close_fds (void); -int close_on_exec (int fd); -char *get_line (FILE *fp); -int get_time (struct timeval *tp); -time_t uptime (void); -void writepid (int fd, pid_t pid); -void *xrealloc (void *ptr, size_t size); -void *xmalloc (size_t size); -void *xzalloc (size_t size); -char *xstrdup (const char *str); - -#endif diff --git a/workspace/networkDiscovery/dhcpcd/config.h b/workspace/networkDiscovery/dhcpcd/config.h deleted file mode 100644 index 2c0991b..0000000 --- a/workspace/networkDiscovery/dhcpcd/config.h +++ /dev/null @@ -1,133 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef CONFIG_H -#define CONFIG_H - -/* You can enable/disable various chunks of optional code here. - * You would only do this to try and shrink the end binary if dhcpcd - * was running on a low memory device */ - -#define ENABLE_ARP -#define ENABLE_NTP -#define ENABLE_NIS -#define ENABLE_INFO -/* Define this to enable some compatability with 1.x and 2.x info files */ -/* #define ENABLE_INFO_COMPAT */ - -/* IPV4LL, aka ZeroConf, aka APIPA, aka RFC 3927. - * Needs ARP. */ -#define ENABLE_IPV4LL - -/* We will auto create a DUID_LLT file if it doesn't exist. - * You can always create your own DUID file that just contains the - * hex string that represents the DUID. - * See RFC 3315 for details on this. */ -#define ENABLE_DUID - -/* resolvconf is framework for multiple interfaces to manage resolv.conf */ -#define ENABLE_RESOLVCONF - -/* Some systems do not have a working fork. - * The Makefile will attempt to work it out, but if it fails to feel free to - * define it here. */ -/* #define THERE_IS_NO_FORK */ - -/* Packname name and pathname definitions. */ - -#define PACKAGE "dhcpcd" - -#define ETCDIR "/etc" -#define RESOLVFILE ETCDIR "/resolv.conf" - -#define NISFILE ETCDIR "/yp.conf" - -#define NTPFILE ETCDIR "/ntp.conf" -#define NTPDRIFTFILE ETCDIR "/ntp.drift" -#define NTPLOGFILE "/var/log/ntp.log" - -#define OPENNTPFILE ETCDIR "/ntpd.conf" - -#define DEFAULT_SCRIPT ETCDIR "/" PACKAGE ".sh" - -#define STATEDIR "/var" -#define PIDFILE STATEDIR "/run/" PACKAGE "-%s.pid" - -#ifndef INFODIR -# define INFODIR "/var/lib/dhcpcd" -#endif -#define INFOFILE INFODIR "/" PACKAGE "-%s.info" -#define DUIDFILE INFODIR "/" PACKAGE ".duid" - -/* OPENRC is Open Run Control, forked from Gentoo's baselayout package - * BSDRC is BSD style Run Control - * SLACKRC is Slackware Run Control - * SERVICE is RedHat service command - * SYSV should cover everthing else */ -#ifdef ENABLE_OPENRC -# define SERVICE "OPENRC" -# define NISSERVICE ETCDIR "/init.d/ypbind" -# define NISRESTARTARGS "--nodeps", "--quiet", "conditionalrestart" -# define NTPSERVICE ETCDIR "/init.d/ntpd" -# define NTPRESTARTARGS "--nodeps", "--quiet", "conditionalrestart" -#endif -#if ENABLE_BSDRC -# define SERVICE "BSDRC" -# define NISSERVICE ETCDIR "/rc.d/ypbind" -# define NISRESTARTARGS "restart" -# define NTPSERVICE ETCDIR "/rc.d/ntpd" -# define NTPRESTARTARGS "restart" -#endif -#if ENABLE_SLACKRC -# define SERVICE "SLACKRC" -# define NISSERVICE ETCDIR "/rc.d/rc.ypbind" -# define NISRESTARTARGS "restart" -# define NTPSERVICE ETCDIR "/rc.d/rc.ntpd" -# define NTPRESTARTARGS "restart" -#endif -#if ENABLE_SERVICE -# define SERVICE "SERVICE" -# define NISSERVICE "service" -# define NISRESTARTARGS "ypbind", "restart" -# define NTPSERVICE "service" -# define NTPRESTARTARGS "ntpd", "restart" -#endif -#if ENABLE_SYSV -# define SERVICE "SYSV" -# define NISSERVICE ETCDIR "/init.d/ypbind" -# define NISRESTARTARGS "restart" -# define NTPSERVICE ETCDIR "/init.d/ntpd" -# define NTPRESTARTARGS "restart" -#endif - -#ifndef NISSERVICE -# undef ENABLE_NIS -#endif -#ifndef NTPSERVICE -# undef ENABLE_NTP -#endif - -#endif diff --git a/workspace/networkDiscovery/dhcpcd/configure.c b/workspace/networkDiscovery/dhcpcd/configure.c deleted file mode 100644 index 0969f73..0000000 --- a/workspace/networkDiscovery/dhcpcd/configure.c +++ /dev/null @@ -1,809 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include - -#include - -#include -#ifdef __linux__ -# include -#endif -#include -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "common.h" -#include "configure.h" -#include "dhcp.h" -#ifdef ENABLE_INFO -# include "info.h" -#endif -#include "interface.h" -#include "dhcpcd.h" -#include "logger.h" -#include "signal.h" -#include "socket.h" - -static int file_in_path (const char *file) -{ - char *p = getenv ("PATH"); - char *path; - char *token; - struct stat s; - char mypath[PATH_MAX]; - int retval = -1; - - if (! p) { - errno = ENOENT; - return (-1); - } - - path = strdup (p); - p = path; - while ((token = strsep (&p, ":"))) { - snprintf (mypath, PATH_MAX, "%s/%s", token, file); - if (stat (mypath, &s) == 0) { - retval = 0; - break; - } - } - free (path); - return (retval); -} - -/* IMPORTANT: Ensure that the last parameter is NULL when calling */ -static int exec_cmd (const char *cmd, const char *args, ...) -{ - va_list va; - char **argv; - int n = 1; - int ret = 0; - pid_t pid; - sigset_t full; - sigset_t old; - - va_start (va, args); - while (va_arg (va, char *) != NULL) - n++; - va_end (va); - argv = xmalloc (sizeof (char *) * (n + 2)); - - va_start (va, args); - n = 2; - argv[0] = (char *) cmd; - argv[1] = (char *) args; - while ((argv[n] = va_arg (va, char *)) != NULL) - n++; - va_end (va); - - /* OK, we need to block signals */ - sigfillset (&full); - sigprocmask (SIG_SETMASK, &full, &old); - -#ifdef THERE_IS_NO_FORK - signal_reset (); - pid = vfork (); -#else - pid = fork(); -#endif - - switch (pid) { - case -1: - logger (LOG_ERR, "vfork: %s", strerror (errno)); - ret = -1; - break; - case 0: -#ifndef THERE_IS_NO_FORK - signal_reset (); -#endif - sigprocmask (SIG_SETMASK, &old, NULL); - if (execvp (cmd, argv) && errno != ENOENT) - logger (LOG_ERR, "error executing \"%s\": %s", - cmd, strerror (errno)); - _exit (111); - /* NOTREACHED */ - } - -#ifdef THERE_IS_NO_FORK - signal_setup (); -#endif - - /* Restore our signals */ - sigprocmask (SIG_SETMASK, &old, NULL); - - free (argv); - return (ret); -} - -static void exec_script (const char *script, const char *infofile, - const char *arg) -{ - struct stat buf; - - if (! script || ! infofile || ! arg) - return; - - if (stat (script, &buf) == -1) { - if (strcmp (script, DEFAULT_SCRIPT) != 0) - logger (LOG_ERR, "`%s': %s", script, strerror (ENOENT)); - return; - } - -#ifdef ENABLE_INFO - logger (LOG_DEBUG, "exec \"%s\" \"%s\" \"%s\"", script, infofile, arg); - exec_cmd (script, infofile, arg, (char *) NULL); -#else - logger (LOG_DEBUG, "exec \"%s\" \"\" \"%s\"", script, arg); - exec_cmd (script, "", arg, (char *) NULL); -#endif -} - -static int make_resolv (const char *ifname, const dhcp_t *dhcp) -{ - FILE *f = NULL; - address_t *address; - -#ifdef ENABLE_RESOLVCONF - char *resolvconf = NULL; - - if (file_in_path ("resolvconf") == 0) { - size_t len = strlen ("resolvconf -a ") + strlen (ifname) + 1; - resolvconf = xmalloc (sizeof (char) * len); - snprintf (resolvconf, len, "resolvconf -a %s", ifname); - if ((f = popen (resolvconf , "w"))) - logger (LOG_DEBUG, - "sending DNS information to resolvconf"); - else if (errno == EEXIST) - logger (LOG_ERR, "popen: %s", strerror (errno)); - - if (ferror (f)) - logger (LOG_ERR, "ferror"); - free (resolvconf); - } -#endif - if (! f) { - logger (LOG_DEBUG, "writing "RESOLVFILE); - if (! (f = fopen(RESOLVFILE, "w"))) - logger (LOG_ERR, "fopen `%s': %s", RESOLVFILE, strerror (errno)); - } - - if (! f) - return (-1); - - fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname); - if (dhcp->dnssearch) - fprintf (f, "search %s\n", dhcp->dnssearch); - else if (dhcp->dnsdomain) { - fprintf (f, "search %s\n", dhcp->dnsdomain); - } - - STAILQ_FOREACH (address, dhcp->dnsservers, entries) - fprintf (f, "nameserver %s\n", inet_ntoa (address->address)); - -#ifdef ENABLE_RESOLVCONF - if (resolvconf) - pclose (f); - else -#endif - fclose (f); - - /* Refresh the local resolver */ - res_init (); - return (0); -} - -static void restore_resolv (const char *ifname) -{ -#ifdef ENABLE_RESOLVCONF - if (file_in_path ("resolvconf") == 0) { - logger (LOG_DEBUG, "removing information from resolvconf"); - exec_cmd("resolvconf", "-d", ifname, (char *) NULL); - } -#endif -} - -static bool in_addresses (const struct address_head *addresses, - struct in_addr address) -{ - const address_t *addr; - - STAILQ_FOREACH (addr, addresses, entries) - if (addr->address.s_addr == address.s_addr) - return (true); - - return (false); -} - -static bool in_routes (const struct route_head *routes, route_t *route) -{ - const route_t *r; - - if (! routes) - return (false); - - STAILQ_FOREACH (r, routes, entries) - if (r->destination.s_addr == route->destination.s_addr && - r->netmask.s_addr == route->netmask.s_addr && - r->gateway.s_addr == route->gateway.s_addr) - return (true); - - return (false); -} - -#ifdef ENABLE_NTP -static int _make_ntp (const char *file, const char *ifname, const dhcp_t *dhcp) -{ - FILE *f; - address_t *address; - char *a; - char *line; - int tomatch = 0; - char *token; - bool ntp = false; - - STAILQ_FOREACH (address, dhcp->ntpservers, entries) - tomatch++; - - /* Check that we really need to update the servers. - * We do this because ntp has to be restarted to - * work with a changed config. */ - if (! (f = fopen (file, "r"))) { - if (errno != ENOENT) { - logger (LOG_ERR, "fopen `%s': %s", - file, strerror (errno)); - return (-1); - } - } else { - while (tomatch != 0 && (line = get_line (f))) { - struct in_addr addr; - - a = line; - token = strsep (&a, " "); - if (! token || strcmp (token, "server") != 0) - goto next; - - if ((token = strsep (&a, " \n")) == NULL) - goto next; - - if (inet_aton (token, &addr) == 1 && - in_addresses (dhcp->ntpservers, addr)) - tomatch--; - -next: - free (line); - } - fclose (f); - - /* File has the same name servers that we do, - * so no need to restart ntp */ - if (tomatch == 0) { - logger (LOG_DEBUG, "%s already configured, skipping", - file); - return (0); - } - } - - logger (LOG_DEBUG, "writing %s", file); - if (! (f = fopen (file, "w"))) { - logger (LOG_ERR, "fopen `%s': %s", file, strerror (errno)); - return (-1); - } - - fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname); -#ifdef NTPFILE - if (strcmp (file, NTPFILE) == 0) { - ntp = true; - fprintf (f, "restrict default noquery notrust nomodify\n"); - fprintf (f, "restrict 127.0.0.1\n"); - } -#endif - - STAILQ_FOREACH (address, dhcp->ntpservers, entries) { - a = inet_ntoa (address->address); - if (ntp) - fprintf (f, "restrict %s nomodify notrap noquery\n", a); - fprintf (f, "server %s\n", a); - } - fclose (f); - - return (1); -} - -static int make_ntp (const char *ifname, const dhcp_t *dhcp) -{ - /* On some systems we have only have one ntp service, but we don't - * know which configuration file we're using. So we need to write - * to both and restart accordingly. */ - - bool restart_ntp = false; - bool restart_openntp = false; - int retval = 0; - -#ifdef NTPFILE - if (_make_ntp (NTPFILE, ifname, dhcp) > 0) - restart_ntp = true; -#endif - -#ifdef OPENNTPFILE - if (_make_ntp (OPENNTPFILE, ifname, dhcp) > 0) - restart_openntp = true; -#endif - -#ifdef NTPSERVICE - if (restart_ntp) { -#ifdef NTPCHECK - if (system (NTPCHECK) == 0) -#endif - retval += exec_cmd (NTPSERVICE, NTPRESTARTARGS, - (char *) NULL); - } -#endif - -#if defined (NTPSERVICE) && defined (OPENNTPSERVICE) - if (restart_openntp && - (strcmp (NTPSERVICE, OPENNTPSERVICE) != 0 || ! restart_ntp)) - { -#ifdef OPENNTPCHECK - if (system (OPENNTPCHECK) == 0) -#endif - retval += exec_cmd (OPENNTPSERVICE, - OPENNTPRESTARTARGS, (char *) NULL); - } -#elif defined (OPENNTPSERVICE) && ! defined (NTPSERVICE) - if (restart_openntp) { -#ifdef OPENNTPCHECK - if (system (OPENNTPCHECK) == 0) -#endif - retval += exec_cmd (OPENNTPSERVICE, - OPENNTPRESTARTARGS, (char *) NULL); - } -#endif - - return (retval); -} -#endif - -#ifdef ENABLE_NIS -#define PREFIXSIZE 256 -static int make_nis (const char *ifname, const dhcp_t *dhcp) -{ - FILE *f; - address_t *address; - char *prefix; - - logger (LOG_DEBUG, "writing "NISFILE); - if (! (f = fopen(NISFILE, "w"))) { - logger (LOG_ERR, "fopen `%s': %s", NISFILE, strerror (errno)); - return (-1); - } - - prefix = xmalloc (sizeof (char) * PREFIXSIZE); - *prefix = '\0'; - fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname); - - if (dhcp->nisdomain) { - setdomainname (dhcp->nisdomain, (int) strlen (dhcp->nisdomain)); - - if (dhcp->nisservers) - snprintf (prefix, PREFIXSIZE, "domain %s server", - dhcp->nisdomain); - else - fprintf (f, "domain %s broadcast\n", dhcp->nisdomain); - } - else - snprintf (prefix, PREFIXSIZE, "%s", "ypserver"); - - NSTAILQ_FOREACH (address, dhcp->nisservers, entries) - fprintf (f, "%s %s\n", prefix, inet_ntoa (address->address)); - - free (prefix); - fclose (f); - -#ifdef NISCHECK - if (system (NISCHECK) == 0) -#endif - exec_cmd (NISSERVICE, NISRESTARTARGS, (char *) NULL); - return (0); -} -#endif - -static char *lookuphostname (char *hostname, const dhcp_t *dhcp, - const options_t *options) -{ - union { - struct sockaddr sa; - struct sockaddr_in sin; - } su; - socklen_t salen; - char *addr; - struct addrinfo hints; - struct addrinfo *res = NULL; - int result; - char *p; - - logger (LOG_DEBUG, "Looking up hostname via DNS"); - addr = xmalloc (sizeof (char) * NI_MAXHOST); - salen = sizeof (su.sa); - memset (&su.sa, 0, salen); - su.sin.sin_family = AF_INET; - memcpy (&su.sin.sin_addr, &dhcp->address, sizeof (su.sin.sin_addr)); - - if ((result = getnameinfo (&su.sa, salen, addr, NI_MAXHOST, - NULL, 0, NI_NAMEREQD)) != 0) { - logger (LOG_ERR, - "Failed to lookup hostname via DNS: %s", - gai_strerror (result)); - free (addr); - return (NULL); - } - - /* Check for a malicious PTR record */ - memset (&hints, 0, sizeof (hints)); - hints.ai_socktype = SOCK_DGRAM; - hints.ai_flags = AI_NUMERICHOST; - result = getaddrinfo (addr, "0", &hints, &res); - if (res) - freeaddrinfo (res); - if (result == 0) - logger (LOG_ERR, "malicious PTR record detected"); - if (result == 0 || ! *addr) { - free (addr); - return (NULL); - } - - p = strchr (addr, '.'); - if (p) { - switch (options->dohostname) { - case 1: /* -H */ - case 4: /* -HHHH */ - break; - case 2: /* -HH */ - case 5: /* -HHHHH */ - /* Strip out the domain if it matches */ - p++; - if (*p && dhcp->dnssearch) { - char *s = xstrdup (dhcp->dnssearch); - char *sp = s; - char *t; - - while ((t = strsep (&sp, " "))) - if (strcmp (t, p) == 0) { - *--p = '\0'; - break; - } - free (s); - } else if (dhcp->dnsdomain) { - if (strcmp (dhcp->dnsdomain, p) == 0) - *--p = '\0'; - } - break; - case 3: /* -HHH */ - case 6: /* -HHHHHH */ - /* Just strip the domain */ - *p = '\0'; - break; - default: /* Too many H! */ - break; - } - } - - strlcpy (hostname, addr, MAXHOSTNAMELEN); - free (addr); - return (hostname); -} - -int configure (const options_t *options, interface_t *iface, - const dhcp_t *dhcp, bool up) -{ - route_t *route = NULL; - struct route_head *new_routes = NULL; - route_t *new_route = NULL; - char *newhostname = NULL; - char *curhostname = NULL; - int remember; -#ifdef ENABLE_IPV4LL - bool haslinklocal = false; -#endif -#ifdef THERE_IS_NO_FORK - int skip = 0; - size_t skiplen; - char *skipp; -#endif - - if (! options || ! iface || ! dhcp) - return (-1); - - if (dhcp->address.s_addr == 0) - up = 0; - - /* Remove old routes. - * Always do this as the interface may have >1 address not added by us - * so the routes we added may still exist. */ - NSTAILQ_FOREACH (route, iface->previous_routes, entries) - if ((route->destination.s_addr || options->dogateway) && - (! up || ! in_routes (dhcp->routes, route))) - del_route (iface->name, route->destination, - route->netmask, route->gateway, - options->metric); - /* If we aren't up, then reset the interface as much as we can */ - if (! up) { - if (iface->previous_routes) { - free_route (iface->previous_routes); - iface->previous_routes = NULL; - } - - /* Restore the original MTU value */ - if (iface->mtu && iface->previous_mtu != iface->mtu) { - set_mtu (iface->name, iface->mtu); - iface->previous_mtu = iface->mtu; - } - -#ifdef ENABLE_INFO - /* If we haven't created an info file, do so now */ - if (! dhcp->frominfo) - write_info (iface, dhcp, options, false); -#endif - - /* Only reset things if we had set them before */ - if (iface->previous_address.s_addr != 0) { - if (! options->keep_address) { - del_address (iface->name, - iface->previous_address, - iface->previous_netmask); - memset (&iface->previous_address, - 0, sizeof (iface->previous_address)); - memset (&iface->previous_netmask, - 0, sizeof (iface->previous_netmask)); - } - } - - restore_resolv (iface->name); - exec_script (options->script, iface->infofile, "down"); - - return (0); - } - - /* Set the MTU requested. - * If the DHCP server no longer sends one OR it's invalid then - * we restore the original MTU */ - if (options->domtu) { - unsigned short mtu = iface->mtu; - if (dhcp->mtu) - mtu = dhcp->mtu; - - if (mtu != iface->previous_mtu) { - if (set_mtu (iface->name, mtu) == 0) - iface->previous_mtu = mtu; - } - } - - /* This also changes netmask */ - if (! options->doinform || ! has_address (iface->name, dhcp->address)) - if (add_address (iface->name, dhcp->address, dhcp->netmask, - dhcp->broadcast) == -1 && errno != EEXIST) - return (false); - - /* Now delete the old address if different */ - if (iface->previous_address.s_addr != dhcp->address.s_addr && - iface->previous_address.s_addr != 0 && - ! options->keep_address) - del_address (iface->name, - iface->previous_address, iface->previous_netmask); - -#ifdef __linux__ - /* On linux, we need to change the subnet route to have our metric. */ - if (iface->previous_address.s_addr != dhcp->address.s_addr && - options->metric > 0 && - dhcp->netmask.s_addr != INADDR_BROADCAST) - { - struct in_addr td; - struct in_addr tg; - memset (&td, 0, sizeof (td)); - memset (&tg, 0, sizeof (tg)); - td.s_addr = dhcp->address.s_addr & dhcp->netmask.s_addr; - add_route (iface->name, td, dhcp->netmask, tg, options->metric); - del_route (iface->name, td, dhcp->netmask, tg, 0); - } -#endif - -#ifdef THERE_IS_NO_FORK - free (dhcpcd_skiproutes); - /* We can never have more than 255 routes. So we need space - * for 255 3 digit numbers and commas */ - skiplen = 255 * 4 + 1; - skipp = dhcpcd_skiproutes = xmalloc (sizeof (char) * skiplen); - *skipp = '\0'; -#endif - - /* Remember added routes */ - NSTAILQ_FOREACH (route, dhcp->routes, entries) { -#ifdef ENABLE_IPV4LL - /* Check if we have already got a link locale route dished - * out by the DHCP server */ - if (route->destination.s_addr == htonl (LINKLOCAL_ADDR) && - route->netmask.s_addr == htonl (LINKLOCAL_MASK)) - haslinklocal = true; -#endif - /* Don't set default routes if not asked to */ - if (route->destination.s_addr == 0 && - route->netmask.s_addr == 0 && - ! options->dogateway) - continue; - - remember = add_route (iface->name, route->destination, - route->netmask, route->gateway, - options->metric); - /* If we failed to add the route, we may have already added it - ourselves. If so, remember it again. */ - if (remember < 0 && in_routes (iface->previous_routes, route)) - remember = 1; - - if (remember >= 0) { - if (! new_routes) { - new_routes = xmalloc (sizeof (*new_routes)); - STAILQ_INIT (new_routes); - } - new_route = xmalloc (sizeof (route_t)); - memcpy (new_route, route, sizeof (*new_route)); - STAILQ_INSERT_TAIL (new_routes, new_route, entries); - } -#ifdef THERE_IS_NO_FORK - /* If we have daemonised yet we need to record which routes - * we failed to add so we can skip them */ - else if (! options->daemonised) { - /* We can never have more than 255 / 4 routes, - * so 3 chars is plently */ - if (*skipp) - *skipp++ = ','; - skipp += snprintf (skipp, - dhcpcd_skiproutes + skiplen - skipp, - "%d", skip); - } - skip++; -#endif - } - -#ifdef THERE_IS_NO_FORK - if (*dhcpcd_skiproutes) - *skipp = '\0'; - else { - free (dhcpcd_skiproutes); - dhcpcd_skiproutes = NULL; - } -#endif - -#ifdef ENABLE_IPV4LL - /* Ensure we always add the link local route if we got a private - * address and isn't link local itself */ - if (options->doipv4ll && - ! haslinklocal && - IN_PRIVATE (ntohl (dhcp->address.s_addr))) - { - struct in_addr dest; - struct in_addr mask; - struct in_addr gate; - - dest.s_addr = htonl (LINKLOCAL_ADDR); - mask.s_addr = htonl (LINKLOCAL_MASK); - gate.s_addr = 0; - remember = add_route (iface->name, dest, mask, gate, - options->metric); - - if (remember >= 0) { - if (! new_routes) { - new_routes = xmalloc (sizeof (*new_routes)); - STAILQ_INIT (new_routes); - } - new_route = xmalloc (sizeof (*new_route)); - new_route->destination.s_addr = dest.s_addr; - new_route->netmask.s_addr = mask.s_addr; - new_route->gateway.s_addr = gate.s_addr; - STAILQ_INSERT_TAIL (new_routes, new_route, entries); - } - } -#endif - - if (iface->previous_routes) - free_route (iface->previous_routes); - iface->previous_routes = new_routes; - - if (options->dodns && dhcp->dnsservers) - make_resolv(iface->name, dhcp); - else - logger (LOG_DEBUG, "no dns information to write"); - -#ifdef ENABLE_NTP - if (options->dontp && dhcp->ntpservers) - make_ntp(iface->name, dhcp); -#endif - -#ifdef ENABLE_NIS - if (options->donis && (dhcp->nisservers || dhcp->nisdomain)) - make_nis(iface->name, dhcp); -#endif - - curhostname = xmalloc (sizeof (char) * MAXHOSTNAMELEN); - *curhostname = '\0'; - - gethostname (curhostname, MAXHOSTNAMELEN); - if (options->dohostname || - strlen (curhostname) == 0 || - strcmp (curhostname, "(none)") == 0 || - strcmp (curhostname, "localhost") == 0) - { - newhostname = xmalloc (sizeof (char) * MAXHOSTNAMELEN); - - if (dhcp->hostname) - strlcpy (newhostname, dhcp->hostname, MAXHOSTNAMELEN); - else - *newhostname = '\0'; - - /* Now we have made a resolv.conf we can obtain a hostname - * if we need it */ - if (! *newhostname || options->dohostname > 3) - lookuphostname (newhostname, dhcp, options); - - if (*newhostname) { - logger (LOG_INFO, "setting hostname to `%s'", - newhostname); - sethostname (newhostname, (int) strlen (newhostname)); - } - - free (newhostname); - } - - free (curhostname); - -#ifdef ENABLE_INFO - if (! dhcp->frominfo) - write_info (iface, dhcp, options, true); -#endif - - if (iface->previous_address.s_addr != dhcp->address.s_addr || - iface->previous_netmask.s_addr != dhcp->netmask.s_addr) - { - memcpy (&iface->previous_address, - &dhcp->address, sizeof (iface->previous_address)); - memcpy (&iface->previous_netmask, - &dhcp->netmask, sizeof (iface->previous_netmask)); - exec_script (options->script, iface->infofile, "new"); - } else - exec_script (options->script, iface->infofile, "up"); - - return (0); -} diff --git a/workspace/networkDiscovery/dhcpcd/configure.h b/workspace/networkDiscovery/dhcpcd/configure.h deleted file mode 100644 index 3166947..0000000 --- a/workspace/networkDiscovery/dhcpcd/configure.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef DHCPCONFIG_H -#define DHCPCONFIG_H - -#include "dhcpcd.h" -#include "interface.h" -#include "dhcp.h" - -int configure (const options_t *options, interface_t *iface, - const dhcp_t *dhcp, bool up); - -#endif diff --git a/workspace/networkDiscovery/dhcpcd/dhcp.c b/workspace/networkDiscovery/dhcpcd/dhcp.c deleted file mode 100644 index f625e8f..0000000 --- a/workspace/networkDiscovery/dhcpcd/dhcp.c +++ /dev/null @@ -1,933 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include - -#include -#include - -#include - -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" - -#include "common.h" -#include "dhcpcd.h" -#include "dhcp.h" -#include "interface.h" -#include "logger.h" -#include "socket.h" - -#ifndef STAILQ_CONCAT -#define STAILQ_CONCAT(head1, head2) do { \ - if (!STAILQ_EMPTY((head2))) { \ - *(head1)->stqh_last = (head2)->stqh_first; \ - (head1)->stqh_last = (head2)->stqh_last; \ - STAILQ_INIT((head2)); \ - } \ -} while (0) -#endif - -typedef struct message { - int value; - const char *name; -} dhcp_message_t; - -static dhcp_message_t dhcp_messages[] = { - { DHCP_DISCOVER, "DHCP_DISCOVER" }, - { DHCP_OFFER, "DHCP_OFFER" }, - { DHCP_REQUEST, "DHCP_REQUEST" }, - { DHCP_DECLINE, "DHCP_DECLINE" }, - { DHCP_ACK, "DHCP_ACK" }, - { DHCP_NAK, "DHCP_NAK" }, - { DHCP_RELEASE, "DHCP_RELEASE" }, - { DHCP_INFORM, "DHCP_INFORM" }, - { -1, NULL } -}; - -static const char *dhcp_message (int type) -{ - dhcp_message_t *d; - for (d = dhcp_messages; d->name; d++) - if (d->value == type) - return (d->name); - - return (NULL); -} - -ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, - uint32_t xid, char type, const options_t *options) -{ - struct udp_dhcp_packet *packet; - dhcpmessage_t *message; - unsigned char *m; - unsigned char *p; - unsigned char *n_params = NULL; - size_t l; - struct in_addr from; - struct in_addr to; - time_t up = uptime() - iface->start_uptime; - uint32_t ul; - uint16_t sz; - size_t message_length; - ssize_t retval; - - if (!iface || !options || !dhcp) - return -1; - - memset (&from, 0, sizeof (from)); - memset (&to, 0, sizeof (to)); - - if (type == DHCP_RELEASE) - to.s_addr = dhcp->serveraddress.s_addr; - - message = xzalloc (sizeof (*message)); - m = (unsigned char *) message; - p = (unsigned char *) &message->options; - - if ((type == DHCP_INFORM || - type == DHCP_RELEASE || - type == DHCP_REQUEST) && - ! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr))) - { - message->ciaddr = iface->previous_address.s_addr; - from.s_addr = iface->previous_address.s_addr; - - /* Just incase we haven't actually configured the address yet */ - if (type == DHCP_INFORM && iface->previous_address.s_addr == 0) - message->ciaddr = dhcp->address.s_addr; - - /* Zero the address if we're currently on a different subnet */ - if (type == DHCP_REQUEST && - iface->previous_netmask.s_addr != dhcp->netmask.s_addr) - message->ciaddr = from.s_addr = 0; - - if (from.s_addr != 0) - to.s_addr = dhcp->serveraddress.s_addr; - } - - message->op = DHCP_BOOTREQUEST; - message->hwtype = iface->family; - switch (iface->family) { - case ARPHRD_ETHER: - case ARPHRD_IEEE802: - message->hwlen = ETHER_ADDR_LEN; - memcpy (&message->chaddr, &iface->hwaddr, - ETHER_ADDR_LEN); - break; - case ARPHRD_IEEE1394: - case ARPHRD_INFINIBAND: - message->hwlen = 0; - if (message->ciaddr == 0) - message->flags = htons (BROADCAST_FLAG); - break; - default: - logger (LOG_ERR, "dhcp: unknown hardware type %d", - iface->family); - } - - if (up < 0 || up > (time_t) UINT16_MAX) - message->secs = htons ((uint16_t) UINT16_MAX); - else - message->secs = htons (up); - message->xid = xid; - message->cookie = htonl (MAGIC_COOKIE); - - *p++ = DHCP_MESSAGETYPE; - *p++ = 1; - *p++ = type; - - if (type == DHCP_REQUEST) { - *p++ = DHCP_MAXMESSAGESIZE; - *p++ = 2; - sz = get_mtu (iface->name); - if (sz < MTU_MIN) { - if (set_mtu (iface->name, MTU_MIN) == 0) - sz = MTU_MIN; - } - sz = htons (sz); - memcpy (p, &sz, 2); - p += 2; - } - - *p++ = DHCP_CLIENTID; - *p++ = iface->clientid_len; - memcpy (p, iface->clientid, iface->clientid_len); - p+= iface->clientid_len; - - if (type != DHCP_DECLINE && type != DHCP_RELEASE) { - if (options->userclass_len > 0) { - *p++ = DHCP_USERCLASS; - *p++ = options->userclass_len; - memcpy (p, &options->userclass, options->userclass_len); - p += options->userclass_len; - } - - if (*options->classid > 0) { - *p++ = DHCP_CLASSID; - *p++ = l = strlen (options->classid); - memcpy (p, options->classid, l); - p += l; - } - } - - if (type == DHCP_DISCOVER || type == DHCP_REQUEST) { -#define PUTADDR(_type, _val) { \ - *p++ = _type; \ - *p++ = 4; \ - memcpy (p, &_val.s_addr, 4); \ - p += 4; \ -} - if (IN_LINKLOCAL (ntohl (dhcp->address.s_addr))) - logger (LOG_ERR, - "cannot request a link local address"); - else { - if (dhcp->address.s_addr && - dhcp->address.s_addr != - iface->previous_address.s_addr) - { - PUTADDR (DHCP_ADDRESS, dhcp->address); - if (dhcp->serveraddress.s_addr) - PUTADDR (DHCP_SERVERIDENTIFIER, - dhcp->serveraddress); - } - } -#undef PUTADDR - - if (options->leasetime != 0) { - *p++ = DHCP_LEASETIME; - *p++ = 4; - ul = htonl (options->leasetime); - memcpy (p, &ul, 4); - p += 4; - } - } - - if (type == DHCP_DISCOVER || - type == DHCP_INFORM || - type == DHCP_REQUEST) - { - if (options->hostname[0]) { - if (options->fqdn == FQDN_DISABLE) { - *p++ = DHCP_HOSTNAME; - *p++ = l = strlen (options->hostname); - memcpy (p, options->hostname, l); - p += l; - } else { - /* Draft IETF DHC-FQDN option (81) */ - *p++ = DHCP_FQDN; - *p++ = (l = strlen (options->hostname)) + 3; - /* Flags: 0000NEOS - * S: 1 => Client requests Server to update - * a RR in DNS as well as PTR - * O: 1 => Server indicates to client that - * DNS has been updated - * E: 1 => Name data is DNS format - * N: 1 => Client requests Server to not - * update DNS - */ - *p++ = options->fqdn & 0x9; - *p++ = 0; /* from server for PTR RR */ - *p++ = 0; /* from server for A RR if S=1 */ - memcpy (p, options->hostname, l); - p += l; - } - } - - *p++ = DHCP_PARAMETERREQUESTLIST; - n_params = p; - *p++ = 0; - /* Only request DNSSERVER in discover to keep the packets small. - * RFC2131 Section 3.5 states that the REQUEST must include the - * list from the DISCOVER message, so I think this is ok. */ - - if (type == DHCP_DISCOVER && ! options->test) - *p++ = DHCP_DNSSERVER; - else { - if (type != DHCP_INFORM) { - *p++ = DHCP_RENEWALTIME; - *p++ = DHCP_REBINDTIME; - } - *p++ = DHCP_NETMASK; - *p++ = DHCP_BROADCAST; - - /* -S means request CSR and MSCSR - * -SS means only request MSCSR incase DHCP message - * is too big */ - if (options->domscsr < 2) - *p++ = DHCP_CSR; - if (options->domscsr > 0) - *p++ = DHCP_MSCSR; - /* RFC 3442 states classless static routes should be - * before routers and static routes as classless static - * routes override them both */ - *p++ = DHCP_STATICROUTE; - *p++ = DHCP_ROUTERS; - *p++ = DHCP_HOSTNAME; - *p++ = DHCP_DNSSEARCH; - *p++ = DHCP_DNSDOMAIN; - *p++ = DHCP_DNSSERVER; -#ifdef ENABLE_NIS - *p++ = DHCP_NISDOMAIN; - *p++ = DHCP_NISSERVER; -#endif -#ifdef ENABLE_NTP - *p++ = DHCP_NTPSERVER; -#endif - *p++ = DHCP_MTU; -#ifdef ENABLE_INFO - *p++ = DHCP_ROOTPATH; - *p++ = DHCP_SIPSERVER; -#endif - } - - *n_params = p - n_params - 1; - } - *p++ = DHCP_END; - -#ifdef BOOTP_MESSAGE_LENTH_MIN - /* Some crappy DHCP servers think they have to obey the BOOTP minimum - * message length. - * They are wrong, but we should still cater for them. */ - while (p - m < BOOTP_MESSAGE_LENTH_MIN) - *p++ = DHCP_PAD; -#endif - - message_length = p - m; - - packet = xzalloc (sizeof (*packet)); - make_dhcp_packet (packet, (unsigned char *) message, message_length, - from, to); - free (message); - - logger (LOG_DEBUG, "sending %s with xid 0x%x", - dhcp_message (type), xid); - retval = send_packet (iface, ETHERTYPE_IP, (unsigned char *) packet, - message_length + - sizeof (packet->ip) + sizeof (packet->udp)); - free (packet); - return (retval); -} - -/* Decode an RFC3397 DNS search order option into a space - * seperated string. Returns length of string (including - * terminating zero) or zero on error. out may be NULL - * to just determine output length. */ -static unsigned int decode_search (const unsigned char *p, int len, char *out) -{ - const unsigned char *r, *q = p; - unsigned int count = 0, l, hops; - - while (q - p < len) { - r = NULL; - hops = 0; - while ((l = *q++)) { - unsigned int label_type = l & 0xc0; - if (label_type == 0x80 || label_type == 0x40) - return 0; - else if (label_type == 0xc0) { /* pointer */ - l = (l & 0x3f) << 8; - l |= *q++; - - /* save source of first jump. */ - if (!r) - r = q; - - hops++; - if (hops > 255) - return 0; - - q = p + l; - if (q - p >= len) - return 0; - } else { - /* straightforward name segment, add with '.' */ - count += l + 1; - if (out) { - memcpy (out, q, l); - out += l; - *out++ = '.'; - } - q += l; - } - } - - /* change last dot to space */ - if (out) - *(out - 1) = ' '; - - if (r) - q = r; - } - - /* change last space to zero terminator */ - if (out) - *(out - 1) = 0; - - return count; -} - -/* Add our classless static routes to the routes variable - * and return the last route set */ -static struct route_head *decode_CSR (const unsigned char *p, int len) -{ - const unsigned char *q = p; - unsigned int cidr; - unsigned int ocets; - struct route_head *routes = NULL; - route_t *route; - - /* Minimum is 5 -first is CIDR and a router length of 4 */ - if (len < 5) - return NULL; - - while (q - p < len) { - if (! routes) { - routes = xmalloc (sizeof (*routes)); - STAILQ_INIT (routes); - } - - route = xzalloc (sizeof (*route)); - - cidr = *q++; - if (cidr > 32) { - logger (LOG_ERR, - "invalid CIDR of %d in classless static route", - cidr); - free_route (routes); - return (NULL); - } - ocets = (cidr + 7) / 8; - - if (ocets > 0) { - memcpy (&route->destination.s_addr, q, (size_t) ocets); - q += ocets; - } - - /* Now enter the netmask */ - if (ocets > 0) { - memset (&route->netmask.s_addr, 255, (size_t) ocets - 1); - memset ((unsigned char *) &route->netmask.s_addr + - (ocets - 1), - (256 - (1 << (32 - cidr) % 8)), 1); - } - - /* Finally, snag the router */ - memcpy (&route->gateway.s_addr, q, 4); - q += 4; - - STAILQ_INSERT_TAIL (routes, route, entries); - } - - return (routes); -} - -void free_dhcp (dhcp_t *dhcp) -{ - if (! dhcp) - return; - - free_route (dhcp->routes); - free (dhcp->hostname); - free_address (dhcp->dnsservers); - free (dhcp->dnsdomain); - free (dhcp->dnssearch); - free_address (dhcp->ntpservers); - free (dhcp->nisdomain); - free_address (dhcp->nisservers); - free (dhcp->rootpath); - free (dhcp->sipservers); - if (dhcp->fqdn) { - free (dhcp->fqdn->name); - free (dhcp->fqdn); - } -} - - -static bool dhcp_add_address (struct address_head **addresses, - const unsigned char *data, - int length) -{ - int i; - address_t *address; - - for (i = 0; i < length; i += 4) { - /* Sanity check */ - if (i + 4 > length) { - logger (LOG_ERR, "invalid address length"); - return (false); - } - - if (*addresses == NULL) { - *addresses = xmalloc (sizeof (**addresses)); - STAILQ_INIT (*addresses); - } - address = xzalloc (sizeof (*address)); - memcpy (&address->address.s_addr, data + i, 4); - STAILQ_INSERT_TAIL (*addresses, address, entries); - } - - return (true); -} - -#ifdef ENABLE_INFO -static char *decode_sipservers (const unsigned char *data, int length) -{ - char *sip = NULL; - char *p; - const char encoding = *data++; - struct in_addr addr; - size_t len; - - length--; - - switch (encoding) { - case 0: - if ((len = decode_search (data, length, NULL)) > 0) { - sip = xmalloc (len); - decode_search (data, length, sip); - } - break; - - case 1: - if (length == 0 || length % 4 != 0) { - logger (LOG_ERR, - "invalid length %d for option 120", - length + 1); - break; - } - len = ((length / 4) * (4 * 4)) + 1; - sip = p = xmalloc (len); - while (length != 0) { - memcpy (&addr.s_addr, data, 4); - data += 4; - p += snprintf (p, len - (p - sip), - "%s ", inet_ntoa (addr)); - length -= 4; - } - *--p = '\0'; - break; - - default: - logger (LOG_ERR, "unknown sip encoding %d", encoding); - break; - } - - return (sip); -} -#endif - -/* This calculates the netmask that we should use for static routes. - * This IS different from the calculation used to calculate the netmask - * for an interface address. */ -static uint32_t route_netmask (uint32_t ip_in) -{ - /* used to be unsigned long - check if error */ - uint32_t p = ntohl (ip_in); - uint32_t t; - - if (IN_CLASSA (p)) - t = ~IN_CLASSA_NET; - else { - if (IN_CLASSB (p)) - t = ~IN_CLASSB_NET; - else { - if (IN_CLASSC (p)) - t = ~IN_CLASSC_NET; - else - t = 0; - } - } - - while (t & p) - t >>= 1; - - return (htonl (~t)); -} - -static struct route_head *decode_routes (const unsigned char *data, int length) -{ - int i; - struct route_head *head = NULL; - route_t *route; - - for (i = 0; i < length; i += 8) { - if (! head) { - head = xmalloc (sizeof (*head)); - STAILQ_INIT (head); - } - route = xzalloc (sizeof (*route)); - memcpy (&route->destination.s_addr, data + i, 4); - memcpy (&route->gateway.s_addr, data + i + 4, 4); - route->netmask.s_addr = - route_netmask (route->destination.s_addr); - STAILQ_INSERT_TAIL (head, route, entries); - } - - return (head); -} - -static struct route_head *decode_routers (const unsigned char *data, int length) -{ - int i; - struct route_head *head = NULL; - route_t *route = NULL; - - for (i = 0; i < length; i += 4) { - if (! head) { - head = xmalloc (sizeof (*head)); - STAILQ_INIT (head); - } - route = xzalloc (sizeof (*route)); - memcpy (&route->gateway.s_addr, data + i, 4); - STAILQ_INSERT_TAIL (head, route, entries); - } - - return (head); -} - -int parse_dhcpmessage (dhcp_t *dhcp, const dhcpmessage_t *message) -{ - const unsigned char *p = message->options; - const unsigned char *end = p; /* Add size later for gcc-3 issue */ - unsigned char option; - unsigned char length; - unsigned int len = 0; - int retval = -1; - struct timeval tv; - struct route_head *routers = NULL; - struct route_head *routes = NULL; - struct route_head *csr = NULL; - struct route_head *mscsr = NULL; - bool in_overload = false; - bool parse_sname = false; - bool parse_file = false; - - end += sizeof (message->options); - - if (gettimeofday (&tv, NULL) == -1) { - logger (LOG_ERR, "gettimeofday: %s", strerror (errno)); - return (-1); - } - - dhcp->address.s_addr = message->yiaddr; - dhcp->leasedfrom = tv.tv_sec; - dhcp->frominfo = false; - dhcp->address.s_addr = message->yiaddr; - strlcpy (dhcp->servername, (char *) message->servername, - sizeof (dhcp->servername)); - -#define LEN_ERR \ - { \ - logger (LOG_ERR, "invalid length %d for option %d", \ - length, option); \ - p += length; \ - continue; \ - } - -parse_start: - while (p < end) { - option = *p++; - if (! option) - continue; - - if (option == DHCP_END) - goto eexit; - - length = *p++; - - if (option != DHCP_PAD && length == 0) { - logger (LOG_ERR, "option %d has zero length", option); - retval = -1; - goto eexit; - } - - if (p + length >= end) { - logger (LOG_ERR, "dhcp option exceeds message length"); - retval = -1; - goto eexit; - } - - switch (option) { - case DHCP_MESSAGETYPE: - retval = (int) *p; - p += length; - continue; - - default: - if (length == 0) { - logger (LOG_DEBUG, - "option %d has zero length, skipping", - option); - continue; - } - } - -#define LENGTH(_length) \ - if (length != _length) \ - LEN_ERR; -#define MIN_LENGTH(_length) \ - if (length < _length) \ - LEN_ERR; -#define MULT_LENGTH(_mult) \ - if (length % _mult != 0) \ - LEN_ERR; -#define GET_UINT8(_val) \ - LENGTH (sizeof (uint8_t)); \ - memcpy (&_val, p, sizeof (uint8_t)); -#define GET_UINT16(_val) \ - LENGTH (sizeof (uint16_t)); \ - memcpy (&_val, p, sizeof (uint16_t)); -#define GET_UINT32(_val) \ - LENGTH (sizeof (uint32_t)); \ - memcpy (&_val, p, sizeof (uint32_t)); -#define GET_UINT16_H(_val) \ - GET_UINT16 (_val); \ - _val = ntohs (_val); -#define GET_UINT32_H(_val) \ - GET_UINT32 (_val); \ - _val = ntohl (_val); - - switch (option) { - case DHCP_ADDRESS: - GET_UINT32 (dhcp->address.s_addr); - break; - case DHCP_NETMASK: - GET_UINT32 (dhcp->netmask.s_addr); - break; - case DHCP_BROADCAST: - GET_UINT32 (dhcp->broadcast.s_addr); - break; - case DHCP_SERVERIDENTIFIER: - GET_UINT32 (dhcp->serveraddress.s_addr); - break; - case DHCP_LEASETIME: - GET_UINT32_H (dhcp->leasetime); - break; - case DHCP_RENEWALTIME: - GET_UINT32_H (dhcp->renewaltime); - break; - case DHCP_REBINDTIME: - GET_UINT32_H (dhcp->rebindtime); - break; - case DHCP_MTU: - GET_UINT16_H (dhcp->mtu); - /* Minimum legal mtu is 68 accoridng to - * RFC 2132. In practise it's 576 which is the - * minimum maximum message size. */ - if (dhcp->mtu < MTU_MIN) { - logger (LOG_DEBUG, - "MTU %d is too low, minimum is %d; ignoring", - dhcp->mtu, MTU_MIN); - dhcp->mtu = 0; - } - break; - -#undef GET_UINT32_H -#undef GET_UINT32 -#undef GET_UINT16_H -#undef GET_UINT16 -#undef GET_UINT8 - -#define GETSTR(_var) { \ - MIN_LENGTH (sizeof (char)); \ - if (_var) free (_var); \ - _var = xmalloc ((size_t) length + 1); \ - memcpy (_var, p, (size_t) length); \ - memset (_var + length, 0, 1); \ -} - case DHCP_HOSTNAME: - GETSTR (dhcp->hostname); - break; - case DHCP_DNSDOMAIN: - GETSTR (dhcp->dnsdomain); - break; - case DHCP_MESSAGE: - GETSTR (dhcp->message); - break; -#ifdef ENABLE_INFO - case DHCP_ROOTPATH: - GETSTR (dhcp->rootpath); - break; -#endif -#ifdef ENABLE_NIS - case DHCP_NISDOMAIN: - GETSTR (dhcp->nisdomain); - break; -#endif -#undef GETSTR - -#define GETADDR(_var) \ - MULT_LENGTH (4); \ - if (! dhcp_add_address (&_var, p, length)) \ - { \ - retval = -1; \ - goto eexit; \ - } - case DHCP_DNSSERVER: - GETADDR (dhcp->dnsservers); - break; -#ifdef ENABLE_NTP - case DHCP_NTPSERVER: - GETADDR (dhcp->ntpservers); - break; -#endif -#ifdef ENABLE_NIS - case DHCP_NISSERVER: - GETADDR (dhcp->nisservers); - break; -#endif -#undef GETADDR - - case DHCP_DNSSEARCH: - MIN_LENGTH (1); - free (dhcp->dnssearch); - len = decode_search (p, length, NULL); - if (len > 0) { - dhcp->dnssearch = xmalloc (len); - decode_search (p, length, - dhcp->dnssearch); - } - break; - - case DHCP_CSR: - MIN_LENGTH (5); - free_route (csr); - csr = decode_CSR (p, length); - break; - - case DHCP_MSCSR: - MIN_LENGTH (5); - free_route (mscsr); - mscsr = decode_CSR (p, length); - break; - -#ifdef ENABLE_INFO - case DHCP_SIPSERVER: - free (dhcp->sipservers); - dhcp->sipservers = decode_sipservers (p,length); - break; -#endif - - case DHCP_STATICROUTE: - MULT_LENGTH (8); - free_route (routes); - routes = decode_routes (p, length); - break; - - case DHCP_ROUTERS: - MULT_LENGTH (4); - free_route (routers); - routers = decode_routers (p, length); - break; - - case DHCP_OPTIONSOVERLOADED: - LENGTH (1); - /* The overloaded option in an overloaded option - * should be ignored, overwise we may get an - * infinite loop */ - if (! in_overload) { - if (*p & 1) - parse_file = true; - if (*p & 2) - parse_sname = true; - } - break; - - case DHCP_FQDN: - /* We ignore replies about FQDN */ - break; - -#undef LENGTH -#undef MIN_LENGTH -#undef MULT_LENGTH - - default: - logger (LOG_DEBUG, - "no facility to parse DHCP code %u", - option); - break; - } - - p += length; - } - -eexit: - /* We may have options overloaded, so go back and grab them */ - if (parse_file) { - parse_file = false; - p = message->bootfile; - end = p + sizeof (message->bootfile); - in_overload = true; - goto parse_start; - } else if (parse_sname) { - parse_sname = false; - p = message->servername; - end = p + sizeof (message->servername); - memset (dhcp->servername, 0, sizeof (dhcp->servername)); - in_overload = true; - goto parse_start; - } - - /* Fill in any missing fields */ - if (! dhcp->netmask.s_addr) - dhcp->netmask.s_addr = get_netmask (dhcp->address.s_addr); - if (! dhcp->broadcast.s_addr) - dhcp->broadcast.s_addr = dhcp->address.s_addr | - ~dhcp->netmask.s_addr; - - /* If we have classess static routes then we discard - * static routes and routers according to RFC 3442 */ - if (csr) { - dhcp->routes = csr; - free_route (mscsr); - free_route (routers); - free_route (routes); - } else if (mscsr) { - dhcp->routes = mscsr; - free_route (routers); - free_route (routes); - } else { - /* Ensure that we apply static routes before routers */ - if (! routes) - routes = routers; - else if (routers) - STAILQ_CONCAT (routes, routers); - dhcp->routes = routes; - } - - return (retval); -} diff --git a/workspace/networkDiscovery/dhcpcd/dhcp.h b/workspace/networkDiscovery/dhcpcd/dhcp.h deleted file mode 100644 index ef97b75..0000000 --- a/workspace/networkDiscovery/dhcpcd/dhcp.h +++ /dev/null @@ -1,215 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef DHCP_H -#define DHCP_H - -#include -#include -#include -#include -#include - -#include "dhcpcd.h" -#include "interface.h" - -/* Max MTU - defines dhcp option length */ -#define MTU_MAX 1500 -#define MTU_MIN 576 - -/* UDP port numbers for DHCP */ -#define DHCP_SERVER_PORT 67 -#define DHCP_CLIENT_PORT 68 - -#define MAGIC_COOKIE 0x63825363 -#define BROADCAST_FLAG 0x8000 - -/* DHCP message OP code */ -#define DHCP_BOOTREQUEST 1 -#define DHCP_BOOTREPLY 2 - -/* DHCP message type */ -#define DHCP_DISCOVER 1 -#define DHCP_OFFER 2 -#define DHCP_REQUEST 3 -#define DHCP_DECLINE 4 -#define DHCP_ACK 5 -#define DHCP_NAK 6 -#define DHCP_RELEASE 7 -#define DHCP_INFORM 8 - -/* DHCP options */ -enum DHCP_OPTIONS -{ - DHCP_PAD = 0, - DHCP_NETMASK = 1, - DHCP_TIMEROFFSET = 2, - DHCP_ROUTERS = 3, - DHCP_TIMESERVER = 4, - DHCP_NAMESERVER = 5, - DHCP_DNSSERVER = 6, - DHCP_LOGSERVER = 7, - DHCP_COOKIESERVER = 8, - DHCP_HOSTNAME = 12, - DHCP_DNSDOMAIN = 15, - DHCP_ROOTPATH = 17, - DHCP_DEFAULTIPTTL = 23, - DHCP_MTU = 26, - DHCP_BROADCAST = 28, - DHCP_MASKDISCOVERY = 29, - DHCP_ROUTERDISCOVERY = 31, - DHCP_STATICROUTE = 33, - DHCP_NISDOMAIN = 40, - DHCP_NISSERVER = 41, - DHCP_NTPSERVER = 42, - DHCP_ADDRESS = 50, - DHCP_LEASETIME = 51, - DHCP_OPTIONSOVERLOADED = 52, - DHCP_MESSAGETYPE = 53, - DHCP_SERVERIDENTIFIER = 54, - DHCP_PARAMETERREQUESTLIST = 55, - DHCP_MESSAGE = 56, - DHCP_MAXMESSAGESIZE = 57, - DHCP_RENEWALTIME = 58, - DHCP_REBINDTIME = 59, - DHCP_CLASSID = 60, - DHCP_CLIENTID = 61, - DHCP_USERCLASS = 77, /* RFC 3004 */ - DHCP_FQDN = 81, - DHCP_DNSSEARCH = 119, /* RFC 3397 */ - DHCP_SIPSERVER = 120, /* RFC 3361 */ - DHCP_CSR = 121, /* RFC 3442 */ - DHCP_MSCSR = 249, /* MS code for RFC 3442 */ - DHCP_END = 255 -}; - -/* SetFQDNHostName values - lsnybble used in flags - * byte (see buildmsg.c), hsnybble to create order - * and to allow 0x00 to mean disable - */ -enum FQQN { - FQDN_DISABLE = 0x00, - FQDN_NONE = 0x18, - FQDN_PTR = 0x20, - FQDN_BOTH = 0x31 -}; - -typedef struct fqdn_t -{ - uint8_t flags; - uint8_t r1; - uint8_t r2; - char *name; -} fqdn_t; - -typedef struct dhcp_t -{ - char version[11]; - - struct in_addr serveraddress; - char serverhw[IF_NAMESIZE]; - char servername[64]; - - struct in_addr address; - struct in_addr netmask; - struct in_addr broadcast; - unsigned short mtu; - - uint32_t leasedfrom; - uint32_t leasetime; - uint32_t renewaltime; - uint32_t rebindtime; - - struct route_head *routes; - - char *hostname; - fqdn_t *fqdn; - - struct address_head *dnsservers; - char *dnsdomain; - char *dnssearch; - - struct address_head *ntpservers; - - struct address_head *nisservers; - char *nisdomain; - - char *sipservers; - - char *message; - char *rootpath; - - bool frominfo; -} dhcp_t; - -/* Sizes for DHCP options */ -#define DHCP_CHADDR_LEN 16 -#define SERVERNAME_LEN 64 -#define BOOTFILE_LEN 128 -#define DHCP_UDP_LEN (20 + 8) -#define DHCP_BASE_LEN (4 + 4 + 2 + 2 + 4 + 4 + 4 + 4 + 4) -#define DHCP_RESERVE_LEN (4 + 4 + 4 + 4 + 2) -#define DHCP_FIXED_LEN (DHCP_BASE_LEN + DHCP_CHADDR_LEN + \ - + SERVERNAME_LEN + BOOTFILE_LEN) -#define DHCP_OPTION_LEN (MTU_MAX - DHCP_FIXED_LEN - DHCP_UDP_LEN \ - - DHCP_RESERVE_LEN) - -/* Some crappy DHCP servers require the BOOTP minimum length */ -#define BOOTP_MESSAGE_LENTH_MIN 300 - -typedef struct dhcpmessage_t -{ - unsigned char op; /* message type */ - unsigned char hwtype; /* hardware address type */ - unsigned char hwlen; /* hardware address length */ - unsigned char hwopcount; /* should be zero in client message */ - uint32_t xid; /* transaction id */ - uint16_t secs; /* elapsed time in sec. from boot */ - uint16_t flags; - uint32_t ciaddr; /* (previously allocated) client IP */ - uint32_t yiaddr; /* 'your' client IP address */ - uint32_t siaddr; /* should be zero in client's messages */ - uint32_t giaddr; /* should be zero in client's messages */ - unsigned char chaddr[DHCP_CHADDR_LEN]; /* client's hardware address */ - unsigned char servername[SERVERNAME_LEN]; /* server host name */ - unsigned char bootfile[BOOTFILE_LEN]; /* boot file name */ - uint32_t cookie; - unsigned char options[DHCP_OPTION_LEN]; /* message options - cookie */ -} dhcpmessage_t; - -struct udp_dhcp_packet -{ - struct ip ip; - struct udphdr udp; - dhcpmessage_t dhcp; -}; - -ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, - uint32_t xid, char type, const options_t *options); -void free_dhcp (dhcp_t *dhcp); -int parse_dhcpmessage (dhcp_t *dhcp, const dhcpmessage_t *message); -#endif diff --git a/workspace/networkDiscovery/dhcpcd/dhcpcd.c b/workspace/networkDiscovery/dhcpcd/dhcpcd.c deleted file mode 100644 index 925717a..0000000 --- a/workspace/networkDiscovery/dhcpcd/dhcpcd.c +++ /dev/null @@ -1,366 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -const char copyright[] = "Copyright (c) 2006-2008 Roy Marples"; - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "client.h" -#include "dhcpcd.h" -#include "dhcp.h" -#include "interface.h" -#include "logger.h" -#include "socket.h" -#include "version.h" - -static int doversion = 0; -static int dohelp = 0; -#define EXTRA_OPTS -static const struct option longopts[] = { - {"arp", no_argument, NULL, 'a'}, - {"script", required_argument, NULL, 'c'}, - {"debug", no_argument, NULL, 'd'}, - {"hostname", optional_argument, NULL, 'h'}, - {"classid", optional_argument, NULL, 'i'}, - {"release", no_argument, NULL, 'k'}, - {"leasetime", required_argument, NULL, 'l'}, - {"metric", required_argument, NULL, 'm'}, - {"renew", no_argument, NULL, 'n'}, - {"persistent", no_argument, NULL, 'p'}, - {"inform", optional_argument, NULL, 's'}, - {"request", optional_argument, NULL, 'r'}, - {"timeout", required_argument, NULL, 't'}, - {"userclass", required_argument, NULL, 'u'}, - {"exit", no_argument, NULL, 'x'}, - {"lastlease", no_argument, NULL, 'E'}, - {"fqdn", required_argument, NULL, 'F'}, - {"nogateway", no_argument, NULL, 'G'}, - {"sethostname", no_argument, NULL, 'H'}, - {"clientid", optional_argument, NULL, 'I'}, - {"noipv4ll", no_argument, NULL, 'L'}, - {"nomtu", no_argument, NULL, 'M'}, - {"nontp", no_argument, NULL, 'N'}, - {"nodns", no_argument, NULL, 'R'}, - {"msscr", no_argument, NULL, 'S'}, - {"test", no_argument, NULL, 'T'}, - {"nonis", no_argument, NULL, 'Y'}, - {"help", no_argument, &dohelp, 1}, - {"version", no_argument, &doversion, 1}, -#ifdef THERE_IS_NO_FORK - {"daemonised", no_argument, NULL, 'f'}, - {"skiproutes", required_argument, NULL, 'g'}, -#endif - {NULL, 0, NULL, 0} -}; - -#ifdef THERE_IS_NO_FORK -char dhcpcd[PATH_MAX]; -char **dhcpcd_argv = NULL; -int dhcpcd_argc = 0; -char *dhcpcd_skiproutes = NULL; -#undef EXTRA_OPTS -#define EXTRA_OPTS "fg:" -#endif - -static int atoint (const char *s) -{ - char *t; - long n; - - errno = 0; - n = strtol (s, &t, 0); - if ((errno != 0 && n == 0) || s == t || - (errno == ERANGE && (n == LONG_MAX || n == LONG_MIN))) - { - logger (LOG_ERR, "`%s' out of range", s); - return (-1); - } - - return ((int) n); -} - -static pid_t read_pid (const char *pidfile) -{ - FILE *fp; - pid_t pid = 0; - - if ((fp = fopen (pidfile, "r")) == NULL) { - errno = ENOENT; - return 0; - } - - fscanf (fp, "%d", &pid); - fclose (fp); - - - return (pid); -} - -static void usage (void) -{ - printf ("usage: "PACKAGE" [-adknpEGHMNRSTY] [-c script] [-h hostname] [-i classID]\n" - " [-l leasetime] [-m metric] [-r ipaddress] [-s ipaddress]\n" - " [-t timeout] [-u userclass] [-F none | ptr | both]\n" - " [-I clientID] \n"); -} -/** - * to be deleted - */ - -int nd_main(char *ifname) -{ - options_t *options; - int userclasses = 0; - int opt; - int option_index = 0; - char *prefix; - pid_t pid; - int debug = 0; - int i; - int pidfd = -1; - int sig = 0; - int retval = EXIT_FAILURE; - - /* Close any un-needed fd's */ - for (i = getdtablesize() - 1; i >= 3; --i) - close (i); - - openlog (PACKAGE, LOG_PID, LOG_LOCAL0); - - options = xzalloc (sizeof (*options)); - options->script = (char *) DEFAULT_SCRIPT; - snprintf (options->classid, CLASS_ID_MAX_LEN, "%s %s", - PACKAGE, VERSION); - - options->doarp = true; - options->dodns = true; - options->domtu = true; - options->donis = true; - options->dontp = true; - options->dogateway = true; - options->daemonise = true; - options->doinform = false; - options->doipv4ll = true; - options->doduid = true; - options->timeout = DEFAULT_TIMEOUT; - - gethostname (options->hostname, sizeof (options->hostname)); - if (strcmp (options->hostname, "(none)") == 0 || - strcmp (options->hostname, "localhost") == 0) - memset (options->hostname, 0, sizeof (options->hostname)); - - -/* -#ifdef THERE_IS_NO_FORK - dhcpcd_argv = argv; - dhcpcd_argc = argc; - if (! realpath (argv[0], dhcpcd)) { - logger (LOG_ERR, "unable to resolve the path `%s': %s", - argv[0], strerror (errno)); - goto abort; - } -#endif -*/ - - if (strlen (ifname) > IF_NAMESIZE) { - logger (LOG_ERR, - "`%s' too long for an interface name (max=%d)", - ifname, IF_NAMESIZE); - goto abort; - } else { - strlcpy (options->interface, ifname, - sizeof (options->interface)); - } - - if (strchr (options->hostname, '.')) { - if (options->fqdn == FQDN_DISABLE) - options->fqdn = FQDN_BOTH; - } else - options->fqdn = FQDN_DISABLE; - - if (options->request_address.s_addr == 0 && options->doinform) { - if ((options->request_address.s_addr = - get_address (options->interface)) != 0) - options->keep_address = true; - } - - if (IN_LINKLOCAL (ntohl (options->request_address.s_addr))) { - logger (LOG_ERR, - "you are not allowed to request a link local address"); - goto abort; - } - - if (geteuid ()) - logger (LOG_WARNING, PACKAGE " will not work correctly unless" - " run as root"); - - prefix = xmalloc (sizeof (char) * (IF_NAMESIZE + 3)); - snprintf (prefix, IF_NAMESIZE, "%s: ", options->interface); - setlogprefix (prefix); - snprintf (options->pidfile, sizeof (options->pidfile), PIDFILE, - options->interface); - free (prefix); - - chdir ("/"); - umask (022); - - if (mkdir (INFODIR, S_IRUSR | S_IWUSR |S_IXUSR | S_IRGRP | S_IXGRP - | S_IROTH | S_IXOTH) && errno != EEXIST) - { - logger (LOG_ERR, - "mkdir(\"%s\",0): %s\n", INFODIR, strerror (errno)); - goto abort; - } - - if (mkdir (ETCDIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP - | S_IROTH | S_IXOTH) && errno != EEXIST) - { - logger (LOG_ERR, - "mkdir(\"%s\",0): %s\n", ETCDIR, strerror (errno)); - goto abort; - } - - if (options->test) { - if (options->dorequest || options->doinform) { - logger (LOG_ERR, - "cannot test with --inform or --request"); - goto abort; - } - - if (options->dolastlease) { - logger (LOG_ERR, "cannot test with --lastlease"); - goto abort; - } - - if (sig != 0) { - logger (LOG_ERR, - "cannot test with --release or --renew"); - goto abort; - } - } - - if (sig != 0) { - int killed = -1; - pid = read_pid (options->pidfile); - if (pid != 0) - logger (LOG_INFO, "sending signal %d to pid %d", - sig, pid); - - if (! pid || (killed = kill (pid, sig))) - logger (sig == SIGALRM ? LOG_INFO : LOG_ERR, - ""PACKAGE" not running"); - - if (pid != 0 && (sig != SIGALRM || killed != 0)) - unlink (options->pidfile); - - if (killed == 0) { - retval = EXIT_SUCCESS; - goto abort; - } - - if (sig != SIGALRM) - goto abort; - } - - if (! options->test && ! options->daemonised) { - if ((pid = read_pid (options->pidfile)) > 0 && - kill (pid, 0) == 0) - { - logger (LOG_ERR, ""PACKAGE - " already running on pid %d (%s)", - pid, options->pidfile); - goto abort; - } - - pidfd = open (options->pidfile, - O_WRONLY | O_CREAT | O_NONBLOCK, 0664); - if (pidfd == -1) { - logger (LOG_ERR, "open `%s': %s", - options->pidfile, strerror (errno)); - goto abort; - } - - /* Lock the file so that only one instance of dhcpcd runs - * on an interface */ - if (flock (pidfd, LOCK_EX | LOCK_NB) == -1) { - logger (LOG_ERR, "flock `%s': %s", - options->pidfile, strerror (errno)); - goto abort; - } - - /* dhcpcd.sh should not interhit this fd */ - if ((i = fcntl (pidfd, F_GETFD, 0)) == -1 || - fcntl (pidfd, F_SETFD, i | FD_CLOEXEC) == -1) - logger (LOG_ERR, "fcntl: %s", strerror (errno)); - - writepid (pidfd, getpid ()); - logger (LOG_INFO, PACKAGE " " VERSION " starting"); - } - - /* Seed random */ - srandomdev (); - - /* Massage our filters per platform */ - setup_packet_filters (); - - if (dhcp_run (options, &pidfd) == 0) - retval = EXIT_SUCCESS; - -abort: - /* If we didn't daemonise then we need to punt the pidfile now */ - if (pidfd > -1) { - close (pidfd); - unlink (options->pidfile); - } - - free (options); - -#ifdef THERE_IS_NO_FORK - /* There may have been an error before the dhcp_run function - * clears this, so just do it here to be safe */ - free (dhcpcd_skiproutes); -#endif - - logger (LOG_INFO, "exiting"); - - exit (retval); - /* NOTREACHED */ -} diff --git a/workspace/networkDiscovery/dhcpcd/dhcpcd.h b/workspace/networkDiscovery/dhcpcd/dhcpcd.h deleted file mode 100644 index 0b41ead..0000000 --- a/workspace/networkDiscovery/dhcpcd/dhcpcd.h +++ /dev/null @@ -1,96 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef DHCPCD_H -#define DHCPCD_H - -#include -#include -#include -#include -#include -#include - -#include "common.h" - -#define DEFAULT_TIMEOUT 20 -#define DEFAULT_LEASETIME 3600 /* 1 hour */ - -#define CLASS_ID_MAX_LEN 48 -#define CLIENT_ID_MAX_LEN 48 -#define USERCLASS_MAX_LEN 255 - -#ifdef THERE_IS_NO_FORK -extern char dhcpcd[PATH_MAX]; -extern char **dhcpcd_argv; -extern int dhcpcd_argc; -extern char *dhcpcd_skiproutes; -#endif - -typedef struct options_t { - char interface[IF_NAMESIZE]; - char hostname[MAXHOSTNAMELEN]; - int fqdn; - char classid[CLASS_ID_MAX_LEN]; - char clientid[CLIENT_ID_MAX_LEN]; - char userclass[USERCLASS_MAX_LEN]; - size_t userclass_len; - uint32_t leasetime; - time_t timeout; - int metric; - - bool doarp; - bool dodns; - bool dodomainname; - bool dogateway; - int dohostname; - bool domtu; - bool donis; - bool dontp; - bool dolastlease; - bool doinform; - bool dorequest; - bool doipv4ll; - bool doduid; - int domscsr; - - struct in_addr request_address; - struct in_addr request_netmask; - - bool persistent; - bool keep_address; - bool daemonise; - bool daemonised; - bool test; - - char *script; - char pidfile[PATH_MAX]; -} options_t; - -int nd_main (char *ifname); - -#endif diff --git a/workspace/networkDiscovery/dhcpcd/dhcpcd.sh b/workspace/networkDiscovery/dhcpcd/dhcpcd.sh deleted file mode 100644 index 8c86aac..0000000 --- a/workspace/networkDiscovery/dhcpcd/dhcpcd.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/sh -# -# This is a sample /etc/dhcpcd.sh script. -# /etc/dhcpcd.sh script is executed by dhcpcd daemon -# any time it configures or shuts down interface. -# The following parameters are passed to dhcpcd.exe script: -# $1 = HostInfoFilePath, e.g "/var/lib/dhcpcd/dhcpcd-eth0.info" -# $2 = "up" if interface has been configured with the same -# IP address as before reboot; -# $2 = "down" if interface has been shut down; -# $2 = "new" if interface has been configured with new IP address; -# -# Sanity checks - -if [ $# -lt 2 ]; then - logger -s -p local0.err -t dhcpcd.sh "wrong usage" - exit 1 -fi - -hostinfo="$1" -state="$2" - -# Reading HostInfo file for configuration parameters -[ -e "${hostinfo}" ] && . "${hostinfo}" - -case "${state}" in - up) - logger -s -p local0.info -t dhcpcd.sh \ - "interface ${INTERFACE} has been configured with old IP=${IPADDR}" - # Put your code here for when the interface has been brought up with an - # old IP address here - ;; - - new) - logger -s -p local0.info -t dhcpcd.sh \ - "interface ${INTERFACE} has been configured with new IP=${IPADDR}" - # Put your code here for when the interface has been brought up with a - # new IP address - ;; - - down) logger -s -p local0.info -t dhcpcd.sh \ - "interface ${INTERFACE} has been brought down" - # Put your code here for the when the interface has been shut down - ;; -esac -exit 0 diff --git a/workspace/networkDiscovery/dhcpcd/duid.c b/workspace/networkDiscovery/dhcpcd/duid.c deleted file mode 100644 index e4dd83b..0000000 --- a/workspace/networkDiscovery/dhcpcd/duid.c +++ /dev/null @@ -1,118 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "common.h" -#include "duid.h" -#include "logger.h" - -#ifdef ENABLE_DUID - -#define THIRTY_YEARS_IN_SECONDS 946707779 - -size_t get_duid (unsigned char *duid, const interface_t *iface) -{ - FILE *f; - uint16_t type = 0; - uint16_t hw = 0; - uint32_t ul; - time_t t; - int x = 0; - unsigned char *p = duid; - size_t len = 0; - - if (! iface) - return (0); - - /* If we already have a DUID then use it as it's never supposed - * to change once we have one even if the interfaces do */ - if ((f = fopen (DUIDFILE, "r"))) { - char *line = get_line (f); - if (line) { - len = hwaddr_aton (NULL, line); - if (len && len <= DUID_LEN) - hwaddr_aton (duid, line); - free (line); - } - fclose (f); - if (len) - return (len); - } else { - if (errno != ENOENT) { - logger (LOG_ERR, "fopen `%s': %s", - DUIDFILE, strerror (errno)); - return (0); - } - } - - /* No file? OK, lets make one based on our interface */ - type = htons (1); /* DUI-D-LLT */ - memcpy (p, &type, 2); - p += 2; - - hw = htons (iface->family); - memcpy (p, &hw, 2); - p += 2; - - /* time returns seconds from jan 1 1970, but DUID-LLT is - * seconds from jan 1 2000 modulo 2^32 */ - t = time (NULL) - THIRTY_YEARS_IN_SECONDS; - ul = htonl (t & 0xffffffff); - memcpy (p, &ul, 4); - p += 4; - - /* Finally, add the MAC address of the interface */ - memcpy (p, iface->hwaddr, iface->hwlen); - p += iface->hwlen; - - len = p - duid; - - if (! (f = fopen (DUIDFILE, "w"))) - logger (LOG_ERR, "fopen `%s': %s", DUIDFILE, strerror (errno)); - else { - x = fprintf (f, "%s\n", hwaddr_ntoa (duid, len)); - fclose (f); - } - - /* Failed to write the duid? scrub it, we cannot use it */ - if (x < 1) { - len = 0; - unlink (DUIDFILE); - } - - return (len); -} -#endif diff --git a/workspace/networkDiscovery/dhcpcd/duid.h b/workspace/networkDiscovery/dhcpcd/duid.h deleted file mode 100644 index 1492990..0000000 --- a/workspace/networkDiscovery/dhcpcd/duid.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef DUID_H -#define DUID_H - -#include "config.h" - -#ifdef ENABLE_DUID -#ifndef DUID_LEN -# define DUID_LEN 128 + 2 -#endif - -#include "interface.h" - -size_t get_duid (unsigned char *duid, const interface_t *iface); -#endif -#endif diff --git a/workspace/networkDiscovery/dhcpcd/info.c b/workspace/networkDiscovery/dhcpcd/info.c deleted file mode 100644 index 8369b43..0000000 --- a/workspace/networkDiscovery/dhcpcd/info.c +++ /dev/null @@ -1,472 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - -#include - -#include -#include -#include -#include -#include - -#include "config.h" -#include "common.h" -#include "dhcp.h" -#include "interface.h" -#include "logger.h" -#include "info.h" - -#ifdef ENABLE_INFO - -/* Create a malloced string of cstr, changing ' to '\'' - * so the contents work in a shell */ -static char *cleanmetas (const char *cstr) -{ - const char *p = cstr; - char *new; - char *n; - size_t len; - - if (cstr == NULL || (len = strlen (cstr)) == 0) - return (xstrdup ("")); - - n = new = xmalloc (sizeof (char) * len + 2); - do - if (*p == '\'') { - size_t pos = n - new; - len += 4; - new = xrealloc (new, sizeof (char) * len + 1); - n = new + pos; - *n++ = '\''; - *n++ = '\\'; - *n++ = '\''; - *n++ = '\''; - } else - *n++ = *p; - while (*p++); - - /* Terminate the sucker */ - *n = '\0'; - - return (new); -} - - -static void print_addresses (FILE *f, const struct address_head *addresses) -{ - const address_t *addr; - - STAILQ_FOREACH (addr, addresses, entries) { - fprintf (f, "%s", inet_ntoa (addr->address)); - if (STAILQ_NEXT (addr, entries)) - fprintf (f, " "); - } -} - -static void print_clean (FILE *f, const char *name, const char *value) -{ - char *clean; - - if (! value) - return; - - clean = cleanmetas (value); - fprintf (f, "%s='%s'\n", name, clean); - free (clean); -} - -bool write_info(const interface_t *iface, const dhcp_t *dhcp, - const options_t *options, bool overwrite) -{ - FILE *f; - route_t *route; - struct stat sb; - - if (options->test) - f = stdout; - else { - if (! overwrite && stat (iface->infofile, &sb) == 0) - return (true); - - logger (LOG_DEBUG, "writing %s", iface->infofile); - if ((f = fopen (iface->infofile, "w")) == NULL) { - logger (LOG_ERR, "fopen `%s': %s", - iface->infofile, strerror (errno)); - return (false); - } - } - - if (dhcp->address.s_addr) { - struct in_addr n; - n.s_addr = dhcp->address.s_addr & dhcp->netmask.s_addr; - fprintf (f, "IPADDR='%s'\n", inet_ntoa (dhcp->address)); - fprintf (f, "NETMASK='%s'\n", inet_ntoa (dhcp->netmask)); - fprintf (f, "NETWORK='%s'\n", inet_ntoa (n)); - fprintf (f, "BROADCAST='%s'\n", inet_ntoa (dhcp->broadcast)); - } - if (dhcp->mtu > 0) - fprintf (f, "MTU='%d'\n", dhcp->mtu); - - if (dhcp->routes) { - bool doneone = false; - fprintf (f, "ROUTES='"); - STAILQ_FOREACH (route, dhcp->routes, entries) { - if (route->destination.s_addr != 0) { - if (doneone) - fprintf (f, " "); - fprintf (f, "%s", inet_ntoa (route->destination)); - fprintf (f, ",%s", inet_ntoa (route->netmask)); - fprintf (f, ",%s", inet_ntoa (route->gateway)); - doneone = true; - } - } - fprintf (f, "'\n"); - - doneone = false; - fprintf (f, "GATEWAYS='"); - STAILQ_FOREACH (route, dhcp->routes, entries) { - if (route->destination.s_addr == 0) { - if (doneone) - fprintf (f, " "); - fprintf (f, "%s", inet_ntoa (route->gateway)); - doneone = true; - } - } - fprintf (f, "'\n"); - } - - print_clean (f, "HOSTNAME", dhcp->hostname); - print_clean (f, "DNSDOMAIN", dhcp->dnsdomain); - print_clean (f, "DNSSEARCH", dhcp->dnssearch); - - if (dhcp->dnsservers) { - fprintf (f, "DNSSERVERS='"); - print_addresses (f, dhcp->dnsservers); - fprintf (f, "'\n"); - } - - if (dhcp->fqdn) { - fprintf (f, "FQDNFLAGS='%u'\n", dhcp->fqdn->flags); - fprintf (f, "FQDNRCODE1='%u'\n", dhcp->fqdn->r1); - fprintf (f, "FQDNRCODE2='%u'\n", dhcp->fqdn->r2); - print_clean (f, "FQDNHOSTNAME", dhcp->fqdn->name); - } - - if (dhcp->ntpservers) { - fprintf (f, "NTPSERVERS='"); - print_addresses (f, dhcp->ntpservers); - fprintf (f, "'\n"); - } - - print_clean (f, "NISDOMAIN", dhcp->nisdomain); - if (dhcp->nisservers) { - fprintf (f, "NISSERVERS='"); - print_addresses (f, dhcp->nisservers); - fprintf (f, "'\n"); - } - - print_clean (f, "ROOTPATH", dhcp->rootpath); - print_clean (f, "SIPSERVERS", dhcp->sipservers); - - if (dhcp->serveraddress.s_addr) - fprintf (f, "DHCPSID='%s'\n", inet_ntoa (dhcp->serveraddress)); - if (dhcp->servername[0]) - print_clean (f, "DHCPSNAME", dhcp->servername); - - if (! options->doinform && dhcp->address.s_addr) { - if (! options->test) - fprintf (f, "LEASEDFROM='%u'\n", dhcp->leasedfrom); - fprintf (f, "LEASETIME='%u'\n", dhcp->leasetime); - fprintf (f, "RENEWALTIME='%u'\n", dhcp->renewaltime); - fprintf (f, "REBINDTIME='%u'\n", dhcp->rebindtime); - } - print_clean (f, "INTERFACE", iface->name); - print_clean (f, "CLASSID", options->classid); - if (iface->clientid_len > 0) { - fprintf (f, "CLIENTID='%s'\n", - hwaddr_ntoa (iface->clientid, iface->clientid_len)); - } - fprintf (f, "DHCPCHADDR='%s'\n", hwaddr_ntoa (iface->hwaddr, - iface->hwlen)); - -#ifdef ENABLE_INFO_COMPAT - /* Support the old .info settings if we need to */ - fprintf (f, "\n# dhcpcd-1.x and 2.x compatible variables\n"); - if (dhcp->dnsservers) { - address_t *addr; - - fprintf (f, "DNS='"); - STAILQ_FOREACH (addr, dhcp->dnsservers, entries) { - fprintf (f, "%s", inet_ntoa (addr->address)); - if (STAILQ_NEXT (addr, entries)) - fprintf (f, ","); - } - fprintf (f, "'\n"); - } - - if (dhcp->routes) { - bool doneone = false; - fprintf (f, "GATEWAY='"); - STAILQ_FOREACH (route, dhcp->routes, entries) { - if (route->destination.s_addr == 0) { - if (doneone) - fprintf (f, ","); - fprintf (f, "%s", inet_ntoa (route->gateway)); - doneone = true; - } - } - fprintf (f, "'\n"); - } -#endif - - if (! options->test) - fclose (f); - return (true); -} - -static bool parse_address (struct in_addr *addr, - const char *value, const char *var) -{ - if (inet_aton (value, addr) == 0) { - logger (LOG_ERR, "%s `%s': %s", var, value, - strerror (errno)); - return (false); - } - return (true); -} - -static bool parse_uint (unsigned int *i, - const char *value, const char *var) -{ - if (sscanf (value, "%u", i) != 1) { - logger (LOG_ERR, "%s `%s': not a valid number", - var, value); - return (false); - } - return (true); -} - -static bool parse_ushort (unsigned short *s, - const char *value, const char *var) -{ - if (sscanf (value, "%hu", s) != 1) { - logger (LOG_ERR, "%s `%s': not a valid number", - var, value); - return (false); - } - return (true); -} - -static struct address_head *parse_addresses (char *value, const char *var) -{ - char *token; - char *p = value; - struct address_head *head = NULL; - - while ((token = strsep (&p, " "))) { - address_t *a = xzalloc (sizeof (*a)); - - if (inet_aton (token, &a->address) == 0) { - logger (LOG_ERR, "%s: invalid address `%s'", var, token); - free_address (head); - free (a); - return (NULL); - } - - if (! head) { - head = xmalloc (sizeof (*head)); - STAILQ_INIT (head); - } - STAILQ_INSERT_TAIL (head, a, entries); - } - - return (head); -} - -bool read_info (const interface_t *iface, dhcp_t *dhcp) -{ - FILE *fp; - char *line; - char *var; - char *value; - char *p; - struct stat sb; - - if (stat (iface->infofile, &sb) != 0) { - logger (LOG_ERR, "lease information file `%s' does not exist", - iface->infofile); - return (false); - } - - if (! (fp = fopen (iface->infofile, "r"))) { - logger (LOG_ERR, "fopen `%s': %s", - iface->infofile, strerror (errno)); - return (false); - } - - dhcp->frominfo = true; - - while ((line = get_line (fp))) { - var = line; - - /* Strip leading spaces/tabs */ - while ((*var == ' ') || (*var == '\t')) - var++; - - /* Trim trailing \n */ - p = var + strlen (var) - 1; - if (*p == '\n') - *p = 0; - - /* Skip comments */ - if (*var == '#') - goto next; - - /* If we don't have an equals sign then skip it */ - if (! (p = strchr (var, '='))) - goto next; - - /* Terminate the = so we have two strings */ - *p = 0; - - value = p + 1; - /* Strip leading and trailing quotes if present */ - if (*value == '\'' || *value == '"') - value++; - p = value + strlen (value) - 1; - if (*p == '\'' || *p == '"') - *p = 0; - - /* Don't process null vars or values */ - if (! *var || ! *value) - goto next; - - if (strcmp (var, "IPADDR") == 0) - parse_address (&dhcp->address, value, "IPADDR"); - else if (strcmp (var, "NETMASK") == 0) - parse_address (&dhcp->netmask, value, "NETMASK"); - else if (strcmp (var, "BROADCAST") == 0) - parse_address (&dhcp->broadcast, value, "BROADCAST"); - else if (strcmp (var, "MTU") == 0) - parse_ushort (&dhcp->mtu, value, "MTU"); - else if (strcmp (var, "ROUTES") == 0) { - p = value; - while ((value = strsep (&p, " "))) { - char *pp = value; - char *dest = strsep (&pp, ","); - char *net = strsep (&pp, ","); - char *gate = strsep (&pp, ","); - route_t *route; - - if (! dest || ! net || ! gate) { - logger (LOG_ERR, "read_info ROUTES `%s,%s,%s': invalid route", - dest, net, gate); - goto next; - } - - /* See if we can create a route */ - route = xzalloc (sizeof (*route)); - if (inet_aton (dest, &route->destination) == 0) { - logger (LOG_ERR, "read_info ROUTES `%s': not a valid destination address", - dest); - free (route); - goto next; - } - if (inet_aton (dest, &route->netmask) == 0) { - logger (LOG_ERR, "read_info ROUTES `%s': not a valid netmask address", - net); - free (route); - goto next; - } - if (inet_aton (dest, &route->gateway) == 0) { - logger (LOG_ERR, "read_info ROUTES `%s': not a valid gateway address", - gate); - free (route); - goto next; - } - - /* OK, now add our route */ - if (! dhcp->routes) { - dhcp->routes = xmalloc (sizeof (*dhcp->routes)); - STAILQ_INIT (dhcp->routes); - } - STAILQ_INSERT_TAIL (dhcp->routes, route, entries); - } - } else if (strcmp (var, "GATEWAYS") == 0) { - p = value; - while ((value = strsep (&p, " "))) { - route_t *route = xzalloc (sizeof (*route)); - if (parse_address (&route->gateway, value, "GATEWAYS")) { - if (! dhcp->routes) { - dhcp->routes = xmalloc (sizeof (*dhcp->routes)); - STAILQ_INIT (dhcp->routes); - } - STAILQ_INSERT_TAIL (dhcp->routes, route, entries); - } else - free (route); - } - } else if (strcmp (var, "HOSTNAME") == 0) - dhcp->hostname = xstrdup (value); - else if (strcmp (var, "DNSDOMAIN") == 0) - dhcp->dnsdomain = xstrdup (value); - else if (strcmp (var, "DNSSEARCH") == 0) - dhcp->dnssearch = xstrdup (value); - else if (strcmp (var, "DNSSERVERS") == 0) - dhcp->dnsservers = parse_addresses (value, "DNSSERVERS"); - else if (strcmp (var, "NTPSERVERS") == 0) - dhcp->ntpservers = parse_addresses (value, "NTPSERVERS"); - else if (strcmp (var, "NISDOMAIN") == 0) - dhcp->nisdomain = xstrdup (value); - else if (strcmp (var, "NISSERVERS") == 0) - dhcp->nisservers = parse_addresses (value, "NISSERVERS"); - else if (strcmp (var, "ROOTPATH") == 0) - dhcp->rootpath = xstrdup (value); - else if (strcmp (var, "DHCPSID") == 0) - parse_address (&dhcp->serveraddress, value, "DHCPSID"); - else if (strcmp (var, "DHCPSNAME") == 0) - strlcpy (dhcp->servername, value, sizeof (dhcp->servername)); - else if (strcmp (var, "LEASEDFROM") == 0) - parse_uint (&dhcp->leasedfrom, value, "LEASEDFROM"); - else if (strcmp (var, "LEASETIME") == 0) - parse_uint (&dhcp->leasetime, value, "LEASETIME"); - else if (strcmp (var, "RENEWALTIME") == 0) - parse_uint (&dhcp->renewaltime, value, "RENEWALTIME"); - else if (strcmp (var, "REBINDTIME") == 0) - parse_uint (&dhcp->rebindtime, value, "REBINDTIME"); - -next: - free (line); - } - - fclose (fp); - return (true); -} - -#endif - diff --git a/workspace/networkDiscovery/dhcpcd/info.h b/workspace/networkDiscovery/dhcpcd/info.h deleted file mode 100644 index 22966db..0000000 --- a/workspace/networkDiscovery/dhcpcd/info.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef INFO_H -#define INFO_H - -#include "dhcpcd.h" -#include "interface.h" -#include "dhcp.h" - -#ifdef ENABLE_INFO -bool write_info (const interface_t *iface, const dhcp_t *dhcp, - const options_t *options, bool overwrite); - -bool read_info (const interface_t *iface, dhcp_t *dhcp); -#endif - -#endif diff --git a/workspace/networkDiscovery/dhcpcd/interface.c b/workspace/networkDiscovery/dhcpcd/interface.c deleted file mode 100644 index d2ff8d6..0000000 --- a/workspace/networkDiscovery/dhcpcd/interface.c +++ /dev/null @@ -1,1060 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include - -#include - -/* Netlink suff */ -#ifdef __linux__ -#include /* Needed for 2.4 kernels */ -#include -#include -#include -#include -#else -#include -#include -#include -#include -#endif - -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "common.h" -#include "dhcp.h" -#include "interface.h" -#include "logger.h" - -void free_address (struct address_head *addresses) -{ - address_t *p; - address_t *n; - - if (! addresses) - return; - - p = STAILQ_FIRST (addresses); - while (p) { - n = STAILQ_NEXT (p, entries); - free (p); - p = n; - } - free (addresses); -} - -void free_route (struct route_head *routes) -{ - route_t *p; - route_t *n; - - if (! routes) - return; - - p = STAILQ_FIRST (routes); - while (p) { - n = STAILQ_NEXT (p, entries); - free (p); - p = n; - } - free (routes); -} - -int inet_ntocidr (struct in_addr address) -{ - int cidr = 0; - uint32_t mask = htonl (address.s_addr); - - while (mask) { - cidr++; - mask <<= 1; - } - - return (cidr); -} - -int inet_cidrtoaddr (int cidr, struct in_addr *addr) { - int ocets; - - if (cidr < 0 || cidr > 32) { - errno = EINVAL; - return (-1); - } - ocets = (cidr + 7) / 8; - - memset (addr, 0, sizeof (*addr)); - if (ocets > 0) { - memset (&addr->s_addr, 255, (size_t) ocets - 1); - memset ((unsigned char *) &addr->s_addr + (ocets - 1), - (256 - (1 << (32 - cidr) % 8)), 1); - } - - return (0); -} - -uint32_t get_netmask (uint32_t addr) -{ - uint32_t dst; - - if (addr == 0) - return (0); - - dst = htonl (addr); - if (IN_CLASSA (dst)) - return (ntohl (IN_CLASSA_NET)); - if (IN_CLASSB (dst)) - return (ntohl (IN_CLASSB_NET)); - if (IN_CLASSC (dst)) - return (ntohl (IN_CLASSC_NET)); - - return (0); -} - -char *hwaddr_ntoa (const unsigned char *hwaddr, size_t hwlen) -{ - static char buffer[(HWADDR_LEN * 3) + 1]; - char *p = buffer; - size_t i; - - for (i = 0; i < hwlen && i < HWADDR_LEN; i++) { - if (i > 0) - *p ++= ':'; - p += snprintf (p, 3, "%.2x", hwaddr[i]); - } - - *p ++= '\0'; - - return (buffer); -} - -size_t hwaddr_aton (unsigned char *buffer, const char *addr) -{ - char c[3]; - const char *p = addr; - unsigned char *bp = buffer; - size_t len = 0; - - c[2] = '\0'; - while (*p) { - c[0] = *p++; - c[1] = *p++; - /* Ensure that next data is EOL or a seperator with data */ - if (! (*p == '\0' || (*p == ':' && *(p + 1) != '\0'))) { - errno = EINVAL; - return (0); - } - /* Ensure that digits are hex */ - if (isxdigit ((int) c[0]) == 0 || isxdigit ((int) c[1]) == 0) { - errno = EINVAL; - return (0); - } - p++; - if (bp) - *bp++ = (unsigned char) strtol (c, NULL, 16); - else - len++; - } - - if (bp) - return (bp - buffer); - return (len); -} - -static int _do_interface (const char *ifname, - _unused unsigned char *hwaddr, _unused size_t *hwlen, - struct in_addr *addr, - bool flush, bool get) -{ - int s; - struct ifconf ifc; - int retval = 0; - int len = 10 * sizeof (struct ifreq); - int lastlen = 0; - char *p; - - if ((s = socket (AF_INET, SOCK_DGRAM, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return -1; - } - - /* Not all implementations return the needed buffer size for - * SIOGIFCONF so we loop like so for all until it works */ - memset (&ifc, 0, sizeof (ifc)); - for (;;) { - ifc.ifc_len = len; - ifc.ifc_buf = xmalloc ((size_t) len); - if (ioctl (s, SIOCGIFCONF, &ifc) == -1) { - if (errno != EINVAL || lastlen != 0) { - logger (LOG_ERR, "ioctl SIOCGIFCONF: %s", - strerror (errno)); - close (s); - free (ifc.ifc_buf); - return -1; - } - } else { - if (ifc.ifc_len == lastlen) - break; - lastlen = ifc.ifc_len; - } - - free (ifc.ifc_buf); - ifc.ifc_buf = NULL; - len *= 2; - } - - for (p = ifc.ifc_buf; p < ifc.ifc_buf + ifc.ifc_len;) { - union { - char *buffer; - struct ifreq *ifr; - } ifreqs; - struct sockaddr_in address; - struct ifreq *ifr; - - /* Cast the ifc buffer to an ifreq cleanly */ - ifreqs.buffer = p; - ifr = ifreqs.ifr; - -#ifdef __linux__ - p += sizeof (*ifr); -#else - p += offsetof (struct ifreq, ifr_ifru) + ifr->ifr_addr.sa_len; -#endif - - if (strcmp (ifname, ifr->ifr_name) != 0) - continue; - -#ifdef AF_LINK - if (hwaddr && hwlen && ifr->ifr_addr.sa_family == AF_LINK) { - struct sockaddr_dl sdl; - - memcpy (&sdl, &ifr->ifr_addr, sizeof (sdl)); - *hwlen = sdl.sdl_alen; - memcpy (hwaddr, sdl.sdl_data + sdl.sdl_nlen, - (size_t) sdl.sdl_alen); - retval = 1; - break; - } -#endif - - if (ifr->ifr_addr.sa_family == AF_INET) { - memcpy (&address, &ifr->ifr_addr, sizeof (address)); - if (flush) { - struct sockaddr_in netmask; - - if (ioctl (s, SIOCGIFNETMASK, ifr) == -1) { - logger (LOG_ERR, - "ioctl SIOCGIFNETMASK: %s", - strerror (errno)); - continue; - } - memcpy (&netmask, &ifr->ifr_addr, - sizeof (netmask)); - - if (del_address (ifname, - address.sin_addr, - netmask.sin_addr) == -1) - retval = -1; - } else if (get) { - addr->s_addr = address.sin_addr.s_addr; - retval = 1; - break; - } else if (address.sin_addr.s_addr == addr->s_addr) { - retval = 1; - break; - } - } - - } - - close (s); - free (ifc.ifc_buf); - return retval; -} - -interface_t *read_interface (const char *ifname, _unused int metric) -{ - int s; - struct ifreq ifr; - interface_t *iface = NULL; - unsigned char *hwaddr = NULL; - size_t hwlen = 0; - sa_family_t family = 0; - unsigned short mtu; -#ifdef __linux__ - char *p; -#endif - - if (! ifname) - return NULL; - - memset (&ifr, 0, sizeof (ifr)); - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - - if ((s = socket (AF_INET, SOCK_DGRAM, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return NULL; - } - -#ifdef __linux__ - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - if (ioctl (s, SIOCGIFHWADDR, &ifr) == -1) { - logger (LOG_ERR, "ioctl SIOCGIFHWADDR: %s", strerror (errno)); - goto exit; - } - - switch (ifr.ifr_hwaddr.sa_family) { - case ARPHRD_ETHER: - case ARPHRD_IEEE802: - hwlen = ETHER_ADDR_LEN; - break; - case ARPHRD_IEEE1394: - hwlen = EUI64_ADDR_LEN; - case ARPHRD_INFINIBAND: - hwlen = INFINIBAND_ADDR_LEN; - break; - default: - logger (LOG_ERR, - "interface is not Ethernet, FireWire, " \ - "InfiniBand or Token Ring"); - goto exit; - } - - hwaddr = xmalloc (sizeof (unsigned char) * HWADDR_LEN); - memcpy (hwaddr, ifr.ifr_hwaddr.sa_data, hwlen); - family = ifr.ifr_hwaddr.sa_family; -#else - ifr.ifr_metric = metric; - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - if (ioctl (s, SIOCSIFMETRIC, &ifr) == -1) { - logger (LOG_ERR, "ioctl SIOCSIFMETRIC: %s", strerror (errno)); - goto exit; - } - - hwaddr = xmalloc (sizeof (unsigned char) * HWADDR_LEN); - if (_do_interface (ifname, hwaddr, &hwlen, NULL, false, false) != 1) { - logger (LOG_ERR, "could not find interface %s", ifname); - goto exit; - } - - family = ARPHRD_ETHER; -#endif - - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - if (ioctl (s, SIOCGIFMTU, &ifr) == -1) { - logger (LOG_ERR, "ioctl SIOCGIFMTU: %s", strerror (errno)); - goto exit; - } - - if (ifr.ifr_mtu < MTU_MIN) { - logger (LOG_DEBUG, "MTU of %d is too low, setting to %d", - ifr.ifr_mtu, MTU_MIN); - ifr.ifr_mtu = MTU_MIN; - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - if (ioctl (s, SIOCSIFMTU, &ifr) == -1) { - logger (LOG_ERR, "ioctl SIOCSIFMTU,: %s", - strerror (errno)); - goto exit; - } - } - mtu = ifr.ifr_mtu; - - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); -#ifdef __linux__ - /* We can only bring the real interface up */ - if ((p = strchr (ifr.ifr_name, ':'))) - *p = '\0'; -#endif - if (ioctl (s, SIOCGIFFLAGS, &ifr) == -1) { - logger (LOG_ERR, "ioctl SIOCGIFFLAGS: %s", strerror (errno)); - goto exit; - } - - if (! (ifr.ifr_flags & IFF_UP) || ! (ifr.ifr_flags & IFF_RUNNING)) { - ifr.ifr_flags |= IFF_UP | IFF_RUNNING; - if (ioctl (s, SIOCSIFFLAGS, &ifr) != 0) { - logger (LOG_ERR, "ioctl SIOCSIFFLAGS: %s", - strerror (errno)); - goto exit; - } - } - - iface = xzalloc (sizeof (*iface)); - strlcpy (iface->name, ifname, IF_NAMESIZE); -#ifdef ENABLE_INFO - snprintf (iface->infofile, PATH_MAX, INFOFILE, ifname); -#endif - memcpy (&iface->hwaddr, hwaddr, hwlen); - iface->hwlen = hwlen; - - iface->family = family; - iface->arpable = ! (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)); - iface->mtu = iface->previous_mtu = mtu; - - logger (LOG_INFO, "hardware address = %s", - hwaddr_ntoa (iface->hwaddr, iface->hwlen)); - - /* 0 is a valid fd, so init to -1 */ - iface->fd = -1; -#ifdef __linux__ - iface->listen_fd = -1; -#endif - -exit: - close (s); - free (hwaddr); - return iface; -} - -int get_mtu (const char *ifname) -{ - struct ifreq ifr; - int r; - int s; - - if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return (-1); - } - - memset (&ifr, 0, sizeof (ifr)); - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - r = ioctl (s, SIOCGIFMTU, &ifr); - close (s); - - if (r == -1) { - logger (LOG_ERR, "ioctl SIOCGIFMTU: %s", strerror (errno)); - return (-1); - } - - return (ifr.ifr_mtu); -} - -int set_mtu (const char *ifname, short int mtu) -{ - struct ifreq ifr; - int r; - int s; - - if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return (-1); - } - - memset (&ifr, 0, sizeof (ifr)); - logger (LOG_DEBUG, "setting MTU to %d", mtu); - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - ifr.ifr_mtu = mtu; - r = ioctl (s, SIOCSIFMTU, &ifr); - close (s); - - if (r == -1) - logger (LOG_ERR, "ioctl SIOCSIFMTU: %s", strerror (errno)); - - return (r == 0 ? 0 : -1); -} - -static void log_route (struct in_addr destination, - struct in_addr netmask, - struct in_addr gateway, - _unused int metric, - int change, int del) -{ - char *dstd = xstrdup (inet_ntoa (destination)); - -#ifdef __linux__ -#define METRIC " metric %d" -#else -#define METRIC "" -#endif - - if (gateway.s_addr == destination.s_addr || - gateway.s_addr == INADDR_ANY) - logger (LOG_INFO, "%s route to %s/%d" METRIC, - change ? "changing" : del ? "removing" : "adding", - dstd, inet_ntocidr (netmask) -#ifdef __linux__ - , metric -#endif - ); - else if (destination.s_addr == INADDR_ANY) - logger (LOG_INFO, "%s default route via %s" METRIC, - change ? "changing" : del ? "removing" : "adding", - inet_ntoa (gateway) - -#ifdef __linux__ - , metric -#endif - ); - else - logger (LOG_INFO, "%s route to %s/%d via %s" METRIC, - change ? "changing" : del ? "removing" : "adding", - dstd, inet_ntocidr (netmask), inet_ntoa (gateway) -#ifdef __linux__ - , metric -#endif - ); - - free (dstd); -} - -#if defined(BSD) || defined(__FreeBSD_kernel__) - -/* Darwin doesn't define this for some very odd reason */ -#ifndef SA_SIZE -# define SA_SIZE(sa) \ - ( (!(sa) || ((struct sockaddr *)(sa))->sa_len == 0) ? \ - sizeof(long) : \ - 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(long) - 1) ) ) -#endif - -static int do_address (const char *ifname, struct in_addr address, - struct in_addr netmask, struct in_addr broadcast, - int del) -{ - int s; - struct ifaliasreq ifa; - - if (! ifname) - return -1; - - if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return -1; - } - - memset (&ifa, 0, sizeof (ifa)); - strlcpy (ifa.ifra_name, ifname, sizeof (ifa.ifra_name)); - -#define ADDADDR(_var, _addr) { \ - union { struct sockaddr *sa; struct sockaddr_in *sin; } _s; \ - _s.sa = &_var; \ - _s.sin->sin_family = AF_INET; \ - _s.sin->sin_len = sizeof (*_s.sin); \ - memcpy (&_s.sin->sin_addr, &_addr, sizeof (_s.sin->sin_addr)); \ -} - - ADDADDR (ifa.ifra_addr, address); - ADDADDR (ifa.ifra_mask, netmask); -if (! del) - ADDADDR (ifa.ifra_broadaddr, broadcast); - -#undef ADDADDR - - if (ioctl (s, del ? SIOCDIFADDR : SIOCAIFADDR, &ifa) == -1) { - logger (LOG_ERR, "ioctl %s: %s", - del ? "SIOCDIFADDR" : "SIOCAIFADDR", - strerror (errno)); - close (s); - return -1; - } - -close (s); -return 0; -} - -static int do_route (const char *ifname, - struct in_addr destination, - struct in_addr netmask, - struct in_addr gateway, - int metric, - int change, int del) -{ - int s; - static int seq; - union sockunion { - struct sockaddr sa; - struct sockaddr_in sin; -#ifdef INET6 - struct sockaddr_in6 sin6; -#endif - struct sockaddr_dl sdl; - struct sockaddr_storage ss; - } su; - struct rtm - { - struct rt_msghdr hdr; - char buffer[sizeof (su) * 3]; - } rtm; - char *bp = rtm.buffer; - size_t l; - - if (! ifname) - return -1; - - log_route (destination, netmask, gateway, metric, change, del); - - if ((s = socket (PF_ROUTE, SOCK_RAW, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return -1; - } - - memset (&rtm, 0, sizeof (rtm)); - - rtm.hdr.rtm_version = RTM_VERSION; - rtm.hdr.rtm_seq = ++seq; - rtm.hdr.rtm_type = change ? RTM_CHANGE : del ? RTM_DELETE : RTM_ADD; - rtm.hdr.rtm_flags = RTF_UP | RTF_STATIC; - - /* This order is important */ - rtm.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK; - -#define ADDADDR(_addr) \ - memset (&su, 0, sizeof (su)); \ - su.sin.sin_family = AF_INET; \ - su.sin.sin_len = sizeof (su.sin); \ - memcpy (&su.sin.sin_addr, &_addr, sizeof (su.sin.sin_addr)); \ - l = SA_SIZE (&(su.sa)); \ - memcpy (bp, &(su), l); \ - bp += l; - - ADDADDR (destination); - - if (netmask.s_addr == INADDR_BROADCAST || - gateway.s_addr == INADDR_ANY) - { - /* Make us a link layer socket */ - unsigned char *hwaddr; - size_t hwlen = 0; - - if (netmask.s_addr == INADDR_BROADCAST) - rtm.hdr.rtm_flags |= RTF_HOST; - - hwaddr = xmalloc (sizeof (unsigned char) * HWADDR_LEN); - _do_interface (ifname, hwaddr, &hwlen, NULL, false, false); - memset (&su, 0, sizeof (su)); - su.sdl.sdl_len = sizeof (su.sdl); - su.sdl.sdl_family = AF_LINK; - su.sdl.sdl_nlen = strlen (ifname); - memcpy (&su.sdl.sdl_data, ifname, (size_t) su.sdl.sdl_nlen); - su.sdl.sdl_alen = hwlen; - memcpy (((unsigned char *) &su.sdl.sdl_data) + su.sdl.sdl_nlen, - hwaddr, (size_t) su.sdl.sdl_alen); - - l = SA_SIZE (&(su.sa)); - memcpy (bp, &su, l); - bp += l; - free (hwaddr); - } else { - rtm.hdr.rtm_flags |= RTF_GATEWAY; - ADDADDR (gateway); - } - - ADDADDR (netmask); -#undef ADDADDR - - rtm.hdr.rtm_msglen = l = bp - (char *)&rtm; - if (write (s, &rtm, l) == -1) { - /* Don't report error about routes already existing */ - if (errno != EEXIST) - logger (LOG_ERR, "write: %s", strerror (errno)); - close (s); - return -1; - } - - close (s); - return 0; -} - -#elif __linux__ -/* This netlink stuff is overly compex IMO. - * The BSD implementation is much cleaner and a lot less code. - * send_netlink handles the actual transmission so we can work out - * if there was an error or not. */ -#define BUFFERLEN 256 -int send_netlink (struct nlmsghdr *hdr, netlink_callback callback, void *arg) -{ - int s; - pid_t mypid = getpid (); - struct sockaddr_nl nl; - struct iovec iov; - struct msghdr msg; - static unsigned int seq; - char *buffer; - ssize_t bytes; - union - { - char *buffer; - struct nlmsghdr *nlm; - } h; - - if ((s = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return -1; - } - - memset (&nl, 0, sizeof (nl)); - nl.nl_family = AF_NETLINK; - if (bind (s, (struct sockaddr *) &nl, sizeof (nl)) == -1) { - logger (LOG_ERR, "bind: %s", strerror (errno)); - close (s); - return -1; - } - - memset (&iov, 0, sizeof (iov)); - iov.iov_base = hdr; - iov.iov_len = hdr->nlmsg_len; - - memset (&msg, 0, sizeof (msg)); - msg.msg_name = &nl; - msg.msg_namelen = sizeof (nl); - msg.msg_iov = &iov; - msg.msg_iovlen = 1; - - /* Request a reply */ - hdr->nlmsg_flags |= NLM_F_ACK; - hdr->nlmsg_seq = ++seq; - - if (sendmsg (s, &msg, 0) == -1) { - logger (LOG_ERR, "write: %s", strerror (errno)); - close (s); - return -1; - } - - buffer = xzalloc (sizeof (char) * BUFFERLEN); - iov.iov_base = buffer; - - for (;;) { - iov.iov_len = BUFFERLEN; - bytes = recvmsg (s, &msg, 0); - - if (bytes == -1) { - if (errno != EINTR) - logger (LOG_ERR, "recvmsg: %s", - strerror (errno)); - continue; - } - - if (bytes == 0) { - logger (LOG_ERR, "netlink: EOF"); - goto eexit; - } - - if (msg.msg_namelen != sizeof (nl)) { - logger (LOG_ERR, - "netlink: sender address length mismatch"); - goto eexit; - } - - for (h.buffer = buffer; bytes >= (signed) sizeof (*h.nlm); ) { - int len = h.nlm->nlmsg_len; - int l = len - sizeof (*h.nlm); - struct nlmsgerr *err = (struct nlmsgerr *) NLMSG_DATA (h.nlm); - - if (l < 0 || len > bytes) { - if (msg.msg_flags & MSG_TRUNC) - logger (LOG_ERR, "netlink: truncated message"); - else - logger (LOG_ERR, "netlink: malformed message"); - goto eexit; - } - - /* Ensure it's our message */ - if (nl.nl_pid != 0 || - (pid_t) h.nlm->nlmsg_pid != mypid || - h.nlm->nlmsg_seq != seq) - { - /* Next Message */ - bytes -= NLMSG_ALIGN (len); - h.buffer += NLMSG_ALIGN (len); - continue; - } - - /* We get an NLMSG_ERROR back with a code of zero for success */ - if (h.nlm->nlmsg_type != NLMSG_ERROR) { - logger (LOG_ERR, "netlink: unexpected reply %d", - h.nlm->nlmsg_type); - goto eexit; - } - - if ((unsigned) l < sizeof (*err)) { - logger (LOG_ERR, "netlink: error truncated"); - goto eexit; - } - - if (err->error == 0) { - int retval = 0; - - close (s); - if (callback) { - if ((retval = callback (hdr, arg)) == -1) - logger (LOG_ERR, "netlink: callback failed"); - } - free (buffer); - return (retval); - } - - errno = -err->error; - /* Don't report on something already existing */ - if (errno != EEXIST) - logger (LOG_ERR, "netlink: %s", - strerror (errno)); - goto eexit; - } - } - -eexit: - close (s); - free (buffer); - return -1; -} - -#define NLMSG_TAIL(nmsg) \ - ((struct rtattr *) (((ptrdiff_t) (nmsg)) + NLMSG_ALIGN ((nmsg)->nlmsg_len))) - -static int add_attr_l(struct nlmsghdr *n, unsigned int maxlen, int type, - const void *data, int alen) -{ - int len = RTA_LENGTH(alen); - struct rtattr *rta; - - if (NLMSG_ALIGN (n->nlmsg_len) + RTA_ALIGN (len) > maxlen) { - logger (LOG_ERR, "add_attr_l: message exceeded bound of %d\n", - maxlen); - return -1; - } - - rta = NLMSG_TAIL (n); - rta->rta_type = type; - rta->rta_len = len; - memcpy (RTA_DATA (rta), data, alen); - n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + RTA_ALIGN (len); - - return 0; -} - -static int add_attr_32(struct nlmsghdr *n, unsigned int maxlen, int type, - uint32_t data) -{ - int len = RTA_LENGTH (sizeof (data)); - struct rtattr *rta; - - if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen) { - logger (LOG_ERR, "add_attr32: message exceeded bound of %d\n", - maxlen); - return -1; - } - - rta = NLMSG_TAIL (n); - rta->rta_type = type; - rta->rta_len = len; - memcpy (RTA_DATA (rta), &data, sizeof (data)); - n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len; - - return 0; -} - -struct nlma -{ - struct nlmsghdr hdr; - struct ifaddrmsg ifa; - char buffer[64]; -}; - -struct nlmr -{ - struct nlmsghdr hdr; - struct rtmsg rt; - char buffer[256]; -}; - -static int do_address(const char *ifname, - struct in_addr address, struct in_addr netmask, - struct in_addr broadcast, int del) -{ - struct nlma *nlm; - int retval; - - if (!ifname) - return -1; - - nlm = xzalloc (sizeof (*nlm)); - nlm->hdr.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg)); - nlm->hdr.nlmsg_flags = NLM_F_REQUEST; - if (! del) - nlm->hdr.nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE; - nlm->hdr.nlmsg_type = del ? RTM_DELADDR : RTM_NEWADDR; - if (! (nlm->ifa.ifa_index = if_nametoindex (ifname))) { - logger (LOG_ERR, "if_nametoindex: no index for interface `%s'", - ifname); - free (nlm); - return -1; - } - nlm->ifa.ifa_family = AF_INET; - - nlm->ifa.ifa_prefixlen = inet_ntocidr (netmask); - - /* This creates the aliased interface */ - add_attr_l (&nlm->hdr, sizeof (*nlm), IFA_LABEL, - ifname, strlen (ifname) + 1); - - add_attr_l (&nlm->hdr, sizeof (*nlm), IFA_LOCAL, - &address.s_addr, sizeof (address.s_addr)); - if (! del) - add_attr_l (&nlm->hdr, sizeof (*nlm), IFA_BROADCAST, - &broadcast.s_addr, sizeof (broadcast.s_addr)); - - retval = send_netlink (&nlm->hdr, NULL, NULL); - free (nlm); - return retval; -} - -static int do_route (const char *ifname, - struct in_addr destination, - struct in_addr netmask, - struct in_addr gateway, - int metric, int change, int del) -{ - struct nlmr *nlm; - unsigned int ifindex; - int retval; - - if (! ifname) - return -1; - - log_route (destination, netmask, gateway, metric, change, del); - - if (! (ifindex = if_nametoindex (ifname))) { - logger (LOG_ERR, "if_nametoindex: no index for interface `%s'", - ifname); - return -1; - } - - nlm = xzalloc (sizeof (*nlm)); - nlm->hdr.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg)); - if (change) - nlm->hdr.nlmsg_flags = NLM_F_REPLACE; - else if (! del) - nlm->hdr.nlmsg_flags = NLM_F_CREATE | NLM_F_EXCL; - nlm->hdr.nlmsg_flags |= NLM_F_REQUEST; - nlm->hdr.nlmsg_type = del ? RTM_DELROUTE : RTM_NEWROUTE; - nlm->rt.rtm_family = AF_INET; - nlm->rt.rtm_table = RT_TABLE_MAIN; - - if (del) - nlm->rt.rtm_scope = RT_SCOPE_NOWHERE; - else { - nlm->hdr.nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL; - nlm->rt.rtm_protocol = RTPROT_BOOT; - if (netmask.s_addr == INADDR_BROADCAST || - gateway.s_addr == INADDR_ANY) - nlm->rt.rtm_scope = RT_SCOPE_LINK; - else - nlm->rt.rtm_scope = RT_SCOPE_UNIVERSE; - nlm->rt.rtm_type = RTN_UNICAST; - } - - nlm->rt.rtm_dst_len = inet_ntocidr (netmask); - add_attr_l (&nlm->hdr, sizeof (*nlm), RTA_DST, - &destination.s_addr, sizeof (destination.s_addr)); - if (netmask.s_addr != INADDR_BROADCAST && - destination.s_addr != gateway.s_addr) - add_attr_l (&nlm->hdr, sizeof (*nlm), RTA_GATEWAY, - &gateway.s_addr, sizeof (gateway.s_addr)); - - add_attr_32 (&nlm->hdr, sizeof (*nlm), RTA_OIF, ifindex); - add_attr_32 (&nlm->hdr, sizeof (*nlm), RTA_PRIORITY, metric); - - retval = send_netlink (&nlm->hdr, NULL, NULL); - free (nlm); - return retval; -} - -#else - #error "Platform not supported!" - #error "We currently support BPF and Linux sockets." - #error "Other platforms may work using BPF. If yours does, please let me know" - #error "so I can add it to our list." -#endif - -int add_address (const char *ifname, struct in_addr address, - struct in_addr netmask, struct in_addr broadcast) -{ - logger (LOG_INFO, "adding IP address %s/%d", - inet_ntoa (address), inet_ntocidr (netmask)); - - return (do_address (ifname, address, netmask, broadcast, 0)); -} - -int del_address (const char *ifname, - struct in_addr address, struct in_addr netmask) -{ - struct in_addr t; - - logger (LOG_INFO, "removing IP address %s/%d", - inet_ntoa (address), inet_ntocidr (netmask)); - - memset (&t, 0, sizeof (t)); - return (do_address (ifname, address, netmask, t, 1)); -} - -int add_route (const char *ifname, struct in_addr destination, - struct in_addr netmask, struct in_addr gateway, int metric) -{ - return (do_route (ifname, destination, netmask, gateway, metric, 0, 0)); -} - -int change_route (const char *ifname, struct in_addr destination, - struct in_addr netmask, struct in_addr gateway, int metric) -{ - return (do_route (ifname, destination, netmask, gateway, metric, 1, 0)); -} - -int del_route (const char *ifname, struct in_addr destination, - struct in_addr netmask, struct in_addr gateway, int metric) -{ - return (do_route (ifname, destination, netmask, gateway, metric, 0, 1)); -} - - -int flush_addresses (const char *ifname) -{ - return (_do_interface (ifname, NULL, NULL, NULL, true, false)); -} - -in_addr_t get_address (const char *ifname) -{ - struct in_addr address; - if (_do_interface (ifname, NULL, NULL, &address, false, true) > 0) - return (address.s_addr); - return (0); -} - -int has_address (const char *ifname, struct in_addr address) -{ - return (_do_interface (ifname, NULL, NULL, &address, false, false)); -} diff --git a/workspace/networkDiscovery/dhcpcd/interface.h b/workspace/networkDiscovery/dhcpcd/interface.h deleted file mode 100644 index 8215d48..0000000 --- a/workspace/networkDiscovery/dhcpcd/interface.h +++ /dev/null @@ -1,173 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef INTERFACE_H -#define INTERFACE_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" - -#ifdef __linux__ -# include -#endif - -#ifdef ENABLE_DUID -#ifndef DUID_LEN -# define DUID_LEN 128 + 2 -#endif -#endif - -#define EUI64_ADDR_LEN 8 -#define INFINIBAND_ADDR_LEN 20 - -/* Linux 2.4 doesn't define this */ -#ifndef ARPHRD_IEEE1394 -# define ARPHRD_IEEE1394 24 -#endif - -/* The BSD's don't define this yet */ -#ifndef ARPHRD_INFINIBAND -# define ARPHRD_INFINIBAND 32 -#endif - -#define HWADDR_LEN 20 - -/* Work out if we have a private address or not - * 10/8 - * 172.16/12 - * 192.168/16 - */ -#ifndef IN_PRIVATE -# define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) || \ - ((addr & 0xfff00000) == 0xac100000) || \ - ((addr & IN_CLASSB_NET) == 0xc0a80000)) -#endif - -#define LINKLOCAL_ADDR 0xa9fe0000 -#define LINKLOCAL_MASK 0xffff0000 -#define LINKLOCAL_BRDC 0xa9feffff - -#ifndef IN_LINKLOCAL -# define IN_LINKLOCAL(addr) ((addr & IN_CLASSB_NET) == LINKLOCAL_ADDR) -#endif - -#ifndef STAILQ_ENTRY -# error "your sys/queue.h is too old and lacks STAILQ" -#endif - -#define NSTAILQ_FOREACH(var, head, field) \ - if (head) STAILQ_FOREACH (var, head, field) - -typedef struct route_t -{ - struct in_addr destination; - struct in_addr netmask; - struct in_addr gateway; - STAILQ_ENTRY (route_t) entries; -} route_t; -STAILQ_HEAD (route_head, route_t); - -typedef struct address_t -{ - struct in_addr address; - STAILQ_ENTRY (address_t) entries; -} address_t; -STAILQ_HEAD (address_head, address_t); - -typedef struct interface_t -{ - char name[IF_NAMESIZE]; - sa_family_t family; - unsigned char hwaddr[HWADDR_LEN]; - size_t hwlen; - bool arpable; - unsigned short mtu; - - int fd; - size_t buffer_length; - -#ifdef __linux__ - int listen_fd; - int socket_protocol; -#endif - - char infofile[PATH_MAX]; - - unsigned short previous_mtu; - struct in_addr previous_address; - struct in_addr previous_netmask; - struct route_head *previous_routes; - - time_t start_uptime; - - unsigned char *clientid; - size_t clientid_len; -} interface_t; - -void free_address (struct address_head *addresses); -void free_route (struct route_head *routes); -uint32_t get_netmask (uint32_t addr); -char *hwaddr_ntoa (const unsigned char *hwaddr, size_t hwlen); -size_t hwaddr_aton (unsigned char *hwaddr, const char *addr); - -interface_t *read_interface (const char *ifname, int metric); -int get_mtu (const char *ifname); -int set_mtu (const char *ifname, short int mtu); - -int add_address (const char *ifname, struct in_addr address, - struct in_addr netmask, struct in_addr broadcast); -int del_address (const char *ifname, struct in_addr address, - struct in_addr netmask); - -int flush_addresses (const char *ifname); -in_addr_t get_address (const char *ifname); -int has_address (const char *ifname, struct in_addr address); - -int add_route (const char *ifname, struct in_addr destination, - struct in_addr netmask, struct in_addr gateway, int metric); -int change_route (const char *ifname, struct in_addr destination, - struct in_addr netmask, struct in_addr gateway, int metric); -int del_route (const char *ifname, struct in_addr destination, - struct in_addr netmask, struct in_addr gateway, int metric); - -int inet_ntocidr (struct in_addr address); -int inet_cidrtoaddr (int cidr, struct in_addr *addr); - -#ifdef __linux__ -typedef int (*netlink_callback) (struct nlmsghdr *hdr, void *arg); -int send_netlink (struct nlmsghdr *hdr, netlink_callback callback, void *arg); -#endif -#endif diff --git a/workspace/networkDiscovery/dhcpcd/ipv4ll.c b/workspace/networkDiscovery/dhcpcd/ipv4ll.c deleted file mode 100644 index 9742b9a..0000000 --- a/workspace/networkDiscovery/dhcpcd/ipv4ll.c +++ /dev/null @@ -1,70 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include - -#include "config.h" -#include "arp.h" -#include "ipv4ll.h" - -#ifdef ENABLE_IPV4LL - -#ifndef ENABLE_ARP - # error IPV4LL requires ARP -#endif - -#define IPV4LL_LEASETIME 20 - -int ipv4ll_get_address (interface_t *iface, dhcp_t *dhcp) { - struct in_addr addr; - - for (;;) { - addr.s_addr = htonl (LINKLOCAL_ADDR | - (((uint32_t) abs ((int) random ()) - % 0xFD00) + 0x0100)); - errno = 0; - if (! arp_claim (iface, addr)) - break; - /* Our ARP may have been interrupted */ - if (errno) - return (-1); - } - - dhcp->address.s_addr = addr.s_addr; - dhcp->netmask.s_addr = htonl (LINKLOCAL_MASK); - dhcp->broadcast.s_addr = htonl (LINKLOCAL_BRDC); - - /* Finally configure some DHCP like lease times */ - dhcp->leasetime = IPV4LL_LEASETIME; - dhcp->renewaltime = (dhcp->leasetime * 0.5); - dhcp->rebindtime = (dhcp->leasetime * 0.875); - - return (0); -} - -#endif diff --git a/workspace/networkDiscovery/dhcpcd/ipv4ll.h b/workspace/networkDiscovery/dhcpcd/ipv4ll.h deleted file mode 100644 index 4fa8943..0000000 --- a/workspace/networkDiscovery/dhcpcd/ipv4ll.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef IPV4LL_H -#define IPV4LL_H - -#ifdef ENABLE_IPV4LL - -#include "dhcp.h" -#include "interface.h" - -int ipv4ll_get_address (interface_t *iface, dhcp_t *dhcp); - -#endif -#endif diff --git a/workspace/networkDiscovery/dhcpcd/logger.c b/workspace/networkDiscovery/dhcpcd/logger.c deleted file mode 100644 index ec10bb9..0000000 --- a/workspace/networkDiscovery/dhcpcd/logger.c +++ /dev/null @@ -1,158 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#define SYSLOG_NAMES - -#define COM_CH "/var/tmp/com.socket" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#include "common.h" -#include "logger.h" - -/*sockets for the logger and the qt-reader */ -int sockfd, ns; - -static int loglevel = LOG_WARNING; -static char logprefix[12] = {0}; - -int logtolevel (const char *priority) -{ - CODE *c; - - if (isdigit ((int) *priority)) - return (atoi (priority)); - - for (c = prioritynames; c->c_name; c++) - if (! strcasecmp (priority, c->c_name)) - return (c->c_val); - - return (-1); -} - -static const char *leveltolog (int level) { - CODE *c; - - for (c = prioritynames; c->c_name; c++) - if (c->c_val == level) - return (c->c_name); - - return (NULL); -} - -void setloglevel (int level) -{ - loglevel = level; -} - -void setlogprefix (const char *prefix) -{ - snprintf (logprefix, sizeof (logprefix), "%s", prefix); -} - -void logger (int level, const char *fmt, ...) -{ - va_list p; - va_list p2; - FILE *f = stderr; - - va_start (p, fmt); - va_copy (p2, p); - - if (level <= LOG_ERR || level <= loglevel) { - if (level == LOG_DEBUG || level == LOG_INFO) - f = stdout; - fprintf (f, "%s, %s", leveltolog (level), logprefix); - vfprintf (f, fmt, p); - fputc ('\n', f); - - /* stdout, stderr may be re-directed to some kind of buffer. - * So we always flush to ensure it's written. */ - fflush (f); - } - - if (level < LOG_DEBUG || level <= loglevel) { - size_t len = strlen (logprefix); - size_t fmt2len = strlen (fmt) + len + 1; - char *fmt2 = malloc (sizeof (char) * fmt2len); - char *pf = fmt2; - if (fmt2) { - memcpy (pf, logprefix, len); - pf += len; - strlcpy (pf, fmt, fmt2len - len); - vsyslog (level, fmt2, p2); - free (fmt2); - } else { - vsyslog (level, fmt, p2); - syslog (LOG_ERR, "logger: memory exhausted"); - exit (EXIT_FAILURE); - } - } - - va_end (p2); - va_end (p); -} - -void initQtLoggerSocket() { - /** - * new code. seems to be right. - */ - struct sockaddr_un serv_addr; - sockfd = socket(AF_UNIX, SOCK_STREAM, 0); - if (sockfd < 0) - fprintf(stdout,"ERROR opening socket"); - serv_addr.sun_family = AF_UNIX; - strcpy(serv_addr.sun_path, "/var/tmp/qt_c_socket_test"); - - if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) - fprintf(stdout,"ERROR connecting"); -} - -void closeQtLoggerSocket (){ - close(s); -} - -void sendToQt(char *msg) { - if (send(ns, msg, strlen(msg), 0) < 0) { - fprintf(stdout, "logger: failed to send message: %s", msg); - } - //char buffer[256]; - char *b = "test"; - int n = write(sockfd, b, strlen(b)); - if (n < 0) - fprintf(stdout, "ERROR writing to socket"); -} - diff --git a/workspace/networkDiscovery/dhcpcd/logger.h b/workspace/networkDiscovery/dhcpcd/logger.h deleted file mode 100644 index e42e020..0000000 --- a/workspace/networkDiscovery/dhcpcd/logger.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef LOGGER_H -#define LOGGER_H - -#if defined(__GNUC__) -# define _PRINTF_LIKE(_one, _two) __attribute__ ((__format__ (__printf__, _one, _two))) -#else -# define _PRINTF_LIKE(_one, _two) -#endif - -#include - - - -int logtolevel (const char *priority); -void setloglevel (int level); -void setlogprefix (const char *prefix); -void logger (int level, const char *fmt, ...) _PRINTF_LIKE (2, 3); - -/** - * new functions for communicating with Qt - */ -void initQtLoggerSocket (); -void closeQtLoggerSocket (); -void sendToQt (); - -#endif diff --git a/workspace/networkDiscovery/dhcpcd/signal.c b/workspace/networkDiscovery/dhcpcd/signal.c deleted file mode 100644 index 9055c9f..0000000 --- a/workspace/networkDiscovery/dhcpcd/signal.c +++ /dev/null @@ -1,183 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include - -#include "common.h" -#include "logger.h" -#include "signal.h" - -static int signal_pipe[2]; -static int signals[5]; - -static const int handle_sigs[] = { - SIGHUP, - SIGALRM, - SIGTERM, - SIGINT -}; - -static void signal_handler (int sig) -{ - unsigned int i = 0; - int serrno = errno; - - /* Add a signal to our stack */ - while (signals[i]) - i++; - if (i > sizeof (signals) / sizeof (signals[0])) - logger (LOG_ERR, "signal buffer overrun"); - else - signals[i] = sig; - - if (write (signal_pipe[1], &sig, sizeof (sig)) == -1) - logger (LOG_ERR, "Could not send signal: %s", strerror (errno)); - - /* Restore errno */ - errno = serrno; -} - -int signal_fd (void) -{ - return (signal_pipe[0]); -} - -/* Check if we have a signal or not */ -int signal_exists (const struct pollfd *fd) -{ - if (signals[0] || (fd && fd->revents & POLLIN)) - return (0); - return (-1); -} - -/* Read a signal from the signal pipe. Returns 0 if there is - * no signal, -1 on error (and sets errno appropriately), and - * your signal on success */ -int signal_read (struct pollfd *fd) -{ - int sig = -1; - - /* Pop a signal off the our stack */ - if (signals[0]) { - unsigned int i = 0; - sig = signals[0]; - while (i < (sizeof (signals) / sizeof (signals[0])) - 1) { - signals[i] = signals[i + 1]; - if (! signals[++i]) - break; - } - } - - if (fd && fd->revents & POLLIN) { - char buf[16]; - size_t bytes; - - memset (buf, 0, sizeof (buf)); - bytes = read (signal_pipe[0], buf, sizeof (buf)); - - if (bytes >= sizeof (sig)) - memcpy (&sig, buf, sizeof (sig)); - - /* We need to clear us from rset if nothing left in the buffer - * in case we are called many times */ - if (bytes == sizeof (sig)) - fd->revents = 0; - } - - return (sig); -} - -/* Call this before doing anything else. Sets up the socket pair - * and installs the signal handler */ -int signal_init (void) -{ - struct sigaction sa; - - if (pipe (signal_pipe) == -1) { - logger (LOG_ERR, "pipe: %s", strerror (errno)); - return (-1); - } - - /* Stop any scripts from inheriting us */ - close_on_exec (signal_pipe[0]); - close_on_exec (signal_pipe[1]); - - /* Ignore child signals and don't make zombies. - * Because we do this, we don't need to be in signal_setup */ - memset (&sa, 0, sizeof (sa)); - sa.sa_handler = SIG_DFL; - sa.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT; - if (sigaction (SIGCHLD, &sa, NULL) == -1) { - logger (LOG_ERR, "sigaction: %s", strerror (errno)); - return (-1); - } - - memset (signals, 0, sizeof (signals)); - return (0); -} - -int signal_setup (void) -{ - unsigned int i; - struct sigaction sa; - - memset (&sa, 0, sizeof (sa)); - sa.sa_handler = signal_handler; - sigemptyset (&sa.sa_mask); - - for (i = 0; i < sizeof (handle_sigs) / sizeof (handle_sigs[0]); i++) - if (sigaction (handle_sigs[i], &sa, NULL) == -1) { - logger (LOG_ERR, "sigaction: %s", strerror (errno)); - return (-1); - } - - return (0); -} - -int signal_reset (void) -{ - struct sigaction sa; - unsigned int i; - - memset (&sa, 0, sizeof (sa)); - sa.sa_handler = SIG_DFL; - sigemptyset (&sa.sa_mask); - - for (i = 0; i < sizeof (handle_sigs) / sizeof (handle_sigs[0]); i++) - if (sigaction (handle_sigs[i], &sa, NULL) == -1) { - logger (LOG_ERR, "sigaction: %s", strerror (errno)); - return (-1); - } - - return (0); -} diff --git a/workspace/networkDiscovery/dhcpcd/signal.h b/workspace/networkDiscovery/dhcpcd/signal.h deleted file mode 100644 index 63a5906..0000000 --- a/workspace/networkDiscovery/dhcpcd/signal.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef SIGNAL_H -#define SIGNAL_H - -#include - -int signal_init (void); -int signal_setup (void); -int signal_reset (void); -int signal_fd (void); -int signal_exists (const struct pollfd *fd); -int signal_read (struct pollfd *fd); - -#endif diff --git a/workspace/networkDiscovery/dhcpcd/socket.c b/workspace/networkDiscovery/dhcpcd/socket.c deleted file mode 100644 index 58ad6c5..0000000 --- a/workspace/networkDiscovery/dhcpcd/socket.c +++ /dev/null @@ -1,647 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#define __FAVOR_BSD /* Nasty hack so we can use BSD semantics for UDP */ -#include -#undef __FAVOR_BSD -#include -#include -#include -#include -#include -#include -#include -#include - -#if defined(BSD) || defined(__FreeBSD_kernel__) -# include -#elif __linux__ -# include -# include -# define bpf_insn sock_filter -#endif - -#include "config.h" -#include "dhcp.h" -#include "interface.h" -#include "logger.h" -#include "socket.h" - -/* A suitably large buffer for all transactions. - * BPF buffer size is set by the kernel, so no define. */ -#ifdef __linux__ -# define BUFFER_LENGTH 4096 -#endif - -/* Broadcast address for IPoIB */ -static const uint8_t ipv4_bcast_addr[] = { - 0x00, 0xff, 0xff, 0xff, - 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff -}; - -/* Credit where credit is due :) - * The below BPF filter is taken from ISC DHCP */ -static struct bpf_insn dhcp_bpf_filter [] = { - /* Make sure this is an IP packet... */ - BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 12), - BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8), - - /* Make sure it's a UDP packet... */ - BPF_STMT (BPF_LD + BPF_B + BPF_ABS, 23), - BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6), - - /* Make sure this isn't a fragment... */ - BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 20), - BPF_JUMP (BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0), - - /* Get the IP header length... */ - BPF_STMT (BPF_LDX + BPF_B + BPF_MSH, 14), - - /* Make sure it's to the right port... */ - BPF_STMT (BPF_LD + BPF_H + BPF_IND, 16), - BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, DHCP_CLIENT_PORT, 0, 1), - - /* If we passed all the tests, ask for the whole packet. */ - BPF_STMT (BPF_RET + BPF_K, ~0U), - - /* Otherwise, drop it. */ - BPF_STMT (BPF_RET + BPF_K, 0), -}; - -static struct bpf_insn arp_bpf_filter [] = { - /* Make sure this is an ARP packet... */ - BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 12), - BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_ARP, 0, 3), - - /* Make sure this is an ARP REPLY... */ - BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 20), - BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REPLY, 0, 1), - - /* If we passed all the tests, ask for the whole packet. */ - BPF_STMT (BPF_RET + BPF_K, ~0U), - - /* Otherwise, drop it. */ - BPF_STMT (BPF_RET + BPF_K, 0), -}; - -void setup_packet_filters (void) -{ -#ifdef __linux__ - /* We need to massage the filters for Linux cooked packets */ - dhcp_bpf_filter[1].jf = 0; /* skip the IP packet type check */ - dhcp_bpf_filter[2].k -= ETH_HLEN; - dhcp_bpf_filter[4].k -= ETH_HLEN; - dhcp_bpf_filter[6].k -= ETH_HLEN; - dhcp_bpf_filter[7].k -= ETH_HLEN; - - arp_bpf_filter[1].jf = 0; /* skip the IP packet type check */ - arp_bpf_filter[2].k -= ETH_HLEN; -#endif -} - -static uint16_t checksum (unsigned char *addr, uint16_t len) -{ - uint32_t sum = 0; - union - { - unsigned char *addr; - uint16_t *i; - } p; - uint16_t nleft = len; - - p.addr = addr; - while (nleft > 1) { - sum += *p.i++; - nleft -= 2; - } - - if (nleft == 1) { - uint8_t a = 0; - memcpy (&a, p.i, 1); - sum += ntohs (a) << 8; - } - - sum = (sum >> 16) + (sum & 0xffff); - sum += (sum >> 16); - - return ~sum; -} - -void make_dhcp_packet(struct udp_dhcp_packet *packet, - const unsigned char *data, size_t length, - struct in_addr source, struct in_addr dest) -{ - struct ip *ip = &packet->ip; - struct udphdr *udp = &packet->udp; - - /* OK, this is important :) - * We copy the data to our packet and then create a small part of the - * ip structure and an invalid ip_len (basically udp length). - * We then fill the udp structure and put the checksum - * of the whole packet into the udp checksum. - * Finally we complete the ip structure and ip checksum. - * If we don't do the ordering like so then the udp checksum will be - * broken, so find another way of doing it! */ - - memcpy (&packet->dhcp, data, length); - - ip->ip_p = IPPROTO_UDP; - ip->ip_src.s_addr = source.s_addr; - if (dest.s_addr == 0) - ip->ip_dst.s_addr = INADDR_BROADCAST; - else - ip->ip_dst.s_addr = dest.s_addr; - - udp->uh_sport = htons (DHCP_CLIENT_PORT); - udp->uh_dport = htons (DHCP_SERVER_PORT); - udp->uh_ulen = htons (sizeof (*udp) + length); - ip->ip_len = udp->uh_ulen; - udp->uh_sum = checksum ((unsigned char *) packet, sizeof (*packet)); - - ip->ip_v = IPVERSION; - ip->ip_hl = 5; - ip->ip_id = 0; - ip->ip_tos = IPTOS_LOWDELAY; - ip->ip_len = htons (sizeof (*ip) + sizeof (*udp) + length); - ip->ip_id = 0; - ip->ip_off = htons (IP_DF); /* Don't fragment */ - ip->ip_ttl = IPDEFTTL; - - ip->ip_sum = checksum ((unsigned char *) ip, sizeof (*ip)); -} - -static int valid_dhcp_packet (unsigned char *data) -{ - union - { - unsigned char *data; - struct udp_dhcp_packet *packet; - } d; - uint16_t bytes; - uint16_t ipsum; - uint16_t iplen; - uint16_t udpsum; - struct in_addr source; - struct in_addr dest; - int retval = 0; - - d.data = data; - bytes = ntohs (d.packet->ip.ip_len); - ipsum = d.packet->ip.ip_sum; - iplen = d.packet->ip.ip_len; - udpsum = d.packet->udp.uh_sum; - - d.data = data; - d.packet->ip.ip_sum = 0; - if (ipsum != checksum ((unsigned char *) &d.packet->ip, - sizeof (d.packet->ip))) - { - logger (LOG_DEBUG, "bad IP header checksum, ignoring"); - retval = -1; - goto eexit; - } - - memcpy (&source, &d.packet->ip.ip_src, sizeof (d.packet->ip.ip_src)); - memcpy (&dest, &d.packet->ip.ip_dst, sizeof (d.packet->ip.ip_dst)); - memset (&d.packet->ip, 0, sizeof (d.packet->ip)); - d.packet->udp.uh_sum = 0; - - d.packet->ip.ip_p = IPPROTO_UDP; - memcpy (&d.packet->ip.ip_src, &source, sizeof (d.packet->ip.ip_src)); - memcpy (&d.packet->ip.ip_dst, &dest, sizeof (d.packet->ip.ip_dst)); - d.packet->ip.ip_len = d.packet->udp.uh_ulen; - if (udpsum && udpsum != checksum (d.data, bytes)) { - logger (LOG_ERR, "bad UDP checksum, ignoring"); - retval = -1; - } - -eexit: - d.packet->ip.ip_sum = ipsum; - d.packet->ip.ip_len = iplen; - d.packet->udp.uh_sum = udpsum; - - return retval; -} - -#if defined(BSD) || defined(__FreeBSD_kernel__) -int open_socket (interface_t *iface, int protocol) -{ - int n = 0; - int fd = -1; - char *device; - int flags; - struct ifreq ifr; - int buf = 0; - struct bpf_program pf; - - device = xmalloc (sizeof (char) * PATH_MAX); - do { - snprintf (device, PATH_MAX, "/dev/bpf%d", n++); - fd = open (device, O_RDWR); - } while (fd == -1 && errno == EBUSY); - free (device); - - if (fd == -1) { - logger (LOG_ERR, "unable to open a BPF device"); - return -1; - } - - close_on_exec (fd); - - memset (&ifr, 0, sizeof (ifr)); - strlcpy (ifr.ifr_name, iface->name, sizeof (ifr.ifr_name)); - if (ioctl (fd, BIOCSETIF, &ifr) == -1) { - logger (LOG_ERR, - "cannot attach interface `%s' to bpf device `%s': %s", - iface->name, device, strerror (errno)); - close (fd); - return -1; - } - - /* Get the required BPF buffer length from the kernel. */ - if (ioctl (fd, BIOCGBLEN, &buf) == -1) { - logger (LOG_ERR, "ioctl BIOCGBLEN: %s", strerror (errno)); - close (fd); - return -1; - } - iface->buffer_length = buf; - - flags = 1; - if (ioctl (fd, BIOCIMMEDIATE, &flags) == -1) { - logger (LOG_ERR, "ioctl BIOCIMMEDIATE: %s", strerror (errno)); - close (fd); - return -1; - } - - /* Install the DHCP filter */ - if (protocol == ETHERTYPE_ARP) { - pf.bf_insns = arp_bpf_filter; - pf.bf_len = sizeof (arp_bpf_filter) - / sizeof (arp_bpf_filter[0]); - } else { - pf.bf_insns = dhcp_bpf_filter; - pf.bf_len = sizeof (dhcp_bpf_filter) - / sizeof (dhcp_bpf_filter[0]); - } - if (ioctl (fd, BIOCSETF, &pf) == -1) { - logger (LOG_ERR, "ioctl BIOCSETF: %s", strerror (errno)); - close (fd); - return -1; - } - - if (iface->fd > -1) - close (iface->fd); - iface->fd = fd; - - return fd; -} - -ssize_t send_packet (const interface_t *iface, int type, - const unsigned char *data, size_t len) -{ - ssize_t retval = -1; - struct iovec iov[2]; - - if (iface->family == ARPHRD_ETHER) { - struct ether_header hw; - memset (&hw, 0, sizeof (hw)); - memset (&hw.ether_dhost, 0xff, ETHER_ADDR_LEN); - hw.ether_type = htons (type); - - iov[0].iov_base = &hw; - iov[0].iov_len = sizeof (hw); - } else { - logger (LOG_ERR, "unsupported interace type %d", iface->family); - return -1; - } - iov[1].iov_base = (unsigned char *) data; - iov[1].iov_len = len; - - if ((retval = writev(iface->fd, iov, 2)) == -1) - logger (LOG_ERR, "writev: %s", strerror (errno)); - - return retval; -} - -/* BPF requires that we read the entire buffer. - * So we pass the buffer in the API so we can loop on >1 dhcp packet. */ -ssize_t get_packet (const interface_t *iface, unsigned char *data, - unsigned char *buffer, - size_t *buffer_len, size_t *buffer_pos) -{ - union - { - unsigned char *buffer; - struct bpf_hdr *packet; - } bpf; - - bpf.buffer = buffer; - - if (*buffer_pos < 1) { - memset (bpf.buffer, 0, iface->buffer_length); - *buffer_len = read (iface->fd, bpf.buffer, iface->buffer_length); - *buffer_pos = 0; - if (*buffer_len < 1) { - struct timespec ts; - logger (LOG_ERR, "read: %s", strerror (errno)); - ts.tv_sec = 3; - ts.tv_nsec = 0; - nanosleep (&ts, NULL); - return (-1); - } - } else - bpf.buffer += *buffer_pos; - - while (bpf.packet) { - size_t len = 0; - union - { - unsigned char *buffer; - struct ether_header *hw; - } hdr; - unsigned char *payload; - bool have_data = false; - - /* Ensure that the entire packet is in our buffer */ - if (*buffer_pos + bpf.packet->bh_hdrlen + bpf.packet->bh_caplen - > (unsigned) *buffer_len) - break; - - hdr.buffer = bpf.buffer + bpf.packet->bh_hdrlen; - payload = hdr.buffer + sizeof (*hdr.hw); - - /* If it's an ARP reply, then just send it back */ - if (hdr.hw->ether_type == htons (ETHERTYPE_ARP)) { - len = bpf.packet->bh_caplen - - sizeof (*hdr.hw); - memcpy (data, payload, len); - have_data = true; - } else { - if (valid_dhcp_packet (payload) >= 0) { - union - { - unsigned char *buffer; - struct udp_dhcp_packet *packet; - } pay; - pay.buffer = payload; - len = ntohs (pay.packet->ip.ip_len) - - sizeof (pay.packet->ip) - - sizeof (pay.packet->udp); - memcpy (data, &pay.packet->dhcp, len); - have_data = true; - } - } - - /* Update the buffer_pos pointer */ - bpf.buffer += BPF_WORDALIGN (bpf.packet->bh_hdrlen + - bpf.packet->bh_caplen); - if ((unsigned) (bpf.buffer - buffer) < *buffer_len) - *buffer_pos = bpf.buffer - buffer; - else - *buffer_pos = 0; - - if (have_data) - return len; - - if (*buffer_pos == 0) - break; - } - - /* No valid packets left, so return */ - *buffer_pos = 0; - return -1; -} - -#elif __linux__ - -int open_socket (interface_t *iface, int protocol) -{ - int fd; - union sockunion { - struct sockaddr sa; - struct sockaddr_in sin; - struct sockaddr_ll sll; - struct sockaddr_storage ss; - } su; - struct sock_fprog pf; - struct ifreq ifr; - int n = 1; - - /* We need to bind to a port, otherwise Linux generate ICMP messages - * that cannot contect the port when we have an address. - * We don't actually use this fd at all, instead using our packet - * filter socket. */ - if (iface->listen_fd == -1 && protocol == ETHERTYPE_IP) { - if ((fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - } else { - memset (&su, 0, sizeof (su)); - su.sin.sin_family = AF_INET; - su.sin.sin_port = htons (DHCP_CLIENT_PORT); - if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, - &n, sizeof (n)) == -1) - logger (LOG_ERR, "SO_REUSEADDR: %s", - strerror (errno)); - if (setsockopt (fd, SOL_SOCKET, SO_RCVBUF, - &n, sizeof (n)) == -1) - logger (LOG_ERR, "SO_RCVBUF: %s", - strerror (errno)); - memset (&ifr, 0, sizeof (ifr)); - strncpy (ifr.ifr_name, iface->name, - sizeof (ifr.ifr_name)); - if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE, - &ifr, sizeof (ifr)) == -1) - logger (LOG_ERR, "SO_SOBINDTODEVICE: %s", - strerror (errno)); - if (bind (fd, &su.sa, sizeof (su)) == -1) { - logger (LOG_ERR, "bind: %s", strerror (errno)); - close (fd); - } else { - iface->listen_fd = fd; - close_on_exec (fd); - } - } - } - - if ((fd = socket (PF_PACKET, SOCK_DGRAM, htons (protocol))) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return (-1); - } - close_on_exec (fd); - - memset (&su, 0, sizeof (su)); - su.sll.sll_family = PF_PACKET; - su.sll.sll_protocol = htons (protocol); - if (! (su.sll.sll_ifindex = if_nametoindex (iface->name))) { - logger (LOG_ERR, - "if_nametoindex: no index for interface `%s'", - iface->name); - close (fd); - return (-1); - } - - /* Install the DHCP filter */ - memset (&pf, 0, sizeof (pf)); - if (protocol == ETHERTYPE_ARP) { - pf.filter = arp_bpf_filter; - pf.len = sizeof (arp_bpf_filter) / sizeof (arp_bpf_filter[0]); - } else { - pf.filter = dhcp_bpf_filter; - pf.len = sizeof (dhcp_bpf_filter) / sizeof (dhcp_bpf_filter[0]); - } - if (setsockopt (fd, SOL_SOCKET, SO_ATTACH_FILTER, - &pf, sizeof (pf)) != 0) - { - logger (LOG_ERR, "SO_ATTACH_FILTER: %s", strerror (errno)); - close (fd); - return (-1); - } - - if (bind (fd, &su.sa, sizeof (su)) == -1) { - logger (LOG_ERR, "bind: %s", strerror (errno)); - close (fd); - return (-1); - } - - if (iface->fd > -1) - close (iface->fd); - iface->fd = fd; - iface->socket_protocol = protocol; - iface->buffer_length = BUFFER_LENGTH; - - return (fd); -} - -ssize_t send_packet (const interface_t *iface, int type, - const unsigned char *data, size_t len) -{ - union sockunion { - struct sockaddr sa; - struct sockaddr_ll sll; - struct sockaddr_storage ss; - } su; - ssize_t retval; - - if (! iface) - return (-1); - - memset (&su, 0, sizeof (su)); - su.sll.sll_family = AF_PACKET; - su.sll.sll_protocol = htons (type); - - if (! (su.sll.sll_ifindex = if_nametoindex (iface->name))) { - logger (LOG_ERR, "if_nametoindex: no index for interface `%s'", - iface->name); - return (-1); - } - - su.sll.sll_hatype = htons (iface->family); - su.sll.sll_halen = iface->hwlen; - if (iface->family == ARPHRD_INFINIBAND) - memcpy (&su.sll.sll_addr, - &ipv4_bcast_addr, sizeof (ipv4_bcast_addr)); - else - memset (&su.sll.sll_addr, 0xff, iface->hwlen); - - if ((retval = sendto (iface->fd, data, len, 0, &su.sa, - sizeof (su))) == -1) - - logger (LOG_ERR, "sendto: %s", strerror (errno)); - return (retval); -} - -/* Linux has no need for the buffer as we can read as much as we want. - * We only have the buffer listed to keep the same API. */ -ssize_t get_packet (const interface_t *iface, unsigned char *data, - unsigned char *buffer, - size_t *buffer_len, size_t *buffer_pos) -{ - ssize_t bytes; - union - { - unsigned char *buffer; - struct udp_dhcp_packet *packet; - } pay; - - /* We don't use the given buffer, but we need to rewind the position */ - *buffer_pos = 0; - - memset (buffer, 0, iface->buffer_length); - bytes = read (iface->fd, buffer, iface->buffer_length); - - if (bytes == -1) { - struct timespec ts; - logger (LOG_ERR, "read: %s", strerror (errno)); - ts.tv_sec = 3; - ts.tv_nsec = 0; - nanosleep (&ts, NULL); - return (-1); - } - - *buffer_len = bytes; - /* If it's an ARP reply, then just send it back */ - if (iface->socket_protocol == ETHERTYPE_ARP) { - memcpy (data, buffer, bytes); - return (bytes); - } - - if ((unsigned) bytes < (sizeof (pay.packet->ip) + - sizeof (pay.packet->udp))) - { - logger (LOG_DEBUG, "message too short, ignoring"); - return (-1); - } - - pay.buffer = buffer; - if (bytes < ntohs (pay.packet->ip.ip_len)) { - logger (LOG_DEBUG, "truncated packet, ignoring"); - return (-1); - } - - if (valid_dhcp_packet (buffer) == -1) - return (-1); - - bytes = ntohs (pay.packet->ip.ip_len) - - (sizeof (pay.packet->ip) + sizeof (pay.packet->udp)); - memcpy (data, &pay.packet->dhcp, bytes); - return (bytes); -} - -#else - #error "Platform not supported!" - #error "We currently support BPF and Linux sockets." - #error "Other platforms may work using BPF. If yours does, please let me know" - #error "so I can add it to our list." -#endif diff --git a/workspace/networkDiscovery/dhcpcd/socket.h b/workspace/networkDiscovery/dhcpcd/socket.h deleted file mode 100644 index bdf26d0..0000000 --- a/workspace/networkDiscovery/dhcpcd/socket.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * dhcpcd - DHCP client daemon - * Copyright 2006-2008 Roy Marples - * All rights reserved - - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef SOCKET_H -#define SOCKET_H - -#include - -#include "dhcp.h" -#include "interface.h" - -void setup_packet_filters (void); -void make_dhcp_packet(struct udp_dhcp_packet *packet, - const unsigned char *data, size_t length, - struct in_addr source, struct in_addr dest); - -int open_socket (interface_t *iface, int protocol); -ssize_t send_packet (const interface_t *iface, int type, - const unsigned char *data, size_t len); -ssize_t get_packet (const interface_t *iface, unsigned char *data, - unsigned char *buffer, size_t *buffer_len, size_t *buffer_pos); -#endif diff --git a/workspace/networkDiscovery/main.cpp b/workspace/networkDiscovery/main.cpp deleted file mode 100644 index 228c6e4..0000000 --- a/workspace/networkDiscovery/main.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "networkdiscovery.h" - -#include -#include - -int main(int argc, char *argv[]) -{ - QApplication a(argc, argv); - NetworkDiscovery w; - w.show(); - return a.exec(); -} diff --git a/workspace/networkDiscovery/networkDiscovery.pro b/workspace/networkDiscovery/networkDiscovery.pro deleted file mode 100644 index 7095e70..0000000 --- a/workspace/networkDiscovery/networkDiscovery.pro +++ /dev/null @@ -1,42 +0,0 @@ -TEMPLATE = app -TARGET = networkDiscovery -QT += core \ - gui \ - network - -# LIBS += -L/usr/lib -# INCLUDEPATH += /usr/include/ -HEADERS += dhcpcd/arp.h \ - dhcpcd/client.h \ - dhcpcd/common.h \ - dhcpcd/config.h \ - dhcpcd/configure.h \ - dhcpcd/dhcp.h \ - dhcpcd/dhcpcd.h \ - dhcpcd/duid.h \ - dhcpcd/info.h \ - dhcpcd/interface.h \ - dhcpcd/ipv4ll.h \ - dhcpcd/logger.h \ - dhcpcd/signal.h \ - dhcpcd/socket.h \ - dhcpcd/version.h \ - networkdiscovery.h -SOURCES += dhcpcd/arp.c \ - dhcpcd/client.c \ - dhcpcd/common.c \ - dhcpcd/configure.c \ - dhcpcd/dhcp.c \ - dhcpcd/dhcpcd.c \ - dhcpcd/duid.c \ - dhcpcd/info.c \ - dhcpcd/interface.c \ - dhcpcd/ipv4ll.c \ - dhcpcd/logger.c \ - dhcpcd/signal.c \ - dhcpcd/socket.c \ - main.cpp \ - networkdiscovery.cpp -FORMS += networkdiscovery.ui \ - networkdiscovery.ui -RESOURCES += diff --git a/workspace/networkDiscovery/networkdiscovery.cpp b/workspace/networkDiscovery/networkdiscovery.cpp deleted file mode 100644 index e308de4..0000000 --- a/workspace/networkDiscovery/networkdiscovery.cpp +++ /dev/null @@ -1,125 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif -#include "dhcpcd/config.h" -#include "dhcpcd/client.h" -#include "dhcpcd/dhcpcd.h" -#include "dhcpcd/dhcp.h" -#include "dhcpcd/interface.h" -#include "dhcpcd/logger.h" -#include "dhcpcd/socket.h" -#include "dhcpcd/version.h" -#ifdef __cplusplus -} -#endif - -#ifdef __cplusplus - -#include -#include "networkdiscovery.h" -#include -#include - - -#endif - -NetworkDiscovery::NetworkDiscovery(QWidget *parent) - : QWidget(parent) -{ - ui.setupUi(this); - QList interfaces = getListOfNetworkInterfaces(); - - /** - * test if i get some dhcp values for the dummy0 interface - */ - doDHCP(interfaces); - doDhcp(interfaces.first()); -} - -NetworkDiscovery::~NetworkDiscovery() -{ - -} - -QList NetworkDiscovery::getListOfNetworkInterfaces() { - QList nIList = QNetworkInterface::allInterfaces(); - QList result; - foreach(QNetworkInterface nI, nIList) { - if (((!(nI.flags() & QNetworkInterface::CanBroadcast)|| - nI.flags() & QNetworkInterface::IsLoopBack) || - nI.flags() & QNetworkInterface::IsPointToPoint)) - { - continue; - } - qDebug() << nI.humanReadableName(); - result.append(nI); - } - return result; -} - -int NetworkDiscovery::doDHCP(QList interfaces) -{ - return 0; -} - - - -int NetworkDiscovery::doDhcp(QNetworkInterface interface) -{ - nd_main("eth0"); -} - -void NetworkDiscovery::listenToLogger() -{ - skt = new QLocalSocket(this); - - connect(skt, SIGNAL(readyRead()), this, SLOT(handleLoggedMessage())); - connect(skt, SIGNAL(error(QLocalSocket::LocalSocketError)), - this, SLOT(displayError(QLocalSocket::LocalSocketError))); - skt->connectToServer(COM_CH); -} - -void NetworkDiscovery::handleLoggedMessage() -{ - -} - -void NetworkDiscovery::displayError(QLocalSocket::LocalSocketError socketError) - { - switch (socketError) { - case QLocalSocket::ServerNotFoundError: - QMessageBox::information(this, tr("Fortune Client"), - tr("The host was not found. Please check the " - "host name and port settings.")); - break; - case QLocalSocket::ConnectionRefusedError: - QMessageBox::information(this, tr("Fortune Client"), - tr("The connection was refused by the peer. " - "Make sure the fortune server is running, " - "and check that the host name and port " - "settings are correct.")); - break; - case QLocalSocket::PeerClosedError: - break; - default: - QMessageBox::information(this, tr("Fortune Client"), - tr("The following error occurred: %1.") - .arg(skt->errorString())); - } - } - diff --git a/workspace/networkDiscovery/networkdiscovery.h b/workspace/networkDiscovery/networkdiscovery.h deleted file mode 100644 index fef0999..0000000 --- a/workspace/networkDiscovery/networkdiscovery.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef NETWORKDISCOVERY_H -#define NETWORKDISCOVERY_H - - -#include -#include -#include -#include "ui_networkdiscovery.h" - -#define COM_CH "/var/tmp/com.socket" - -class NetworkDiscovery : public QWidget -{ - Q_OBJECT - -public: - NetworkDiscovery(QWidget *parent = 0); - ~NetworkDiscovery(); - -public slots: - void handleLoggedMessage(); - void displayError(QLocalSocket::LocalSocketError socketError); - -private: - Ui::NetworkDiscoveryClass ui; - QList getListOfNetworkInterfaces(); - int doDHCP(QList interfaces); - int doDhcp(QNetworkInterface interface); - void listenToLogger(); - - QLocalSocket *skt; - -}; - -#endif // NETWORKDISCOVERY_H diff --git a/workspace/networkDiscovery/networkdiscovery.ui b/workspace/networkDiscovery/networkdiscovery.ui deleted file mode 100644 index 5ea46e3..0000000 --- a/workspace/networkDiscovery/networkdiscovery.ui +++ /dev/null @@ -1,19 +0,0 @@ - - NetworkDiscoveryClass - - - - 0 - 0 - 400 - 300 - - - - NetworkDiscovery - - - - - - -- cgit v1.2.3-55-g7522