summaryrefslogtreecommitdiff
path: root/sysdeps/mach/hurd/if_index.c
blob: e8ee9998992fb668057f56c3e30bc3b4ae1c3afe (plain)
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
/* Find network interface names and index numbers.  Hurd version.
   Copyright (C) 2000 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The GNU C Library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */

#include <net/if.h>
#include <hurd.h>
#include <hurd/fsys.h>
#include <string.h>
#include <sys/mman.h>

static int
map_interfaces (int domain,
		unsigned int *idxp,
		int (*counted_initializer) (unsigned int count,
					    size_t nameslen),
		int (*iterator) (const char *))
{
  static const char ifopt[] = "--interface=";
  file_t server;
  char optsbuf[512], *opts = optsbuf, *p;
  size_t optslen = sizeof optsbuf;
  error_t err;

  /* Find the socket server for DOMAIN.  */
  server = _hurd_socket_server (domain, 0);
  if (server == MACH_PORT_NULL)
    return 0;

  err = __file_get_fs_options (server, &opts, &optslen);
  if (err == MACH_SEND_INVALID_DEST || err == MIG_SERVER_DIED)
    {
      /* On the first use of the socket server during the operation,
	 allow for the old server port dying.  */
      server = _hurd_socket_server (domain, 1);
      if (server == MACH_PORT_NULL)
	return -1;
      err = __file_get_fs_options (server, &opts, &optslen);
    }
  if (err)
    return __hurd_fail (err), 0;

  if (counted_initializer)
    {
      unsigned int count = 0;
      size_t nameslen = 0;
      p = memchr (opts, '\0', optslen);
      while (p != 0)
	{
	  char *end = memchr (p + 1, '\0', optslen - (p - opts));
	  if (end == 0)
	    break;
	  if (optslen - (p - opts) >= sizeof ifopt
	      && !memcmp (p + 1, ifopt, sizeof ifopt - 1))
	    {
	      size_t len = end + 1 - (p + sizeof ifopt);
	      nameslen += len > IFNAMSIZ+1 ? IFNAMSIZ+1 : len;
	      ++count;
	    }
	  p = end;
	}

      if ((*counted_initializer) (count, nameslen))
	return 0;
    }

  *idxp = 0;
  for (p = memchr (opts, '\0', optslen); p != 0;
       p = memchr (p + 1, '\0', optslen - (p - opts)))
    {
      ++*idxp;
      if (optslen - (p - opts) >= sizeof ifopt
	  && !memcmp (p + 1, ifopt, sizeof ifopt - 1)
	  && (*iterator) (p + sizeof ifopt))
	break;
    }

  if (opts != optsbuf)
    __munmap (opts, optslen);

  return 1;
}

unsigned int
if_nametoindex (const char *ifname)
{
  unsigned int idx;
  int find_name (const char *name)
    {
      return !strcmp (name, ifname);
    }
  return map_interfaces (PF_INET, &idx, 0, &find_name) ? idx : 0;
}

char *
if_indextoname (unsigned int ifindex, char *ifname)
{
  unsigned int idx;
  int find_idx (const char *name)
    {
      if (idx == ifindex)
	{
	  strncpy (ifname, name, IFNAMSIZ);
	  return 1;
	}
      return 0;
    }
  return map_interfaces (PF_INET, &idx, 0, &find_idx) ? ifname : NULL;
}


struct if_nameindex *
if_nameindex (void)
{
  unsigned int idx;
  struct if_nameindex *buf;
  char *namep;
  int alloc (unsigned int count, size_t nameslen)
    {
      buf = malloc ((sizeof buf[0] * (count + 1)) + nameslen);
      if (buf == 0)
	return 1;
      buf[count].if_index = 0;
      buf[count].if_name = NULL;
      namep = (char *) &buf[count + 1];
      return 0;
    }
  int fill (const char *name)
    {
      buf[idx - 1].if_index = idx;
      buf[idx - 1].if_name = namep;
      namep = __memccpy (namep, name, '\0', IFNAMSIZ+1) ?: &namep[IFNAMSIZ+1];
      return 0;
    }

  return map_interfaces (PF_INET, &idx, &alloc, &fill) ? buf : NULL;
}

void
if_freenameindex (struct if_nameindex *ifn)
{
  free (ifn);
}


void
internal_function
__protocol_available (int *have_inet, int *have_inet6)
{
  *have_inet = _hurd_socket_server (PF_INET, 0) != MACH_PORT_NULL;
  *have_inet6 = _hurd_socket_server (PF_INET6, 0) != MACH_PORT_NULL;
}