更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
dhcps.h 文件参考

浏览源代码.

函数

err_t dhcps_start (struct netif *netif, const char *start_ip, u16_t ip_num)
 
void dhcps_stop (struct netif *netif)
 

函数说明

◆ dhcps_start()

err_t dhcps_start ( struct netif *  netif,
const char *  start_ip,
u16_t  ip_num 
)

在文件 dhcps.c857 行定义.

858{
859 struct dhcps *dhcps = NULL;
860 ip4_addr_t address_in_hton;
861 int err;
862
863 LWIP_ERROR("netif != NULL", (netif != NULL), return ERR_ARG);
864 dhcps = netif_get_dhcps(netif);
865
866 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
867 ("dhcps_start(netif=%p) %s\n", (void *)netif, netif_get_name(netif)));
868
869 if (netif->mtu < DHCP_MAX_MSG_LEN_MIN_REQUIRED) {
870 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE,
871 ("MTU =%"U16_F",DHCP Msg Len Required =%"U32_F"\n", netif->mtu, DHCP_MAX_MSG_LEN_MIN_REQUIRED));
872 return ERR_MEM;
873 }
874
875 if (dhcps != NULL) {
876 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcps_start(): DHCP Server is already started\n"));
877 return ERR_MEM;
878 }
879
880 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcps_start(): starting new DHCP Server\n"));
881 dhcps = (struct dhcps *)mem_malloc(sizeof(struct dhcps));
882 if (dhcps == NULL) {
883 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcps_start(): could not allocate dhcp\n"));
884 return ERR_MEM;
885 }
886
887 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcps_start(): allocated dhcp"));
888
889 (void)memset_s(dhcps, sizeof(struct dhcps), 0, sizeof(struct dhcps));
890
891 dhcps->pcb = udp_new();
892 if (dhcps->pcb == NULL) {
893 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): could not allocate pcb\n"));
894 mem_free((void *)dhcps);
895 return ERR_MEM;
896 }
897
898#if LWIP_SO_BINDTODEVICE
899 /* bind dhcp udp_pcb to specific netif, this could make dhcp server start on multiple netif */
900 dhcps->pcb->ifindex = netif->ifindex;
901#endif
902 if ((start_ip == NULL) || (ip_num == 0)) {
903 /* use default ip lease configuration. */
904 dhcps->start_addr.addr = ntohl(ip_2_ip4(&netif->ip_addr)->addr & ip_2_ip4(&netif->netmask)->addr) + 1;
905 dhcps->end_addr.addr = ntohl(ip_2_ip4(&netif->ip_addr)->addr | (~ip_2_ip4(&netif->netmask)->addr)) - 1;
906 dhcps->lease_num = (u8_t)(dhcps->end_addr.addr - dhcps->start_addr.addr + 1);
907 if (dhcps->lease_num > LWIP_DHCPS_MAX_LEASE) {
908 dhcps->lease_num = LWIP_DHCPS_MAX_LEASE;
909 dhcps->end_addr.addr = dhcps->start_addr.addr + LWIP_DHCPS_MAX_LEASE - 1;
910 }
911 } else {
912 dhcps->start_addr.addr = ntohl(ipaddr_addr(start_ip));
913 dhcps->end_addr.addr = (u32_t)(dhcps->start_addr.addr +
914 (u32_t)(LWIP_MIN(ip_num - 1, LWIP_DHCPS_MAX_LEASE - 1)));
915
916 ip4_addr_set_hton(&address_in_hton, &dhcps->start_addr);
917
918 if (!ip4_addr_netcmp((&address_in_hton), ip_2_ip4(&netif->ip_addr), ip_2_ip4(&netif->netmask)) ||
919 ip4_addr_isbroadcast((&address_in_hton), netif)) {
920 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): %s in not a valid ip lease\n", start_ip));
921 udp_remove(dhcps->pcb);
922 mem_free((void *)dhcps);
923 return ERR_ARG;
924 }
925
926 ip4_addr_set_hton(&address_in_hton, &dhcps->end_addr);
927
928 if (!ip4_addr_netcmp((&address_in_hton), ip_2_ip4(&netif->ip_addr), ip_2_ip4(&netif->netmask)) ||
929 ip4_addr_isbroadcast((&address_in_hton), netif)) {
930 dhcps->end_addr.addr = ntohl(ip_2_ip4(&netif->ip_addr)->addr | (~ip_2_ip4(&netif->netmask)->addr)) - 1;
931 }
932 dhcps->lease_num = (u8_t)(dhcps->end_addr.addr - dhcps->start_addr.addr + 1);
933 }
934
935 dhcps->netif = netif;
936 dhcps->pcb->so_options |= SOF_BROADCAST;
937 err = udp_bind(dhcps->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
938 if (err != ERR_OK) {
939 udp_remove(dhcps->pcb);
940 mem_free((void *)dhcps);
941 return ERR_MEM;
942 }
943
944 err = udp_connect(dhcps->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT);
945 if (err != ERR_OK) {
946 udp_remove(dhcps->pcb);
947 mem_free((void *)dhcps);
948 return ERR_MEM;
949 }
950 udp_recv(dhcps->pcb, dhcps_recv, netif);
951 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcps_start(): starting DHCPS Successfully\n"));
952#ifdef LWIP_DEV_DEBUG
953 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE,
954 ("dhcps_start(): DHCPS Conf:: netif addr = %"U32_F" dhcps start addr%"U32_F" dhcp end addr%"U32_F"\n",
955 ip_2_ip4(&netif->ip_addr)->addr, dhcps->start_addr.addr, dhcps->end_addr.addr));
956#endif
957 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE,
958 ("dhcps_start(): DHCPS Lease Conf:: Lease Time = %"U32_F" Offer Time = %"U32_F"\n",
959 LWIP_DHCPS_LEASE_TIME, LWIP_DHCPS_OFFER_TIME));
960 netif_set_dhcps(netif, dhcps);
961 return ERR_OK;
962}
LWIP_STATIC void dhcps_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *ip_addr, u16_t port)
Definition: dhcps.c:758
Definition: dhcps.c:83
u8_t lease_num
Definition: dhcps.c:88
ip4_addr_t end_addr
Definition: dhcps.c:91
ip4_addr_t start_addr
Definition: dhcps.c:90
struct udp_pcb * pcb
Definition: dhcps.c:85
struct netif * netif
Definition: dhcps.c:89
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void
函数调用图:
这是这个函数的调用关系图:

◆ dhcps_stop()

void dhcps_stop ( struct netif *  netif)

在文件 dhcps.c964 行定义.

965{
966 LWIP_ERROR("dhcps_stop: netif != NULL", (netif != NULL), return);
967 struct dhcps *dhcps = netif_get_dhcps(netif);
968 if (dhcps != NULL) {
969 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcps_stop(): Stopping DHCP Server\n"));
970 if (dhcps->pcb != NULL) {
971 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcps_stop(): Removing UDP PCB\n"));
972 udp_remove(dhcps->pcb);
973 dhcps->pcb = NULL;
974 }
975
976 mem_free(dhcps);
977 netif_set_dhcps(netif, NULL);
978 }
979}
这是这个函数的调用关系图: