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

📄 ip.lst

📁 cs8900 c51应用
💻 LST
📖 第 1 页 / 共 3 页
字号:
 181          
 182          
 183                  if((received_ip_packet.dip != localmachine.localip )&&
 184                          (received_ip_packet.dip != IP_BROADCAST_ADDRESS)) {
 185          
 186                          /* It's not for us. Check still if ICMP with rigth physical     */
 187                          /* address that migth be used to set temporary IP                       */
 188                          
 189                          IP_DEBUGOUT("IP address does not match!\n\r");
 190                          
 191                          if( received_ip_packet.protocol != IP_ICMP) 
 192                                  return(-1);
 193                                  
 194                          /* Check physical address                       */
 195          #ifdef CS8900           
 196                          for(i=0; i<PHY_ADR_LEN; i++)
 197                          {
 198                                  if(frame->destination[i] != localmachine.localHW[i])
 199                                          return(-1);
 200                          }       
 201          #endif
 202                  }
 203                  
 204                  
 205                  /* Is there options to copy?            */
 206                          
 207                  olen = ((received_ip_packet.vihl & 0x0F) << 2) - IP_MIN_HLEN;
 208                  
 209                  /* Somebody bluffing with too long option field?        */
 210                  
 211                  if(olen > MAX_IP_OPTLEN) {
 212                          IP_DEBUGOUT("ERROR:Size of maximum allowed IP option lengt exceeded!\n\r");
 213                          return(-1);
 214                  }
 215                  
 216                  if( olen > (frame->frame_size - ETH_HEADER_LEN - IP_HLEN) )     {
 217                          IP_DEBUGOUT("ERROR:IP option field too long!\n\r");
 218                          return(-1);
 219                  }
 220                          
 221                  for( i=0; i < olen; i++ ) {
 222                          received_ip_packet.opt[i] = RECEIVE_NETWORK_B();        
 223                          IP_DEBUGOUT("IP Options..\n\r");
 224                  }
 225                  
 226                  if(received_ip_packet.tlen >  (frame->frame_size - ETH_HEADER_LEN) ) {
 227                          IP_DEBUGOUT("ERROR: Total len too long\r\n");
 228                          return(-1);
 229                  }
 230                  
 231                  /* Is the checksum OK?  */
 232                  
