[systemd-devel] [PATCH 28/28] dhcp: Use callback in test client
Patrik Flykt
patrik.flykt at linux.intel.com
Wed Nov 13 13:22:56 PST 2013
Print out the event received and possibly also IP related data.
---
src/dhcp/dhcp-example-client.c | 53 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+)
diff --git a/src/dhcp/dhcp-example-client.c b/src/dhcp/dhcp-example-client.c
index afb7102..3635bd8 100644
--- a/src/dhcp/dhcp-example-client.c
+++ b/src/dhcp/dhcp-example-client.c
@@ -24,6 +24,9 @@
#include <unistd.h>
#include <net/ethernet.h>
#include <errno.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
#include "client.h"
@@ -79,6 +82,52 @@ static int get_index(char *ifname)
return ifr.ifr_ifindex;
}
+static const char *print_event(int event)
+{
+ if (event < 0)
+ return strerror(-event);
+
+ switch (event) {
+ case DHCP_EVENT_STOP:
+ return "Stopped";
+ case DHCP_EVENT_NAK:
+ return "DHCP NAK";
+ case DHCP_EVENT_IP_ACQUIRE:
+ return "DHCP address acquired";
+ case DHCP_EVENT_IP_CHANGE:
+ return "DHCP address changed";
+ case DHCP_EVENT_EXPIRED:
+ return "DHCP lease expired";
+ default:
+ break;
+ }
+
+ return "unknown";
+}
+
+static void print_state(DHCPClient *client, int event, void *userdata)
+{
+ sd_event *e = userdata;
+ struct in_addr addr;
+
+ if (event < 0) {
+ printf("Error %d %s\n", event, print_event(event));
+ sd_event_unref(e);
+ return;
+ }
+
+ printf("Event %s\n", print_event(event));
+
+ if (dhcp_client_get_address(client, &addr) >= 0)
+ printf("Address %s\n", inet_ntoa(addr));
+
+ if (dhcp_client_get_netmask(client, &addr) >= 0)
+ printf("Netmask %s\n", inet_ntoa(addr));
+
+ if (dhcp_client_get_router(client, &addr) >= 0)
+ printf("Default router %s\n", inet_ntoa(addr));
+}
+
int main(int argc, char **argv)
{
DHCPClient *client;
@@ -109,9 +158,13 @@ int main(int argc, char **argv)
printf("Interface %s index %d\n", argv[1], index);
dhcp_client_set_index(client, index);
dhcp_client_set_mac(client, &mac);
+ dhcp_client_set_callback(client, print_state, &event);
dhcp_client_start(client);
sd_event_loop(event);
+ dhcp_client_free(client);
+ printf("Exit\n");
+
return 0;
}
--
1.7.10.4
More information about the systemd-devel
mailing list