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
14 struct in_addr string_to_ip(const char *s)
24 for (addr.s_addr = 0, i = 0; i < 4; ++i) {
25 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
30 if (i != 3 && *e != '.') {
35 addr.s_addr |= (val & 0xFF);
41 addr.s_addr = htonl(addr.s_addr);
45 void string_to_enetaddr(const char *addr, uint8_t *enetaddr)
53 for (i = 0; i < 6; ++i) {
54 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
56 addr = (*end) ? end + 1 : end;
60 uint compute_ip_checksum(const void *vptr, uint nbytes)
63 const unsigned short *ptr = vptr;
72 ((u8 *)&oddbyte)[0] = *(u8 *)ptr;
73 ((u8 *)&oddbyte)[1] = 0;
76 sum = (sum >> 16) + (sum & 0xffff);
83 uint add_ip_checksums(uint offset, uint sum, uint new)
91 * byte-swap the sum if it came from an odd offset; since the
92 * computation is endian-independent this works.
94 new = ((new >> 8) & 0xff) | ((new << 8) & 0xff00);
97 if (checksum > 0xffff)
100 return (~checksum) & 0xffff;
103 int ip_checksum_ok(const void *addr, uint nbytes)
105 return !(compute_ip_checksum(addr, nbytes) & 0xfffe);