📄 client.c
字号:
struct DHCP_MESSAGE * rebind (char * interface_name, struct DHCP_MESSAGE * server_reply){ struct DHCP_MESSAGE ** server_rebind_reply; struct DHCP_MESSAGE * dhcp_message_ptr; char buff[MIN_MESSAGE_SIZE]; int n, msg_count; dhcp_message_ptr = create_rebind_message (server_reply, interface_name); write_to_log ("Rebind message constructed.", 1); print_linked_list_contents (dhcp_message_ptr); n = store_in_buffer (dhcp_message_ptr, buff); write_to_log ("Sending Rebind message ...", 1); sendto (sfd, buff, n, 0, (struct sockaddr *) &sa, sl); write_to_log ("Waiting for REPLY (for REBIND) message ...", 1); server_rebind_reply = wait_until_response (sfd, cfd, REB_IRT, REB_MRT, REB_MRC, REBIND, REPLY, buff, n, &msg_count); write_to_log ("\n\n\n------------------------------------------------------\n\n\n", 0); if (server_rebind_reply) { write_to_log ("Reply (for Rebind) message received from server.", 1); return * server_rebind_reply; } else { write_to_log ("No Reply (for Rebind) message received from server.", 1); return 0; }} struct DHCP_MESSAGE * release_lease (char * interface_name, struct DHCP_MESSAGE * server_reply){ struct DHCP_MESSAGE ** server_release_reply; struct DHCP_MESSAGE * dhcp_message_ptr; char buff[MIN_MESSAGE_SIZE]; int n, msg_count; // send Release message dhcp_message_ptr = create_release_message (server_reply, interface_name); write_to_log ("RELEASE message has been constructed", 1); n = store_in_buffer (dhcp_message_ptr, buff); sendto (sfd, buff, n, 0, (struct sockaddr *) &sa, sl); write_to_log ("RELEASE message has been sent.", 1); write_to_log ("Waiting for REPLY (for RELEASE) message from server.", 1); server_release_reply = wait_until_response (sfd, cfd, REL_IRT, REL_MRT, REL_MRC, RELEASE, REPLY, buff, n, &msg_count); if (!server_release_reply) write_to_log ("No REPLY (for RELEASE) message from server. Abandoning attempts ...", 1); else { if (check_message (* server_release_reply, REPLY, RELEASE)) { print_linked_list_contents (* server_release_reply); write_to_log ("REPLY (for RELEASE) message received from server.", 1); } } write_to_log ("\n\n\n------------------------------------------------------\n\n\n", 0); if (server_release_reply) return * server_release_reply; else return 0;}void store_current_server_duid (struct OPTIONS * options_ptr){ struct DUID * duid_ptr; struct DUID1 * duid1_ptr; struct DUID2 * duid2_ptr; struct DUID3 * duid3_ptr; if (!options_ptr) return; if (server_duid_ptr) { duid_ptr = server_duid_ptr; switch (duid_ptr -> u_duid_type.duid_type) { case 1 : duid1_ptr = (struct DUID1 *) duid_ptr -> duid_type; free (duid1_ptr); break; case 2 : duid2_ptr = (struct DUID2 *) duid_ptr -> duid_type; free (duid2_ptr); break; case 3 : duid3_ptr = (struct DUID3 *) duid_ptr -> duid_type; free (duid3_ptr); break; } free (duid_ptr); } duid_ptr = (struct DUID *) options_ptr -> opt_data; server_duid_ptr = (struct DUID *) malloc (sizeof (struct DUID)); server_duid_ptr -> u_duid_type.duid_type = duid_ptr -> u_duid_type.duid_type; server_duid_ptr -> opt = 0; switch (duid_ptr -> u_duid_type.duid_type) { case 1 : duid1_ptr = (struct DUID1 *) malloc (sizeof (struct DUID1)); server_duid_ptr -> duid_type = duid1_ptr; copy_duid1 ((struct DUID1 *) duid_ptr -> duid_type, duid1_ptr); break; case 2 : duid2_ptr = (struct DUID2 *) malloc (sizeof (struct DUID2)); server_duid_ptr -> duid_type = duid2_ptr; copy_duid2 ((struct DUID2 *) duid_ptr -> duid_type, duid2_ptr); break; case 3 : duid3_ptr = (struct DUID3 *) malloc (sizeof (struct DUID3)); server_duid_ptr -> duid_type = duid3_ptr; copy_duid3 ((struct DUID3 *) duid_ptr -> duid_type, duid3_ptr); break; }}// Main Functionint main(int argc, char ** argv){ struct DHCP_MESSAGE * server_decline_reply, * server_renew_reply, * server_rebind_reply, *server_release_reply; struct DHCP_MESSAGE ** server_advertise; int adv_msg_count = 0, allow_for_renewal; struct timeval timeout; u_int32_t renewal_timer, rebind_timer, valid_lifetime_timer; char * interface_name; int wait_for_key; sl = sizeof (sa); if (argc == 1) return 1; else if (argc == 2) interface_name = argv[1]; intialize_event_logging (); if (!intialize_client (interface_name)) { printf ("Failed to initialize client.Exiting ...\n"); exit (0); } fflush (stdin); while (1) { printf ("\nEntering solicitize function ...\n"); wait_for_key = getch(); server_advertise = solicitize (interface_name, &adv_msg_count); if (!server_advertise) { printf ("No server found. Bye Bye \n"); write_to_log ("No server found. Bye Bye", 1); exit (0); } printf ("\nEntering requesting function ...\n"); wait_for_key = getch(); reply_for_request = requesting (interface_name, server_advertise, &adv_msg_count); g_state = CHECKING; if (check_address_in_use (get_options_ptr (reply_for_request, OPTION_IAADDR))) { printf ("\nEntering decline_server function ..."); wait_for_key = getch(); server_decline_reply = decline_server (interface_name, reply_for_request); } else { g_state = BOUND; write_to_log ("Setting the IPv6 address ...", 1); printf ("\nSetting IPv6 address ...\n"); wait_for_key = getch(); set_ipv6_address (get_options_ptr (reply_for_request, OPTION_IAADDR), interface_name); store_current_server_duid (get_options_ptr (reply_for_request, OPTION_SERVERID)); while (1) { write_to_log ("Getting the timers and starting the timers...", 1); get_timers (reply_for_request, &renewal_timer, &rebind_timer, &valid_lifetime_timer); bind_and_count (interface_name, reply_for_request, &allow_for_renewal); if (allow_for_renewal) { g_state = RENEWING; write_to_log ("Moving to Renewal state ...", 1); printf ("\nEntering renew_lease function ...\n"); wait_for_key = getch(); server_renew_reply = renew_lease (interface_name, reply_for_request); if (!server_renew_reply) { write_to_log ("No server reply (REPLY) messages ...", 1); if (valid_lifetime_timer >= rebind_timer) { g_state = REBINDING; write_to_log ("Moving to Rebinding state ...", 1); printf ("\nEntering rebind lease function ...\n"); wait_for_key = getch(); server_rebind_reply = rebind (interface_name, reply_for_request); if (!server_rebind_reply) { // Create and send a release g_state = END; write_to_log ("Moving into End state ...", 1); server_release_reply = release_lease (interface_name, reply_for_request); // DELETE THE PREVIOUS IPV6 ADDRESS write_to_log ("Unsetting the IPv6 address ...", 1); unset_ipv6_address (get_options_ptr (reply_for_request, OPTION_IAADDR), interface_name); break; } else if (check_ia_status (server_rebind_reply)) { g_state = REQUESTING; write_to_log ("Moving to Requesting state ...", 1); reply_for_request = requesting (interface_name, server_advertise, &adv_msg_count); } else { g_state = BOUND; write_to_log ("Moving back to Bound state ...", 1); reply_for_request = server_rebind_reply; } } else { // Create and send a release g_state = END; write_to_log ("Moving into End state ...", 1); server_release_reply = release_lease (interface_name, reply_for_request); // DELETE THE PREVIOUS IPV6 ADDRESS write_to_log ("Unsetting the IPv6 address ...", 1); unset_ipv6_address (get_options_ptr (reply_for_request, OPTION_IAADDR), interface_name); break; } } else { if (check_ia_status (server_renew_reply)) { g_state = REQUESTING; write_to_log ("Moving to Requesting state ...", 1); reply_for_request = requesting (interface_name, server_advertise, &adv_msg_count); } else { g_state = BOUND; write_to_log ("Moving back to Bound state ...", 1); reply_for_request = server_renew_reply; } } } else { // Create and send a release write_to_log ("Moving into End state ...", 1); g_state = END; printf ("\nEntering release_lease function ...\n"); wait_for_key = getch(); server_release_reply = release_lease (interface_name, reply_for_request); // DELETE THE PREVIOUS IPV6 ADDRESS unset_ipv6_address (get_options_ptr (reply_for_request, OPTION_IAADDR), interface_name); break; } } } } // Exit the program exit (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -