aboutsummaryrefslogtreecommitdiff
path: root/lib/libncp/ncpl_misc.c
blob: 67d19a9132822df890b2a8391b1ea383fb7b806e (plain) (blame)
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
/*
 * Copyright (c) 1999, Boris Popov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * calls that don't fit to any other category
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/types.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>

#include <netncp/ncp_lib.h>

static time_t
ncp_nw_to_ctime(struct nw_time_buffer *source) {
	struct tm u_time;

	bzero(&u_time,sizeof(struct tm));
	/*
	 * XXX: NW 4.x tracks daylight automatically
	 */
	u_time.tm_isdst = -1;
	u_time.tm_sec = source->second;
	u_time.tm_min = source->minute;
	u_time.tm_hour = source->hour;
	u_time.tm_mday = source->day;
	u_time.tm_mon = source->month - 1;
	u_time.tm_year = source->year;

	if (u_time.tm_year < 80) {
		u_time.tm_year += 100;
	}
	return mktime(&u_time);
}

int
ncp_get_file_server_information(NWCONN_HANDLE connid,
	struct ncp_file_server_info *target)
{
	int error;
	DECLARE_RQ;

	ncp_init_request_s(conn, 17);
	if ((error = ncp_request(connid, 23, conn)) != 0) 
		return error;
	memcpy(target, ncp_reply_data(conn, 0), sizeof(*target));
	target->MaximumServiceConnections
	    = htons(target->MaximumServiceConnections);
	target->ConnectionsInUse
	    = htons(target->ConnectionsInUse);
	target->MaxConnectionsEverUsed
	    = htons(target->MaxConnectionsEverUsed);
	target->NumberMountedVolumes
	    = htons(target->NumberMountedVolumes);
	return 0;
}

int
ncp_get_stations_logged_info(NWCONN_HANDLE connid, u_int32_t connection,
	struct ncp_bindery_object *target, time_t *login_time)
{
	int error;
	DECLARE_RQ;

	ncp_init_request_s(conn, 28);
	ncp_add_dword_lh(conn, connection);

	if ((error = ncp_request(connid, 23, conn)) != 0)
		return error;
	bzero(target, sizeof(*target));
	target->object_id = ncp_reply_dword_hl(conn, 0);
	target->object_type = ncp_reply_word_hl(conn, 4);
	memcpy(target->object_name, ncp_reply_data(conn, 6),
	       sizeof(target->object_name));
	*login_time = ncp_nw_to_ctime((struct nw_time_buffer *)ncp_reply_data(conn, 54));
	return 0;
}

int
ncp_get_internet_address(NWCONN_HANDLE connid, u_int32_t connection,
	struct ipx_addr *target, u_int8_t * conn_type)
{
	int error;
	DECLARE_RQ;

	ncp_init_request_s(conn, 26);
	ncp_add_dword_lh(conn, connection);
	error = ncp_request(connid, 23, conn);
	if (error) return error;
	bzero(target, sizeof(*target));
	ipx_netlong(*target) = ncp_reply_dword_lh(conn, 0);
	memcpy(&(target->x_host), ncp_reply_data(conn, 4), 6);
	target->x_port = ncp_reply_word_lh(conn, 10);
	*conn_type = ncp_reply_byte(conn, 12);
	return 0;
}

NWCCODE
NWGetObjectConnectionNumbers(NWCONN_HANDLE connHandle,
		pnstr8 pObjName, nuint16 objType,
		pnuint16 pNumConns, pnuint16 pConnHandleList,
		nuint16 maxConns) 
{
	int error, i, n;
	nuint32 lastconn;
	DECLARE_RQ;

	lastconn = 0;
	ncp_init_request_s(conn, 27);
	ncp_add_dword_lh(conn, lastconn);
	ncp_add_word_hl(conn, objType);
	ncp_add_pstring(conn, pObjName);
	if ((error = ncp_request(connHandle, 23, conn)) != 0) return error;
	n = min(ncp_reply_byte(conn, 0), maxConns);
	*pNumConns = n;
	for (i = 0; i < n ; i++) {
		*pConnHandleList++ = ncp_reply_dword_lh(conn, i * 4 + 1);
	}
	return 0;
}

