dhcp_cmd.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <stdio.h>
00022 #include <stdint.h>
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <errno.h>
00026 #include <stddef.h>
00027 #include <string.h>
00028 #include <assert.h>
00029 #include <getopt.h>
00030 #include <gpxe/netdevice.h>
00031 #include <gpxe/in.h>
00032 #include <gpxe/command.h>
00033 #include <usr/dhcpmgmt.h>
00034 
00035 /** @file
00036  *
00037  * DHCP management commands
00038  *
00039  */
00040 
00041 /**
00042  * "dhcp" command syntax message
00043  *
00044  * @v argv              Argument list
00045  */
00046 static void dhcp_syntax ( char **argv ) {
00047         printf ( "Usage:\n"
00048                  "  %s <interface>\n"
00049                  "\n"
00050                  "Configure a network interface using DHCP\n",
00051                  argv[0] );
00052 }
00053 
00054 /**
00055  * The "dhcp" command
00056  *
00057  * @v argc              Argument count
00058  * @v argv              Argument list
00059  * @ret rc              Exit code
00060  */
00061 static int dhcp_exec ( int argc, char **argv ) {
00062         static struct option longopts[] = {
00063                 { "help", 0, NULL, 'h' },
00064                 { NULL, 0, NULL, 0 },
00065         };
00066         const char *netdev_txt;
00067         struct net_device *netdev;
00068         int c;
00069         int rc;
00070 
00071         /* Parse options */
00072         while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
00073                 switch ( c ) {
00074                 case 'h':
00075                         /* Display help text */
00076                 default:
00077                         /* Unrecognised/invalid option */
00078                         dhcp_syntax ( argv );
00079                         return 1;
00080                 }
00081         }
00082 
00083         /* Need exactly one interface name remaining after the options */
00084         if ( optind != ( argc - 1 ) ) {
00085                 dhcp_syntax ( argv );
00086                 return 1;
00087         }
00088         netdev_txt = argv[optind];
00089 
00090         /* Parse arguments */
00091         netdev = find_netdev ( netdev_txt );
00092         if ( ! netdev ) {
00093                 printf ( "No such interface: %s\n", netdev_txt );
00094                 return 1;
00095         }
00096 
00097         /* Perform DHCP */
00098         if ( ( rc = dhcp ( netdev ) ) != 0 ) {
00099                 printf ( "Could not configure %s: %s\n", netdev->name,
00100                          strerror ( rc ) );
00101                 return 1;
00102         }
00103 
00104         return 0;
00105 }
00106 
00107 /**
00108  * "pxebs" command syntax message
00109  *
00110  * @v argv              Argument list
00111  */
00112 static void pxebs_syntax ( char **argv ) {
00113         printf ( "Usage:\n"
00114                  "  %s <interface> <server_type>\n"
00115                  "\n"
00116                  "Perform PXE Boot Server discovery\n",
00117                  argv[0] );
00118 }
00119 
00120 /**
00121  * The "pxebs" command
00122  *
00123  * @v argc              Argument count
00124  * @v argv              Argument list
00125  * @ret rc              Exit code
00126  */
00127 static int pxebs_exec ( int argc, char **argv ) {
00128         static struct option longopts[] = {
00129                 { "help", 0, NULL, 'h' },
00130                 { NULL, 0, NULL, 0 },
00131         };
00132         const char *netdev_txt;
00133         const char *pxe_type_txt;
00134         struct net_device *netdev;
00135         unsigned int pxe_type;
00136         char *end;
00137         int c;
00138         int rc;
00139 
00140         /* Parse options */
00141         while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
00142                 switch ( c ) {
00143                 case 'h':
00144                         /* Display help text */
00145                 default:
00146                         /* Unrecognised/invalid option */
00147                         pxebs_syntax ( argv );
00148                         return 1;
00149                 }
00150         }
00151         if ( optind != ( argc - 2 ) ) {
00152                 pxebs_syntax ( argv );
00153                 return 1;
00154         }
00155         netdev_txt = argv[optind];
00156         pxe_type_txt = argv[ optind + 1 ];
00157 
00158         /* Parse arguments */
00159         netdev = find_netdev ( netdev_txt );
00160         if ( ! netdev ) {
00161                 printf ( "No such interface: %s\n", netdev_txt );
00162                 return 1;
00163         }
00164         pxe_type = strtoul ( pxe_type_txt, &end, 0 );
00165         if ( *end ) {
00166                 printf ( "Bad server type: %s\n", pxe_type_txt );
00167                 return 1;
00168         }
00169 
00170         /* Perform Boot Server Discovery */
00171         if ( ( rc = pxebs ( netdev, pxe_type ) ) != 0 ) {
00172                 printf ( "Could not discover boot server on %s: %s\n",
00173                          netdev->name, strerror ( rc ) );
00174                 return 1;
00175         }
00176 
00177         return 0;
00178 }
00179 
00180 /** DHCP management commands */
00181 struct command dhcp_commands[] __command = {
00182         {
00183                 .name = "dhcp",
00184                 .exec = dhcp_exec,
00185         },
00186         {
00187                 .name = "pxebs",
00188                 .exec = pxebs_exec,
00189         },
00190 };

Generated on Tue Apr 6 20:01:06 2010 for gPXE by  doxygen 1.5.7.1