]> andersk Git - moira.git/blob - lib/mr_param.c
b267382f60d2b161932e57969d69ec525a4611d7
[moira.git] / lib / mr_param.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 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 "sms_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 sms_cont_send(op, hcon, arg)
32     OPERATION op;
33     HALF_CONNECTION hcon;
34     struct sms_params *arg;
35 {
36     op->result = OP_SUCCESS;
37     free(arg->sms_flattened);
38     arg->sms_flattened = NULL;
39     
40     return OP_COMPLETE;
41 }
42
43 sms_start_send(op, hcon, arg)
44     OPERATION op;
45     HALF_CONNECTION hcon;
46     register struct sms_params *arg;
47 {
48     int i, len;
49     unsigned int sms_size;
50     int *argl;
51     char *buf, *bp;
52         
53     /*
54      * This should probably be split into several routines.
55      * It could also probably be made more efficient (punting most
56      * of the argument marshalling stuff) by doing I/O directly
57      * from the strings.  Anyone for a scatter/gather sms_send_data?
58      *
59      * that would look a lot like the uio stuff in the kernel..  hmm.
60      */
61         
62     /*
63      * Marshall the entire data right now..
64      * We are sending the version number,
65      * total request size, request number, 
66      * argument count, and then each argument.
67      * At least for now, each argument is a string, which is
68      * sent as a count of bytes followed by the bytes
69      * (including the trailing '\0'), padded
70      * to a longword boundary.
71      */
72
73     sms_size = 4 * sizeof(long);
74
75     argl = (int *)malloc((unsigned)(sizeof(int) * arg->sms_argc));
76
77     /*
78      * For each argument, figure out how much space is needed.
79      */
80         
81     for (i = 0; i < arg->sms_argc; ++i) {
82         if (arg->sms_argl)
83             argl[i] = len = arg->sms_argl[i];
84         else
85             argl[i] = len = strlen(arg->sms_argv[i]) + 1;
86         sms_size += sizeof(long) + len;
87         /* Round up to next longword boundary.. */
88         sms_size = sizeof(long) * howmany(sms_size, sizeof(long));
89     }
90         
91     arg->sms_flattened = buf = malloc(sms_size);
92
93     bzero(arg->sms_flattened, sms_size);
94         
95     arg->sms_size = sms_size;
96         
97     /*
98      * This is gross.  Any better suggestions, anyone?
99      * It should work on the RT's, since malloc is guaranteed to
100      * return a pointer which is aligned correctly for any data.
101      */
102
103     ((long *)buf)[0] = htonl(sms_size);
104     ((long *)buf)[1] = htonl(arg->sms_version_no);
105     ((long *)buf)[2] = htonl(arg->sms_procno);
106     ((long *)buf)[3] = htonl(arg->sms_argc);
107
108     /*
109      * bp is a pointer into the point in the buffer to put
110      * the next argument.
111      */
112         
113     bp = (char *)(((long *)buf) + 4);
114         
115     for (i = 0; i<arg->sms_argc; ++i) {
116         len = argl[i];
117         *((long *)bp) = htonl(len);
118         bp += sizeof(long);
119         bcopy(arg->sms_argv[i], bp, len);
120         bp += sizeof(long) * howmany(len, sizeof(long));
121     }
122     op->fcn.cont = sms_cont_send;
123     arg->sms_size = sms_size;
124
125     free(argl);
126     
127     if (gdb_send_data(hcon, arg->sms_flattened, sms_size) == OP_COMPLETE)
128         return sms_cont_send(op, hcon, arg);
129     else return OP_RUNNING;
130 }       
131         
132 sms_cont_recv(op, hcon, argp)
133     OPERATION op;
134     HALF_CONNECTION hcon;
135     sms_params **argp;
136 {
137     int done = FALSE;
138     char *cp;
139     int *ip;
140     int i;
141     register sms_params *arg = *argp;
142                                                        
143     while (!done) {
144         switch (arg->sms_state) {
145         case S_RECV_START:
146             arg->sms_state = S_RECV_DATA;
147             if (gdb_receive_data(hcon, (caddr_t)&arg->sms_size,
148                                  sizeof(long)) == OP_COMPLETE)
149                 continue;
150             done = TRUE;
151             break;
152         case S_RECV_DATA:
153             fflush(stdout);
154             /* Should validate that length is reasonable */
155             arg->sms_size = ntohl(arg->sms_size);
156             if (arg->sms_size > 65536) {
157                 return OP_CANCELLED;
158             }
159             arg->sms_flattened = malloc(arg->sms_size);
160             arg->sms_state = S_DECODE_DATA;
161             bcopy((caddr_t)&arg->sms_size, arg->sms_flattened, sizeof(long));
162                         
163             if (gdb_receive_data(hcon,
164                                  arg->sms_flattened + sizeof(long),
165                                  arg->sms_size - sizeof(long))
166                 == OP_COMPLETE)
167                 continue;
168             done = TRUE;
169             break;
170         case S_DECODE_DATA:
171             cp = arg->sms_flattened;
172             ip = (int *) cp;
173             /* we already got the overall length.. */
174             for(i=1; i <4; i++) ip[i] = ntohl(ip[i]);
175             arg->sms_version_no = ip[1];
176             if (arg->sms_version_no != SMS_VERSION_1 &&
177                 arg->sms_version_no != SMS_VERSION_2)
178                 arg->sms_status = SMS_VERSION_MISMATCH;
179             else arg->sms_status = ip[2];
180             arg->sms_argc = ip[3];
181             cp += 4 * sizeof(int);
182             arg->sms_argv=(char **)malloc(arg->sms_argc *sizeof(char **));
183             arg->sms_argl=(int *)malloc(arg->sms_argc *sizeof(int *));
184                         
185             for (i = 0; i<arg->sms_argc; ++i) {
186                 u_short nlen = ntohl(* (int *) cp);
187                 cp += sizeof (long);
188                 if (cp + nlen > arg->sms_flattened + arg->sms_size) {
189                     free(arg->sms_flattened);
190                     arg->sms_flattened = NULL;
191                     return OP_CANCELLED;
192                 }                   
193                 arg->sms_argv[i] = (char *)malloc(nlen);
194                 bcopy(cp, arg->sms_argv[i], nlen);
195                 arg->sms_argl[i]=nlen;
196                 cp += sizeof(long) * howmany(nlen, sizeof(long));
197             }
198             free(arg->sms_flattened);
199             arg->sms_flattened = NULL;
200             return OP_COMPLETE;
201         }
202     }
203     return OP_RUNNING;
204 }
205                         
206
207 sms_start_recv(op, hcon, argp)
208     OPERATION op;
209     HALF_CONNECTION hcon;
210     struct sms_params **argp;
211 {
212     register sms_params *arg = *argp;
213     if (!arg) {
214         *argp = arg = (sms_params *)malloc(sizeof(sms_params));
215         arg->sms_argl = NULL;
216         arg->sms_argv = NULL;
217         arg->sms_flattened = NULL;
218     }
219     arg->sms_state = S_RECV_START;
220     op->fcn.cont = sms_cont_recv;
221     return sms_cont_recv(op, hcon, argp);
222 }
223
224 sms_destroy_reply(reply)
225     sms_params *reply;
226 {
227     int i;
228     if (reply) {
229         if (reply->sms_argl)
230             free(reply->sms_argl);
231         reply->sms_argl = NULL;
232         if (reply->sms_flattened)
233             free(reply->sms_flattened);
234         reply->sms_flattened = NULL;
235         if (reply->sms_argv) {
236             for (i=0; i<reply->sms_argc; i++) {
237                 if (reply->sms_argv[i])
238                     free (reply->sms_argv[i]);
239                 reply->sms_argv[i] = NULL;
240             }
241             free(reply->sms_argv);
242         }
243         reply->sms_argv = NULL;
244         free(reply);
245     }
246 }
247
248 /*
249  * Local Variables:
250  * mode: c
251  * c-indent-level: 4
252  * c-continued-statement-offset: 4
253  * c-brace-offset: -4
254  * c-argdecl-indent: 4
255  * c-label-offset: -4
256  * End:
257  */
This page took 0.354285 seconds and 3 git commands to generate.