void
NWUnpackDateTime(nuint32 dateTime, NW_DATE *sDate, NW_TIME *sTime) {
	NWUnpackDate(dateTime >> 16, sDate);
	NWUnpackTime(dateTime & 0xffff, sTime);
}

void
NWUnpackDate(nuint16 date, NW_DATE *sDate) {
	sDate->day = date & 0x1f;
	sDate->month = (date >> 5) & 0xf;
	sDate->year = ((date >> 9) & 0x7f) + 1980;
}

void
NWUnpackTime(nuint16 time, NW_TIME *sTime) {
	sTime->seconds = time & 0x1f;
	sTime->minutes = (time >> 5) & 0x3f;
	sTime->hours = (time >> 11) & 0x1f;
}

nuint32
NWPackDateTime(NW_DATE *sDate, NW_TIME *sTime) {
	return 0;
}

nuint16
NWPackDate(NW_DATE *sDate) {
	return 0;
}

nuint16
NWPackTime(NW_TIME *sTime) {
	return 0;
}

time_t
ncp_UnpackDateTime(nuint32 dateTime) {
	struct tm u_time;
	NW_DATE d;
	NW_TIME t;

	NWUnpackDateTime(dateTime, &d, &t);
	bzero(&u_time,sizeof(struct tm));
	u_time.tm_isdst = -1;
	u_time.tm_sec = t.seconds;
	u_time.tm_min = t.minutes;
	u_time.tm_hour = t.hours;
	u_time.tm_mday = d.day;
	u_time.tm_mon = d.month - 1;
	u_time.tm_year = d.year - 1900;

	return mktime(&u_time);
}

int
ncp_GetFileServerDateAndTime(NWCONN_HANDLE cH, time_t *target) {
	int error;
	DECLARE_RQ;

	ncp_init_request(conn);
	if ((error = ncp_request(cH, 20, conn)) != 0)
		return error;
	*target = ncp_nw_to_ctime((struct nw_time_buffer *) ncp_reply_data(conn, 0));
	return 0;
}

int
ncp_SetFileServerDateAndTime(NWCONN_HANDLE cH, time_t * source) {
	int year;
	struct tm *utime = localtime(source);
	DECLARE_RQ;

	year = utime->tm_year;
	if (year > 99) {
		year -= 100;
	}
	ncp_init_request_s(conn, 202);
	ncp_add_byte(conn, year);
	ncp_add_byte(conn, utime->tm_mon + 1);
	ncp_add_byte(conn, utime->tm_mday);
	ncp_add_byte(conn, utime->tm_hour);
	ncp_add_byte(conn, utime->tm_min);
	ncp_add_byte(conn, utime->tm_sec);
	return ncp_request(cH, 23, conn);
}

NWCCODE
NWDownFileServer(NWCONN_HANDLE cH, int force) {
	DECLARE_RQ;

	ncp_init_request_s(conn, 211);
	ncp_add_byte(conn, force ? 0 : 0xff);
	return ncp_request(cH, 23, conn);
}

NWCCODE
NWCloseBindery(NWCONN_HANDLE cH) {
	DECLARE_RQ;

	ncp_init_request_s(conn, 68);
	return ncp_request(cH, 23, conn);
}

NWCCODE
NWOpenBindery(NWCONN_HANDLE cH) {
	DECLARE_RQ;

	ncp_init_request_s(conn, 69);
	return ncp_request(cH, 23, conn);
}

NWCCODE
NWDisableTTS(NWCONN_HANDLE cH) {
	DECLARE_RQ;

	ncp_init_request_s(conn, 207);
	return ncp_request(cH, 23, conn);
}

NWCCODE
NWEnableTTS(NWCONN_HANDLE cH) {
	DECLARE_RQ;

	ncp_init_request_s(conn, 208);
	return ncp_request(cH, 23, conn);
}

NWCCODE
NWDisableFileServerLogin(NWCONN_HANDLE cH) {
	DECLARE_RQ;

	ncp_init_request_s(conn, 203);
	return ncp_request(cH, 23, conn);
}

NWCCODE
NWEnableFileServerLogin(NWCONN_HANDLE cH) {
	DECLARE_RQ;

	ncp_init_request_s(conn, 204);
	return ncp_request(cH, 23, conn);
}