DECLINLINE(int) tftpSendError(PNATState pData, PTFTPSESSION pTftpSession, uint16_t errorcode, const char *msg, PCTFTPIPHDR pcTftpIpHeaderRecv) { struct mbuf *m = NULL; PTFTPIPHDR pTftpIpHeader = NULL; LogFlowFunc(("ENTER: errorcode: %RX16, msg: %s\n", errorcode, msg)); m = slirpTftpMbufAlloc(pData); if (!m) { LogFlowFunc(("LEAVE: Can't allocate mbuf\n")); return -1; } m->m_data += if_maxlinkhdr; m->m_len = sizeof(TFTPIPHDR) + strlen(msg) + 1; /* ending zero */ m->m_pkthdr.header = mtod(m, void *); pTftpIpHeader = mtod(m, PTFTPIPHDR); pTftpIpHeader->u16TftpOpType = RT_H2N_U16_C(TFTP_ERROR); pTftpIpHeader->Core.u16TftpOpCode = RT_H2N_U16(errorcode); m_copyback(pData, m, sizeof(TFTPIPHDR), strlen(msg) + 1 /* copy ending zerro*/, (c_caddr_t)msg); tftpSend(pData, pTftpSession, m, pcTftpIpHeaderRecv); tftpSessionTerminate(pTftpSession); LogFlowFuncLeave(); return 0; }
static int tftp_send_oack(PNATState pData, struct tftp_session *spt, const char *key, uint32_t value, struct tftp_t *recv_tp) { struct sockaddr_in saddr, daddr; struct mbuf *m; struct tftp_t *tp; int n = 0; m = slirpTftpMbufAlloc(pData); if (!m) return -1; m->m_data += if_maxlinkhdr; m->m_pkthdr.header = mtod(m, void *); tp = (void *)m->m_data; m->m_data += sizeof(struct udpiphdr); tp->tp_op = RT_H2N_U16_C(TFTP_OACK); n += RTStrPrintf((char *)tp->x.tp_buf + n, M_TRAILINGSPACE(m), "%s", key) + 1; n += RTStrPrintf((char *)tp->x.tp_buf + n, M_TRAILINGSPACE(m), "%u", value) + 1; saddr.sin_addr = recv_tp->ip.ip_dst; saddr.sin_port = recv_tp->udp.uh_dport; daddr.sin_addr = spt->client_ip; daddr.sin_port = spt->client_port; m->m_len = sizeof(struct tftp_t) - 514 + n - sizeof(struct ip) - sizeof(struct udphdr); udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY); return 0; }
static int tftp_send_data(PNATState pData, struct tftp_session *spt, u_int16_t block_nr, struct tftp_t *recv_tp) { struct sockaddr_in saddr, daddr; struct mbuf *m; struct tftp_t *tp; int nobytes; if (block_nr < 1) return -1; m = slirpTftpMbufAlloc(pData); if (!m) return -1; m->m_data += if_maxlinkhdr; m->m_pkthdr.header = mtod(m, void *); tp = mtod(m, void *); m->m_data += sizeof(struct udpiphdr); tp->tp_op = RT_H2N_U16_C(TFTP_DATA); tp->x.tp_data.tp_block_nr = RT_H2N_U16(block_nr); saddr.sin_addr = recv_tp->ip.ip_dst; saddr.sin_port = recv_tp->udp.uh_dport; daddr.sin_addr = spt->client_ip; daddr.sin_port = spt->client_port; nobytes = tftp_read_data(pData, spt, block_nr - 1, tp->x.tp_data.tp_buf, 512); if (nobytes < 0) { m_freem(pData, m); /* send "file not found" error back */ tftp_send_error(pData, spt, 1, "File not found", tp); return -1; } m->m_len = sizeof(struct tftp_t) - (512 - nobytes) - sizeof(struct ip) - sizeof(struct udphdr); udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY); if (nobytes == 512) tftp_session_update(pData, spt); else tftp_session_terminate(spt); return 0; }
static int tftpSendData(PNATState pData, PTFTPSESSION pTftpSession, uint16_t u16Block, PCTFTPIPHDR pcTftpIpHeaderRecv) { struct mbuf *m; PTFTPIPHDR pTftpIpHeader; int cbRead = 0; int rc = VINF_SUCCESS; if (u16Block == pTftpSession->cTftpAck) pTftpSession->cTftpAck++; else { tftpSendError(pData, pTftpSession, 6, "ACK is wrong", pcTftpIpHeaderRecv); tftpSessionTerminate(pTftpSession); return -1; } m = slirpTftpMbufAlloc(pData); if (!m) return -1; m->m_data += if_maxlinkhdr; m->m_pkthdr.header = mtod(m, void *); pTftpIpHeader = mtod(m, PTFTPIPHDR); m->m_len = sizeof(TFTPIPHDR); pTftpIpHeader->u16TftpOpType = RT_H2N_U16_C(TFTP_DATA); pTftpIpHeader->Core.u16TftpOpCode = RT_H2N_U16(pTftpSession->cTftpAck); rc = tftpReadDataBlock(pData, pTftpSession, (uint8_t *)&pTftpIpHeader->Core.u16TftpOpCode + sizeof(uint16_t), &cbRead); if (RT_SUCCESS(rc)) { pTftpSession->cbTransfered += cbRead; m->m_len += cbRead; tftpSend(pData, pTftpSession, m, pcTftpIpHeaderRecv); if (cbRead > 0) tftpSessionUpdate(pData, pTftpSession); else tftpSessionTerminate(pTftpSession); } else { m_freem(pData, m); tftpSendError(pData, pTftpSession, 1, "File not found", pcTftpIpHeaderRecv); /* send "file not found" error back */ return -1; } return 0; }
static int tftp_send_error(PNATState pData, struct tftp_session *spt, u_int16_t errorcode, const char *msg, struct tftp_t *recv_tp) { struct sockaddr_in saddr, daddr; struct mbuf *m; struct tftp_t *tp; int nobytes; m = slirpTftpMbufAlloc(pData); if (!m) return -1; m->m_data += if_maxlinkhdr; m->m_pkthdr.header = mtod(m, void *); tp = (void *)m->m_data; m->m_data += sizeof(struct udpiphdr); tp->tp_op = RT_H2N_U16_C(TFTP_ERROR); tp->x.tp_error.tp_error_code = RT_H2N_U16(errorcode); strcpy((char *)tp->x.tp_error.tp_msg, msg); saddr.sin_addr = recv_tp->ip.ip_dst; saddr.sin_port = recv_tp->udp.uh_dport; daddr.sin_addr = spt->client_ip; daddr.sin_port = spt->client_port; nobytes = 2; m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) - sizeof(struct ip) - sizeof(struct udphdr); udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY); tftp_session_terminate(spt); return 0; }
DECLINLINE(int) tftpSendOACK(PNATState pData, PTFTPSESSION pTftpSession, PCTFTPIPHDR pcTftpIpHeaderRecv) { struct mbuf *m; PTFTPIPHDR pTftpIpHeader; int rc = VINF_SUCCESS; rc = tftpSessionEvaluateOptions(pData, pTftpSession); if (RT_FAILURE(rc)) { tftpSendError(pData, pTftpSession, 2, "Internal Error (blksize evaluation)", pcTftpIpHeaderRecv); LogFlowFuncLeave(); return -1; } m = slirpTftpMbufAlloc(pData); if (!m) return -1; m->m_data += if_maxlinkhdr; m->m_pkthdr.header = mtod(m, void *); pTftpIpHeader = mtod(m, PTFTPIPHDR); m->m_len = sizeof(TFTPIPHDR) - sizeof(uint16_t); /* no u16TftpOpCode */ pTftpIpHeader->u16TftpOpType = RT_H2N_U16_C(TFTP_OACK); if (pTftpSession->OptionBlkSize.fRequested) { if (pTftpSession->OptionBlkSize.u64Value > UINT16_MAX) rc = VERR_INVALID_PARAMETER; else rc = tftpAddOptionToOACK(pData, m, "blksize", pTftpSession->OptionBlkSize.u64Value); } if ( RT_SUCCESS(rc) && pTftpSession->OptionTSize.fRequested) rc = tftpAddOptionToOACK(pData, m, "tsize", pTftpSession->OptionTSize.u64Value); rc = tftpSend(pData, pTftpSession, m, pcTftpIpHeaderRecv); return RT_SUCCESS(rc) ? 0 : -1; }
/** * Deal with ARP queries. * * @returns true if ARP. * * @param pSession The support driver session. * @param hIf The internal network interface handle. * @param pBuf The internal network interface buffer. * @param pMacAddr Our MAC address. * @param IPv4Addr Our IPv4 address. */ bool VBoxNetArpHandleIt(PSUPDRVSESSION pSession, INTNETIFHANDLE hIf, PINTNETBUF pBuf, PCRTMAC pMacAddr, RTNETADDRIPV4 IPv4Addr) { /* * Valid IntNet Ethernet frame? Skip GSO, no ARP in there. */ PCINTNETHDR pHdr = IntNetRingGetNextFrameToRead(&pBuf->Recv); if ( !pHdr || pHdr->u16Type != INTNETHDR_TYPE_FRAME) return false; size_t cbFrame = pHdr->cbFrame; const void *pvFrame = IntNetHdrGetFramePtr(pHdr, pBuf); PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvFrame; /* * Arp frame? */ if (pEthHdr->EtherType != RT_H2N_U16_C(RTNET_ETHERTYPE_ARP)) return false; if ( ( pEthHdr->DstMac.au16[0] != 0xffff || pEthHdr->DstMac.au16[1] != 0xffff || pEthHdr->DstMac.au16[2] != 0xffff) && ( pEthHdr->DstMac.au16[0] != pMacAddr->au16[0] || pEthHdr->DstMac.au16[1] != pMacAddr->au16[1] || pEthHdr->DstMac.au16[2] != pMacAddr->au16[2]) ) return false; if (cbFrame < sizeof(RTNETARPIPV4) + sizeof(RTNETETHERHDR)) return false; PCRTNETARPHDR pArpHdr = (PCRTNETARPHDR)(pEthHdr + 1); if (pArpHdr->ar_htype != RT_H2N_U16_C(RTNET_ARP_ETHER)) return false; if (pArpHdr->ar_hlen != sizeof(RTMAC)) return false; if (pArpHdr->ar_ptype != RT_H2N_U16_C(RTNET_ETHERTYPE_IPV4)) return false; if (pArpHdr->ar_plen != sizeof(RTNETADDRIPV4)) return false; /* It's ARP, alright. Anything we need to do something about. */ PCRTNETARPIPV4 pArp = (PCRTNETARPIPV4)pArpHdr; switch (pArp->Hdr.ar_oper) { case RT_H2N_U16_C(RTNET_ARPOP_REQUEST): case RT_H2N_U16_C(RTNET_ARPOP_REVREQUEST): case RT_H2N_U16_C(RTNET_ARPOP_INVREQUEST): break; default: return true; } /* * Deal with the queries. */ RTNETARPIPV4 Reply; switch (pArp->Hdr.ar_oper) { /* 'Who has ar_tpa? Tell ar_spa.' */ case RT_H2N_U16_C(RTNET_ARPOP_REQUEST): if (pArp->ar_tpa.u != IPv4Addr.u) return true; Reply.Hdr.ar_oper = RT_H2N_U16_C(RTNET_ARPOP_REPLY); break; case RT_H2N_U16_C(RTNET_ARPOP_REVREQUEST): if ( pArp->ar_tha.au16[0] != pMacAddr->au16[0] || pArp->ar_tha.au16[1] != pMacAddr->au16[1] || pArp->ar_tha.au16[2] != pMacAddr->au16[2]) return true; Reply.Hdr.ar_oper = RT_H2N_U16_C(RTNET_ARPOP_REVREPLY); break; case RT_H2N_U16_C(RTNET_ARPOP_INVREQUEST): /** @todo RTNET_ARPOP_INVREQUEST */ return true; //Reply.Hdr.ar_oper = RT_H2N_U16_C(RTNET_ARPOP_INVREPLY); //break; } /* * Complete the reply and send it. */ Reply.Hdr.ar_htype = RT_H2N_U16_C(RTNET_ARP_ETHER); Reply.Hdr.ar_ptype = RT_H2N_U16_C(RTNET_ETHERTYPE_IPV4); Reply.Hdr.ar_hlen = sizeof(RTMAC); Reply.Hdr.ar_plen = sizeof(RTNETADDRIPV4); Reply.ar_sha = *pMacAddr; Reply.ar_spa = IPv4Addr; Reply.ar_tha = pArp->ar_sha; Reply.ar_tpa = pArp->ar_spa; RTNETETHERHDR EthHdr; EthHdr.DstMac = pArp->ar_sha; EthHdr.SrcMac = *pMacAddr; EthHdr.EtherType = RT_H2N_U16_C(RTNET_ETHERTYPE_ARP); uint8_t abTrailer[60 - sizeof(Reply) - sizeof(EthHdr)]; memset(abTrailer, '\0', sizeof(abTrailer)); INTNETSEG aSegs[3]; aSegs[0].cb = sizeof(EthHdr); aSegs[0].pv = &EthHdr; aSegs[1].pv = &Reply; aSegs[1].cb = sizeof(Reply); aSegs[2].pv = &abTrailer[0]; aSegs[2].cb = sizeof(abTrailer); VBoxNetIntIfSend(pSession, hIf, pBuf, RT_ELEMENTS(aSegs), &aSegs[0], true /* fFlush */); return true; }
int main() { RTTEST hTest; int rc = RTTestInitAndCreate("tstRTStrFormat", &hTest); if (rc) return rc; RTTestBanner(hTest); uint32_t u32 = 0x010; uint64_t u64 = 0x100; #define BUF_SIZE 120 char *pszBuf = (char *)RTTestGuardedAllocHead(hTest, BUF_SIZE); char *pszBuf2 = (char *)RTTestGuardedAllocHead(hTest, BUF_SIZE); RTTestSub(hTest, "Basics"); /* simple */ size_t cch = RTStrPrintf(pszBuf, BUF_SIZE, "u32=%d u64=%lld u64=%#llx", u32, u64, u64); if (strcmp(pszBuf, "u32=16 u64=256 u64=0x100")) { RTTestIFailed("error: '%s'\n" "wanted 'u32=16 u64=256 u64=0x100'\n", pszBuf); } /* just big. */ u64 = UINT64_C(0x7070605040302010); cch = RTStrPrintf(pszBuf, BUF_SIZE, "u64=%#llx 42=%d u64=%lld 42=%d", u64, 42, u64, 42); if (strcmp(pszBuf, "u64=0x7070605040302010 42=42 u64=8102081627430068240 42=42")) { RTTestIFailed("error: '%s'\n" "wanted 'u64=0x8070605040302010 42=42 u64=8102081627430068240 42=42'\n", pszBuf); RTTestIPrintf(RTTESTLVL_FAILURE, "%d\n", (int)(u64 % 10)); } /* huge and negative. */ u64 = UINT64_C(0x8070605040302010); cch = RTStrPrintf(pszBuf, BUF_SIZE, "u64=%#llx 42=%d u64=%llu 42=%d u64=%lld 42=%d", u64, 42, u64, 42, u64, 42); /* Not sure if this is the correct decimal representation... But both */ if (strcmp(pszBuf, "u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42")) { RTTestIFailed("error: '%s'\n" "wanted 'u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42'\n", pszBuf); RTTestIPrintf(RTTESTLVL_FAILURE, "%d\n", (int)(u64 % 10)); } /* 64-bit value bug. */ u64 = 0xa0000000; cch = RTStrPrintf(pszBuf, BUF_SIZE, "u64=%#llx 42=%d u64=%lld 42=%d", u64, 42, u64, 42); if (strcmp(pszBuf, "u64=0xa0000000 42=42 u64=2684354560 42=42")) RTTestIFailed("error: '%s'\n" "wanted 'u64=0xa0000000 42=42 u64=2684354560 42=42'\n", pszBuf); /* uuid */ RTUUID Uuid; RTUuidCreate(&Uuid); char szCorrect[RTUUID_STR_LENGTH]; RTUuidToStr(&Uuid, szCorrect, sizeof(szCorrect)); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%RTuuid", &Uuid); if (strcmp(pszBuf, szCorrect)) RTTestIFailed("error: '%s'\n" "expected: '%s'\n", pszBuf, szCorrect); /* * Nested */ RTTestSub(hTest, "Nested (%N)"); testNested(__LINE__, "42 2684354560 42 asdf 42", "42 %u 42 %s 42", 2684354560U, "asdf"); testNested(__LINE__, "", ""); /* * allocation */ RTTestSub(hTest, "RTStrAPrintf"); char *psz = (char *)~0; int cch2 = RTStrAPrintf(&psz, "Hey there! %s%s", "This is a test", "!"); if (cch2 < 0) RTTestIFailed("RTStrAPrintf failed, cch2=%d\n", cch2); else if (strcmp(psz, "Hey there! This is a test!")) RTTestIFailed("RTStrAPrintf failed\n" "got : '%s'\n" "wanted: 'Hey there! This is a test!'\n", psz); else if ((int)strlen(psz) != cch2) RTTestIFailed("RTStrAPrintf failed, cch2 == %d expected %u\n", cch2, strlen(psz)); RTStrFree(psz); #define CHECK42(fmt, arg, out) \ do { \ cch = RTStrPrintf(pszBuf, BUF_SIZE, fmt " 42=%d " fmt " 42=%d", arg, 42, arg, 42); \ if (strcmp(pszBuf, out " 42=42 " out " 42=42")) \ RTTestIFailed("at line %d: format '%s'\n" \ " output: '%s'\n" \ " wanted: '%s'\n", \ __LINE__, fmt, pszBuf, out " 42=42 " out " 42=42"); \ else if (cch != sizeof(out " 42=42 " out " 42=42") - 1) \ RTTestIFailed("at line %d: Invalid length %d returned, expected %u!\n", \ __LINE__, cch, sizeof(out " 42=42 " out " 42=42") - 1); \ } while (0) #define CHECKSTR(Correct) \ if (strcmp(pszBuf, Correct)) \ RTTestIFailed("error: '%s'\n" \ "expected: '%s'\n", pszBuf, Correct); \ /* * Runtime extensions. */ RTTestSub(hTest, "Runtime format types (%R*)"); CHECK42("%RGi", (RTGCINT)127, "127"); CHECK42("%RGi", (RTGCINT)-586589, "-586589"); CHECK42("%RGp", (RTGCPHYS)0x0000000044505045, "0000000044505045"); CHECK42("%RGp", ~(RTGCPHYS)0, "ffffffffffffffff"); CHECK42("%RGu", (RTGCUINT)586589, "586589"); CHECK42("%RGu", (RTGCUINT)1, "1"); CHECK42("%RGu", (RTGCUINT)3000000000U, "3000000000"); #if GC_ARCH_BITS == 32 CHECK42("%RGv", (RTGCUINTPTR)0, "00000000"); CHECK42("%RGv", ~(RTGCUINTPTR)0, "ffffffff"); CHECK42("%RGv", (RTGCUINTPTR)0x84342134, "84342134"); #else CHECK42("%RGv", (RTGCUINTPTR)0, "0000000000000000"); CHECK42("%RGv", ~(RTGCUINTPTR)0, "ffffffffffffffff"); CHECK42("%RGv", (RTGCUINTPTR)0x84342134, "0000000084342134"); #endif CHECK42("%RGx", (RTGCUINT)0x234, "234"); CHECK42("%RGx", (RTGCUINT)0xffffffff, "ffffffff"); CHECK42("%RRv", (RTRCUINTPTR)0, "00000000"); CHECK42("%RRv", ~(RTRCUINTPTR)0, "ffffffff"); CHECK42("%RRv", (RTRCUINTPTR)0x84342134, "84342134"); CHECK42("%RHi", (RTHCINT)127, "127"); CHECK42("%RHi", (RTHCINT)-586589, "-586589"); CHECK42("%RHp", (RTHCPHYS)0x0000000044505045, "0000000044505045"); CHECK42("%RHp", ~(RTHCPHYS)0, "ffffffffffffffff"); CHECK42("%RHu", (RTHCUINT)586589, "586589"); CHECK42("%RHu", (RTHCUINT)1, "1"); CHECK42("%RHu", (RTHCUINT)3000000000U, "3000000000"); if (sizeof(void*) == 8) { CHECK42("%RHv", (RTHCUINTPTR)0, "0000000000000000"); CHECK42("%RHv", ~(RTHCUINTPTR)0, "ffffffffffffffff"); CHECK42("%RHv", (RTHCUINTPTR)0x84342134, "0000000084342134"); } else { CHECK42("%RHv", (RTHCUINTPTR)0, "00000000"); CHECK42("%RHv", ~(RTHCUINTPTR)0, "ffffffff"); CHECK42("%RHv", (RTHCUINTPTR)0x84342134, "84342134"); } CHECK42("%RHx", (RTHCUINT)0x234, "234"); CHECK42("%RHx", (RTHCUINT)0xffffffff, "ffffffff"); CHECK42("%RI16", (int16_t)1, "1"); CHECK42("%RI16", (int16_t)-16384, "-16384"); CHECK42("%RI32", (int32_t)1123, "1123"); CHECK42("%RI32", (int32_t)-86596, "-86596"); CHECK42("%RI64", (int64_t)112345987345LL, "112345987345"); CHECK42("%RI64", (int64_t)-8659643985723459LL, "-8659643985723459"); CHECK42("%RI8", (int8_t)1, "1"); CHECK42("%RI8", (int8_t)-128, "-128"); CHECK42("%Rbn", "file.c", "file.c"); CHECK42("%Rbn", "foo/file.c", "file.c"); CHECK42("%Rbn", "/foo/file.c", "file.c"); CHECK42("%Rbn", "/dir/subdir/", "subdir/"); CHECK42("%Rfn", "function", "function"); CHECK42("%Rfn", "void function(void)", "function"); CHECK42("%RTfile", (RTFILE)127, "127"); CHECK42("%RTfile", (RTFILE)12341234, "12341234"); CHECK42("%RTfmode", (RTFMODE)0x123403, "00123403"); CHECK42("%RTfoff", (RTFOFF)12342312, "12342312"); CHECK42("%RTfoff", (RTFOFF)-123123123, "-123123123"); CHECK42("%RTfoff", (RTFOFF)858694596874568LL, "858694596874568"); RTFAR16 fp16; fp16.off = 0x34ff; fp16.sel = 0x0160; CHECK42("%RTfp16", fp16, "0160:34ff"); RTFAR32 fp32; fp32.off = 0xff094030; fp32.sel = 0x0168; CHECK42("%RTfp32", fp32, "0168:ff094030"); RTFAR64 fp64; fp64.off = 0xffff003401293487ULL; fp64.sel = 0x0ff8; CHECK42("%RTfp64", fp64, "0ff8:ffff003401293487"); fp64.off = 0x0; fp64.sel = 0x0; CHECK42("%RTfp64", fp64, "0000:0000000000000000"); CHECK42("%RTgid", (RTGID)-1, "-1"); CHECK42("%RTgid", (RTGID)1004, "1004"); CHECK42("%RTino", (RTINODE)0, "0000000000000000"); CHECK42("%RTino", (RTINODE)0x123412341324ULL, "0000123412341324"); CHECK42("%RTint", (RTINT)127, "127"); CHECK42("%RTint", (RTINT)-586589, "-586589"); CHECK42("%RTint", (RTINT)-23498723, "-23498723"); CHECK42("%RTiop", (RTIOPORT)0x3c4, "03c4"); CHECK42("%RTiop", (RTIOPORT)0xffff, "ffff"); RTMAC Mac; Mac.au8[0] = 0; Mac.au8[1] = 0x1b; Mac.au8[2] = 0x21; Mac.au8[3] = 0x0a; Mac.au8[4] = 0x1d; Mac.au8[5] = 0xd9; CHECK42("%RTmac", &Mac, "00:1b:21:0a:1d:d9"); Mac.au16[0] = 0xffff; Mac.au16[1] = 0xffff; Mac.au16[2] = 0xffff; CHECK42("%RTmac", &Mac, "ff:ff:ff:ff:ff:ff"); RTNETADDRIPV4 Ipv4Addr; Ipv4Addr.u = RT_H2N_U32_C(0xf040d003); CHECK42("%RTnaipv4", Ipv4Addr.u, "240.64.208.3"); Ipv4Addr.u = RT_H2N_U32_C(0xffffffff); CHECK42("%RTnaipv4", Ipv4Addr.u, "255.255.255.255"); RTNETADDRIPV6 Ipv6Addr; /* any */ memset(&Ipv6Addr, 0, sizeof(Ipv6Addr)); CHECK42("%RTnaipv6", &Ipv6Addr, "::"); /* loopback */ Ipv6Addr.au8[15] = 1; CHECK42("%RTnaipv6", &Ipv6Addr, "::1"); /* IPv4-compatible */ Ipv6Addr.au8[12] = 1; Ipv6Addr.au8[13] = 1; Ipv6Addr.au8[14] = 1; Ipv6Addr.au8[15] = 1; CHECK42("%RTnaipv6", &Ipv6Addr, "::1.1.1.1"); /* IPv4-mapped */ Ipv6Addr.au16[5] = RT_H2N_U16_C(0xffff); CHECK42("%RTnaipv6", &Ipv6Addr, "::ffff:1.1.1.1"); /* IPv4-translated */ Ipv6Addr.au16[4] = RT_H2N_U16_C(0xffff); Ipv6Addr.au16[5] = RT_H2N_U16_C(0x0000); CHECK42("%RTnaipv6", &Ipv6Addr, "::ffff:0:1.1.1.1"); /* single zero word is not abbreviated, leading zeroes are not printed */ Ipv6Addr.au16[0] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0001); Ipv6Addr.au16[2] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0001); Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[5] = RT_H2N_U16_C(0x0001); Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[7] = RT_H2N_U16_C(0x0001); CHECK42("%RTnaipv6", &Ipv6Addr, "0:1:0:1:0:1:0:1"); /* longest run is abbreviated (here: at the beginning) */ Ipv6Addr.au16[0] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[2] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0001); Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[5] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0001); Ipv6Addr.au16[7] = RT_H2N_U16_C(0x0000); CHECK42("%RTnaipv6", &Ipv6Addr, "::1:0:0:1:0"); /* longest run is abbreviated (here: first) */ Ipv6Addr.au16[0] = RT_H2N_U16_C(0x0001); Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[2] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0001); Ipv6Addr.au16[5] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[7] = RT_H2N_U16_C(0x0001); CHECK42("%RTnaipv6", &Ipv6Addr, "1::1:0:0:1"); /* longest run is abbreviated (here: second) */ Ipv6Addr.au16[0] = RT_H2N_U16_C(0x0001); Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[2] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0001); Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[5] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[7] = RT_H2N_U16_C(0x0001); CHECK42("%RTnaipv6", &Ipv6Addr, "1:0:0:1::1"); /* longest run is abbreviated (here: at the end) */ Ipv6Addr.au16[0] = RT_H2N_U16_C(0x0001); Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[2] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0001); Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[5] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[7] = RT_H2N_U16_C(0x0000); CHECK42("%RTnaipv6", &Ipv6Addr, "1:0:0:1::"); /* first of the two runs of equal length is abbreviated */ Ipv6Addr.au16[0] = RT_H2N_U16_C(0x2001); Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0db8); Ipv6Addr.au16[2] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0001); Ipv6Addr.au16[5] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[7] = RT_H2N_U16_C(0x0001); CHECK42("%RTnaipv6", &Ipv6Addr, "2001:db8::1:0:0:1"); Ipv6Addr.au16[0] = RT_H2N_U16_C(0x2001); Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0db8); Ipv6Addr.au16[2] = RT_H2N_U16_C(0x85a3); Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0000); Ipv6Addr.au16[5] = RT_H2N_U16_C(0x8a2e); Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0370); Ipv6Addr.au16[7] = RT_H2N_U16_C(0x7334); CHECK42("%RTnaipv6", &Ipv6Addr, "2001:db8:85a3::8a2e:370:7334"); Ipv6Addr.au64[0] = UINT64_MAX; Ipv6Addr.au64[1] = UINT64_MAX; CHECK42("%RTnaipv6", &Ipv6Addr, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); RTNETADDR NetAddr; memset(&NetAddr, 0, sizeof(NetAddr)); /* plain IPv6 address if port is not specified */ NetAddr.enmType = RTNETADDRTYPE_IPV6; NetAddr.uAddr.au16[0] = RT_H2N_U16_C(0x0001); NetAddr.uAddr.au16[7] = RT_H2N_U16_C(0x0001); NetAddr.uPort = RTNETADDR_PORT_NA; CHECK42("%RTnaddr", &NetAddr, "1::1"); /* square brackets around IPv6 address if port is specified */ NetAddr.uPort = 1; CHECK42("%RTnaddr", &NetAddr, "[1::1]:1"); CHECK42("%RTproc", (RTPROCESS)0xffffff, "00ffffff"); CHECK42("%RTproc", (RTPROCESS)0x43455443, "43455443"); if (sizeof(RTUINTPTR) == 8) { CHECK42("%RTptr", (RTUINTPTR)0, "0000000000000000"); CHECK42("%RTptr", ~(RTUINTPTR)0, "ffffffffffffffff"); CHECK42("%RTptr", (RTUINTPTR)0x84342134, "0000000084342134"); } else { CHECK42("%RTptr", (RTUINTPTR)0, "00000000"); CHECK42("%RTptr", ~(RTUINTPTR)0, "ffffffff"); CHECK42("%RTptr", (RTUINTPTR)0x84342134, "84342134"); } if (sizeof(RTCCUINTREG) == 8) { CHECK42("%RTreg", (RTCCUINTREG)0, "0000000000000000"); CHECK42("%RTreg", ~(RTCCUINTREG)0, "ffffffffffffffff"); CHECK42("%RTreg", (RTCCUINTREG)0x84342134, "0000000084342134"); CHECK42("%RTreg", (RTCCUINTREG)0x23484342134ULL, "0000023484342134"); } else { CHECK42("%RTreg", (RTCCUINTREG)0, "00000000"); CHECK42("%RTreg", ~(RTCCUINTREG)0, "ffffffff"); CHECK42("%RTreg", (RTCCUINTREG)0x84342134, "84342134"); } CHECK42("%RTsel", (RTSEL)0x543, "0543"); CHECK42("%RTsel", (RTSEL)0xf8f8, "f8f8"); if (sizeof(RTSEMEVENT) == 8) { CHECK42("%RTsem", (RTSEMEVENT)0, "0000000000000000"); CHECK42("%RTsem", (RTSEMEVENT)0x23484342134ULL, "0000023484342134"); } else { CHECK42("%RTsem", (RTSEMEVENT)0, "00000000"); CHECK42("%RTsem", (RTSEMEVENT)0x84342134, "84342134"); } CHECK42("%RTsock", (RTSOCKET)12234, "12234"); CHECK42("%RTsock", (RTSOCKET)584854543, "584854543"); if (sizeof(RTTHREAD) == 8) { CHECK42("%RTthrd", (RTTHREAD)0, "0000000000000000"); CHECK42("%RTthrd", (RTTHREAD)~(uintptr_t)0, "ffffffffffffffff"); CHECK42("%RTthrd", (RTTHREAD)0x63484342134ULL, "0000063484342134"); } else { CHECK42("%RTthrd", (RTTHREAD)0, "00000000"); CHECK42("%RTthrd", (RTTHREAD)~(uintptr_t)0, "ffffffff"); CHECK42("%RTthrd", (RTTHREAD)0x54342134, "54342134"); } CHECK42("%RTuid", (RTUID)-2, "-2"); CHECK42("%RTuid", (RTUID)90344, "90344"); CHECK42("%RTuint", (RTUINT)584589, "584589"); CHECK42("%RTuint", (RTUINT)3, "3"); CHECK42("%RTuint", (RTUINT)2400000000U, "2400000000"); RTUuidCreate(&Uuid); RTUuidToStr(&Uuid, szCorrect, sizeof(szCorrect)); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%RTuuid", &Uuid); if (strcmp(pszBuf, szCorrect)) RTTestIFailed("error: '%s'\n" "expected: '%s'\n", pszBuf, szCorrect); CHECK42("%RTxint", (RTUINT)0x2345, "2345"); CHECK42("%RTxint", (RTUINT)0xffff8fff, "ffff8fff"); CHECK42("%RU16", (uint16_t)7, "7"); CHECK42("%RU16", (uint16_t)46384, "46384"); CHECK42("%RU32", (uint32_t)1123, "1123"); CHECK42("%RU32", (uint32_t)86596, "86596"); CHECK42("%4RU32", (uint32_t)42, " 42"); CHECK42("%04RU32", (uint32_t)42, "0042"); CHECK42("%.4RU32", (uint32_t)42, "0042"); CHECK42("%RU64", (uint64_t)112345987345ULL, "112345987345"); CHECK42("%RU64", (uint64_t)8659643985723459ULL, "8659643985723459"); CHECK42("%14RU64", (uint64_t)4, " 4"); CHECK42("%014RU64", (uint64_t)4, "00000000000004"); CHECK42("%.14RU64", (uint64_t)4, "00000000000004"); CHECK42("%RU8", (uint8_t)1, "1"); CHECK42("%RU8", (uint8_t)254, "254"); CHECK42("%RU8", 256, "0"); CHECK42("%RX16", (uint16_t)0x7, "7"); CHECK42("%RX16", 0x46384, "6384"); CHECK42("%RX32", (uint32_t)0x1123, "1123"); CHECK42("%RX32", (uint32_t)0x49939493, "49939493"); CHECK42("%RX64", UINT64_C(0x348734), "348734"); CHECK42("%RX64", UINT64_C(0x12312312312343f), "12312312312343f"); CHECK42("%5RX64", UINT64_C(0x42), " 42"); CHECK42("%05RX64", UINT64_C(0x42), "00042"); CHECK42("%.5RX64", UINT64_C(0x42), "00042"); CHECK42("%.05RX64", UINT64_C(0x42), "00042"); /* '0' is ignored */ CHECK42("%RX8", (uint8_t)1, "1"); CHECK42("%RX8", (uint8_t)0xff, "ff"); CHECK42("%RX8", 0x100, "0"); /* * Thousand separators. */ RTTestSub(hTest, "Thousand Separators (%'*)"); RTStrFormatNumber(pszBuf, 1, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1"); memset(pszBuf, '!', BUF_SIZE); RTStrFormatNumber(pszBuf, 10, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("10"); memset(pszBuf, '!', BUF_SIZE); RTStrFormatNumber(pszBuf, 100, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("100"); memset(pszBuf, '!', BUF_SIZE); RTStrFormatNumber(pszBuf, 1000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1 000"); memset(pszBuf, '!', BUF_SIZE); RTStrFormatNumber(pszBuf, 10000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("10 000"); memset(pszBuf, '!', BUF_SIZE); RTStrFormatNumber(pszBuf, 100000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("100 000"); memset(pszBuf, '!', BUF_SIZE); RTStrFormatNumber(pszBuf, 1000000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1 000 000"); memset(pszBuf, '!', BUF_SIZE); CHECK42("%'u", 1, "1"); CHECK42("%'u", 10, "10"); CHECK42("%'u", 100, "100"); CHECK42("%'u", 1000, "1 000"); CHECK42("%'u", 10000, "10 000"); CHECK42("%'u", 100000, "100 000"); CHECK42("%'u", 1000000, "1 000 000"); CHECK42("%'RU64", _1T, "1 099 511 627 776"); CHECK42("%'RU64", _1E, "1 152 921 504 606 846 976"); /* * String formatting. */ RTTestSub(hTest, "String formatting (%s)"); // 0 1 2 3 4 5 6 7 // 0....5....0....5....0....5....0....5....0....5....0....5....0....5....0 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10s %-30s %s", "cmd", "args", "description"); CHECKSTR("cmd args description"); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10s %-30s %s", "cmd", "", "description"); CHECKSTR("cmd description"); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%*s", 0, ""); CHECKSTR(""); /* automatic conversions. */ static RTUNICP s_usz1[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0 }; //assumes ascii. static RTUTF16 s_wsz1[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0 }; //assumes ascii. cch = RTStrPrintf(pszBuf, BUF_SIZE, "%ls", s_wsz1); CHECKSTR("hello world"); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Ls", s_usz1); CHECKSTR("hello world"); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.5ls", s_wsz1); CHECKSTR("hello"); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.5Ls", s_usz1); CHECKSTR("hello"); /* * Unicode string formatting. */ RTTestSub(hTest, "Unicode string formatting (%ls)"); static RTUTF16 s_wszEmpty[] = { 0 }; //assumes ascii. static RTUTF16 s_wszCmd[] = { 'c', 'm', 'd', 0 }; //assumes ascii. static RTUTF16 s_wszArgs[] = { 'a', 'r', 'g', 's', 0 }; //assumes ascii. static RTUTF16 s_wszDesc[] = { 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'i', 'o', 'n', 0 }; //assumes ascii. // 0 1 2 3 4 5 6 7 // 0....5....0....5....0....5....0....5....0....5....0....5....0....5....0 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10ls %-30ls %ls", s_wszCmd, s_wszArgs, s_wszDesc); CHECKSTR("cmd args description"); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10ls %-30ls %ls", s_wszCmd, s_wszEmpty, s_wszDesc); CHECKSTR("cmd description"); #if 0 static RTUNICP s_usz2[] = { 0xc5, 0xc6, 0xf8, 0 }; static RTUTF16 s_wsz2[] = { 0xc5, 0xc6, 0xf8, 0 }; static char s_sz2[] = { 0xc5, 0xc6, 0xf8, 0 };///@todo multibyte tests. cch = RTStrPrintf(pszBuf, BUF_SIZE, "%ls", s_wsz2); CHECKSTR(s_sz2); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Ls", s_usz2); CHECKSTR(s_sz2); #endif /* * Hex formatting. */ RTTestSub(hTest, "Hex dump formatting (%Rhx*)"); static uint8_t const s_abHex1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }; cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.1Rhxs", s_abHex1); CHECKSTR("00"); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.2Rhxs", s_abHex1); CHECKSTR("00 01"); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Rhxs", s_abHex1); CHECKSTR("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f"); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.*Rhxs", sizeof(s_abHex1), s_abHex1); CHECKSTR("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14"); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%4.*Rhxs", sizeof(s_abHex1), s_abHex1); CHECKSTR("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14"); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%1.*Rhxs", sizeof(s_abHex1), s_abHex1); CHECKSTR("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14"); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%256.*Rhxs", sizeof(s_abHex1), s_abHex1); CHECKSTR("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14"); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%4.8Rhxd", s_abHex1); RTStrPrintf(pszBuf2, BUF_SIZE, "%p 0000: 00 01 02 03 ....\n" "%p 0004: 04 05 06 07 ....", &s_abHex1[0], &s_abHex1[4]); CHECKSTR(pszBuf2); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%4.6Rhxd", s_abHex1); RTStrPrintf(pszBuf2, BUF_SIZE, "%p 0000: 00 01 02 03 ....\n" "%p 0004: 04 05 ..", &s_abHex1[0], &s_abHex1[4]); CHECKSTR(pszBuf2); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.*Rhxd", sizeof(s_abHex1), s_abHex1); RTStrPrintf(pszBuf2, BUF_SIZE, "%p 0000: 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n" "%p 0010: 10 11 12 13 14 ....." , &s_abHex1[0], &s_abHex1[0x10]); CHECKSTR(pszBuf2); /* * x86 register formatting. */ RTTestSub(hTest, "x86 register format types (%RAx86[*])"); CHECK42("%RAx86[cr0]", UINT64_C(0x80000011), "80000011{PE,ET,PG}"); CHECK42("%RAx86[cr0]", UINT64_C(0x80000001), "80000001{PE,PG}"); CHECK42("%RAx86[cr0]", UINT64_C(0x00000001), "00000001{PE}"); CHECK42("%RAx86[cr0]", UINT64_C(0x80000000), "80000000{PG}"); CHECK42("%RAx86[cr4]", UINT64_C(0x80000001), "80000001{VME,unkn=80000000}"); CHECK42("%#RAx86[cr4]", UINT64_C(0x80000001), "0x80000001{VME,unkn=0x80000000}"); /* * Custom types. */ RTTestSub(hTest, "Custom format types (%R[*])"); RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type3", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS); RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type3", (void *)((uintptr_t)TstType + 3)), VINF_SUCCESS); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3]", (void *)1); CHECKSTR("type3=1"); RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type1", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS); RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type1", (void *)((uintptr_t)TstType + 1)), VINF_SUCCESS); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1]", (void *)1, (void *)2); CHECKSTR("type3=1 type1=2"); RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type4", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS); RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type4", (void *)((uintptr_t)TstType + 4)), VINF_SUCCESS); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4]", (void *)1, (void *)2, (void *)3); CHECKSTR("type3=1 type1=2 type4=3"); RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type2", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS); RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type2", (void *)((uintptr_t)TstType + 2)), VINF_SUCCESS); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4] %R[type2]", (void *)1, (void *)2, (void *)3, (void *)4); CHECKSTR("type3=1 type1=2 type4=3 type2=4"); RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type5", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS); RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type5", (void *)((uintptr_t)TstType + 5)), VINF_SUCCESS); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4] %R[type2] %R[type5]", (void *)1, (void *)2, (void *)3, (void *)4, (void *)5); CHECKSTR("type3=1 type1=2 type4=3 type2=4 type5=5"); RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type1", (void *)((uintptr_t)TstType + 1)), VINF_SUCCESS); RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type2", (void *)((uintptr_t)TstType + 2)), VINF_SUCCESS); RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type3", (void *)((uintptr_t)TstType + 3)), VINF_SUCCESS); RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type4", (void *)((uintptr_t)TstType + 4)), VINF_SUCCESS); RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type5", (void *)((uintptr_t)TstType + 5)), VINF_SUCCESS); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4] %R[type2] %R[type5]", (void *)10, (void *)20, (void *)30, (void *)40, (void *)50); CHECKSTR("type3=10 type1=20 type4=30 type2=40 type5=50"); RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type2"), VINF_SUCCESS); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4] %R[type5]", (void *)10, (void *)20, (void *)30, (void *)40); CHECKSTR("type3=10 type1=20 type4=30 type5=40"); RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type5"), VINF_SUCCESS); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4]", (void *)10, (void *)20, (void *)30); CHECKSTR("type3=10 type1=20 type4=30"); RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type4"), VINF_SUCCESS); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1]", (void *)10, (void *)20); CHECKSTR("type3=10 type1=20"); RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type1"), VINF_SUCCESS); cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3]", (void *)10); CHECKSTR("type3=10"); RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type3"), VINF_SUCCESS); /* * Summarize and exit. */ return RTTestSummaryAndDestroy(hTest); }
/* m->m_data points at ip packet header * m->m_len length ip packet * ip->ip_len length data (IPDU) */ void udp_input(PNATState pData, register struct mbuf *m, int iphlen) { register struct ip *ip; register struct udphdr *uh; int len; struct ip save_ip; struct socket *so; int ret; int ttl, tos; LogFlowFunc(("ENTER: m = %p, iphlen = %d\n", m, iphlen)); ip = mtod(m, struct ip *); Log2(("%RTnaipv4 iphlen = %d\n", ip->ip_dst, iphlen)); udpstat.udps_ipackets++; /* * Strip IP options, if any; should skip this, * make available to user, and use on returned packets, * but we don't yet have a way to check the checksum * with options still present. */ if (iphlen > sizeof(struct ip)) { ip_stripoptions(m, (struct mbuf *)0); iphlen = sizeof(struct ip); } /* * Get IP and UDP header together in first mbuf. */ ip = mtod(m, struct ip *); uh = (struct udphdr *)((caddr_t)ip + iphlen); /* * Make mbuf data length reflect UDP length. * If not enough data to reflect UDP length, drop. */ len = RT_N2H_U16((u_int16_t)uh->uh_ulen); Assert(ip->ip_len + iphlen == (ssize_t)m_length(m, NULL)); if (ip->ip_len != len) { if (len > ip->ip_len) { udpstat.udps_badlen++; Log3(("NAT: IP(id: %hd) has bad size\n", ip->ip_id)); goto bad_free_mbuf; } m_adj(m, len - ip->ip_len); ip->ip_len = len; } /* * Save a copy of the IP header in case we want restore it * for sending an ICMP error message in response. */ save_ip = *ip; save_ip.ip_len+= iphlen; /* tcp_input subtracts this */ /* * Checksum extended UDP header and data. */ if (udpcksum && uh->uh_sum) { memset(((struct ipovly *)ip)->ih_x1, 0, 9); ((struct ipovly *)ip)->ih_len = uh->uh_ulen; #if 0 /* keep uh_sum for ICMP reply */ uh->uh_sum = cksum(m, len + sizeof (struct ip)); if (uh->uh_sum) { #endif if (cksum(m, len + iphlen)) { udpstat.udps_badsum++; Log3(("NAT: IP(id: %hd) has bad (udp) cksum\n", ip->ip_id)); goto bad_free_mbuf; } } #if 0 } #endif /* * handle DHCP/BOOTP */ if (uh->uh_dport == RT_H2N_U16_C(BOOTP_SERVER)) { bootp_input(pData, m); goto done_free_mbuf; } LogFunc(("uh src: %RTnaipv4:%d, dst: %RTnaipv4:%d\n", ip->ip_src.s_addr, RT_N2H_U16(uh->uh_sport), ip->ip_dst.s_addr, RT_N2H_U16(uh->uh_dport))); /* * handle DNS host resolver without creating a socket */ if ( pData->fUseHostResolver && uh->uh_dport == RT_H2N_U16_C(53) && CTL_CHECK(ip->ip_dst.s_addr, CTL_DNS)) { struct sockaddr_in dst, src; src.sin_addr.s_addr = ip->ip_dst.s_addr; src.sin_port = uh->uh_dport; dst.sin_addr.s_addr = ip->ip_src.s_addr; dst.sin_port = uh->uh_sport; m_adj(m, sizeof(struct udpiphdr)); m = hostresolver(pData, m, ip->ip_src.s_addr, uh->uh_sport); if (m == NULL) goto done_free_mbuf; slirpMbufTagService(pData, m, CTL_DNS); udp_output2(pData, NULL, m, &src, &dst, IPTOS_LOWDELAY); LogFlowFuncLeave(); return; } /* * handle TFTP */ if ( uh->uh_dport == RT_H2N_U16_C(TFTP_SERVER) && CTL_CHECK(ip->ip_dst.s_addr, CTL_TFTP)) { if (pData->pvTftpSessions) slirpTftpInput(pData, m); goto done_free_mbuf; } /* * XXX: DNS proxy currently relies on the fact that each socket * only serves one request. */ if ( pData->fUseDnsProxy && CTL_CHECK(ip->ip_dst.s_addr, CTL_DNS) && (uh->uh_dport == RT_H2N_U16_C(53))) { so = NULL; goto new_socket; } /* * Locate pcb for datagram. */ so = udp_last_so; if ( so->so_lport != uh->uh_sport || so->so_laddr.s_addr != ip->ip_src.s_addr) { struct socket *tmp; for (tmp = udb.so_next; tmp != &udb; tmp = tmp->so_next) { if ( tmp->so_lport == uh->uh_sport && tmp->so_laddr.s_addr == ip->ip_src.s_addr) { so = tmp; break; } } if (tmp == &udb) so = NULL; else { udpstat.udpps_pcbcachemiss++; udp_last_so = so; } } new_socket: if (so == NULL) { /* * If there's no socket for this packet, * create one */ if ((so = socreate()) == NULL) { Log2(("NAT: IP(id: %hd) failed to create socket\n", ip->ip_id)); goto bad_free_mbuf; } /* * Setup fields */ so->so_laddr = ip->ip_src; so->so_lport = uh->uh_sport; so->so_iptos = ip->ip_tos; if (udp_attach(pData, so) <= 0) { Log2(("NAT: IP(id: %hd) udp_attach errno = %d (%s)\n", ip->ip_id, errno, strerror(errno))); sofree(pData, so); goto bad_free_mbuf; } /* udp_last_so = so; */ /* * XXXXX Here, check if it's in udpexec_list, * and if it is, do the fork_exec() etc. */ } so->so_faddr = ip->ip_dst; /* XXX */ so->so_fport = uh->uh_dport; /* XXX */ Assert(so->so_type == IPPROTO_UDP); /* * DNS proxy */ if ( pData->fUseDnsProxy && CTL_CHECK(ip->ip_dst.s_addr, CTL_DNS) && (uh->uh_dport == RT_H2N_U16_C(53))) { dnsproxy_query(pData, so, m, iphlen); goto done_free_mbuf; } iphlen += sizeof(struct udphdr); m->m_len -= iphlen; m->m_data += iphlen; ttl = ip->ip_ttl = save_ip.ip_ttl; if (ttl != so->so_sottl) { ret = setsockopt(so->s, IPPROTO_IP, IP_TTL, (char *)&ttl, sizeof(ttl)); if (RT_LIKELY(ret == 0)) so->so_sottl = ttl; } tos = save_ip.ip_tos; if (tos != so->so_sotos) { ret = setsockopt(so->s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(tos)); if (RT_LIKELY(ret == 0)) so->so_sotos = tos; } { /* * Different OSes have different socket options for DF. We * can't use IP_HDRINCL here as it's only valid for SOCK_RAW. */ # define USE_DF_OPTION(_Optname) \ const int dfopt = _Optname #if defined(IP_MTU_DISCOVER) USE_DF_OPTION(IP_MTU_DISCOVER); #elif defined(IP_DONTFRAG) /* Solaris 11+, FreeBSD */ USE_DF_OPTION(IP_DONTFRAG); #elif defined(IP_DONTFRAGMENT) /* Windows */ USE_DF_OPTION(IP_DONTFRAGMENT); #else USE_DF_OPTION(0); #endif if (dfopt) { int df = (save_ip.ip_off & IP_DF) != 0; #if defined(IP_MTU_DISCOVER) df = df ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT; #endif if (df != so->so_sodf) { ret = setsockopt(so->s, IPPROTO_IP, dfopt, (char *)&df, sizeof(df)); if (RT_LIKELY(ret == 0)) so->so_sodf = df; } } } if ( sosendto(pData, so, m) == -1 && ( !soIgnorableErrorCode(errno) && errno != ENOTCONN)) { m->m_len += iphlen; m->m_data -= iphlen; *ip = save_ip; Log2(("NAT: UDP tx errno = %d (%s) on sent to %RTnaipv4\n", errno, strerror(errno), ip->ip_dst)); icmp_error(pData, m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno)); so->so_m = NULL; LogFlowFuncLeave(); return; } if (so->so_m) m_freem(pData, so->so_m); /* used for ICMP if error on sorecvfrom */ /* restore the orig mbuf packet */ m->m_len += iphlen; m->m_data -= iphlen; *ip = save_ip; so->so_m = m; /* ICMP backup */ LogFlowFuncLeave(); return; bad_free_mbuf: Log2(("NAT: UDP(id: %hd) datagram to %RTnaipv4 with size(%d) claimed as bad\n", ip->ip_id, &ip->ip_dst, ip->ip_len)); done_free_mbuf: /* some services like bootp(built-in), dns(buildt-in) and dhcp don't need sockets * and create new m'buffers to send them to guest, so we'll free their incomming * buffers here. */ if (m != NULL) m_freem(pData, m); LogFlowFuncLeave(); return; }
/* m->m_data points at ip packet header * m->m_len length ip packet * ip->ip_len length data (IPDU) */ void udp_input(PNATState pData, register struct mbuf *m, int iphlen) { register struct ip *ip; register struct udphdr *uh; int len; struct ip save_ip; struct socket *so; int ret; int ttl; LogFlowFunc(("ENTER: m = %p, iphlen = %d\n", m, iphlen)); ip = mtod(m, struct ip *); Log2(("%RTnaipv4 iphlen = %d\n", ip->ip_dst, iphlen)); udpstat.udps_ipackets++; /* * Strip IP options, if any; should skip this, * make available to user, and use on returned packets, * but we don't yet have a way to check the checksum * with options still present. */ if (iphlen > sizeof(struct ip)) { ip_stripoptions(m, (struct mbuf *)0); iphlen = sizeof(struct ip); } /* * Get IP and UDP header together in first mbuf. */ ip = mtod(m, struct ip *); uh = (struct udphdr *)((caddr_t)ip + iphlen); /* * Make mbuf data length reflect UDP length. * If not enough data to reflect UDP length, drop. */ len = RT_N2H_U16((u_int16_t)uh->uh_ulen); Assert((ip->ip_len == len)); Assert((ip->ip_len + iphlen == m_length(m, NULL))); if (ip->ip_len != len) { if (len > ip->ip_len) { udpstat.udps_badlen++; Log3(("NAT: IP(id: %hd) has bad size\n", ip->ip_id)); } m_adj(m, len - ip->ip_len); ip->ip_len = len; } /* * Save a copy of the IP header in case we want restore it * for sending an ICMP error message in response. */ save_ip = *ip; save_ip.ip_len+= iphlen; /* tcp_input subtracts this */ /* * Checksum extended UDP header and data. */ if (udpcksum && uh->uh_sum) { memset(((struct ipovly *)ip)->ih_x1, 0, 9); ((struct ipovly *)ip)->ih_len = uh->uh_ulen; #if 0 /* keep uh_sum for ICMP reply */ uh->uh_sum = cksum(m, len + sizeof (struct ip)); if (uh->uh_sum) { #endif if (cksum(m, len + iphlen)) { udpstat.udps_badsum++; Log3(("NAT: IP(id: %hd) has bad (udp) cksum\n", ip->ip_id)); goto bad_free_mbuf; } } #if 0 } #endif /* * handle DHCP/BOOTP */ if (uh->uh_dport == RT_H2N_U16_C(BOOTP_SERVER)) { bootp_input(pData, m); goto done_free_mbuf; } LogFunc(("uh src: %RTnaipv4:%d, dst: %RTnaipv4:%d\n", ip->ip_src, RT_H2N_U16_C(uh->uh_sport), ip->ip_dst, RT_H2N_U16_C(uh->uh_dport))); if ( pData->fUseHostResolver && uh->uh_dport == RT_H2N_U16_C(53) && CTL_CHECK(ip->ip_dst.s_addr, CTL_DNS)) { struct sockaddr_in dst, src; src.sin_addr.s_addr = ip->ip_dst.s_addr; src.sin_port = uh->uh_dport; dst.sin_addr.s_addr = ip->ip_src.s_addr; dst.sin_port = uh->uh_sport; slirpMbufTagService(pData, m, CTL_DNS); /* udp_output2() expects a pointer to the body of UDP packet. */ m->m_data += sizeof(struct udpiphdr); m->m_len -= sizeof(struct udpiphdr); udp_output2(pData, NULL, m, &src, &dst, IPTOS_LOWDELAY); LogFlowFuncLeave(); return; } /* * handle TFTP */ if ( uh->uh_dport == RT_H2N_U16_C(TFTP_SERVER) && CTL_CHECK(ip->ip_dst.s_addr, CTL_TFTP)) { if (pData->pvTftpSessions) slirpTftpInput(pData, m); goto done_free_mbuf; } /* * Locate pcb for datagram. */ so = udp_last_so; if ( so->so_lport != uh->uh_sport || so->so_laddr.s_addr != ip->ip_src.s_addr) { struct socket *tmp; for (tmp = udb.so_next; tmp != &udb; tmp = tmp->so_next) { if ( tmp->so_lport == uh->uh_sport && tmp->so_laddr.s_addr == ip->ip_src.s_addr) { so = tmp; break; } } if (tmp == &udb) so = NULL; else { udpstat.udpps_pcbcachemiss++; udp_last_so = so; } } if (so == NULL) { /* * If there's no socket for this packet, * create one */ if ((so = socreate()) == NULL) { Log2(("NAT: IP(id: %hd) failed to create socket\n", ip->ip_id)); goto bad_free_mbuf; } if (udp_attach(pData, so) <= 0) { Log2(("NAT: IP(id: %hd) udp_attach errno = %d (%s)\n", ip->ip_id, errno, strerror(errno))); sofree(pData, so); goto bad_free_mbuf; } /* * Setup fields */ /* udp_last_so = so; */ so->so_laddr = ip->ip_src; so->so_lport = uh->uh_sport; so->so_iptos = ip->ip_tos; /* * XXXXX Here, check if it's in udpexec_list, * and if it is, do the fork_exec() etc. */ } so->so_faddr = ip->ip_dst; /* XXX */ so->so_fport = uh->uh_dport; /* XXX */ Assert(so->so_type == IPPROTO_UDP); /* * DNS proxy */ if ( pData->fUseDnsProxy && CTL_CHECK(ip->ip_dst.s_addr, CTL_DNS) && (uh->uh_dport == RT_H2N_U16_C(53))) { dnsproxy_query(pData, so, m, iphlen); goto done_free_mbuf; } iphlen += sizeof(struct udphdr); m->m_len -= iphlen; m->m_data += iphlen; ttl = ip->ip_ttl = save_ip.ip_ttl; ret = setsockopt(so->s, IPPROTO_IP, IP_TTL, (const char*)&ttl, sizeof(ttl)); if (ret < 0) LogRel(("NAT: Error (%s) occurred while setting TTL(%d) attribute " "of IP packet to socket %R[natsock]\n", strerror(errno), ip->ip_ttl, so)); if ( sosendto(pData, so, m) == -1 && ( !soIgnorableErrorCode(errno) && errno != ENOTCONN)) { m->m_len += iphlen; m->m_data -= iphlen; *ip = save_ip; Log2(("NAT: UDP tx errno = %d (%s) on sent to %RTnaipv4\n", errno, strerror(errno), ip->ip_dst)); #if 0 /* ICMP_SOURCEQUENCH haven't got any effect, the idea here * inform guest about the exosting NAT resources with assumption that * that guest reduce traffic. But it doesn't work */ if( errno == EAGAIN || errno == EWOULDBLOCK || errno == EINPROGRESS || errno == ENOTCONN) icmp_error(pData, m, ICMP_SOURCEQUENCH, 0, 1, strerror(errno)); else #endif icmp_error(pData, m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno)); so->so_m = NULL; LogFlowFuncLeave(); return; } if (so->so_m) m_freem(pData, so->so_m); /* used for ICMP if error on sorecvfrom */ /* restore the orig mbuf packet */ m->m_len += iphlen; m->m_data -= iphlen; *ip = save_ip; so->so_m = m; /* ICMP backup */ LogFlowFuncLeave(); return; bad_free_mbuf: Log2(("NAT: UDP(id: %hd) datagram to %RTnaipv4 with size(%d) claimed as bad\n", ip->ip_id, &ip->ip_dst, ip->ip_len)); done_free_mbuf: /* some services like bootp(built-in), dns(buildt-in) and dhcp don't need sockets * and create new m'buffers to send them to guest, so we'll free their incomming * buffers here. */ m_freem(pData, m); LogFlowFuncLeave(); return; }