blob: cfae842752414ebb7a24d3ed5229245a114e02da [file] [log] [blame]
Dirk Behme8b3cc062010-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 Denkd79de1d2013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
Dirk Behme8b3cc062010-01-03 08:33:58 +010011 */
12
13#include <common.h>
14
Joe Hershberger5874dec2015-04-08 01:41:01 -050015struct in_addr string_to_ip(const char *s)
Dirk Behme8b3cc062010-01-03 08:33:58 +010016{
Joe Hershberger5874dec2015-04-08 01:41:01 -050017 struct in_addr addr;
Dirk Behme8b3cc062010-01-03 08:33:58 +010018 char *e;
19 int i;
20
Joe Hershberger5874dec2015-04-08 01:41:01 -050021 addr.s_addr = 0;
Dirk Behme8b3cc062010-01-03 08:33:58 +010022 if (s == NULL)
Joe Hershberger5874dec2015-04-08 01:41:01 -050023 return addr;
Dirk Behme8b3cc062010-01-03 08:33:58 +010024
Joe Hershberger5874dec2015-04-08 01:41:01 -050025 for (addr.s_addr = 0, i = 0; i < 4; ++i) {
Dirk Behme8b3cc062010-01-03 08:33:58 +010026 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
Joe Hershberger5874dec2015-04-08 01:41:01 -050027 addr.s_addr <<= 8;
28 addr.s_addr |= (val & 0xFF);
Dirk Behme8b3cc062010-01-03 08:33:58 +010029 if (s) {
30 s = (*e) ? e+1 : e;
31 }
32 }
33
Joe Hershberger5874dec2015-04-08 01:41:01 -050034 addr.s_addr = htonl(addr.s_addr);
35 return addr;
Dirk Behme8b3cc062010-01-03 08:33:58 +010036}