]> Git Repo - J-u-boot.git/blame - lib/net_utils.c
fdt_region: move fdt_region.c to common/ from lib/libfdt/
[J-u-boot.git] / lib / net_utils.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
6a45e384
DB
2/*
3 * Generic network code. Moved from net.c
4 *
5 * Copyright 1994 - 2000 Neil Russell.
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, [email protected]
9 * Copyright 2009 Dirk Behme, [email protected]
6a45e384
DB
10 */
11
12#include <common.h>
13
049a95a7 14struct in_addr string_to_ip(const char *s)
6a45e384 15{
049a95a7 16 struct in_addr addr;
6a45e384
DB
17 char *e;
18 int i;
19
049a95a7 20 addr.s_addr = 0;
6a45e384 21 if (s == NULL)
049a95a7 22 return addr;
6a45e384 23
049a95a7 24 for (addr.s_addr = 0, i = 0; i < 4; ++i) {
6a45e384 25 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
d921ed9a
CP
26 if (val > 255) {
27 addr.s_addr = 0;
28 return addr;
29 }
f267e40f
CP
30 if (i != 3 && *e != '.') {
31 addr.s_addr = 0;
32 return addr;
33 }
049a95a7
JH
34 addr.s_addr <<= 8;
35 addr.s_addr |= (val & 0xFF);
6a45e384
DB
36 if (s) {
37 s = (*e) ? e+1 : e;
38 }
39 }
40
049a95a7
JH
41 addr.s_addr = htonl(addr.s_addr);
42 return addr;
6a45e384 43}
fb8977c5
JH
44
45void string_to_enetaddr(const char *addr, uint8_t *enetaddr)
46{
47 char *end;
48 int i;
49
50 if (!enetaddr)
51 return;
52
53 for (i = 0; i < 6; ++i) {
54 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
55 if (addr)
56 addr = (*end) ? end + 1 : end;
57 }
58}
d721001f
SG
59
60uint compute_ip_checksum(const void *vptr, uint nbytes)
61{
62 int sum, oddbyte;
63 const unsigned short *ptr = vptr;
64
65 sum = 0;
66 while (nbytes > 1) {
67 sum += *ptr++;
68 nbytes -= 2;
69 }
70 if (nbytes == 1) {
71 oddbyte = 0;
72 ((u8 *)&oddbyte)[0] = *(u8 *)ptr;
73 ((u8 *)&oddbyte)[1] = 0;
74 sum += oddbyte;
75 }
76 sum = (sum >> 16) + (sum & 0xffff);
77 sum += (sum >> 16);
78 sum = ~sum & 0xffff;
79
80 return sum;
81}
82
83uint add_ip_checksums(uint offset, uint sum, uint new)
84{
85 ulong checksum;
86
87 sum = ~sum & 0xffff;
88 new = ~new & 0xffff;
89 if (offset & 1) {
90 /*
91 * byte-swap the sum if it came from an odd offset; since the
92 * computation is endian-independent this works.
93 */
94 new = ((new >> 8) & 0xff) | ((new << 8) & 0xff00);
95 }
96 checksum = sum + new;
97 if (checksum > 0xffff)
98 checksum -= 0xffff;
99
100 return (~checksum) & 0xffff;
101}
102
103int ip_checksum_ok(const void *addr, uint nbytes)
104{
105 return !(compute_ip_checksum(addr, nbytes) & 0xfffe);
106}
This page took 0.408213 seconds and 4 git commands to generate.