]> andersk Git - moira.git/blob - lib/mr_param.c
separate rule for profiled library
[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 mr_cont_send(op, hcon, arg)
32     OPERATION op;
33     HALF_CONNECTION hcon;
34     struct mr_params *arg;
35 {
36     op->result = OP_SUCCESS;
37     free(arg->mr_flattened);
38     arg->mr_flattened = NULL;
39     
40     return OP_COMPLETE;
41 }
42
43 mr_start_send(op, hcon, arg)
44     OPERATION op;
45     HALF_CONNECTION hcon;
46     register struct mr_params *arg;
47 {
48     int i, len;
49     unsigned int mr_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 mr_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     mr_size = 4 * sizeof(long);
74
75     argl = (int *)malloc((unsigned)(sizeof(int) * arg->mr_argc));
76
77     /*
78      * For each argument, figure out how much space is needed.
79      */
80         
81     for (i = 0; i < arg->mr_argc; ++i) {
82         if (arg->mr_argl)
83             argl[i] = len = arg->mr_argl[i];
84         else
85             argl[i] = len = strlen(arg->mr_argv[i]) + 1;
86         mr_size += sizeof(long) + len;
87         /* Round up to next longword boundary.. */
88         mr_size = sizeof(long) * howmany(mr_size, sizeof(long));
89     }
90         
91     arg->mr_flattened = buf = malloc(mr_size);
92
93     bzero(arg->mr_flattened, mr_size);
94         
95     arg->mr_size = mr_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(mr_size);
104     ((long *)buf)[1] = htonl(arg->mr_version_no);
105     ((long *)buf)[2] = htonl(arg->mr_procno);
106     ((long *)buf)[3] = htonl(arg->mr_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->mr_argc; ++i) {
116         len = argl[i];
117         *((long *)bp) = htonl(len);
118         bp += sizeof(long);
119         bcopy(arg->mr_argv[i], bp, len);
120         bp += sizeof(long) * howmany(len, sizeof(long));
121     }
122     op->fcn.cont = mr_cont_send;
123     arg->mr_size = mr_size;
124
125     free(argl);
126     
127     if (gdb_send_data(hcon, arg->mr_flattened, mr_size) == OP_COMPLETE)
128         return mr_cont_send(op, hcon, arg);
129     else return OP_RUNNING;
130 }       
131         
132 mr_cont_recv(op, hcon, argp)
133     OPERATION op;
134     HALF_CONNECTION hcon;
135     mr_params **argp;
136 {
137     int done = FALSE;
138     char *cp;
139     int *ip;
140     int i;
141     register mr_params *arg = *argp;
142                                                        
143     while (!done) {
144         switch (arg->mr_state) {
145         case S_RECV_START:
146             arg->mr_state = S_RECV_DATA;
147             if (gdb_receive_data(hcon, (caddr_t)&arg->mr_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->mr_size = ntohl(arg->mr_size);
156             if (arg->mr_size > 65536) {
157                 return OP_CANCELLED;
158             }
159             arg->mr_flattened = malloc(arg->mr_size);
160             arg->mr_state = S_DECODE_DATA;
161             bcopy((caddr_t)&arg->mr_size, arg->mr_flattened, sizeof(long));
162                         
163             if (gdb_receive_data(hcon,
164                                  arg->mr_flattened + sizeof(long),
165                                  arg->mr_size - sizeof(long))
166                 == OP_COMPLETE)
167                 continue;
168             done = TRUE;
169             break;
170         case S_DECODE_DATA:
171             cp = arg->mr_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->mr_version_no = ip[1];
176             if (arg->mr_version_no != MR_VERSION_1 &&
177                 arg->mr_version_no != MR_VERSION_2)
178                 arg->mr_status = MR_VERSION_MISMATCH;
179             else arg->mr_status = ip[2];
180             arg->mr_argc = ip[3];
181             cp += 4 * sizeof(int);
182             arg->mr_argv=(char **)malloc(arg->mr_argc *sizeof(char **));
183             arg->mr_argl=(int *)malloc(arg->mr_argc *sizeof(int *));
184                         
185             for (i = 0; i<arg->mr_argc; ++i) {
186                 u_short nlen = ntohl(* (int *) cp);
187                 cp += sizeof (long);
188                 if (cp + nlen > arg->mr_flattened + arg->mr_size) {
189                     free(arg->mr_flattened);
190                     arg->mr_flattened = NULL;
191                     return OP_CANCELLED;
192                 }                   
193                 arg->mr_argv[i] = (char *)malloc(nlen);
194                 bcopy(cp, arg->mr_argv[i], nlen);
195                 arg->mr_argl[i]=nlen;
196                 cp += sizeof(long) * howmany(nlen, sizeof(long));
197             }
198             free(arg->mr_flattened);
199             arg->mr_flattened = NULL;
200             return OP_COMPLETE;
201         }
202     }
203     return OP_RUNNING;
204 }
205                         
206
207 mr_start_recv(op, hcon, argp)
208     OPERATION op;
209     HALF_CONNECTION hcon;
210     struct mr_params **argp;
211 {
212     register mr_params *arg = *argp;
213     if (!arg) {
214         *argp = arg = (mr_params *)malloc(sizeof(mr_params));
215         arg->mr_argl = NULL;
216         arg->mr_argv = NULL;
217         arg->mr_flattened = NULL;
218     }
219     arg->mr_state = S_RECV_START;
220     op->fcn.cont = mr_cont_recv;
221     return mr_cont_recv(op, hcon, argp);
222 }
223
224 mr_destroy_reply(reply)
225     mr_params *reply;
226 {
227     int i;
228     if (reply) {
229         if (reply->mr_argl)
230             free(reply->mr_argl);
231         reply->mr_argl = NULL;
232         if (reply->mr_flattened)
233             free(reply->mr_flattened);
234         reply->mr_flattened = NULL;
235         if (reply->mr_argv) {
236             for (i=0; i<reply->mr_argc; i++) {
237                 if (reply->mr_argv[i])
238                     free (reply->mr_argv[i]);
239                 reply->mr_argv[i] = NULL;
240             }
241             free(reply->mr_argv);
242         }
243         reply->mr_argv = NULL;
244         free(reply);
245     }
246 }
This page took 0.456391 seconds and 5 git commands to generate.