/* *=========================================================================== * netsysctlname2oid *=========================================================================== * Description: * Parameters: ctl - * name - * pNewName - * nameLen - * Returns: * */ IP_PUBLIC int netsysctlname2oid(Ipnet_cmd_sysctl *ctl, char* name, int* pNewName, int* nameLen) { #ifdef IPCOM_USE_INET int i; if(ctl == IP_NULL) { ctl = ipnet_sysctl_inet_table; pNewName[(*nameLen)++] = IP_IPPROTO_IP; } /* if you want to support trunk node, pls change this function */ while (ctl->comp != IP_NULL) { if (IP_BIT_ISSET(ctl->argument, IPNET_CMD_SYSCTL_NETIF_NO)) { if (ipcom_strcasecmp(name, ctl->comp) == 0) { for (i = 0; ctl->ctl[i] != -1; i++); pNewName[(*nameLen)++] = ctl->ctl[i ? i-1 : i]; } } /* Do the netifs */ if (IP_BIT_ISSET(ctl->argument, IPNET_CMD_SYSCTL_NETIF_YES)) { /* Dump the arguments */ char **argv = ipnet_cmd_sysctl_dump_netif(); if (argv) { for (i = 0; argv[i] != IP_NULL; i++) { if(ipcom_strcasecmp(name, argv[i])==0) { name += ipcom_strlen(argv[i]) + 1; pNewName[(*nameLen)++] = i; netsysctlname2oid(ipnet_sysctl_inet_table, name, pNewName, nameLen); } ipcom_free(argv[i]); } ipcom_free(argv); } } ctl++; } #endif return 0; }
/***************************************************************************** * * example_concatenate_tokens - Concatenate all options on this line * * .IP <tok> * The tokenizer buffer * * RETURNS: * A dynamically allocated memory buffer containing all the tokenized options * on success, IP_NULL otherwise. * * NOMANUAL */ IP_STATIC char * example_concatenate_tokens(example_token_buffer_t *tok) { char *value; char *conc = ipcom_malloc(256); if (conc != IP_NULL) { int no_values = 0; conc[0] = '\0'; while ((value = example_get_token(tok, IP_FALSE)) != IP_NULL) { if (ipcom_strcasecmp(value, ";") == 0) break; no_values++; ipcom_strncat(conc, value, 256); ipcom_strncat(conc, " ", 256); } /* No semicolon or no values */ if (no_values == 0 || value == IP_NULL) { ipcom_free(conc); return IP_NULL; } return conc; } return IP_NULL; }
/***************************************************************************** * * example_check_token - Check that the next token matches what is required. * * .IP <tok> * The tokenizer buffer * * .IP <req> * The required token * * .IP <nl> * IP_TRUE if we're allowed to consume newlines * * RETURNS: * IP_TRUE on success, IP_FALSE otherwise * * NOMANUAL */ IP_STATIC Ip_bool example_check_token(example_token_buffer_t *tok, const char *req, Ip_bool nl) { char *token = example_get_token(tok, nl); if (token) { if (req && ipcom_strcasecmp(token, req) == 0) return IP_TRUE; return IP_FALSE; } return req? IP_FALSE : IP_TRUE; }
/* *=========================================================================== * ipnet_cmd_sysctl_strcmp *=========================================================================== * Description: * Parameters: * Returns: * */ IP_STATIC Ip_bool ipnet_cmd_sysctl_strcmp(const Ipnet_cmd_sysctl *ctl, const char *what, int *name, int *namelen, char **names, int *nameslen) { if (ipcom_strcasecmp(what, ctl->comp) == 0) { int i; for (i = 0; ctl->ctl[i] != -1; i++) { /* Add the CTL values */ name[(*namelen)++] = ctl->ctl[i]; } /* Store name */ names[(*nameslen)++] = (char *)ctl->comp; return IP_TRUE; } return IP_FALSE; }
/* *=========================================================================== * ipcom_cmd_ipd *=========================================================================== * Description: * Parameters: * Returns: */ IP_PUBLIC int ipcom_cmd_ipd(int argc, char **argv) { Ipcom_getopt opt; int i, c, msgtype; Ip_err err = IPCOM_SUCCESS; #if IPCOM_VR_MAX > 1 int vr = ipcom_proc_vr_get(); int vr_new = vr; #endif if (argc < 2) { usage: ipcom_fprintf(ip_stderr, "Interpeak daemon (IPD) command, version 1.2"IP_LF "usage: "IP_LF " ipd [-V <vr>] list"IP_LF " ipd [-V <vr>] start <service>"IP_LF " ipd [-V <vr>] kill <service>"IP_LF " ipd [-V <vr>] reconfigure <service>"IP_LF " ipd [-V <vr>] <#> <service>"IP_LF IP_LF); return 0; } ipcom_getopt_clear_r(&opt); while ((c = ipcom_getopt_r(argc, argv, "V:", &opt)) != -1) { switch(c) { case 'V': #if IPCOM_VR_MAX > 1 vr_new = ipcom_atoi(opt.optarg); #endif break; default: ipcom_printf("ipd: unknown option %c"IP_LF, (char)c); return -1; } } if (opt.optind >= argc) { ipcom_printf("ipd: missing <command> argument"IP_LF); goto usage; } if(ipcom_strcmp(argv[opt.optind], "list") == 0) { #if IPCOM_VR_MAX > 1 if (vr != vr_new) ipcom_proc_vr_set(vr_new); #endif ipcom_printf("Services:"IP_LF); for (i = 0; ipcom_ipd_products[i].name != IP_NULL; i++) { if ((argc - (opt.optind + 1)) > 0) { int p; for (p = opt.optind + 1; p < argc; p++) { if (ipcom_strcasecmp(ipcom_ipd_products[i].name, argv[p]) == 0) goto print_service; } continue; } print_service: if (IP_BIT_ISSET(ipcom_ipd_products[i].flags, IPCOM_IPD_FLAG_IPD_START)) { #ifdef IP_PORT_OSE5 if (ipcom_ipd_products[i].start == IP_NULL && ipcom_ipd_isinstalled_ose5(ipcom_ipd_products[i].name) != IPCOM_SUCCESS) continue; #endif err = ipcom_ipd_send(ipcom_ipd_products[i].name, IPCOM_IPD_MSGTYPE_PING); ipcom_printf("%-20s %-s"IP_LF, ipcom_ipd_products[i].name, err == IPCOM_SUCCESS ? "started" : "killed"); } else if (ipcom_ipd_products[i].start) ipcom_printf("%-20s %-s"IP_LF, ipcom_ipd_products[i].name, "started"); } ipcom_printf(IP_LF); #if IPCOM_VR_MAX > 1 if (vr != vr_new) ipcom_proc_vr_set(vr); #endif return 0; } if ((argc - opt.optind) < 2) { ipcom_printf("ipd: missing <service> argument"IP_LF); return -1; } for (i = 0; ipcom_cmd_ipd_messages[i].name; i++) if (ipcom_strcmp(argv[opt.optind], ipcom_cmd_ipd_messages[i].name) == 0) { msgtype = ipcom_cmd_ipd_messages[i].msgtype; goto sendmsg; } if (*argv[opt.optind] == '-' && ipcom_isdigit(argv[opt.optind][1])) { /* "UNIX" signal support (using negative numbers) */ msgtype = -ipcom_atoi(argv[opt.optind] + 1); goto sendmsg; } if (ipcom_isdigit(argv[opt.optind][0])) { /* positive numbers */ msgtype = ipcom_atoi(argv[1]); goto sendmsg; } /* unknown command. */ ipcom_printf ("ipd: unknown command '%s'"IP_LF, argv[opt.optind]); return -1; /* send msg */ sendmsg: #if IPCOM_VR_MAX > 1 if (vr != vr_new) ipcom_proc_vr_set(vr_new); #endif if ((argc - opt.optind) < 3) err = ipcom_ipd_send(argv[opt.optind+1], msgtype); else { Ipcom_ipd_msg *msg; int sz = ipcom_strlen(argv[opt.optind + 2]) + 1; msg = ipcom_calloc(1, sz + sizeof(*msg)); if (msg != IP_NULL) { msg->msgtype = msgtype; ipcom_memcpy(msg + 1, argv[opt.optind + 2], sz); err = ipcom_ipd_sendmsg(argv[opt.optind+1], msg, sz + sizeof(*msg)); ipcom_free(msg); } } if(err == IPCOM_SUCCESS) ipcom_printf("ipd: %s %s ok"IP_LF, argv[opt.optind], argv[opt.optind+1]); else ipcom_printf("ipd: %s %s failed: %s"IP_LF, argv[opt.optind], argv[opt.optind+1], ipcom_err_string(err)); #if IPCOM_VR_MAX > 1 if (vr != vr_new) ipcom_proc_vr_set(vr); #endif return 0; }
/* *=========================================================================== * ipdnsc_hostent_merge *=========================================================================== * Description: Merge two AF_INET6 hostent structures to a common * Parameters: he1, he2 - pointers to the hostent structures to be merged * Returns: pointer to the new hostent structure or NULL if failed */ IP_STATIC struct Ip_hostent* ipdnsc_hostent_merge(struct Ip_hostent *he1, struct Ip_hostent *he2) { Ip_s32 i, num_addr1, num_alias1, num_addr2, num_alias2; struct Ip_hostent *he; if (he1 == IP_NULL || he2 == IP_NULL) return IP_NULL; /* Make sure hostnames are the same */ if (ipcom_strcasecmp(he1->h_name, he2->h_name)) return IP_NULL; /* Get the number of alias and address entries */ num_addr1 = ipdnsc_hostent_addr_count(he1); num_addr2 = ipdnsc_hostent_addr_count(he2); num_alias1 = ipdnsc_hostent_alias_count(he1); num_alias2 = ipdnsc_hostent_alias_count(he2); /* Create the new host entry structure */ he = ipdnsc_hostent_create(IP_AF_INET6); if (he == IP_NULL) return IP_NULL; /* Allocate memory for and copy the aliases */ for (i=0; i<(num_alias1-1); i++) { if (ipdnsc_hostent_insert_alias(he, he1->h_aliases[i])) { goto err_out; } } for (i=0; i<(num_alias2-1); i++) { Ip_s32 j, duplicate; /* Avoid duplicates */ duplicate = IP_FALSE; for (j=0; j<(num_alias1-1); j++) { if (!ipcom_strcasecmp(he1->h_aliases[j], he2->h_aliases[i])) { duplicate = IP_TRUE; break; } } if (duplicate == IP_FALSE) { if (ipdnsc_hostent_insert_alias(he, he2->h_aliases[i])) { goto err_out; } } } /* Allocate memory for and copy the addresses */ for (i=0; i<(num_addr1-1); i++) { if (ipdnsc_hostent_insert_addr(he, he1->h_addr_list[i])) { goto err_out; } } for (i=0; i<(num_addr2-1); i++) { if (ipdnsc_hostent_insert_addr(he, he2->h_addr_list[i])) { goto err_out; } } /* Insert the hostname */ if (ipdnsc_hostent_insert_name(he, he1->h_name)) { goto err_out; } return he; err_out: ipdnsc_hostent_free(he); return IP_NULL; }
/***************************************************************************** * * example_parse_base - Parse top-level ISC configuration items * * .IP <tok> * The tokenizer buffer * * RETURNS: * 0 on success * * NOMANUAL */ IP_STATIC Ip_s32 example_parse_base(example_token_buffer_t *tok) { for (;;) { char *token = example_get_token(tok, IP_TRUE); char *value; int ret; /* EOF; We've read the entire file - exit */ if (token == IP_NULL) return 0; /* Check the token and see if we know it */ if (ipcom_strcasecmp(token, "subnet") == 0) { ret = example_parse_subnet(tok); } else if (ipcom_strcasecmp(token, "option") == 0) { ret = example_parse_option(tok, "default"); } else if (ipcom_strcasecmp(token, "default-lease-time") == 0) { value = example_get_token(tok, IP_FALSE); if (!example_check_token(tok, ";", IP_FALSE)) { IPCOM_LOG1(ERR, "DHCPS(%d): lease time specified or ';' missing in default-lease-time directive", tok->curline); return -1; } example_do_command("dhcps config set default default-lease-time %s", value); } else if (ipcom_strcasecmp(token, "max-lease-time") == 0) { value = example_get_token(tok, IP_FALSE); if (!example_check_token(tok, ";", IP_FALSE)) { IPCOM_LOG1(ERR, "DHCPS(%d): lease time specified or ';' missing in max-lease-time directive", tok->curline); return -1; } example_do_command("dhcps config set default max-lease-time %s", value); } else if (ipcom_strcasecmp(token, "authoritative") == 0 || ipcom_strcasecmp(token, "ddns-update-style") == 0 || ipcom_strcasecmp(token, "log-facility") == 0) { /* Unsupported ISC directives * Consume this line */ IPCOM_LOG2(WARNING, "DHCPS(%d): Unhandled directive %s specified", tok->curline, token); example_get_line(tok); } } }
/***************************************************************************** * * example_parse_subnet - Parse an ISC subnet directive * * .IP <tok> * The tokenizer buffer. * * RETURNS: * 0 on success * * NOMANUAL */ IP_STATIC Ip_s32 example_parse_subnet(example_token_buffer_t *tok) { /* address NETMASK netmask { */ char *address, *netmask; char *token; /* We need to retrieve the address, the NETMASK keyword and the * netmask value */ address = example_get_token(tok, IP_FALSE); if (!example_check_token(tok, "netmask", IP_FALSE)) return -1; netmask = example_get_token(tok, IP_FALSE); /**/ if (address == IP_NULL || netmask == IP_NULL) return -1; /* The subnet block starts with a left bracer */ if (!example_check_token(tok, "{", IP_TRUE)) return -1; /* Add the subnet command */ example_do_command("dhcps subnet add %s %s", address, netmask); /* Go through the subnet */ while ((token = example_get_token(tok, IP_TRUE)) != IP_NULL) { int retval; /* End of subnet is a right bracer, exit successfully if found */ if (ipcom_strcasecmp(token, "}") == 0) return 0; else if (ipcom_strcasecmp(token, "option") == 0) { /* Parse a subnet specific option */ if ((retval = example_parse_option(tok, address)) != 0) return retval; } else if (ipcom_strcasecmp(token, "range") == 0) { /* Parse a pool range */ char *start, *end; start = example_get_token(tok, IP_FALSE); end = example_get_token(tok, IP_FALSE); if (!example_check_token(tok, ";", IP_FALSE)) { IPCOM_LOG1(ERR, "DHCPS(%d): Missing range start, end or ';' in range directive", tok->curline); return -1; } example_do_command("dhcps pool add %s %s %s", address, start, end); } else if (ipcom_strcasecmp(token, "default-lease-time") == 0) { char *lease; lease = example_get_token(tok, IP_FALSE); if (!example_check_token(tok, ";", IP_FALSE)) { IPCOM_LOG1(ERR, "DHCPS(%d): lease time specified or ';' missing in default-lease-time directive", tok->curline); return -1; } example_do_command("dhcps config set %s default-lease-time %s", address, lease); } else if (ipcom_strcasecmp(token, "max-lease-time") == 0) { char *lease; lease = example_get_token(tok, IP_FALSE); if (!example_check_token(tok, ";", IP_FALSE)) { IPCOM_LOG1(ERR, "DHCPS(%d): lease time specified or ';' missing in max-lease-time directive", tok->curline); return -1; } example_do_command("dhcps config set %s max-lease-time %s", address, lease); } } }
/***************************************************************************** * * example_parse_option - Parse a ISC option string * * .IP <tok> * The tokenizer buffer * * .IP <context> * The context the option resides in * * RETURNS: * 0 on success. * * NOMANUAL */ IP_STATIC Ip_s32 example_parse_option(example_token_buffer_t *tok, const char *context) { /* Retrieve the option name */ char *option = example_get_token(tok, IP_FALSE); if (option == IP_NULL) { IPCOM_LOG1(ERR, "DHCPS(%d): no option name specified", tok->curline); return -1; } if (ipcom_strcasecmp(option, "domain-name-servers") == 0) { /* DNS servers option; retrieve the list of servers and pass it * to our dhcps command */ char *value = example_concatenate_tokens(tok); if (value == IP_NULL) { IPCOM_LOG1(ERR, "DHCPS(%d): No servers specified or lacking ';' in domain-name-servers option", tok->curline); return -1; } example_do_command("dhcps option add %s 5 %s", context, value); ipcom_free(value); } else if (ipcom_strcasecmp(option, "domain-name") == 0) { /* A domain name */ char *value = example_get_token(tok, IP_FALSE); if (!example_check_token(tok, ";", IP_FALSE)) { IPCOM_LOG1(ERR, "DHCPS(%d): No domain name specified or lacking ';' in domain-name option", tok->curline); return -1; } example_do_command("dhcps option add %s 15 %s", context, value); } else if (ipcom_strcasecmp(option, "routers") == 0) { /* One or many routers */ char *value = example_concatenate_tokens(tok); if (value == IP_NULL) { IPCOM_LOG1(ERR, "DHCPS(%d): No servers specified or lacking ';' in routers option", tok->curline); return -1; } example_do_command("dhcps option add %s 3 %s", context, value); ipcom_free(value); } else { /* Unknown or unhandled option */ IPCOM_LOG2(WARNING, "DHCPS(%d): Unknown option %s", tok->curline, option); return -1; } return 0; }