From 84b0defcf6903a8b014ab1ba38d8923282f230ed Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Sun, 25 Mar 2007 18:44:51 +0000 Subject: ICE (work in progress): use single socket for all candidates in component, and implemented IP interface enumeration on Win32 git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@1104 74dad513-b988-da41-8d7b-12977e46ad98 --- pjlib/build/pjlib.dsp | 13 ++++ pjlib/build/pjlib.vcproj | 60 ++++++++++++----- pjlib/include/pj/ip_helper.h | 88 +++++++++++++++++++++++++ pjlib/include/pj/sock.h | 1 + pjlib/include/pjlib.h | 1 + pjlib/src/pj/ip_helper_generic.c | 69 ++++++++++++++++++++ pjlib/src/pj/ip_helper_win32.c | 136 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 352 insertions(+), 16 deletions(-) create mode 100644 pjlib/include/pj/ip_helper.h create mode 100644 pjlib/src/pj/ip_helper_generic.c create mode 100644 pjlib/src/pj/ip_helper_win32.c (limited to 'pjlib') diff --git a/pjlib/build/pjlib.dsp b/pjlib/build/pjlib.dsp index 39df8bcc..bd841f71 100644 --- a/pjlib/build/pjlib.dsp +++ b/pjlib/build/pjlib.dsp @@ -110,6 +110,11 @@ SOURCE=..\src\pj\ioqueue_epoll.c # End Source File # Begin Source File +SOURCE=..\src\pj\ip_helper_generic.c +# PROP Exclude_From_Build 1 +# End Source File +# Begin Source File + SOURCE=..\src\pj\log_writer_printk.c # PROP Exclude_From_Build 1 # End Source File @@ -254,6 +259,10 @@ SOURCE=..\src\pj\ioqueue_winnt.c # End Source File # Begin Source File +SOURCE=..\src\pj\ip_helper_win32.c +# End Source File +# Begin Source File + SOURCE=..\src\pj\list.c # End Source File # Begin Source File @@ -518,6 +527,10 @@ SOURCE=..\include\pj\ioqueue.h # End Source File # Begin Source File +SOURCE=..\include\pj\ip_helper.h +# End Source File +# Begin Source File + SOURCE=..\include\pj\list.h # End Source File # Begin Source File diff --git a/pjlib/build/pjlib.vcproj b/pjlib/build/pjlib.vcproj index ec3c5644..92183780 100644 --- a/pjlib/build/pjlib.vcproj +++ b/pjlib/build/pjlib.vcproj @@ -16,8 +16,8 @@ + + @@ -1120,6 +1124,26 @@ /> + + + + + + + + @@ -1458,6 +1482,10 @@ RelativePath="..\include\pj\ioqueue.h" > + + diff --git a/pjlib/include/pj/ip_helper.h b/pjlib/include/pj/ip_helper.h new file mode 100644 index 00000000..9515d978 --- /dev/null +++ b/pjlib/include/pj/ip_helper.h @@ -0,0 +1,88 @@ +/* $Id$ */ +/* + * Copyright (C)2003-2007 Benny Prijono + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef __PJ_IP_ROUTE_H__ +#define __PJ_IP_ROUTE_H__ + +/** + * @file ip_helper.h + * @brief IP helper API + */ + +#include + +PJ_BEGIN_DECL + +/** + * @defgroup pj_ip_helper IP Interface and Routing Helper + * @ingroup PJ_IO + * @{ + * + * This module provides functions to query local host's IP interface and + * routing table. + */ + +/** + * This structure describes IP routing entry. + */ +typedef union pj_ip_route_entry +{ + /** IP routing entry for IP version 4 routing */ + struct + { + pj_in_addr if_addr; /**< Local interface IP address. */ + pj_in_addr dst_addr; /**< Destination IP address. */ + pj_in_addr mask; /**< Destination mask. */ + } ipv4; +} pj_ip_route_entry; + + +/** + * Enumerate the local IP interface currently active in the host. + * + * @param count On input, specify the number of entries. On output, + * it will be filled with the actual number of entries. + * @param ifs Array of IP addresses. + * + * @return PJ_SUCCESS on success, or the appropriate error code. + */ +PJ_DECL(pj_status_t) pj_enum_ip_interface(unsigned *count, + pj_in_addr ifs[]); + + +/** + * Enumerate the IP routing table for this host. + * + * @param count On input, specify the number of routes entries. On output, + * it will be filled with the actual number of route entries. + * @param routes Array of IP routing entries. + * + * @return PJ_SUCCESS on success, or the appropriate error code. + */ +PJ_DECL(pj_status_t) pj_enum_ip_route(unsigned *count, + pj_ip_route_entry routes[]); + + + +/** @} */ + +PJ_END_DECL + + +#endif /* __PJ_IP_ROUTE_H__ */ + diff --git a/pjlib/include/pj/sock.h b/pjlib/include/pj/sock.h index d2da570c..0be02589 100644 --- a/pjlib/include/pj/sock.h +++ b/pjlib/include/pj/sock.h @@ -207,6 +207,7 @@ struct pj_sockaddr_in char sin_zero[8]; /**< Padding. */ }; +#undef s6_addr /** * This structure describes IPv6 address. diff --git a/pjlib/include/pjlib.h b/pjlib/include/pjlib.h index e300d487..025fe219 100644 --- a/pjlib/include/pjlib.h +++ b/pjlib/include/pjlib.h @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include diff --git a/pjlib/src/pj/ip_helper_generic.c b/pjlib/src/pj/ip_helper_generic.c new file mode 100644 index 00000000..3b713f5a --- /dev/null +++ b/pjlib/src/pj/ip_helper_generic.c @@ -0,0 +1,69 @@ +/* $Id$ */ +/* + * Copyright (C)2003-2007 Benny Prijono + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include +#include +#include +#include +#include + +/* + * Enumerate the local IP interface currently active in the host. + */ +PJ_DEF(pj_status_t) pj_enum_ip_interface(unsigned *p_cnt, + pj_in_addr ifs[]) +{ + pj_status_t status; + + PJ_ASSERT_RETURN(p_cnt && *p_cnt > 0 && ifs, PJ_EINVAL); + + pj_bzero(ifs, sizeof(ifs[0]) * (*p_cnt)); + + /* Just get one default route */ + status = pj_gethostip(&ifs[0]); + if (status != PJ_SUCCESS) + return status; + + *p_cnt = 1; + return PJ_SUCCESS; +} + +/* + * Enumerate the IP routing table for this host. + */ +PJ_DEF(pj_status_t) pj_enum_ip_route(unsigned *p_cnt, + pj_ip_route_entry routes[]) +{ + pj_status_t status; + + PJ_ASSERT_RETURN(p_cnt && *p_cnt > 0 && routes, PJ_EINVAL); + + pj_bzero(routes, sizeof(routes[0]) * (*p_cnt)); + + /* Just get one default route */ + status = pj_gethostip(&routes[0].ipv4.if_addr); + if (status != PJ_SUCCESS) + return status; + + routes[0].ipv4.dst_addr.s_addr = 0; + routes[0].ipv4.mask.s_addr = 0; + *p_cnt = 1; + + return PJ_SUCCESS; +} + diff --git a/pjlib/src/pj/ip_helper_win32.c b/pjlib/src/pj/ip_helper_win32.c new file mode 100644 index 00000000..094cfc9f --- /dev/null +++ b/pjlib/src/pj/ip_helper_win32.c @@ -0,0 +1,136 @@ +/* $Id$ */ +/* + * Copyright (C)2003-2007 Benny Prijono + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#define WIN32_LEAN_AND_MEAN +#include + +/* PMIB_ICMP_EX is not declared in VC6, causing error */ +#if defined(_MSC_VER) && _MSC_VER < 1400 +# define PMIB_ICMP_EX void* +#endif +#include + +#include +#include +#include +#include + + +/* + * Enumerate the local IP interface currently active in the host. + */ +PJ_DEF(pj_status_t) pj_enum_ip_interface(unsigned *p_cnt, + pj_in_addr ifs[]) +{ + /* Provide enough buffer or otherwise it will fail with + * error 22 ("Not Enough Buffer") error. + */ + MIB_IPADDRTABLE ipTabBuff[4]; + MIB_IPADDRTABLE *pTab; + ULONG tabSize; + unsigned i, count; + DWORD rc; + + PJ_ASSERT_RETURN(p_cnt && ifs, PJ_EINVAL); + + pTab = ipTabBuff; + + /* Get IP address table */ + tabSize = sizeof(ipTabBuff); + rc = GetIpAddrTable(ipTabBuff, &tabSize, FALSE); + if (rc != NO_ERROR) + return PJ_RETURN_OS_ERROR(rc); + + /* Reset result */ + pj_bzero(ifs, sizeof(ifs[0]) * (*p_cnt)); + + /* Now fill out the entries */ + count = (pTab->dwNumEntries < *p_cnt) ? pTab->dwNumEntries : *p_cnt; + for (i=0; itable[i].dwAddr; + } + + *p_cnt = count; + + return PJ_SUCCESS; + +} + + +/* + * Enumerate the IP routing table for this host. + */ +PJ_DEF(pj_status_t) pj_enum_ip_route(unsigned *p_cnt, + pj_ip_route_entry routes[]) +{ + MIB_IPADDRTABLE ipTabBuff[4]; + MIB_IPADDRTABLE *pIpTab; + MIB_IPFORWARDTABLE rtabBuff[4]; + MIB_IPFORWARDTABLE *prTab; + ULONG tabSize; + unsigned i, count; + DWORD rc; + + PJ_ASSERT_RETURN(p_cnt && routes, PJ_EINVAL); + + pIpTab = ipTabBuff; + prTab = rtabBuff; + + /* First get IP address table */ + tabSize = sizeof(ipTabBuff); + rc = GetIpAddrTable(ipTabBuff, &tabSize, FALSE); + if (rc != NO_ERROR) + return PJ_RETURN_OS_ERROR(rc); + + /* Next get IP route table */ + tabSize = sizeof(rtabBuff); + rc = GetIpForwardTable(rtabBuff, &tabSize, 1); + if (rc != NO_ERROR) + return PJ_RETURN_OS_ERROR(rc); + + /* Reset routes */ + pj_bzero(routes, sizeof(routes[0]) * (*p_cnt)); + + /* Now fill out the route entries */ + count = (prTab->dwNumEntries < *p_cnt) ? prTab->dwNumEntries : *p_cnt; + *p_cnt = 0; + for (i=0; idwNumEntries; ++j) { + if (pIpTab->table[j].dwIndex == prTab->table[i].dwForwardIfIndex) + break; + } + + if (j==pIpTab->dwNumEntries) + continue; /* Interface not found */ + + routes[*p_cnt].ipv4.if_addr.s_addr = pIpTab->table[j].dwAddr; + routes[*p_cnt].ipv4.dst_addr.s_addr = prTab->table[i].dwForwardDest; + routes[*p_cnt].ipv4.mask.s_addr = prTab->table[i].dwForwardMask; + + (*p_cnt)++; + } + + return PJ_SUCCESS; +} + + + -- cgit v1.2.3