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