int main(int argc, char* argv[]) { FILE *fd = NULL; char c, buf[512], buf1[64], buf2[64]; while((c = getopt(argc, argv, "f:")) != -1) { switch(c) { case 'f': fd = fopen(optarg, "rb"); if(fd == NULL) { printf("Unable to open file %s\n", optarg); return(-1); } break; } } if(fd == NULL) { help(); return(-1); } while(fread(buf, sizeof(FlowHashBucketCoreFields), 1, fd) == 1) { FlowHashBucketCoreFields *f = (FlowHashBucketCoreFields*)buf; printf("[proto=%d][vlan=%d][%s:%d <-> %s:%d][bytes=%u/%u][pkts=%u/%u]\n", f->key.k.ipKey.proto, f->key.vlanId, intoa(f->key.k.ipKey.src, buf1, sizeof(buf1)), f->key.k.ipKey.sport, intoa(f->key.k.ipKey.dst, buf2, sizeof(buf2)), f->key.k.ipKey.dport, f->flowCounters.bytesSent, f->flowCounters.bytesRcvd, f->flowCounters.pktSent, f->flowCounters.pktRcvd); } fclose(fd); return(0); }
static int print_report(netdissect_options *ndo, const u_char *bp, const u_char *ep, u_int len) { uint32_t mask, origin; u_int metric, done; u_int i, width; while (len > 0) { if (len < 3) { ND_PRINT(" [|]"); return (0); } ND_TCHECK_3(bp); mask = (uint32_t)0xff << 24 | GET_U_1(bp) << 16 | GET_U_1(bp + 1) << 8 | GET_U_1(bp + 2); width = 1; if (GET_U_1(bp)) width = 2; if (GET_U_1(bp + 1)) width = 3; if (GET_U_1(bp + 2)) width = 4; ND_PRINT("\n\tMask %s", intoa(htonl(mask))); bp += 3; len -= 3; do { if (bp + width + 1 > ep) { ND_PRINT(" [|]"); return (0); } if (len < width + 1) { ND_PRINT("\n\t [Truncated Report]"); return (0); } origin = 0; for (i = 0; i < width; ++i) { ND_TCHECK_1(bp); origin = origin << 8 | GET_U_1(bp); bp++; } for ( ; i < 4; ++i) origin <<= 8; ND_TCHECK_1(bp); metric = GET_U_1(bp); bp++; done = metric & 0x80; metric &= 0x7f; ND_PRINT("\n\t %s metric %u", intoa(htonl(origin)), metric); len -= width + 1; } while (!done); } return (0); trunc: return (-1); }
static int print_report(netdissect_options *ndo, register const u_char *bp, register const u_char *ep, register u_int len) { register u_int32_t mask, origin; register int metric, done; register u_int i, width; while (len > 0) { if (len < 3) { ND_PRINT((ndo, " [|]")); return (0); } ND_TCHECK2(bp[0], 3); mask = (u_int32_t)0xff << 24 | bp[0] << 16 | bp[1] << 8 | bp[2]; width = 1; if (bp[0]) width = 2; if (bp[1]) width = 3; if (bp[2]) width = 4; ND_PRINT((ndo, "\n\tMask %s", intoa(htonl(mask)))); bp += 3; len -= 3; do { if (bp + width + 1 > ep) { ND_PRINT((ndo, " [|]")); return (0); } if (len < width + 1) { ND_PRINT((ndo, "\n\t [Truncated Report]")); return (0); } origin = 0; for (i = 0; i < width; ++i) { ND_TCHECK(*bp); origin = origin << 8 | *bp++; } for ( ; i < 4; ++i) origin <<= 8; ND_TCHECK(*bp); metric = *bp++; done = metric & 0x80; metric &= 0x7f; ND_PRINT((ndo, "\n\t %s metric %d", intoa(htonl(origin)), metric)); len -= width + 1; } while (!done); } return (0); trunc: return (-1); }
static void print_header( const char * msg, const struct n2n_packet_header * hdr ) { ipstr_t buf; ipstr_t buf2; traceEvent(TRACE_INFO, "%s hdr: public_ip=(%d)%s:%d, private_ip=(%d)%s:%d", msg, hdr->public_ip.family, intoa(ntohl(hdr->public_ip.addr_type.v4_addr), buf, sizeof(buf)), ntohs(hdr->public_ip.port), hdr->private_ip.family, intoa(ntohl(hdr->private_ip.addr_type.v4_addr), buf2, sizeof(buf2)), ntohs(hdr->private_ip.port) ); }
char * InetNtoa(u_int32_t addr) { const char *s = intoa(addr); char *p = (char *)malloc(strlen(s) + 1); strcpy(p, s); return p; }
int rtipChannel::openrsock(u_int32_t addr, u_short port, int& lcid_) const { /*XXX bruce says this is host order */ port = ntohs(port); /* * RCAP-related initialization...register application on a port. */ int rc = RcapRegister(port, 1); if (rc != RCAP_SUCCESS) { fprintf(stderr, "vic: RcapRegister failed\n"); exit(1); } parmblock pb; rcapTraffic traffic; rcapRequirements requirements; rcapAddress src; rcapUserControl control; pb.traffic = &traffic; pb.requirements = &requirements; pb.control = &control; /* * Get next channel establishment request. If there are not * any, we block in the library call until we get one. */ fprintf(stderr, "vic: waiting for rcap receiver connect to %s\n", intoa(addr)); /*XXX ignore return values? */ u_short lcid; (void)RcapReceiveRequest(port, &pb, &src, &lcid); (void)RcapEstablishReturn(lcid, RCAP_SUCCESS, 0, &control); lcid_ = lcid; /* * Create a new data socket and associate it with the * newly-created channel. Also do a bit of bookkeeping * so we knew what sockets to look for. */ int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_RMTP); if (s < 0) { perror("socket"); return (-1); } if (setsockopt(s, IPPROTO_RTIP, RTIP_DESTINATION_ASSOC, (char *)&lcid, sizeof(lcid)) < 0) { perror("setsockopt"); } /*XXX*/ int rbuf = 40 * 1024; if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&rbuf, sizeof(rbuf)) < 0) { perror("vic: SO_RCVBUF"); exit(1); } fprintf(stderr, "vic: rcap: receiver connection established\n"); return (s); }
int net_mountroot(void) { #ifdef DEBUG printf("net_mountroot\n"); #endif /* * Get info for NFS boot: our IP address, our hostname, * server IP address, and our root path on the server. * There are two ways to do this: The old, Sun way, * and the more modern, BOOTP way. (RFC951, RFC1048) */ #ifdef SUN_BOOTPARAMS /* Get boot info using RARP and Sun bootparams. */ /* Get our IP address. (rarp.c) */ if (rarp_getipaddress(netdev_sock) == -1) return errno; printf("boot: client IP address: %s\n", inet_ntoa(myip)); /* Get our hostname, server IP address. */ if (bp_whoami(netdev_sock)) return (errno); printf("boot: client name: %s\n", hostname); /* Get the root pathname. */ if (bp_getfile(netdev_sock, "root", &rootip, rootpath)) return (errno); #else /* Get boot info using BOOTP way. (RFC951, RFC1048) */ bootp(netdev_sock); printf("Using IP address: %s\n", inet_ntoa(myip)); printf("myip: %s (%s)", hostname, inet_ntoa(myip)); if (gateip) printf(", gateip: %s", inet_ntoa(gateip)); if (netmask) printf(", netmask: %s", intoa(netmask)); printf("\n"); #endif printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); /* Get the NFS file handle (mount). */ if (nfs_mount(netdev_sock, rootip, rootpath) != 0) return (errno); return 0; }
void process_fddi(register u_char *u, register const struct pcap_pkthdr *h, register const u_char *p) { register struct fddi_header *fh; register struct ether_arp *ea; register u_char *sea, *sha; register time_t t; u_int32_t sia; fh = (struct fddi_header *)p; ea = (struct ether_arp *)(fh + 1); if (!swapped) { bit_reverse(fh->src, 6); bit_reverse(fh->dst, 6); } if (!sanity_fddi(fh, ea, h->caplen)) return; /* Source MAC hardware ethernet address */ sea = (u_char *)fh->src; /* Source ARP ethernet address */ sha = (u_char *)SHA(ea); /* Source ARP ip address */ BCOPY(SPA(ea), &sia, 4); /* Watch for bogons */ if (isbogon(sia)) { dosyslog(LOG_INFO, "bogon", sia, sea, sha); return; } /* Watch for ethernet broadcast */ if (MEMCMP(sea, zero, 6) == 0 || MEMCMP(sea, allones, 6) == 0 || MEMCMP(sha, zero, 6) == 0 || MEMCMP(sha, allones, 6) == 0) { dosyslog(LOG_INFO, "ethernet broadcast", sia, sea, sha); return; } /* Double check ethernet addresses */ if (MEMCMP(sea, sha, 6) != 0) { dosyslog(LOG_INFO, "ethernet mismatch", sia, sea, sha); return; } /* Got a live one */ t = h->ts.tv_sec; can_checkpoint = 0; if (!ent_add(sia, sea, t, NULL)) syslog(LOG_ERR, "ent_add(%s, %s, %ld) failed", intoa(sia), e2str(sea), t); can_checkpoint = 1; }
int test(int n, char *buf, size_t bufsize, int value, int exp1, const char *exp2) { int rv; rv = (intoa(buf, bufsize, value) != exp1); rv += (strcmp(buf, exp2) != 0); printf("Test %d: \texpected \"%s\"\n", n, buf); printf(" \tgot \"%s\"\n", exp2); return rv; }
void dumpone(register u_int32_t a, register u_char *e, register time_t t, register char *h) { (void)fprintf(dumpf, "%s\t%s", e2str(e), intoa(a)); if (t != 0 || h != NULL) (void)fprintf(dumpf, "\t%u", (u_int32_t)t); if (h != NULL && *h != '\0') (void)fprintf(dumpf, "\t%s", h); (void)putc('\n', dumpf); }
/* syslog() helper routine */ void dosyslog(register int p, register char *s, register u_int32_t a, register u_char *ea, register u_char *ha) { char xbuf[64]; /* No report until we're initialized */ if (initializing) return; /* Display both ethernet addresses if they don't match */ (void)strcpy(xbuf, e2str(ea)); if (ha != NULL && MEMCMP(ea, ha, 6) != 0) { (void)strcat(xbuf, " ("); (void)strcat(xbuf, e2str(ha)); (void)strcat(xbuf, ")"); } if (debug) fprintf(stderr, "%s: %s %s %s\n", prog, s, intoa(a), xbuf); else syslog(p, "%s %s %s", s, intoa(a), xbuf); }
static void opt_subcmd_print(char *opt) { printf("boot options:\n" "magic:\t\t%s\n" "pathname:\t`%s'\n" "bootp:\t\t%s\n", bootopts.b_magic == BOOTOPT_MAGIC ? "ok" : "bad", bootopts.b_pathname, bootopts.b_flags & B_F_USE_BOOTP ? "yes" : "no"); printf("remote IP:\t%s\n", inet_ntoa(bootopts.b_remote_ip)); printf("local IP:\t%s\n", inet_ntoa(bootopts.b_local_ip)); printf("netmask:\t%s\n", intoa(bootopts.b_netmask)); printf("gateway IP:\t%s\n", inet_ntoa(bootopts.b_gate_ip)); }
/* Return the cannonical name of the host */ char * gethname(u_int32_t a) { register int32_t options; register struct hostent *hp; options = _res.options; _res.options |= RES_AAONLY; _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); hp = gethostbyaddr((char *)&a, sizeof(a), AF_INET); _res.options = options; if (hp == NULL) return (intoa(a)); return (hp->h_name); }
int net_mountroot_bootp(void) { bootp(netdev_sock); if (myip.s_addr == 0) return(ENOENT); printf("Using BOOTP protocol: "); printf("ip address: %s", inet_ntoa(myip)); if (hostname[0]) printf(", hostname: %s", hostname); if (netmask) printf(", netmask: %s", intoa(netmask)); if (gateip.s_addr) printf(", gateway: %s", inet_ntoa(gateip)); printf("\n"); return (0); }
int net_mountroot() { #ifdef DEBUG printf("net_mountroot\n"); #endif /* * Get info for NFS boot: our IP address, out hostname, * server IP address, and our root path on the server. * We use BOOTP (RFC951, RFC1532) exclusively as mandated * by PowerPC Reference Platform Specification I.4.2 */ bootp(netdev_sock); if (myip.s_addr == 0) return ETIMEDOUT; printf("Using IP address: %s\n", inet_ntoa(myip)); #ifdef DEBUG printf("myip: %s (%s)", hostname, inet_ntoa(myip)); if (gateip.s_addr) printf(", gateip: %s", inet_ntoa(gateip)); if (netmask) printf(", netmask: %s", intoa(netmask)); printf("\n"); #endif printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); /* * Get the NFS file handle (mount). */ if (nfs_mount(netdev_sock, rootip, rootpath) < 0) return errno; return 0; }
static int net_getparams(int sock) { char buf[MAXHOSTNAMELEN]; n_long rootaddr, smask; #ifdef SUPPORT_BOOTP /* * Try to get boot info using BOOTP. If we succeed, then * the server IP address, gateway, and root path will all * be initialized. If any remain uninitialized, we will * use RARP and RPC/bootparam (the Sun way) to get them. */ if (try_bootp) bootp(sock, BOOTP_NONE); if (myip.s_addr != 0) goto exit; #ifdef NETIF_DEBUG if (debug) printf("net_open: BOOTP failed, trying RARP/RPC...\n"); #endif #endif /* * Use RARP to get our IP address. This also sets our * netmask to the "natural" default for our address. */ if (rarp_getipaddress(sock)) { printf("net_open: RARP failed\n"); return (EIO); } printf("net_open: client addr: %s\n", inet_ntoa(myip)); /* Get our hostname, server IP address, gateway. */ if (bp_whoami(sock)) { printf("net_open: bootparam/whoami RPC failed\n"); return (EIO); } #ifdef NETIF_DEBUG if (debug) printf("net_open: client name: %s\n", hostname); #endif /* * Ignore the gateway from whoami (unreliable). * Use the "gateway" parameter instead. */ smask = 0; gateip.s_addr = 0; if (bp_getfile(sock, "gateway", &gateip, buf) == 0) { /* Got it! Parse the netmask. */ smask = ip_convertaddr(buf); } if (smask) { netmask = smask; #ifdef NETIF_DEBUG if (debug) printf("net_open: subnet mask: %s\n", intoa(netmask)); #endif } #ifdef NETIF_DEBUG if (gateip.s_addr && debug) printf("net_open: net gateway: %s\n", inet_ntoa(gateip)); #endif /* Get the root server and pathname. */ if (bp_getfile(sock, "root", &rootip, rootpath)) { printf("net_open: bootparam/getfile RPC failed\n"); return (EIO); } exit: if ((rootaddr = net_parse_rootpath()) != INADDR_NONE) rootip.s_addr = rootaddr; #ifdef NETIF_DEBUG if (debug) { printf("net_open: server addr: %s\n", inet_ntoa(rootip)); printf("net_open: server path: %s\n", rootpath); } #endif return (0); }
/* * Called by devopen after it sets f->f_dev to our devsw entry. * This opens the low-level device and sets f->f_devdata. * This is declared with variable arguments... */ static int net_open(struct open_file *f, ...) { char temp[FNAME_SIZE]; struct iodesc *d; va_list args; char *devname; /* Device part of file name (or NULL). */ int error = 0; va_start(args, f); devname = va_arg(args, char*); va_end(args); #ifdef NETIF_OPEN_CLOSE_ONCE /* Before opening another interface, close the previous one first. */ if (netdev_sock >= 0 && strcmp(devname, netdev_name) != 0) net_cleanup(); #endif /* On first open, do netif open, mount, etc. */ if (netdev_opens == 0) { /* Find network interface. */ if (netdev_sock < 0) { netdev_sock = netif_open(devname); if (netdev_sock < 0) { printf("net_open: netif_open() failed\n"); return (ENXIO); } netdev_name = strdup(devname); #ifdef NETIF_DEBUG if (debug) printf("net_open: netif_open() succeeded\n"); #endif } /* * If network params were not set by netif_open(), try to get * them via bootp, rarp, etc. */ if (rootip.s_addr == 0) { /* Get root IP address, and path, etc. */ error = net_getparams(netdev_sock); if (error) { /* getparams makes its own noise */ free(netdev_name); netif_close(netdev_sock); netdev_sock = -1; return (error); } } /* * Set the variables required by the kernel's nfs_diskless * mechanism. This is the minimum set of variables required to * mount a root filesystem without needing to obtain additional * info from bootp or other sources. */ d = socktodesc(netdev_sock); sprintf(temp, "%6D", d->myea, ":"); setenv("boot.netif.hwaddr", temp, 1); setenv("boot.netif.ip", inet_ntoa(myip), 1); setenv("boot.netif.netmask", intoa(netmask), 1); setenv("boot.netif.gateway", inet_ntoa(gateip), 1); setenv("boot.nfsroot.server", inet_ntoa(rootip), 1); setenv("boot.nfsroot.path", rootpath, 1); } netdev_opens++; f->f_devdata = &netdev_sock; return (error); }
int netmountroot(struct open_file *f, char *devname) { int error; struct iodesc *d; #ifdef DEBUG printf("netmountroot: %s\n", devname); #endif if (netio_ask) { get_my_ip: printf("My IP address? "); bzero(input_line, sizeof(input_line)); gets(input_line); if ((myip.s_addr = inet_addr(input_line)) == htonl(INADDR_NONE)) { printf("invalid IP address: %s\n", input_line); goto get_my_ip; } get_my_netmask: printf("My netmask? "); bzero(input_line, sizeof(input_line)); gets(input_line); if ((netmask = inet_addr(input_line)) == htonl(INADDR_NONE)) { printf("invalid netmask: %s\n", input_line); goto get_my_netmask; } get_my_gateway: printf("My gateway? "); bzero(input_line, sizeof(input_line)); gets(input_line); if ((gateip.s_addr = inet_addr(input_line)) == htonl(INADDR_NONE)) { printf("invalid IP address: %s\n", input_line); goto get_my_gateway; } get_server_ip: printf("Server IP address? "); bzero(input_line, sizeof(input_line)); gets(input_line); if ((rootip.s_addr = inet_addr(input_line)) == htonl(INADDR_NONE)) { printf("invalid IP address: %s\n", input_line); goto get_server_ip; } get_server_path: printf("Server path? "); bzero(rootpath, sizeof(rootpath)); gets(rootpath); if (rootpath[0] == '\0' || rootpath[0] == '\n') goto get_server_path; if ((d = socktodesc(netdev_sock)) == NULL) return (EMFILE); d->myip = myip; goto do_nfs_mount; } /* * Get info for NFS boot: our IP address, our hostname, * server IP address, and our root path on the server. * There are two ways to do this: The old, Sun way, * and the more modern, BOOTP way. (RFC951, RFC1048) */ #ifdef SUN_BOOTPARAMS /* Get boot info using RARP and Sun bootparams. */ /* Get our IP address. (rarp.c) */ if (rarp_getipaddress(netdev_sock) == -1) return (errno); printf("boot: client IP address: %s\n", inet_ntoa(myip)); /* Get our hostname, server IP address. */ if (bp_whoami(netdev_sock)) return (errno); printf("boot: client name: %s\n", hostname); /* Get the root pathname. */ if (bp_getfile(netdev_sock, "root", &rootip, rootpath)) return (errno); #else /* Get boot info using BOOTP way. (RFC951, RFC1048) */ bootp(netdev_sock); printf("Using IP address: %s\n", inet_ntoa(myip)); printf("myip: %s (%s)", hostname, inet_ntoa(myip)); if (gateip) printf(", gateip: %s", inet_ntoa(gateip)); if (mask) printf(", mask: %s", intoa(netmask)); printf("\n"); #endif /* SUN_BOOTPARAMS */ printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); do_nfs_mount: /* Get the NFS file handle (mount). */ error = nfs_mount(netdev_sock, rootip, rootpath); return (error); }
static void cnfp_v6_print(netdissect_options *ndo, const u_char *cp) { register const struct nfhdr_v6 *nh; register const struct nfrec_v6 *nr; struct protoent *pent; int nrecs, ver; #if 0 time_t t; #endif nh = (const struct nfhdr_v6 *)cp; ND_TCHECK(*nh); ver = EXTRACT_16BITS(&nh->version); nrecs = EXTRACT_32BITS(&nh->count); #if 0 /* * This is seconds since the UN*X epoch, and is followed by * nanoseconds. XXX - format it, rather than just dumping the * raw seconds-since-the-Epoch. */ t = EXTRACT_32BITS(&nh->utc_sec); #endif ND_PRINT((ndo, "NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver, EXTRACT_32BITS(&nh->msys_uptime)/1000, EXTRACT_32BITS(&nh->msys_uptime)%1000, EXTRACT_32BITS(&nh->utc_sec), EXTRACT_32BITS(&nh->utc_nsec))); ND_PRINT((ndo, "#%u, ", EXTRACT_32BITS(&nh->sequence))); nr = (const struct nfrec_v6 *)&nh[1]; ND_PRINT((ndo, "%2u recs", nrecs)); for (; nrecs != 0; nr++, nrecs--) { char buf[20]; char asbuf[20]; /* * Make sure we have the entire record. */ ND_TCHECK(*nr); ND_PRINT((ndo, "\n started %u.%03u, last %u.%03u", EXTRACT_32BITS(&nr->start_time)/1000, EXTRACT_32BITS(&nr->start_time)%1000, EXTRACT_32BITS(&nr->last_time)/1000, EXTRACT_32BITS(&nr->last_time)%1000)); asbuf[0] = buf[0] = '\0'; snprintf(buf, sizeof(buf), "/%u", nr->src_mask); snprintf(asbuf, sizeof(asbuf), ":%u", EXTRACT_16BITS(&nr->src_as)); ND_PRINT((ndo, "\n %s%s%s:%u ", intoa(nr->src_ina.s_addr), buf, asbuf, EXTRACT_16BITS(&nr->srcport))); snprintf(buf, sizeof(buf), "/%d", nr->dst_mask); snprintf(asbuf, sizeof(asbuf), ":%u", EXTRACT_16BITS(&nr->dst_as)); ND_PRINT((ndo, "> %s%s%s:%u ", intoa(nr->dst_ina.s_addr), buf, asbuf, EXTRACT_16BITS(&nr->dstport))); ND_PRINT((ndo, ">> %s\n ", intoa(nr->nhop_ina.s_addr))); pent = getprotobynumber(nr->proto); if (!pent || ndo->ndo_nflag) ND_PRINT((ndo, "%u ", nr->proto)); else ND_PRINT((ndo, "%s ", pent->p_name)); /* tcp flags for tcp only */ if (pent && pent->p_proto == IPPROTO_TCP) { int flags; flags = nr->tcp_flags; ND_PRINT((ndo, "%s%s%s%s%s%s%s", flags & TH_FIN ? "F" : "", flags & TH_SYN ? "S" : "", flags & TH_RST ? "R" : "", flags & TH_PUSH ? "P" : "", flags & TH_ACK ? "A" : "", flags & TH_URG ? "U" : "", flags ? " " : "")); } buf[0]='\0'; snprintf(buf, sizeof(buf), "(%u<>%u encaps)", (EXTRACT_16BITS(&nr->flags) >> 8) & 0xff, (EXTRACT_16BITS(&nr->flags)) & 0xff); ND_PRINT((ndo, "tos %u, %u (%u octets) %s", nr->tos, EXTRACT_32BITS(&nr->packets), EXTRACT_32BITS(&nr->octets), buf)); } return; trunc: ND_PRINT((ndo, "[|cnfp]")); return; }
/* * Open an rtip datagram connection for output. * addr is in network order; port is in host order. * XXX port should be in network order */ int rtipChannel::openssock(u_int32_t addr, u_short port, rcapTraffic* rt, rcapRequirements* rr, int& lcid_) const { /*XXX bruce says this is host order */ port = ntohs(port); fprintf(stderr, "vic: rtip-traffic: xmin %d xave %d I %d smax %d\n", rt->xmin, rt->xave, rt->I, rt->smax); fprintf(stderr, "vic: rtip-req: D %d J %d Z %d W %d U %d type %d\n", rr->D, rr->J, rr->Z, rr->W, rr->U, rr->type); rcapAddress dst; dst.ipAddr.s_addr = addr; dst.port = port; rcapUserControl control; control.userControl = NULL; /* no user control data */ control.userControlLength = 0; parmblock pb; pb.traffic = rt; pb.requirements = rr; pb.destination = &dst; pb.control = &control; /* XXX error structure not right here */ /* * Do the channel establishment using the parameters initialized * above. */ fprintf(stderr, "vic: waiting for rcap sender connect to %s\n", intoa(addr)); u_short lcid; again: int rc = RcapEstablishRequest(&pb, &lcid, (struct in_addr *)&dst); if (rc == RCAP_ERROR_UNREGISTERED_PORT) { fprintf(stderr, "vic: no rcap receiver at destination\n"); fprintf(stderr, "vic: trying to connect again...\n"); sleep(1); goto again; } if (rc != RCAP_SUCCESS) { fprintf(stderr, "vic: RcapEstablishRequest (rcap errno %d)\n", rc); exit(1); } lcid_ = lcid; /* * Now get a real-time socket and create the socket-to-channel * association using a socket option. */ int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_RMTP); if (s < 0) { perror("vic: socket"); return (-1); } if (setsockopt(s, IPPROTO_RTIP, RTIP_ASSOC, (char *)&lcid, sizeof(lcid)) < 0) { perror("vic: setsockopt"); return (-1); } fprintf(stderr, "vic: rcap: sender connection established\n"); int bufsize = 32 * 1024; if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&bufsize, sizeof(bufsize)) < 0) perror("vic: setsockopt (SO_SNDBUF)"); return (s); }
/* Fetch required bootp infomation */ void bootp(int sock, int flag) { struct iodesc *d; struct bootp *bp; struct { u_char header[HEADER_SIZE]; struct bootp wbootp; } wbuf; struct { u_char header[HEADER_SIZE]; struct bootp rbootp; } rbuf; #ifdef BOOTP_DEBUG if (debug) printf("bootp: socket=%d\n", sock); #endif if (!bot) bot = getsecs(); if (!(d = socktodesc(sock))) { printf("bootp: bad socket. %d\n", sock); return; } #ifdef BOOTP_DEBUG if (debug) printf("bootp: d=%lx\n", (long)d); #endif bp = &wbuf.wbootp; bzero(bp, sizeof(*bp)); bp->bp_op = BOOTREQUEST; bp->bp_htype = 1; /* 10Mb Ethernet (48 bits) */ bp->bp_hlen = 6; bp->bp_xid = htonl(d->xid); MACPY(d->myea, bp->bp_chaddr); strncpy(bp->bp_file, bootfile, sizeof(bp->bp_file)); bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)); #ifdef SUPPORT_DHCP bp->bp_vend[4] = TAG_DHCP_MSGTYPE; bp->bp_vend[5] = 1; bp->bp_vend[6] = DHCPDISCOVER; /* * If we are booting from PXE, we want to send the string * 'PXEClient' to the DHCP server so you have the option of * only responding to PXE aware dhcp requests. */ if (flag & BOOTP_PXE) { bp->bp_vend[7] = TAG_CLASSID; bp->bp_vend[8] = 9; bcopy("PXEClient", &bp->bp_vend[9], 9); bp->bp_vend[18] = TAG_PARAM_REQ; bp->bp_vend[19] = 7; bp->bp_vend[20] = TAG_ROOTPATH; bp->bp_vend[21] = TAG_HOSTNAME; bp->bp_vend[22] = TAG_SWAPSERVER; bp->bp_vend[23] = TAG_GATEWAY; bp->bp_vend[24] = TAG_SUBNET_MASK; bp->bp_vend[25] = TAG_INTF_MTU; bp->bp_vend[26] = TAG_SERVERID; bp->bp_vend[27] = TAG_END; } else bp->bp_vend[7] = TAG_END; #else bp->bp_vend[4] = TAG_END; #endif d->myip.s_addr = INADDR_ANY; d->myport = htons(IPPORT_BOOTPC); d->destip.s_addr = INADDR_BROADCAST; d->destport = htons(IPPORT_BOOTPS); #ifdef SUPPORT_DHCP expected_dhcpmsgtype = DHCPOFFER; dhcp_ok = 0; #endif if(sendrecv(d, bootpsend, bp, sizeof(*bp), bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp)) == -1) { printf("bootp: no reply\n"); return; } #ifdef SUPPORT_DHCP if(dhcp_ok) { u_int32_t leasetime; bp->bp_vend[6] = DHCPREQUEST; bp->bp_vend[7] = TAG_REQ_ADDR; bp->bp_vend[8] = 4; bcopy(&rbuf.rbootp.bp_yiaddr, &bp->bp_vend[9], 4); bp->bp_vend[13] = TAG_SERVERID; bp->bp_vend[14] = 4; bcopy(&dhcp_serverip.s_addr, &bp->bp_vend[15], 4); bp->bp_vend[19] = TAG_LEASETIME; bp->bp_vend[20] = 4; leasetime = htonl(300); bcopy(&leasetime, &bp->bp_vend[21], 4); if (flag & BOOTP_PXE) { bp->bp_vend[25] = TAG_CLASSID; bp->bp_vend[26] = 9; bcopy("PXEClient", &bp->bp_vend[27], 9); bp->bp_vend[36] = TAG_END; } else bp->bp_vend[25] = TAG_END; expected_dhcpmsgtype = DHCPACK; if(sendrecv(d, bootpsend, bp, sizeof(*bp), bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp)) == -1) { printf("DHCPREQUEST failed\n"); return; } } #endif myip = d->myip = rbuf.rbootp.bp_yiaddr; servip = rbuf.rbootp.bp_siaddr; if(rootip.s_addr == INADDR_ANY) rootip = servip; bcopy(rbuf.rbootp.bp_file, bootfile, sizeof(bootfile)); bootfile[sizeof(bootfile) - 1] = '\0'; if (!netmask) { if (IN_CLASSA(ntohl(myip.s_addr))) netmask = htonl(IN_CLASSA_NET); else if (IN_CLASSB(ntohl(myip.s_addr))) netmask = htonl(IN_CLASSB_NET); else netmask = htonl(IN_CLASSC_NET); #ifdef BOOTP_DEBUG if (debug) printf("'native netmask' is %s\n", intoa(netmask)); #endif } #ifdef BOOTP_DEBUG if (debug) printf("mask: %s\n", intoa(netmask)); #endif /* We need a gateway if root is on a different net */ if (!SAMENET(myip, rootip, netmask)) { #ifdef BOOTP_DEBUG if (debug) printf("need gateway for root ip\n"); #endif } /* Toss gateway if on a different net */ if (!SAMENET(myip, gateip, netmask)) { #ifdef BOOTP_DEBUG if (debug) printf("gateway ip (%s) bad\n", inet_ntoa(gateip)); #endif gateip.s_addr = 0; } /* Bump xid so next request will be unique. */ ++d->xid; }
char * inet_ntoa(struct in_addr ia) { return (intoa(ia.s_addr)); }
/* Fetch required bootp information */ void bootp(int sock) { struct iodesc *d; struct bootp *bp; struct { u_char header[UDP_TOTAL_HEADER_SIZE]; struct bootp wbootp; } wbuf; struct { u_char header[UDP_TOTAL_HEADER_SIZE]; struct bootp rbootp; } rbuf; unsigned int index; #ifdef BOOTP_DEBUG if (debug) printf("bootp: socket=%d\n", sock); #endif if (!bot) bot = getsecs(); if (!(d = socktodesc(sock))) { printf("bootp: bad socket. %d\n", sock); return; } #ifdef BOOTP_DEBUG if (debug) printf("bootp: d=%lx\n", (long)d); #endif bp = &wbuf.wbootp; (void)memset(bp, 0, sizeof(*bp)); bp->bp_op = BOOTREQUEST; bp->bp_htype = 1; /* 10Mb Ethernet (48 bits) */ bp->bp_hlen = 6; bp->bp_xid = htonl(d->xid); MACPY(d->myea, bp->bp_chaddr); (void)strncpy((char *)bp->bp_file, bootfile, sizeof(bp->bp_file)); (void)memcpy(bp->bp_vend, vm_rfc1048, sizeof(vm_rfc1048)); index = 4; #ifdef SUPPORT_DHCP bp->bp_vend[index++] = TAG_DHCP_MSGTYPE; bp->bp_vend[index++] = 1; bp->bp_vend[index++] = DHCPDISCOVER; #endif bootp_addvend(&bp->bp_vend[index]); d->myip.s_addr = INADDR_ANY; d->myport = htons(IPPORT_BOOTPC); d->destip.s_addr = INADDR_BROADCAST; d->destport = htons(IPPORT_BOOTPS); #ifdef SUPPORT_DHCP expected_dhcpmsgtype = DHCPOFFER; dhcp_ok = 0; #endif if (sendrecv(d, bootpsend, bp, sizeof(*bp), bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp)) == -1) { printf("bootp: no reply\n"); return; } #ifdef SUPPORT_DHCP if (dhcp_ok) { u_int32_t leasetime; index = 6; bp->bp_vend[index++] = DHCPREQUEST; bp->bp_vend[index++] = TAG_REQ_ADDR; bp->bp_vend[index++] = 4; (void)memcpy(&bp->bp_vend[9], &rbuf.rbootp.bp_yiaddr, 4); index += 4; bp->bp_vend[index++] = TAG_SERVERID; bp->bp_vend[index++] = 4; (void)memcpy(&bp->bp_vend[index], &dhcp_serverip.s_addr, 4); index += 4; bp->bp_vend[index++] = TAG_LEASETIME; bp->bp_vend[index++] = 4; leasetime = htonl(300); (void)memcpy(&bp->bp_vend[index], &leasetime, 4); index += 4; bootp_addvend(&bp->bp_vend[index]); expected_dhcpmsgtype = DHCPACK; if (sendrecv(d, bootpsend, bp, sizeof(*bp), bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp)) == -1) { printf("DHCPREQUEST failed\n"); return; } } #endif myip = d->myip = rbuf.rbootp.bp_yiaddr; servip = rbuf.rbootp.bp_siaddr; if (rootip.s_addr == INADDR_ANY) rootip = servip; (void)memcpy(bootfile, rbuf.rbootp.bp_file, sizeof(bootfile)); bootfile[sizeof(bootfile) - 1] = '\0'; if (IN_CLASSA(myip.s_addr)) nmask = IN_CLASSA_NET; else if (IN_CLASSB(myip.s_addr)) nmask = IN_CLASSB_NET; else nmask = IN_CLASSC_NET; #ifdef BOOTP_DEBUG if (debug) printf("'native netmask' is %s\n", intoa(nmask)); #endif /* Get subnet (or natural net) mask */ netmask = nmask; if (smask) netmask = smask; #ifdef BOOTP_DEBUG if (debug) printf("mask: %s\n", intoa(netmask)); #endif /* We need a gateway if root is on a different net */ if (!SAMENET(myip, rootip, netmask)) { #ifdef BOOTP_DEBUG if (debug) printf("need gateway for root ip\n"); #endif } /* Toss gateway if on a different net */ if (!SAMENET(myip, gateip, netmask)) { #ifdef BOOTP_DEBUG if (debug) printf("gateway ip (%s) bad\n", inet_ntoa(gateip)); #endif gateip.s_addr = 0; } #ifdef BOOTP_DEBUG if (debug) { printf("client addr: %s\n", inet_ntoa(myip)); if (smask) printf("subnet mask: %s\n", intoa(smask)); if (gateip.s_addr != 0) printf("net gateway: %s\n", inet_ntoa(gateip)); printf("server addr: %s\n", inet_ntoa(rootip)); if (rootpath[0] != '\0') printf("server path: %s\n", rootpath); if (bootfile[0] != '\0') printf("file name: %s\n", bootfile); } #endif /* Bump xid so next request will be unique. */ ++d->xid; }
static int net_getparams(int sock) { char buf[MAXHOSTNAMELEN]; char temp[FNAME_SIZE]; struct iodesc *d; int i; n_long smask; #ifdef SUPPORT_BOOTP /* * Try to get boot info using BOOTP. If we succeed, then * the server IP address, gateway, and root path will all * be initialized. If any remain uninitialized, we will * use RARP and RPC/bootparam (the Sun way) to get them. */ if (try_bootp) bootp(sock, BOOTP_NONE); if (myip.s_addr != 0) goto exit; if (debug) printf("net_open: BOOTP failed, trying RARP/RPC...\n"); #endif /* * Use RARP to get our IP address. This also sets our * netmask to the "natural" default for our address. */ if (rarp_getipaddress(sock)) { printf("net_open: RARP failed\n"); return (EIO); } printf("net_open: client addr: %s\n", inet_ntoa(myip)); /* Get our hostname, server IP address, gateway. */ if (bp_whoami(sock)) { printf("net_open: bootparam/whoami RPC failed\n"); return (EIO); } printf("net_open: client name: %s\n", hostname); /* * Ignore the gateway from whoami (unreliable). * Use the "gateway" parameter instead. */ smask = 0; gateip.s_addr = 0; if (bp_getfile(sock, "gateway", &gateip, buf) == 0) { /* Got it! Parse the netmask. */ smask = ip_convertaddr(buf); } if (smask) { netmask = smask; printf("net_open: subnet mask: %s\n", intoa(netmask)); } if (gateip.s_addr) printf("net_open: net gateway: %s\n", inet_ntoa(gateip)); /* Get the root server and pathname. */ if (bp_getfile(sock, "root", &rootip, rootpath)) { printf("net_open: bootparam/getfile RPC failed\n"); return (EIO); } exit: /* * If present, strip the server's address off of the rootpath * before passing it along. This allows us to be compatible with * the kernel's diskless (BOOTP_NFSROOT) booting conventions */ for (i = 0; i < FNAME_SIZE && rootpath[i] != '\0'; i++) if (rootpath[i] == ':') break; if (i && i != FNAME_SIZE && rootpath[i] == ':') { rootpath[i++] = '\0'; if (inet_addr(&rootpath[0]) != INADDR_NONE) rootip.s_addr = inet_addr(&rootpath[0]); memcpy(&temp[0], &rootpath[i], strlen(&rootpath[i])+1); memcpy(&rootpath[0], &temp[0], strlen(&rootpath[i])+1); } printf("net_open: server addr: %s\n", inet_ntoa(rootip)); printf("net_open: server path: %s\n", rootpath); d = socktodesc(sock); snprintf(temp, sizeof(temp), "%6D", d->myea, ":"); setenv("boot.netif.ip", inet_ntoa(myip), 1); setenv("boot.netif.netmask", intoa(netmask), 1); setenv("boot.netif.gateway", inet_ntoa(gateip), 1); setenv("boot.netif.hwaddr", temp, 1); setenv("boot.nfsroot.server", inet_ntoa(rootip), 1); setenv("boot.nfsroot.path", rootpath, 1); return (0); }
/* Returns 0 if this is the packet we're waiting for else -1 (and errno == 0) */ static ssize_t bootprecv(struct iodesc *d, void *pkt, size_t len, time_t tleft) { ssize_t n; struct bootp *bp; #ifdef BOOTP_DEBUG if (debug) printf("bootprecv: called\n"); #endif n = readudp(d, pkt, len, tleft); if (n < 0 || (size_t)n < sizeof(struct bootp)) goto bad; bp = (struct bootp *)pkt; #ifdef BOOTP_DEBUG if (debug) printf("bootprecv: checked. bp = 0x%x, n = %d\n", (unsigned)bp, n); #endif if (bp->bp_xid != htonl(d->xid)) { #ifdef BOOTP_DEBUG if (debug) { printf("bootprecv: expected xid 0x%lx, got 0x%lx\n", d->xid, ntohl(bp->bp_xid)); } #endif goto bad; } #ifdef BOOTP_DEBUG if (debug) printf("bootprecv: got one!\n"); #endif /* Pick up our ip address (and natural netmask) */ myip = d->myip = bp->bp_yiaddr; #ifdef BOOTP_DEBUG if (debug) printf("our ip address is %s\n", inet_ntoa(d->myip)); #endif if (IN_CLASSA(d->myip.s_addr)) nmask = IN_CLASSA_NET; else if (IN_CLASSB(d->myip.s_addr)) nmask = IN_CLASSB_NET; else nmask = IN_CLASSC_NET; #ifdef BOOTP_DEBUG if (debug) printf("'native netmask' is %s\n", intoa(nmask)); #endif /* Pick up root or swap server address and file spec. */ if (bp->bp_siaddr.s_addr != 0) rootip = bp->bp_siaddr; if (bp->bp_file[0] != '\0') { strncpy(bootfile, (char *)bp->bp_file, sizeof(bootfile)); bootfile[sizeof(bootfile) - 1] = '\0'; } /* Suck out vendor info */ if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0) vend_cmu(bp->bp_vend); else if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) vend_rfc1048(bp->bp_vend, sizeof(bp->bp_vend)); else printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend); /* Check subnet mask against net mask; toss if bogus */ if ((nmask & smask) != nmask) { #ifdef BOOTP_DEBUG if (debug) printf("subnet mask (%s) bad\n", intoa(smask)); #endif smask = 0; } /* Get subnet (or natural net) mask */ netmask = nmask; if (smask) netmask = smask; #ifdef BOOTP_DEBUG if (debug) printf("mask: %s\n", intoa(netmask)); #endif /* We need a gateway if root or swap is on a different net */ if (!SAMENET(d->myip, rootip, netmask)) { #ifdef BOOTP_DEBUG if (debug) printf("need gateway for root ip\n"); #endif } if (!SAMENET(d->myip, swapip, netmask)) { #ifdef BOOTP_DEBUG if (debug) printf("need gateway for swap ip\n"); #endif } /* Toss gateway if on a different net */ if (!SAMENET(d->myip, gateip, netmask)) { #ifdef BOOTP_DEBUG if (debug) printf("gateway ip (%s) bad\n", inet_ntoa(gateip)); #endif gateip.s_addr = 0; } return (n); bad: errno = 0; return (-1); }
/* Fetch required bootp infomation */ void bootp(int sock) { void *pkt; struct iodesc *d; struct bootp *bp; struct { uchar_t header[HEADER_SIZE]; struct bootp wbootp; } wbuf; struct bootp *rbootp; #ifdef BOOTP_DEBUG if (debug) printf("bootp: socket=%d\n", sock); #endif if (!bot) bot = getsecs(); if (!(d = socktodesc(sock))) { printf("bootp: bad socket. %d\n", sock); return; } #ifdef BOOTP_DEBUG if (debug) printf("bootp: d=%lx\n", (long)d); #endif bp = &wbuf.wbootp; bzero(bp, sizeof (*bp)); bp->bp_op = BOOTREQUEST; bp->bp_htype = 1; /* 10Mb Ethernet (48 bits) */ bp->bp_hlen = 6; bp->bp_xid = htonl(d->xid); MACPY(d->myea, bp->bp_chaddr); strncpy(bp->bp_file, bootfile, sizeof (bp->bp_file)); bcopy(vm_rfc1048, bp->bp_vend, sizeof (vm_rfc1048)); #ifdef SUPPORT_DHCP bp->bp_vend[4] = TAG_DHCP_MSGTYPE; bp->bp_vend[5] = 1; bp->bp_vend[6] = DHCPDISCOVER; bootp_fill_request(&bp->bp_vend[7]); #else bp->bp_vend[4] = TAG_END; #endif d->myip.s_addr = INADDR_ANY; d->myport = htons(IPPORT_BOOTPC); d->destip.s_addr = INADDR_BROADCAST; d->destport = htons(IPPORT_BOOTPS); #ifdef SUPPORT_DHCP expected_dhcpmsgtype = DHCPOFFER; dhcp_ok = 0; #endif if (sendrecv(d, bootpsend, bp, sizeof (*bp), bootprecv, &pkt, (void **)&rbootp, NULL) == -1) { printf("bootp: no reply\n"); return; } #ifdef SUPPORT_DHCP if (dhcp_ok) { uint32_t leasetime; bp->bp_vend[6] = DHCPREQUEST; bp->bp_vend[7] = TAG_REQ_ADDR; bp->bp_vend[8] = 4; bcopy(&rbootp->bp_yiaddr, &bp->bp_vend[9], 4); bp->bp_vend[13] = TAG_SERVERID; bp->bp_vend[14] = 4; bcopy(&dhcp_serverip.s_addr, &bp->bp_vend[15], 4); bp->bp_vend[19] = TAG_LEASETIME; bp->bp_vend[20] = 4; leasetime = htonl(300); bcopy(&leasetime, &bp->bp_vend[21], 4); bootp_fill_request(&bp->bp_vend[25]); expected_dhcpmsgtype = DHCPACK; free(pkt); if (sendrecv(d, bootpsend, bp, sizeof (*bp), bootprecv, &pkt, (void **)&rbootp, NULL) == -1) { printf("DHCPREQUEST failed\n"); return; } } #endif myip = d->myip = rbootp->bp_yiaddr; servip = rbootp->bp_siaddr; if (rootip.s_addr == INADDR_ANY) rootip = servip; bcopy(rbootp->bp_file, bootfile, sizeof (bootfile)); bootfile[sizeof (bootfile) - 1] = '\0'; if (!netmask) { if (IN_CLASSA(ntohl(myip.s_addr))) netmask = htonl(IN_CLASSA_NET); else if (IN_CLASSB(ntohl(myip.s_addr))) netmask = htonl(IN_CLASSB_NET); else netmask = htonl(IN_CLASSC_NET); #ifdef BOOTP_DEBUG if (debug) printf("'native netmask' is %s\n", intoa(netmask)); #endif } #ifdef BOOTP_DEBUG if (debug) printf("mask: %s\n", intoa(netmask)); #endif /* We need a gateway if root is on a different net */ if (!SAMENET(myip, rootip, netmask)) { #ifdef BOOTP_DEBUG if (debug) printf("need gateway for root ip\n"); #endif } /* Toss gateway if on a different net */ if (!SAMENET(myip, gateip, netmask)) { #ifdef BOOTP_DEBUG if (debug) printf("gateway ip (%s) bad\n", inet_ntoa(gateip)); #endif gateip.s_addr = 0; } /* Bump xid so next request will be unique. */ ++d->xid; free(pkt); }