Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
363 johboh 1
/*********************************************************************
2
 *
3
 *  NetBIOS Name Service (NBNS) Server
4
 *   -Responds to NBNS name requests to allow human name assignment
5
 *    to the board
6
 *
7
 *********************************************************************
8
 * FileName:        NBNS.c
9
 * Dependencies:    UDP, ARP, Tick
10
 * Processor:       PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F
11
 * Complier:        Microchip C18 v3.02 or higher
12
 *                  Microchip C30 v2.01 or higher
13
 * Company:         Microchip Technology, Inc.
14
 *
15
 * Software License Agreement
16
 *
17
 * This software is owned by Microchip Technology Inc. ("Microchip")
18
 * and is supplied to you for use exclusively as described in the
19
 * associated software agreement.  This software is protected by
20
 * software and other intellectual property laws.  Any use in
21
 * violation of the software license may subject the user to criminal
22
 * sanctions as well as civil liability.  Copyright 2006 Microchip
23
 * Technology Inc.  All rights reserved.
24
 *
25
 * This software is provided "AS IS."  MICROCHIP DISCLAIMS ALL
26
 * WARRANTIES, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, NOT LIMITED
27
 * TO MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
28
 * INFRINGEMENT.  Microchip shall in no event be liable for special,
29
 * incidental, or consequential damages.
30
 *
31
 *
32
 * Author               Date    Comment
33
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34
 * Howard Schlunder     8/01/06 Original
35
 ********************************************************************/
36
#define THIS_IS_NET_BIOS_NAME_SERVICE
37
 
38
#include "..\Include\Compiler.h"
39
#include "..\Include\StackTsk.h"
40
#include "..\Include\UDP.h"
41
#include "..\Include\Tick.h"
42
#include "..\Include\Helpers.h"
43
 
44
#if defined(STACK_USE_NBNS)
45
 
46
#define NBNS_PORT       (137u)
47
 
48
typedef struct _NBNS_HEADER
49
{
50
    WORD_VAL TransactionID;
51
    WORD_VAL Flags;
52
    WORD_VAL Questions;
53
    WORD_VAL Answers;
54
    WORD_VAL AuthoritativeRecords;
55
    WORD_VAL AdditionalRecords;
56
} NBNS_HEADER;
57
 
58
static void NBNSPutName(BYTE *String);
59
static void NBNSGetName(BYTE *String);
60
 
61
 
62
/*********************************************************************
63
 * Function:        void NBNSTask(void)
64
 *
65
 * PreCondition:    None
66
 *
67
 * Input:           None
68
 *
69
 * Output:          Sends responses to NetBIOS name requests
70
 *
71
 * Side Effects:    None
72
 *
73
 * Overview:        None
74
 *
75
 * Note:            None
76
 ********************************************************************/
77
void NBNSTask(void)
78
{
79
    static UDP_SOCKET   MySocket;
80
    BYTE                i;
81
    WORD_VAL            Type, Class;
82
    NBNS_HEADER         NBNSHeader;
83
    BYTE                NameString[16];
84
    static enum
85
    {
86
        NBNS_HOME = 0,
87
        NBNS_OPEN_SOCKET,
88
        NBNS_LISTEN
89
    } smNBNS = NBNS_HOME;
90
 
91
    switch(smNBNS)
92
    {
93
        case NBNS_HOME:
94
            smNBNS++;
95
            break;
96
 
97
        case NBNS_OPEN_SOCKET:
98
            MySocket = UDPOpen(NBNS_PORT, NULL, NBNS_PORT);
99
            if(MySocket == INVALID_UDP_SOCKET)
100
                break;
101
 
102
            smNBNS++;
103
 
104
        case NBNS_LISTEN:
105
            if(!UDPIsGetReady(MySocket))
106
                break;
107
 
108
            // Retrieve the NBNS header and de-big-endian it
109
            UDPGet(&NBNSHeader.TransactionID.v[1]);
110
            UDPGet(&NBNSHeader.TransactionID.v[0]);
111
            UDPGet(&NBNSHeader.Flags.v[1]);
112
            UDPGet(&NBNSHeader.Flags.v[0]);
113
            UDPGet(&NBNSHeader.Questions.v[1]);
114
            UDPGet(&NBNSHeader.Questions.v[0]);
115
            UDPGet(&NBNSHeader.Answers.v[1]);
116
            UDPGet(&NBNSHeader.Answers.v[0]);
117
            UDPGet(&NBNSHeader.AuthoritativeRecords.v[1]);
118
            UDPGet(&NBNSHeader.AuthoritativeRecords.v[0]);
119
            UDPGet(&NBNSHeader.AdditionalRecords.v[1]);
120
            UDPGet(&NBNSHeader.AdditionalRecords.v[0]);
121
 
122
            // Remove all questions
123
            while(NBNSHeader.Questions.Val--)
124
            {
125
                NBNSGetName(NameString);
126
                UDPGet(&i);             // <??> Trailing character on string
127
                UDPGet(&Type.v[1]);     // Question type
128
                UDPGet(&Type.v[0]);
129
                UDPGet(&Class.v[1]);    // Question class
130
                UDPGet(&Class.v[0]);
131
 
132
                if(Type.Val == 0x0020 && Class.Val == 0x0001 && strcmp(NameString, AppConfig.NetBIOSName) == 0)
133
                {
134
                    while(!UDPIsPutReady(MySocket));
135
 
136
                    NBNSHeader.Flags.Val = 0x8400;
137
 
138
                    UDPPut(NBNSHeader.TransactionID.v[1]);
139
                    UDPPut(NBNSHeader.TransactionID.v[0]);
140
                    UDPPut(NBNSHeader.Flags.v[1]);
141
                    UDPPut(NBNSHeader.Flags.v[0]);
142
                    UDPPut(0x00);   // 0x0000 Questions
143
                    UDPPut(0x00);
144
                    UDPPut(0x00);   // 0x0001 Answers
145
                    UDPPut(0x01);
146
                    UDPPut(0x00);   // 0x0000 Athoritative records
147
                    UDPPut(0x00);
148
                    UDPPut(0x00);   // 0x0000 Additional records
149
                    UDPPut(0x00);
150
 
151
                    NBNSPutName(AppConfig.NetBIOSName);
152
                    UDPPut(0x00);   // 0x0020 Type: NetBIOS
153
                    UDPPut(0x20);
154
                    UDPPut(0x00);   // 0x0001 Class: Internet
155
                    UDPPut(0x01);
156
                    UDPPut(0x00);   // 0x00000000 Time To Live
157
                    UDPPut(0x00);
158
                    UDPPut(0x00);
159
                    UDPPut(0x00);
160
 
161
                    UDPPut(0x00);   // 0x0006 Data length
162
                    UDPPut(0x06);  
163
                    UDPPut(0x60);   // 0x6000 Flags: H-node, Unique
164
                    UDPPut(0x00);
165
                    UDPPut(AppConfig.MyIPAddr.v[0]);    // Put out IP address
166
                    UDPPut(AppConfig.MyIPAddr.v[1]);
167
                    UDPPut(AppConfig.MyIPAddr.v[2]);
168
                    UDPPut(AppConfig.MyIPAddr.v[3]);
169
 
170
                    UDPFlush();            
171
                }
172
 
173
            }
174
 
175
            UDPDiscard();
176
 
177
            break;
178
    }
179
}
180
 
181
static void NBNSPutName(BYTE *String)
182
{
183
    BYTE i, j;
184
 
185
    UDPPut(32); // NetBIOS names are always 32 bytes long (16 decoded bytes)
186
    for(i = 0; i < 16; i++)
187
    {
188
        j = *String++;
189
        UDPPut((j>>4) + 'A');
190
        UDPPut((j & 0x0F) + 'A');
191
    }
192
 
193
    UDPPut(0x00);
194
}
195
 
196
static void NBNSGetName(BYTE *String)
197
{
198
    BYTE i, j, k;
199
 
200
    if(String == NULL)
201
    {
202
        UDPGet(&i);
203
        while(i--)
204
        {
205
            UDPGet(&j);
206
        }
207
    }
208
    else
209
    {
210
        UDPGet(&i);
211
        while(i--)
212
        {
213
            UDPGet(&j);
214
            j -= 'A';
215
            k = j<<4;
216
            i--;
217
            UDPGet(&j);
218
            j -= 'A';
219
            *String++ = k | j;
220
        }
221
    }
222
}
223
 
224
 
225
#endif //#if defined(STACK_USE_NBNS)