b.liu | d440f9f | 2025-04-18 10:44:31 +0800 | [diff] [blame] | 1 | /* |
| 2 | * MBTK DHCP Header. |
| 3 | * |
| 4 | * Author : lb |
| 5 | * Date : 2021/8/20 13:38:03 |
| 6 | * |
| 7 | */ |
| 8 | #ifndef _MBTK_DHCP_H |
| 9 | #define _MBTK_DHCP_H |
| 10 | #include <netinet/in.h> |
| 11 | |
| 12 | #include "mbtk_type.h" |
| 13 | |
| 14 | #define PORT_BOOTP_SERVER 67 |
| 15 | #define PORT_BOOTP_CLIENT 68 |
| 16 | #define OP_BOOTREQUEST 1 |
| 17 | #define OP_BOOTREPLY 2 |
| 18 | #define FLAGS_BROADCAST 0x8000 |
| 19 | #define HTYPE_ETHER 1 |
| 20 | #define DHCP_MSG_FIXED_SIZE 236 |
| 21 | |
| 22 | /* first four bytes of options are a cookie to indicate that |
| 23 | ** the payload are DHCP options as opposed to some other BOOTP |
| 24 | ** extension. |
| 25 | */ |
| 26 | #define OPT_COOKIE1 0x63 |
| 27 | #define OPT_COOKIE2 0x82 |
| 28 | #define OPT_COOKIE3 0x53 |
| 29 | #define OPT_COOKIE4 0x63 |
| 30 | |
| 31 | /* BOOTP/DHCP options - see RFC 2132 */ |
| 32 | #define OPT_PAD 0 |
| 33 | |
| 34 | #define OPT_SUBNET_MASK 1 /* 4 <ipaddr> */ |
| 35 | #define OPT_TIME_OFFSET 2 /* 4 <seconds> */ |
| 36 | #define OPT_GATEWAY 3 /* 4*n <ipaddr> * n */ |
| 37 | #define OPT_DNS 6 /* 4*n <ipaddr> * n */ |
| 38 | #define OPT_DOMAIN_NAME 15 /* n <domainnamestring> */ |
| 39 | #define OPT_BROADCAST_ADDR 28 /* 4 <ipaddr> */ |
| 40 | |
| 41 | #define OPT_REQUESTED_IP 50 /* 4 <ipaddr> */ |
| 42 | #define OPT_LEASE_TIME 51 /* 4 <seconds> */ |
| 43 | #define OPT_MESSAGE_TYPE 53 /* 1 <msgtype> */ |
| 44 | #define OPT_SERVER_ID 54 /* 4 <ipaddr> */ |
| 45 | #define OPT_PARAMETER_LIST 55 /* n <optcode> * n */ |
| 46 | #define OPT_MESSAGE 56 /* n <errorstring> */ |
| 47 | #define OPT_CLASS_ID 60 /* n <opaque> */ |
| 48 | #define OPT_CLIENT_ID 61 /* n <opaque> */ |
| 49 | #define OPT_END 255 |
| 50 | |
| 51 | /* DHCP message types */ |
| 52 | #define DHCPDISCOVER 1 |
| 53 | #define DHCPOFFER 2 |
| 54 | #define DHCPREQUEST 3 |
| 55 | #define DHCPDECLINE 4 |
| 56 | #define DHCPACK 5 |
| 57 | #define DHCPNAK 6 |
| 58 | #define DHCPRELEASE 7 |
| 59 | #define DHCPINFORM 8 |
| 60 | |
| 61 | typedef unsigned long long msecs_t; |
| 62 | |
| 63 | typedef struct { |
| 64 | uint8_t op; /* BOOTREQUEST / BOOTREPLY */ |
| 65 | uint8_t htype; /* hw addr type */ |
| 66 | uint8_t hlen; /* hw addr len */ |
| 67 | uint8_t hops; /* client set to 0 */ |
| 68 | |
| 69 | uint32_t xid; /* transaction id */ |
| 70 | |
| 71 | uint16_t secs; /* seconds since start of acq */ |
| 72 | uint16_t flags; |
| 73 | |
| 74 | uint32_t ciaddr; /* client IP addr */ |
| 75 | uint32_t yiaddr; /* your (client) IP addr */ |
| 76 | uint32_t siaddr; /* ip addr of next server */ |
| 77 | /* (DHCPOFFER and DHCPACK) */ |
| 78 | uint32_t giaddr; /* relay agent IP addr */ |
| 79 | |
| 80 | uint8_t chaddr[16]; /* client hw addr */ |
| 81 | char sname[64]; /* asciiz server hostname */ |
| 82 | char file[128]; /* asciiz boot file name */ |
| 83 | |
| 84 | uint8_t options[1024]; /* optional parameters */ |
| 85 | } dhcp_msg; |
| 86 | |
| 87 | typedef struct { |
| 88 | uint32_t type; |
| 89 | |
| 90 | uint32_t ipaddr; |
| 91 | uint32_t gateway; |
| 92 | uint32_t prefixLength; |
| 93 | |
| 94 | uint32_t dns1; |
| 95 | uint32_t dns2; |
| 96 | |
| 97 | uint32_t serveraddr; |
| 98 | uint32_t lease; |
| 99 | } dhcp_info; |
| 100 | |
| 101 | int mbtk_do_dhcp(const char *name); |
| 102 | |
| 103 | #endif /* _MBTK_DHCP_H */ |
| 104 | |