File: network.c

package info (click to toggle)
freeipmi 1.6.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,452 kB
  • sloc: ansic: 318,860; sh: 4,648; makefile: 2,090; perl: 1,078
file content (280 lines) | stat: -rw-r--r-- 7,242 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*****************************************************************************\
 *  $Id: hostlist.c,v 1.3 2009-12-16 17:49:39 chu11 Exp $
 *****************************************************************************
 *  Copyright (C) 2007-2015 Lawrence Livermore National Security, LLC.
 *  Copyright (C) 2003-2007 The Regents of the University of California.
 *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
 *  Written by Albert Chu <chu11@llnl.gov>
 *  UCRL-CODE-155698
 *
 *  This file is part of Ipmipower, a remote power control utility.
 *  For details, see http://www.llnl.gov/linux/.
 *
 *  Ipmipower is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 3 of the License, or (at your
 *  option) any later version.
 *
 *  Ipmipower is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with Ipmipower.  If not, see <http://www.gnu.org/licenses/>.
\*****************************************************************************/


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif  /* HAVE_CONFIG_H */

#include <stdio.h>
#include <stdlib.h>
#if STDC_HEADERS
#include <string.h>
#include <ctype.h>
#endif /* STDC_HEADERS */
#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <limits.h>             /* MAXHOSTNAMELEN */
#ifdef HAVE_NETDB_H
#include <netdb.h>              /* MAXHOSTNAMELEN Solaris */
#endif /* HAVE_NETDB_H */

#include "fi_hostlist.h"
#include "hostlist.h"

#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif

int
host_is_ipv6_with_port (const char *host, char **addr, char **port)
{
  char *str = NULL;
  int is_ipv6_with_port = 0;
  int rv = -1;

  assert (host);

  if (!(str = strdup (host)))
    goto cleanup;

  /* achu:
   *
   * Some of this code is from
   *
   * LaMont Jones <lamont@mmjgroup.com>
   *
   * In his experimental patch
   *
   * git pull https://github.com/lamontj/freeipmi-mirror.git ipmipower-ipv6
   *
   */

  if (str[0] == '[')
    {
      char *addrptr = &str[1];
      char *tmp;

      if ((tmp = strchr (addrptr, ']')))
        {
          *tmp = '\0';
          tmp++;

          /* Is character after the right bracket a colon? */
          if (*tmp == ':')
            {
              char *portptr;

              /* is everything after the colon a number? */
              tmp++;
              portptr = tmp;
              while (isdigit (*tmp))
                tmp++;

              /* are we at the end of the string? */
              /* and did we find a port */
              if (*tmp == '\0' && strlen (portptr))
                {
                  struct sockaddr_in6 saddr;

                  /* is what's in between the brackets an IPv6 address? */
                  if (inet_pton(AF_INET6, addrptr, &saddr) == 1)
                    {
                      /* if yes, we've got the special IPv6/port combo */
                      is_ipv6_with_port = 1;

                      if (addr)
                        {
                          if (!(*addr = strdup (addrptr)))
                            goto cleanup;
                        }

                      if (port)
                        {
                          if (!(*port = strdup (portptr)))
                            {
                              free (*addr);
                              goto cleanup;
                            }
                        }
                    }
                }
            }
        }
    }

  rv = is_ipv6_with_port;
 cleanup:
  free (str);
  return (rv);
}

int
host_is_host_with_port (const char *host, char **addr, char **port)
{
  char *str = NULL;
  int rv = -1;
  int is_host_with_port = 0;
  int ret;

  assert (host);

  if ((ret = host_is_ipv6_with_port (host, addr, port)) < 0)
    goto cleanup;

  if (ret)
    {
      rv = ret;
      goto cleanup;
    }

  if (strchr (host, ':'))
    {
      struct sockaddr_in6 saddr;
      char *addrptr;
      char *portptr;
      char *lastcolonptr = NULL; /* remove warning */
      char *tmp;

      /* First check, maybe it's a valid IPv6 address straight up */
      if (inet_pton (AF_INET6, host, &saddr) == 1)
        goto out;

      if (!(str = strdup (host)))
        goto cleanup;

      addrptr = str;

      /* find last colon */
      portptr = addrptr;
      while ((tmp = strchr (portptr, ':')))
        {
          lastcolonptr = tmp;
          portptr = tmp + 1;
        }

      *lastcolonptr = '\0';

      /* is everything after the colon a number? */
      tmp = portptr;
      while (isdigit (*tmp))
        tmp++;

      /* are we at the end of the string? */
      /* and did we find a port */
      if (*tmp == '\0' && strlen (portptr))
        {
          is_host_with_port = 1;

          if (addr)
            {
              if (!(*addr = strdup (addrptr)))
                goto cleanup;
            }

          if (port)
            {
              if (!(*port = strdup (portptr)))
                {
                  free (*addr);
                  goto cleanup;
                }
            }
        }
    }

 out:
  rv = is_host_with_port;
 cleanup:
  free (str);
  return (rv);
}

int
host_is_valid (const char *addr, const char *port, uint16_t *portptr)
{
  assert (addr);

  /* achu: max length IPv6 is 45 chars
   * ABCD:ABCD:ABCD:ABCD:ABCD:ABCD:192.168.100.200
   */
  if (strlen (addr) > MAXHOSTNAMELEN)
    return (0);

  if (port)
    {
      char *endptr;
      int tmp;

      errno = 0;
      tmp = strtol (port, &endptr, 0);
      if (errno
          || endptr[0] != '\0'
          || tmp <= 0
          || tmp > USHRT_MAX)
        return 0;

      if (portptr)
        *portptr = tmp;
    }

  return (1);
}

/* Note that we do not do address resolution to map hypothetical
 * situations (e.g. "foobar" resolves to "127.0.0.1").  We only
 * hardcode and check for the known popular strings.
 *
 * The reason is that most of FreeIPMI supports mapping "localhost" to
 * "inband" communication primarily for convenience.  Programmers
 * don't have to handle "inband" communication differently than
 * "outofband" cases.  e.g.  you can script/program with the hosts
 * "node1,node2,node3,localhost,node4" and not have to program a
 * special case for "inband" communication.
 *
 * If a user truly wants some funky host/string to resolve to
 * "localhost", we suggest they use one of the known popular strings
 * instead.  We don't want to have to do host resolution checks for
 * every host/IP ever input into FreeIPMI.
 */
int
host_is_localhost (const char *host)
{
  assert (host);

  /* Ordered by my assumption of most popular */
  if (!strcasecmp (host, "localhost")
      || !strcmp (host, "127.0.0.1")
      || !strcasecmp (host, "ipv6-localhost")
      || !strcmp (host, "::1")
      || !strcasecmp (host, "ip6-localhost")
      || !strcmp (host, "0:0:0:0:0:0:0:1"))
    return (1);

  return (0);
}