// NAT enumeration void NmNatRefresh(HWND hWnd, SM_HUB *r) { LVB *b; RPC_ENUM_NAT t; UINT i; // Validate arguments if (hWnd == NULL || r == NULL) { return; } Zero(&t, sizeof(t)); StrCpy(t.HubName, sizeof(t.HubName), r->HubName); if (CALL(hWnd, ScEnumNAT(r->Rpc, &t)) == false) { Close(hWnd); return; } b = LvInsertStart(); for (i = 0;i < t.NumItem;i++) { RPC_ENUM_NAT_ITEM *e = &t.Items[i]; wchar_t tmp0[MAX_SIZE]; wchar_t *tmp1 = L""; wchar_t tmp2[MAX_SIZE]; wchar_t tmp3[MAX_SIZE]; wchar_t tmp4[MAX_SIZE]; wchar_t tmp5[MAX_SIZE]; wchar_t tmp6[MAX_SIZE]; wchar_t tmp7[MAX_SIZE]; wchar_t tmp8[MAX_SIZE]; wchar_t *tmp9 = L""; char v1[128], v2[128]; // ID UniToStru(tmp0, e->Id); // Protocol switch (e->Protocol) { case NAT_TCP: tmp1 = _UU("NM_NAT_PROTO_TCP"); break; case NAT_UDP: tmp1 = _UU("NM_NAT_PROTO_UDP"); break; case NAT_DNS: tmp1 = _UU("NM_NAT_PROTO_DNS"); break; case NAT_ICMP: tmp1 = _UU("NM_NAT_PROTO_ICMP"); break; } // Source host StrToUni(tmp2, sizeof(tmp2), e->SrcHost); // Source port UniToStru(tmp3, e->SrcPort); // Destination host StrToUni(tmp4, sizeof(tmp4), e->DestHost); // Destination port UniToStru(tmp5, e->DestPort); // Creation date and time of the session GetDateTimeStrEx64(tmp6, sizeof(tmp6), SystemToLocal64(e->CreatedTime), NULL); // Last communication date and time GetDateTimeStrEx64(tmp7, sizeof(tmp7), SystemToLocal64(e->LastCommTime), NULL); // Communication amount ToStr3(v1, sizeof(v1), e->RecvSize); ToStr3(v2, sizeof(v2), e->SendSize); UniFormat(tmp8, sizeof(tmp8), L"%S / %S", v1, v2); // TCP state if (e->Protocol == NAT_TCP) { switch (e->TcpStatus) { case NAT_TCP_CONNECTING: tmp9 = _UU("NAT_TCP_CONNECTING"); break; case NAT_TCP_SEND_RESET: tmp9 = _UU("NAT_TCP_SEND_RESET"); break; case NAT_TCP_CONNECTED: tmp9 = _UU("NAT_TCP_CONNECTED"); break; case NAT_TCP_ESTABLISHED: tmp9 = _UU("NAT_TCP_ESTABLISHED"); break; case NAT_TCP_WAIT_DISCONNECT: tmp9 = _UU("NAT_TCP_WAIT_DISCONNECT"); break; } } LvInsertAdd(b, ICO_PROTOCOL, NULL, 10, tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9); } LvInsertEnd(b, hWnd, L_TABLE); FreeRpcEnumNat(&t); }
// Get the NAT list UINT NtEnumNatList(NAT *n, RPC_ENUM_NAT *t) { UINT ret = ERR_NO_ERROR; VH *v = NULL; Lock(n->lock); { v = n->Virtual; if (n->Online == false || v == NULL) { ret = ERR_OFFLINE; } else { LockVirtual(v); { if (v->Active == false) { ret = ERR_OFFLINE; } else { FreeRpcEnumNat(t); Zero(t, sizeof(RPC_ENUM_NAT)); LockList(v->NatTable); { UINT i; UINT num_usermode_nat = LIST_NUM(v->NatTable); UINT num_kernel_mode_nat = 0; NATIVE_NAT *native = NULL; if (NnIsActive(v) && (v->NativeNat != NULL)) { native = v->NativeNat; num_kernel_mode_nat = LIST_NUM(native->NatTableForSend->AllList); } t->NumItem = num_usermode_nat + num_kernel_mode_nat; t->Items = ZeroMalloc(sizeof(RPC_ENUM_NAT_ITEM) * t->NumItem); // Enumerate entries of the user mode NAT for (i = 0;i < num_usermode_nat;i++) { NAT_ENTRY *nat = LIST_DATA(v->NatTable, i); RPC_ENUM_NAT_ITEM *e = &t->Items[i]; e->Id = nat->Id; e->Protocol = nat->Protocol; e->SrcIp = nat->SrcIp; e->DestIp = nat->DestIp; e->SrcPort = nat->SrcPort; e->DestPort = nat->DestPort; e->CreatedTime = TickToTime(nat->CreatedTime); e->LastCommTime = TickToTime(nat->LastCommTime); IPToStr32(e->SrcHost, sizeof(e->SrcHost), e->SrcIp); IPToStr32(e->DestHost, sizeof(e->DestHost), e->DestIp); if (nat->Sock != NULL) { e->SendSize = nat->Sock->SendSize; e->RecvSize = nat->Sock->RecvSize; if (nat->Sock->Type == SOCK_TCP) { StrCpy(e->DestHost, sizeof(e->DestHost), nat->Sock->RemoteHostname); } } e->TcpStatus = nat->TcpStatus; } // Enumerate the entries in the kernel-mode NAT if (native != NULL) { for (i = 0;i < num_kernel_mode_nat;i++) { NATIVE_NAT_ENTRY *nat = LIST_DATA(native->NatTableForSend->AllList, i); RPC_ENUM_NAT_ITEM *e = &t->Items[num_usermode_nat + i]; e->Id = nat->Id; e->Protocol = nat->Protocol; e->SrcIp = nat->SrcIp; e->DestIp = nat->DestIp; e->SrcPort = nat->SrcPort; e->DestPort = nat->DestPort; e->CreatedTime = TickToTime(nat->CreatedTime); e->LastCommTime = TickToTime(nat->LastCommTime); IPToStr32(e->SrcHost, sizeof(e->SrcHost), e->SrcIp); IPToStr32(e->DestHost, sizeof(e->DestHost), e->DestIp); e->SendSize = nat->TotalSent; e->RecvSize = nat->TotalRecv; e->TcpStatus = nat->Status; } } } UnlockList(v->NatTable); } } UnlockVirtual(v); } } Unlock(n->lock); return ret; }
// NAT リストの取得 UINT NtEnumNatList(NAT *n, RPC_ENUM_NAT *t) { UINT ret = ERR_NO_ERROR; VH *v = NULL; Lock(n->lock); { v = n->Virtual; if (n->Online == false || v == NULL) { ret = ERR_OFFLINE; } else { LockVirtual(v); { if (v->Active == false) { ret = ERR_OFFLINE; } else { FreeRpcEnumNat(t); Zero(t, sizeof(RPC_ENUM_NAT)); LockList(v->NatTable); { UINT i; t->NumItem = LIST_NUM(v->NatTable); t->Items = ZeroMalloc(sizeof(RPC_ENUM_NAT_ITEM) * t->NumItem); for (i = 0;i < t->NumItem;i++) { NAT_ENTRY *nat = LIST_DATA(v->NatTable, i); RPC_ENUM_NAT_ITEM *e = &t->Items[i]; e->Id = nat->Id; e->Protocol = nat->Protocol; e->SrcIp = nat->SrcIp; e->DestIp = nat->DestIp; e->SrcPort = nat->SrcPort; e->DestPort = nat->DestPort; e->CreatedTime = TickToTime(nat->CreatedTime); e->LastCommTime = TickToTime(nat->LastCommTime); IPToStr32(e->SrcHost, sizeof(e->SrcHost), e->SrcIp); IPToStr32(e->DestHost, sizeof(e->DestHost), e->DestIp); if (nat->Sock != NULL) { e->SendSize = nat->Sock->SendSize; e->RecvSize = nat->Sock->RecvSize; if (nat->Sock->Type == SOCK_TCP) { StrCpy(e->DestHost, sizeof(e->DestHost), nat->Sock->RemoteHostname); } } e->TcpStatus = nat->TcpStatus; } } UnlockList(v->NatTable); } } UnlockVirtual(v); } } Unlock(n->lock); return ret; }