int main(int argc, char* argv[]) { int rc, idx; uint32_t addr, netmask, netaddr; struct sockaddr_in inaddr; char **aliases=NULL; if (0 > (rc = opal_init(&argc, &argv))) { fprintf(stderr, "orte_interface: couldn't init opal - error code %d\n", rc); return rc; } if (2 == argc) { rc = opal_iftupletoaddr(argv[1], &netaddr, &netmask); fprintf(stderr, "netaddr %03d.%03d.%03d.%03d netmask %03d.%03d.%03d.%03d rc %d\n", OPAL_IF_FORMAT_ADDR(netaddr), OPAL_IF_FORMAT_ADDR(netmask), rc); /* search for a matching interface - take the first one within the returned scope */ idx = opal_ifbegin(); while (0 < idx) { /* ignore the loopback interface */ if (opal_ifisloopback(idx)) { fprintf(stderr, "LOOPBACK IGNORED\n"); idx = opal_ifnext(idx); continue; } if (0 != (rc = opal_ifindextoaddr(idx, (struct sockaddr*)&inaddr, sizeof(inaddr)))) { break; } addr = ntohl(inaddr.sin_addr.s_addr); fprintf(stderr, "checking netaddr %03d.%03d.%03d.%03d addr %03d.%03d.%03d.%03d netmask %03d.%03d.%03d.%03d rc %d\n", OPAL_IF_FORMAT_ADDR(netaddr), OPAL_IF_FORMAT_ADDR(addr), OPAL_IF_FORMAT_ADDR(netmask), rc); if (netaddr == (addr & netmask)) { fprintf(stderr, "MATCH FOUND\n"); } idx = opal_ifnext(idx); } } /* check the aliases */ opal_ifgetaliases(&aliases); idx = 0; while (NULL != aliases[idx]) { fprintf(stderr, "alias: %s\n", aliases[idx]); idx++; } opal_finalize(); return 0; }
/* Determine if an interface matches any entry in the given list, taking * into account that the list entries could be given as named interfaces, * IP addrs, or subnet+mask */ int opal_ifmatches(int kidx, char **nets) { bool named_if; int i, rc; size_t j; int kindex; struct sockaddr_in inaddr; uint32_t addr, netaddr, netmask; /* get the address info for the given network in case we need it */ if (OPAL_SUCCESS != (rc = opal_ifkindextoaddr(kidx, (struct sockaddr*)&inaddr, sizeof(inaddr)))) { return rc; } addr = ntohl(inaddr.sin_addr.s_addr); for (i=0; NULL != nets[i]; i++) { /* if the specified interface contains letters in it, then it * was given as an interface name and not an IP tuple */ named_if = false; for (j=0; j < strlen(nets[i]); j++) { if (isalpha(nets[i][j]) && '.' != nets[i][j]) { named_if = true; break; } } if (named_if) { if (0 > (kindex = opal_ifnametokindex(nets[i]))) { continue; } if (kindex == kidx) { return OPAL_SUCCESS; } } else { if (OPAL_SUCCESS != (rc = opal_iftupletoaddr(nets[i], &netaddr, &netmask))) { opal_show_help("help-opal-util.txt", "invalid-net-mask", true, nets[i]); return rc; } if (netaddr == (addr & netmask)) { return OPAL_SUCCESS; } } } /* get here if not found */ return OPAL_ERR_NOT_FOUND; }
static int open_channel(orte_rmcast_channel_t channel, char *name, char *network, int port, char *interface, uint8_t direction) { opal_list_item_t *item; rmcast_base_channel_t *nchan, *chan; uint32_t netaddr=0, netmask=0, intr=0; int rc; unsigned int i, n, start, end, range; bool port_assigned; OPAL_OUTPUT_VERBOSE((2, orte_rmcast_base.rmcast_output, "%s opening channel %d for %s", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME), channel, name)); /* parse the network, if provided */ if (NULL != network) { if (ORTE_SUCCESS != (rc = opal_iftupletoaddr(network, &netaddr, &netmask))) { orte_show_help("help-rmcast-base.txt", "invalid-net-mask", true, network, ORTE_ERROR_NAME(rc)); return ORTE_ERR_SILENT; } } /* parse the interface, if provided */ if (NULL != interface) { if (ORTE_SUCCESS != (rc = opal_iftupletoaddr(interface, &intr, NULL))) { orte_show_help("help-rmcast-base.txt", "invalid-net-mask", true, interface, ORTE_ERROR_NAME(rc)); return ORTE_ERR_SILENT; } } /* see if this name has already been assigned a channel on the specified network */ OPAL_OUTPUT_VERBOSE((7, orte_rmcast_base.rmcast_output, "%s open_channel: searching for %s:%d", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME), name, channel)); chan = NULL; ORTE_ACQUIRE_THREAD(&orte_rmcast_base.main_ctl); for (item = opal_list_get_first(&orte_rmcast_base.channels); item != opal_list_get_end(&orte_rmcast_base.channels); item = opal_list_get_next(item)) { nchan = (rmcast_base_channel_t*)item; OPAL_OUTPUT_VERBOSE((7, orte_rmcast_base.rmcast_output, "%s open_channel: channel %s:%d", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME), nchan->name, channel)); if (nchan->channel == channel || 0 == strcasecmp(nchan->name, name)) { chan = nchan; break; } } if (NULL != chan) { /* already exists - check that the requested * sockets are setup */ OPAL_OUTPUT_VERBOSE((2, orte_rmcast_base.rmcast_output, "%s rmcast:udp using existing channel %s:%d network %03d.%03d.%03d.%03d port %d", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME), chan->name, chan->channel, OPAL_IF_FORMAT_ADDR(chan->network), (int)chan->port)); if (ORTE_SUCCESS != (rc = setup_channel(chan, direction))) { ORTE_ERROR_LOG(rc); ORTE_RELEASE_THREAD(&orte_rmcast_base.main_ctl); return rc; } ORTE_RELEASE_THREAD(&orte_rmcast_base.main_ctl); return ORTE_SUCCESS; } /* we didn't find an existing match, so create a new channel */ OPAL_OUTPUT_VERBOSE((2, orte_rmcast_base.rmcast_output, "%s creating new channel %s for %s", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME), orte_rmcast_base_print_channel(channel), name)); chan = OBJ_NEW(rmcast_base_channel_t); chan->name = strdup(name); chan->channel = channel; /* if we were not given a network, use the default */ if (NULL == network) { chan->network = orte_rmcast_base.xmit_network; } else { chan->network = netaddr; } /* if we were not given an interface, use the default */ if (NULL == interface) { chan->interface = orte_rmcast_base.interface; } else { chan->interface = intr; } /* if we were not given a port, use a default one */ if (port < 0) { /* cycle thru the port ranges until we find the * port corresponding to this channel number */ n=0; port_assigned = false; for (i=0; NULL != orte_rmcast_base.ports.start[i]; i++) { /* how many ports are in this range? */ start = strtol(orte_rmcast_base.ports.start[i], NULL, 10); end = strtol(orte_rmcast_base.ports.end[i], NULL, 10); range = end - start + 1; if (chan->channel < (n + range)) { /* take the corresponding port */ chan->port = start + (chan->channel - n); port_assigned = true; break; } n += range; } if (!port_assigned) { opal_output(0, "%s CANNOT ASSIGN PORT TO CHANNEL %s", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME), orte_rmcast_base_print_channel(chan->channel)); return ORTE_ERROR; } } else { chan->port = port; } opal_list_append(&orte_rmcast_base.channels, &chan->item); ORTE_RELEASE_THREAD(&orte_rmcast_base.main_ctl); /* if this is my input, set that value */ if (ORTE_RMCAST_MY_INPUT & direction) { orte_rmcast_base.my_input_channel = chan; } /* if this is my output, set that value */ if (ORTE_RMCAST_MY_OUTPUT & direction) { orte_rmcast_base.my_output_channel = chan; } OPAL_OUTPUT_VERBOSE((2, orte_rmcast_base.rmcast_output, "%s rmcast:udp opening new channel %s:%s network %03d.%03d.%03d.%03d port %d for%s%s", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME), chan->name, orte_rmcast_base_print_channel(chan->channel), OPAL_IF_FORMAT_ADDR(chan->network), (int)chan->port, (ORTE_RMCAST_RECV & direction) ? " RECV" : " ", (ORTE_RMCAST_XMIT & direction) ? " XMIT" : " ")); if (ORTE_SUCCESS != (rc = setup_channel(chan, direction))) { ORTE_ERROR_LOG(rc); return rc; } return ORTE_SUCCESS; }
int main(int argc, char* argv[]) { int rc, idx; uint32_t addr, netmask, netaddr; struct sockaddr_in inaddr, *paddr; char **aliases=NULL; opal_if_t *intf; if (0 > (rc = opal_init(&argc, &argv))) { fprintf(stderr, "opal_interface: couldn't init opal - error code %d\n", rc); return rc; } if (OPAL_SUCCESS != mca_base_framework_open(&opal_if_base_framework, 0)) { fprintf(stderr, "opal_interface: couldn't get interfaces\n"); return 1; } for (intf = (opal_if_t*)opal_list_get_first(&opal_if_list); intf != (opal_if_t*)opal_list_get_end(&opal_if_list); intf = (opal_if_t*)opal_list_get_next(intf)) { paddr = (struct sockaddr_in*) &intf->if_addr; fprintf(stderr, "intf: %s AF: %s indx: %d kindx: %d\n", intf->if_name, (AF_INET == paddr->sin_family) ? "v4" : "v6", intf->if_index, intf->if_kernel_index); } if (2 == argc) { rc = opal_iftupletoaddr(argv[1], &netaddr, &netmask); fprintf(stderr, "netaddr %03d.%03d.%03d.%03d netmask %03d.%03d.%03d.%03d rc %d\n", OPAL_IF_FORMAT_ADDR(netaddr), OPAL_IF_FORMAT_ADDR(netmask), rc); /* search for a matching interface - take the first one within the returned scope */ idx = opal_ifbegin(); while (0 < idx) { /* ignore the loopback interface */ if (opal_ifisloopback(idx)) { fprintf(stderr, "LOOPBACK IGNORED\n"); idx = opal_ifnext(idx); continue; } if (0 != (rc = opal_ifindextoaddr(idx, (struct sockaddr*)&inaddr, sizeof(inaddr)))) { break; } addr = ntohl(inaddr.sin_addr.s_addr); fprintf(stderr, "checking netaddr %03d.%03d.%03d.%03d addr %03d.%03d.%03d.%03d netmask %03d.%03d.%03d.%03d rc %d\n", OPAL_IF_FORMAT_ADDR(netaddr), OPAL_IF_FORMAT_ADDR(addr), OPAL_IF_FORMAT_ADDR(netmask), rc); if (netaddr == (addr & netmask)) { fprintf(stderr, "MATCH FOUND\n"); } idx = opal_ifnext(idx); } } /* check the aliases */ opal_ifgetaliases(&aliases); idx = 0; while (NULL != aliases[idx]) { fprintf(stderr, "alias: %s\n", aliases[idx]); idx++; } opal_finalize(); return 0; }