summaryrefslogtreecommitdiff
path: root/nuttx/net/uip/uip-fw.c
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2007-09-16 22:12:04 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2007-09-16 22:12:04 +0000
commit9e15c4be04516ac9957f33ea179a188cc2a081a0 (patch)
tree77fe706483352a68b683ecde64215d54cbc89f4f /nuttx/net/uip/uip-fw.c
parent4077a70fc256a7dd65febe986f176b8ac62091fc (diff)
downloadpx4-nuttx-9e15c4be04516ac9957f33ea179a188cc2a081a0.tar.gz
px4-nuttx-9e15c4be04516ac9957f33ea179a188cc2a081a0.tar.bz2
px4-nuttx-9e15c4be04516ac9957f33ea179a188cc2a081a0.zip
Associate address with network driver; implement ioctl calls to set addresses
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@345 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/net/uip/uip-fw.c')
-rw-r--r--nuttx/net/uip/uip-fw.c209
1 files changed, 101 insertions, 108 deletions
diff --git a/nuttx/net/uip/uip-fw.c b/nuttx/net/uip/uip-fw.c
index 18834c883..b529c6e06 100644
--- a/nuttx/net/uip/uip-fw.c
+++ b/nuttx/net/uip/uip-fw.c
@@ -52,44 +52,50 @@ static struct uip_fw_netif *defaultnetif = NULL;
struct tcpip_hdr
{
- /* IP header. */
- uint8 vhl,
- tos;
- uint16 len,
- ipid,
- ipoffset;
- uint8 ttl,
- proto;
+ /* IP header */
+
+ uint8 vhl;
+ uint8 tos;
+ uint16 len;
+ uint16 ipid;
+ uint16 ipoffset;
+ uint8 ttl;
+ uint8 proto;
uint16 ipchksum;
- uint16 srcipaddr[2],
- destipaddr[2];
-
- /* TCP header. */
- uint16 srcport,
- destport;
- uint8 seqno[4],
- ackno[4],
- tcpoffset,
- flags,
- wnd[2];
+ in_addr_t srcipaddr;
+ in_addr_t destipaddr;
+
+ /* TCP header */
+
+ uint16 srcport;
+ uint16 destport;
+ uint8 seqno[4];
+ uint8 ackno[4];
+ uint8 tcpoffset;
+ uint8 flags;
+ uint8 wnd[2];
uint16 tcpchksum;
- uint8 urgp[2];
+ uint8 urgp[2];
uint8 optdata[4];
};
-struct icmpip_hdr {
- /* IP header. */
- uint8 vhl,
- tos,
- len[2],
- ipid[2],
- ipoffset[2],
- ttl,
- proto;
+struct icmpip_hdr
+{
+ /* IP header */
+
+ uint8 vhl;
+ uint8 tos;
+ uint8 len[2];
+ uint8 ipid[2];
+ uint8 ipoffset[2];
+ uint8 ttl;
+ uint8 proto;
uint16 ipchksum;
- uint16 srcipaddr[2],
- destipaddr[2];
- /* ICMP (echo) header. */
+ in_addr_t srcipaddr;
+ in_addr_t destipaddr;
+
+ /* ICMP (echo) header */
+
uint8 type, icode;
uint16 icmpchksum;
uint16 id, seqno;
@@ -115,8 +121,8 @@ struct icmpip_hdr {
struct fwcache_entry
{
uint16 timer;
- uint16 srcipaddr[2];
- uint16 destipaddr[2];
+ in_addr_t srcipaddr;
+ in_addr_t destipaddr;
uint16 ipid;
uint8 proto;
uint8 unused;
@@ -159,22 +165,6 @@ void uip_fw_init(void)
}
}
-/* Check if an IP address is within the network defined by an IP
- * address and a netmask.
- *
- * ipaddr The IP address to be checked.
- * netipaddr The IP address of the network.
- * netmask The netmask of the network.
- *
- * Return: Non-zero if IP address is in network, zero otherwise.
- */
-
-static unsigned char ipaddr_maskcmp(uint16 *ipaddr, uint16 *netipaddr, uint16 *netmask)
-{
- return (ipaddr[0] & netmask [0]) == (netipaddr[0] & netmask[0]) &&
- (ipaddr[1] & netmask[1]) == (netipaddr[1] & netmask[1]);
-}
-
/* Send out an ICMP TIME-EXCEEDED message.
*
* This function replaces the packet in the d_buf buffer with the
@@ -183,10 +173,10 @@ static unsigned char ipaddr_maskcmp(uint16 *ipaddr, uint16 *netipaddr, uint16 *n
static void time_exceeded(struct uip_driver_s *dev)
{
- uint16 tmp16;
+ in_addr_t tmp_addr;
/* We don't send out ICMP errors for ICMP messages. */
- if(ICMPBUF->proto == UIP_PROTO_ICMP) {
+ if (ICMPBUF->proto == UIP_PROTO_ICMP) {
dev->d_len = 0;
return;
}
@@ -202,17 +192,16 @@ static void time_exceeded(struct uip_driver_s *dev)
ICMPBUF->icmpchksum = ~uip_chksum((uint16 *)&(ICMPBUF->type), 36);
/* Set the IP destination address to be the source address of the
- original packet. */
- tmp16= BUF->destipaddr[0];
- BUF->destipaddr[0] = BUF->srcipaddr[0];
- BUF->srcipaddr[0] = tmp16;
- tmp16 = BUF->destipaddr[1];
- BUF->destipaddr[1] = BUF->srcipaddr[1];
- BUF->srcipaddr[1] = tmp16;
+ * original packet.
+ */
+
+ tmp_addr = BUF->destipaddr;
+ BUF->destipaddr = BUF->srcipaddr;
+ BUF->srcipaddr = tmp_addr;
/* Set our IP address as the source address. */
- BUF->srcipaddr[0] = uip_hostaddr[0];
- BUF->srcipaddr[1] = uip_hostaddr[1];
+
+ BUF->srcipaddr = dev->d_ipaddr;
/* The size of the ICMP time exceeded packet is 36 + the size of the
IP header (20) = 56. */
@@ -245,11 +234,11 @@ static void fwcache_register(struct uip_driver_s *dev)
fw = NULL;
/* Find the oldest entry in the cache. */
- for(i = 0; i < FWCACHE_SIZE; ++i) {
- if(fwcache[i].timer == 0) {
+ for (i = 0; i < FWCACHE_SIZE; ++i) {
+ if (fwcache[i].timer == 0) {
fw = &fwcache[i];
break;
- } else if(fwcache[i].timer <= oldest) {
+ } else if (fwcache[i].timer <= oldest) {
fw = &fwcache[i];
oldest = fwcache[i].timer;
}
@@ -257,10 +246,8 @@ static void fwcache_register(struct uip_driver_s *dev)
fw->timer = FW_TIME;
fw->ipid = BUF->ipid;
- fw->srcipaddr[0] = BUF->srcipaddr[0];
- fw->srcipaddr[1] = BUF->srcipaddr[1];
- fw->destipaddr[0] = BUF->destipaddr[0];
- fw->destipaddr[1] = BUF->destipaddr[1];
+ fw->srcipaddr = BUF->srcipaddr;
+ fw->destipaddr = BUF->destipaddr;
fw->proto = BUF->proto;
#if notdef
fw->payload[0] = BUF->srcport;
@@ -274,20 +261,24 @@ static void fwcache_register(struct uip_driver_s *dev)
/* Find a network interface for the IP packet in d_buf. */
-static struct uip_fw_netif *find_netif(struct uip_driver_s *dev)
+static struct uip_fw_netif *find_netif (struct uip_driver_s *dev)
{
struct uip_fw_netif *netif;
/* Walk through every network interface to check for a match. */
- for(netif = netifs; netif != NULL; netif = netif->next) {
- if(ipaddr_maskcmp(BUF->destipaddr, netif->ipaddr,
- netif->netmask)) {
- /* If there was a match, we break the loop. */
- return netif;
+
+ for (netif = netifs; netif != NULL; netif = netif->next)
+ {
+ if (uip_ipaddr_maskcmp(BUF->destipaddr, netif->ipaddr, netif->netmask))
+ {
+ /* If there was a match, we break the loop. */
+
+ return netif;
+ }
}
- }
/* If no matching netif was found, we use default netif. */
+
return defaultnetif;
}
@@ -310,7 +301,7 @@ uint8 uip_fw_output(struct uip_driver_s *dev)
{
struct uip_fw_netif *netif;
- if(dev->d_len == 0) {
+ if (dev->d_len == 0) {
return UIP_FW_ZEROLEN;
}
@@ -318,23 +309,23 @@ uint8 uip_fw_output(struct uip_driver_s *dev)
#if UIP_BROADCAST
/* Link local broadcasts go out on all interfaces. */
- if(/*BUF->proto == UIP_PROTO_UDP &&*/
+ if (/*BUF->proto == UIP_PROTO_UDP &&*/
BUF->destipaddr[0] == 0xffff &&
BUF->destipaddr[1] == 0xffff) {
- if(defaultnetif != NULL) {
+ if (defaultnetif != NULL) {
defaultnetif->output();
}
- for(netif = netifs; netif != NULL; netif = netif->next) {
+ for (netif = netifs; netif != NULL; netif = netif->next) {
netif->output();
}
return UIP_FW_OK;
}
#endif /* UIP_BROADCAST */
- netif = find_netif(dev);
+ netif = find_netif (dev);
dbg("uip_fw_output: netif %p ->output %p len %d\n", netif, netif->output, dev->d_len);
- if(netif == NULL) {
+ if (netif == NULL) {
return UIP_FW_NOROUTE;
}
/* If we now have found a suitable network interface, we call its
@@ -353,36 +344,35 @@ uint8 uip_fw_forward(struct uip_driver_s *dev)
struct fwcache_entry *fw;
/* First check if the packet is destined for ourselves and return 0
- to indicate that the packet should be processed locally. */
- if(BUF->destipaddr[0] == uip_hostaddr[0] &&
- BUF->destipaddr[1] == uip_hostaddr[1]) {
- return UIP_FW_LOCAL;
- }
+ * to indicate that the packet should be processed locally.
+ */
+
+ if (BUF->destipaddr == dev->d_ipaddr)
+ {
+ return UIP_FW_LOCAL;
+ }
/* If we use ping IP address configuration, and our IP address is
not yet configured, we should intercept all ICMP echo packets. */
#if UIP_PINGADDRCONF
- if((uip_hostaddr[0] | uip_hostaddr[1]) == 0 &&
- BUF->proto == UIP_PROTO_ICMP &&
- ICMPBUF->type == ICMP_ECHO) {
- return UIP_FW_LOCAL;
- }
+ if (dev->d_ipaddr == 0 && BUF->proto == UIP_PROTO_ICMP && ICMPBUF->type == ICMP_ECHO)
+ {
+ return UIP_FW_LOCAL;
+ }
#endif /* UIP_PINGADDRCONF */
/* Check if the packet is in the forwarding cache already, and if so
we drop it. */
- for(fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
- if(fw->timer != 0 &&
+ for (fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
+ if (fw->timer != 0 &&
#if UIP_REASSEMBLY > 0
fw->len == BUF->len &&
fw->offset == BUF->ipoffset &&
#endif
fw->ipid == BUF->ipid &&
- fw->srcipaddr[0] == BUF->srcipaddr[0] &&
- fw->srcipaddr[1] == BUF->srcipaddr[1] &&
- fw->destipaddr[0] == BUF->destipaddr[0] &&
- fw->destipaddr[1] == BUF->destipaddr[1] &&
+ fw->srcipaddr == BUF->srcipaddr &&
+ fw->destipaddr == BUF->destipaddr &&
#if notdef
fw->payload[0] == BUF->srcport &&
fw->payload[1] == BUF->destport &&
@@ -398,31 +388,34 @@ uint8 uip_fw_forward(struct uip_driver_s *dev)
of the packet.
*/
- if(BUF->ttl <= 1) {
- /* No time exceeded for broadcasts and multicasts! */
- if(BUF->destipaddr[0] == 0xffff && BUF->destipaddr[1] == 0xffff) {
- return UIP_FW_LOCAL;
+ if (BUF->ttl <= 1)
+ {
+ /* No time exceeded for broadcasts and multicasts! */
+
+ if (BUF->destipaddr == 0xffffffff)
+ {
+ return UIP_FW_LOCAL;
+ }
+ time_exceeded(dev);
}
- time_exceeded(dev);
- }
/* Decrement the TTL (time-to-live) value in the IP header */
BUF->ttl = BUF->ttl - 1;
/* Update the IP checksum. */
- if(BUF->ipchksum >= HTONS(0xffff - 0x0100)) {
+ if (BUF->ipchksum >= HTONS(0xffff - 0x0100)) {
BUF->ipchksum = BUF->ipchksum + HTONS(0x0100) + 1;
} else {
BUF->ipchksum = BUF->ipchksum + HTONS(0x0100);
}
- if(dev->d_len > 0) {
+ if (dev->d_len > 0) {
dev->d_appdata = &dev->d_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN];
uip_fw_output(dev);
}
#if UIP_BROADCAST
- if(BUF->destipaddr[0] == 0xffff && BUF->destipaddr[1] == 0xffff) {
+ if (BUF->destipaddr[0] == 0xffff && BUF->destipaddr[1] == 0xffff) {
return UIP_FW_LOCAL;
}
#endif /* UIP_BROADCAST */
@@ -463,8 +456,8 @@ void uip_fw_default(struct uip_fw_netif *netif)
void uip_fw_periodic(void)
{
struct fwcache_entry *fw;
- for(fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
- if(fw->timer > 0) {
+ for (fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
+ if (fw->timer > 0) {
--fw->timer;
}
}