How to get IP address of the browser

本文介绍了一种通过客户端JavaScript和ActiveX控件获取浏览器背后的IP地址和MAC地址的方法。该方法适用于运行Windows操作系统的计算机,并需要在浏览器中启用ActiveX。文章提供了具体的ASP文件示例,展示了如何使用Windows Management Instrumentation (WMI)来获取这些信息。
http://www.faqts.com/knowledge_base/view.phtml/aid/9005

How to get IP address of the browser when its operating behind a proxy/firewall? (applets...activex....??)
i have copied the code in my front page but it doesnt work. i dont have .asp setup.
Can u plz send me a simple javascript for .html page to do this job? just MAC address is required
Nov 23rd, 2006 22:39
Anzer M, Michael Scalise, Silvio Pedroso, Hari prasad, Qamar Ahmad, Built in ActiveX Problem in Windows XP

Hi,
there are some problems with below ActiveX it wont work in two builds of
Windows XP. Details are

The components used
<object classid="CLSID:76A64158-CB41-11D1-8B02-00600806D9B6"
id="locator" VIEWASTEXT></object>

<object classid="CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223"
id="theId"></object>

It works for all system except two. The specifications of the systems
are as follows:
IE version - 6
OS - Windows Xp Professional version
6.0.2900.2180.xpsp_sp2_rtm.040803-2158
and Windows Xp Professional version 6.0.2900.2180.xpsp_sp2_gdr.050301-1519

The systems are configured exactly like the others. It seems that
something is restricting the Active X control from loading into the page.

Please let me know if there is any work around or patch to this problem...

----End----

Hi,

You may use a client side javascript in order to discover the IP
address.

<script language="javascript">
alert(location.hostaddress);
</script>

(The solution above doesn't work. It retrieves only the public Ip
address, not the IPaddress on the PC behind the firewall/router. My
solution below does work, but it is a bit more complicated. Mike
Scalise 9/18/2003)

This solution does retrieve IP addresses from behind a firewall. It
does have limitations. The client computer must be running Windows. It
requires ActiveX to be enabled in the browser.
The first file uses Windows Management Instrumentation (WMI) to
retrieve the physical address of your network card (MAC Address) and
the IP address currently assigned to that computer. Remember, if client
computer gets an address using DHCP, this Ip address may change daily.
The MAC address never changes. It is burned into your network card and
is supposed to be unique in the world. (In fact, microsoft uses the MAC
address and the curent date and time to generate GUID's. )

The first page does an asyncronous call to the WMI system. When the
async call it is done, then it then automatically posts the data to
another file which displayed the results. Nifty cool.
Mike Scalise



Copy and paste the two ASPFiles below.

The first ASP file- NIC5.ASP

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>WMI Scripting HTML</title>

<script FOR="foo" EVENT="OnCompleted(hResult,
pErrorObject, pAsyncContext)" LANGUAGE="JScript">

document.forms[0].txtMACAddr.value=unescape(MACAddr);
document.forms[0].txtIPAddr.value=unescape(IPAddr);
document.forms[0].txtDNSName.value=unescape(sDNSName);
document.formbar.submit();
</script>
<script FOR="foo" EVENT="OnObjectReady(objObject,
objAsyncContext)" LANGUAGE="JScript">

if(objObject.IPEnabled != null && objObject.IPEnabled !
= "undefined" && objObject.IPEnabled == true)
{

if(objObject.MACAddress != null &&
objObject.MACAddress != "undefined")
MACAddr = objObject.MACAddress;


if(objObject.IPEnabled && objObject.IPAddress(0) !=
null && objObject.IPAddress(0) != "undefined")
IPAddr = objObject.IPAddress(0);

if(objObject.DNSHostName != null &&
objObject.DNSHostName != "undefined")
sDNSName = objObject.DNSHostName;

}
</script>

</head>
<body>
<p>


<FONT color="red"><span
ID="info"> </span>. </FONT>
<object classid="CLSID:76A64158-CB41-11D1-8B02-
00600806D9B6" id="locator" VIEWASTEXT>
</object>
<object classid="CLSID:75718C9A-F029-11d1-A1AC-
00C04FB6C223" id="foo">
</object>

