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