📄 dhcp.lst
字号:
225 2 pTxdnet->dhcpframe.options[i++]=DHCPReg.myip.bytes[3];
226 2 }
227 1
228 1
229 1 //----- host name(用ID代替) ------
230 1 model_id_len = strlen(ModelIdBak);
231 1
232 1 pTxdnet->dhcpframe.options[i++] = 12;
233 1 pTxdnet->dhcpframe.options[i++] = model_id_len;
234 1 for (j=0; j<model_id_len; j++) { //ID的字符串
235 2 pTxdnet->dhcpframe.options[i++] = ModelIdBak[j];
236 2 }
237 1
238 1 if (PACK_TYPE == OP_DHCPREQUEST) {
239 2 pTxdnet->dhcpframe.options[i++]=81;
240 2 pTxdnet->dhcpframe.options[i++]=4+model_id_len;
C51 COMPILER V7.06 DHCP 09/26/2008 13:36:22 PAGE 5
241 2 pTxdnet->dhcpframe.options[i++]=0;
242 2 pTxdnet->dhcpframe.options[i++]=0;
243 2 pTxdnet->dhcpframe.options[i++]=0;
244 2 for (j=0; j<model_id_len; j++) { //ID的字符串
245 3 pTxdnet->dhcpframe.options[i++] = ModelIdBak[j];
246 3 }
247 2 pTxdnet->dhcpframe.options[i++]=0x2e;
248 2 }
249 1
250 1 //----- vendor class ------
251 1 pTxdnet->dhcpframe.options[i++]=60;
252 1 pTxdnet->dhcpframe.options[i++]=8;
253 1 pTxdnet->dhcpframe.options[i++]=0x4d;
254 1 pTxdnet->dhcpframe.options[i++]=0x53;
255 1 pTxdnet->dhcpframe.options[i++]=0x46;
256 1 pTxdnet->dhcpframe.options[i++]=0x54;
257 1 pTxdnet->dhcpframe.options[i++]=0x20;
258 1 pTxdnet->dhcpframe.options[i++]=0x35;
259 1 pTxdnet->dhcpframe.options[i++]=0x2e;
260 1 pTxdnet->dhcpframe.options[i++]=0x30;
261 1
262 1 pTxdnet->dhcpframe.options[i++]=255; //DHCP包结束
263 1
264 1 len+=i; //+选项长度很重要
265 1 pTxdnet->udpframe.length = len; //UDP长度头+数据
266 1
267 1 createudpcrc(pTxdnet,len); //CRC包括UDP头及UDP数据
268 1
269 1
270 1 //===================以上打包===============================
271 1 //构建以太网包
272 1 pTxdnet->etherframe.protocal=0x0800;
273 1 pTxdnet->etherframe.uSourceID[0]=my_ethernet_address.words[0]; //网卡地址
274 1 pTxdnet->etherframe.uSourceID[1]=my_ethernet_address.words[1];
275 1 pTxdnet->etherframe.uSourceID[2]=my_ethernet_address.words[2];
276 1 pTxdnet->ipframe.verandihl=0x45;
277 1 pTxdnet->ipframe.typeofserver=0x00;
278 1 pTxdnet->ipframe.totallength=20+len;
279 1 pTxdnet->ipframe.ttl=0x80;
280 1 pTxdnet->ipframe.frameindex=frameindex;
281 1 frameindex++;
282 1 pTxdnet->ipframe.segment=0x0000;
283 1 pTxdnet->ipframe.protocal=UDP_TYPE; //基于UDP协议
284 1 pTxdnet->ipframe.crc=0;
285 1
286 1 pTxdnet->ipframe.destip[0]=dest_ip.words[0]; //填全1
287 1 pTxdnet->ipframe.destip[1]=dest_ip.words[1];
288 1 pTxdnet->ipframe.sourceip[0]=source_ip.words[0]; //填全0
289 1 pTxdnet->ipframe.sourceip[1]=source_ip.words[1];
290 1
291 1 pTxdnet->ipframe.crc=createipheadcrc(pTxdnet);
292 1
293 1 pTxdnet->etherframe.uDestID[0]=0xffff; //填写目的MAC,采用广播
294 1 pTxdnet->etherframe.uDestID[1]=0xffff;
295 1 pTxdnet->etherframe.uDestID[2]=0xffff;
296 1
297 1 send_packet(pTxdnet,34+len);
298 1
299 1 }
300
301 /*
302 *****************************************************************************************************
C51 COMPILER V7.06 DHCP 09/26/2008 13:36:22 PAGE 6
303 *FUNC: 如果一直没分配到IP,2s 请求一次
304 *NOTE:
305 1:封装发送广播一个DHCPDISCOVER(DHCP发现)包,目的端口填67,
306 2:进入SELECT状态
307 *****************************************************************************************************
308 */
309 void DHCPRequest(void)
310 {
311 1 if (ModelStatu == MODEL_CONFIG_STATU) { //配置状态下不去连接服务器
312 2 return;
313 2 }
314 1
315 1 DHCPValueInit(); //变量及各IP地址初始化
316 1
317 1 EA = 0;
318 1 DHCPReg.transactionid = initial_sequence_nr; //我的交互ID
319 1 initial_sequence_nr += 64000L;
320 1 EA = 1;
321 1
322 1 DebugMsg(60);
323 1 DHCP_Pack(OP_DHCPDISCOVER); // 封装发送广播一个DHCPDISCOVER(DHCP发现)包,目的端口填67,
324 1 DHCPReg.state = DHCP_SELECT; //进入SELECT状态
325 1 }
326
327
328 /*
329 *****************************************************************************************************
330 *FUNC: 响应码查询是不是想要的信息
331 *NOTE:
332 *****************************************************************************************************
333 */
334 //******* DHCP 响应选项中可能包含的信息代码 *******
335 #define DHCP_OPCODE_NUM 5
336 unsigned char code DHCP_Opcode_Table[DHCP_OPCODE_NUM]=
337 {
338 1, //0:子网掩码
339 3, //1:路由IP地址
340 51, //2:租期时间
341 6, //3:DNS服务器的IP,有可能是这样的:6 08 0 1 2 3 4 5 6 7 //8字节IP为主副DNS IP
342 54 //4:服务器ip,指DHCP服务器
343 };
344
345
346 //******* 对应上面的表 *******
347 #define OP_CODE_MASKIP 0 //子网掩码
348 #define OP_CODE_ROUTERS 1 //路由器IP
349 #define OP_CODE_USETIME 2 //租期时间
350 #define OP_CODE_DNS 3 //DNS服务器IP
351 #define OP_CODE_SEVERIP 4 //服务器IP
352
353 bit Find_Opcode_Table(unsigned char op_code,unsigned char *op_type)
354 {
355 1 unsigned char i;
356 1
357 1 for (i=0; i<DHCP_OPCODE_NUM; i++) {
358 2 if (op_code == DHCP_Opcode_Table[i]) {
359 3 *op_type = i;
360 3 return TRUE;
361 3 }
362 2 }
363 1 return FALSE;
364 1 }
C51 COMPILER V7.06 DHCP 09/26/2008 13:36:22 PAGE 7
365
366
367 /*
368 *****************************************************************************************************
369 *FUNC: 解析接收到的 DHCP 侦里的各信息
370 *NOTE:
371 *****************************************************************************************************
372 */
373 #define OPTIONS_IP 255
374 bit ParseOptions(unsigned char len,unsigned char type,
375 unsigned char xdata *psource,unsigned char xdata *pdest)
376 {
377 1 unsigned char i;
378 1
379 1 if (len != 4) { //IP长度不能超过4
380 2 return FALSE;
381 2 }
382 1
383 1 for (i=0; i<4; i++) {
384 2 *pdest = *psource++;
385 2 if ((type == OPTIONS_IP) && (*pdest > 255)) { //IP不能大于255
386 3 return FALSE;
387 3 }
388 2 pdest++;
389 2 }
390 1 return TRUE;
391 1 }
392
393
394 /*
395 *****************************************************************************************************
396 *FUNC: 解析接收到的 DHCP 侦
397 *NOTE: dhcp_len为DHCP包的长度不包含UDP头的8字节
398
399 #define OP_DHCPDISCOVER 1
400 #define OP_DHCPOFFER 2
401 #define OP_DHCPREQUEST 3
402 #define OP_DHCPDELINE 4
403 #define OP_DHCPACK 5
404 #define OP_DHCPNACK 6
405 #define OP_DHCPRELEASE 7
406 #define OP_DHCPINFORM 8
407 *****************************************************************************************************
408 */
409 void ParseRxdDHCP(union netcard xdata *pRxdnet,unsigned int dhcp_len)
410 {
411 1 unsigned int options_len=0; //选项长度,因其是可变长的
412 1 unsigned int i;
413 1 unsigned char op_code; //项目代码 如53表示是包类型
414 1 unsigned char op_len; //项目代码 对应的包长度
415 1 unsigned char op_type; //项目类型: 3为路由;1:子网掩码.....
416 1 unsigned char pack_type; //包类型,8种状态.
417 1
418 1 if (pRxdnet->dhcpframe.op != 2) { //1:请求, 2:应答
419 2 return;
420 2 }
421 1 if (pRxdnet->dhcpframe.transactionid != DHCPReg.transactionid) { //不是对应的包
422 2 return;
423 2 }
424 1
425 1 for (i=0; i<6; i++) { //16字节前6字节填是客户硬件地址
426 2 if (pRxdnet->dhcpframe.clientmac[i] != my_ethernet_address.bytes[i]){
C51 COMPILER V7.06 DHCP 09/26/2008 13:36:22 PAGE 8
427 3 return; //判断网卡地址是不是我的
428 3 }
429 2 }
430 1
431 1 if (dhcp_len < 240) { //236为DHCP封装包OPTIONS前所有的长度
432 2 return;
433 2 }
434 1 options_len = dhcp_len-240; //还要减去4字节的(OK)
435 1
436 1 /*
437 1 //观察续租返回包
438 1 if ((DHCPReg.state == DHCP_RENEW ) || (DHCPReg.state == DHCP_REBIND)) {
439 1 Uart0Putsl(pRxdnet->dhcpframe.options,options_len);
440 1 }
441 1 */
442 1
443 1 if (pRxdnet->dhcpframe.options[0] != 53) { //包类型
444 2 return;
445 2 } else {
446 2 op_code = 53;
447 2 op_len = pRxdnet->dhcpframe.options[1];
448 2 pack_type = pRxdnet->dhcpframe.options[2];
449 2 }
450 1
451 1 if (pack_type > 8) { //DHCP 报文类型只有8种
452 2 return;
453 2 }
454 1
455 1 if ((pack_type == OP_DHCPACK) || (pack_type == OP_DHCPOFFER)) { //是分配IP的应答包,此时OPTIONS里有想要
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -