File: ipxsap.c

package info (click to toggle)
ipxripd 0.7-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 188 kB
  • ctags: 270
  • sloc: ansic: 2,847; makefile: 71; sh: 21
file content (314 lines) | stat: -rw-r--r-- 7,140 bytes parent folder | download | duplicates (5)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
   IPX support library - SAP

   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.

 */
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "ipxsap.h"
#include "ipxd.h"

int 
ipx_sap_size(int n, unsigned short int operation)
{
	if (n <= 0)
	{
		return 0;
	}

	switch (operation)
	{
	case IPX_SAP_OP_REQUEST:
		return n == 1 ? IPX_SAP_REQUEST_LEN : 0;

	case IPX_SAP_OP_RESPONSE:
		if (n > IPX_SAP_MAX_ENTRIES)
		{
			return 0;
		}
		return (2 + n * sizeof(struct sap_entry));

	case IPX_SAP_OP_GNS_REQUEST:
		return n == 1 ? IPX_SAP_REQUEST_LEN : 0;

	case IPX_SAP_OP_GNS_RESPONSE:
		return n == 1 ? 2 + sizeof(struct sap_entry) : 0;
		break;
	default:
		return 0;
	}
}

void 
ipx_sap_assign_ser_name(ser_name_t dest, ser_name_t src)
{
	memset(dest, 0, IPX_SAP_SERVER_NAME_LEN);
	strncpy(dest, src, IPX_SAP_SERVER_NAME_LEN);
}

int 
ipx_sap_name_equal(ser_name_t n1, ser_name_t n2)
{
	return strncmp(n1, n2, IPX_SAP_SERVER_NAME_LEN) == 0;
}

int 
ipx_sap_type_equal(ser_type_t t1, ser_type_t t2)
{
	return t1==IPX_SAP_GENERAL_RQ || t2==IPX_SAP_GENERAL_RQ || t1 == t2;
}

static int
ipx_sap_sendto(int sock, void *buffer, int size, struct sockaddr_ipx *daddr)
{
	
	if (ipx_sap_output_func != NULL)
	{
		return ipx_sap_output_func(sock, buffer, size, daddr);
	}
	return 0;
}

int 
ipx_sap_output_init(struct sap_output *out, IPXNet net)
{
	out->entries = 0;
	out->send_error = 0;
	out->buffer.operation = htons(IPX_SAP_OP_REQUEST);
	out->dest_addr.sipx_family = AF_IPX;
	out->dest_addr.sipx_type = IPX_SAP_PTYPE;
	out->dest_addr.sipx_network = htonl(net);
	return 0;
}

void 
ipx_sap_output_flush(struct sap_output * out)
{
	if (out->entries == 0)
	{
		return;
	}
	if (out->send_error != 0)
	{
		out->entries = 0;
		return;
	}
	if (ipx_sap_sendto(out->sk, &(out->buffer),
			   ipx_sap_size(out->entries,
					ntohs(out->buffer.operation)),
			   &(out->dest_addr)) < 0)
	{
		out->send_error = 1;
		check_request = 1;
	}
	out->entries = 0;
}

void 
ipx_sap_output_request(struct sap_output * out, ser_type_t ser_type)
{
	struct sap_entry *se;
	ipx_sap_output_flush(out);
	out->buffer.operation = htons(IPX_SAP_OP_REQUEST);
	se = &(out->buffer.sap_entries[out->entries]);
	se->ser_type = htons(ser_type);
	out->entries++;
}

void 
ipx_sap_output_gns_request(struct sap_output * out, ser_type_t ser_type)
{
	struct sap_entry *se;
	ipx_sap_output_flush(out);
	out->buffer.operation = htons(IPX_SAP_OP_GNS_REQUEST);
	se = &(out->buffer.sap_entries[out->entries]);
	se->ser_type = htons(ser_type);
	out->entries++;
}

void 
ipx_sap_output_response(struct sap_output * out, ser_type_t type,
			ser_name_t name, struct sockaddr_ipx *addr,
			hop_t hops, int down_allow)
{
	struct sap_entry *se;

	if (hops >= IPX_SAP_SERVER_DOWN && !down_allow)
	{
		return;
	}
	if (out->entries >= IPX_SAP_MAX_ENTRIES)
	{
		ipx_sap_output_flush(out);
	}
	if (out->buffer.operation != htons(IPX_SAP_OP_RESPONSE))
	{
		ipx_sap_output_flush(out);
		out->buffer.operation = htons(IPX_SAP_OP_RESPONSE);
	}

	if (passive != 0)
	{
		return;
	}

	se = &(out->buffer.sap_entries[out->entries]);
	se->ser_type = htons(type);
	ipx_sap_assign_ser_name(se->ser_name, name);
	se->network = addr->sipx_network;
	ipx_assign_node(se->node, addr->sipx_node);
	se->port = addr->sipx_port;
	se->hops = htons(hops);
	out->entries++;
}

void 
ipx_sap_output_gns_response(struct sap_output * out,
			    ser_type_t type, ser_name_t name,
			    struct sockaddr_ipx *addr, hop_t hops)
{
	struct sap_entry *se;

	if (hops >= IPX_SAP_SERVER_DOWN)
	{
		return;
	}

	ipx_sap_output_flush(out);

	if (passive != 0)
	{
		return;
	}
	
	out->buffer.operation = htons(IPX_SAP_OP_GNS_RESPONSE);
	se = &(out->buffer.sap_entries[out->entries]);
	se->ser_type = htons(type);
	ipx_sap_assign_ser_name(se->ser_name, name);
	se->network = addr->sipx_network;
	ipx_assign_node(se->node, addr->sipx_node);
	se->port = addr->sipx_port;
	se->hops = htons(hops);
	out->entries++;
}

void 
ipx_sap_output_set_destination(struct sap_output * out,
			       IPXNode node, IPXPort port)
{
	ipx_sap_output_flush(out);
	ipx_assign_node(out->dest_addr.sipx_node, node);
	out->dest_addr.sipx_port = htons(port);
}

void 
ipx_sap_fprint_name(FILE * file, char *sname)
{
	char name[IPX_SAP_SERVER_NAME_LEN + 1];
	int len;

	memcpy(name, sname, IPX_SAP_SERVER_NAME_LEN);
	name[IPX_SAP_SERVER_NAME_LEN] = 0;
	len = strlen(name);
	memset(name + len, ' ', IPX_SAP_SERVER_NAME_LEN - len);
	fprintf(file, name);
}

static void 
fprint_entry(FILE * file, struct sap_entry *se)
{
	fprintf(file, "SAP: type: %04X name: ", ntohs(se->ser_type));
	ipx_sap_fprint_name(file, se->ser_name);
	fprintf(file, " \nhops: %i addr: ", ntohs(se->hops));
	ipx_fprint_network(file, ntohl(se->network));
	fprintf(file, ":");
	ipx_fprint_node(file, se->node);
	fprintf(file, ":");
	ipx_fprint_port(file, ntohs(se->port));
	fprintf(file, "\n");
}

void 
ipx_sap_dump(struct sap_packet *pkt, int len)
{
	ipx_sap_fdump(stdout, pkt, len);
}

void 
ipx_sap_fdump(FILE * file, struct sap_packet *pkt, int len)
{
	int ent;
	struct sap_entry *se = pkt->sap_entries;

	if (len < 2)
	{
		return;
	}

	fprintf(file, "Operation: %i size: %i ", ntohs(pkt->operation), len);
	switch (ntohs(pkt->operation))
	{
	case IPX_SAP_OP_REQUEST:
		fprintf(file, "(Request)\n");
		if (ipx_sap_size(1, IPX_SAP_OP_REQUEST) != len)
		{
			fprintf(file, "Warning: Bad SAP size\n");
		}
		fprintf(file, "SAP: Type: %04X\n", ntohs(se->ser_type));
		break;
	case IPX_SAP_OP_GNS_REQUEST:
		fprintf(file, "(Get Nearest Server Request)\n");
		if (ipx_sap_size(1, IPX_SAP_OP_GNS_REQUEST) != len)
		{
			fprintf(file, "Warning: Bad SAP size\n");
		}
		fprintf(file, "SAP: Type: %04X\n", ntohs(se->ser_type));
		break;
	case IPX_SAP_OP_RESPONSE:

		fprintf(file, "(Response)\n");
		ent = (len - 2) / sizeof(struct sap_entry);

		if (ipx_sap_size(ent, IPX_SAP_OP_RESPONSE) != len)
		{
			fprintf(file, "Warning: Bad SAP size\n");
		}
		for (; ent--; se++)
		{
			fprint_entry(file, se);
		}
		break;
	case IPX_SAP_OP_GNS_RESPONSE:
		fprintf(file, "(Get Nearest Server Response)\n");
		if (ipx_sap_size(1, IPX_SAP_OP_RESPONSE) != len)
		{
			fprintf(file, "Warning: Bad SAP size\n");
		}
		fprint_entry(file, se);
		break;
	default:
		fprintf(file, "(Unknown)\n");
		break;
	}
}

int (*ipx_sap_output_func) (int sock, void *, int, struct sockaddr_ipx *)
	= NULL;