- New function Buf_Append(), which is given a pointer to a string to
[dragonfly.git] / sys / netproto / ncp / ncp_rq.h
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_rq.h,v 1.4 2000/01/14 19:54:38 bde Exp $
33  * $DragonFly: src/sys/netproto/ncp/ncp_rq.h,v 1.3 2003/06/25 03:56:05 dillon Exp $
34  */
35 #ifndef _NETNCP_NCP_RQ_H_
36 #define _NETNCP_NCP_RQ_H_
37
38 #include <machine/endian.h>
39
40 #define getb(buf,ofs)           (((const u_int8_t *)(buf))[ofs])
41 #define setb(buf,ofs,val)       (((u_int8_t*)(buf))[ofs])=val
42 #define getbw(buf,ofs)          ((u_int16_t)(getb(buf,ofs)))
43
44 #if (BYTE_ORDER == LITTLE_ENDIAN)
45
46 #define getwle(buf,ofs) (*((u_int16_t*)(&((u_int8_t*)(buf))[ofs])))
47 #define getdle(buf,ofs) (*((u_int32_t*)(&((u_int8_t*)(buf))[ofs])))
48 #define getwbe(buf,ofs) (ntohs(getwle(buf,ofs)))
49 #define getdbe(buf,ofs) (ntohl(getdle(buf,ofs)))
50
51 #define setwle(buf,ofs,val) getwle(buf,ofs)=val
52 #define setwbe(buf,ofs,val) getwle(buf,ofs)=htons(val)
53 #define setdle(buf,ofs,val) getdle(buf,ofs)=val
54 #define setdbe(buf,ofs,val) getdle(buf,ofs)=htonl(val)
55
56 #define htoles(x)       ((u_int16_t)(x))
57 #define letohs(x)       ((u_int16_t)(x))
58 #define htolel(x)       ((u_int32_t)(x))
59 #define letohl(x)       ((u_int32_t)(x))
60
61 #else
62 #error "Macros for Big-Endians are incomplete"
63 #define getwle(buf,ofs) ((u_int16_t)(getb(buf, ofs) | (getb(buf, ofs + 1) << 8)))
64 #define getdle(buf,ofs) ((u_int32_t)(getb(buf, ofs) | \
65                                     (getb(buf, ofs + 1) << 8) | \
66                                     (getb(buf, ofs + 2) << 16) | \
67                                     (getb(buf, ofs + 3) << 24)))
68 #define getwbe(buf,ofs) (*((u_int16_t*)(&((u_int8_t*)(buf))[ofs])))
69 #define getdbe(buf,ofs) (*((u_int32_t*)(&((u_int8_t*)(buf))[ofs])))
70 /*
71 #define setwle(buf,ofs,val) getwle(buf,ofs)=val
72 #define setdle(buf,ofs,val) getdle(buf,ofs)=val
73 */
74 #define setwbe(buf,ofs,val) getwle(buf,ofs)=val
75 #define setdbe(buf,ofs,val) getdle(buf,ofs)=val
76 /*
77 #define htoles(x)       ((u_int16_t)(x))
78 #define letohs(x)       ((u_int16_t)(x))
79 #define htolel(x)       ((u_int32_t)(x))
80 #define letohl(x)       ((u_int32_t)(x))
81 */
82 #endif
83
84
85 #ifdef _KERNEL
86 struct ncp_nlstables;
87 /* 
88  * Structure to prepare ncp request and receive reply 
89  */
90 struct ncp_rq {
91         struct ncp_conn *conn;          /* back link */
92         struct mbuf     *rq;
93         struct mbuf     *mrq;
94         struct mbuf     *rp;
95         struct mbuf     *mrp;
96         caddr_t         bpos;
97 /*      int             rqsize;*/               /* request size without ncp header */
98         int             rpsize;         /* reply size minus ncp header */
99         int             cc;             /* completion code */
100         int             cs;             /* connection state */
101         struct thread   *td;            /* thread that did rq */
102         struct ucred    *cred;          /* user that did rq */
103         int             rexmit;
104 };
105
106 #define DECLARE_RQ      struct ncp_rq rq;struct ncp_rq *rqp=&rq
107
108
109 int  ncp_rq_head(struct ncp_rq *rqp,u_int32_t ptype, u_int8_t fn,
110     struct thread *td, struct ucred *cred);
111 int  ncp_rq_done(struct ncp_rq *rqp);
112
113 /* common case for normal request */
114 #define ncp_rq_init(rqp,fn,td,c) ncp_rq_head((rqp),NCP_REQUEST,(fn),(td),(c))
115 #define ncp_rq_close(rqp)       ncp_rq_done((rqp))
116
117 #define NCP_RQ_HEAD(fn,td,c)    ncp_rq_init(rqp,fn,td,c)
118 #define NCP_RQ_HEAD_S(fn,sfn,td,c)      do { NCP_RQ_HEAD(fn,td,c);ncp_rq_word(rqp,0);ncp_rq_byte(rqp,(sfn)); } while(0)
119 #define NCP_RQ_EXIT     bad: ncp_rq_close(rqp)
120 #define NCP_RQ_EXIT_NB  ncp_rq_close(rqp)
121 #define ncp_rq_word     ncp_rq_word_lh
122 #define ncp_rq_dword    ncp_rq_dword_lh
123
124 /*void ncp_init_request(struct ncp_rq *rqp, int fn);
125 void ncp_close_request(struct ncp_rq *rqp);*/
126 void ncp_rq_byte(struct ncp_rq *rqp, u_int8_t x);
127 void ncp_rq_word_hl(struct ncp_rq *rqp, u_int16_t x);
128 void ncp_rq_word_lh(struct ncp_rq *rqp, u_int16_t x);
129 void ncp_rq_dword_lh(struct ncp_rq *rqp, u_int32_t x);
130 static void ncp_rq_mem(struct ncp_rq *rqp, caddr_t source, int size);
131 static int  ncp_rq_usermem(struct ncp_rq *rqp, caddr_t source, int size);
132 int  ncp_rq_mbuf(struct ncp_rq *rqp, struct mbuf *m, int size);
133 int  ncp_rq_putanymem(struct ncp_rq *rqp, caddr_t source, int size,int type);
134 void ncp_rq_pathstring(struct ncp_rq *rqp, int size, char *name, struct ncp_nlstables*);
135 void ncp_rq_dbase_path(struct ncp_rq *, u_int8_t vol_num,
136                     u_int32_t dir_base, int namelen, u_char *name, struct ncp_nlstables *nt);
137 void ncp_rq_pstring(struct ncp_rq *rqp, char *s);
138
139 u_int8_t  ncp_rp_byte(struct ncp_rq *rqp);
140 u_int16_t ncp_rp_word_hl(struct ncp_rq *rqp);
141 u_int16_t ncp_rp_word_lh(struct ncp_rq *rqp);
142 u_int32_t ncp_rp_dword_hl(struct ncp_rq *rqp);
143 u_int32_t ncp_rp_dword_lh(struct ncp_rq *rqp);
144 void      ncp_rp_mem(struct ncp_rq *rqp,caddr_t target, int size);
145 int       ncp_rp_usermem(struct ncp_rq *rqp,caddr_t target, int size);
146 int nwfs_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, caddr_t *dpos);
147 int nwfs_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos);
148 struct mbuf* ncp_rp_mbuf(struct ncp_rq *rqp, int size);
149
150 static void __inline ncp_rq_mem(struct ncp_rq *rqp, caddr_t source, int size) {
151         ncp_rq_putanymem(rqp,source,size,0);
152 }
153 static int __inline ncp_rq_usermem(struct ncp_rq *rqp, caddr_t source, int size) {
154         return ncp_rq_putanymem(rqp,source,size,1);
155 }
156 void ncp_sign_init(const char *logindata, char *sign_root);
157
158 #else /* ifdef _KERNEL */
159
160 #define DECLARE_RQ      struct ncp_buf conn1, *conn=&conn1
161
162 #define ncp_add_byte(conn,x) (conn)->packet[(conn)->rqsize++]=x
163
164 struct ncp_buf;
165
166 __BEGIN_DECLS
167
168 void ncp_init_request(struct ncp_buf *);
169 void ncp_init_request_s(struct ncp_buf *, int);
170 void ncp_add_word_lh(struct ncp_buf *, u_int16_t);
171 void ncp_add_dword_lh(struct ncp_buf *, u_int32_t);
172 void ncp_add_word_hl(struct ncp_buf *, u_int16_t);
173 void ncp_add_dword_hl(struct ncp_buf *, u_int32_t);
174 void ncp_add_mem(struct ncp_buf *, const void *, int);
175 void ncp_add_mem_nls(struct ncp_buf *, const void *, int);
176 void ncp_add_pstring(struct ncp_buf *, const char *);
177 void ncp_add_handle_path(struct ncp_buf *, nuint32, nuint32, int, const char *);
178
179 #define ncp_reply_data(conn,offset) ((conn)->packet+offset)
180 #define ncp_reply_byte(conn,offset) (*(u_int8_t*)(ncp_reply_data(conn, offset)))
181
182 u_int16_t ncp_reply_word_hl(struct ncp_buf *, int);
183 u_int16_t ncp_reply_word_lh(struct ncp_buf *, int);
184 u_int32_t ncp_reply_dword_hl(struct ncp_buf *, int);
185 u_int32_t ncp_reply_dword_lh(struct ncp_buf *, int);
186
187 static __inline void
188 ConvertToNWfromDWORD(u_int32_t sfd, ncp_fh *fh) {
189         fh->val1 = (fh->val.val32 = sfd);
190         return;
191 }
192
193 __END_DECLS
194
195 #endif /* ifdef _KERNEL */
196
197 #endif  /* !_NETNCP_NCP_RQ_H_ */