Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

dhcp_cmd.c 4.3KB

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