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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdio.h>
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <stddef.h>
  25. #include <string.h>
  26. #include <assert.h>
  27. #include <getopt.h>
  28. #include <gpxe/netdevice.h>
  29. #include <gpxe/in.h>
  30. #include <gpxe/command.h>
  31. #include <usr/dhcpmgmt.h>
  32. /** @file
  33. *
  34. * DHCP management commands
  35. *
  36. */
  37. /**
  38. * "dhcp" command syntax message
  39. *
  40. * @v argv Argument list
  41. */
  42. static void dhcp_syntax ( char **argv ) {
  43. printf ( "Usage:\n"
  44. " %s <interface>\n"
  45. "\n"
  46. "Configure a network interface using DHCP\n",
  47. argv[0] );
  48. }
  49. /**
  50. * The "dhcp" command
  51. *
  52. * @v argc Argument count
  53. * @v argv Argument list
  54. * @ret rc Exit code
  55. */
  56. static int dhcp_exec ( int argc, char **argv ) {
  57. static struct option longopts[] = {
  58. { "help", 0, NULL, 'h' },
  59. { NULL, 0, NULL, 0 },
  60. };
  61. const char *netdev_txt;
  62. struct net_device *netdev;
  63. int c;
  64. int rc;
  65. /* Parse options */
  66. while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
  67. switch ( c ) {
  68. case 'h':
  69. /* Display help text */
  70. default:
  71. /* Unrecognised/invalid option */
  72. dhcp_syntax ( argv );
  73. return 1;
  74. }
  75. }
  76. /* Need exactly one interface name remaining after the options */
  77. if ( optind != ( argc - 1 ) ) {
  78. dhcp_syntax ( argv );
  79. return 1;
  80. }
  81. netdev_txt = argv[optind];
  82. /* Parse arguments */
  83. netdev = find_netdev ( netdev_txt );
  84. if ( ! netdev ) {
  85. printf ( "No such interface: %s\n", netdev_txt );
  86. return 1;
  87. }
  88. /* Perform DHCP */
  89. if ( ( rc = dhcp ( netdev ) ) != 0 ) {
  90. printf ( "Could not configure %s: %s\n", netdev->name,
  91. strerror ( rc ) );
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. /**
  97. * "pxebs" command syntax message
  98. *
  99. * @v argv Argument list
  100. */
  101. static void pxebs_syntax ( char **argv ) {
  102. printf ( "Usage:\n"
  103. " %s <interface> <server_type>\n"
  104. "\n"
  105. "Perform PXE Boot Server discovery\n",
  106. argv[0] );
  107. }
  108. /**
  109. * The "pxebs" command
  110. *
  111. * @v argc Argument count
  112. * @v argv Argument list
  113. * @ret rc Exit code
  114. */
  115. static int pxebs_exec ( int argc, char **argv ) {
  116. static struct option longopts[] = {
  117. { "help", 0, NULL, 'h' },
  118. { NULL, 0, NULL, 0 },
  119. };
  120. const char *netdev_txt;
  121. const char *pxe_type_txt;
  122. struct net_device *netdev;
  123. unsigned int pxe_type;
  124. char *end;
  125. int c;
  126. int rc;
  127. /* Parse options */
  128. while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
  129. switch ( c ) {
  130. case 'h':
  131. /* Display help text */
  132. default:
  133. /* Unrecognised/invalid option */
  134. pxebs_syntax ( argv );
  135. return 1;
  136. }
  137. }
  138. if ( optind != ( argc - 2 ) ) {
  139. pxebs_syntax ( argv );
  140. return 1;
  141. }
  142. netdev_txt = argv[optind];
  143. pxe_type_txt = argv[ optind + 1 ];
  144. /* Parse arguments */
  145. netdev = find_netdev ( netdev_txt );
  146. if ( ! netdev ) {
  147. printf ( "No such interface: %s\n", netdev_txt );
  148. return 1;
  149. }
  150. pxe_type = strtoul ( pxe_type_txt, &end, 0 );
  151. if ( *end ) {
  152. printf ( "Bad server type: %s\n", pxe_type_txt );
  153. return 1;
  154. }
  155. /* Perform Boot Server Discovery */
  156. if ( ( rc = pxebs ( netdev, pxe_type ) ) != 0 ) {
  157. printf ( "Could not discover boot server on %s: %s\n",
  158. netdev->name, strerror ( rc ) );
  159. return 1;
  160. }
  161. return 0;
  162. }
  163. /** DHCP management commands */
  164. struct command dhcp_commands[] __command = {
  165. {
  166. .name = "dhcp",
  167. .exec = dhcp_exec,
  168. },
  169. {
  170. .name = "pxebs",
  171. .exec = pxebs_exec,
  172. },
  173. };