⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 leases.c

📁 关于Linux下DHCP支持IPv6的版本实现
💻 C
📖 第 1 页 / 共 2 页
字号:
	{	    q = r;	    r = r -> next;	}	q -> next = p;    }    // Copy transaction id from the advertise message    p -> u_trans_id.trans_id = advertise -> u_trans_id.trans_id;        // Copy details from addr_details structure    p -> u_t1.t1 = available_addr -> t1;    p -> u_t2.t2 = available_addr -> t2;    p -> u_pref_lifetime.pref_lifetime = available_addr -> pref_life;    p -> u_valid_lifetime.valid_lifetime = available_addr -> valid_life;    p -> max_renew_count = available_addr -> max_renew_count;    p -> renew_count = 1;	// Leased for the first time    memcpy (&p -> assigned_ipv6_addr, &available_addr -> ipv6_addr, sizeof (struct in6_addr));        // Set start time for partial leases (5 minutes expiry)    time (&p -> start_time);        // Copy from IA option of advertise message    options_ptr = get_options_ptr (advertise, OPTION_IA);    ia = (struct IA *) options_ptr -> opt_data;    p -> u_iaid.iaid = ia -> u_iaid.iaid;        // Copy from IA address option of advertise message    options_ptr = get_options_ptr (advertise, OPTION_IAADDR);    iaaddr = (struct IA_ADDRESS *) options_ptr -> opt_data;    p -> prefix_length = iaaddr -> prefix_length;        // Copy the client's duid from the option    options_ptr = get_options_ptr (advertise, OPTION_CLIENTID);    d = (struct DUID *) options_ptr -> opt_data;    p -> client_duid = (struct DUID *) malloc (sizeof (struct DUID));    p -> client_duid -> u_duid_type.duid_type = d -> u_duid_type.duid_type;    p -> client_duid -> opt = 0;        switch (d -> u_duid_type.duid_type)    {	case 1 :	    p -> client_duid -> duid_type = (struct DUID1 *) malloc (sizeof (struct DUID1));	    d1 = (struct DUID1 *) d -> duid_type;	    copy_duid1 (d1, (struct DUID1 *) p -> client_duid -> duid_type);	    break;	case 2 :	    p -> client_duid -> duid_type = (struct DUID2 *) malloc (sizeof (struct DUID2));	    d2 = (struct DUID2 *) d -> duid_type;	    copy_duid2 (d2, (struct DUID2 *) p -> client_duid -> duid_type);	    break;	case 3 :	    p -> client_duid -> duid_type = (struct DUID3 *) malloc (sizeof (struct DUID3));	    d3 = (struct DUID3 *) d -> duid_type;	    copy_duid3 (d3, (struct DUID3 *) p -> client_duid -> duid_type);	    break;    }}int check_for_match (struct DHCP_MESSAGE * dhcp_msg_ptr, struct lease_details *lease_struct){    struct lease_details * p_lease = lease_struct;    struct OPTIONS * options_ptr;    struct IA * ia_ptr;    struct IA_ADDRESS * iaaddr_ptr;    while (p_lease)    {	options_ptr = get_options_ptr (dhcp_msg_ptr, OPTION_IAADDR);      iaaddr_ptr = (struct IA_ADDRESS *) options_ptr -> opt_data;	if (memcmp (iaaddr_ptr->addr, p_lease -> assigned_ipv6_addr.s6_addr, 16))	{	    p_lease = p_lease -> next;	    continue;	}	if (iaaddr_ptr -> prefix_length != p_lease -> prefix_length)	{	    p_lease = p_lease->next;	    continue;	}	if (iaaddr_ptr -> u_pref_lifetime.pref_lifetime != p_lease -> u_pref_lifetime.pref_lifetime)	{	    p_lease = p_lease -> next;	    continue;	}	if (iaaddr_ptr -> u_valid_lifetime.valid_lifetime != p_lease -> u_valid_lifetime.valid_lifetime)	{	    p_lease = p_lease -> next;	    continue;	}		options_ptr = get_options_ptr (dhcp_msg_ptr, OPTION_IA);	if (!options_ptr)	{	    p_lease = p_lease -> next;	    continue;	}	ia_ptr = (struct IA *) options_ptr -> opt_data;		if (ia_ptr -> u_iaid.iaid != p_lease -> u_iaid.iaid)	{	    p_lease = p_lease -> next;	    continue;	}	if (ia_ptr -> u_t1.t1 != p_lease -> u_t1.t1)	{	    p_lease = p_lease -> next;	    continue;	}	if (ia_ptr -> u_t2.t2 == p_lease -> u_t2.t2)	    return 1;		p_lease = p_lease -> next;    }    return 0;}int check_for_duid_match (struct DHCP_MESSAGE *dhcp_message, struct lease_details *lease_struct){   struct lease_details * p_lease;   struct OPTIONS * options_ptr;   struct DUID *d;   struct DUID1 *d1;   struct DUID2 *d2;   struct DUID3 *d3;   int client_duid_type;      p_lease = lease_struct;   options_ptr = get_options_ptr (dhcp_message, OPTION_CLIENTID);   d = (struct DUID *) options_ptr -> opt_data;      while (p_lease)   {	client_duid_type = p_lease -> client_duid -> u_duid_type.duid_type;		switch (client_duid_type)	{	   case 1 :		d1 = (struct DUID1 *) d -> duid_type;		if (client_duid_type == 1)		   if (check_duid1 (d1, (struct DUID1 *) p_lease -> client_duid -> duid_type))			return 1;		break;	   case 2 :		d2 = (struct DUID2 *) d -> duid_type;		if (client_duid_type == 2)		   if (check_duid2 (d2, (struct DUID2 *) p_lease -> client_duid -> duid_type))			return 1;		break;			   case 3 :		d3 = (struct DUID3 *) d -> duid_type;		if (client_duid_type == 3)		   if (check_duid3 (d3, (struct DUID3 *) p_lease -> client_duid -> duid_type))			return 1;		break;	}		p_lease = p_lease -> next;   }}int move_node_from_partial_to_lease (struct DHCP_MESSAGE *request_message){    extern struct lease_details *partial_lease, *lease;    struct OPTIONS *opt_ptr;    struct DUID *d;    struct DUID1 *d1;    struct DUID2 *d2;    struct DUID3 *d3;    struct IA *ia;    struct IA_ADDRESS *iaaddr;    struct lease_details *p = partial_lease, *q, *r;        while (p)    {	opt_ptr = get_options_ptr (request_message, OPTION_CLIENTID);	d = (struct DUID *) opt_ptr -> opt_data;		switch (d -> u_duid_type.duid_type)	{	    case 1 :		if (p -> client_duid -> u_duid_type.duid_type != 1)		{		    p = p -> next;		    continue;		}				d1 = (struct DUID1 *) d -> duid_type;		if (!check_duid1 (d1, (struct DUID1 *) p -> client_duid -> duid_type))		{		    p = p -> next;		    continue;		}		break;			    case 2 :		if (p -> client_duid -> u_duid_type.duid_type != 2)		{		    p = p -> next;		    continue;		}				d2 = (struct DUID2 *) d -> duid_type;		if (!check_duid2 (d2, (struct DUID2 *) p -> client_duid -> duid_type))		{		    p = p -> next;		    continue;		}		break;	    case 3 :		if (p -> client_duid -> u_duid_type.duid_type != 3)		{		    p = p -> next;		    continue;		}				d3 = (struct DUID3 *) d -> duid_type;		if (!check_duid3 (d3, (struct DUID3 *) p -> client_duid -> duid_type))		{		    p = p -> next;		    continue;		}		break;	}			opt_ptr = get_options_ptr (request_message, OPTION_IA);	ia = (struct IA *) opt_ptr -> opt_data;		if (p -> u_iaid.iaid == ia -> u_iaid.iaid)	    if (p -> u_t1.t1 == ia -> u_t1.t1)		if (p -> u_t2.t2 == ia -> u_t2.t2)		{		    opt_ptr = get_options_ptr (request_message, OPTION_IAADDR);		    iaaddr = (struct IA_ADDRESS *) opt_ptr -> opt_data;		    		    if (p -> u_pref_lifetime.pref_lifetime == iaaddr -> u_pref_lifetime.pref_lifetime)			if (p -> u_valid_lifetime.valid_lifetime == iaaddr -> u_valid_lifetime.valid_lifetime)			    if (p -> prefix_length == iaaddr -> prefix_length)				if (!memcmp (&p -> assigned_ipv6_addr.s6_addr, iaaddr -> addr, 16))				{				    if (p != partial_lease)				    {					r = partial_lease;					while (r != p)					{					    q = r;					    r = r -> next;					}				    	q -> next = p -> next;				    }				    else					partial_lease = partial_lease -> next;									    if (lease)				    {					r = lease;					while (r)					{					    q = r;					    r = r -> next;					}					q -> next = p;				    }				    else					lease = p;				    				    p -> next = 0;				    // Copy trans id from request message				    p -> u_trans_id.trans_id = request_message -> u_trans_id.trans_id;				    p -> renew_count = 1;				    // Set start time				    time (&p -> start_time);				    return 1;				}		}		p = p -> next;    }        return 0;}int move_node_from_lease_to_partial (struct DHCP_MESSAGE *message){    extern struct lease_details *partial_lease, *lease;    struct OPTIONS *opt_ptr;    struct DUID *d;    struct DUID1 *d1;    struct DUID2 *d2;    struct DUID3 *d3;    struct IA *ia;    struct IA_ADDRESS *iaaddr;    struct lease_details *p = lease, *q, *r;        while (p)    {	opt_ptr = get_options_ptr (message, OPTION_CLIENTID);	d = (struct DUID *) opt_ptr -> opt_data;		switch (d -> u_duid_type.duid_type)	{	    case 1 :		if (p -> client_duid -> u_duid_type.duid_type != 1)		{		    p = p -> next;		    continue;		}				d1 = (struct DUID1 *) d -> duid_type;		if (!check_duid1 (d1, (struct DUID1 *) p -> client_duid -> duid_type))		{		    p = p -> next;		    continue;		}		break;			    case 2 :		if (p -> client_duid -> u_duid_type.duid_type != 2)		{		    p = p -> next;		    continue;		}				d2 = (struct DUID2 *) d -> duid_type;		if (!check_duid2 (d2, (struct DUID2 *) p -> client_duid -> duid_type))		{		    p = p -> next;		    continue;		}		break;	    case 3 :		if (p -> client_duid -> u_duid_type.duid_type != 3)		{		    p = p -> next;		    continue;		}				d3 = (struct DUID3 *) d -> duid_type;		if (!check_duid3 (d3, (struct DUID3 *) p -> client_duid -> duid_type))		{		    p = p -> next;		    continue;		}		break;	}			opt_ptr = get_options_ptr (message, OPTION_IA);	ia = (struct IA *) opt_ptr -> opt_data;		if (p -> u_iaid.iaid == ia -> u_iaid.iaid)	    if (p -> u_t1.t1 == ia -> u_t1.t1)		if (p -> u_t2.t2 == ia -> u_t2.t2)		{		    opt_ptr = get_options_ptr (message, OPTION_IAADDR);		    iaaddr = (struct IA_ADDRESS *) opt_ptr -> opt_data;		    		    if (p -> u_pref_lifetime.pref_lifetime == iaaddr -> u_pref_lifetime.pref_lifetime)			if (p -> u_valid_lifetime.valid_lifetime == iaaddr -> u_valid_lifetime.valid_lifetime)			    if (p -> prefix_length == iaaddr -> prefix_length)				if (!memcmp (&p -> assigned_ipv6_addr.s6_addr, iaaddr -> addr, 16))				{				    if (p != lease)				    {					r = lease;					while (r != p)					{					    q = r;					    r = r -> next;					}				    	q -> next = p -> next;				    }				    else					lease = lease -> next;									    if (partial_lease)				    {					r = partial_lease;					while (r)					{					    q = r;					    r = r -> next;					}					q -> next = p;				    }				    else					partial_lease = p;				    				    p -> next = 0;				    // Copy trans id from message				    p -> u_trans_id.trans_id = message -> u_trans_id.trans_id;				    				    // Set start time				    time (&p -> start_time);				    return 1;				}		}		p = p -> next;    }        return 0;}void remove_expired_nodes_from_partial_lease (void){   extern struct lease_details *partial_lease;   struct lease_details *p = partial_lease;   time_t curr_time = time (&curr_time);      while (p)   {	if ((p -> start_time + FIVE_MINUTES) < curr_time)	    p = delete_lease_node (p, partial_lease); 	else	    p = p -> next;   }}void del_node_from_lease (struct DHCP_MESSAGE *message, struct lease_details *lease_struct){    struct OPTIONS *opt_ptr = get_options_ptr (message, OPTION_IAADDR);    struct lease_details *p = lease_struct;    struct in6_addr ipv6_addr;        memcpy (&ipv6_addr.s6_addr, ((struct IA_ADDRESS *) opt_ptr -> opt_data) -> addr, 16);    while (p)    {	if (!memcmp (&ipv6_addr, &p -> assigned_ipv6_addr, sizeof (struct in6_addr)))	   p = delete_lease_node (p, lease_struct);	else	    p = p -> next;    }}int check_renewal_policy (struct DHCP_MESSAGE *message, struct lease_details *lease_struct){    struct lease_details *p = lease_struct;    struct OPTIONS *opt_ptr = get_options_ptr (message, OPTION_IAADDR);    struct IA_ADDRESS *iaaddr_ptr = (struct IA_ADDRESS *) opt_ptr -> opt_data;            while (p)    {	if (!memcmp (p -> assigned_ipv6_addr.s6_addr, iaaddr_ptr -> addr, 16))	{	    if (p -> renew_count == p -> max_renew_count)		return 1;	    else	    {		p -> renew_count++;		time (&p -> start_time);		return 0;	    }	}		p = p -> next;    }        return 1;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -