File: mdns_mini.cpp

package info (click to toggle)
cvsnt 2.5.03.2382-3.3%2Blenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 32,288 kB
  • ctags: 24,567
  • sloc: ansic: 136,648; cpp: 102,037; sh: 42,846; asm: 3,495; makefile: 1,763; ada: 1,681; perl: 1,352; pascal: 1,089; cs: 1,008; yacc: 805; python: 453; xml: 263; sql: 210; lisp: 7
file content (240 lines) | stat: -rw-r--r-- 5,897 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
	CVSNT mdns helpers - minimdns
    Copyright (C) 2004-5 Tony Hoyle and March-Hare Software Ltd

    This library 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.

    This library 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 this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <config.h>
#include "lib/api_system.h"
#include "cvs_string.h"
#include "mdns_mini.h"
#include "ServerIO.h"

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#define sock_errno WSAGetLastError()
#else
#define SOCKET int
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define sock_errno errno
#endif

void CMdnsHelperMini::_browse_srv_func(const char *name, uint16_t port, const char *target, void *userdata)
{
	((CMdnsHelperMini*)userdata)->browse_srv_func(name,port,target);
}

void CMdnsHelperMini::browse_srv_func(const char *name, uint16_t port, const char *target)
{
	m_callbacks->srv_fn(name,port,target,m_userdata);
}

void CMdnsHelperMini::_browse_txt_func(const char *name, const char *txt, void *userdata)
{
	((CMdnsHelperMini*)userdata)->browse_txt_func(name,txt);
}

void CMdnsHelperMini::browse_txt_func(const char *name, const char *txt)
{
	m_callbacks->txt_fn(name,txt,m_userdata);
}

void CMdnsHelperMini::_browse_ipv4_func(const char *name, const ipv4_address_t *ipv4, void *userdata)
{
	((CMdnsHelperMini*)userdata)->browse_ipv4_func(name,ipv4);
}

void CMdnsHelperMini::browse_ipv4_func(const char *name, const ipv4_address_t *ipv4)
{
	m_callbacks->ipv4_fn(name,ipv4->address,m_userdata);
}

void CMdnsHelperMini::_browse_ipv6_func(const char *name, const ipv6_address_t *ipv6, void *userdata)
{
	((CMdnsHelperMini*)userdata)->browse_ipv6_func(name,ipv6);
}

void CMdnsHelperMini::browse_ipv6_func(const char *name, const ipv6_address_t *ipv6)
{
	m_callbacks->ipv6_fn(name,ipv6->address,m_userdata);
}

CMdnsHelperMini::CMdnsHelperMini()
{
	m_handle = NULL;
}

CMdnsHelperMini::~CMdnsHelperMini()
{
	close();
}

int CMdnsHelperMini::open()
{
	m_handle = mdns_open();
	return m_handle?0:-1;
}

int CMdnsHelperMini::publish(const char *instance, const char *service, const char *location, int port, const char *text)
{
	char host[1024];

	char tmp[256];
	strncpy(tmp,service,sizeof(tmp));
	char *p=tmp+strlen(tmp)-1;
	if(strlen(tmp)>0 && *p=='.') *(p--)='\0';
	if(strlen(tmp)>6 && !strcmp(p-5,".local")) *(p-5)='\0';

	service = tmp;

	int ret;
	mdns_service_item_t *serv = new mdns_service_item_t;
	serv->Instance = strdup(instance);
	serv->Service = strdup(service);
	serv->Port = port;
	serv->Location = NULL;
	serv->ipv4 = NULL;
	serv->ipv6 = NULL;

	if(gethostname(host,sizeof(host)))
		strcpy(host,"unknown");
	p=strchr(host,'.');
	if(p) *p='\0';

	strcat(host,".local");

	serv->Location = strdup(host);

	if(!location)
	{
		if(gethostname(host,sizeof(host)))
			strcpy(host,"unknown");
		location = host;
	}

	addrinfo hint = {0}, *addr = NULL, *ai;

	int err=getaddrinfo(location,NULL,&hint,&addr);
	if(err)
	{
#ifdef EAI_SYSTEM
		if(err==EAI_SYSTEM) err=sock_errno;
#endif
		CServerIo::trace(3,"Unable to resolve host %s: %s",location,gai_strerror(err));
		return false;
	}

	for(ai = addr; ai; ai=ai->ai_next)
	{
		if(ai->ai_family==PF_INET6 && !serv->ipv6)
		{
			sockaddr_in6 *sin = (sockaddr_in6*)ai->ai_addr;
			serv->ipv6 = new ipv6_address_t;
			memcpy(serv->ipv6->address,&sin->sin6_addr,sizeof(serv->ipv6->address));
		}
		if(ai->ai_family==PF_INET && !serv->ipv4)
		{
			sockaddr_in *sin = (sockaddr_in*)ai->ai_addr;
			int type = ntohl(sin->sin_addr.s_addr)>>24;
			if(type!=127 && type!=255)
			{
				serv->ipv4 = new ipv4_address_t;
				memcpy(serv->ipv4->address,&sin->sin_addr,sizeof(serv->ipv4->address));
			}
			else
			{
				printf("Hostname %s returned loopback address!  Invalid DNS configuration.\n");
			}
		}
	}
	freeaddrinfo(addr);

	if(!(ret=mdns_add_service(m_handle,serv)))
	{
		m_services.push_back(serv);
	}
	else
	{
		if(serv->Instance) free((void*)serv->Instance);
		if(serv->Service) free((void*)serv->Service);
		if(serv->Location) free((void*)serv->Location);
		delete serv->ipv4;
		delete serv->ipv6;
		delete serv;
	}
	return ret;
}

int CMdnsHelperMini::step()
{
	return mdns_server_step(m_handle);
}

int CMdnsHelperMini::browse(const char *service, MdnsBrowseCallback *callbacks, void *userdata)
{
	struct mdns_callback serv_fn = 
	{
		NULL,/*name_func*/
		_browse_srv_func,
		_browse_txt_func,
		_browse_ipv4_func,
		_browse_ipv6_func
	};

	if(!callbacks->ipv4_fn)
		serv_fn.ipv4_func = NULL;
	if(!callbacks->ipv6_fn)
		serv_fn.ipv6_func = NULL;
	if(!callbacks->txt_fn)
		serv_fn.txt_func = NULL;
	if(!callbacks->srv_fn)
		serv_fn.srv_func = NULL;

	m_userdata = userdata;
	m_callbacks = callbacks;

	return mdns_query_services(m_handle,service,&serv_fn,this,0);
}

int CMdnsHelperMini::close()
{
	mdns_close(m_handle);
	m_handle = NULL;
	for(size_t n=0; n<m_services.size(); n++)
	{
		mdns_service_item_t *serv = m_services[n];
		if(serv->Instance) free((void*)serv->Instance);
		if(serv->Service) free((void*)serv->Service);
		if(serv->Location) free((void*)serv->Location);
		delete serv->ipv4;
		delete serv->ipv6;
		delete serv;
	}
	m_services.resize(0);
	return 0;
}

extern "C" CMdnsHelperBase *MdnsHelperMini_Alloc()
{
	return new CMdnsHelperMini();
}