C51 COMPILER V7.06   IP                                                                    11/26/2004 11:32:45 PAGE 5   

 233                  IP_DEBUGOUT("Validating the IP checksum..\n\r");
 234                  
 235                  if ( ip_check_cs(&received_ip_packet) != TRUE ) {
 236                          IP_DEBUGOUT("IP Checksum Corrupted..\n\r");
 237                          return(-1);
 238                  }       
 239                  
 240                  IP_DEBUGOUT("..Checksum OK!\n\r");      
 241                  
 242                  /* Add the address to ARP cache */
 243                  
 244                  if( received_ip_packet.sip != IP_BROADCAST_ADDRESS)
 245                          arp_add( received_ip_packet.sip, &frame->source[0], ARP_TEMP_IP);
 246                  
 247                  /* Calculate the start of next layer data       */
 248                  
 249                  received_ip_packet.buf_index = frame->buf_index + IP_HLEN + olen;
 250                  
 251                  /* Is this packet fragmented?                                           */
 252                  /* We don't deal with those                                                     */
 253                  /* TODO: Implement Stub handler for more mem. uP's      */
 254                  
 255                  if( received_ip_packet.frags & (IP_MOREFRAGS | IP_FRAGOFF) ) {
 256                          IP_DEBUGOUT("Fragmented IP packet\r\n");
 257                          return(-1);
 258                  }
 259                  /* checking moved upwards!
 260                  if( received_ip_packet.frags & IP_FRAGOFF )     {
 261                          IP_DEBUGOUT("Fragmented IP packet\r\n");
 262                          return(-1);
 263                  }
 264                  */
 265                  
 266                  IP_DEBUGOUT("Leaving IP succesfully..\n\r");
 267                  
 268                  return(received_ip_packet.tlen - IP_HLEN - olen);
 269                  
 270          }
 271          
 272          /** \brief Try to send out IP frame
 273           *      \author 
 274           *              \li Jari Lahti
 275           *      \date 11.06.2002
 276           *      \param ipadr remote IP address
 277           *      \param pcol protocol over IP used. Can be one of the
 278           *              following:
 279           *              \li #IP_ICMP
 280           *              \li #IP_UDP
 281           *              \li #IP_TCP
 282           *      \param tos type of service required
 283           *      \param ttl time to live header field of IP packet
 284           *      \param dat pointer to data buffer
 285           *      \param len length of data to be sent in IP datagram
 286           *      \return
 287           *              \li -1 - general error
 288           *              \li -2 - ARP cache not ready
 289           *              \li >0 - number of data bytes sent (packet OK)
 290           *
 291           *      Invoke this function to perform all of the necessary preparation in
 292           *      order to send out an IP packet. These include:
 293           *              \li Consulting ARP cache for HW address to send the packet to
 294           *              \li Filling send_ip_packet variable with correct values
C51 COMPILER V7.06   IP                                                                    11/26/2004 11:32:45 PAGE 6   

 295           *              \li Calculating checksum for the IP packet
 296           *              \li Adding datalink header information
 297           *              \li     Sending IP header and data
 298           *              \li Instructing NIC to send the data
 299           */
 300          INT16 process_ip_out (UINT32 ipadr, UINT8 pcol, UINT8 tos, UINT8 ttl, UINT8* dat, UINT16 len)
 301          {
 302                  struct arp_entry *qstruct;
 303                  UINT16 i;
 304                  
 305                  /* Try to get MAC address from ARP cache        */
 306          #ifdef CS8900
 307                  qstruct = arp_find(ipadr, &localmachine, ARP_TEMP_IP);
 308                  
 309                  if( qstruct == 0 )              /* Not ready yet        */
 310                          return(-2);
 311          #endif
 312                          
 313                  /* Select network buffer                                                */
 314                  /* TODO: This network related stuff should              */
 315                  /* be moved and abstracted to Ethernet layer    */
 316                  
 317                  switch(pcol) {
 318                          case IP_ICMP:
 319                          
 320                                  NETWORK_SEND_INITIALIZE(ICMP_BUF);
 321                                  IP_DEBUGOUT("Assembling IP packet to ICMP buffer\n\r");
 322                                  
 323                                  break;
 324                                  
 325                          case IP_UDP:
 326                          
 327                                  NETWORK_SEND_INITIALIZE(UDP_BUF);
 328                                  IP_DEBUGOUT("Assembling IP packet to UDP buffer\n\r");
 329                                  
 330                                  break;
 331                                  
 332                          case IP_TCP:
 333                          
 334                                  NETWORK_SEND_INITIALIZE(TCP_BUF);
 335                                  IP_DEBUGOUT("Assembling IP packet to TCP buffer\n\r");
 336                                  
 337                                  break;
 338                  
 339                          default:                        /* Unknown protocol     */
 340                                  return(-1);
 341                  }
 342                  
 343                  /* Fill the Ethernet information        */
 344                  
 345                  for( i=0; i<MAXHWALEN; i++)     {
 346                          send_frame.destination[i] = qstruct->hwadr[i];
 347                          send_frame.source[i] = localmachine.localHW[i];
 348                  }
 349                  
 350                  send_frame.protocol = PROTOCOL_IP;
 351                  
 352                  NETWORK_ADD_DATALINK(&send_frame);
 353                  
 354                  /* Construct the IP header      */
 355                  
 356                  send_ip_packet.vihl = IP_DEF_VIHL;
C51 COMPILER V7.06   IP                                                                    11/26/2004 11:32:45 PAGE 7   

 357                  send_ip_packet.tos = tos;
 358                  send_ip_packet.tlen = IP_HLEN + len;
 359                  send_ip_packet.id = ip_id++;
 360                  send_ip_packet.frags = 0;
 361                  send_ip_packet.ttl = ttl;
 362                  send_ip_packet.protocol = pcol;
 363                  send_ip_packet.checksum = 0;
 364                  send_ip_packet.sip = localmachine.localip;
 365                  send_ip_packet.dip = ipadr;
 366                  
 367                  /* Calculate checksum for the IP header */
 368                  
 369                  send_ip_packet.checksum = ip_construct_cs( &send_ip_packet );
 370                  
 371                  /* Assemble bytes to network    */
 372                  
 373                  SEND_NETWORK_B(send_ip_packet.vihl);
 374                  SEND_NETWORK_B(send_ip_packet.tos);
 375                  SEND_NETWORK_B( (UINT8)(send_ip_packet.tlen >> 8) );
 376                  SEND_NETWORK_B( (UINT8)send_ip_packet.tlen );           
 377                  SEND_NETWORK_B( (UINT8)(send_ip_packet.id >> 8) );
 378                  SEND_NETWORK_B( (UINT8)send_ip_packet.id );

⌨️ 快捷键说明

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