1 #include   "tx_api.h"
2 #include   "nx_api.h"
3 #include   "nx_ram_network_driver_test_1500.h"
4 
5 extern void    test_control_return(UINT status);
6 
7 #if defined __PRODUCT_NETXDUO__ && !defined NX_MDNS_DISABLE_CLIENT && !defined NX_MDNS_DISABLE_SERVER && !defined NX_DISABLE_IPV4
8 #include   "nxd_mdns.h"
9 
10 #define     DEMO_STACK_SIZE    2048
11 #define     BUFFER_SIZE        10240
12 #define     LOCAL_FULL_SERVICE_COUNT    16
13 #define     PEER_FULL_SERVICE_COUNT     16
14 #define     PEER_PARTIAL_SERVICE_COUNT  32
15 
16 /* Define the ThreadX and NetX object control blocks...  */
17 
18 static TX_THREAD               ntest_0;
19 
20 static NX_PACKET_POOL          pool_0;
21 static NX_IP                   ip_0;
22 
23 /* Define the NetX MDNS object control blocks.  */
24 
25 static NX_MDNS                 mdns_0;
26 static UCHAR                   buffer[BUFFER_SIZE];
27 static ULONG                   current_buffer_size;
28 
29 /* Define the counters used in the test application...  */
30 
31 static ULONG                   error_counter;
32 static CHAR                   *pointer;
33 static CHAR                    host_registered = NX_FALSE;
34 static CHAR                    service_registered = NX_FALSE;
35 static CHAR                    query_received;
36 
37 /* Define thread prototypes.  */
38 
39 static void    ntest_0_entry(ULONG thread_input);
40 extern VOID    _nx_ram_network_driver_1500(NX_IP_DRIVER *driver_req_ptr);
41 extern UINT    (*advanced_packet_process_callback)(NX_IP *ip_ptr, NX_PACKET *packet_ptr, UINT *operation_ptr, UINT *delay_ptr);
42 static UINT    my_packet_process(NX_IP *ip_ptr, NX_PACKET *packet_ptr, UINT *operation_ptr, UINT *delay_ptr);
43 static VOID    probing_notify(struct NX_MDNS_STRUCT *mdns_ptr, UCHAR *name, UINT state);
44 
45 /* Define what the initial system looks like.  */
46 
47 #ifdef CTEST
test_application_define(void * first_unused_memory)48 VOID test_application_define(void *first_unused_memory)
49 #else
50 void           netx_mdns_local_cache_continuous_query_test(void *first_unused_memory)
51 #endif
52 {
53 
54 UINT       status;
55 
56     /* Setup the working pointer.  */
57     pointer = (CHAR *) first_unused_memory;
58     error_counter = 0;
59 
60     /* Initialize the NetX system.  */
61     nx_system_initialize();
62 
63     /* Create a packet pool.  */
64     status = nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", 512, pointer, 8192);
65     pointer = pointer + 8192;
66 
67     if(status)
68         error_counter++;
69 
70     /* Create an IP instance.  */
71     status = nx_ip_create(&ip_0, "NetX IP Instance 0", IP_ADDRESS(192, 168, 0, 31), 0xFFFFFF00UL, &pool_0,
72                           _nx_ram_network_driver_1500, pointer, 2048, 1);
73     pointer = pointer + 2048;
74 
75     /* Check for IP create errors.  */
76     if (status)
77         error_counter++;
78 
79     /* Enable ARP and supply ARP cache memory for IP Instance 0.  */
80     status = nx_arp_enable(&ip_0, (void *) pointer, 1024);
81     pointer = pointer + 1024;
82     if(status)
83         error_counter++;
84 
85     /* Enable UDP processing for both IP instances.  */
86     status = nx_udp_enable(&ip_0);
87 
88     /* Check UDP enable status.  */
89     if(status)
90         error_counter++;
91 
92     status = nx_igmp_enable(&ip_0);
93 
94     /* Check status. */
95     if(status)
96         error_counter++;
97 
98     /* Create the test thread.  */
99     tx_thread_create(&ntest_0, "thread 0", ntest_0_entry, NX_NULL,
100                      pointer, DEMO_STACK_SIZE,
101                      3, 3, TX_NO_TIME_SLICE, TX_AUTO_START);
102     pointer = pointer + DEMO_STACK_SIZE;
103 }
104 
105 /* Define the test threads.  */
106 
ntest_0_entry(ULONG thread_input)107 static void    ntest_0_entry(ULONG thread_input)
108 {
109 UINT       status;
110 ULONG      actual_status;
111 
112 
113     NX_PARAMETER_NOT_USED(thread_input);
114 
115     printf("NetX Test:   MDNS Local Cache Continuous Query Test....................");
116 
117     /* Ensure the IP instance has been initialized.  */
118     status = nx_ip_status_check(&ip_0, NX_IP_INITIALIZE_DONE, &actual_status, 100);
119 
120     /* Check status. */
121     if(status != NX_SUCCESS)
122     {
123         printf("ERROR!\n");
124         test_control_return(1);
125     }
126 
127     /* Create mDNS. */
128     current_buffer_size = (BUFFER_SIZE >> 1);
129     status = nx_mdns_create(&mdns_0, &ip_0, &pool_0, 2, pointer, DEMO_STACK_SIZE, (UCHAR*)"NETX-MDNS",
130                             buffer, current_buffer_size, buffer + current_buffer_size, current_buffer_size, probing_notify);
131     pointer = pointer + DEMO_STACK_SIZE;
132 
133     /* Check status. */
134     if(status != NX_SUCCESS)
135     {
136         printf("ERROR!\n");
137         test_control_return(1);
138     }
139 
140     /* Enable mDNS.  */
141     status = nx_mdns_enable(&mdns_0, 0);
142 
143     /* Check status. */
144     if(status != NX_SUCCESS)
145     {
146         printf("ERROR!\n");
147         test_control_return(1);
148     }
149 
150     /* Wait for host probing. */
151     while(host_registered == NX_FALSE)
152     {
153         tx_thread_sleep(NX_IP_PERIODIC_RATE);
154     }
155 
156     /* Add a service. */
157     if(nx_mdns_service_add(&mdns_0, (UCHAR*)"test", (UCHAR *)"_http._tcp", NX_NULL, NX_NULL, 100, 0, 0, 80, NX_MDNS_RR_SET_UNIQUE, 0))
158     {
159         printf("ERROR!\n");
160         test_control_return(1);
161     }
162 
163     /* Wait for service probing. */
164     while(service_registered == NX_FALSE)
165     {
166         tx_thread_sleep(NX_IP_PERIODIC_RATE);
167     }
168 
169     /* Initialize the query received. */
170     query_received = 0;
171 
172     /* Set callback function pointer. */
173     advanced_packet_process_callback = my_packet_process;
174 
175     /* Start one-shot query. */
176     if(nx_mdns_service_continuous_query(&mdns_0, NX_NULL, (UCHAR*)"_http._tcp", NX_NULL))
177     {
178         printf("ERROR!\n");
179         test_control_return(1);
180     }
181 
182     /* Wait for query. */
183     while(query_received == 0)
184     {
185         tx_thread_sleep(NX_IP_PERIODIC_RATE);
186     }
187 
188     /* Determine if the test was successful.  */
189     if(error_counter)
190     {
191         printf("ERROR!\n");
192         test_control_return(1);
193     }
194     else
195     {
196         printf("SUCCESS!\n");
197         test_control_return(0);
198     }
199 }
200 
201 
my_packet_process(NX_IP * ip_ptr,NX_PACKET * packet_ptr,UINT * operation_ptr,UINT * delay_ptr)202 static UINT    my_packet_process(NX_IP *ip_ptr, NX_PACKET *packet_ptr, UINT *operation_ptr, UINT *delay_ptr)
203 {
204 UCHAR       *packet_pointer;
205 USHORT      mdns_flags;
206 USHORT      question_count;
207 USHORT      answer_count;
208 
209 
210 
211     NX_PARAMETER_NOT_USED(ip_ptr);
212     NX_PARAMETER_NOT_USED(operation_ptr);
213     NX_PARAMETER_NOT_USED(delay_ptr);
214 
215     /* Get protocol. */
216     packet_pointer = packet_ptr -> nx_packet_prepend_ptr + 9;
217 
218     /* Check UDP packets only. */
219     if(*packet_pointer != NX_PROTOCOL_UDP)
220         return NX_TRUE;
221 
222     /* Get port. */
223     packet_pointer = packet_ptr -> nx_packet_prepend_ptr + 20;
224 
225     /* Check UDP port 5353 only. */
226     if((((*packet_pointer << 8) + *(packet_pointer + 1)) != 5353) ||
227        (((*(packet_pointer + 2) << 8) + *(packet_pointer + 3)) != 5353))
228         return NX_TRUE;
229 
230     /* Point to mDNS message.  */
231     packet_pointer = packet_ptr -> nx_packet_prepend_ptr + 28;
232 
233     /* Extract the message type which should be the first byte.  */
234     mdns_flags = NX_MDNS_GET_USHORT_DATA(packet_pointer + NX_MDNS_FLAGS_OFFSET);
235 
236     /* Check whether this packet is the query. */
237     if(mdns_flags != NX_MDNS_QUERY_FLAG)
238         return NX_TRUE;
239 
240     /* Increase the query count.  */
241     query_received++;
242 
243     /* Get the question count.  */
244     question_count = NX_MDNS_GET_USHORT_DATA(packet_pointer + NX_MDNS_QDCOUNT_OFFSET);
245 
246     /* Determine if we have any 'answers' to our DNS query. */
247     answer_count = NX_MDNS_GET_USHORT_DATA(packet_pointer + NX_MDNS_ANCOUNT_OFFSET);
248 
249     /* Check the question count and answer count.  */
250     if ((question_count != 1) || (answer_count != 1))
251         error_counter++;
252 
253     return NX_TRUE;
254 }
255 
probing_notify(struct NX_MDNS_STRUCT * mdns_ptr,UCHAR * name,UINT state)256 static VOID  probing_notify(struct NX_MDNS_STRUCT *mdns_ptr, UCHAR *name, UINT state)
257 {
258 
259     NX_PARAMETER_NOT_USED(mdns_ptr);
260     NX_PARAMETER_NOT_USED(name);
261 
262     switch(state)
263     {
264         case NX_MDNS_LOCAL_SERVICE_REGISTERED_SUCCESS:
265         {
266             service_registered = NX_TRUE;
267             break;
268         }
269         case NX_MDNS_LOCAL_SERVICE_REGISTERED_FAILURE:
270         {
271             service_registered = NX_FALSE;
272             break;
273         }
274         case NX_MDNS_LOCAL_HOST_REGISTERED_SUCCESS:
275         {
276             host_registered = NX_TRUE;
277             break;
278         }
279         case NX_MDNS_LOCAL_HOST_REGISTERED_FAILURE:
280         {
281             host_registered = NX_FALSE;
282             break;
283         }
284     }
285 }
286 #else
287 
288 #ifdef CTEST
test_application_define(void * first_unused_memory)289 VOID test_application_define(void *first_unused_memory)
290 #else
291 void           netx_mdns_local_cache_continuous_query_test(void *first_unused_memory)
292 #endif
293 {
294     printf("NetX Test:   MDNS Local Cache Continuous Query Test....................N/A\n");
295     test_control_return(3);
296 }
297 #endif /* NX_MDNS_DISABLE_CLIENT  */
298 
299