vcpe/srcs/service/dhcpd/dhcp_option.c

222 lines
11 KiB
C

//
// Created by xajhuang on 2023/3/21.
//
#include <string.h>
#include "common.h"
#include "dhcp_options.h"
#include "user_errno.h"
static const DHCP_OPTION_CFG g_opCfg[] = {
// region DHCP optinos configure table
{"SubnetMask", OPT_NETMASK, 3, TRUE },
{"TimeOffset", OPT_TIMEOFFSET, 4, TRUE },
{"Router", OPT_ROUTER, 3, TRUE },
{"TimeServer", OPT_TIMESERVER, 3, TRUE },
{"NameServer", OPT_NAMESERVER, 3, TRUE },
{"DomainServer", OPT_DNS, 3, TRUE },
{"LogServer", OPT_LOGSERVER, 3, TRUE },
{"QuotesServer", OPT_COOKIESERVER, 3, TRUE },
{"LPRServer", OPT_LPRSERVER, 3, TRUE },
{"ImpressServer", OPT_IMPRESSSERVER, 3, TRUE },
{"RLPServer", OPT_RESLOCSERVER, 3, TRUE },
{"Hostname", OPT_HOSTNAME, 1, TRUE },
{"BootFileSize", OPT_BOOTFILESIZE, 5, TRUE },
{"MeritDumpFile", OPT_MERITDUMP, 1, TRUE },
{"DomainName", OPT_DOMAINNAME, 1, TRUE },
{"SwapServer", OPT_SWAPSERVER, 3, TRUE },
{"RootPath", OPT_ROOTPATH, 1, TRUE },
{"ExtensionFile", OPT_EXTSPATH, 1, TRUE },
{"ForwardOn/Off", OPT_IPFORWARD, 7, TRUE },
{"SrcRteOn/Off", OPT_NONLOCALSR, 7, TRUE },
{"PolicyFilter", OPT_POLICYFILTER, 8, TRUE },
{"MaxDGAssembly", OPT_MAXREASSEMBLE, 5, TRUE },
{"DefaultIPTTL", OPT_IPTTL, 6, TRUE },
{"MTUTimeout", OPT_PATHMTUAGING, 4, TRUE },
{"MTUPlateau", OPT_PATHMTUPLATEAU, 2, TRUE },
{"MTUInterface", OPT_INTERFACEMTU, 5, TRUE },
{"MTUSubnet", OPT_SUBNETSLOCAL, 7, TRUE },
{"BroadcastAddress", OPT_BCASTADDRESS, 3, TRUE },
{"MaskDiscovery", OPT_MASKDISCOVERY, 7, TRUE },
{"MaskSupplier", OPT_MASKSUPPLIER, 7, TRUE },
{"RouterDiscovery", OPT_ROUTERDISCOVERY, 7, TRUE },
{"RouterRequest", OPT_ROUTERSOLIC, 3, TRUE },
{"StaticRoute", OPT_STATICROUTE, 8, TRUE },
{"Trailers", OPT_TRAILERENCAPS, 7, TRUE },
{"ARPTimeout", OPT_ARPTIMEOUT, 4, TRUE },
{"Ethernet", OPT_ETHERNETENCAPS, 7, TRUE },
{"DefaultTCPTTL", OPT_TCPTTL, 6, TRUE },
{"KeepaliveTime", OPT_TCPKEEPALIVEINT, 4, TRUE },
{"KeepaliveData", OPT_TCPKEEPALIVEGRBG, 7, TRUE },
{"NISDomain", OPT_NISDOMAIN, 1, TRUE },
{"NISServers", OPT_NISSERVERS, 3, TRUE },
{"NTPServers", OPT_NTPSERVERS, 3, TRUE },
{"VendorSpecificInf", OPT_VENDORSPECIFIC, 2, FALSE},
{"NETBIOSNameSrv", OPT_NETBIOSNAMESERV, 3, TRUE },
{"NETBIOSDistSrv", OPT_NETBIOSDGDIST, 3, TRUE },
{"NETBIOSNodeType", OPT_NETBIOSNODETYPE, 6, TRUE },
{"NETBIOSScope", OPT_NETBIOSSCOPE, 1, TRUE },
{"XWindowFont", OPT_X11FONTS, 1, TRUE },
{"XWindowManager", OPT_X11DISPLAYMNGR, 3, TRUE },
{"AddressRequest", OPT_REQUESTEDIPADDR, 3, TRUE },
{"AddressTime", OPT_IPADDRLEASE, 4, TRUE },
{"OverLoad", OPT_OVERLOAD, 7, FALSE},
{"DHCPMsgType", OPT_MESSAGETYPE, 6, TRUE },
{"DHCPServerId", OPT_SERVERID, 3, TRUE },
{"ParameterList", OPT_PARAMREQLIST, 2, TRUE },
{"DHCPMessage", OPT_MESSAGE, 1, FALSE},
{"DHCPMaxMsgSize", OPT_MAXDHCPMSGSIZE, 5, FALSE},
{"RenewalTime", OPT_RENEWALTIME, 4, TRUE },
{"RebindingTime", OPT_REBINDINGTIME, 4, TRUE },
{"ClassId", OPT_VENDORCLASSID, 1, TRUE },
{"ClientId", OPT_CLIENTID, 2, FALSE},
{"NetWareIPDomain", OPT_NETWARE_IPDOMAIN, 1, TRUE },
{"NetWareIPOption", OPT_NETWARE_IPOPTION, 2, TRUE },
{"NISDomainName", OPT_NISPLUSDOMAIN, 1, TRUE },
{"NISServerAddr", OPT_NISPLUSSERVERS, 3, TRUE },
{"TFTPServerName", OPT_TFTPSERVER, 1, TRUE },
{"BootFileOption", OPT_BOOTFILE, 1, TRUE },
{"HomeAgentAddrs", OPT_MOBILEIPHOME, 3, TRUE },
{"SMTPServer", OPT_SMTPSERVER, 3, TRUE },
{"POP3Server", OPT_POP3SERVER, 3, TRUE },
{"NNTPServer", OPT_NNTPSERVER, 3, TRUE },
{"WWWServer", OPT_WWWSERVER, 3, TRUE },
{"FingerServer", OPT_FINGERSERVER, 3, TRUE },
{"IRCServer", OPT_IRCSERVER, 3, TRUE },
{"StreetTalkServer", OPT_STSERVER, 3, TRUE },
{"STDAServer", OPT_STDASERVER, 3, TRUE },
{"UserClass", OPT_USERCLASS, 1, FALSE},
{"DirectoryAgent", OPT_SLPDIRAGENT, 1, TRUE },
{"ServiceScope", OPT_SLPDIRSCOPE, 1, TRUE },
{"RapidCommit", OPT_RAPIDCOMMIT, 2, FALSE},
{"ClientFQDN", OPT_CLIENTFQDN, 2, FALSE},
{"RelayAgentInformation", OPT_RELAYAGENTINFO, 2, FALSE},
{"iSNS", OPT_I_SNS, 1, TRUE },
{"NDSServers", OPT_NDSSERVERS, 3, TRUE },
{"NDSTreeName", OPT_NDSTREENAME, 1, TRUE },
{"NDSContext", OPT_NDSCONTEXT, 1, TRUE },
{"LDAP", OPT_LDAP, 1, TRUE },
{"PCode", OPT_P_CODE, 1, TRUE },
{"TCode", OPT_T_CODE, 1, TRUE },
{"NetInfoAddress", OPT_NETINFOADDRESS, 3, TRUE },
{"NetInfoTag", OPT_NETINFOTAG, 1, TRUE },
{"URL", OPT_URL, 1, TRUE },
{"AutoConfig", OPT_AUTO_CONFIG, 7, TRUE },
{"NameServiceSearch", OPT_NAMESERVICESEARCH, 2, TRUE },
{"SubnetSelectionOption", OPT_SUBNETSELECTION, 3, TRUE },
{"DomainSearch", OPT_DOMAINSEARCH, 1, TRUE },
{"SIPServersDHCPOption", OPT_SIPSERVERSDHCP, 1, TRUE },
{"ClasslessStaticRoute", OPT_CLASSLESSSTATICROUTE, 1, TRUE },
{"CCC", OPT_CCC, 1, TRUE },
{"TFTPServerIPaddress", OPT_TFPTSERVERIPADDRESS, 3, TRUE },
{"CallServerIPaddress", OPT_CALLSERVERIPADDRESS, 3, TRUE },
{"DiscriminationString", OPT_DISCRIMINATIONSTRING, 1, TRUE },
{"RemoteStatisticsServerIPAddress", OPT_REMOTESTATISTICSSERVER, 3, TRUE },
{"HTTPProxyPhone", OPT_HTTPPROXYFORPHONE_SPEC, 3, TRUE },
{"OPTION_CAPWAP_AC_V4", 138, 1, TRUE },
{"OPTIONIPv4_AddressMoS", 139, 1, TRUE },
{"OPTIONIPv4_FQDNMoS", 140, 1, TRUE },
{"SIPUAServiceDomains", 141, 1, TRUE },
{"OPTIONIPv4_AddressANDSF", 142, 1, TRUE },
{"IPTelephone", 176, 1, TRUE },
{"ConfigurationFile", 209, 1, TRUE },
{"PathPrefix", 210, 1, TRUE },
{"RebootTime", 211, 4, TRUE },
{"OPTION_6RD", 212, 1, TRUE },
{"OPTION_V4_ACCESS_DOMAIN", 213, 1, TRUE },
{"BootFileName", OPT_BP_FILE, 1, TRUE },
{"NextServer", OPT_NEXTSERVER, 3, TRUE },
// endregion
};
static DHCP_OPTION_CFG g_optRuntime[256];
void dhcp_option_cfg_init() {
int i;
memset(g_optRuntime, 0, sizeof(g_optRuntime));
for (i = 0; i < ARRAY_SIZE(g_opCfg); i++) {
if (g_opCfg[i].enable) {
memcpy(&g_optRuntime[g_opCfg[i].opTag], &g_opCfg[i], sizeof(DHCP_OPTION_CFG));
}
}
}
int dhcp_add_u8_option(U8 *pBegin, int opt, U8 value) {
*pBegin = opt;
pBegin++;
*pBegin = 1;
pBegin++;
*pBegin = value;
return 3;
}
int dhcp_add_u32_option(U8 *pBegin, int opt, U32 value) {
*pBegin = opt;
pBegin++;
*pBegin = 4;
pBegin++;
memcpy(pBegin, &value, 4);
return 4 + 2;
}
int dhcp_add_buf_option(U8 *pBegin, int opt, U8 *buf, U8 nSize) {
*pBegin = opt;
pBegin++;
*pBegin = nSize;
pBegin++;
memcpy(pBegin, buf, nSize);
pBegin += nSize;
return nSize + 2;
}
int dhcp_add_string_option(U8 *pBegin, int opt, const char *buf) {
dhcp_add_buf_option(pBegin, opt, (U8 *)buf, strlen(buf) + 1);
return (int)(strlen(buf) + 1 + 2);
}
int dhcp_get_option(int opt, U8 *pOptData, U32 nSize, PDHCP_OPT pVal) {
U8 *p = pOptData;
if (!pVal || !pOptData) {
return -ERR_INPUT_PARAMS;
}
while (p < (pOptData + nSize)) {
U8 id = *p;
U8 len = *(p + 1);
if (id == 0xFF) {
return -ERR_ITEM_UNEXISTS;
}
if (g_optRuntime[id].enable && id == opt) {
pVal->len = len;
pVal->pValue = p + 2;
return ERR_SUCCESS;
}
p = p + len + 2;
}
return -ERR_ITEM_UNEXISTS;
}
const char* dhcp_get_opName(int opt) {
for(int i=0; i<ARRAY_SIZE(g_opCfg); i++) {
if(opt == g_opCfg[i].opTag)
return g_opCfg[i].opName;
}
return "Unknown";
}
int dhcp_get_opType(int opt) {
for(int i=0; i<ARRAY_SIZE(g_opCfg); i++) {
if(opt == g_opCfg[i].opTag)
return g_opCfg[i].opType;
}
return -1;
}