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