⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arp.lst

📁 cs8900 c51应用
💻 LST
📖 第 1 页 / 共 4 页
字号:
 478                          if( arp_table[aenext].state == ARP_FREE ) {
 479                                  found=i;
 480                                  break;
 481                          }
 482                  }
 483                  
 484                  if(found != (-1) ) {
 485                          qstruct = &arp_table[found];
 486                          qstruct->state = ARP_RESERVED;
 487                          qstruct->type = type;   
 488                          return( (UINT8)found );
 489                  }
 490          
 491          
 492                  /* if no success, try ro find first temporary entry     */
 493                  /* on round-robin fashion                                                       */
 494                  
 495          
 496                  for( i=0; i<ARP_TSIZE; i++ ) {  
 497                          if( arp_table[aenext].type == ARP_TEMP_IP) {
 498                                  found = aenext;                 
 499                                  break;
 500                          }
 501                                  
 502                          /* Move to next entry */
 503                  
 504                          aenext = (aenext + 1);
 505                          if( aenext >= ARP_TSIZE )
 506                                  aenext = 1;
 507                                  
 508                  }
 509                  
 510                  
 511                  /* Was there free or temporary entries? */
 512                  
 513                  if( found == (-1) )
 514                          return(-1);
 515                          
 516                  /* Next time start from next entry      */
 517                  
 518                  aenext = (aenext + 1);  
 519                  if( aenext >= ARP_TSIZE )
 520                          aenext = 1;             
 521          
 522                  qstruct = &arp_table[found];
 523                  
 524                  /* Set ARP initial parameters   */
 525                  
 526                  qstruct->state = ARP_RESERVED;
 527                  qstruct->type = type;
 528                  
 529                  /* Was return(i)!!! <-wrong!!   */
 530                  
 531                  return((UINT8)found);
 532          
 533          
 534          }
 535          /** \brief Add given IP address and MAC address to ARP cache
 536           *      \author 
 537           *              \li Jari Lahti (jari.lahti@violasystems.com)
 538           *      \date 10.07.2002
 539           *      \param pra - protocol address (assumed IPv4)
C51 COMPILER V7.06   ARP                                                                   11/26/2004 11:32:44 PAGE 10  

 540           *      \param hwadr - pointer to Ethernet MAC address (6 bytes)
 541           *      \param type - type of address allocated if not found. Can be one of the
 542           *              following:
 543           *              \li #ARP_FIXED_IP
 544           *              \li #ARP_TEMP_IP
 545           *      \return
 546           *              \li 0 - Address already in cache. Refreshed.
 547           *              \li 1 - New entry in ARP cache created
 548           *
 549           *      New IP address is added to ARP cache based on the information
 550           *      supplied to function as parameters.
 551           */
 552          INT8 arp_add (UINT32 pra, UINT8* hwadr, UINT8 type)
 553          {
 554                  struct arp_entry *qstruct;
 555                  INT8 i;
 556                  INT8 j;
 557          
 558                  for( i=0; i<ARP_TSIZE; i++ ) {
 559                          qstruct = &arp_table[i];
 560                          
 561                          if( qstruct->state == ARP_FREE )
 562                                  continue;
 563                                  
 564                          if((qstruct->pradr == pra)&&(pra != IP_BROADCAST_ADDRESS)) {
 565                                  /* The address is in cache, refresh it   */
 566                                  
 567                                  ARP_DEBUGOUT(" Refreshing Existing ARP Entry..\n\r");
 568                          
 569                                  for( j=0; j<MAXHWALEN; j++ )            
 570                                          qstruct->hwadr[j] = *hwadr++;
 571                                          
 572                                  qstruct->ttl = ARP_TIMEOUT;
 573                                  qstruct->retries = ARP_MAXRETRY;
 574                                  qstruct->state = ARP_RESOLVED;
 575                  
 576                                  /* All OK       */
 577                          
 578                                  return (0);     
 579                          }
 580                  
 581                  }
 582                  
 583                  if(is_subnet(pra,&localmachine) == FALSE){
 584                          return (-1);
 585                  }
 586                  
 587                  if( localmachine.defgw == pra ) {
 588                          if(localmachine.defgw != 0) {
 589                                  type = ARP_FIXED_IP;
 590                          }
 591                  }
 592          
 593                  
 594                  /* Address was'nt on cache. Need to allocate new one    */
 595                  
 596                  ARP_DEBUGOUT("Allocating New ARP Entry..\n\r");
 597                  
 598                  i = arp_alloc(type);
 599                  
 600                  if( i < 0 )                             /* No Entries Left?     */
 601                          return(-1);
C51 COMPILER V7.06   ARP                                                                   11/26/2004 11:32:44 PAGE 11  

 602                                  
 603                  /* Fill the fields              */
 604                          
 605                  qstruct = &arp_table[i];
 606                          
 607                  qstruct->pradr = pra;                                                                           /* Fill IP                              */
 608                  
 609                  for(i=0; i<MAXHWALEN; i++)
 610                          qstruct->hwadr[i] = *hwadr++;                                                   /* Fill HW address              */
 611          
 612                  qstruct->retries = ARP_MAXRETRY;
 613                  qstruct->ttl = ARP_TIMEOUT;
 614                  qstruct->state = ARP_RESOLVED;                          
 615                  
 616                  ARP_DEBUGOUT("ARP Entry Created!..\n\r");
 617          
 618                  return(1);
 619          
 620          } 
 621          /** \brief Find an ARP entry given a protocol address
 622           *      \author 
 623           *              \li Jari Lahti (jari.lahti@violasystems.com)
 624           *      \date 01.11.2001
 625           *      \param pra - Protocol address (IPv4)
 626           *      \param machine - Pointer to configuration of network interface used
 627           *      \param type - Type of address allocated if not found. Can be one of the
 628           *              following:
 629           *              \li #ARP_FIXED_IP
 630           *              \li #ARP_TEMP_IP
 631           *      \return
 632           *              \li 0 - ARP entry not found or not ready yet (waiting for ARP response)
 633           *              \li struct arp_entry* - pointer to solved entry of ARP cache table
 634           *
 635           *      This function tries to resolve IPv4 address by checking the ARP cache
 636           *      table and sending ARP requested if needed. 
 637           */
 638          struct arp_entry* arp_find (LWORD pra, struct netif *machine, UINT8 type)
 639          {
 640                  struct arp_entry *qstruct;
 641                  INT8 i;
 642                  
 643                  ARP_DEBUGOUT("Trying to find MAC address from ARP Cache\n\r");
 644                  
 645                  /* Is the address in the cache  */
 646                  
 647                  for( i=0; i<ARP_TSIZE; i++ ) {
 648                          qstruct = &arp_table[i];
 649                          
 650                          if( qstruct->state == ARP_FREE )
 651                                  continue;
 652                          if( qstruct->pradr == pra) {
 653                                  /* The address is in cache, is it valid? */
 654                                  
 655                                  ARP_DEBUGOUT("Address In Cache\n\r");
 656                                  
 657                                  if( qstruct->state < ARP_RESOLVED ) {
 658                                          ARP_DEBUGOUT("Address in cache but unresolved :(\n\r");
 659                                          return(0);
 660                                  }
 661                                  /* All OK       */
 662                          
 663                                  return(qstruct);        
C51 COMPILER V7.06   ARP                                                                   11/26/2004 11:32:44 PAGE 12  

 664                          }
 665                  
 666                  }
 667                  
 668                  /* The address wasn't on the cache. Is it in our Subnet?        */
 669                  
 670                  if( is_subnet(pra, machine) ) {
 671                          /* Yep, we need to send ARP REQUEST     */
 672                          
 673                          ARP_DEBUGOUT("Need to send ARP Request to local network..\n\r");
 674                          
 675                          if( machine->defgw == pra ) {
 676                                  if(machine->defgw != 0) {
 677                                          type = ARP_FIXED_IP;
 678                                  }
 679                          }
 680                          i = arp_alloc(type);
 681                          
 682                          if( i < 0 )                             /* No Entries Left?     */
 683                                  return(0);
 684                                  
 685                          /* Send Request after filling the fields        */
 686                          
 687                          qstruct = &arp_table[i];
 688                          
 689                          qstruct->pradr = pra;                                           /* Fill IP                              */
 690                          qstruct->hwadr[0] = 0xFF;                                       /* Fill Broadcast IP    */
 691                          qstruct->hwadr[1] = 0xFF;
 692                          qstruct->hwadr[2] = 0xFF;
 693                          qstruct->hwadr[3] = 0xFF;
 694                          qstruct->hwadr[4] = 0xFF;
 695                          qstruct->hwadr[5] = 0xFF;
 696                          qstruct->retries = ARP_MAXRETRY;
 697                          qstruct->ttl = ARP_RESEND;
 698                          arp_send_req( i );
 699                          qstruct->state = ARP_PENDING;                           /* Waiting for Reply    */
 700                          
 701                          return(0);
 702                  
 703                  } 
 704          
 705                  /* The Address belongst to the outern world, need to use MAC of                 */
 706                  /* Default Gateway                                                                                                              */
 707                  
 708                  ARP_DEBUGOUT("Need to use MAC of Default GW\n\r");
 709                  
 710                  /* Check for Broadcast                                                                                                  */
 711                  
 712                  if(machine->defgw == 0)                         /* It's not specified   */
 713                          return(0);
 714                  
 715                  
 716                  for( i=0; i<ARP_TSIZE; i++ ) {
 717                          qstruct = &arp_table[i];
 718                          
 719                          if( qstruct->state == ARP_FREE )
 720                                  continue;
 721                                  
 722                          if( qstruct->pradr == machine->defgw ) {
 723                                  /* The address is in cache, is it valid? */
 724                           
 725                                  
C51 COMPILER V7.06   ARP                                                                   11/26/2004 11:32:44 PAGE 13  

 726                                  if( qstruct->state < ARP_RESOLVED ) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -