選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

dhcp_cmd.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <ipxe/netdevice.h>
  29. #include <ipxe/in.h>
  30. #include <ipxe/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>] [<interface>...]\n"
  45. "\n"
  46. "Configure a network interface using DHCP\n",
  47. argv[0] );
  48. }
  49. /**
  50. * Execute "dhcp" command for a network device
  51. *
  52. * @v netdev Network device
  53. * @ret rc Exit code
  54. */
  55. static int dhcp_exec_netdev ( struct net_device *netdev ) {
  56. int rc;
  57. if ( ( rc = dhcp ( netdev ) ) != 0 ) {
  58. printf ( "Could not configure %s: %s\n",
  59. netdev->name, strerror ( rc ) );
  60. /* Close device on failure, to avoid memory exhaustion */
  61. netdev_close ( netdev );
  62. return 1;
  63. }
  64. return 0;
  65. }
  66. /**
  67. * Execute "dhcp" command for a named network device
  68. *
  69. * @v netdev_name Network device name
  70. * @ret rc Exit code
  71. */
  72. static int dhcp_exec_name ( const char *netdev_name ) {
  73. struct net_device *netdev;
  74. netdev = find_netdev ( netdev_name );
  75. if ( ! netdev ) {
  76. printf ( "No such interface \"%s\"\n", netdev_name );
  77. return 1;
  78. }
  79. return dhcp_exec_netdev ( netdev );
  80. }
  81. /**
  82. * The "dhcp" command
  83. *
  84. * @v argc Argument count
  85. * @v argv Argument list
  86. * @ret rc Exit code
  87. */
  88. static int dhcp_exec ( int argc, char **argv ) {
  89. static struct option longopts[] = {
  90. { "help", 0, NULL, 'h' },
  91. { NULL, 0, NULL, 0 },
  92. };
  93. const char *netdev_name;
  94. struct net_device *netdev;
  95. int c;
  96. int rc;
  97. /* Parse options */
  98. while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
  99. switch ( c ) {
  100. case 'h':
  101. /* Display help text */
  102. default:
  103. /* Unrecognised/invalid option */
  104. dhcp_syntax ( argv );
  105. return 1;
  106. }
  107. }
  108. if ( optind != argc ) {
  109. /* Treat arguments as a list of interfaces to try */
  110. while ( optind != argc ) {
  111. netdev_name = argv[optind++];
  112. if ( ( rc = dhcp_exec_name ( netdev_name ) ) == 0 )
  113. return 0;
  114. }
  115. } else {
  116. /* Try all interfaces */
  117. for_each_netdev ( netdev ) {
  118. if ( ( rc = dhcp_exec_netdev ( netdev ) ) == 0 )
  119. return 0;
  120. }
  121. }
  122. return 1;
  123. }
  124. /**
  125. * "pxebs" command syntax message
  126. *
  127. * @v argv Argument list
  128. */
  129. static void pxebs_syntax ( char **argv ) {
  130. printf ( "Usage:\n"
  131. " %s <interface> <server_type>\n"
  132. "\n"
  133. "Perform PXE Boot Server discovery\n",
  134. argv[0] );
  135. }
  136. /**
  137. * The "pxebs" command
  138. *
  139. * @v argc Argument count
  140. * @v argv Argument list
  141. * @ret rc Exit code
  142. */
  143. static int pxebs_exec ( int argc, char **argv ) {
  144. static struct option longopts[] = {
  145. { "help", 0, NULL, 'h' },
  146. { NULL, 0, NULL, 0 },
  147. };
  148. const char *netdev_txt;
  149. const char *pxe_type_txt;
  150. struct net_device *netdev;
  151. unsigned int pxe_type;
  152. char *end;
  153. int c;
  154. int rc;
  155. /* Parse options */
  156. while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
  157. switch ( c ) {
  158. case 'h':
  159. /* Display help text */
  160. default:
  161. /* Unrecognised/invalid option */
  162. pxebs_syntax ( argv );
  163. return 1;
  164. }
  165. }
  166. if ( optind != ( argc - 2 ) ) {
  167. pxebs_syntax ( argv );
  168. return 1;
  169. }
  170. netdev_txt = argv[optind];
  171. pxe_type_txt = argv[ optind + 1 ];
  172. /* Parse arguments */
  173. netdev = find_netdev ( netdev_txt );
  174. if ( ! netdev ) {
  175. printf ( "No such interface: %s\n", netdev_txt );
  176. return 1;
  177. }
  178. pxe_type = strtoul ( pxe_type_txt, &end, 0 );
  179. if ( *end ) {
  180. printf ( "Bad server type: %s\n", pxe_type_txt );
  181. return 1;
  182. }
  183. /* Perform Boot Server Discovery */
  184. if ( ( rc = pxebs ( netdev, pxe_type ) ) != 0 ) {
  185. printf ( "Could not discover boot server on %s: %s\n",
  186. netdev->name, strerror ( rc ) );
  187. return 1;
  188. }
  189. return 0;
  190. }
  191. /** DHCP management commands */
  192. struct command dhcp_commands[] __command = {
  193. {
  194. .name = "dhcp",
  195. .exec = dhcp_exec,
  196. },
  197. {
  198. .name = "pxebs",
  199. .exec = pxebs_exec,
  200. },
  201. };