Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / netproto / ncp / ncp_subr.c
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  *
32  * $FreeBSD: src/sys/netncp/ncp_subr.c,v 1.2.2.1 2001/02/22 08:54:11 bp Exp $
33  * $DragonFly: src/sys/netproto/ncp/ncp_subr.c,v 1.2 2003/06/17 04:28:53 dillon Exp $
34  */
35 #include <sys/param.h>
36 #include <sys/errno.h>
37 #include <sys/proc.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/time.h>
42 #include <sys/uio.h>
43 #include <sys/mbuf.h>
44
45 #include <netncp/ncp.h>
46 #include <netncp/ncp_conn.h>
47 #include <netncp/ncp_sock.h>
48 #include <netncp/ncp_subr.h>
49 #include <netncp/ncp_rq.h>
50 #include <netncp/ncp_ncp.h>
51 #include <netncp/nwerror.h>
52
53 int ncp_debuglevel = 0;
54
55 struct callout_handle ncp_timer_handle;
56
57 static void ncp_at_exit(struct proc *p);
58 static void ncp_timer(void *arg);
59
60 /*
61  * duplicate string from user space. It should be very-very slow.
62  */
63 char *
64 ncp_str_dup(char *s) {
65         char *p, bt;
66         int len = 0;
67
68         for (p = s;;p++) {
69                 if (copyin(p, &bt, 1)) return NULL;
70                 len++;
71                 if (bt == 0) break;
72         }
73         MALLOC(p, char*, len, M_NCPDATA, M_WAITOK);
74         copyin(s, p, len);
75         return p;
76 }
77
78
79 void
80 ncp_at_exit(struct proc *p) {
81         struct ncp_conn *ncp, *nncp;
82
83         if (ncp_conn_putprochandles(p) == 0) return;
84
85         ncp_conn_locklist(LK_EXCLUSIVE, p);
86         for (ncp = SLIST_FIRST(&conn_list); ncp; ncp = nncp) {
87                 nncp = SLIST_NEXT(ncp, nc_next);
88                 if (ncp->ref_cnt != 0) continue;
89                 if (ncp_conn_lock(ncp, p, p->p_ucred,NCPM_READ|NCPM_EXECUTE|NCPM_WRITE))
90                         continue;
91                 if (ncp_disconnect(ncp) != 0)
92                         ncp_conn_unlock(ncp,p);
93         }
94         ncp_conn_unlocklist(p);
95         return;
96 }
97
98 int
99 ncp_init(void) {
100         ncp_conn_init();
101         if (at_exit(ncp_at_exit)) {
102                 NCPFATAL("can't register at_exit handler\n");
103                 return ENOMEM;
104         }
105         ncp_timer_handle = timeout(ncp_timer,NULL,NCP_TIMER_TICK);
106         return 0;
107 }
108
109 void
110 ncp_done(void) {
111         struct ncp_conn *ncp, *nncp;
112         struct proc *p = curproc;
113         
114         untimeout(ncp_timer,NULL,ncp_timer_handle);
115         rm_at_exit(ncp_at_exit);
116         ncp_conn_locklist(LK_EXCLUSIVE, p);
117         for (ncp = SLIST_FIRST(&conn_list); ncp; ncp = nncp) {
118                 nncp = SLIST_NEXT(ncp, nc_next);
119                 ncp->ref_cnt = 0;
120                 if (ncp_conn_lock(ncp, p, p->p_ucred,NCPM_READ|NCPM_EXECUTE|NCPM_WRITE)) {
121                         NCPFATAL("Can't lock connection !\n");
122                         continue;
123                 }
124                 if (ncp_disconnect(ncp) != 0)
125                         ncp_conn_unlock(ncp,p);
126         }
127         ncp_conn_unlocklist(p);
128 }
129
130
131 /* tick every second and check for watch dog packets and lost connections */
132 static void
133 ncp_timer(void *arg){
134         struct ncp_conn *conn;
135
136         if(ncp_conn_locklist(LK_SHARED | LK_NOWAIT, NULL) == 0) {
137                 SLIST_FOREACH(conn, &conn_list, nc_next)
138                         ncp_check_conn(conn);
139                 ncp_conn_unlocklist(NULL);
140         }
141         ncp_timer_handle = timeout(ncp_timer,NULL,NCP_TIMER_TICK);
142 }
143
144 int
145 ncp_get_bindery_object_id(struct ncp_conn *conn, 
146                 u_int16_t object_type, char *object_name, 
147                 struct ncp_bindery_object *target,
148                 struct proc *p,struct ucred *cred)
149 {
150         int error;
151         DECLARE_RQ;
152
153         NCP_RQ_HEAD_S(23,53,p,cred);
154         ncp_rq_word_hl(rqp, object_type);
155         ncp_rq_pstring(rqp, object_name);
156         checkbad(ncp_request(conn,rqp));
157         if (rqp->rpsize < 54) {
158                 printf("ncp_rp_size %d < 54\n", rqp->rpsize);
159                 error = EINVAL;
160                 goto bad;
161         }
162         target->object_id = ncp_rp_dword_hl(rqp);
163         target->object_type = ncp_rp_word_hl(rqp);
164         ncp_rp_mem(rqp,(caddr_t)target->object_name, 48);
165         NCP_RQ_EXIT;
166         return error;
167 }
168
169 int
170 ncp_read(struct ncp_conn *conn, ncp_fh *file, struct uio *uiop, struct ucred *cred) {
171         int error = 0, len = 0, retlen=0, tsiz, burstio;
172         DECLARE_RQ;
173
174         tsiz = uiop->uio_resid;
175 #ifdef NCPBURST
176         burstio = (ncp_burst_enabled && tsiz > conn->buffer_size);
177 #else
178         burstio = 0;
179 #endif
180
181         while (tsiz > 0) {
182                 if (!burstio) {
183                         len = min(4096 - (uiop->uio_offset % 4096), tsiz);
184                         len = min(len, conn->buffer_size);
185                         NCP_RQ_HEAD(72,uiop->uio_procp,cred);
186                         ncp_rq_byte(rqp, 0);
187                         ncp_rq_mem(rqp, (caddr_t)file, 6);
188                         ncp_rq_dword(rqp, htonl(uiop->uio_offset));
189                         ncp_rq_word(rqp, htons(len));
190                         checkbad(ncp_request(conn,rqp));
191                         retlen = ncp_rp_word_hl(rqp);
192                         if (uiop->uio_offset & 1)
193                                 ncp_rp_byte(rqp);
194                         error = nwfs_mbuftouio(&rqp->mrp,uiop,retlen,&rqp->bpos);
195                         NCP_RQ_EXIT;
196                 } else {
197 #ifdef NCPBURST
198                         error = ncp_burst_read(conn, file, tsiz, &len, &retlen, uiop, cred);
199 #endif
200                 }
201                 if (error) break;
202                 tsiz -= retlen;
203                 if (retlen < len)
204                         break;
205         }
206         return (error);
207 }
208
209 int
210 ncp_write(struct ncp_conn *conn, ncp_fh *file, struct uio *uiop, struct ucred *cred)
211 {
212         int error = 0, len, tsiz, backup;
213         DECLARE_RQ;
214
215         if (uiop->uio_iovcnt != 1) {
216                 printf("%s: can't handle iovcnt>1 !!!\n", __FUNCTION__);
217                 return EIO;
218         }
219         tsiz = uiop->uio_resid;
220         while (tsiz > 0) {
221                 len = min(4096 - (uiop->uio_offset % 4096), tsiz);
222                 len = min(len, conn->buffer_size);
223                 if (len == 0) {
224                         printf("gotcha!\n");
225                 }
226                 /* rq head */
227                 NCP_RQ_HEAD(73,uiop->uio_procp,cred);
228                 ncp_rq_byte(rqp, 0);
229                 ncp_rq_mem(rqp, (caddr_t)file, 6);
230                 ncp_rq_dword(rqp, htonl(uiop->uio_offset));
231                 ncp_rq_word_hl(rqp, len);
232                 nwfs_uiotombuf(uiop,&rqp->mrq,len,&rqp->bpos);
233                 checkbad(ncp_request(conn,rqp));
234                 if (len == 0)
235                         break;
236                 NCP_RQ_EXIT;
237                 if (error) {
238                         backup = len;
239                         uiop->uio_iov->iov_base -= backup;
240                         uiop->uio_iov->iov_len += backup;
241                         uiop->uio_offset -= backup;
242                         uiop->uio_resid += backup;
243                         break;
244                 }
245                 tsiz -= len;
246         }
247         if (error)
248                 uiop->uio_resid = tsiz;
249         switch (error) {
250             case NWE_INSUFFICIENT_SPACE:
251                 error = ENOSPC;
252                 break;
253         }
254         return (error);
255 }