1 // SPDX-License-Identifier: GPL-2.0+
3 * Generic network code. Moved from net.c
5 * Copyright 1994 - 2000 Neil Russell.
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
9 * Copyright 2009 Dirk Behme, dirk.behme@googlemail.com
15 struct in_addr string_to_ip(const char *s)
25 for (addr.s_addr = 0, i = 0; i < 4; ++i) {
26 ulong val = s ? dectoul(s, &e) : 0;
31 if (i != 3 && *e != '.') {
36 addr.s_addr |= (val & 0xFF);
42 addr.s_addr = htonl(addr.s_addr);
46 void string_to_enetaddr(const char *addr, uint8_t *enetaddr)
54 for (i = 0; i < 6; ++i) {
55 enetaddr[i] = addr ? hextoul(addr, &end) : 0;
57 addr = (*end) ? end + 1 : end;
61 uint compute_ip_checksum(const void *vptr, uint nbytes)
64 const unsigned short *ptr = vptr;
73 ((u8 *)&oddbyte)[0] = *(u8 *)ptr;
74 ((u8 *)&oddbyte)[1] = 0;
77 sum = (sum >> 16) + (sum & 0xffff);
84 uint add_ip_checksums(uint offset, uint sum, uint new)
92 * byte-swap the sum if it came from an odd offset; since the
93 * computation is endian-independent this works.
95 new = ((new >> 8) & 0xff) | ((new << 8) & 0xff00);
98 if (checksum > 0xffff)
101 return (~checksum) & 0xffff;
104 int ip_checksum_ok(const void *addr, uint nbytes)
106 return !(compute_ip_checksum(addr, nbytes) & 0xfffe);