ncpl_rpc.c revision 90868
1/*
2 * Copyright (c) 1999, Boris Popov
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *    This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 * NetWare RPCs
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/lib/libncp/ncpl_rpc.c 90868 2002-02-18 20:35:27Z mike $");
36
37#include <sys/types.h>
38#include <sys/time.h>
39#include <arpa/inet.h>
40#include <errno.h>
41#include <stdio.h>
42#include <strings.h>
43#include <netncp/ncp_lib.h>
44
45struct ncp_rpc_rq {
46	nuint16		len;	/* HL */
47	nuint8		subfn;
48	nuint32		reserved[4];
49	nuint8		flags[4];
50} __attribute__ ((packed));
51
52struct ncp_rpc_rp {
53	nuint32		rpccode;
54	nuint32		reserved[4];
55	nuint32		rpcval;
56} __attribute__ ((packed));
57
58static NWCCODE
59ncp_rpc(NWCONN_HANDLE cH, int rpcfn,
60	const nuint8* rpcarg, char* arg1, char *arg2,
61	nuint32* rpcval) {
62	NWCCODE error;
63	NW_FRAGMENT rq[4], rp;
64	struct ncp_rpc_rq rqh;
65	struct ncp_rpc_rp rph;
66
67	rqh.subfn = rpcfn;
68	if (rpcarg)
69		bcopy(rpcarg, rqh.reserved, 4 * 4 + 4);
70	else
71		bzero(rqh.reserved, 4 * 4 + 4);
72	rq[0].fragAddress = (char*)&rqh;
73	rq[0].fragSize = sizeof(rqh);
74	rq[1].fragAddress = arg1;
75	rq[1].fragSize = strlen(arg1) + 1;
76	rq[2].fragAddress = arg2;
77	rq[2].fragSize = arg2 ? (strlen(arg2) + 1) : 0;
78	rqh.len = htons(rq[2].fragSize + rq[1].fragSize + sizeof(rqh) - 2);
79	rp.fragAddress = (char*)&rph;
80	rp.fragSize = sizeof(rph);
81	error = NWRequest(cH, 131, 3, rq, 1, &rp);
82	if (error) return error;
83	if (rp.fragSize < 4) return EBADRPC;
84	error = rph.rpccode;
85	if (error) return error;
86	if (rpcval) {
87		if (rp.fragSize < 24)
88			return EBADRPC;
89		*rpcval = rph.rpcval;
90	}
91	return 0;
92}
93
94NWCCODE
95NWSMLoadNLM(NWCONN_HANDLE cH, pnstr8 cmd) {
96	return ncp_rpc(cH, 1, NULL, cmd, NULL, NULL);
97}
98
99NWCCODE
100NWSMUnloadNLM(NWCONN_HANDLE cH, pnstr8 cmd) {
101	return ncp_rpc(cH, 2, NULL, cmd, NULL, NULL);
102}
103
104NWCCODE
105NWSMMountVolume(NWCONN_HANDLE cH, pnstr8 volName, nuint32* volnum) {
106	return ncp_rpc(cH, 3, NULL, volName, NULL, volnum);
107}
108
109NWCCODE
110NWSMDismountVolumeByName(NWCONN_HANDLE cH, pnstr8 vol) {
111	return ncp_rpc(cH, 4, NULL, vol, NULL, NULL);
112}
113
114struct ncp_set_hdr {
115	nuint32	typeFlag;	/* 0 - str, 1 - value */
116	nuint32	value;
117	nuint32	pad[20 - 4 - 4];
118} __attribute__ ((packed));
119
120NWCCODE
121NWSMSetDynamicCmdIntValue(NWCONN_HANDLE cH, pnstr8 setCommandName, nuint32 cmdValue) {
122	struct ncp_set_hdr rq;
123
124	memset(&rq, 0, sizeof(rq));
125	rq.typeFlag = 1;
126	rq.value = cmdValue;
127	return ncp_rpc(cH, 6, (char*)&rq, setCommandName, NULL, NULL);
128}
129
130NWCCODE
131NWSMSetDynamicCmdStrValue(NWCONN_HANDLE cH, pnstr8 setCommandName,
132		pnstr8 cmdValue) {
133	return ncp_rpc(cH, 6, NULL, setCommandName, cmdValue, NULL);
134}
135
136NWCCODE
137NWSMExecuteNCFFile(NWCONN_HANDLE cH, pnstr8 NCFFileName) {
138	return ncp_rpc(cH, 7, NULL, NCFFileName, NULL, NULL);
139}
140