summaryrefslogtreecommitdiffstats
path: root/net.h
diff options
context:
space:
mode:
authorRoy Marples <roy@marples.name>2008-04-11 16:14:55 +0000
committerRoy Marples <roy@marples.name>2008-04-11 16:14:55 +0000
commit1a60d14fa2f91876b9ec14d6e6c88d97dacf6c4c (patch)
tree9e992f64d969ddef2e7d3836afa0d4065f69706a /net.h
parent211da073490c5decd1deb46034385437a54ca977 (diff)
downloaddhcpcd-1a60d14fa2f91876b9ec14d6e6c88d97dacf6c4c.tar.xz
Normally I hate massive code drops, but heh.
The code has been drastically re-arranged. Instead of populating a custom structure while parsing dhcp messages, we now pluck what we need right out of the message itself. We have custom functions and a lookup table to make this really easy. This makes us more like dhclient and udhcpc, and will enable us to easily add (and remove!) more dhcp options without having to actually change the code (much). We now store the real dhcp message we got in /var/db/dhcpcd-$iface.lease, the mtime of the file being used as when we got the lease. This file is read in when re-using an old lease instead of parsing the .info file. The benefit of all of this means that we're actually ~15k smaller when compiled with the same features. This has been tested for quite some time, and I'm pretty sure most bugs with the 3.2 branch have been fixed whilst making this. Right now, we are 99% command line compatible with the 3.2 branch.
Diffstat (limited to 'net.h')
-rw-r--r--net.h180
1 files changed, 180 insertions, 0 deletions
diff --git a/net.h b/net.h
new file mode 100644
index 00000000..eaa6b75e
--- /dev/null
+++ b/net.h
@@ -0,0 +1,180 @@
+/*
+ * dhcpcd - DHCP client daemon
+ * Copyright 2006-2008 Roy Marples <roy@marples.name>
+ * 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 <sys/types.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+
+#include <net/if.h>
+#include <netinet/in.h>
+#include <netinet/if_ether.h>
+
+#include <limits.h>
+
+#include "config.h"
+
+#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
+
+/* There is an argument that this should be converted to an STAIL using
+ * queue(3). However, that isn't readily available on all libc's that
+ * dhcpcd works on. The only benefit of STAILQ over this is the ability to
+ * quickly loop backwards through the list - currently we reverse the list
+ * and then move through it forwards. This isn't that much of a big deal
+ * though as the norm is to just have one default route, and an IPV4LL route.
+ * You can (and do) get more routes in the DHCP message, but not enough to
+ * really warrant a change to STAIL queue for performance reasons. */
+struct rt {
+ struct in_addr dest;
+ struct in_addr net;
+ struct in_addr gate;
+ struct rt *next;
+};
+
+struct interface
+{
+ char name[IF_NAMESIZE];
+ sa_family_t family;
+ unsigned char hwaddr[HWADDR_LEN];
+ size_t hwlen;
+ int arpable;
+
+ int fd;
+ int udp_fd;
+ size_t buffer_length;
+
+#ifdef __linux__
+ int socket_protocol;
+#endif
+
+ char leasefile[PATH_MAX];
+ char infofile[PATH_MAX];
+
+ unsigned short initial_mtu;
+ unsigned short mtu;
+ struct in_addr addr;
+ struct in_addr net;
+ struct rt *routes;
+
+ time_t start_uptime;
+
+ unsigned char *clientid;
+ size_t clientid_len;
+};
+
+uint32_t get_netmask(uint32_t);
+char *hwaddr_ntoa(const unsigned char *, size_t);
+size_t hwaddr_aton(unsigned char *, const char *);
+
+struct interface *read_interface(const char *, int);
+int do_mtu(const char *, short int);
+#define get_mtu(iface) do_mtu(iface, 0)
+#define set_mtu(iface, mtu) do_mtu(iface, mtu)
+
+int inet_ntocidr(struct in_addr);
+int inet_cidrtoaddr(int, struct in_addr *);
+
+int do_interface(const char *, unsigned char *, size_t *,
+ struct in_addr *, struct in_addr *, int);
+int if_address(const char *, const struct in_addr *, const struct in_addr *,
+ const struct in_addr *, int);
+#define add_address(ifname, addr, net, brd) \
+ if_address(ifname, addr, net, brd, 1)
+#define del_address(ifname, addr, net) \
+ if_address(ifname, addr, net, NULL, -1)
+#define has_address(ifname, addr, net) \
+ do_interface(ifname, NULL, NULL, addr, net, 0)
+#define get_address(ifname, addr, net) \
+ do_interface(ifname, NULL, NULL, addr, net, 1)
+
+int if_route(const char *, const struct in_addr *, const struct in_addr *,
+ const struct in_addr *, int, int);
+#define add_route(ifname, dest, mask, gate, metric) \
+ if_route(ifname, dest, mask, gate, metric, 1)
+#define del_route(ifname, dest, mask, gate, metric) \
+ if_route(ifname, dest, mask, gate, metric, -1)
+void free_routes(struct rt *);
+
+int open_udp_socket(struct interface *);
+ssize_t make_udp_packet(uint8_t **, const uint8_t *, size_t,
+ struct in_addr, struct in_addr);
+ssize_t get_udp_data(const uint8_t **, const uint8_t *);
+int valid_udp_packet(uint8_t *);
+
+#ifdef __linux__
+void setup_packet_filters(void);
+#endif
+int open_socket(struct interface *, int);
+ssize_t send_packet(const struct interface *, int,
+ const uint8_t *, ssize_t);
+ssize_t get_packet(const struct interface *, uint8_t *,
+ uint8_t *, ssize_t *, ssize_t *);
+
+#ifdef ENABLE_ARP
+int arp_claim(struct interface *, struct in_addr);
+#endif
+#endif