6 * Copyright (C) 1987, 1990 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
13 static char *rcsid_sms_param_c = "$Header$";
16 #include <mit-copyright.h>
17 #include <sys/types.h>
18 #include <netinet/in.h>
19 #include "mr_private.h"
22 * GDB operations to send and recieve RPC requests and replies.
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.
32 mr_cont_send(op, hcon, arg)
35 struct mr_params *arg;
37 op->result = OP_SUCCESS;
38 free(arg->mr_flattened);
39 arg->mr_flattened = NULL;
44 mr_start_send(op, hcon, arg)
47 register struct mr_params *arg;
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?
60 * that would look a lot like the uio stuff in the kernel.. hmm.
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.
74 mr_size = 4 * sizeof(long);
76 argl = (int *)malloc((unsigned)(sizeof(int) * arg->mr_argc));
79 * For each argument, figure out how much space is needed.
82 for (i = 0; i < arg->mr_argc; ++i) {
84 argl[i] = len = arg->mr_argl[i];
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));
92 arg->mr_flattened = buf = malloc(mr_size);
94 bzero(arg->mr_flattened, mr_size);
96 arg->mr_size = mr_size;
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.
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);
110 * bp is a pointer into the point in the buffer to put
114 bp = (char *)(((long *)buf) + 4);
116 for (i = 0; i<arg->mr_argc; ++i) {
118 *((long *)bp) = htonl(len);
120 bcopy(arg->mr_argv[i], bp, len);
121 bp += sizeof(long) * howmany(len, sizeof(long));
123 op->fcn.cont = mr_cont_send;
124 arg->mr_size = mr_size;
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;
134 mr_cont_recv(op, hcon, argp)
136 HALF_CONNECTION hcon;
143 register mr_params *arg = *argp;
146 switch (arg->mr_state) {
148 arg->mr_state = S_RECV_DATA;
149 if (gdb_receive_data(hcon, (caddr_t)&arg->mr_size,
150 sizeof(long)) == OP_COMPLETE)
156 /* Should validate that length is reasonable */
157 arg->mr_size = ntohl(arg->mr_size);
158 if (arg->mr_size > 65536) {
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));
165 if (gdb_receive_data(hcon,
166 arg->mr_flattened + sizeof(long),
167 arg->mr_size - sizeof(long))
173 cp = arg->mr_flattened;
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 *));
187 for (i = 0; i<arg->mr_argc; ++i) {
188 u_short nlen = ntohl(* (int *) cp);
190 if (cp + nlen > arg->mr_flattened + arg->mr_size) {
191 free(arg->mr_flattened);
192 arg->mr_flattened = NULL;
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));
200 free(arg->mr_flattened);
201 arg->mr_flattened = NULL;
209 mr_start_recv(op, hcon, argp)
211 HALF_CONNECTION hcon;
212 struct mr_params **argp;
214 register mr_params *arg = *argp;
216 *argp = arg = (mr_params *)malloc(sizeof(mr_params));
219 arg->mr_flattened = NULL;
221 arg->mr_state = S_RECV_START;
222 op->fcn.cont = mr_cont_recv;
223 return mr_cont_recv(op, hcon, argp);
226 mr_destroy_reply(reply)
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;
243 free(reply->mr_argv);
245 reply->mr_argv = NULL;