This is the simplified "usbnet" dhcpd that will assign an IP address to 
the remote.

diff -ruNP usbnet_dhcpd.orig/dhcp.h usbnet_dhcpd/dhcp.h
--- usbnet_dhcpd.orig/dhcp.h    1969-12-31 19:00:00.000000000 -0500
+++ usbnet_dhcpd/dhcp.h 2009-07-24 18:04:52.000000000 -0400
@@ -0,0 +1,202 @@
+/* dhcp.h
+
+   Protocol structures... */
+
+/*
+ * Copyright (c) 2004-2009 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1995-2003 by Internet Software Consortium
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ *   Internet Systems Consortium, Inc.
+ *   950 Charter Street
+ *   Redwood City, CA 94063
+ *   <i...@isc.org>
+ *   https://www.isc.org/
+ *
+ * This software has been written for Internet Systems Consortium
+ * by Ted Lemon in cooperation with Vixie Enterprises.  To learn more
+ * about Internet Systems Consortium, see ``https://www.isc.org''.
+ * To learn more about Vixie Enterprises, see ``http://www.vix.com''.
+ */
+
+#ifndef DHCP_H
+#define DHCP_H
+
+#define DHCP_UDP_OVERHEAD      (20 + /* IP header */                   \
+                               8)   /* UDP header */
+#define DHCP_SNAME_LEN         64
+#define DHCP_FILE_LEN          128
+#define DHCP_FIXED_NON_UDP     236
+#define DHCP_FIXED_LEN         (DHCP_FIXED_NON_UDP + DHCP_UDP_OVERHEAD)
+                                               /* Everything but options. */
+#define BOOTP_MIN_LEN          300
+
+#define DHCP_MTU_MAX           1500
+#define DHCP_MTU_MIN            576
+
+#define DHCP_MAX_OPTION_LEN    (DHCP_MTU_MAX - DHCP_FIXED_LEN)
+#define DHCP_MIN_OPTION_LEN     (DHCP_MTU_MIN - DHCP_FIXED_LEN)
+
+struct dhcp_packet {
+ u_int8_t  op;         /* 0: Message opcode/type */
+       u_int8_t  htype;        /* 1: Hardware addr type (net/if_types.h) */
+       u_int8_t  hlen;         /* 2: Hardware addr length */
+       u_int8_t  hops;         /* 3: Number of relay agent hops from client */
+       u_int32_t xid;          /* 4: Transaction ID */
+       u_int16_t secs;         /* 8: Seconds since client started looking */
+       u_int16_t flags;        /* 10: Flag bits */
+       struct in_addr ciaddr;  /* 12: Client IP address (if already in use) */
+       struct in_addr yiaddr;  /* 16: Client IP address */
+       struct in_addr siaddr;  /* 18: IP address of next server to talk to */
+       struct in_addr giaddr;  /* 20: DHCP relay agent IP address */
+       unsigned char chaddr [16];      /* 24: Client hardware address */
+       char sname [DHCP_SNAME_LEN];    /* 40: Server name */
+       char file [DHCP_FILE_LEN];      /* 104: Boot filename */
+       unsigned char options [DHCP_MAX_OPTION_LEN];
+                               /* 212: Optional parameters
+                         (actual length dependent on MTU). */
+};
+
+/* BOOTP (rfc951) message types */
+#define        BOOTREQUEST     1
+#define BOOTREPLY      2
+
+/* Possible values for flags field... */
+#define BOOTP_BROADCAST 32768L
+
+/* Possible values for hardware type (htype) field... */
+#define HTYPE_ETHER    1               /* Ethernet 10Mbps              */
+#define HTYPE_IEEE802  6               /* IEEE 802.2 Token Ring...     */
+#define HTYPE_FDDI     8               /* FDDI...                      */
+
+/* Magic cookie validating dhcp options field (and bootp vendor
+   extensions field). */
+#define DHCP_OPTIONS_COOKIE    "\143\202\123\143"
+
+/* DHCP Option codes: */
+
+#define DHO_PAD                                        0
+#define DHO_SUBNET_MASK                                1
+#define DHO_TIME_OFFSET                                2
+#define DHO_ROUTERS                            3
+#define DHO_TIME_SERVERS                       4
+#define DHO_NAME_SERVERS                       5
+#define DHO_DOMAIN_NAME_SERVERS                        6
+#define DHO_LOG_SERVERS                                7
+#define DHO_COOKIE_SERVERS                     8
+#define DHO_LPR_SERVERS                                9
+#define DHO_IMPRESS_SERVERS                    10
+#define DHO_RESOURCE_LOCATION_SERVERS          11
+#define DHO_HOST_NAME                          12
+#define DHO_BOOT_SIZE                          13
+#define DHO_MERIT_DUMP                         14
+#define DHO_DOMAIN_NAME                                15
+#define DHO_SWAP_SERVER                                16
+#define DHO_ROOT_PATH                          17
+#define DHO_EXTENSIONS_PATH                    18
+#define DHO_IP_FORWARDING                      19
+#define DHO_NON_LOCAL_SOURCE_ROUTING           20
+#define DHO_POLICY_FILTER                      21
+#define DHO_MAX_DGRAM_REASSEMBLY               22
+#define DHO_DEFAULT_IP_TTL                     23
+#define DHO_PATH_MTU_AGING_TIMEOUT             24
+#define DHO_PATH_MTU_PLATEAU_TABLE             25
+#define DHO_INTERFACE_MTU                      26
+#define DHO_ALL_SUBNETS_LOCAL                  27
+#define DHO_BROADCAST_ADDRESS                  28
+#define DHO_PERFORM_MASK_DISCOVERY             29
+#define DHO_MASK_SUPPLIER                      30
+#define DHO_ROUTER_DISCOVERY                   31
+#define DHO_ROUTER_SOLICITATION_ADDRESS                32
+#define DHO_STATIC_ROUTES                      33
+#define DHO_TRAILER_ENCAPSULATION              34
+#define DHO_ARP_CACHE_TIMEOUT                  35
+#define DHO_IEEE802_3_ENCAPSULATION            36
+#define DHO_DEFAULT_TCP_TTL                    37
+#define DHO_TCP_KEEPALIVE_INTERVAL             38
+#define DHO_TCP_KEEPALIVE_GARBAGE              39
+#define DHO_NIS_DOMAIN                         40
+#define DHO_NIS_SERVERS                                41
+#define DHO_NTP_SERVERS                                42
+#define DHO_VENDOR_ENCAPSULATED_OPTIONS                43
+#define DHO_NETBIOS_NAME_SERVERS               44
+#define DHO_NETBIOS_DD_SERVER                  45
+#define DHO_NETBIOS_NODE_TYPE                  46
+#define DHO_NETBIOS_SCOPE                      47
+#define DHO_FONT_SERVERS                       48
+#define DHO_X_DISPLAY_MANAGER                  49
+#define DHO_DHCP_REQUESTED_ADDRESS             50
+#define DHO_DHCP_LEASE_TIME                    51
+#define DHO_DHCP_OPTION_OVERLOAD               52
+#define DHO_DHCP_MESSAGE_TYPE                  53
+#define DHO_DHCP_SERVER_IDENTIFIER             54
+#define DHO_DHCP_PARAMETER_REQUEST_LIST                55
+#define DHO_DHCP_MESSAGE                       56
+#define DHO_DHCP_MAX_MESSAGE_SIZE              57
+#define DHO_DHCP_RENEWAL_TIME                  58
+#define DHO_DHCP_REBINDING_TIME                        59
+#define DHO_VENDOR_CLASS_IDENTIFIER            60
+#define DHO_DHCP_CLIENT_IDENTIFIER             61
+#define DHO_NWIP_DOMAIN_NAME                   62
+#define DHO_NWIP_SUBOPTIONS                    63
+#define DHO_USER_CLASS                         77
+#define DHO_FQDN                               81
+#define DHO_DHCP_AGENT_OPTIONS                 82
+#define DHO_AUTHENTICATE                       90  /* RFC3118, was 210 */
+#define DHO_CLIENT_LAST_TRANSACTION_TIME       91
+#define DHO_ASSOCIATED_IP                      92
+#define DHO_SUBNET_SELECTION                   118 /* RFC3011! */
+#define DHO_DOMAIN_SEARCH                      119 /* RFC3397 */
+#define DHO_VIVCO_SUBOPTIONS                   124
+#define DHO_VIVSO_SUBOPTIONS                   125
+
+#define DHO_END                                        255
+
+/* DHCP message types. */
+#define DHCPDISCOVER           1
+#define DHCPOFFER              2
+#define DHCPREQUEST            3
+#define DHCPDECLINE            4
+#define DHCPACK                        5
+#define DHCPNAK                        6
+#define DHCPRELEASE            7
+#define DHCPINFORM             8
+#define DHCPLEASEQUERY         10
+#define DHCPLEASEUNASSIGNED    11
+#define DHCPLEASEUNKNOWN       12
+#define DHCPLEASEACTIVE                13
+
+
+/* Relay Agent Information option subtypes: */
+#define RAI_CIRCUIT_ID 1
+#define RAI_REMOTE_ID  2
+#define RAI_AGENT_ID   3
+#define RAI_LINK_SELECT        5
+
+/* FQDN suboptions: */
+#define FQDN_NO_CLIENT_UPDATE          1
+#define FQDN_SERVER_UPDATE             2
+#define FQDN_ENCODED                   3
+#define FQDN_RCODE1                    4
+#define FQDN_RCODE2                    5
+#define FQDN_HOSTNAME                  6
+#define FQDN_DOMAINNAME                        7
+#define FQDN_FQDN                      8
+#define FQDN_SUBOPTION_COUNT           8
+
+/* Enterprise Suboptions: */
+#define VENDOR_ISC_SUBOPTIONS          2495
+
+#endif /* DHCP_H */
+
diff -ruNP usbnet_dhcpd.orig/usbnet_dhcpd.c usbnet_dhcpd/usbnet_dhcpd.c
--- usbnet_dhcpd.orig/usbnet_dhcpd.c    1969-12-31 19:00:00.000000000 -0500
+++ usbnet_dhcpd/usbnet_dhcpd.c 2012-03-09 22:05:57.908865237 -0500
@@ -0,0 +1,192 @@
+/*
+ * vi: formatoptions+=tc textwidth=80 tabstop=8 shiftwidth=8 noexpandtab:
+ *
+ * $Id: libconcord.h,v 1.22.2.14 2011/02/20 00:27:19 jaymzh Exp $
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * (C) Copyright Scott Talbert 2012
+ */
+
+#include <stdio.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <stdlib.h>
+#include <string.h>
+#include "dhcp.h"
+
+const u_short dhcpd_port = 67;
+const u_short dhcpc_port = 68;
+const char * const local_ip_address = "169.254.1.1";
+const char * const remote_ip_address = "169.254.1.2";
+const char * const broadcast_ip_address = "255.255.255.255";
+const char * const subnet_mask = "255.255.0.0";
+
+void die(char* s)
+{
+  perror(s);
+  exit(1);
+}
+
+u_int8_t get_response_message_type(char* options)
+{
+  // Find opcode.
+  int iindex = 0;
+  u_int8_t input_message_type;
+  int char_left = DHCP_MAX_OPTION_LEN;
+  if (strncmp(options, DHCP_OPTIONS_COOKIE, 4) == 0)
+    {
+      char_left = char_left - 4;
+      iindex = iindex + 4;
+      while (char_left >= 3) // At least enough for option 53 - DHCP message 
type
+       {
+         if (options[iindex] == DHO_DHCP_MESSAGE_TYPE)
+           {
+             input_message_type = options[iindex+2];
+             printf("received DHCP packet with opcode %d!\n", 
input_message_type);
+             break;
+           }
+         char_left = char_left - 2 - options[iindex+1];
+         iindex = iindex + 2 + options[iindex+1];
+       }
+    }
+
+  if (input_message_type == DHCPDISCOVER)
+    return DHCPOFFER;
+  else if (input_message_type == DHCPREQUEST)
+    return DHCPACK;
+  else
+      printf("Unknown DHCP message type received: %d\n", input_message_type);
+
+  return 0;
+}
+
+int get_response(struct dhcp_packet* in_packet, struct dhcp_packet* out_packet)
+{
+  out_packet->op = BOOTREPLY;
+  out_packet->htype = in_packet->htype;
+  out_packet->hlen = in_packet->hlen;
+  out_packet->hops = in_packet->hops;
+  out_packet->xid = in_packet->xid;
+  out_packet->secs = in_packet->secs;
+  out_packet->flags = in_packet->flags;
+  memcpy(&out_packet->ciaddr, &in_packet->ciaddr, sizeof(out_packet->ciaddr));
+  out_packet->yiaddr.s_addr = inet_addr(remote_ip_address);
+  out_packet->siaddr.s_addr = inet_addr(local_ip_address);
+  memcpy(&out_packet->giaddr, &in_packet->giaddr, sizeof(out_packet->giaddr));
+  memcpy(out_packet->chaddr, in_packet->chaddr, sizeof(out_packet->chaddr));
+  memcpy(out_packet->sname, in_packet->sname, sizeof(out_packet->sname));
+  memcpy(out_packet->file, in_packet->file, sizeof(out_packet->file));
+
+  int oindex = 0;
+  memcpy(out_packet->options, DHCP_OPTIONS_COOKIE, 4);
+  oindex = oindex + 4;
+
+  out_packet->options[oindex] = DHO_DHCP_MESSAGE_TYPE;
+  oindex++;
+  out_packet->options[oindex] = 1;
+  oindex++;
+  out_packet->options[oindex] = get_response_message_type(in_packet->options);
+  oindex++;
+
+  out_packet->options[oindex] = DHO_DHCP_SERVER_IDENTIFIER;
+  oindex++;
+  out_packet->options[oindex] = 4;
+  oindex++;
+  uint32_t ip_addr = inet_addr(local_ip_address);
+  memcpy(&out_packet->options[oindex], &ip_addr, sizeof(ip_addr));
+  oindex = oindex + 4;
+
+  out_packet->options[oindex] = DHO_DHCP_LEASE_TIME;
+  oindex++;
+  out_packet->options[oindex] = 4;
+  oindex++;
+  uint32_t lease_time = htonl(43200);
+  memcpy(&out_packet->options[oindex], &lease_time, sizeof(lease_time));
+  oindex = oindex + 4;
+
+  out_packet->options[oindex] = DHO_SUBNET_MASK;
+  oindex++;
+  out_packet->options[oindex] = 4;
+  oindex++;
+  uint32_t netmask = inet_addr(subnet_mask);
+  memcpy(&out_packet->options[oindex], &netmask, sizeof(netmask));
+  oindex = oindex + 4;
+
+  out_packet->options[oindex] = 0xff;
+  oindex++;
+
+  int i;
+  for (i=0; i<38; i++)
+    {
+      out_packet->options[oindex] = 0x00;
+      oindex++;
+    }
+
+  return DHCP_FIXED_NON_UDP + oindex;
+}
+
+void main(int argc, char** argv)
+{
+  int sock;
+  struct sockaddr_in si_me;
+  struct sockaddr_in si_dst;
+  struct dhcp_packet recv_packet;
+  struct dhcp_packet send_packet;
+  int rcv_len;
+  int send_len;
+
+  if (argv[1] == NULL)
+    die("no device specified");
+  char* device = argv[1];
+
+  if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
+    die("socket");
+
+  if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, device,
+                sizeof(device)) == -1)
+    die ("setsockopt SO_BINDTODEVICE");
+
+  int optval = 1;
+  if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &optval,
+                sizeof(optval)) == -1)
+    die ("setsockopt SO_BROADCAST");
+ 
+  memset((char*) &si_me, 0, sizeof(si_me));
+  si_me.sin_family = AF_INET;
+  si_me.sin_port = htons(dhcpd_port);
+  si_me.sin_addr.s_addr = inet_addr(broadcast_ip_address);
+  if (bind(sock, (struct sockaddr*)&si_me, sizeof(si_me)) == -1)
+    die("bind");
+
+  memset((char*) &si_dst, 0, sizeof(si_me));
+  si_dst.sin_family = AF_INET;
+  si_dst.sin_port = htons(dhcpc_port);
+  si_dst.sin_addr.s_addr = inet_addr(broadcast_ip_address);
+
+  while(1)
+    {
+      rcv_len = recvfrom(sock, &recv_packet, sizeof(recv_packet), 0, NULL, 
NULL);
+      if (rcv_len > 0)
+       {
+         int pkt_len = get_response(&recv_packet, &send_packet);
+         send_len = sendto(sock, &send_packet, pkt_len, 0, 
+                           (struct sockaddr*) &si_dst, sizeof(si_dst));
+         if (send_len < 0)
+           perror("send failed");
+       }
+    }
+}

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
concordance-devel mailing list
concordance-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/concordance-devel

Reply via email to