~ubuntu-branches/ubuntu/warty/quagga/warty

« back to all changes in this revision

Viewing changes to zebra/ipforward_solaris.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabio M. Di Nitto
  • Date: 2004-06-29 09:50:59 UTC
  • Revision ID: james.westby@ubuntu.com-20040629095059-px1m2m108z4qw1mr
Tags: upstream-0.96.5
ImportĀ upstreamĀ versionĀ 0.96.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ipforward value get function for solaris.
 
3
 * Copyright (C) 1997 Kunihiro Ishiguro
 
4
 *
 
5
 * This file is part of GNU Zebra.
 
6
 *
 
7
 * GNU Zebra is free software; you can redistribute it and/or modify it
 
8
 * under the terms of the GNU General Public License as published by the
 
9
 * Free Software Foundation; either version 2, or (at your option) any
 
10
 * later version.
 
11
 *
 
12
 * GNU Zebra is distributed in the hope that it will be useful, but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with GNU Zebra; see the file COPYING.  If not, write to the Free
 
19
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
20
 * 02111-1307, USA.  
 
21
 */
 
22
 
 
23
#include <zebra.h>
 
24
#include "log.h"
 
25
#include "prefix.h"
 
26
 
 
27
#include "privs.h"
 
28
 
 
29
/*
 
30
** Solaris should define IP_DEV_NAME in <inet/ip.h>, but we'll save
 
31
** configure.in changes for another day.  We can use the same device
 
32
** for both IPv4 and IPv6.
 
33
*/
 
34
/* #include <inet/ip.h> */
 
35
#ifndef IP_DEV_NAME
 
36
#define IP_DEV_NAME "/dev/ip"
 
37
#endif
 
38
 
 
39
 
 
40
extern struct zebra_privs_t zserv_privs;
 
41
 
 
42
/* This is a limited ndd style function that operates one integer
 
43
** value only.  Errors return -1. ND_SET commands return 0 on
 
44
** success. ND_GET commands return the value on success (which could
 
45
** be -1 and be confused for an error).  The parameter is the string
 
46
** name of the parameter being referenced.
 
47
*/
 
48
 
 
49
static int
 
50
solaris_nd(const int cmd, const char* parameter, const int value)
 
51
{
 
52
#define ND_BUFFER_SIZE 1024
 
53
  int fd;
 
54
  char nd_buf[ND_BUFFER_SIZE];
 
55
  struct strioctl strioctl;
 
56
  const char* device = IP_DEV_NAME;
 
57
  int retval;
 
58
  memset(nd_buf, '\0', ND_BUFFER_SIZE);
 
59
  /*
 
60
  ** ND_SET takes a NULL delimited list of strings further terminated
 
61
  ** buy a NULL.  ND_GET returns a list in a similar layout, although
 
62
  ** here we only use the first result.
 
63
  */
 
64
  if (cmd == ND_SET) {
 
65
    snprintf(nd_buf, ND_BUFFER_SIZE, "%s%c%d%c", parameter, '\0', value,'\0');
 
66
  } else if (cmd == ND_GET) {
 
67
    snprintf(nd_buf, ND_BUFFER_SIZE, "%s", parameter);
 
68
  } else {
 
69
    zlog_err("internal error - inappropriate command given to solaris_nd()%s:%d", __FILE__, __LINE__);
 
70
    return -1;
 
71
  }
 
72
 
 
73
  strioctl.ic_cmd = cmd;
 
74
  strioctl.ic_timout = 0;
 
75
  strioctl.ic_len = ND_BUFFER_SIZE;
 
76
  strioctl.ic_dp = nd_buf;
 
77
  
 
78
  if ( zserv_privs.change (ZPRIVS_RAISE) )
 
79
       zlog_err ("solaris_nd: Can't raise privileges");
 
80
  if ((fd = open (device, O_RDWR)) < 0) 
 
81
    {
 
82
      zlog_warn("failed to open device %s - %s", device, strerror(errno));
 
83
      if ( zserv_privs.change (ZPRIVS_LOWER) )
 
84
        zlog_err ("solaris_nd: Can't lower privileges");
 
85
      return -1;
 
86
    }
 
87
  if (ioctl (fd, I_STR, &strioctl) < 0) 
 
88
    {
 
89
      if ( zserv_privs.change (ZPRIVS_LOWER) )
 
90
        zlog_err ("solaris_nd: Can't lower privileges");
 
91
      close (fd);
 
92
      zlog_warn("ioctl I_STR failed on device %s - %s", device,strerror(errno));
 
93
      return -1;
 
94
    }
 
95
  close(fd);
 
96
  if ( zserv_privs.change (ZPRIVS_LOWER) )
 
97
         zlog_err ("solaris_nd: Can't lower privileges");
 
98
  
 
99
  if (cmd == ND_GET) 
 
100
    {
 
101
      errno = 0;
 
102
      retval = atoi(nd_buf);
 
103
      if (errno) 
 
104
        {
 
105
          zlog_warn("failed to convert returned value to integer - %s",
 
106
                    strerror(errno));
 
107
          retval = -1;
 
108
        }
 
109
    } 
 
110
  else 
 
111
    {
 
112
      retval = 0;
 
113
    }
 
114
  return retval;
 
115
}
 
116
 
 
117
static int
 
118
solaris_nd_set(const char* parameter, const int value) {
 
119
  return solaris_nd(ND_SET, parameter, value);
 
120
}
 
121
static int
 
122
solaris_nd_get(const char* parameter) {
 
123
  return solaris_nd(ND_GET, parameter, 0);
 
124
}
 
125
int
 
126
ipforward()
 
127
{
 
128
  return solaris_nd_get("ip_forwarding");
 
129
}
 
130
 
 
131
int
 
132
ipforward_on ()
 
133
{
 
134
  (void) solaris_nd_set("ip_forwarding", 1);
 
135
  return ipforward();
 
136
}
 
137
 
 
138
int
 
139
ipforward_off ()
 
140
{
 
141
  (void) solaris_nd_set("ip_forwarding", 0);
 
142
  return ipforward();
 
143
}
 
144
#ifdef HAVE_IPV6
 
145
int ipforward_ipv6()
 
146
{
 
147
  return solaris_nd_get("ip6_fowarding");
 
148
}
 
149
int
 
150
ipforward_ipv6_on ()
 
151
{
 
152
  (void) solaris_nd_set("ip6_forwarding", 1);
 
153
  return ipforward_ipv6();
 
154
}
 
155
int
 
156
ipforward_ipv6_off ()
 
157
{
 
158
  (void) solaris_nd_set("ip6_forwarding", 0);
 
159
  return ipforward_ipv6();
 
160
}
 
161
#endif /* HAVE_IPV6 */