Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 1 | /* |
| 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 Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 15 | struct in_addr string_to_ip(const char *s) |
Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 16 | { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 17 | struct in_addr addr; |
Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 18 | char *e; |
| 19 | int i; |
| 20 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 21 | addr.s_addr = 0; |
Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 22 | if (s == NULL) |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 23 | return addr; |
Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 24 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 25 | for (addr.s_addr = 0, i = 0; i < 4; ++i) { |
Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 26 | ulong val = s ? simple_strtoul(s, &e, 10) : 0; |
Chris Packham | d921ed9 | 2017-01-04 13:36:25 +1300 | [diff] [blame] | 27 | if (val > 255) { |
| 28 | addr.s_addr = 0; |
| 29 | return addr; |
| 30 | } |
Chris Packham | f267e40 | 2017-01-04 13:36:26 +1300 | [diff] [blame] | 31 | if (i != 3 && *e != '.') { |
| 32 | addr.s_addr = 0; |
| 33 | return addr; |
| 34 | } |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 35 | addr.s_addr <<= 8; |
| 36 | addr.s_addr |= (val & 0xFF); |
Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 37 | if (s) { |
| 38 | s = (*e) ? e+1 : e; |
| 39 | } |
| 40 | } |
| 41 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 42 | addr.s_addr = htonl(addr.s_addr); |
| 43 | return addr; |
Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 44 | } |