File: ipxkern.c

package info (click to toggle)
ipxripd 0.7-5
  • links: PTS
  • area: main
  • in suites: slink
  • size: 200 kB
  • ctags: 272
  • sloc: ansic: 2,847; makefile: 67; sh: 39
file content (228 lines) | stat: -rw-r--r-- 4,819 bytes parent folder | download | duplicates (2)
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
/*
    IPX support library - kernel dependent functions

    Copyright (C) 1994, 1995  Ales Dryak <e-mail: A.Dryak@sh.cvut.cz>
    Copyright (C) 1996, Volker Lendecke <lendecke@namu01.gwdg.de>

    This program 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 2 of the License, or
    (at your option) any later version.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#define _LINUX_SOCKET_H        /* dirty hack to make it compile :-) */

#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <string.h>
#include <linux/route.h>
#include "ipxkern.h"

#define MAX_IFC (256)

static int 
asc2frame(char *frame)
{
	if (strcasecmp("etherii", frame) == 0)
		return IPX_FRAME_ETHERII;
	else if (strcasecmp("802.2", frame) == 0)
		return IPX_FRAME_8022;
	else if (strcasecmp("802.3", frame) == 0)
		return IPX_FRAME_8023;
	else if (strcasecmp("snap", frame) == 0)
		return IPX_FRAME_SNAP;
	else
		return 0;
}

int
ipx_kern_scan_rtable(IPXrtScanFunc f, void *data)
{
	FILE *ipx_route;
	char buf[512];

	ipx_route = fopen("/proc/net/ipx_route", "r");

	if (ipx_route == NULL)
	{
		sprintf(ipx_err_string, "open ipx_route: %s",
			strerror(errno));
		return -1;
	}

	/* ignore header line */
	fgets(buf, sizeof(buf), ipx_route);

	while (fgets(buf, sizeof(buf), ipx_route) != NULL)
	{
		IPXNet network;
		IPXNet router_net;
		IPXNode router_node;
		int type = 0;

		if (strncmp(&(buf[11]), "Directly", 8) == 0)
		{
			if (ipx_sscanf_net(buf, &network) != 0)
			{
				fclose(ipx_route);
				return -1;
			}
			router_net = 0;
			memset(router_node, 0, sizeof(router_node));
		}
		else
		{
			type |= IPX_KRT_ROUTE;

			if (   (ipx_sscanf_net(buf, &network) != 0)
			    || (ipx_sscanf_net(&(buf[11]), &router_net) != 0)
			    || (ipx_sscanf_node(&(buf[24]), router_node) != 0))
			{
				fclose(ipx_route);
				return -1;
			}
		}

		if (f(network, router_net, router_node, type, data) != 0)
		{
			fclose(ipx_route);
			return 0;
		}
	}
	fclose(ipx_route);
	return 1;
}
		
int
ipx_kern_scan_ifaces(IPXifcScanFunc f, void *data)
{
	FILE *ipx_ifc;
	char buf[512];

	ipx_ifc = fopen("/proc/net/ipx_interface", "r");

	if (ipx_ifc == NULL)
	{
		sprintf(ipx_err_string, "open ipx_interface: %s",
			strerror(errno));
		return -1;
	}

	/* ignore header line */
	fgets(buf, sizeof(buf), ipx_ifc);

	while (fgets(buf, sizeof(buf), ipx_ifc) != NULL)
	{
		IPXNet network;
		IPXNode node;
		int type = 0;
		char device[128];
		int i;
		int result;

		type = asc2frame(&(buf[46]));
		
		if (strncmp(&(buf[35]), "Internal", 8) == 0)
		{
			type |= IPX_KRT_INTERNAL;
		}

		if (   (ipx_sscanf_net(buf, &network) != 0)
		    || (ipx_sscanf_node(&(buf[11]), node) != 0))
		{
			fclose(ipx_ifc);
			return -1;
		}

		memset(device, 0, sizeof(device));

		for (i = 0; i < sizeof(device)-1; i++)
		{
			if (buf[i+35] == ' ')
			{
				break;
			}
			device[i] = buf[i+35];
		}

		if ((result = f(network, node, device, type, data)) != 0)
		{
			fclose(ipx_ifc);
			return result;
		}
	}
	fclose(ipx_ifc);
	return 0;
}

int
ipx_kern_route_add(int sock, IPXNet net, IPXNet rt_net, IPXNode rt_node)
{
	struct rtentry rt;
	struct sockaddr_ipx* sr;
	struct sockaddr_ipx* st;
	
	sr=(struct sockaddr_ipx*)&rt.rt_gateway;
	st=(struct sockaddr_ipx*)&rt.rt_dst;
	
	sr->sipx_family=st->sipx_family=AF_IPX;
	st->sipx_network=htonl(net);
	sr->sipx_network=htonl(rt_net);
	ipx_assign_node(sr->sipx_node,rt_node);
	rt.rt_flags=RTF_GATEWAY;

	if(ioctl(sock,SIOCADDRT,(void *)&rt)!=0)
	{
		sprintf(ipx_err_string,"ioctl SIOCADDRT: %s",strerror(errno));
		return -1;
	}
	return 0;
}

int
ipx_kern_route_delete(int sock, IPXNet net)
{
	struct rtentry rt;
	struct sockaddr_ipx* sr;
	struct sockaddr_ipx* st;
	
	sr=(struct sockaddr_ipx*)&rt.rt_gateway;
	st=(struct sockaddr_ipx*)&rt.rt_dst;
	
	sr->sipx_family=st->sipx_family=AF_IPX;
	st->sipx_network=htonl(net);
	rt.rt_flags=RTF_GATEWAY;

	if(ioctl(sock,SIOCDELRT,(void *)&rt)!=0)
	{
		sprintf(ipx_err_string,"ioctl SIOCDELRT: %s",strerror(errno));
		return -1;
	}
	return 0;
}

int
ipx_kern_enable_broadcast(int sock)
{
	int opt=1;
	/* Permit broadcast output */
	if(setsockopt(sock,SOL_SOCKET,SO_BROADCAST, &opt,sizeof(opt))==-1)
	{
		sprintf(ipx_err_string,"setsockopt SO_BROADCAST: %s",
			strerror(errno));
		return -1;
	}
	return 0;
}