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

📄 ip_udp_arp.c

📁 可以在linux和wince下运行
💻 C
📖 第 1 页 / 共 3 页
字号:
                      { Print(" udp lan ret=%d len=%d errno=%d\n",ret_val,len,errno); }
        close(sock);
}
void udp_send_direct(ULONG ip,ULONG num)
{
 
  long   ret_val;
  ULONG  i,mess[4],ptr;
  USHORT len;
  UCHAR  *buff;
  int opt, optsize=4;
  int sock;
  struct sockaddr_in object, user_addr[UDP_PORT_MAX_NUM];  

      memset((const char *)&user_addr[24], 0, sizeof(struct sockaddr_in) );
       
      user_addr[24].sin_family      = AF_INET;
      user_addr[24].sin_addr.s_addr = htonl(LOCAL_IP_ADDR);
      user_addr[24].sin_port        = htons(port[24]);
     
      sock = socket(AF_INET, SOCK_DGRAM, UDP);
      if(sock == -1)
        {  
           ret_val=errno;
           Print("socket( %d ) ERROR %x!\n", 24,ret_val);
           return;
        }
        
      if( bind(sock, (struct sockaddr *)&user_addr[24], sizeof(user_addr[24]) )!=0 )
       {
           ret_val=errno;
         close(sock);
//         Print(" bind( %d ), ERROR %x!\n",24,ret_val);	
         return;
       }
       opt=1;
        ret_val=setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char *)&opt, optsize);
                  if(ret_val != 0)
                      { Print(" setsockopt ret=%d errno=%d\n",ret_val,errno); }

	ras_to_udp_send.port=1719;
//	if ((ip & 0xffffff00) == (LOCAL_IP_ADDR & 0xffffff00))
//	ras_to_udp_send.ip_addr=ip | 0xff;
//	else
	ras_to_udp_send.ip_addr=ip;
	ras_to_udp_send.self_port=1719;
	ras_to_udp_send.busy=1;
	memcpy(&ras_to_udp_send.data[1],&LOCAL_IP_ADDR,4);
	memcpy(&ras_to_udp_send.data[5],&num,4);
	ras_to_udp_send.data[0]='D';
	ras_to_udp_send.len=9;

                  memset( (const char *)&object, 0, sizeof(object) );
                  object.sin_family      = AF_INET;
                  object.sin_addr.s_addr = htonl(ras_to_udp_send.ip_addr);
                  object.sin_port        = htons(ras_to_udp_send.port);                  
                  len = ras_to_udp_send.len;
                  buff = ras_to_udp_send.data;
                  ret_val = sendto(sock, buff, len, 0, (struct sockaddr *)&object, sizeof(object));	
                  if(ret_val != len)
                      { Print(" udp lan ret=%d len=%d errno=%d\n",ret_val,len,errno); }
        close(sock);
}

