Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
pandax381 committed Feb 8, 2022
1 parent 9b5d98c commit 0fdbf09
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 34 deletions.
54 changes: 25 additions & 29 deletions ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ ip_iface_register(struct net_device *dev, struct ip_iface *iface)
}
iface->next = ifaces;
ifaces = iface;
infof("registerd: dev=%s, unicast=%s, netmask=%s, broadcast=%s",
infof("registered: dev=%s, unicast=%s, netmask=%s, broadcast=%s",
dev->name,
ip_addr_ntop(iface->unicast, addr1, sizeof(addr1)),
ip_addr_ntop(iface->netmask, addr2, sizeof(addr2)),
Expand All @@ -291,7 +291,7 @@ ip_input(const uint8_t *data, size_t len, struct net_device *dev)
{
struct ip_hdr *hdr;
uint8_t v;
uint16_t hlen, total;
uint16_t hlen, total, offset;
struct ip_iface *iface;
char addr[IP_ADDR_STR_LEN];
struct ip_protocol *proto;
Expand All @@ -316,14 +316,15 @@ ip_input(const uint8_t *data, size_t len, struct net_device *dev)
errorf("total length error: total=%u, len=%u", total, len);
return;
}
if (!hdr->ttl) {
errorf("time exeeded (ttl=0)");
return;
}
if (cksum16((uint16_t *)hdr, hlen, 0) != 0) {
errorf("checksum error: sum=0x%04x, verify=0x%04x", ntoh16(hdr->sum), ntoh16(cksum16((uint16_t *)hdr, hlen, -hdr->sum)));
return;
}
offset = ntoh16(hdr->offset);
if (offset & 0x2000 || offset & 0x1fff) {
errorf("fragments does not support");
return;
}
iface = (struct ip_iface *)net_device_get_iface(dev, NET_IFACE_FAMILY_IP);
if (!iface) {
/* iface is not registered to the device */
Expand All @@ -340,7 +341,7 @@ ip_input(const uint8_t *data, size_t len, struct net_device *dev)
ip_dump(data, total);
for (proto = protocols; proto; proto = proto->next) {
if (proto->type == hdr->protocol) {
proto->handler((uint8_t *)hdr + hlen, len - hlen, hdr->src, hdr->dst, iface);
proto->handler((uint8_t *)hdr + hlen, total - hlen, hdr->src, hdr->dst, iface);
return;
}
}
Expand Down Expand Up @@ -390,7 +391,7 @@ ip_output_core(struct ip_iface *iface, uint8_t protocol, const uint8_t *data, si
hdr->sum = cksum16((uint16_t *)hdr, hlen, 0); /* don't convert bytoder */
memcpy(hdr+1, data, len);
debugf("dev=%s, iface=%s, protocol=%s(0x%02x), len=%u",
NET_IFACE(iface)->dev->name, ip_addr_ntop(dst, addr, sizeof(addr)), ip_protocol_name(protocol), protocol, total);
NET_IFACE(iface)->dev->name, ip_addr_ntop(iface->unicast, addr, sizeof(addr)), ip_protocol_name(protocol), protocol, total);
ip_dump(buf, total);
return ip_output_device(iface, buf, total, nexthop);
}
Expand All @@ -417,26 +418,21 @@ ip_output(uint8_t protocol, const uint8_t *data, size_t len, ip_addr_t src, ip_a
ip_addr_t nexthop;
uint16_t id;

if (src == IP_ADDR_ANY) {
route = ip_route_lookup(dst);
if (!route) {
errorf("no route to host, addr=%s", ip_addr_ntop(dst, addr, sizeof(addr)));
return -1;
}
iface = route->iface;
nexthop = (route->nexthop != IP_ADDR_ANY) ? route->nexthop : dst;
} else {
iface = ip_iface_select(src);
if (!iface) {
errorf("iface not found, addr=%s", ip_addr_ntop(src, addr, sizeof(addr)));
return -1;
}
if ((dst & iface->netmask) != (iface->unicast & iface->netmask) && dst != IP_ADDR_BROADCAST) {
errorf("not reached, addr=%s", ip_addr_ntop(src, addr, sizeof(addr)));
return -1;
}
nexthop = dst;
if (src == IP_ADDR_ANY && dst == IP_ADDR_BROADCAST) {
errorf("source address is required for broadcast addresses");
return -1;
}
route = ip_route_lookup(dst);
if (!route) {
errorf("no route to host, addr=%s", ip_addr_ntop(dst, addr, sizeof(addr)));
return -1;
}
iface = route->iface;
if (src != IP_ADDR_ANY && src != iface->unicast) {
errorf("unable to output with specified source address, addr=%s", ip_addr_ntop(src, addr, sizeof(addr)));
return -1;
}
nexthop = (route->nexthop != IP_ADDR_ANY) ? route->nexthop : dst;
if (NET_IFACE(iface)->dev->mtu < IP_HDR_SIZE_MIN + len) {
errorf("too long, dev=%s, mtu=%s, tatal=%zu",
NET_IFACE(iface)->dev->name, NET_IFACE(iface)->dev->mtu, IP_HDR_SIZE_MIN + len);
Expand All @@ -458,7 +454,7 @@ ip_protocol_register(const char *name, uint8_t type, void (*handler)(const uint8

for (entry = protocols; entry; entry = entry->next) {
if (entry->type == type) {
errorf("already registerd, type=%s(0x%02x), exist=%s(0x%02x)", name, type, entry->name, entry->type);
errorf("already exists, type=%s(0x%02x), exist=%s(0x%02x)", name, type, entry->name, entry->type);
return -1;
}
}
Expand All @@ -472,7 +468,7 @@ ip_protocol_register(const char *name, uint8_t type, void (*handler)(const uint8
entry->handler = handler;
entry->next = protocols;
protocols = entry;
infof("registerd, type=%s(0x%02x)", entry->name, entry->type);
infof("registered, type=%s(0x%02x)", entry->name, entry->type);
return 0;
}

Expand Down
8 changes: 4 additions & 4 deletions net.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ net_device_register(struct net_device *dev)
snprintf(dev->name, sizeof(dev->name), "net%d", dev->index);
dev->next = devices;
devices = dev;
infof("registerd, dev=%s, type=0x%04x", dev->name, dev->type);
infof("registered, dev=%s, type=0x%04x", dev->name, dev->type);
return 0;
}

Expand Down Expand Up @@ -207,7 +207,7 @@ net_protocol_register(const char *name, uint16_t type, void (*handler)(const uin

for (proto = protocols; proto; proto = proto->next) {
if (type == proto->type) {
errorf("already registerd, type=%s(0x%04x), exist=%s(0x%04x)", name, type, proto->name, proto->type);
errorf("already registered, type=%s(0x%04x), exist=%s(0x%04x)", name, type, proto->name, proto->type);
return -1;
}
}
Expand All @@ -222,7 +222,7 @@ net_protocol_register(const char *name, uint16_t type, void (*handler)(const uin
proto->handler = handler;
proto->next = protocols;
protocols = proto;
infof("registerd, type=%s(0x%04x)", proto->name, type);
infof("registered, type=%s(0x%04x)", proto->name, type);
return 0;
}

Expand Down Expand Up @@ -256,7 +256,7 @@ net_timer_register(const char *name, struct timeval interval, void (*handler)(vo
timer->handler = handler;
timer->next = timers;
timers = timer;
infof("registerd: %s interval={%d, %d}", timer->name, interval.tv_sec, interval.tv_usec);
infof("registered: %s interval={%d, %d}", timer->name, interval.tv_sec, interval.tv_usec);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include "platform.h"

#include "util.h"
#include "net.h"
#include "ip.h"

#include "tcp.h"

#define TCP_FLG_FIN 0x01
Expand Down
1 change: 1 addition & 0 deletions udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "platform.h"

#include "util.h"
#include "net.h"
#include "ip.h"
#include "udp.h"

Expand Down

0 comments on commit 0fdbf09

Please sign in to comment.