xref: /openbmc/u-boot/lib/net_utils.c (revision cbd2fba1)
1 // SPDX-License-Identifier: GPL-2.0+
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, wd@denx.de
9  * Copyright 2009 Dirk Behme, dirk.behme@googlemail.com
10  */
11 
12 #include <common.h>
13 
14 struct in_addr string_to_ip(const char *s)
15 {
16 	struct in_addr addr;
17 	char *e;
18 	int i;
19 
20 	addr.s_addr = 0;
21 	if (s == NULL)
22 		return addr;
23 
24 	for (addr.s_addr = 0, i = 0; i < 4; ++i) {
25 		ulong val = s ? simple_strtoul(s, &e, 10) : 0;
26 		if (val > 255) {
27 			addr.s_addr = 0;
28 			return addr;
29 		}
30 		if (i != 3 && *e != '.') {
31 			addr.s_addr = 0;
32 			return addr;
33 		}
34 		addr.s_addr <<= 8;
35 		addr.s_addr |= (val & 0xFF);
36 		if (s) {
37 			s = (*e) ? e+1 : e;
38 		}
39 	}
40 
41 	addr.s_addr = htonl(addr.s_addr);
42 	return addr;
43 }
44