blob: d06be22849fb44643e484029c50d2181d6f46344 [file] [log] [blame]
Dirk Behme6a45e382010-01-03 08:33:58 +01001/*
2 * Generic network code. Moved from net.c
3 *
4 * Copyright 1994 - 2000 Neil Russell.
5 * Copyright 2000 Roland Borde
6 * Copyright 2000 Paolo Scaffardi
7 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
8 * Copyright 2009 Dirk Behme, dirk.behme@googlemail.com
9 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
Dirk Behme6a45e382010-01-03 08:33:58 +010011 */
12
13#include <common.h>
14
Joe Hershberger049a95a2015-04-08 01:41:01 -050015struct in_addr string_to_ip(const char *s)
Dirk Behme6a45e382010-01-03 08:33:58 +010016{
Joe Hershberger049a95a2015-04-08 01:41:01 -050017 struct in_addr addr;
Dirk Behme6a45e382010-01-03 08:33:58 +010018 char *e;
19 int i;
20
Joe Hershberger049a95a2015-04-08 01:41:01 -050021 addr.s_addr = 0;
Dirk Behme6a45e382010-01-03 08:33:58 +010022 if (s == NULL)
Joe Hershberger049a95a2015-04-08 01:41:01 -050023 return addr;
Dirk Behme6a45e382010-01-03 08:33:58 +010024
Joe Hershberger049a95a2015-04-08 01:41:01 -050025 for (addr.s_addr = 0, i = 0; i < 4; ++i) {
Dirk Behme6a45e382010-01-03 08:33:58 +010026 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
Chris Packhamd921ed92017-01-04 13:36:25 +130027 if (val > 255) {
28 addr.s_addr = 0;
29 return addr;
30 }
Chris Packhamf267e402017-01-04 13:36:26 +130031 if (i != 3 && *e != '.') {
32 addr.s_addr = 0;
33 return addr;
34 }
Joe Hershberger049a95a2015-04-08 01:41:01 -050035 addr.s_addr <<= 8;
36 addr.s_addr |= (val & 0xFF);
Dirk Behme6a45e382010-01-03 08:33:58 +010037 if (s) {
38 s = (*e) ? e+1 : e;
39 }
40 }
41
Joe Hershberger049a95a2015-04-08 01:41:01 -050042 addr.s_addr = htonl(addr.s_addr);
43 return addr;
Dirk Behme6a45e382010-01-03 08:33:58 +010044}