blob: 9fb9d4a4b05fc19a492f3f4bb66e9826879600d0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dirk Behme8b3cc062010-01-03 08:33:58 +01002/*
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
Dirk Behme8b3cc062010-01-03 08:33:58 +010010 */
11
12#include <common.h>
13
Joe Hershberger5874dec2015-04-08 01:41:01 -050014struct in_addr string_to_ip(const char *s)
Dirk Behme8b3cc062010-01-03 08:33:58 +010015{
Joe Hershberger5874dec2015-04-08 01:41:01 -050016 struct in_addr addr;
Dirk Behme8b3cc062010-01-03 08:33:58 +010017 char *e;
18 int i;
19
Joe Hershberger5874dec2015-04-08 01:41:01 -050020 addr.s_addr = 0;
Dirk Behme8b3cc062010-01-03 08:33:58 +010021 if (s == NULL)
Joe Hershberger5874dec2015-04-08 01:41:01 -050022 return addr;
Dirk Behme8b3cc062010-01-03 08:33:58 +010023
Joe Hershberger5874dec2015-04-08 01:41:01 -050024 for (addr.s_addr = 0, i = 0; i < 4; ++i) {
Dirk Behme8b3cc062010-01-03 08:33:58 +010025 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
Chris Packhame917c362017-01-04 13:36:25 +130026 if (val > 255) {
27 addr.s_addr = 0;
28 return addr;
29 }
Chris Packham06562482017-01-04 13:36:26 +130030 if (i != 3 && *e != '.') {
31 addr.s_addr = 0;
32 return addr;
33 }
Joe Hershberger5874dec2015-04-08 01:41:01 -050034 addr.s_addr <<= 8;
35 addr.s_addr |= (val & 0xFF);
Dirk Behme8b3cc062010-01-03 08:33:58 +010036 if (s) {
37 s = (*e) ? e+1 : e;
38 }
39 }
40
Joe Hershberger5874dec2015-04-08 01:41:01 -050041 addr.s_addr = htonl(addr.s_addr);
42 return addr;
Dirk Behme8b3cc062010-01-03 08:33:58 +010043}