From 3f5052b89c82ec731b63ae1149a00b9f49958318 Mon Sep 17 00:00:00 2001 From: Martin 'povik' Poviser Date: Thu, 4 Feb 2010 23:06:00 +0100 Subject: [PATCH] Added dhcpcl command; Some updates in kernel/core/net/dhcp.c; --- kernel/core/commands.c | 51 ++++++++++++++++++++++++++- kernel/core/net/dhcp.c | 88 +++++++++++++++++++++++++++++------------------ kernel/include/net/dhcp.h | 32 +++++++++-------- 3 files changed, 121 insertions(+), 50 deletions(-) diff --git a/kernel/core/commands.c b/kernel/core/commands.c index fb358d1..28b66d2 100644 --- a/kernel/core/commands.c +++ b/kernel/core/commands.c @@ -1267,6 +1267,54 @@ unsigned command_dnsconfig (char *command, unsigned len) return 1; } +unsigned command_dhcpcl (char *command, unsigned len) +{ + strcpy (test, argparse (currtty->shell)); + + if (!test) + return 0; + + unsigned l = strlen (test); + + if (!l) { + printf ("dhcpcl -> please specify interface\n"); + return 0; + } + + netif_t *netif = netif_findbyname (test); + + if (!netif) { + printf ("dhcpcl -> bad network interface name, example: eth0\n"); + return 0; + } + + unsigned ret = dhcp_config_if (netif); + + if (ret < 0) { + switch (ret) { + case DHCP_OUT_OF_MEMORY: + printf ("dhcpcl -> out of memory\n"); + break; + case DHCP_SOCKET_FAILED: + case DHCP_CONNECT_FAILED: + case DHCP_CANT_SEND_REQUEST: + printf ("dhcpcl -> problem with network layer\n"); + break; + case DHCP_CANT_RECV_RESPONSE: + printf ("dhcpcl -> can't receive response\n"); + break; + case DHCP_BAD_PACKET: + printf ("dhcpcl -> bad packet received\n"); + break; + case DHCP_SRV_DIDNT_UNDERSTAND: + printf ("dhcpcl -> server didn't understand\n"); + break; + } + } else { + printf ("dhcpcl -> interface %s successfully configured\n"); + } +} + unsigned command_tunconfig (char *command, unsigned len) { strcpy (test, argparse (currtty->shell)); @@ -2095,7 +2143,7 @@ unsigned int init_commands () command_register ("xmastime", "play noels on pc-speaker", &command_spk, 0); command_register ("test", "Some test", &command_test, 0); command_register ("test2", "Some test", &command_test2, 0); - command_register ("test3", "Some test", &command_test3, 0); + //command_register ("test3", "Some test", &command_test3, 0); //command_register ("testd", "Some test", &command_test4, 0); //command_register ("vesa", "Some test", &command_vesa, 0); command_register ("kill", "Send a signal to process", &command_kill, 0); @@ -2109,6 +2157,7 @@ unsigned int init_commands () command_register ("ifconfig", "Configure network interface", &command_ifconfig, 0); command_register ("ifroute", "Configure gateway address", &command_ifroute, 0); command_register ("dnsconfig", "Configure domain name server address", &command_dnsconfig, 0); + command_register ("dhcpcl", "Configure interface using DHCP", &command_dhcpcl, 0); #ifdef CONFIG_PROTO_TUN6 command_register ("tunconfig", "Configure IPv6 tunnel server address", &command_tunconfig, 0); #endif diff --git a/kernel/core/net/dhcp.c b/kernel/core/net/dhcp.c index 005537b..859e66e 100644 --- a/kernel/core/net/dhcp.c +++ b/kernel/core/net/dhcp.c @@ -2,6 +2,7 @@ * ZeX/OS * Copyright (C) 2009 Martin 'povik' Poviser (martin.povik@gmail.com) * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com) + * Copyright (C) 2010 Martin 'povik' Poviser (martin.povik@gmail.com) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -75,18 +76,18 @@ int dhcp_config_if (netif_t *netif) dhcp_request_t request; if (!netif) - return -1; + return DHCP_BAD_PARAMETERS; request.buffer = kmalloc (DHCP_REQUEST_MAX_LEN); if (!request.buffer) - return -2; + return DHCP_OUT_OF_MEMORY; char *buffer = kmalloc (1024); if (!buffer) { kfree (request.buffer); - return -2; + return DHCP_OUT_OF_MEMORY; } net_ipv4 config_gw = 0x00000000; @@ -100,7 +101,7 @@ int dhcp_config_if (netif_t *netif) /* Create a socket */ if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { - kprintf ("DHCP client -> Can't create socket.\n"); + DPRINT (DBG_DHCP, "DHCP client -> Can't create socket."); ret = DHCP_SOCKET_FAILED; goto clear; } @@ -112,7 +113,7 @@ int dhcp_config_if (netif_t *netif) /* Let's connect to server */ if (connect (sock, (sockaddr *) &server_socket, sizeof (server_socket)) == -1) { - kprintf ("DHCP client -> Connection can't be estabilished\n"); + DPRINT (DBG_DHCP, "DHCP client -> Connection can't be estabilished"); ret = DHCP_CONNECT_FAILED; goto clear; } @@ -150,13 +151,13 @@ int dhcp_config_if (netif_t *netif) int recv_ret; - kprintf ("DHCP client -> sending request\n"); + DPRINT (DBG_DHCP, "DHCP client -> sending request"); if (send (sock, request.buffer, pos, 0) < 0) { - kprintf ("DHCP client -> Can't send request.\n"); + DPRINT (DBG_DHCP, "DHCP client -> Can't send request."); ret = DHCP_CANT_SEND_REQUEST; goto clear; } - kprintf ("DHCP client -> reuquest sended\n"); + DPRINT (DBG_DHCP, "DHCP client -> reuquest sended"); recv_dhcpoffer: config_ip = 0x00000000; @@ -164,16 +165,16 @@ recv_dhcpoffer: config_dns = 0x00000000; config_dhcp = 0x00000000; - kprintf ("DHCP client -> receiving response\n"); + DPRINT (DBG_DHCP, "DHCP client -> receiving response"); if ((recv_ret = recv (sock, request.buffer, DHCP_REQUEST_MAX_LEN, 0)) < sizeof (dhcp_bootp_header_t)) { - kprintf ("DHCP client -> Can't receive response.\n"); + DPRINT (DBG_DHCP, "DHCP client -> Can't receive response."); ret = DHCP_CANT_RECV_RESPONSE; goto clear; } - kprintf ("DHCP client -> response received\n"); + DPRINT (DBG_DHCP, "DHCP client -> response received"); if (request.bootp_header->mag_cookie != DHCP_MAGIC_COOKIE) { - kprintf ("DHCP client -> bad magic (finded 0x%X - needed 0x%X\n", + DPRINT (DBG_DHCP, "DHCP client -> bad magic (finded 0x%X - needed 0x%X", request.bootp_header->mag_cookie, DHCP_MAGIC_COOKIE); ret = DHCP_BAD_PACKET; goto clear; @@ -184,7 +185,7 @@ recv_dhcpoffer: config_ip = request.bootp_header->yiaddr; if (request.bootp_header->xid != xid) { - kprintf ("DHCP client -> bad xid"); + DPRINT (DBG_DHCP, "DHCP client -> bad xid"); goto recv_dhcpoffer; } @@ -229,7 +230,7 @@ recv_dhcpoffer: goto clear; break; default: - kprintf ("DHCP client -> bad DHCP message type"); + DPRINT (DBG_DHCP, "DHCP client -> bad DHCP message type"); goto recv_dhcpoffer; } break; @@ -249,6 +250,10 @@ recv_dhcpoffer: if (option_len >= 4) memcpy (&config_dhcp, request.buffer + pos, sizeof (net_ipv4)); break; + case DHCP_OPTION_LEASE_TIME: + if (option_len >= 4) + memcpy (&config_lease_time, request.buffer + pos, sizeof (unsigned int)); + break; default: break; } @@ -256,7 +261,7 @@ recv_dhcpoffer: pos += option_len; } - kprintf ("DHCP client -> response parsed\n"); + DPRINT (DBG_DHCP, "DHCP client -> response parsed"); if (dhcpack) goto configure; @@ -276,33 +281,33 @@ recv_dhcpoffer: pos = dhcp_request_option_add (request, DHCP_OPTION_DHCP_SERVER, buffer, 4, pos); - kprintf ("DHCP client -> sending request\n"); + DPRINT (DBG_DHCP, "DHCP client -> sending request"); if (send (sock, request.buffer, pos, 0) < 0) { - kprintf ("DHCP client -> Can't send request.\n"); + DPRINT (DBG_DHCP, "DHCP client -> Can't send request."); ret = DHCP_CANT_SEND_REQUEST; goto clear; } - kprintf ("DHCP client -> request sended\n"); + DPRINT (DBG_DHCP, "DHCP client -> request sended"); recv_dhcpack: - kprintf ("DHCP client -> receiving response\n"); + DPRINT (DBG_DHCP, "DHCP client -> receiving response"); if ((recv_ret = recv (sock, request.buffer, DHCP_REQUEST_MAX_LEN, 0)) < sizeof (dhcp_bootp_header_t)) { - kprintf ("DHCP client -> Can't receive response.\n"); + DPRINT (DBG_DHCP, "DHCP client -> Can't receive response."); ret = DHCP_CANT_RECV_RESPONSE; goto clear; } - kprintf ("DHCP client -> response received\n"); + DPRINT (DBG_DHCP, "DHCP client -> response received"); if (request.bootp_header->mag_cookie != DHCP_MAGIC_COOKIE) { - kprintf ("DHCP client -> bad magic (finded 0x%X - needed 0x%X\n", + DPRINT (DBG_DHCP, "DHCP client -> bad magic (finded 0x%X - needed 0x%X", request.bootp_header->mag_cookie, DHCP_MAGIC_COOKIE); ret = DHCP_BAD_PACKET; goto clear; } if (request.bootp_header->xid != xid) { - kprintf ("DHCP client -> bad xid"); + DPRINT (DBG_DHCP, "DHCP client -> bad xid"); goto recv_dhcpoffer; } @@ -349,7 +354,7 @@ recv_dhcpack: goto clear; break; default: - kprintf ("DHCP client -> bad DHCP message type"); + DPRINT (DBG_DHCP, "DHCP client -> bad DHCP message type"); goto recv_dhcpoffer; } break; @@ -365,6 +370,10 @@ recv_dhcpack: if (option_len >= 4) memcpy (&config_dns, request.buffer + pos, sizeof (net_ipv4)); break; + case DHCP_OPTION_LEASE_TIME: + if (option_len >= 4) + memcpy (&config_lease_time, request.buffer + pos, sizeof (unsigned int)); + break; default: break; } @@ -373,15 +382,26 @@ recv_dhcpack: } configure: - kprintf ("DHCP client -> configuring interface\n"); - kprintf ("DHCP client -> my IP: "); - net_proto_ip_print (config_ip); - kprintf ("\nDHCP client -> router IP: "); - net_proto_ip_print (config_gw); - kprintf ("\nDHCP client -> DNS server IP: "); - net_proto_ip_print (config_dns); - kprintf ("\n"); - + DPRINT (DBG_DHCP, "DHCP client -> configuring interface"); + + char *ip_str = kmalloc (17); // 17 = IPv4 address in string maximum size + 1 zero + + if (ip_str) { + memset (ip_str, 0, 17); + net_proto_ip_convert2 (config_ip, ip_str); + DPRINT (DBG_DHCP, "DHCP client -> my IP: %s", ip_str); + + memset (ip_str, 0, 17); + net_proto_ip_convert2 (config_gw, ip_str); + DPRINT (DBG_DHCP, "DHCP client -> router IP: %s", ip_str); + + memset (ip_str, 0, 17); + net_proto_ip_convert2 (config_dns, ip_str); + DPRINT (DBG_DHCP, "DHCP client -> DNS server IP: %s", ip_str); + + DPRINT (DBG_DHCP, "DHCP client -> IP lease time: %ud seconds", config_lease_time); + kfree (ip_str); + } dns_addr (config_dns); netif_ip_addr (netif, config_ip, IF_CFG_TYPE_DHCP); netif_gw_addr (netif, config_gw); @@ -392,4 +412,4 @@ clear: return ret; } - + diff --git a/kernel/include/net/dhcp.h b/kernel/include/net/dhcp.h index 0246bd1..6aec5f6 100644 --- a/kernel/include/net/dhcp.h +++ b/kernel/include/net/dhcp.h @@ -1,6 +1,7 @@ /* * ZeX/OS * Copyright (C) 2009 Martin 'povik' Poviser (martin.povik@gmail.com) + * Copyright (C) 2010 Martin 'povik' Poviser (martin.povik@gmail.com) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,10 +29,10 @@ #define DHCP_OUT_OF_MEMORY -2 #define DHCP_SOCKET_FAILED -3 #define DHCP_CONNECT_FAILED -4 -#define DHCP_CANT_SEND_REQUEST -5 -#define DHCP_CANT_RECV_RESPONSE -6 +#define DHCP_CANT_SEND_REQUEST -5 +#define DHCP_CANT_RECV_RESPONSE -6 #define DHCP_BAD_PACKET -7 -#define DHCP_SRV_DIDNT_UNDERSTAND -8 +#define DHCP_SRV_DIDNT_UNDERSTAND -8 /* magic cookie */ #define DHCP_MAGIC_COOKIE 0x63538263 @@ -47,21 +48,22 @@ #define DHCP_OPTION_DNS_SERVER 0x06 #define DHCP_OPTION_HOSTNAME 12 #define DHCP_OPTION_REQUESTED_IP 50 +#define DHCP_OPTION_LEASE_TIME 51 #define DHCP_OPTION_MESSAGE_TYPE 53 -#define DHCP_OPTION_VALUE_DHCPDISCOVER 0x01 -#define DHCP_OPTION_VALUE_DHCPOFFER 0x02 -#define DHCP_OPTION_VALUE_DHCPREQUEST 0x03 -#define DHCP_OPTION_VALUE_DHCPDECLINE 0x04 -#define DHCP_OPTION_VALUE_DHCPACK 0x05 -#define DHCP_OPTION_VALUE_DHCPNAK 0x06 -#define DHCP_OPTION_VALUE_DHCPRELEASE 0x07 -#define DHCP_OPTION_VALUE_DHCPINFORM 0x08 +#define DHCP_OPTION_VALUE_DHCPDISCOVER 0x01 +#define DHCP_OPTION_VALUE_DHCPOFFER 0x02 +#define DHCP_OPTION_VALUE_DHCPREQUEST 0x03 +#define DHCP_OPTION_VALUE_DHCPDECLINE 0x04 +#define DHCP_OPTION_VALUE_DHCPACK 0x05 +#define DHCP_OPTION_VALUE_DHCPNAK 0x06 +#define DHCP_OPTION_VALUE_DHCPRELEASE 0x07 +#define DHCP_OPTION_VALUE_DHCPINFORM 0x08 #define DHCP_OPTION_DHCP_SERVER 54 #define DHCP_OPTION_PARAMETER_REQUEST_LIST 55 -#define DHCP_OPTION_VALUE_SUBNET_MASK 0x01 -#define DHCP_OPTION_VALUE_ROUTER 0x03 -#define DHCP_OPTION_VALUE_DNS_SERVER 0x06 -#define DHCP_OPTION_VALUE_DOMAIN_NAME 0x0f +#define DHCP_OPTION_VALUE_SUBNET_MASK 0x01 +#define DHCP_OPTION_VALUE_ROUTER 0x03 +#define DHCP_OPTION_VALUE_DNS_SERVER 0x06 +#define DHCP_OPTION_VALUE_DOMAIN_NAME 0x0f #define DHCP_OPTION_CLIENT_IDENTIFIER 61 #define DHCP_OPTION_END 0xff -- 2.11.4.GIT