Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | #define WIN32_LEAN_AND_MEAN |
| 21 | #include <windows.h> |
| 22 | |
| 23 | /* PMIB_ICMP_EX is not declared in VC6, causing error */ |
| 24 | #if defined(_MSC_VER) && _MSC_VER < 1400 |
| 25 | # define PMIB_ICMP_EX void* |
| 26 | #endif |
| 27 | #include <Iphlpapi.h> |
| 28 | |
| 29 | #include <pj/ip_helper.h> |
| 30 | #include <pj/assert.h> |
| 31 | #include <pj/errno.h> |
| 32 | #include <pj/string.h> |
| 33 | |
| 34 | |
| 35 | /* |
| 36 | * Enumerate the local IP interface currently active in the host. |
| 37 | */ |
| 38 | PJ_DEF(pj_status_t) pj_enum_ip_interface(unsigned *p_cnt, |
| 39 | pj_in_addr ifs[]) |
| 40 | { |
| 41 | /* Provide enough buffer or otherwise it will fail with |
| 42 | * error 22 ("Not Enough Buffer") error. |
| 43 | */ |
| 44 | MIB_IPADDRTABLE ipTabBuff[4]; |
| 45 | MIB_IPADDRTABLE *pTab; |
| 46 | ULONG tabSize; |
| 47 | unsigned i, count; |
| 48 | DWORD rc; |
| 49 | |
| 50 | PJ_ASSERT_RETURN(p_cnt && ifs, PJ_EINVAL); |
| 51 | |
| 52 | pTab = ipTabBuff; |
| 53 | |
| 54 | /* Get IP address table */ |
| 55 | tabSize = sizeof(ipTabBuff); |
| 56 | rc = GetIpAddrTable(ipTabBuff, &tabSize, FALSE); |
| 57 | if (rc != NO_ERROR) |
| 58 | return PJ_RETURN_OS_ERROR(rc); |
| 59 | |
| 60 | /* Reset result */ |
| 61 | pj_bzero(ifs, sizeof(ifs[0]) * (*p_cnt)); |
| 62 | |
| 63 | /* Now fill out the entries */ |
| 64 | count = (pTab->dwNumEntries < *p_cnt) ? pTab->dwNumEntries : *p_cnt; |
Benny Prijono | fed1af9 | 2007-03-27 23:29:27 +0000 | [diff] [blame^] | 65 | *p_cnt = 0; |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 66 | for (i=0; i<count; ++i) { |
Benny Prijono | fed1af9 | 2007-03-27 23:29:27 +0000 | [diff] [blame^] | 67 | /* Some Windows returns 0.0.0.0! */ |
| 68 | if (pTab->table[i].dwAddr == 0) |
| 69 | continue; |
| 70 | ifs[*p_cnt].s_addr = pTab->table[i].dwAddr; |
| 71 | (*p_cnt)++; |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 74 | return PJ_SUCCESS; |
| 75 | |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /* |
| 80 | * Enumerate the IP routing table for this host. |
| 81 | */ |
| 82 | PJ_DEF(pj_status_t) pj_enum_ip_route(unsigned *p_cnt, |
| 83 | pj_ip_route_entry routes[]) |
| 84 | { |
| 85 | MIB_IPADDRTABLE ipTabBuff[4]; |
| 86 | MIB_IPADDRTABLE *pIpTab; |
| 87 | MIB_IPFORWARDTABLE rtabBuff[4]; |
| 88 | MIB_IPFORWARDTABLE *prTab; |
| 89 | ULONG tabSize; |
| 90 | unsigned i, count; |
| 91 | DWORD rc; |
| 92 | |
| 93 | PJ_ASSERT_RETURN(p_cnt && routes, PJ_EINVAL); |
| 94 | |
| 95 | pIpTab = ipTabBuff; |
| 96 | prTab = rtabBuff; |
| 97 | |
| 98 | /* First get IP address table */ |
| 99 | tabSize = sizeof(ipTabBuff); |
| 100 | rc = GetIpAddrTable(ipTabBuff, &tabSize, FALSE); |
| 101 | if (rc != NO_ERROR) |
| 102 | return PJ_RETURN_OS_ERROR(rc); |
| 103 | |
| 104 | /* Next get IP route table */ |
| 105 | tabSize = sizeof(rtabBuff); |
| 106 | rc = GetIpForwardTable(rtabBuff, &tabSize, 1); |
| 107 | if (rc != NO_ERROR) |
| 108 | return PJ_RETURN_OS_ERROR(rc); |
| 109 | |
| 110 | /* Reset routes */ |
| 111 | pj_bzero(routes, sizeof(routes[0]) * (*p_cnt)); |
| 112 | |
| 113 | /* Now fill out the route entries */ |
| 114 | count = (prTab->dwNumEntries < *p_cnt) ? prTab->dwNumEntries : *p_cnt; |
| 115 | *p_cnt = 0; |
| 116 | for (i=0; i<count; ++i) { |
| 117 | unsigned j; |
| 118 | |
| 119 | /* Find interface entry */ |
| 120 | for (j=0; j<pIpTab->dwNumEntries; ++j) { |
| 121 | if (pIpTab->table[j].dwIndex == prTab->table[i].dwForwardIfIndex) |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | if (j==pIpTab->dwNumEntries) |
| 126 | continue; /* Interface not found */ |
| 127 | |
| 128 | routes[*p_cnt].ipv4.if_addr.s_addr = pIpTab->table[j].dwAddr; |
| 129 | routes[*p_cnt].ipv4.dst_addr.s_addr = prTab->table[i].dwForwardDest; |
| 130 | routes[*p_cnt].ipv4.mask.s_addr = prTab->table[i].dwForwardMask; |
| 131 | |
| 132 | (*p_cnt)++; |
| 133 | } |
| 134 | |
| 135 | return PJ_SUCCESS; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | |