summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/nds/dswifi/arm9/source/sgIP_DHCP.c
blob: 33805ba5e4e3c315b033c1396679d1367c257bb3 (plain) (blame)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// DSWifi Project - sgIP Internet Protocol Stack Implementation
// Copyright (C) 2005-2006 Stephen Stair - sgstair@akkit.org - http://www.akkit.org
/******************************************************************************
DSWifi Lib and test materials are licenced under the MIT open source licence:
Copyright (c) 2005-2006 Stephen Stair

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#include "sgIP_DHCP.h"
#include "sgIP_DNS.h"
#include <string.h>
#include "sys/socket.h"
#include "netinet/in.h"

extern unsigned long volatile sgIP_timems;
int dhcp_socket;
char dhcp_hostname[64];
int dhcp_tid;
unsigned long dhcp_timestart, dhcp_timelastaction;
sgIP_DHCP_Packet * dhcp_p;
sgIP_Hub_HWInterface * dhcp_int;
int dhcp_optionptr;
int dhcp_requestDNS;
int dhcp_status;
int dhcp_state; // 0== send DHCPDISCOVER wait for DHCPOFFER, 1== send DHCPREQUEST wait for DHCPACK
unsigned long dhcp_rcvd_ip, dhcp_rcvd_gateway, dhcp_rcvd_snmask, dhcp_rcvd_dns[3], dhcp_serverip;


void sgIP_DHCP_Init() {
   dhcp_socket=0;
   dhcp_p=0;
   dhcp_int=0;
   dhcp_rcvd_ip=0;
   strcpy(dhcp_hostname,SGIP_DHCP_DEFAULTHOSTNAME);
   dhcp_status=SGIP_DHCP_STATUS_IDLE;
}
void sgIP_DHCP_SetHostName(char * s) {
   strncpy(dhcp_hostname,s,63);
   dhcp_hostname[63]=0;
}
int sgIP_DHCP_IsDhcpIp(unsigned long ip) { // check if the IP address was assigned via dhcp.
	return ip==dhcp_rcvd_ip;
}

void sgIP_DHCP_SendDgram() {
   struct sockaddr_in sain;
   int len_dhcp;
   sain.sin_port=htons(67); // bootp server port
   sain.sin_addr.s_addr=0xFFFFFFFF; // broadcast
   dhcp_p->options[dhcp_optionptr++]=0xFF; // terminate options list.
   //sendto(dhcp_socket,dhcp_p,sizeof(sgIP_DHCP_Packet)-312+dhcp_optionptr,0,(struct sockaddr *)&sain,sizeof(sain));
   len_dhcp = sizeof(sgIP_DHCP_Packet)-312+dhcp_optionptr;
   if(len_dhcp<300) len_dhcp=300;
   sendto(dhcp_socket,dhcp_p,len_dhcp,0,(struct sockaddr *)&sain,sizeof(sain));
   sgIP_free(dhcp_p);
   dhcp_p=0;
   dhcp_timelastaction=sgIP_timems;
}

void sgIP_DHCP_BeginDgram(int dgramtype) {
   int i;
   if(dhcp_p) sgIP_free(dhcp_p);
   dhcp_p = (sgIP_DHCP_Packet *) sgIP_malloc(sizeof(sgIP_DHCP_Packet));
   if(!dhcp_p) return;

   // ensure packet is zero'd.. seems to pacify some routers.  malloc doesn't initialise the memory returned.
   memset(dhcp_p,0,sizeof(sgIP_DHCP_Packet));

   dhcp_p->op=1;                 // 1==BOOTREQUEST
   dhcp_p->htype=1;              // 1== ethernet address type
   dhcp_p->hlen=6;               // hardware address length
   dhcp_p->hops=0;               // client sets to zero
   dhcp_p->xid=dhcp_tid;         // DHCP transaction ID
   dhcp_p->secs=(sgIP_timems-dhcp_timestart)/1000; // seconds since DHCP start
   dhcp_p->flags=htons(0x0000);  // top bit set = request broadcast response
   dhcp_p->ciaddr=0;
   dhcp_p->yiaddr=0;
   dhcp_p->siaddr=0;
   dhcp_p->giaddr=0;

   memcpy(dhcp_p->chaddr,dhcp_int->hwaddr,6);

   dhcp_optionptr=0;
   dhcp_p->options[dhcp_optionptr++]=0x63;
   dhcp_p->options[dhcp_optionptr++]=0x82;
   dhcp_p->options[dhcp_optionptr++]=0x53;
   dhcp_p->options[dhcp_optionptr++]=0x63; // 4-byte "magic cookie" (bleh!)
   // add some necessary options... - by default add the dhcp message type, host name, class id, and parameter request list
   dhcp_p->options[dhcp_optionptr++]=0x35; // DHCP Message type
   dhcp_p->options[dhcp_optionptr++]=0x01;
   dhcp_p->options[dhcp_optionptr++]=dgramtype;

   dhcp_p->options[dhcp_optionptr++]=0x3D; // DHCP client identifier
   dhcp_p->options[dhcp_optionptr++]=0x07; // length
   dhcp_p->options[dhcp_optionptr++]=0x01; // hw type
   for(i=0;i<6;i++)    dhcp_p->options[dhcp_optionptr++]=dhcp_int->hwaddr[i];

   dhcp_p->options[dhcp_optionptr++]=0x0C; // DHCP host name
   dhcp_p->options[dhcp_optionptr++]=strlen(dhcp_hostname);
   for(i=0;i<strlen(dhcp_hostname);i++) {
      dhcp_p->options[dhcp_optionptr++]=dhcp_hostname[i];
   }

   dhcp_p->options[dhcp_optionptr++]=0x37; // DHCP Parameter request list
   dhcp_p->options[dhcp_optionptr++]=2+dhcp_requestDNS;
   dhcp_p->options[dhcp_optionptr++]=1; // subnet mask
   dhcp_p->options[dhcp_optionptr++]=3; // router
   if(dhcp_requestDNS) dhcp_p->options[dhcp_optionptr++]=6; // dns server

   if(dgramtype==DHCP_TYPE_REQUEST) {
	   dhcp_p->options[dhcp_optionptr++]=0x32; // DHCP Requested IP address
	   dhcp_p->options[dhcp_optionptr++]=0x04;
	   dhcp_p->options[dhcp_optionptr++]=(dhcp_rcvd_ip)&255;
	   dhcp_p->options[dhcp_optionptr++]=(dhcp_rcvd_ip>>8)&255;
	   dhcp_p->options[dhcp_optionptr++]=(dhcp_rcvd_ip>>16)&255;
	   dhcp_p->options[dhcp_optionptr++]=(dhcp_rcvd_ip>>24)&255;

	   dhcp_p->options[dhcp_optionptr++]=0x36; // DHCP Server identifier
	   dhcp_p->options[dhcp_optionptr++]=0x04;
	   dhcp_p->options[dhcp_optionptr++]=(dhcp_serverip)&255;
	   dhcp_p->options[dhcp_optionptr++]=(dhcp_serverip>>8)&255;
	   dhcp_p->options[dhcp_optionptr++]=(dhcp_serverip>>16)&255;
	   dhcp_p->options[dhcp_optionptr++]=(dhcp_serverip>>24)&255;
   }

   dhcp_p->options[dhcp_optionptr++]=0x3C; // DHCP Vendor Class ID
   dhcp_p->options[dhcp_optionptr++]=strlen(SGIP_DHCP_CLASSNAME);
   for(i=0;i<strlen(SGIP_DHCP_CLASSNAME);i++) {
      dhcp_p->options[dhcp_optionptr++]=(SGIP_DHCP_CLASSNAME)[i];
   }

   // reason we don't send it immediately is in case the calling code wants to modify some data or add some options.
}



void sgIP_DHCP_Start(sgIP_Hub_HWInterface * interface, int getDNS) { // begin dhcp transaction to get IP and maybe DNS data.
   struct sockaddr_in sain;
   int i;
   SGIP_DEBUG_MESSAGE(("sgIP_DHCP_Start()"));
   sgIP_DHCP_Terminate();
   dhcp_requestDNS=getDNS?1:0;
   dhcp_int=interface;
   dhcp_timestart=sgIP_timems;
   dhcp_timelastaction=sgIP_timems;
   dhcp_tid=sgIP_timems;
   dhcp_status=SGIP_DHCP_STATUS_WORKING;
   dhcp_state=0;

   dhcp_rcvd_ip = 0;
   dhcp_rcvd_gateway=0;
   dhcp_rcvd_snmask=0;
   dhcp_rcvd_dns[0]=0;
   dhcp_rcvd_dns[1]=0;
   dhcp_rcvd_dns[2]=0;

   dhcp_socket=socket(AF_INET,SOCK_DGRAM,0);
   sain.sin_addr.s_addr=0;
   sain.sin_port=htons(68); // BOOTP client
   bind(dhcp_socket,(struct sockaddr *)&sain,sizeof(sain));
   i=1;
   ioctl(dhcp_socket,FIONBIO,&i);

   sgIP_DHCP_BeginDgram(DHCP_TYPE_DISCOVER);
   sgIP_DHCP_SendDgram();
}
void sgIP_DHCP_Release() { // call to dump our DHCP address and leave.
   if(dhcp_status==SGIP_DHCP_STATUS_WORKING) {
      sgIP_DHCP_Terminate();
   } else {
      sgIP_DHCP_BeginDgram(DHCP_TYPE_RELEASE);
      dhcp_p->ciaddr=dhcp_int->ipaddr;
      sgIP_DHCP_SendDgram();

   }
}
int  sgIP_DHCP_Update() { // MUST be called periodicly; returns status - call until it returns SGIP_DHCP_STATUS_SUCCESS or _FAILED.
	sgIP_DHCP_Packet * p;
	struct sockaddr_in * sain;
	int i,j,n,l;
	if(dhcp_status!=SGIP_DHCP_STATUS_WORKING) return dhcp_status;
	int send = 0;

	p=(sgIP_DHCP_Packet *)sgIP_malloc(sizeof(sgIP_DHCP_Packet));
	if(p) {

		while(1) {
			l=recvfrom(dhcp_socket,p,sizeof(sgIP_DHCP_Packet),0,(struct sockaddr *)&sain,&n);
			if(l==-1) break;
			if(p->op!=2 || p->htype!=1 || p->hlen!=6 || p->xid!=dhcp_tid ) continue;
			// check magic cookie
			if(p->options[0]!=0x63 || p->options[1]!=0x82 || p->options[2]!=0x53 || p->options[3]!=0x63) continue;
			i=4; // yay, the cookie is valid.
			l -= (sizeof(sgIP_DHCP_Packet)-312); // number of bytes remaining in the options
			while(i<l) {
				n=p->options[i++];
				switch(n) {
				case 0: // ignore
					break;
				case 255: // end-of-options marker.
					l=0;
					break;
				case 53: // message type, variable length, 2+n
					j=p->options[i++];
					if(dhcp_state==0) {
						if(p->options[i]!=DHCP_TYPE_OFFER) l=-1;
					} else {
						if(p->options[i]==DHCP_TYPE_ACK) {
							sgIP_free(p);
							sgIP_DHCP_Terminate();
							dhcp_int->ipaddr=dhcp_rcvd_ip;
							dhcp_int->gateway=dhcp_rcvd_gateway;
							dhcp_int->snmask=dhcp_rcvd_snmask;
							SGIP_DEBUG_MESSAGE(("DHCP Configured!"));
							SGIP_DEBUG_MESSAGE(("IP%08X SM%08X GW%08X",dhcp_rcvd_ip,dhcp_rcvd_snmask,dhcp_rcvd_gateway));
							if(dhcp_requestDNS) {
								dhcp_int->dns[0]=dhcp_rcvd_dns[0];
								dhcp_int->dns[1]=dhcp_rcvd_dns[1];
								dhcp_int->dns[2]=dhcp_rcvd_dns[2];
								SGIP_DEBUG_MESSAGE(("DNS %08X %08X %08X",dhcp_rcvd_dns[0],dhcp_rcvd_dns[1],dhcp_rcvd_dns[2]));
							}

							dhcp_status=SGIP_DHCP_STATUS_SUCCESS;
							return dhcp_status;
						} else {
							l=-1;
						}
					}
					i+=j;

					break;
				case 1: // subnet mask field, variable length 2+n
					j=p->options[i++];
					if(dhcp_state==1 || j<4) {i+=j; break; }
					dhcp_rcvd_snmask = (dhcp_rcvd_snmask>>8) | (p->options[i++]<<24);
					dhcp_rcvd_snmask = (dhcp_rcvd_snmask>>8) | (p->options[i++]<<24);
					dhcp_rcvd_snmask = (dhcp_rcvd_snmask>>8) | (p->options[i++]<<24);
					dhcp_rcvd_snmask = (dhcp_rcvd_snmask>>8) | (p->options[i++]<<24);
					i+=j-4;
					break;
				case 3: // gateway, variable length 2+n
					j=p->options[i++];
					if(dhcp_state==1 || j<4) {i+=j; break; }
					dhcp_rcvd_gateway = (dhcp_rcvd_gateway>>8) | (p->options[i++]<<24);
					dhcp_rcvd_gateway = (dhcp_rcvd_gateway>>8) | (p->options[i++]<<24);
					dhcp_rcvd_gateway = (dhcp_rcvd_gateway>>8) | (p->options[i++]<<24);
					dhcp_rcvd_gateway = (dhcp_rcvd_gateway>>8) | (p->options[i++]<<24);
					i+=j-4;
					break;
				case 54: // server ID, variable length 2+n
					j=p->options[i++];
					if(dhcp_state==1 || j<4) {i+=j; break; }
					dhcp_serverip = (dhcp_serverip>>8) | (p->options[i++]<<24);
					dhcp_serverip = (dhcp_serverip>>8) | (p->options[i++]<<24);
					dhcp_serverip = (dhcp_serverip>>8) | (p->options[i++]<<24);
					dhcp_serverip = (dhcp_serverip>>8) | (p->options[i++]<<24);
					i+=j-4;
					break;
				case 6: // dns servers, variable length 2+n
					if(dhcp_requestDNS && !dhcp_state) {
						j=p->options[i++];
						n=0;
						while(n<3 && 4*n+3<j) {
							dhcp_rcvd_dns[n] = (dhcp_rcvd_dns[n]>>8) | (p->options[i++]<<24);
							dhcp_rcvd_dns[n] = (dhcp_rcvd_dns[n]>>8) | (p->options[i++]<<24);
							dhcp_rcvd_dns[n] = (dhcp_rcvd_dns[n]>>8) | (p->options[i++]<<24);
							dhcp_rcvd_dns[n] = (dhcp_rcvd_dns[n]>>8) | (p->options[i++]<<24);
							n++;
						}
						i+=j-4*n;
						break;
					}
				default:
					j=p->options[i++];
					i+=j;
				}
			}
			if(l==-1) continue;
			dhcp_rcvd_ip=(p->yiaddr);

		 // discover succeeded.  increment transaction id.  force sending REQUEST message next.
         dhcp_state=1;
		 dhcp_tid += ( sgIP_timems-dhcp_timestart ) + 1;
		 send = 1;
         break;
		}

		sgIP_free(p);
		// has timeout expired?
		if( (sgIP_timems-dhcp_timestart) > SGIP_DHCP_ERRORTIMEOUT) {
            SGIP_DEBUG_MESSAGE(("sgIP DHCP error timeout!"));
			sgIP_DHCP_Terminate();
			dhcp_status=SGIP_DHCP_STATUS_FAILED;
			return dhcp_status;
		}
		if( send || (sgIP_timems-dhcp_timelastaction) > SGIP_DHCP_RESENDTIMEOUT )
		{
			if(dhcp_state==0) sgIP_DHCP_BeginDgram(DHCP_TYPE_DISCOVER); else sgIP_DHCP_BeginDgram(DHCP_TYPE_REQUEST);
			sgIP_DHCP_SendDgram();
		}
	} else {
        SGIP_DEBUG_MESSAGE(("sgIP DHCP alloc failed!"));
		sgIP_DHCP_Terminate();
		dhcp_status=SGIP_DHCP_STATUS_FAILED;
	}

	return dhcp_status;
}
void sgIP_DHCP_Terminate() { // kill the process where it stands; deallocate all DHCP resources.
   if(dhcp_socket) closesocket(dhcp_socket);
   dhcp_socket=0;
   if(dhcp_p) sgIP_free(dhcp_p);
   dhcp_p=0;
   dhcp_status=SGIP_DHCP_STATUS_IDLE;
}

int gethostname(char *name, size_t len)
{
    int size = sizeof(dhcp_hostname);
    if (name == NULL)
        return SGIP_ERROR(EFAULT);

    if ( len <= size )
        return SGIP_ERROR(EINVAL);

    strncpy(name, dhcp_hostname, size);
    name[size]=0;
    return 0;
}

int sethostname(const char *name, size_t len)
{
    sgIP_DNS_Record *rec;

    int size = sizeof(dhcp_hostname);
    if (name == NULL)
        return SGIP_ERROR(EFAULT);

    if ( len > size - 1)
        return SGIP_ERROR(EINVAL);

   rec = sgIP_DNS_FindDNSRecord(dhcp_hostname);

   strncpy(dhcp_hostname, name, len);
   dhcp_hostname[len]=0;
   strncpy(rec->aliases[0], name, len);
   rec->aliases[0][len]=0;
   strncpy(rec->name, name, len);
   rec->name[len]=0;

   return 0;
}