GenericRoutingTable::ROUTING_TABLE GenericRoutingTable::detail::getRoutingTableOmnet (DistackOmnetModule* module) { ROUTING_TABLE ret; // // find the routing omnet module // this is similar to RoutingTableAccess, but there // the simulation context would be wrong // cModule* m = findModuleSomewhereUp ("routingTable", (cModule*)module); IRoutingTable* omnetTable = check_and_cast<IRoutingTable*> (m); if (omnetTable == NULL) return ret; for (int i=0; i<omnetTable->getNumRoutes(); i++) { const IPRoute* omnetEntry = omnetTable->getRoute (i); if (omnetEntry == NULL) continue; IP_ADDR addr = IP_ADDR (omnetEntry->getHost()); IP_ADDR mask = IP_ADDR (omnetEntry->getNetmask()); ret.push_back (ROUTING_ENTRY (addr, mask)); } return ret; }
/* * Set the state of the connection to be LISTEN, which means that it * is able to accept incoming connections. The protocol control block * is reallocated in order to consume less memory. Setting the * connection to LISTEN is an irreversible process. */ tcp_socket_t *tcp_listen (ip_t *ip, unsigned char *ipaddr, unsigned short port) { tcp_socket_t *s, *cs; if (! ipaddr) { ipaddr = (unsigned char*) "\0\0\0\0"; } s = mem_alloc (ip->pool, sizeof (tcp_socket_t)); if (s == 0) { return 0; } s->ip = ip; /* Bind the connection to a local portnumber and IP address. */ mutex_lock (&ip->lock); if (port == 0) { port = tcp_new_port (ip); } tcp_debug ("tcp_listen: port %u\n", port); /* Check if the address already is in use. */ for (cs = ip->tcp_listen_sockets; cs != 0; cs = cs->next) { if (cs->local_port == port) { if (memcmp (cs->local_ip, IP_ADDR(0), 4) == 0 || memcmp (ipaddr, IP_ADDR(0), 4) == 0 || memcmp (cs->local_ip, ipaddr, 4) == 0) { mutex_unlock (&ip->lock); mem_free (s); return 0; } } } for (cs = ip->tcp_sockets; cs != 0; cs = cs->next) { if (cs->local_port == port) { if (memcmp (cs->local_ip, IP_ADDR(0), 4) == 0 || memcmp (ipaddr, IP_ADDR(0), 4) == 0 || memcmp (cs->local_ip, ipaddr, 4) == 0) { mutex_unlock (&ip->lock); mem_free (s); return 0; } } } if (memcmp (ipaddr, IP_ADDR(0), 4) != 0) { memcpy (s->local_ip, ipaddr, 4); } s->local_port = port; s->state = LISTEN; tcp_list_add (&ip->tcp_listen_sockets, s); mutex_unlock (&ip->lock); return s; }
GenericRoutingTable::ROUTING_TABLE GenericRoutingTable::detail::getRoutingTableFile (string filename, ROUTING_TEXT_FORMAT format) { ROUTING_TABLE ret; // // read the contents of the file // ifstream file (filename.c_str()); assert (file.is_open()); string content = ""; while (! file.eof ()) { char item; file.read (&item, 1); if (! file.eof()) content += item; } // // get the individual lines of the routing table // typedef vector<string> STRING_VECTOR; typedef STRING_VECTOR::iterator STRING_VECTOR_ITERATOR; STRING_VECTOR lines; boost::split (lines, content, boost::is_any_of ("\n")); STRING_VECTOR_ITERATOR i = lines.begin (); STRING_VECTOR_ITERATOR iend = lines.end (); for ( ; i != iend; i++) { string line = *i; boost::trim (line); IP_ADDR addr, mask; // // parse the content of each line // if (format == ROUTING_TEXT_FORMAT_SIMPLE) { STRING_VECTOR items; boost::split (items, line, boost::is_any_of ("/")); if (items.size() != 2) continue; addr.fromString (items[0]); int bits = Helper::stoi (items[1]); if (! (bits >= 0 && bits <= 32)) continue; IP_ADDR tempMask; for (int i=0; i<bits; i++) { unsigned char byte = i / 8; tempMask.x[byte] |= (0x1 << (8 - 1 - (i % 8))); } mask = tempMask; } else if (format == ROUTING_TEXT_FORMAT_DETAILED) { typedef enum _PROC_NET_ROUTE_FIELDS { PROC_NET_ROUTE_FIELDS_ADDR = 1, PROC_NET_ROUTE_FIELDS_MASK = 7, } PROC_NET_ROUTE_FIELDS; STRING_VECTOR items; boost::split (items, line, boost::is_any_of ("\t")); if (items.size() < (PROC_NET_ROUTE_FIELDS_MASK+1)) continue; boost::trim (items[PROC_NET_ROUTE_FIELDS_ADDR]); boost::trim (items[PROC_NET_ROUTE_FIELDS_MASK]); if (! isValidDetailedAddress (items[PROC_NET_ROUTE_FIELDS_ADDR])) continue; if (! isValidDetailedAddress (items[PROC_NET_ROUTE_FIELDS_MASK])) continue; addr = IP_ADDR (Helper::hstoui(items[PROC_NET_ROUTE_FIELDS_ADDR])); mask = IP_ADDR (Helper::hstoui(items[PROC_NET_ROUTE_FIELDS_MASK])); // If default route 0.0.0.0/0 shouldn't be regarded uncomment the following 2 lines //if (addr == IP_ADDR (0)) // continue; } else assert (false); ret.push_back (ROUTING_ENTRY (addr, mask)); } // for ( ; i != iend; i++) return ret; }
int getFileFromNfs(char * url, char * dest, struct loaderData_s * loaderData) { char ret[47]; char * host = NULL, *path = NULL, * file = NULL, * opts = NULL; int failed = 0; struct networkDeviceConfig netCfg; ip_addr_t *tip, *u; if (kickstartNetworkUp(loaderData, &netCfg)) { logMessage(ERROR, "unable to bring up network"); return 1; } /* if they just did 'linux ks', they want us to figure it out from * the dhcp/bootp information */ if (url == NULL) { if (!(netCfg.dev.set & PUMP_INTFINFO_HAS_NEXTSERVER)) { logMessage(ERROR, "no bootserver was found"); return 1; } tip = &(netCfg.dev.nextServer); if (!(netCfg.dev.set & PUMP_INTFINFO_HAS_BOOTFILE)) { inet_ntop(tip->sa_family, IP_ADDR(tip), ret, IP_STRLEN(tip)); url = sdupprintf("%s:%s", ret, "/kickstart/"); logMessage(ERROR, "bootp: no bootfile received"); } else { inet_ntop(tip->sa_family, IP_ADDR(tip), ret, IP_STRLEN(tip)); url = sdupprintf("%s:%s", ret, netCfg.dev.bootFile); logMessage(INFO, "bootp: bootfile is %s", netCfg.dev.bootFile); } } /* get the IP of the target system */ if (FL_HAVE_CMSCONF(flags)) { if (loaderData->ip == NULL) { logMessage(ERROR, "getFileFromNfs: no client IP information"); return 1; } else { sprintf(ret, "%s", loaderData->ip); } } else { tip = &(netCfg.dev.ipv4); u = &(netCfg.dev.ip); if ((inet_ntop(tip->sa_family, IP_ADDR(tip), ret, IP_STRLEN(tip)) == NULL) && (inet_ntop(u->sa_family, IP_ADDR(u), ret, IP_STRLEN(u)) == NULL)) { logMessage(ERROR, "getFileFromNfs: no client IP information"); return 1; } } logMessage(INFO, "url is %s", url); getHostandPath(url, &host, &path, ret); opts = strchr(host, ':'); if (opts && (strlen(opts) > 1)) { char * c = opts; opts = host; host = c + 1; *c = '\0'; } else { opts = NULL; } /* nfs has to be a little bit different... split off the last part as * the file and then concatenate host + dir path */ file = strrchr(path, '/'); if (!file) { file = path; } else { *file++ ='\0'; host = sdupprintf("%s/%s", host, path); } logMessage(INFO, "file location: nfs://%s/%s", host, file); if (!doPwMount(host, "/tmp/mnt", "nfs", IMOUNT_RDONLY, opts)) { char * buf; buf = alloca(strlen(file) + 10); sprintf(buf, "/tmp/mnt/%s", file); if (copyFile(buf, dest)) { logMessage(ERROR, "failed to copy file to %s", dest); failed = 1; } } else { logMessage(ERROR, "failed to mount nfs source"); failed = 1; } umount("/tmp/mnt"); unlink("/tmp/mnt"); return failed; }
#include <stdio.h> #include <loadcore.h> #include "lanman.h" #include "arp.h" #include "tcp.h" #include "smap.h" #include "udptty.h" #define MODNAME "lanman" IRX_ID(MODNAME, 1, 1); static g_param_t g_param = { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, // needed for broadcast { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, IP_ADDR(192, 168, 0, 2), IP_ADDR(192, 168, 0, 10), IP_PORT(445), IP_PORT(0x4712) }; //------------------------------------------------------------------------- int _start(int argc, char *argv[]) { // Init SMAP if (smap_init(&g_param.eth_addr_src[0]) != 0) return MODULE_NO_RESIDENT_END; // Does ARP request and wait reply to get server MAC address arp_init(&g_param);
int getFileFromUrl(char * url, char * dest, struct loaderData_s * loaderData) { char ret[47]; struct iurlinfo ui; enum urlprotocol_t proto = !strncmp(url, "ftp://", 6) ? URL_METHOD_FTP : URL_METHOD_HTTP; char * host = NULL, * file = NULL, * chptr = NULL; char * user = NULL, * password = NULL; int fd, rc; struct networkDeviceConfig netCfg; char * ehdrs = NULL; ip_addr_t *tip; #ifdef ROCKS char *drivername; #endif #ifdef ROCKS /* * Call non-interactive, exhaustive NetworkUp() if we are * a cluster appliance. */ if (!strlen(url)) { logMessage(INFO, "ROCKS:getFileFromUrl:calling rocksNetworkUp"); rc = rocksNetworkUp(loaderData, &netCfg); } else { logMessage(INFO, "ROCKS:getFileFromUrl:calling kickstartNetworkUp"); rc = kickstartNetworkUp(loaderData, &netCfg); } if (rc) return 1; fd = 0; /* * this will be used when starting up mini_httpd() * * Get the nextServer from PUMP if we DHCP, otherwise it * better be on the command line. */ if ( netCfg.dev.set & PUMP_INTFINFO_HAS_BOOTFILE ) { tip = &(netCfg.dev.nextServer); inet_ntop(tip->sa_family, IP_ADDR(tip), ret, IP_STRLEN(tip)); if (strlen(ret) > 0) { loaderData->nextServer = strdup(ret); } else { loaderData->nextServer = NULL; } } /* * If no nextServer use the gateway. */ if ( !loaderData->nextServer ) { loaderData->nextServer = strdup(loaderData->gateway); } logMessage(INFO, "%s: nextServer %s", "ROCKS:getFileFromUrl", loaderData->nextServer); #else if (kickstartNetworkUp(loaderData, &netCfg)) { logMessage(ERROR, "unable to bring up network"); return 1; } #endif /* ROCKS */ memset(&ui, 0, sizeof(ui)); ui.protocol = proto; #ifdef ROCKS { struct sockaddr_in *sin; int string_size; int ncpus; char np[16]; char *arch; char *base; #if defined(__i386__) arch = "i386"; #elif defined(__ia64__) arch = "ia64"; #elif defined(__x86_64__) arch = "x86_64"; #endif if (!strlen(url)) { base = strdup("install/sbin/kickstart.cgi"); host = strdup(loaderData->nextServer); } else { char *p, *q; base = NULL; host = NULL; p = strstr(url, "//"); if (p != NULL) { p += 2; /* * 'base' is the file name */ base = strchr(p, '/'); if (base != NULL) { base += 1; } /* * now get the host portion of the URL */ q = strchr(p, '/'); if (q != NULL) { *q = '\0'; host = strdup(p); } } if (!base || !host) { logMessage(ERROR, "kickstartFromUrl:url (%s) not well formed.\n", url); return(1); } } /* We always retrieve our kickstart file via HTTPS, * however the official install method (for *.img and rpms) * is still HTTP. */ ui.protocol = URL_METHOD_HTTPS; winStatus(40, 3, _("Secure Kickstart"), _("Looking for Kickstart keys...")); getCert(loaderData); newtPopWindow(); /* seed random number generator with our IP: unique for our purposes. * Used for nack backoff. */ tip = &(netCfg.dev.nextServer); sin = (struct sockaddr_in *)IP_ADDR(tip); if (sin == NULL) { srand(time(NULL)); } else { srand((unsigned int)sin->sin_addr.s_addr); } ncpus = num_cpus(); sprintf(np, "%d", ncpus); string_size = strlen(base) + strlen("?arch=") + strlen(arch) + strlen("&np=") + strlen(np) + 1; if ((file = alloca(string_size)) == NULL) { logMessage(ERROR, "kickstartFromUrl:alloca failed\n"); return(1); } memset(file, 0, string_size); sprintf(file, "/%s?arch=%s&np=%s", base, arch, np); } logMessage(INFO, "ks location: https://%s%s", host, file); #else tip = &(netCfg.dev.ip); inet_ntop(tip->sa_family, IP_ADDR(tip), ret, IP_STRLEN(tip)); getHostPathandLogin((proto == URL_METHOD_FTP ? url + 6 : url + 7), &host, &file, &user, &password, ret); logMessage(INFO, "file location: %s://%s/%s", (proto == URL_METHOD_FTP ? "ftp" : "http"), host, file); #endif /* ROCKS */ chptr = strchr(host, '/'); if (chptr == NULL) { ui.address = strdup(host); ui.prefix = strdup("/"); } else { *chptr = '\0'; ui.address = strdup(host); host = chptr; *host = '/'; ui.prefix = strdup(host); } if (user && strlen(user)) { ui.login = strdup(user); if (password && strlen(password)) ui.password = strdup(password); } if (proto == URL_METHOD_HTTP) { ehdrs = (char *) malloc(24+strlen(VERSION)); sprintf(ehdrs, "User-Agent: anaconda/%s\r\n", VERSION); } if (proto == URL_METHOD_HTTP && FL_KICKSTART_SEND_MAC(flags)) { /* find all ethernet devices and make a header entry for each one */ int i; unsigned int hdrlen; char *dev, *mac, tmpstr[128]; struct device ** devices; hdrlen = 0; devices = probeDevices(CLASS_NETWORK, BUS_UNSPEC, PROBE_LOADED); for (i = 0; devices && devices[i]; i++) { dev = devices[i]->device; mac = nl_mac2str(dev); #ifdef ROCKS drivername = get_driver_name(dev); #endif if (mac) { #ifdef ROCKS /* A hint as to our primary interface. */ if (!strcmp(dev, loaderData->netDev)) { snprintf(tmpstr, sizeof(tmpstr), "X-RHN-Provisioning-MAC-%d: %s %s %s ks\r\n", i, dev, mac, drivername); } else { snprintf(tmpstr, sizeof(tmpstr), "X-RHN-Provisioning-MAC-%d: %s %s %s\r\n", i, dev, mac, drivername); } #else snprintf(tmpstr, sizeof(tmpstr), "X-RHN-Provisioning-MAC-%d: %s %s\r\n", i, dev, mac); #endif /* ROCKS */ #ifdef ROCKS free(drivername); #endif free(mac); if (!ehdrs) { hdrlen = 128; ehdrs = (char *) malloc(hdrlen); *ehdrs = '\0'; } else if ( strlen(tmpstr) + strlen(ehdrs) + 2 > hdrlen) { hdrlen += 128; ehdrs = (char *) realloc(ehdrs, hdrlen); } strcat(ehdrs, tmpstr); } } } #ifdef ROCKS { /* Retrieve the kickstart file via HTTPS */ BIO *sbio; sbio = urlinstStartSSLTransfer(&ui, file, ehdrs, 1, flags, loaderData->nextServer); if (!sbio) { logMessage(ERROR, "failed to retrieve https://%s/%s", ui.address, file); return 1; } rc = copyFileSSL(sbio, dest); if (rc) { unlink (dest); logMessage(ERROR, "failed to copy file to %s", dest); return 1; } urlinstFinishSSLTransfer(sbio); if (haveCertificate()) umount("/mnt/rocks-disk"); } #else fd = urlinstStartTransfer(&ui, file, ehdrs); if (fd < 0) { logMessage(ERROR, "failed to retrieve http://%s/%s/%s", ui.address, ui.prefix, file); if (ehdrs) free(ehdrs); return 1; } rc = copyFileFd(fd, dest); if (rc) { unlink (dest); logMessage(ERROR, "failed to copy file to %s", dest); if (ehdrs) free(ehdrs); return 1; } urlinstFinishTransfer(&ui, fd); #endif /* ROCKS */ if (ehdrs) free(ehdrs); return 0; }