Sync usage with man page.
[netbsd-mini2440.git] / dist / smbfs / lib / smb / rq.c
blob2f5944a832908d5c5dd0de74d2bb81a9b026e450
1 /*
2 * Copyright (c) 2000, Boris Popov
3 * All rights reserved.
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.
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.
32 * from: Id: rq.c,v 1.7 2001/04/16 04:33:01 bp Exp
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: rq.c,v 1.4 2006/05/10 06:24:02 skrll Exp $");
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/stat.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <err.h>
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <strings.h>
47 #include <stdlib.h>
48 #include <sysexits.h>
50 #include <netsmb/smb_lib.h>
51 #include <netsmb/smb_conn.h>
52 #include <netsmb/smb_rap.h>
54 #include "smb_kernelops.h"
56 int
57 smb_rq_init(struct smb_ctx *ctx, u_char cmd, size_t rpbufsz, struct smb_rq **rqpp)
59 struct smb_rq *rqp;
61 rqp = malloc(sizeof(*rqp));
62 if (rqp == NULL)
63 return ENOMEM;
64 bzero(rqp, sizeof(*rqp));
65 rqp->rq_cmd = cmd;
66 rqp->rq_ctx = ctx;
67 mb_init(&rqp->rq_rq, M_MINSIZE);
68 mb_init(&rqp->rq_rp, rpbufsz);
69 *rqpp = rqp;
70 return 0;
73 void
74 smb_rq_done(struct smb_rq *rqp)
76 mb_done(&rqp->rq_rp);
77 mb_done(&rqp->rq_rq);
78 free(rqp);
81 void
82 smb_rq_wend(struct smb_rq *rqp)
84 if (rqp->rq_rq.mb_count & 1)
85 smb_error("smbrq_wend: odd word count\n", 0);
86 rqp->rq_wcount = rqp->rq_rq.mb_count / 2;
87 rqp->rq_rq.mb_count = 0;
90 int
91 smb_rq_dmem(struct mbdata *mbp, const char *src, size_t size)
93 struct mbuf *m;
94 char * dst;
95 int cplen, error;
97 if (size == 0)
98 return 0;
99 m = mbp->mb_cur;
100 if ((error = m_getm(m, size, &m)) != 0)
101 return error;
102 while (size > 0) {
103 cplen = M_TRAILINGSPACE(m);
104 if (cplen == 0) {
105 m = m->m_next;
106 continue;
108 if (cplen > (int)size)
109 cplen = size;
110 dst = mtod(m, char *) + m->m_len;
111 nls_mem_toext(dst, src, cplen);
112 size -= cplen;
113 src += cplen;
114 m->m_len += cplen;
115 mbp->mb_count += cplen;
117 mbp->mb_pos = mtod(m, char *) + m->m_len;
118 mbp->mb_cur = m;
119 return 0;
123 smb_rq_dstring(struct mbdata *mbp, const char *s)
125 return smb_rq_dmem(mbp, s, strlen(s) + 1);
129 smb_rq_simple(struct smb_rq *rqp)
131 struct smbioc_rq krq;
132 struct mbdata *mbp;
133 char *data;
135 mbp = smb_rq_getrequest(rqp);
136 m_lineup(mbp->mb_top, &mbp->mb_top);
137 data = mtod(mbp->mb_top, char*);
138 bzero(&krq, sizeof(krq));
139 krq.ioc_cmd = rqp->rq_cmd;
140 krq.ioc_twc = rqp->rq_wcount;
141 krq.ioc_twords = data;
142 krq.ioc_tbc = mbp->mb_count;
143 krq.ioc_tbytes = data + rqp->rq_wcount * 2;
144 mbp = smb_rq_getreply(rqp);
145 krq.ioc_rpbufsz = mbp->mb_top->m_maxlen;
146 krq.ioc_rpbuf = mtod(mbp->mb_top, char *);
147 if (smb_kops.ko_ioctl(rqp->rq_ctx->ct_fd, SMBIOC_REQUEST, &krq) == -1)
148 return errno;
149 mbp->mb_top->m_len = krq.ioc_rwc * 2 + krq.ioc_rbc;
150 rqp->rq_wcount = krq.ioc_rwc;
151 rqp->rq_bcount = krq.ioc_rbc;
152 return 0;
156 smb_t2_request(struct smb_ctx *ctx, int setup, int setupcount,
157 const char *name,
158 int tparamcnt, void *tparam,
159 int tdatacnt, void *tdata,
160 int *rparamcnt, void *rparam,
161 int *rdatacnt, void *rdata)
163 struct smbioc_t2rq krq;
165 bzero(&krq, sizeof(krq));
166 krq.ioc_setup[0] = setup;
167 krq.ioc_setupcnt = setupcount;
168 krq.ioc_name = __UNCONST(name);
169 krq.ioc_tparamcnt = tparamcnt;
170 krq.ioc_tparam = tparam;
171 krq.ioc_tdatacnt = tdatacnt;
172 krq.ioc_tdata = tdata;
173 krq.ioc_rparamcnt = *rparamcnt;
174 krq.ioc_rparam = rparam;
175 krq.ioc_rdatacnt = *rdatacnt;
176 krq.ioc_rdata = rdata;
177 if (smb_kops.ko_ioctl(ctx->ct_fd, SMBIOC_T2RQ, &krq) == -1)
178 return errno;
179 *rparamcnt = krq.ioc_rparamcnt;
180 *rdatacnt = krq.ioc_rdatacnt;
181 return 0;