]> andersk Git - moira.git/blob - lib/mr_param.c
Fix error code for the error we translate to `duplicate ID'. Should
[moira.git] / lib / mr_param.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987, 1990 by the Massachusetts Institute of Technology
7  *      For copying and distribution information, please see the file
8  *      <mit-copyright.h>.
9  *
10  */
11
12 #ifndef lint
13 static char *rcsid_sms_param_c = "$Header$";
14 #endif
15
16 #include <mit-copyright.h>
17 #include <sys/types.h>
18 #include <netinet/in.h>
19 #include "mr_private.h"
20 #include <string.h>
21 #include <stdlib.h>
22
23 /*
24  * GDB operations to send and recieve RPC requests and replies.
25  */
26
27 /*
28  * This doesn't get called until after the actual buffered write completes.
29  * In a non-preflattening version of this, this would then queue the
30  * write of the next bunch of data.
31  */
32
33 /*ARGSUSED*/
34 mr_cont_send(op, hcon, arg)
35     OPERATION op;
36     HALF_CONNECTION hcon;
37     struct mr_params *arg;
38 {
39     op->result = OP_SUCCESS;
40     free(arg->mr_flattened);
41     arg->mr_flattened = NULL;
42     
43     return OP_COMPLETE;
44 }
45
46 mr_start_send(op, hcon, arg)
47     OPERATION op;
48     HALF_CONNECTION hcon;
49     register struct mr_params *arg;
50 {
51     int i, len;
52     unsigned int mr_size;
53     int *argl;
54     char *buf, *bp;
55         
56     /*
57      * This should probably be split into several routines.
58      * It could also probably be made more efficient (punting most
59      * of the argument marshalling stuff) by doing I/O directly
60      * from the strings.  Anyone for a scatter/gather mr_send_data?
61      *
62      * that would look a lot like the uio stuff in the kernel..  hmm.
63      */
64         
65     /*
66      * Marshall the entire data right now..
67      * We are sending the version number,
68      * total request size, request number, 
69      * argument count, and then each argument.
70      * At least for now, each argument is a string, which is
71      * sent as a count of bytes followed by the bytes
72      * (including the trailing '\0'), padded
73      * to a 32-bit boundary.
74      */
75
76     mr_size = 4 * sizeof(int32);
77
78     argl = (int *)malloc((unsigned)(sizeof(int) * arg->mr_argc));
79
80     /*
81      * For each argument, figure out how much space is needed.
82      */
83         
84     for (i = 0; i < arg->mr_argc; ++i) {
85         if (arg->mr_argl)
86             argl[i] = len = arg->mr_argl[i];
87         else
88             argl[i] = len = strlen(arg->mr_argv[i]) + 1;
89         mr_size += sizeof(int32) + len;
90         /* Round up to next 32-bit boundary.. */
91         mr_size = sizeof(int32) * howmany(mr_size, sizeof(int32));
92     }
93         
94     arg->mr_flattened = buf = malloc(mr_size);
95
96     memset(arg->mr_flattened, 0, mr_size);
97         
98     arg->mr_size = mr_size;
99         
100     ((int32 *)buf)[0] = htonl(mr_size);
101     ((int32 *)buf)[1] = htonl(arg->mr_version_no);
102     ((int32 *)buf)[2] = htonl(arg->mr_procno);
103     ((int32 *)buf)[3] = htonl(arg->mr_argc);
104
105     /*
106      * bp is a pointer into the point in the buffer to put
107      * the next argument.
108      */
109         
110     bp = (char *)(((int32 *)buf) + 4);
111         
112     for (i = 0; i<arg->mr_argc; ++i) {
113         len = argl[i];
114         *((int32 *)bp) = htonl(len);
115         bp += sizeof(int32);
116         memcpy(bp, arg->mr_argv[i], len);
117         bp += sizeof(int32) * howmany(len, sizeof(int32));
118     }
119     op->fcn.cont = mr_cont_send;
120     arg->mr_size = mr_size;
121
122     free(argl);
123     
124     if (gdb_send_data(hcon, arg->mr_flattened, mr_size) == OP_COMPLETE)
125         return mr_cont_send(op, hcon, arg);
126     else return OP_RUNNING;
127 }       
128         
129 /*ARGSUSED*/
130 mr_cont_recv(op, hcon, argp)
131     OPERATION op;
132     HALF_CONNECTION hcon;
133     mr_params **argp;
134 {
135     int done = FALSE;
136     char *cp;
137     int *ip;
138     int i;
139     register mr_params *arg = *argp;
140                                                        
141     while (!done) {
142         switch (arg->mr_state) {
143         case S_RECV_START:
144             arg->mr_state = S_RECV_DATA;
145             if (gdb_receive_data(hcon, (caddr_t)&arg->mr_size,
146                                  sizeof(int32)) == OP_COMPLETE)
147                 continue;
148             done = TRUE;
149             break;
150         case S_RECV_DATA:
151             fflush(stdout);
152             /* Should validate that length is reasonable */
153             arg->mr_size = ntohl(arg->mr_size);
154             if (arg->mr_size > 65536) {
155                 return OP_CANCELLED;
156             }
157             arg->mr_flattened = malloc(arg->mr_size);
158             arg->mr_state = S_DECODE_DATA;
159             memcpy(arg->mr_flattened, (caddr_t)&arg->mr_size, sizeof(int32));
160                         
161             if (gdb_receive_data(hcon,
162                                  arg->mr_flattened + sizeof(int32),
163                                  arg->mr_size - sizeof(int32))
164                 == OP_COMPLETE)
165                 continue;
166             done = TRUE;
167             break;
168         case S_DECODE_DATA:
169             cp = arg->mr_flattened;
170             ip = (int *) cp;
171             /* we already got the overall length.. */
172             for(i=1; i <4; i++) ip[i] = ntohl(ip[i]);
173             arg->mr_version_no = ip[1];
174             if (arg->mr_version_no != MR_VERSION_1 &&
175                 arg->mr_version_no != MR_VERSION_2)
176                 arg->mr_status = MR_VERSION_MISMATCH;
177             else arg->mr_status = ip[2];
178             arg->mr_argc = ip[3];
179             cp += 4 * sizeof(int);
180             arg->mr_argv=(char **)malloc(arg->mr_argc *sizeof(char **));
181             arg->mr_argl=(int *)malloc(arg->mr_argc *sizeof(int *));
182                         
183             for (i = 0; i<arg->mr_argc; ++i) {
184                 u_short nlen = ntohl(* (int *) cp);
185                 cp += sizeof (int32);
186                 if (cp + nlen > arg->mr_flattened + arg->mr_size) {
187                     free(arg->mr_flattened);
188                     arg->mr_flattened = NULL;
189                     return OP_CANCELLED;
190                 }                   
191                 arg->mr_argv[i] = (char *)malloc(nlen);
192                 memcpy(arg->mr_argv[i], cp, nlen);
193                 arg->mr_argl[i]=nlen;
194                 cp += sizeof(int32) * howmany(nlen, sizeof(int32));
195             }
196             free(arg->mr_flattened);
197             arg->mr_flattened = NULL;
198             return OP_COMPLETE;
199         }
200     }
201     return OP_RUNNING;
202 }
203                         
204
205 mr_start_recv(op, hcon, argp)
206     OPERATION op;
207     HALF_CONNECTION hcon;
208     struct mr_params **argp;
209 {
210     register mr_params *arg = *argp;
211     if (!arg) {
212         *argp = arg = (mr_params *)malloc(sizeof(mr_params));
213         arg->mr_argl = NULL;
214         arg->mr_argv = NULL;
215         arg->mr_flattened = NULL;
216     }
217     arg->mr_state = S_RECV_START;
218     op->fcn.cont = mr_cont_recv;
219     return mr_cont_recv(op, hcon, argp);
220 }
221
222 mr_destroy_reply(reply)
223     mr_params *reply;
224 {
225     int i;
226     if (reply) {
227         if (reply->mr_argl)
228             free(reply->mr_argl);
229         reply->mr_argl = NULL;
230         if (reply->mr_flattened)
231             free(reply->mr_flattened);
232         reply->mr_flattened = NULL;
233         if (reply->mr_argv) {
234             for (i=0; i<reply->mr_argc; i++) {
235                 if (reply->mr_argv[i])
236                     free (reply->mr_argv[i]);
237                 reply->mr_argv[i] = NULL;
238             }
239             free(reply->mr_argv);
240         }
241         reply->mr_argv = NULL;
242         free(reply);
243     }
244 }
This page took 0.056869 seconds and 5 git commands to generate.