1 |
/*********************************************************************
|
2 |
*
|
3 |
* NetBIOS Name Service (NBNS) Server
|
4 |
* Module for Microchip TCP/IP Stack
|
5 |
* -Responds to NBNS name requests to allow human name assignment
|
6 |
* to the board. i.e. allows nodes on the same IP subnet to use a
|
7 |
* hostname to access the board instead of an IP address.
|
8 |
* -Reference: RFC 1002
|
9 |
*
|
10 |
*********************************************************************
|
11 |
* FileName: NBNS.c
|
12 |
* Dependencies: UDP, ARP, Tick
|
13 |
* Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F
|
14 |
* Complier: Microchip C18 v3.02 or higher
|
15 |
* Microchip C30 v2.01 or higher
|
16 |
* Company: Microchip Technology, Inc.
|
17 |
*
|
18 |
* Software License Agreement
|
19 |
*
|
20 |
* Copyright © 2002-2007 Microchip Technology Inc. All rights
|
21 |
* reserved.
|
22 |
*
|
23 |
* Microchip licenses to you the right to use, modify, copy, and
|
24 |
* distribute:
|
25 |
* (i) the Software when embedded on a Microchip microcontroller or
|
26 |
* digital signal controller product (“Device”) which is
|
27 |
* integrated into Licensee’s product; or
|
28 |
* (ii) ONLY the Software driver source files ENC28J60.c and
|
29 |
* ENC28J60.h ported to a non-Microchip device used in
|
30 |
* conjunction with a Microchip ethernet controller for the
|
31 |
* sole purpose of interfacing with the ethernet controller.
|
32 |
*
|
33 |
* You should refer to the license agreement accompanying this
|
34 |
* Software for additional information regarding your rights and
|
35 |
* obligations.
|
36 |
*
|
37 |
* THE SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS” WITHOUT
|
38 |
* WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
|
39 |
* LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
|
40 |
* PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
41 |
* MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
|
42 |
* CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
|
43 |
* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
|
44 |
* BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
|
45 |
* THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
|
46 |
* SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
|
47 |
* (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
|
48 |
*
|
49 |
*
|
50 |
* Author Date Comment
|
51 |
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
52 |
* Howard Schlunder 8/01/06 Original
|
53 |
********************************************************************/
|
54 |
#define __NBNS_C
|
55 |
|
56 |
#include "TCPIP Stack/TCPIP.h"
|
57 |
|
58 |
#if defined(STACK_USE_NBNS)
|
59 |
|
60 |
#define NBNS_PORT (137u)
|
61 |
|
62 |
typedef struct _NBNS_HEADER
|
63 |
{
|
64 |
WORD_VAL TransactionID;
|
65 |
WORD_VAL Flags;
|
66 |
WORD_VAL Questions;
|
67 |
WORD_VAL Answers;
|
68 |
WORD_VAL AuthoritativeRecords;
|
69 |
WORD_VAL AdditionalRecords;
|
70 |
} NBNS_HEADER;
|
71 |
|
72 |
static void NBNSPutName(BYTE *String);
|
73 |
static void NBNSGetName(BYTE *String);
|
74 |
|
75 |
|
76 |
/*********************************************************************
|
77 |
* Function: void NBNSTask(void)
|
78 |
*
|
79 |
* PreCondition: None
|
80 |
*
|
81 |
* Input: None
|
82 |
*
|
83 |
* Output: Sends responses to NetBIOS name requests
|
84 |
*
|
85 |
* Side Effects: None
|
86 |
*
|
87 |
* Overview: None
|
88 |
*
|
89 |
* Note: None
|
90 |
********************************************************************/
|
91 |
void NBNSTask(void)
|
92 |
{
|
93 |
static UDP_SOCKET MySocket;
|
94 |
BYTE i;
|
95 |
WORD_VAL Type, Class;
|
96 |
NBNS_HEADER NBNSHeader;
|
97 |
BYTE NameString[16];
|
98 |
static enum
|
99 |
{
|
100 |
NBNS_HOME = 0,
|
101 |
NBNS_OPEN_SOCKET,
|
102 |
NBNS_LISTEN
|
103 |
} smNBNS = NBNS_HOME;
|
104 |
|
105 |
switch(smNBNS)
|
106 |
{
|
107 |
case NBNS_HOME:
|
108 |
smNBNS++;
|
109 |
break;
|
110 |
|
111 |
case NBNS_OPEN_SOCKET:
|
112 |
MySocket = UDPOpen(NBNS_PORT, NULL, NBNS_PORT);
|
113 |
if(MySocket == INVALID_UDP_SOCKET)
|
114 |
break;
|
115 |
|
116 |
smNBNS++;
|
117 |
|
118 |
case NBNS_LISTEN:
|
119 |
if(!UDPIsGetReady(MySocket))
|
120 |
break;
|
121 |
|
122 |
// Retrieve the NBNS header and de-big-endian it
|
123 |
UDPGet(&NBNSHeader.TransactionID.v[1]);
|
124 |
UDPGet(&NBNSHeader.TransactionID.v[0]);
|
125 |
UDPGet(&NBNSHeader.Flags.v[1]);
|
126 |
UDPGet(&NBNSHeader.Flags.v[0]);
|
127 |
UDPGet(&NBNSHeader.Questions.v[1]);
|
128 |
UDPGet(&NBNSHeader.Questions.v[0]);
|
129 |
UDPGet(&NBNSHeader.Answers.v[1]);
|
130 |
UDPGet(&NBNSHeader.Answers.v[0]);
|
131 |
UDPGet(&NBNSHeader.AuthoritativeRecords.v[1]);
|
132 |
UDPGet(&NBNSHeader.AuthoritativeRecords.v[0]);
|
133 |
UDPGet(&NBNSHeader.AdditionalRecords.v[1]);
|
134 |
UDPGet(&NBNSHeader.AdditionalRecords.v[0]);
|
135 |
|
136 |
// Remove all questions
|
137 |
while(NBNSHeader.Questions.Val--)
|
138 |
{
|
139 |
NBNSGetName(NameString);
|
140 |
UDPGet(&i); // <??> Trailing character on string
|
141 |
UDPGet(&Type.v[1]); // Question type
|
142 |
UDPGet(&Type.v[0]);
|
143 |
UDPGet(&Class.v[1]); // Question class
|
144 |
UDPGet(&Class.v[0]);
|
145 |
|
146 |
if(Type.Val == 0x0020u && Class.Val == 0x0001u && strcmp((char*)NameString, (char*)AppConfig.NetBIOSName) == 0)
|
147 |
{
|
148 |
while(!UDPIsPutReady(MySocket));
|
149 |
|
150 |
NBNSHeader.Flags.Val = 0x8400;
|
151 |
|
152 |
UDPPut(NBNSHeader.TransactionID.v[1]);
|
153 |
UDPPut(NBNSHeader.TransactionID.v[0]);
|
154 |
UDPPut(NBNSHeader.Flags.v[1]);
|
155 |
UDPPut(NBNSHeader.Flags.v[0]);
|
156 |
UDPPut(0x00); // 0x0000 Questions
|
157 |
UDPPut(0x00);
|
158 |
UDPPut(0x00); // 0x0001 Answers
|
159 |
UDPPut(0x01);
|
160 |
UDPPut(0x00); // 0x0000 Athoritative records
|
161 |
UDPPut(0x00);
|
162 |
UDPPut(0x00); // 0x0000 Additional records
|
163 |
UDPPut(0x00);
|
164 |
|
165 |
NBNSPutName(AppConfig.NetBIOSName);
|
166 |
UDPPut(0x00); // 0x0020 Type: NetBIOS
|
167 |
UDPPut(0x20);
|
168 |
UDPPut(0x00); // 0x0001 Class: Internet
|
169 |
UDPPut(0x01);
|
170 |
UDPPut(0x00); // 0x00000000 Time To Live
|
171 |
UDPPut(0x00);
|
172 |
UDPPut(0x00);
|
173 |
UDPPut(0x00);
|
174 |
|
175 |
UDPPut(0x00); // 0x0006 Data length
|
176 |
UDPPut(0x06);
|
177 |
UDPPut(0x60); // 0x6000 Flags: H-node, Unique
|
178 |
UDPPut(0x00);
|
179 |
UDPPut(AppConfig.MyIPAddr.v[0]); // Put out IP address
|
180 |
UDPPut(AppConfig.MyIPAddr.v[1]);
|
181 |
UDPPut(AppConfig.MyIPAddr.v[2]);
|
182 |
UDPPut(AppConfig.MyIPAddr.v[3]);
|
183 |
|
184 |
UDPFlush();
|
185 |
}
|
186 |
|
187 |
}
|
188 |
|
189 |
UDPDiscard();
|
190 |
|
191 |
break;
|
192 |
}
|
193 |
}
|
194 |
|
195 |
static void NBNSPutName(BYTE *String)
|
196 |
{
|
197 |
BYTE i, j;
|
198 |
|
199 |
UDPPut(32); // NetBIOS names are always 32 bytes long (16 decoded bytes)
|
200 |
for(i = 0; i < 16u; i++)
|
201 |
{
|
202 |
j = *String++;
|
203 |
UDPPut((j>>4) + 'A');
|
204 |
UDPPut((j & 0x0F) + 'A');
|
205 |
}
|
206 |
|
207 |
UDPPut(0x00);
|
208 |
}
|
209 |
|
210 |
static void NBNSGetName(BYTE *String)
|
211 |
{
|
212 |
BYTE i, j, k;
|
213 |
|
214 |
if(String == NULL)
|
215 |
{
|
216 |
UDPGet(&i);
|
217 |
while(i--)
|
218 |
{
|
219 |
UDPGet(&j);
|
220 |
}
|
221 |
}
|
222 |
else
|
223 |
{
|
224 |
UDPGet(&i);
|
225 |
while(i--)
|
226 |
{
|
227 |
UDPGet(&j);
|
228 |
j -= 'A';
|
229 |
k = j<<4;
|
230 |
i--;
|
231 |
UDPGet(&j);
|
232 |
j -= 'A';
|
233 |
*String++ = k | j;
|
234 |
}
|
235 |
}
|
236 |
}
|
237 |
|
238 |
|
239 |
#endif //#if defined(STACK_USE_NBNS)
|