📄 dhcpcmd.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/router/dhcpcmd.c,v 1.2 2001/11/09 21:06:49 josh Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved. Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** * Copyright 1994-1997 Epilogue Technology Corporation. * Copyright 1998 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* * $Log: dhcpcmd.c,v $ * Revision 1.2 2001/11/09 21:06:49 josh * demo router path modification * * Revision 1.1.1.1 2001/11/05 17:49:09 tneale * Tornado shuffle * * Revision 2.4 2001/01/19 22:24:37 paul * Update copyright. * * Revision 2.3 2000/03/17 00:14:17 meister * Update copyright message * * Revision 2.2 1998/08/17 22:47:21 wes * Modify 'dhcp punt' command slightly * * Revision 2.1 1998/06/25 21:31:12 wes * Add command to start/stop/abort DHCP from the command line * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*/#include <stdio.h>#include <stdlib.h>#include <wrn/wm/attache/config.h>#include <wrn/wm/attache/net.h>#include <wrn/wm/attache/ip.h>#include <wrn/wm/attache/ipaddr.h>#include <wrn/wm/attache/glue.h>#include "cmds.h"#if INSTALL_ATTACHE_DHCP#include <wrn/wm/attache/dhcp.h>#endif#include <wrn/wm/attache/route.h>#include <wrn/wm/demo/snarklib.h>#include <wrn/wm/demo/read_ini.h>#if INSTALL_ATTACHE_DHCP/* string for DHCP second-level commands */static char * dhcp_cmds[] = { "start", "stop", "status", "punt" };#define DHCP_CMDS (sizeof(dhcp_cmds)/sizeof(*dhcp_cmds))/* dhcp_parse_parm() * * Parse a parameter to a command. * * Takes a string containing the parameter entered, * a list of possible parameter values to match it against, * and a count of the number of values in that list. * Returns the 1-based offset of the matched value, * or 0 if not found. */static int dhcp_parse_parm(char * cmd, char * cmd_list[], int num_choices){ int i; for (i=0; i<num_choices; i++) if (STRNICMP(cmd, cmd_list[i], strlen(cmd)) == 0) return (i+1); return 0;}static void dhcp_cmd_cb_func(void * mycookie, dhcp_hdr_t * msg, dhcp_stateblk_t * state_block, dhcp_event_t event){#if 0 char buf[80]; char *s; struct ini_handle *oldhandle = ini_handle;#endif int p;#if SNARK_GRATUITOUS_ARP ipaddr_t myaddr_ip;#endif p = snark_dhcp_get_punt(state_block); if (p != 0) { p--; if (p == 0) dhcp_stop(state_block); printf("dhcp: stop in %d callbacks\n", p); snark_dhcp_set_punt(state_block, p); return; } switch (event) { case DHCP_EVT_GOT_RESPONSE: if (DHCP_INVC_STATE(state_block) == DHCP_ST_SELECTING) dhcp_request(state_block, msg); break; case DHCP_EVT_TIMEOUT_CHOOSE_OFFER: fprintf(stderr,"\"Shouldn't happen\" DHCP event!\n"); dhcp_stop(state_block); break; case DHCP_EVT_MSG_TO_GO:#if 0 if (oldhandle == 0) ini_handle = ini_open(256); /* Open the ini file */ sprintf(buf,"%s-dhcp-clientid",state_block->net->s_name); if ((s = INI_LOOKUP(attache_section, buf)) != 0 && (STRLEN(s) >0)) dhcp_add_option(msg, DHCP_TAG_CLIENT_ID, (bits8_t) STRLEN(s), (bits8_t *) s); if (oldhandle == 0) ini_close(ini_handle); /* Close the ini file */ ini_handle = oldhandle;#endif break; case DHCP_EVT_CANT_ADD_ADDR_MASK: case DHCP_EVT_CANT_ADD_ADDR_BRD: case DHCP_EVT_CANT_ADD_ADDR_MEM: case DHCP_EVT_CANT_ADD_ADDR_UNK: fprintf(stderr,"DHCP problem adding address!\n"); dhcp_stop(state_block); break; case DHCP_EVT_BOUND: { int optionlen = 0; unsigned char *buf = 0; dhcp_err_t code = dhcp_get_option(msg, DHCP_TAG_X_FS, &optionlen, 0); printf("get_option returns code %d, optlen %d\n", code, optionlen); code = dhcp_get_option(msg, DHCP_TAG_ROUTER, &optionlen, 0); printf("get_option returns code %d, optlen %d\n", code, optionlen); if (code == 0) { int i; buf = GLUE_ALLOC(optionlen); code = dhcp_get_option(msg, DHCP_TAG_ROUTER, &optionlen, buf); printf("Value:"); for (i=0; i<optionlen; i++) { printf(" %02x", buf[i]); } printf("\n"); } } #if INSTALL_SNARK_DHCP_CONSOLE_MSGS printf(" -- Interface %s has address %s; DHCP running --\n", state_block->net->s_name, inettoa(state_block->myaddr));#endif /* INSTALL_SNARK_DHCP_CONSOLE_MSGS */#if SNARK_GRATUITOUS_ARP /* it'll end up doing this on every lease renewal as well as * when it first starts, but that's okay for demo code; * production code shouldn't be doing this in the first place * (because it should have a permanent hardware address) */ SET_IPADDR_TYPE(&myaddr_ip, IPV4); MEMCPY(PTR_IPADDR_BITS(&myaddr_ip), &state_block->myaddr, IPV4_ADDR_LEN); snark_gratuitous_arp(state_block->net, &myaddr_ip);#endif break; case DHCP_EVT_BOOTP_MSG: fprintf(stderr, "DHCP -- Server error!\n"); break; case DHCP_EVT_LEASE_RENEWING: case DHCP_EVT_LEASE_REBINDING: case DHCP_EVT_ADDRESS_LOST:#if INSTALL_SNARK_DHCP_CONSOLE_MSGS printf(" -- DHCP address renegotiation... --\n");#endif /* INSTALL_SNARK_DHCP_CONSOLE_MSGS */ break; case DHCP_EVT_QUITTING:#if INSTALL_SNARK_DHCP_CONSOLE_MSGS printf(" -- DHCP quit. --\n");#endif /* INSTALL_SNARK_DHCP_CONSOLE_MSGS */ snark_dhcp_remove(state_block); break; case DHCP_EVT_DECLINE: case DHCP_EVT_TIMEOUT_NO_RESPONSE: default: /* If we don't know about this particular event, just ignore it. */ break; } return;}int dump_status (void *cookie, dhcp_stateblk_t *dhcp, net_if *net) { struct sty *sty = cookie; static char *state_name[] = { "REBOOT", "INIT", "SELECTING", "REQUESTING", "BOUND", "RENEWING", "REBINDING", "ADDRESSCHECK" }; sty_printf(sty, "%s: %s\n", net->s_name? net->s_name : "???", state_name[dhcp->state]); return 0;}/* * commands: * dhcp start <if> * dhcp stop <if> * dhcp punt <if> * dhcp status <if> */boolean_t dhcp_do_cmd (int argc, char **argv, struct sty *sty) { dhcp_err_t code; dhcp_stateblk_t *dhcphandle; int p; char *ifname = argv[2]; net_if *n = if_lookup(ifname); if (n == NULL) { sty_printf(sty, "Unknown interface %s\n", ifname); return 1; } switch (dhcp_parse_parm(argv[1], dhcp_cmds, DHCP_CMDS)) { case 1: code = dhcp_coldstart (n, 0, dhcp_cmd_cb_func, &dhcphandle, 0); sty_printf(sty, "Coldstart returned %d\n", code); if (code == 0) { snark_dhcp_add (n, dhcphandle); } /* start */ break; case 2: dhcphandle = snark_dhcp_find (n); if (dhcphandle == NULL) { sty_printf(sty, "No DHCP invocation on %s found!\n", ifname); } else { dhcp_stop(dhcphandle); } /* stop */ break; case 3: snark_dhcp_walk (dump_status, sty); /* status */ break; case 4: p = 1; if (argc > 3) p = atoi(argv[3]); dhcphandle = snark_dhcp_find (n); if (dhcphandle == NULL) { sty_printf(sty, "No DHCP invocation on %s found!\n", ifname); } else { snark_dhcp_set_punt(dhcphandle, p); printf("dhcp will punt after %d callbacks\n", p); } /* punt */ break; } return 1;}boolean_t cmd_dhcp (struct sty *sty, enum help_level help, int argc, char **argv){ if (argc < 3) help=help_short; switch(help) { case help_none: return dhcp_do_cmd(argc, argv, sty); break; case help_short: sty_puts(sty, "dhcp dhcp start/stop/status/punt interface\n"); break; case help_long: sty_puts(sty, "\The \"dhcp\" command allows for automatic configuration of addresses on\n\interfaces via the DHCP protocol.\n"); break; } return 1;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -