-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresolve.c
210 lines (155 loc) · 5.21 KB
/
resolve.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <net/if.h>
#include <netdb.h>
#include "dns_sd.h"
struct BrowseReplyData{
DNSServiceErrorType errorCode;
char *serviceName;
char *regType;
char *replyDomain;
};
char *discoveredName;
char *discoveredRegtype;
char *discoveredDomain;
uint16_t endianChange(uint16_t i)
{
return ((i>>8)&0xff)+((i<<8)&0xff00);
}
void customBrowseReply( DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode,
const char *serviceName, const char *regtype, const char *replyDomain, void *context)
{
// init BrowseReplyData
struct BrowseReplyData *replyData=context;
replyData->errorCode=errorCode;
replyData->serviceName=malloc(strlen(serviceName)+1);
replyData->regType=malloc(strlen(regtype)+1);
replyData->replyDomain=malloc(strlen(replyDomain)+1);
printf("In BrowseReply callback!!\n");
if(flags == kDNSServiceFlagsMoreComing){
printf("flag: kDNSServiceFlagsMoreComing\n");
}else if(flags == kDNSServiceFlagsAdd){
printf("flag: kDNSServiceFlagsAdd\n");
}else{
printf("flag: %d\n", flags);
}
printf("Error Code: %d\n", errorCode);
printf("Service Name: %s\n", serviceName);
printf("RegType: %s\n", regtype);
printf("Domain: %s\n", replyDomain);
discoveredName=malloc(strlen(serviceName)+1);
strcpy(discoveredName, serviceName);
strcpy(replyData->serviceName, serviceName);
discoveredRegtype=malloc(strlen(regtype)+1);
strcpy(discoveredRegtype, regtype);
strcpy(replyData->regType, regtype);
discoveredDomain=malloc(strlen(replyDomain)+1);
strcpy(discoveredDomain, replyDomain);
strcpy(replyData->replyDomain, replyDomain);
}
void customResolveReply( DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode,
const char *fullname, const char *hosttarget, uint16_t port, /* In network byte order */
uint16_t txtLen, const unsigned char *txtRecord, void *context)
{
struct hostent *hostInfo;
struct in_addr *serv_addr;
if ( (hostInfo=gethostbyname(hosttarget))== NULL){
printf("Call gethostbyname ERROR!!");
}else{
serv_addr=(struct in_addr *) hostInfo->h_addr_list[0];
printf("In ResolveReply callback!!\n");
printf("full name: %s\n", fullname);
printf("host target: %s\n", hosttarget);
printf("host ip addr: %s\n", inet_ntoa(*serv_addr));
printf("port: %d\n", endianChange(port));
printf("txtLen: %d\n", txtLen);
char *temp=malloc(txtLen+1);
strncpy(temp, txtRecord, txtLen);
temp[txtLen]='\0';
printf("txtRecord: %s\n", temp);
}
}
int main(int argc, char* argv[])
{
DNSServiceRef browseRef;
DNSServiceRef resolveRef;
DNSServiceErrorType result;
DNSServiceFlags nativeFlag=0;
struct BrowseReplyData *replyData;
int port = 11234;
char* serviceType="_personal._tcp";
char* netInterface="eth0";
int netIndex=if_nametoindex(netInterface);
int serviceFD;
char input;
printf("Starting to Discover service...\n");
result=DNSServiceBrowse(
&browseRef,
nativeFlag,
netIndex,
serviceType,
NULL, /* domain */
&customBrowseReply, /* callback */
replyData /* context, user-defined that will be an argument of callback */
);
if (result == kDNSServiceErr_NoError){
printf("Browse service success!!\n");
}else{
printf("Browser service fail!!\n");
printf("Error code: %d\n", result);
return 1;
}
//serviceFD=DNSServiceRefSockFD(browseRef);
// this method will active the callback register before.
// TO-DO if no service is available, will block the program. solve it.
result=DNSServiceProcessResult( browseRef );
if (result == kDNSServiceErr_NoError){
printf("Browse response success!!\n");
printf("discovered:\n");
printf("%s %s %s\n", discoveredName, discoveredRegtype, discoveredDomain);
printf("%s %s %s\n", replyData->serviceName, replyData->regType, replyData->replyDomain);
}else{
printf("Browse response fail!!\n");
printf("Error code: %d\n", result);
return 1;
}
result=DNSServiceResolve(
&resolveRef,
nativeFlag,
netIndex,
discoveredName,
discoveredRegtype,
discoveredDomain,
&customResolveReply, /* callback */
NULL /* context, user-defined that will be an argument of callback */
);
if (result == kDNSServiceErr_NoError){
printf("Resolve service success!!\n");
}else{
printf("Resolve service fail!!\n");
printf("Error code: %d\n", result);
return 1;
}
result=DNSServiceProcessResult(resolveRef);
if (result == kDNSServiceErr_NoError){
printf("Resolve response success!!\n");
}else{
printf("Rosolve response fail!!\n");
printf("Error code: %d\n", result);
return 1;
}
printf("press 'q' to exist:\n");
while ((input=getchar()) != -1 && input!='q');
DNSServiceRefDeallocate(resolveRef);
// this must be call before calling DNSServiceBrowser again.
DNSServiceRefDeallocate(browseRef);
// release replyData
free(replyData->serviceName);
free(replyData->regType);
free(replyData->replyDomain);
free(discoveredName);
free(discoveredRegtype);
free(discoveredDomain);
return 0;
}