unsigned long (*pNA_anounce_packet)(ULONG, char *, ULONG, ULONG, char *, char *);
long Receive(unsigned long type,void *buf,unsigned long size,
             unsigned long info,char *srcaddr,char *dstaddr)
{
  extern struct ni_funcs Ni_funcs;	/* defined in `devices/lan/lan8xx.c' */
  long    nb;
  int     len;
  ULONG   i,j,ret_val,mess[4];
  static ULONG udp_rece_ptr=0;

    if (udp_setup)
	{
		type = type & 0xffff;
		if (type == 0x0806)	/* ARP */         
		{
			ULONG rc;
			mess[0] = (ULONG) buf;
			mess[1] = (ULONG) size;
			mess[2] = (ULONG) srcaddr;
			mess[3] = (ULONG) dstaddr;
			rc = q_send(qid_arp, mess);
			if (rc != 0)
				Ni_funcs.freemsg((mblk_t *) buf);
			return 1;
		}
		if (type == 0x0800)	                                                /* IP */
		{
			IP_HEADER * ip = (IP_HEADER *)((mblk_t*)buf)->b_rptr;
			if(ip->ip_dst  == SysVars.Lan1IP) 
			{
		    	if(configdata.B_MacAddress==0)
				{
					configdata.B_MacAddress=1;
					memcpy(configdata.MacAddress,EthernetAddress,6);
				}
									
			if(ip->ip_proto == 0x11)										/* UDP */
			{
				UDP_HEADER * udp = (UDP_HEADER *)((UCHAR *)ip + ((ip->ip_verlen & 0x0f) << 2));
				USHORT port = udp->udp_dst_port;

				if (((port >= 6000) && (port <= 6120)) || (port == 1719) || (port == 10000))
				{
//                    Print("dest_ip=%x ",ip->ip_dst);
//                    Print("port=%d ",port);
//                    Print("sour_port=%d ",udp->udp_src_port);
//                    Print("sour_ip=%x ",ip->ip_src);
//                    Print("ip_proto=%d \n",ip->ip_proto);
					udp_rece_to_app[udp_rece_ptr].len       = udp->udp_pkt_len - 8;                  /* data len */
					udp_rece_to_app[udp_rece_ptr].self_port = port;              /* self port number */
					udp_rece_to_app[udp_rece_ptr].port      = udp->udp_src_port;        /* object port number */
					udp_rece_to_app[udp_rece_ptr].ip_addr   = ip->ip_src;      /* object port number */
					udp_rece_to_app[udp_rece_ptr].busy = 1;
                    memcpy(&udp_rece_to_app[udp_rece_ptr].data[0],&udp->udp_data[0],udp->udp_pkt_len - 8);
					
					mess[0] =  110;   // udp identfy
					mess[1] =  udp_rece_ptr;
					mess[2] = (ULONG)&udp_rece_to_app[udp_rece_ptr];
					ret_val = q_send(udp_rece_quene_ID, mess);
					if(ret_val !=0) 
					{ 
						Print("udp lan rece task q_send error 0x%x \n",errno); 
					}
					if(udp_rece_ptr == (UDP_DATA_MAX_RECE_BUFF_NUM-1) )
				     udp_rece_ptr = 0; 
					else   
				     udp_rece_ptr++; 
                    // Print("* ");
                    Ni_funcs.freemsg((mblk_t *) buf); 
					return 1;
				} /* UDP to V/FDSP or echo test*/
			} /* UDP */
		   }
		   else
		   {
			if((ip->ip_dst & 0xffffff00) == (SysVars.Lan1IP & 0xffffff00)) 
			{
			    if (ip->ip_dst & 0xff == 0xff)
			    {
		         if ((configdata.RemoteEndpoint[0].ip == ip->ip_src) && (CENTER_STRING==1))
		         {

			      if(ip->ip_proto == 0x11)										/* UDP */
			      {
			 	    UDP_HEADER * udp = (UDP_HEADER *)((UCHAR *)ip + ((ip->ip_verlen & 0x0f) << 2));
				    USHORT port = udp->udp_dst_port;

				    if (((port >= 6000) && (port <= 6120)) || (port == 1719) || (port == 10000))
				    {
//                    Print("dest_ip=%x ",ip->ip_dst);
//                    Print("port=%d ",port);
//                    Print("sour_port=%d ",udp->udp_src_port);
//                    Print("sour_ip=%x ",ip->ip_src);
//                    Print("ip_proto=%d \n",ip->ip_proto);
					  udp_rece_to_app[udp_rece_ptr].len       = udp->udp_pkt_len - 8;                  /* data len */
					  udp_rece_to_app[udp_rece_ptr].self_port = port;              /* self port number */
					  udp_rece_to_app[udp_rece_ptr].port      = udp->udp_src_port;        /* object port number */
					  udp_rece_to_app[udp_rece_ptr].ip_addr   = ip->ip_src;      /* object port number */
					  udp_rece_to_app[udp_rece_ptr].busy = 1;
                      memcpy(&udp_rece_to_app[udp_rece_ptr].data[0],&udp->udp_data[0],udp->udp_pkt_len - 8);
					
					  mess[0] =  110;   // udp identfy
					  mess[1] =  udp_rece_ptr;
					  mess[2] = (ULONG)&udp_rece_to_app[udp_rece_ptr];
					  ret_val = q_send(udp_rece_quene_ID, mess);
					  if(ret_val !=0) 
					  { 
						Print("udp lan rece task q_send error 0x%x \n",errno); 
					  }
					  if(udp_rece_ptr == (UDP_DATA_MAX_RECE_BUFF_NUM-1) )
				      udp_rece_ptr = 0; 
					  else   
				      udp_rece_ptr++; 
                    // Print("* ");
                      Ni_funcs.freemsg((mblk_t *) buf); 
					  return 1;
				    } /* UDP to V/FDSP or echo test*/
			      } /* UDP */
			     }
			    }
			}
		   }
		} /* ARP or IP */
    }
//	Print("MAC:: %02X %02X %02X %02X %02X %02X---\n",srcaddr[0],srcaddr[1],srcaddr[2],srcaddr[3],srcaddr[4],srcaddr[5]);
    return (*pNA_anounce_packet)(type, buf, size, info, srcaddr, dstaddr);	
}

