[systemd-devel] [PATCH 15/28] dhcp: Add example DHCP client test program

Patrik Flykt patrik.flykt at linux.intel.com
Wed Nov 13 13:22:43 PST 2013


The test program runs the DHCP protocol using libsystemd-dhcp.
---
 src/dhcp/dhcp-example-client.c |  112 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)
 create mode 100644 src/dhcp/dhcp-example-client.c

diff --git a/src/dhcp/dhcp-example-client.c b/src/dhcp/dhcp-example-client.c
new file mode 100644
index 0000000..b7f1a5f
--- /dev/null
+++ b/src/dhcp/dhcp-example-client.c
@@ -0,0 +1,112 @@
+/***
+  This file is part of systemd.
+
+  Copyright (C) 2013 Intel Corporation. All rights reserved.
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <net/if.h>
+#include <string.h>
+#include <unistd.h>
+#include <net/ethernet.h>
+#include <errno.h>
+
+#include "client.h"
+
+static int get_mac(int index, struct ether_addr *mac)
+{
+        struct ifreq ifr;
+        int s, err;
+
+        if (index < 0 || !mac)
+                return -EINVAL;
+
+        s = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+        if (s < 0)
+                return -EIO;
+
+        memset(&ifr, 0, sizeof(ifr));
+        ifr.ifr_ifindex = index;
+
+        if (ioctl(s, SIOCGIFNAME, &ifr) < 0) {
+                err = -errno;
+                close(s);
+                return err;
+        }
+
+        if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
+                err = -errno;
+                close(s);
+                return err;
+        }
+
+        memcpy(&mac->ether_addr_octet[0], &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
+
+        return 0;
+}
+
+static int get_index(char *ifname)
+{
+        struct ifreq ifr;
+        int s;
+
+        s = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+        if (s < 0)
+                return -EIO;
+
+        memset(&ifr, 0, sizeof(ifr));
+        strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
+
+        if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
+                close(s);
+                return -EIO;
+        }
+
+        return ifr.ifr_ifindex;
+}
+
+int main(int argc, char **argv)
+{
+        DHCPClient *client;
+        int err, index;
+        struct ether_addr mac;
+
+        if (argc != 2) {
+                printf("Usage: %s <interface>\n", argv[0]);
+                return 1;
+        }
+
+        index = get_index(argv[1]);
+        if (index < 0)
+                return 2;
+
+        err = get_mac(index, &mac);
+        if (err < 0)
+                return 2;
+
+        client = dhcp_client_new();
+        if (!client)
+                return 3;
+
+        printf("Interface %s index %d\n", argv[1], index);
+        dhcp_client_set_index(client, index);
+        dhcp_client_set_mac(client, &mac);
+
+        dhcp_client_start(client);
+
+        return 0;
+}
-- 
1.7.10.4



More information about the systemd-devel mailing list