david/ipxe
Archived
1
0
This repository has been archived on 2020-12-06. You can view files and clone it, but cannot push or open issues or pull requests.
ipxe/src/hci/commands/dhcp_cmd.c

225 lines
4.7 KiB
C
Raw Normal View History

2007-01-12 10:53:28 +01:00
/*
* Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
*
* 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 the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
FILE_LICENCE ( GPL2_OR_LATER );
2007-07-03 19:20:22 +02:00
#include <stdio.h>
2007-01-12 10:53:28 +01:00
#include <stdint.h>
#include <stdlib.h>
2007-01-19 02:13:12 +01:00
#include <stdio.h>
2007-07-03 19:20:22 +02:00
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>
2007-01-12 10:53:28 +01:00
#include <getopt.h>
#include <ipxe/netdevice.h>
#include <ipxe/in.h>
#include <ipxe/command.h>
2007-01-12 10:53:28 +01:00
#include <usr/dhcpmgmt.h>
/** @file
*
* DHCP management commands
*
*/
/**
* "dhcp" command syntax message
*
* @v argv Argument list
*/
static void dhcp_syntax ( char **argv ) {
printf ( "Usage:\n"
" %s [<interface>] [<interface>...]\n"
2007-01-12 10:53:28 +01:00
"\n"
"Configure a network interface using DHCP\n",
argv[0] );
}
/**
* Execute "dhcp" command for a network device
*
* @v netdev Network device
* @ret rc Exit code
*/
static int dhcp_exec_netdev ( struct net_device *netdev ) {
int rc;
if ( ( rc = dhcp ( netdev ) ) != 0 ) {
printf ( "Could not configure %s: %s\n",
netdev->name, strerror ( rc ) );
/* Close device on failure, to avoid memory exhaustion */
netdev_close ( netdev );
return 1;
}
return 0;
}
/**
* Execute "dhcp" command for a named network device
*
* @v netdev_name Network device name
* @ret rc Exit code
*/
static int dhcp_exec_name ( const char *netdev_name ) {
struct net_device *netdev;
netdev = find_netdev ( netdev_name );
if ( ! netdev ) {
printf ( "No such interface \"%s\"\n", netdev_name );
return 1;
}
return dhcp_exec_netdev ( netdev );
}
2007-01-12 10:53:28 +01:00
/**
* The "dhcp" command
*
* @v argc Argument count
* @v argv Argument list
* @ret rc Exit code
*/
static int dhcp_exec ( int argc, char **argv ) {
static struct option longopts[] = {
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 },
};
const char *netdev_name;
2007-01-12 10:53:28 +01:00
struct net_device *netdev;
int c;
int rc;
/* Parse options */
while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
switch ( c ) {
case 'h':
/* Display help text */
default:
/* Unrecognised/invalid option */
dhcp_syntax ( argv );
return 1;
}
}
if ( optind != argc ) {
/* Treat arguments as a list of interfaces to try */
while ( optind != argc ) {
netdev_name = argv[optind++];
if ( ( rc = dhcp_exec_name ( netdev_name ) ) == 0 )
return 0;
}
} else {
/* Try all interfaces */
for_each_netdev ( netdev ) {
if ( ( rc = dhcp_exec_netdev ( netdev ) ) == 0 )
return 0;
}
2007-01-12 10:53:28 +01:00
}
return 1;
2007-01-12 10:53:28 +01:00
}
/**
* "pxebs" command syntax message
*
* @v argv Argument list
*/
static void pxebs_syntax ( char **argv ) {
printf ( "Usage:\n"
" %s <interface> <server_type>\n"
"\n"
"Perform PXE Boot Server discovery\n",
argv[0] );
}
/**
* The "pxebs" command
*
* @v argc Argument count
* @v argv Argument list
* @ret rc Exit code
*/
static int pxebs_exec ( int argc, char **argv ) {
static struct option longopts[] = {
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 },
};
const char *netdev_txt;
const char *pxe_type_txt;
struct net_device *netdev;
unsigned int pxe_type;
char *end;
int c;
int rc;
/* Parse options */
while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
switch ( c ) {
case 'h':
/* Display help text */
default:
/* Unrecognised/invalid option */
pxebs_syntax ( argv );
return 1;
}
}
if ( optind != ( argc - 2 ) ) {
pxebs_syntax ( argv );
return 1;
}
netdev_txt = argv[optind];
pxe_type_txt = argv[ optind + 1 ];
/* Parse arguments */
netdev = find_netdev ( netdev_txt );
if ( ! netdev ) {
printf ( "No such interface: %s\n", netdev_txt );
return 1;
}
pxe_type = strtoul ( pxe_type_txt, &end, 0 );
if ( *end ) {
printf ( "Bad server type: %s\n", pxe_type_txt );
return 1;
}
/* Perform Boot Server Discovery */
if ( ( rc = pxebs ( netdev, pxe_type ) ) != 0 ) {
printf ( "Could not discover boot server on %s: %s\n",
netdev->name, strerror ( rc ) );
return 1;
}
return 0;
}
2007-01-12 10:53:28 +01:00
/** DHCP management commands */
struct command dhcp_commands[] __command = {
{
.name = "dhcp",
.exec = dhcp_exec,
},
{
.name = "pxebs",
.exec = pxebs_exec,
},
2007-01-12 10:53:28 +01:00
};