/************************************************************************

/* udp rece, data from udp port to upper_layer */
void udp_rece(void)
{
  long    nb;
  UCHAR   data_buff[UDP_DATA_MAX_LEN];
  int     len;
  ULONG   i,j,ret_val,mess[4], udp_rece_ptr;
  struct  sockaddr_in addr;
  struct  timeval wait;     
  fd_set  tmp;
  fd_set  udp_rece_mask;
  ULONG   tmp_val1;
  
  int tmpr = sizeof(addr);  
  
  FD_ZERO(&udp_rece_mask);
  
  for(i=0; i<UDP_PORT_MAX_NUM; i++)
  { 
	FD_SET(udp_share_sock_rece[i], &udp_rece_mask);
  }

  udp_rece_ptr = 0;

  wait.tv_sec = 1; /* wait for 1 second */
  wait.tv_usec =0;
  UdpCount = 0;
  while(1)
  { 
 
    tm_wkafter(1);
    tmp = udp_rece_mask; 
	nb=select(FD_SETSIZE, &tmp, (fd_set *)0, (fd_set *)0, (struct timeval *)&wait);
	if (nb>0)
	{
	      tmp_val1 = tmp.fds_bits[0];
	      i=0;
	                 
	      while(1)
	          {
	                   tmp_val1 = (tmp_val1<<1);
	                   if((tmp_val1 & 0xffffffff)==0)
	                       {break;}
	                       
	                   if((tmp_val1 & 0x80000000) == 0)
	                       {  i++;
	                          continue;
	                       }
	                 
	                 			 
	    			   len = recvfrom(udp_share_sock_rece[i], &udp_rece_to_app[udp_rece_ptr].data[0], UDP_DATA_MAX_LEN, 0 ,(struct sockaddr *)&addr, &tmpr);

					   if(len <=0) 
					     { 
					   	    Print(" udp rece recvfrom error \n"); 
					   	    i++;
					        continue;
					     }
					   else
					     {   
//			  		        Print(" UDP  receive self_port =%d , from ip = %I, port = %d \n",port[i],addr.sin_addr.s_addr,addr.sin_port );
//				  		    for(j=0; j<len; j++)	
//						    {  udp_rece_to_app[udp_rece_ptr].data[j] = data_buff[j]; }	
//                            memcpy(&udp_rece_to_app[udp_rece_ptr].data[0],&data_buff[0],len);
						    udp_rece_to_app[udp_rece_ptr].len       = len;                  /* data len */
					  	    udp_rece_to_app[udp_rece_ptr].self_port = port[i];              /* self port number */
							udp_rece_to_app[udp_rece_ptr].port      = addr.sin_port;        /* object port number */
							udp_rece_to_app[udp_rece_ptr].ip_addr   = addr.sin_addr.s_addr;      /* object port number */
							udp_rece_to_app[udp_rece_ptr].busy = 1;
							memset( &mess, 0, sizeof(mess) );
						
							mess[0] =  110;   // udp identfy
							mess[1] =  udp_rece_ptr;
							mess[2] = (ULONG)&udp_rece_to_app[udp_rece_ptr];
//							FN_RTP(mess[0],mess[1],mess[2],(ULONG *) mess[3]);
							ret_val = q_send(udp_rece_quene_ID, mess);
							UdpCount++;
							if(ret_val !=0) 
							   { Print("udp lan rece task q_send error 0x%x \n",errno); }
	                    
							if(udp_rece_ptr == (UDP_DATA_MAX_RECE_BUFF_NUM-1) )
						       { udp_rece_ptr = 0; }
							else   
							   { udp_rece_ptr++;  }	
			
			                i=i+1;
			             }
  
           }
    }  
	else	
	{
//		Print("\ntimeout!\n");
	
	}
  } //end do
  Print("lan rece task error JMPout \n");
  return;
}


void lansend(ULONG ipto, mblk_t *m, USHORT size)
{
	char * hwa;
	hwa = arp_out(ipto);
        if(hwa == NULL)
		pna_freemsg(m);
	else
	{
		ni_send(hwa, (char *)m, size, (USHORT)0x0800);
	}
}


