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