067cc028fabdd1f25e04bf97a3e857525ba2a998
[dragonfly.git] / lib / libncp / ncpl_queue.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/lib/libncp/ncpl_queue.c,v 1.2 1999/10/31 03:39:03 bp Exp $
33  *
34  * NetWare queue interface
35  *
36  */
37 #include <sys/types.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <netncp/ncp_lib.h>
42
43 int
44 ncp_create_queue_job_and_file(NWCONN_HANDLE connid, u_int32_t queue_id, 
45         struct queue_job *job)
46 {
47         int error;
48         DECLARE_RQ;
49
50         ncp_init_request_s(conn, 121);
51         ncp_add_dword_hl(conn, queue_id);
52         ncp_add_mem(conn, &(job->j), sizeof(job->j));
53
54         if ((error = ncp_request(connid, 23, conn)) != 0)
55                 return error;
56         memcpy(&(job->j), ncp_reply_data(conn, 0), 78);
57         ConvertToNWfromDWORD(job->j.JobFileHandle, &job->file_handle);
58         return 0;
59 }
60
61 int
62 ncp_close_file_and_start_job(NWCONN_HANDLE connid, u_int32_t queue_id,
63         struct queue_job *job)
64 {
65         int error;
66         DECLARE_RQ;
67
68         ncp_init_request_s(conn, 127);
69         ncp_add_dword_hl(conn, queue_id);
70         ncp_add_dword_lh(conn, job->j.JobNumber);
71         error = ncp_request(connid, 23, conn);
72         return error;
73 }
74
75 int
76 ncp_attach_to_queue(NWCONN_HANDLE connid, u_int32_t queue_id) {
77         int error;
78         DECLARE_RQ;
79
80         ncp_init_request_s(conn, 111);
81         ncp_add_dword_hl(conn, queue_id);
82         error = ncp_request(connid, 23, conn);
83         return error;
84 }
85
86 int
87 ncp_detach_from_queue(NWCONN_HANDLE connid, u_int32_t queue_id) {
88         int error;
89         DECLARE_RQ;
90
91         ncp_init_request_s(conn, 112);
92         ncp_add_dword_hl(conn, queue_id);
93         error= ncp_request(connid, 23, conn);
94         return error;
95 }
96
97 int
98 ncp_service_queue_job(NWCONN_HANDLE connid, u_int32_t queue_id,
99         u_int16_t job_type, struct queue_job *job)
100 {
101         int error;
102         DECLARE_RQ;
103
104         ncp_init_request_s(conn, 124);
105         ncp_add_dword_hl(conn, queue_id);
106         ncp_add_word_hl(conn, job_type);
107         if ((error = ncp_request(connid, 23, conn)) != 0) {
108                 return error;
109         }
110         memcpy(&(job->j), ncp_reply_data(conn, 0), 78);
111         ConvertToNWfromDWORD(job->j.JobFileHandle, &job->file_handle);
112         return error;
113 }
114
115 int
116 ncp_finish_servicing_job(NWCONN_HANDLE connid, u_int32_t queue_id,
117         u_int32_t job_number, u_int32_t charge_info)
118 {
119         int error;
120         DECLARE_RQ;
121
122         ncp_init_request_s(conn, 131);
123         ncp_add_dword_hl(conn, queue_id);
124         ncp_add_dword_lh(conn, job_number);
125         ncp_add_dword_hl(conn, charge_info);
126
127         error = ncp_request(connid, 23, conn);
128         return error;
129 }
130
131 int
132 ncp_abort_servicing_job(NWCONN_HANDLE connid, u_int32_t queue_id,
133         u_int32_t job_number)
134 {
135         int error;
136         DECLARE_RQ;
137
138         ncp_init_request_s(conn, 132);
139         ncp_add_dword_hl(conn, queue_id);
140         ncp_add_dword_lh(conn, job_number);
141         error = ncp_request(connid, 23, conn);
142         return error;
143 }
144
145 int
146 ncp_get_queue_length(NWCONN_HANDLE connid, u_int32_t queue_id,
147         u_int32_t *queue_length)
148 {
149         int error;
150         DECLARE_RQ;
151
152         ncp_init_request_s(conn, 125);
153         ncp_add_dword_hl(conn, queue_id);
154
155         if ((error = ncp_request(connid, 23, conn)) != 0) 
156                 return error;
157         if (conn->rpsize < 12) {
158                 ncp_printf("ncp_reply_size %d < 12\n", conn->rpsize);
159                 return EINVAL;
160         }
161         if (ncp_reply_dword_hl(conn,0) != queue_id) {
162                 printf("Ouch! Server didn't reply with same queue id in ncp_get_queue_length!\n");
163                 return EINVAL;
164         }
165         *queue_length = ncp_reply_dword_lh(conn,8);
166         return error;
167 }
168
169 int 
170 ncp_get_queue_job_ids(NWCONN_HANDLE connid, u_int32_t queue_id,
171         u_int32_t queue_section, u_int32_t *length1, u_int32_t *length2,
172         u_int32_t ids[])
173 {
174         int error;
175         DECLARE_RQ;
176
177         ncp_init_request_s(conn,129);
178         ncp_add_dword_hl(conn, queue_id);
179         ncp_add_dword_lh(conn, queue_section);
180         
181         if ((error = ncp_request(connid, 23, conn)) != 0)
182                 return error;
183         if (conn->rpsize < 8) {
184                 ncp_printf("ncp_reply_size %d < 8\n", conn->rpsize);
185                 return EINVAL;
186         }
187         *length2 = ncp_reply_dword_lh(conn,4);
188         if (conn->rpsize < 8 + 4*(*length2)) {
189                 ncp_printf("ncp_reply_size %d < %d\n", conn->rpsize, 8+4*(*length2));
190                 return EINVAL;
191         }
192         if (ids) {
193                 int count = min(*length1, *length2)*sizeof(u_int32_t);
194                 int pos;
195
196                 for (pos=0; pos<count; pos+=sizeof(u_int32_t)) {
197                         *ids++ = ncp_reply_dword_lh(conn, 8+pos);
198                 }
199         }
200         *length1 = ncp_reply_dword_lh(conn,0);
201         return error;
202 }
203
204 int
205 ncp_get_queue_job_info(NWCONN_HANDLE connid, u_int32_t queue_id,
206         u_int32_t job_id, struct nw_queue_job_entry *jobdata)
207 {
208         int error;
209         DECLARE_RQ;
210
211         ncp_init_request_s(conn,122);
212         ncp_add_dword_hl(conn, queue_id);
213         ncp_add_dword_lh(conn, job_id);
214
215         if ((error = ncp_request(connid, 23, conn)) != 0)
216                 return error;
217
218         if (conn->rpsize < sizeof(struct nw_queue_job_entry)) {
219                 ncp_printf("ncp_reply_size %d < %zu\n", conn->rpsize,sizeof(struct nw_queue_job_entry));
220                 return EINVAL;
221         }    
222         memcpy(jobdata,ncp_reply_data(conn,0), sizeof(struct nw_queue_job_entry));
223         return error;
224 }