You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dhcp_cmd.c 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stdio.h>
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <stddef.h>
  24. #include <string.h>
  25. #include <assert.h>
  26. #include <getopt.h>
  27. #include <gpxe/netdevice.h>
  28. #include <gpxe/command.h>
  29. #include <usr/dhcpmgmt.h>
  30. /** @file
  31. *
  32. * DHCP management commands
  33. *
  34. */
  35. /**
  36. * "dhcp" command syntax message
  37. *
  38. * @v argv Argument list
  39. */
  40. static void dhcp_syntax ( char **argv ) {
  41. printf ( "Usage:\n"
  42. " %s <interface>\n"
  43. "\n"
  44. "Configure a network interface using DHCP\n",
  45. argv[0] );
  46. }
  47. /**
  48. * The "dhcp" command
  49. *
  50. * @v argc Argument count
  51. * @v argv Argument list
  52. * @ret rc Exit code
  53. */
  54. static int dhcp_exec ( int argc, char **argv ) {
  55. static struct option longopts[] = {
  56. { "help", 0, NULL, 'h' },
  57. { NULL, 0, NULL, 0 },
  58. };
  59. const char *name;
  60. struct net_device *netdev;
  61. int c;
  62. int rc;
  63. /* Parse options */
  64. while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
  65. switch ( c ) {
  66. case 'h':
  67. /* Display help text */
  68. default:
  69. /* Unrecognised/invalid option */
  70. dhcp_syntax ( argv );
  71. return 1;
  72. }
  73. }
  74. /* Need exactly one interface name remaining after the options */
  75. if ( optind != ( argc - 1 ) ) {
  76. dhcp_syntax ( argv );
  77. return 1;
  78. }
  79. name = argv[optind];
  80. /* Perform DHCP */
  81. netdev = find_netdev ( name );
  82. if ( ! netdev ) {
  83. printf ( "No such interface: %s\n", name );
  84. return 1;
  85. }
  86. if ( ( rc = dhcp ( netdev ) ) != 0 ) {
  87. printf ( "Could not configure %s: %s\n", netdev->name,
  88. strerror ( rc ) );
  89. return 1;
  90. }
  91. return 0;
  92. }
  93. /** DHCP management commands */
  94. struct command dhcp_commands[] __command = {
  95. {
  96. .name = "dhcp",
  97. .exec = dhcp_exec,
  98. },
  99. };