<script LANGUAGE="JScript">

var service = locator.ConnectServer();
var MACAddr ;
var IPAddr ;
var DomainAddr;
var sDNSName;
service.Security_.ImpersonationLevel=3;
service.InstancesOfAsync
(foo, 'Win32_NetworkAdapterConfiguration');
</script>
</p>


<form method="POST" action="NICPost.asp" id="formfoo" name="formbar">

<input type="hidden" name="txtMACAddr">
<input type="hidden" name="txtIPAddr">
<input type="hidden" name="txtDNSName">

</form>


</body>
</html>

*************************
This is the second file. It is an ASP page, but you could change it to
plain HTML and use Java to retrieve the form field values, if you
wanted.

NICPost.asp

<HTML>
<HEAD>
</HEAD>
<BODY>
Network Interface Card Information Page
<BR>
<BR>
<BR>
You are at IP Address <STRONG>
<%=request.form("txtIPAddr")%>
</STRONG>
<BR>
Your MAC address on your network card is <STRONG>
<% =request.form("txtMACAddr")%>
</STRONG>
<BR>
Your DNS Host name is <STRONG>
<% =request.form("txtDNSName")%>
</STRONG>
<BR>
<BR>
<BR>
To confirm your IP and MAC address information, go to
the command prompt and
type in
<BR>
<BR>
IPCONFIG/ALL
<BR>
<BR>
ASP reports that your IP Address is <STRONG>
<%
response.Write Request.Servervariables("REMOTE_ADDR")
%>
</STRONG>which is your external WAN IP address that
anyone can see,
<BR>
but maybe be shared by hundreds of users if you use Net
Address Translation
(NAT)
<BR>
through a common router.
</BODY>
</HTML>
我需要找到AvahiSRecordBrowserCallback的具体实现。 #ifndef foolookuphfoo #define foolookuphfoo /*** This file is part of avahi. avahi is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. avahi 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with avahi; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. ***/ /** \file avahi-core/lookup.h Functions for browsing/resolving services and other RRs */ /** \example core-browse-services.c Example how to browse for DNS-SD * services using an embedded mDNS stack. */ /** A browsing object for arbitrary RRs */ typedef struct AvahiSRecordBrowser AvahiSRecordBrowser; /** A host name to IP adddress resolver object */ typedef struct AvahiSHostNameResolver AvahiSHostNameResolver; /** An IP address to host name resolver object ("reverse lookup") */ typedef struct AvahiSAddressResolver AvahiSAddressResolver; /** A local domain browsing object. May be used to enumerate domains used on the local LAN */ typedef struct AvahiSDomainBrowser AvahiSDomainBrowser; /** A DNS-SD service type browsing object. May be used to enumerate the service types of all available services on the local LAN */ typedef struct AvahiSServiceTypeBrowser AvahiSServiceTypeBrowser; /** A DNS-SD service browser. Use this to enumerate available services of a certain kind on the local LAN. Use AvahiSServiceResolver to get specific service data like address and port for a service. */ typedef struct AvahiSServiceBrowser AvahiSServiceBrowser; /** A DNS-SD service resolver. Use this to retrieve addres, port and TXT data for a DNS-SD service */ typedef struct AvahiSServiceResolver AvahiSServiceResolver; #include <avahi-common/cdecl.h> #include <avahi-common/defs.h> #include <avahi-core/core.h> AVAHI_C_DECL_BEGIN /** Callback prototype for AvahiSRecordBrowser events */ typedef void (*AvahiSRecordBrowserCallback)( AvahiSRecordBrowser *b, /**< The AvahiSRecordBrowser object that is emitting this callback */ AvahiIfIndex interface, /**< Logical OS network interface number the record was found on */ AvahiProtocol protocol, /**< Protocol number the record was found. */ AvahiBrowserEvent event, /**< Browsing event, either AVAHI_BROWSER_NEW or AVAHI_BROWSER_REMOVE */ AvahiRecord *record, /**< The record that was found */ AvahiLookupResultFlags flags, /**< Lookup flags */ void* userdata /**< Arbitrary user data passed to avahi_s_record_browser_new() */ ); /** Create a new browsing object for arbitrary RRs */ AvahiSRecordBrowser *avahi_s_record_browser_new( AvahiServer *server, /**< The server object to which attach this query */ AvahiIfIndex interface, /**< Logical OS interface number where to look for the records, or AVAHI_IF_UNSPEC to look on interfaces */ AvahiProtocol protocol, /**< Protocol number to use when looking for the record, or AVAHI_PROTO_UNSPEC to look on all protocols */ AvahiKey *key, /**< The search key */ AvahiLookupFlags flags, /**< Lookup flags. Must have set either AVAHI_LOOKUP_FORCE_WIDE_AREA or AVAHI_LOOKUP_FORCE_MULTICAST, since domain based detection is not available here. */ AvahiSRecordBrowserCallback callback, /**< The callback to call on browsing events */ void* userdata /**< Arbitrary use suppliable data which is passed to the callback */); /** Prepare a new browsing object for arbitrary RRs */ AvahiSRecordBrowser *avahi_s_record_browser_prepare( AvahiServer *server, /**< The server object to which attach this query */ AvahiIfIndex interface, /**< Logical OS interface number where to look for the records, or AVAHI_IF_UNSPEC to look on interfaces */ AvahiProtocol protocol, /**< Protocol number to use when looking for the record, or AVAHI_PROTO_UNSPEC to look on all protocols */ AvahiKey *key, /**< The search key */ AvahiLookupFlags flags, /**< Lookup flags. Must have set either AVAHI_LOOKUP_FORCE_WIDE_AREA or AVAHI_LOOKUP_FORCE_MULTICAST, since domain based detection is not available here. */ AvahiSRecordBrowserCallback callback, /**< The callback to call on browsing events */ void* userdata /**< Arbitrary use suppliable data which is passed to the callback */); /** Free an AvahiSRecordBrowser object */ void avahi_s_record_browser_free(AvahiSRecordBrowser *b); /** Start querying on an AvahiSRecordBrowser object */ void avahi_s_record_browser_start_query(AvahiSRecordBrowser *b); /** Callback prototype for AvahiSHostNameResolver events */ typedef void (*AvahiSHostNameResolverCallback)( AvahiSHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, /**< Resolving event */ const char *host_name, /**< Host name which should be resolved. May differ in case from the query */ const AvahiAddress *a, /**< The address, or NULL if the host name couldn't be resolved. */ AvahiLookupResultFlags flags, /**< Lookup flags */ void* userdata); /** Create an AvahiSHostNameResolver object for resolving a host name to an adddress. See AvahiSRecordBrowser for more info on the paramters. */ AvahiSHostNameResolver *avahi_s_host_name_resolver_new( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *host_name, /**< The host name to look for */ AvahiProtocol aprotocol, /**< The address family of the desired address or AVAHI_PROTO_UNSPEC if doesn't matter. */ AvahiLookupFlags flags, /**< Lookup flags. */ AvahiSHostNameResolverCallback calback, void* userdata); /** Prepare an AvahiSHostNameResolver object for resolving a host name to an adddress. See AvahiSRecordBrowser for more info on the paramters. */ AvahiSHostNameResolver *avahi_s_host_name_resolver_prepare( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *host_name, /**< The host name to look for */ AvahiProtocol aprotocol, /**< The address family of the desired address or AVAHI_PROTO_UNSPEC if doesn't matter. */ AvahiLookupFlags flags, /**< Lookup flags. */ AvahiSHostNameResolverCallback calback, void* userdata); /** Start querying on an AvahiSHostNameResolver object */ void avahi_s_host_name_resolver_start(AvahiSHostNameResolver *r); /** Free a AvahiSHostNameResolver object */ void avahi_s_host_name_resolver_free(AvahiSHostNameResolver *r); /** Callback prototype for AvahiSAddressResolver events */ typedef void (*AvahiSAddressResolverCallback)( AvahiSAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *a, const char *host_name, /**< A host name for the specified address, if one was found, i.e. event == AVAHI_RESOLVER_FOUND */ AvahiLookupResultFlags flags, /**< Lookup flags */ void* userdata); /** Create an AvahiSAddressResolver object. See AvahiSRecordBrowser for more info on the paramters. */ AvahiSAddressResolver *avahi_s_address_resolver_new( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const AvahiAddress *address, AvahiLookupFlags flags, /**< Lookup flags. */ AvahiSAddressResolverCallback calback, void* userdata); /** Prepare an AvahiSAddressResolver object. See AvahiSRecordBrowser for more info on the paramters. */ AvahiSAddressResolver *avahi_s_address_resolver_prepare( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const AvahiAddress *address, AvahiLookupFlags flags, /**< Lookup flags. */ AvahiSAddressResolverCallback calback, void* userdata); /** Start querying on an AvahiSAddressResolver object */ void avahi_s_address_resolver_start(AvahiSAddressResolver *r); /** Free an AvahiSAddressResolver object */ void avahi_s_address_resolver_free(AvahiSAddressResolver *r); /** Callback prototype for AvahiSDomainBrowser events */ typedef void (*AvahiSDomainBrowserCallback)( AvahiSDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *domain, AvahiLookupResultFlags flags, /**< Lookup flags */ void* userdata); /** Create a new AvahiSDomainBrowser object */ AvahiSDomainBrowser *avahi_s_domain_browser_new( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiDomainBrowserType type, AvahiLookupFlags flags, /**< Lookup flags. */ AvahiSDomainBrowserCallback callback, void* userdata); /** Prepare a new AvahiSDomainBrowser object */ AvahiSDomainBrowser *avahi_s_domain_browser_prepare( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiDomainBrowserType type, AvahiLookupFlags flags, /**< Lookup flags. */ AvahiSDomainBrowserCallback callback, void* userdata); /** Start querying on an AvahiSDomainBrowser object */ void avahi_s_domain_browser_start(AvahiSDomainBrowser *b); /** Free an AvahiSDomainBrowser object */ void avahi_s_domain_browser_free(AvahiSDomainBrowser *b); /** Callback prototype for AvahiSServiceTypeBrowser events */ typedef void (*AvahiSServiceTypeBrowserCallback)( AvahiSServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *type, const char *domain, AvahiLookupResultFlags flags, /**< Lookup flags */ void* userdata); /** Create a new AvahiSServiceTypeBrowser object. */ AvahiSServiceTypeBrowser *avahi_s_service_type_browser_new( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiLookupFlags flags, /**< Lookup flags. */ AvahiSServiceTypeBrowserCallback callback, void* userdata); /** Prepare a new AvahiSServiceTypeBrowser object. */ AvahiSServiceTypeBrowser *avahi_s_service_type_browser_prepare( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiLookupFlags flags, /**< Lookup flags. */ AvahiSServiceTypeBrowserCallback callback, void* userdata); /** Start querying on an AvahiSServiceTypeBrowser object */ void avahi_s_service_type_browser_start(AvahiSServiceTypeBrowser *b); /** Free an AvahiSServiceTypeBrowser object */ void avahi_s_service_type_browser_free(AvahiSServiceTypeBrowser *b); /** Callback prototype for AvahiSServiceBrowser events */ typedef void (*AvahiSServiceBrowserCallback)( AvahiSServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name /**< Service name, e.g. "Lennart's Files" */, const char *type /**< DNS-SD type, e.g. "_http._tcp" */, const char *domain /**< Domain of this service, e.g. "local" */, AvahiLookupResultFlags flags, /**< Lookup flags */ void* userdata); /** Create a new AvahiSServiceBrowser object. */ AvahiSServiceBrowser *avahi_s_service_browser_new( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *service_type /** DNS-SD service type, e.g. "_http._tcp" */, const char *domain, AvahiLookupFlags flags, /**< Lookup flags. */ AvahiSServiceBrowserCallback callback, void* userdata); /** Prepare a new AvahiSServiceBrowser object. */ AvahiSServiceBrowser *avahi_s_service_browser_prepare( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *service_type /** DNS-SD service type, e.g. "_http._tcp" */, const char *domain, AvahiLookupFlags flags, /**< Lookup flags. */ AvahiSServiceBrowserCallback callback, void* userdata); /** Start querying on an AvahiSServiceBrowser object */ void avahi_s_service_browser_start(AvahiSServiceBrowser *b); /** Free an AvahiSServiceBrowser object */ void avahi_s_service_browser_free(AvahiSServiceBrowser *b); /** Callback prototype for AvahiSServiceResolver events */ typedef void (*AvahiSServiceResolverCallback)( AvahiSServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, /**< Is AVAHI_RESOLVER_FOUND when the service was resolved successfully, and everytime it changes. Is AVAHI_RESOLVER_TIMOUT when the service failed to resolve or disappeared. */ const char *name, /**< Service name */ const char *type, /**< Service Type */ const char *domain, const char *host_name, /**< Host name of the service */ const AvahiAddress *a, /**< The resolved host name */ uint16_t port, /**< Service name */ AvahiStringList *txt, /**< TXT record data */ AvahiLookupResultFlags flags, /**< Lookup flags */ void* userdata); /** Create a new AvahiSServiceResolver object. The specified callback function will be called with the resolved service data. */ AvahiSServiceResolver *avahi_s_service_resolver_new( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain, AvahiProtocol aprotocol, /**< Address family of the desired service address. Use AVAHI_PROTO_UNSPEC if you don't care */ AvahiLookupFlags flags, /**< Lookup flags. */ AvahiSServiceResolverCallback calback, void* userdata); /** Prepare a new AvahiSServiceResolver object. The specified callback function will be called with the resolved service data. */ AvahiSServiceResolver *avahi_s_service_resolver_prepare( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain, AvahiProtocol aprotocol, /**< Address family of the desired service address. Use AVAHI_PROTO_UNSPEC if you don't care */ AvahiLookupFlags flags, /**< Lookup flags. */ AvahiSServiceResolverCallback calback, void* userdata); /** Start querying on an AvahiSServiceResolver object */ void avahi_s_service_resolver_start(AvahiSServiceResolver *r); /** Free an AvahiSServiceResolver object */ void avahi_s_service_resolver_free(AvahiSServiceResolver *r); AVAHI_C_DECL_END #endif
最新发布
08-16
<think>根据用户提供的头文件内容,`AvahiSRecordBrowserCallback` 是一个函数指针类型定义,它规定了回调函数的参数和返回值。但是,用户询问的是“具体实现”,即该回调函数在Avahi项目中的实际使用例子。 需要注意的是,`AvahiSRecordBrowserCallback` 是一个由用户(使用Avahi库的开发者)实现的回调函数,而不是Avahi内部实现的函数。Avahi库本身不会提供这个回调函数的具体实现,因为它是一个回调接口,具体逻辑由库的使用者定义。 然而,我们可以从Avahi的示例代码或测试代码中寻找该回调函数的使用实例。在Avahi源码中,`avahi-core`目录下的示例程序(如`core-browse-services.c`)可能包含该回调的实现。 ### 查找示例实现 在Avahi源码中,以下文件可能包含`AvahiSRecordBrowserCallback`的使用: - `avahi-core/examples/core-browse-services.c` - `avahi-core/examples/core-browse-domains.c` - `avahi-core/examples/core-browse-service-types.c` ### 示例代码片段(模拟) 假设在`core-browse-services.c`中有一个回调函数实现,可能如下: ```c #include <avahi-core/lookup.h> static void service_browse_callback( AvahiSRecordBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, AvahiLookupResultFlags flags, void* userdata) { (void)b; // 防止未使用变量警告 (void)userdata; // 处理事件 switch (event) { case AVAHI_BROWSER_NEW: printf("New service: %s\n", avahi_record_to_string(record)); break; case AVAHI_BROWSER_REMOVE: printf("Service removed: %s\n", avahi_record_to_string(record)); break; case AVAHI_BROWSER_ALL_FOR_NOW: printf("No more services for now.\n"); break; default: break; } } int main() { // ... 初始化AvahiServer等 AvahiSRecordBrowser *browser = avahi_s_record_browser_new(server, ... , service_browse_callback, NULL); // ... } ``` ### 关键点说明 1. **回调函数签名**:必须与`AvahiSRecordBrowserCallback`定义一致。 2. **事件处理**:通常需要处理`AVAHI_BROWSER_NEW`(新服务发现)和`AVAHI_BROWSER_REMOVE`(服务移除)事件。 3. **记录解析**:使用`avahi_record_to_string()`或手动解析`AvahiRecord`结构体获取详细信息。 4. **资源管理**:回调中不需要释放传入的记录(`record`),因为Avahi会管理其生命周期。 ### 实际源码位置 要查看具体实现,建议查阅Avahi源码中的示例程序: - 下载源码:`git clone https://github.com/lathiat/avahi.git` - 浏览示例:`avahi/avahi-core/examples/` §§相关问题§§ 1. 在Avahi中,如何解析`AvahiRecord`结构体以获取服务名称和类型? 2. 使用`avahi_s_record_browser_new`创建浏览器后,如何控制其生命周期? 3. 回调函数中如何处理`AvahiLookupResultFlags`标志位? 4. 如果需要在回调函数中访问多个自定义数据,如何组织`userdata`结构? 5. 如何区分同一个服务的IPv4和IPv6记录?</think>在 Avahi 源码中,`AvahiSRecordBrowserCallback` 是一个**函数指针类型定义**,而不是一个具体的实现函数。这个回调类型定义了用户在使用 Avahi 库时需要实现的自定义回调函数签名。在 Avahi 项目中,实际实现由库的使用者提供,而不是在核心库内部实现。 ### 关键说明: 1. **回调类型定义**: ```c typedef void (*AvahiSRecordBrowserCallback)( AvahiSRecordBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, AvahiLookupResultFlags flags, void* userdata); ``` 这只是一个函数签名规范,规定了用户自定义回调必须符合的参数列表和返回类型。 2. **实现位置**: - 该回调的实际实现由 **Avahi 库的使用者**提供 - 在 Avahi 的测试用例或示例程序中可能有参考实现 - 真正的实现存在于使用 Avahi 的应用程序中(如网络服务发现工具) ### 典型实现示例 虽然核心库中没有具体实现,但用户自定义回调通常包含以下逻辑: ```c void my_record_browser_callback( AvahiSRecordBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, AvahiLookupResultFlags flags, void *userdata) { MyAppContext *ctx = (MyAppContext*)userdata; // 用户自定义上下文 switch (event) { case AVAHI_BROWSER_NEW: printf("[NEW] 接口 %d, 协议 %d: %s\n", interface, protocol, avahi_record_to_string(record)); if (flags & AVAHI_LOOKUP_RESULT_LOCAL) { printf(">> 这是本地服务\n"); } break; case AVAHI_BROWSER_REMOVE: printf("[REMOVE] 记录: %s\n", avahi_key_to_string(record->key)); break; case AVAHI_BROWSER_ALL_FOR_NOW: printf("--- 当前无更多记录 ---\n"); break; } // 处理自定义业务逻辑 if (record && record->key->type == AVAHI_DNS_TYPE_SRV) { handle_srv_record(ctx, record); } } ``` ### 在 Avahi 源码中的使用位置 虽然回调本身不由 Avahi 实现,但您可以在以下位置找到其触发机制: 1. **查找引擎**:`avahi-core/lookup.c` - `lookup_multicast_callback()` 函数(您之前提供的代码) - 第 58 行:`b->callback(...)` 触发用户回调 2. **浏览器对象**:`avahi-core/browse.c` - `avahi_s_record_browser_new()` 函数 - 将用户回调存储在 `browser->callback` 3. **测试用例**:`avahi-core/test.c` - 包含用于单元测试的模拟实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值