int netstat_main(int argc, char *argv[]) { printf("Proto Recv-Q Send-Q Local Address Foreign Address State\n"); ipv4("/proc/net/tcp", "tcp"); ipv4("/proc/net/udp", "udp"); ipv6("/proc/net/tcp6", "tcp6"); ipv6("/proc/net/udp6", "udp6"); return 0; }
void ClientHandler::addClient(Ethernet *packet) { switch (packet->getEther_type()) { case ETHERTYPE_ARP: { ARP<Ethernet> arp(*packet); addClient(arp.getSha(), arp.getSpa(), (userMac.compare(arp.getSha().c_str())) ? false : true); addClient(arp.getTha(), arp.getTpa(), (userMac.compare(arp.getTha().c_str())) ? false : true); break; } case ETHERTYPE_IP: { IP<Ethernet> ip(*packet); addClient(ip.getEther_shost(), ip.getIpSrc(), (userMac.compare(ip.getEther_dhost().c_str())) ? false : true); addClient(ip.getEther_dhost(), ip.getIpDst(), (userMac.compare(ip.getEther_shost().c_str())) ? false : true); break; } case ETHERTYPE_IPV6: { IPV6<Ethernet> ipv6(*packet); addClient(ipv6.getEther_shost(), ipv6.getIp6Src(), (userMac.compare(ipv6.getEther_dhost().c_str())) ? false : true); addClient(ipv6.getEther_dhost(), ipv6.getIp6Dest(), (userMac.compare(ipv6.getEther_shost().c_str())) ? false : true); break; } default: { addClient(packet->getEther_shost(), (userMac.compare(packet->getEther_dhost().c_str())) ? false : true); addClient(packet->getEther_dhost(), (userMac.compare(packet->getEther_shost().c_str())) ? false : true); break; } } }
void tst_QNetworkAddressEntry::prefixAndNetmask_data() { QTest::addColumn<QHostAddress>("ip"); QTest::addColumn<QHostAddress>("netmask"); QTest::addColumn<int>("prefix"); // IPv4 set: QHostAddress ipv4(QHostAddress::LocalHost); QTest::newRow("v4/0") << ipv4 << QHostAddress(QHostAddress::AnyIPv4) << 0; QTest::newRow("v4/32") << ipv4 << QHostAddress("255.255.255.255") << 32; QTest::newRow("v4/24") << ipv4 << QHostAddress("255.255.255.0") << 24; QTest::newRow("v4/23") << ipv4 << QHostAddress("255.255.254.0") << 23; QTest::newRow("v4/20") << ipv4 << QHostAddress("255.255.240.0") << 20; QTest::newRow("v4/invalid1") << ipv4 << QHostAddress(QHostAddress::LocalHost) << -1; QTest::newRow("v4/invalid2") << ipv4 << QHostAddress(QHostAddress::AnyIPv6) << -1; QTest::newRow("v4/invalid3") << ipv4 << QHostAddress("255.255.253.0") << -1; QTest::newRow("v4/invalid4") << ipv4 << QHostAddress() << -2; QTest::newRow("v4/invalid5") << ipv4 << QHostAddress() << 33; // IPv6 set: QHostAddress ipv6(QHostAddress::LocalHostIPv6); QTest::newRow("v6/0") << ipv6 << QHostAddress(QHostAddress::AnyIPv6) << 0; QTest::newRow("v6/128") << ipv6 << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") << 128; QTest::newRow("v6/64") << ipv6 << QHostAddress("ffff:ffff:ffff:ffff::") << 64; QTest::newRow("v6/63") << ipv6 << QHostAddress("ffff:ffff:ffff:fffe::") << 63; QTest::newRow("v6/60") << ipv6 << QHostAddress("ffff:ffff:ffff:fff0::") << 60; QTest::newRow("v6/48") << ipv6 << QHostAddress("ffff:ffff:ffff::") << 48; QTest::newRow("v6/3") << ipv6 << QHostAddress("e000::") << 3; QTest::newRow("v6/invalid1") << ipv6 << QHostAddress(QHostAddress::LocalHostIPv6) << -1; QTest::newRow("v6/invalid2") << ipv6 << QHostAddress(QHostAddress::Any) << -1; QTest::newRow("v6/invalid3") << ipv6 << QHostAddress("fffd::") << -1; QTest::newRow("v6/invalid4") << ipv6 << QHostAddress() << -2; QTest::newRow("v6/invalid5") << ipv6 << QHostAddress() << 129; }
int protocol_handler(int protoId,int *sd, int port){ /*Unspec or IPV4 or IPV6*/ //if (debug) puts("Choosing protocol..."); switch(protoId){ case 0: //IPV4 or IPV6 if (unspec(sd,port) < 0) return -1; if (debug) puts("Setting unspec"); //if (debug) puts("Done!"); break; case 4: //IPV4 if (ipv4(sd,port) < 0) return -1; if (debug) puts("Setting ipv4"); //if (debug) puts("Done!"); break; case 6: //IPV6 if (ipv6(sd,port) < 0) return -1; if (debug) puts("Setting ipv6"); //if (debug) puts("Done!"); break; default: puts("Bad ProtocolId"); return -1; } return 0; }
int main() { std::regex ipv4("(([0-9]{1,2}|1[0-9]{1,2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]{1,2}|1[0-9]{1,2}|2[0-4][0-9]|25[0-5])"); std::regex ipv6("(([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}))"); std::string strn; getline(std::cin, strn); int n = atoi(strn.c_str()); for(int i = 0; i < n; i++) { std::string input; getline(std::cin, input); input = trim(input); if(std::regex_match(input, ipv4)) { std::cout << "IPv4" << std::endl; continue; } if(std::regex_match(input, ipv6)) { std::cout << "IPv6" << std::endl; continue; } std::cout << "Neither" << std::endl; } return 0; }
/* Get a socket address from a host/port combination. If a host provides both IPv4 and IPv6 addresses, prefer the IPv4 address. This routine uses getaddrinfo. Caller must free addr. */ PUBLIC int socketInfo(char *ip, int port, int *family, int *protocol, struct sockaddr_storage *addr, Socklen *addrlen) { struct addrinfo hints, *res, *r; char portBuf[16]; int v6; assert(addr); memset((char*) &hints, '\0', sizeof(hints)); /* Note that IPv6 does not support broadcast, there is no 255.255.255.255 equivalent. Multicast can be used over a specific link, but the user must provide that address plus %scope_id. */ if (ip == 0 || ip[0] == '\0') { ip = 0; hints.ai_flags |= AI_PASSIVE; /* Bind to 0.0.0.0 and :: */ } v6 = ipv6(ip); hints.ai_socktype = SOCK_STREAM; if (ip) { hints.ai_family = v6 ? AF_INET6 : AF_INET; } else { hints.ai_family = AF_UNSPEC; } itosbuf(portBuf, sizeof(portBuf), port, 10); /* Try to sleuth the address to avoid duplicate address lookups. Then try IPv4 first then IPv6. */ res = 0; if (getaddrinfo(ip, portBuf, &hints, &res) != 0) { return -1; } /* Prefer IPv4 if IPv6 not requested */ for (r = res; r; r = r->ai_next) { if (v6) { if (r->ai_family == AF_INET6) { break; } } else { if (r->ai_family == AF_INET) { break; } } } if (r == NULL) { r = res; } memset(addr, 0, sizeof(*addr)); memcpy((char*) addr, (char*) r->ai_addr, (int) r->ai_addrlen); *addrlen = (int) r->ai_addrlen; *family = r->ai_family; *protocol = r->ai_protocol; freeaddrinfo(res); return 0; }
PUBLIC bool socketIsV6(int sid) { WebsSocket *sp; if ((sp = socketPtr(sid)) == NULL) { return 0; } return sp->ip && ipv6(sp->ip); }
int main(int argc, char *argv[]) { (void) argc; (void) argv; ipv4(); ipv6(); std::cout << "Ok\n"; return 0; }
int process(char *packet) { struct s_ethernet *eth; /* the ethernet header */ char *payload; /* the IP header + packet payload */ /* parse ethernet header */ eth = (struct s_ethernet *) (packet); payload = packet + sizeof(struct s_ethernet); switch (htons(eth->type)) { case ETHERTYPE_IP: return ipv4(eth, payload); case ETHERTYPE_IPV6: return ipv6(eth, payload); case ETHERTYPE_ARP: log_debug("HW Protocol: ARP"); return arp(eth, payload); default: log_debug("HW Protocol: unknown [%d/0x%04x]", htons(eth->type), htons(eth->type)); return 1; } }
PUBLIC bool socketAddressIsV6(char *ip) { return ip && ipv6(ip); }
/* Parse address and return the IP address and port components. Handles ipv4 and ipv6 addresses. If the IP portion is absent, *pip is set to null. If the port portion is absent, port is set to the defaultPort. If a ":*" port specifier is used, *pport is set to -1; When an address contains an ipv6 port it should be written as: aaaa:bbbb:cccc:dddd:eeee:ffff:gggg:hhhh:iiii or [aaaa:bbbb:cccc:dddd:eeee:ffff:gggg:hhhh:iiii]:port If supplied an IPv6 address, the backets are stripped in the returned IP address. This routine parses any "https://" prefix. Caller must free *pip */ PUBLIC int socketParseAddress(char *address, char **pip, int *pport, int *secure, int defaultPort) { char *ip, *cp; ip = 0; if (defaultPort < 0) { defaultPort = 80; } *secure = strncmp(address, "https", 5) == 0; if ((cp = strstr(address, "://")) != 0) { address = &cp[3]; } if (ipv6(address)) { /* IPv6. If port is present, it will follow a closing bracket ']' */ if ((cp = strchr(address, ']')) != 0) { cp++; if ((*cp) && (*cp == ':')) { *pport = (*++cp == '*') ? -1 : atoi(cp); /* Set ipAddr to ipv6 address without brackets */ ip = sclone(address + 1); cp = strchr(ip, ']'); *cp = '\0'; } else { /* Handles [a:b:c:d:e:f:g:h:i] case (no port)- should not occur */ ip = sclone(address + 1); if ((cp = strchr(ip, ']')) != 0) { *cp = '\0'; } if (*ip == '\0') { ip = 0; } /* No port present, use callers default */ *pport = defaultPort; } } else { /* Handles a:b:c:d:e:f:g:h:i case (no port) */ ip = sclone(address); /* No port present, use callers default */ *pport = defaultPort; } } else { /* ipv4 */ ip = sclone(address); if ((cp = strchr(ip, ':')) != 0) { *cp++ = '\0'; if (*cp == '*') { *pport = -1; } else { *pport = atoi(cp); } if (*ip == '*' || *ip == '\0') { ip = 0; } } else if (strchr(ip, '.')) { *pport = defaultPort; } else { if (isdigit((uchar) *ip)) { *pport = atoi(ip); ip = 0; } else { /* No port present, use callers default */ *pport = defaultPort; } } } if (pip) { *pip = ip; } return 0; }
PUBLIC int socketListen(char *ip, int port, SocketAccept accept, int flags) { WebsSocket *sp; struct sockaddr_storage addr; Socklen addrlen; char *sip; int family, protocol, sid, rc, only; if (port > SOCKET_PORT_MAX) { return -1; } if ((sid = socketAlloc(ip, port, accept, flags)) < 0) { return -1; } sp = socketList[sid]; assert(sp); /* Change null IP address to be an IPv6 endpoint if the system is dual-stack. That way we can listen on both IPv4 and IPv6 */ sip = ((ip == 0 || *ip == '\0') && socketHasDualNetworkStack()) ? "::" : ip; /* Bind to the socket endpoint and the call listen() to start listening */ if (socketInfo(sip, port, &family, &protocol, &addr, &addrlen) < 0) { return -1; } if ((sp->sock = socket(family, SOCK_STREAM, protocol)) == SOCKET_ERROR) { socketFree(sid); return -1; } socketHighestFd = max(socketHighestFd, sp->sock); #if ME_COMPILER_HAS_FCNTL fcntl(sp->sock, F_SETFD, FD_CLOEXEC); #endif rc = 1; #if ME_UNIX_LIKE || VXWORKS setsockopt(sp->sock, SOL_SOCKET, SO_REUSEADDR, (char*) &rc, sizeof(rc)); #elif ME_WIN_LIKE && defined(SO_EXCLUSIVEADDRUSE) setsockopt(sp->sock, SOL_SOCKET, SO_REUSEADDR | SO_EXCLUSIVEADDRUSE, (char*) &rc, sizeof(rc)); #endif #if defined(IPV6_V6ONLY) /* By default, most stacks listen on both IPv6 and IPv4 if ip == 0, except windows which inverts this. So we explicitly control. */ if (hasIPv6) { if (ip == 0) { only = 0; setsockopt(sp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char*) &only, sizeof(only)); } else if (ipv6(ip)) { only = 1; setsockopt(sp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char*) &only, sizeof(only)); } } #endif if (bind(sp->sock, (struct sockaddr*) &addr, addrlen) == SOCKET_ERROR) { error("Can't bind to address %s:%d, errno %d", ip ? ip : "*", port, errno); socketFree(sid); return -1; } if (listen(sp->sock, SOMAXCONN) < 0) { socketFree(sid); return -1; } sp->flags |= SOCKET_LISTENING | SOCKET_NODELAY; sp->handlerMask |= SOCKET_READABLE; socketSetBlock(sid, (flags & SOCKET_BLOCK)); if (sp->flags & SOCKET_NODELAY) { socketSetNoDelay(sid, 1); } return sid; }
void ip(struct pktinfo *pktinfo) { register const struct ip *ipv4 = NULL; register int ipv4_p = 0; /* next header */ const struct in_addr *ipv4_src = NULL; const struct in_addr *ipv4_dst = NULL; struct addr_4 addr_ipv4; struct udphdr *udphdr = NULL; struct tcphdr *tcphdr = NULL; struct pktinfo n_hdr; u_int32_t src = 0; u_int32_t dst = 0; char *addr = NULL; const u_char *pkt = pktinfo->pkt; bpf_u_int32 len = pktinfo->len; ipv4 = (const struct ip *)pkt; /* next header type code */ ipv4_p = ipv4->ip_p; /* IPv4 address */ ipv4_src = &ipv4->ip_src; ipv4_dst = &ipv4->ip_dst; src = htonl(ipv4_src->s_addr); dst = htonl(ipv4_dst->s_addr); addr_ipv4.src = src; addr_ipv4.dst = dst; len -= sizeof(struct ip); pkt += sizeof(struct ip); n_hdr.len = len; n_hdr.pkt = pkt; addr_ipv4.p_tcp = NULL; addr_ipv4.p_udp = NULL; addr_ipv4.nxtflag = 0; if (ipv4_p == IPPROTO_TCP) { tcphdr = (struct tcphdr *)pkt; addr_ipv4.p_tcp = tcphdr; addr_ipv4.nxtflag = 1; } else if (ipv4_p == IPPROTO_UDP) { udphdr = (struct udphdr *)pkt; addr_ipv4.p_udp = udphdr; addr_ipv4.nxtflag = 2; } addr = v4_addr(&addr_ipv4, encap); encap ++; switch(ipv4_p) { case IPPROTO_IPV4 : /* IP header */ ip(&n_hdr); break; case IPPROTO_TCP : /* tcp */ if (rflag != 1) tcp(&n_hdr); break; case IPPROTO_UDP : /* user datagram protocol */ if (bflag != 1) udp(&n_hdr); break; case IPPROTO_IPV6 : /* IP6 header */ ipv6(&n_hdr); break; default: } /* end of switch */ } /* end of ip() */
/* static */ void IPv6RouteAdvertiser::createAdvertisementPacket( const Interface* intf, folly::io::RWPrivateCursor* cursor, folly::MacAddress dstMac, const folly::IPAddressV6& dstIP) { const auto* ndpConfig = &intf->getNdpConfig(); // Settings uint8_t hopLimit = ndpConfig->curHopLimit; // Set managed and other bits in router advertisements. // These bits control whether address and other information // (e.g. where to grab the image) is available via DHCP. uint8_t flags = 0; if (ndpConfig->routerAdvertisementManagedBit) { flags |= ND_RA_FLAG_MANAGED; } if (ndpConfig->routerAdvertisementOtherBit) { flags |= ND_RA_FLAG_OTHER; } std::chrono::seconds lifetime(ndpConfig->routerLifetime); std::chrono::seconds reachableTimer(0); std::chrono::seconds retransTimer(0); uint32_t prefixValidLifetime = ndpConfig->prefixValidLifetimeSeconds; uint32_t prefixPreferredLifetime = ndpConfig->prefixPreferredLifetimeSeconds; // Build the list of prefixes to advertise typedef std::pair<IPAddressV6, uint8_t> Prefix; uint32_t mtu = intf->getMtu(); std::set<Prefix> prefixes; foreachAddrToAdvertise(intf, [&]( const std::pair<folly::IPAddress, uint8_t> & addr) { uint8_t mask = addr.second; prefixes.emplace(addr.first.asV6().mask(mask), mask); }); auto serializeBody = [&](RWPrivateCursor* cursor) { cursor->writeBE<uint8_t>(hopLimit); cursor->writeBE<uint8_t>(flags); cursor->writeBE<uint16_t>(lifetime.count()); cursor->writeBE<uint32_t>(reachableTimer.count()); cursor->writeBE<uint32_t>(retransTimer.count()); // Source MAC option cursor->writeBE<uint8_t>(1); // Option type (src link-layer address) cursor->writeBE<uint8_t>(1); // Option length = 1 (x8) cursor->push(intf->getMac().bytes(), MacAddress::SIZE); // Prefix options for (const auto& prefix : prefixes) { cursor->writeBE<uint8_t>(3); // Option type (prefix information) cursor->writeBE<uint8_t>(4); // Option length = 4 (x8) cursor->writeBE<uint8_t>(prefix.second); uint8_t prefixFlags = 0xc0; // on link, autonomous address configuration cursor->writeBE<uint8_t>(prefixFlags); cursor->writeBE<uint32_t>(prefixValidLifetime); cursor->writeBE<uint32_t>(prefixPreferredLifetime); cursor->writeBE<uint32_t>(0); // reserved cursor->push(prefix.first.bytes(), IPAddressV6::byteCount()); } // MTU option cursor->writeBE<uint8_t>(5); // Option type (MTU) cursor->writeBE<uint8_t>(1); // Option length = 1 (x8) cursor->writeBE<uint16_t>(0); // Reserved cursor->writeBE<uint32_t>(mtu); }; auto bodyLength = getAdvertisementPacketBodySize(prefixes.size()); IPAddressV6 srcIP(IPAddressV6::LINK_LOCAL, intf->getMac()); IPv6Hdr ipv6(srcIP, dstIP); ipv6.trafficClass = 0xe0; // CS7 precedence (network control) ipv6.payloadLength = ICMPHdr::SIZE + bodyLength; ipv6.nextHeader = IP_PROTO_IPV6_ICMP; ipv6.hopLimit = 255; ICMPHdr icmp6(ICMPV6_TYPE_NDP_ROUTER_ADVERTISEMENT, 0, 0); icmp6.serializeFullPacket(cursor, dstMac, intf->getMac(), intf->getVlanID(), ipv6, bodyLength, serializeBody); }