/*------------------------------------------------------------------
|Function name:h245UserInputIndication
|-------------------------------------------------------------------
|Date : 2001.10.08
|------------------------------------------------------------------*/
int SendToIp(ULONG dst_ip_addr,
					 USHORT dst_udp_port,
					 USHORT src_udp_port,
					 UCHAR * buf,
					 int len)
{
    extern char * arp_out(ULONG ip);
	IP_HEADER *ip;
	UDP_HEADER *udp;
	mblk_t *m;
	USHORT *ps;
	char * hwa;
	ULONG sum = 0;
	int total_len;
	int i,j;

	if (dst_ip_addr == 0)
	{
	    dst_ip_addr = SysVars.Lan1IP; /*test*/
		Print("dst_ip_addr = 0\n");
	}
	for(j=0;j<10;j++)
	total_len = IP_PLUS_UDP_HEADER_LEN + len;
	m = pna_allocb(total_len + 2, PNA_TXQ_MEMPOOL);
	if (m == NULL)
	{
        Print("pna_allocb error len=%d\n",len);
		return -1;
	}

	/* fill the IP header */
	ip = (IP_HEADER *)m->b_wptr;
	ip->ip_verlen	= (UCHAR) 0x45;		/* VERSION and HEADER LENGTH */
	ip->ip_tos		= TOS_Priority | TOS_D |TOS_T | TOS_R;		/* TYPE OF SERVICE */
	ip->ip_len		= total_len;		/* TOTAL PACKET LENGTH */
	ip->ip_id		= ++datagram_id;	/* DATAGRAM ID */
	ip->ip_fragoff	= (USHORT) 0x00;	/* FRAGMENT */
	ip->ip_ttl		= (UCHAR) 0xFF;		/* TIME TO LIVE */
	ip->ip_proto	= (UCHAR) 0x11;		/* IP PROTOCOL (0x11: UDP) */
	ip->ip_chksum	= (USHORT) 0x00;	/* HEADER CHECKSUM */
	ip->ip_src		= SysVars.Lan1IP;	/* SOURCE IP ADDRESS */
	ip->ip_dst		= dst_ip_addr;		/* DESTINATION IP ADDRESS */

	if (dst_ip_addr == SysVars.Lan1IP)
	{
	    Print("send to itself!\n");        //2001.10.10
	    pna_freemsg(m);
		return -1;
//		ip->ip_dst = 0xC0A800A8;	/* 192.168.0.168 */
//		ip->ip_len += 2;
	}

	ps = (USHORT *)m->b_wptr;
	for(i = 0; i < 10; i++)
	   sum += *ps++;
	sum = (sum >> 16) + (sum & 0xffff);
	sum += (sum >> 16);
	ip->ip_chksum = (USHORT)~sum;	/* HEADER CHECKSUM */

	/* fill the UDP header */
	udp = (UDP_HEADER *)(m->b_wptr + 20);
	udp->udp_src_port = src_udp_port;	/* UDP SOURCE PORT */
	udp->udp_dst_port = dst_udp_port;	/* UDP DESTINATION PORT */
	udp->udp_pkt_len  = (USHORT)(8 + len);		/* UDP DATAGRAM LENGTH */
	udp->udp_chksum   = 0;	/* UDP CHECKSUM */


	/* copy the data */
	memcpy(m->b_wptr + IP_PLUS_UDP_HEADER_LEN, buf, len);
	m->b_wptr += total_len;
	m->b_cont = 0;

	udp->udp_chksum = UdpChecksum(ip);	/* make udp checksum */

	if (   (dst_ip_addr    & SysVars.Lan1SubnetMask)
		== (SysVars.Lan1IP & SysVars.Lan1SubnetMask))
		lansend(ip->ip_dst, m, (USHORT)total_len);
	else
		lansend(SysVars.DefGtwyIP, m, (USHORT)total_len);
		
	return 0;
}
/***************************************************************************
*     udpcksum -  compute a UDP pseudo-header checksum
/**************************************************************************/
USHORT UdpChecksum(IP_HEADER * ip)
{
	UDP_HEADER * udp = (UDP_HEADER *)ip->ip_data;
	USHORT * sptr;
	ULONG	ucksum = 0;
	int		i;
    int  len = udp->udp_pkt_len;
	
	sptr = (unsigned short *) &ip->ip_src;

	for (i = 0; i < 4; i++)
		ucksum += *sptr++;
	ucksum += (unsigned short)(IPT_UDP + len);
/*	Print("\n\n!!!!!BEGIN!!!!!\nudpchecksum = %08x\n", ucksum);	*/

	sptr = (unsigned short *)udp;
	if (len % 2) 
	{
		((char *)udp)[len] = 0;	/* pad */
		len += 1;
	}
	len /= 2;	/* convert to length in shorts */

	for (i = 0; i < len; i++)
	{
		ucksum += *sptr++;
		/*Print("sptr(%d) = %04x\n", (i + 1), *(sptr - 1)); */
	}
	/*Print("udpchecksum = %08x\n", ucksum);*/
	ucksum = (ucksum >> 16) + (ucksum & 0xffff);
	ucksum += (ucksum >> 16); 
	/*Print("udpchecksum = %04x\n", (USHORT)(~ucksum & 0xffff));*/
	
	return ((USHORT)(~ucksum & 0xffff));
}

⌨️ 快捷键说明

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