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.
31 mr_cont_send(op, hcon, arg)
34 struct mr_params *arg;
36 op->result = OP_SUCCESS;
37 free(arg->mr_flattened);
38 arg->mr_flattened = NULL;
43 mr_start_send(op, hcon, arg)
46 register struct mr_params *arg;
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?
59 * that would look a lot like the uio stuff in the kernel.. hmm.
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.
73 mr_size = 4 * sizeof(long);
75 argl = (int *)malloc((unsigned)(sizeof(int) * arg->mr_argc));
78 * For each argument, figure out how much space is needed.
81 for (i = 0; i < arg->mr_argc; ++i) {
83 argl[i] = len = arg->mr_argl[i];
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));
91 arg->mr_flattened = buf = malloc(mr_size);
93 bzero(arg->mr_flattened, mr_size);
95 arg->mr_size = mr_size;
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.
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);
109 * bp is a pointer into the point in the buffer to put
113 bp = (char *)(((long *)buf) + 4);
115 for (i = 0; i<arg->mr_argc; ++i) {
117 *((long *)bp) = htonl(len);
119 bcopy(arg->mr_argv[i], bp, len);
120 bp += sizeof(long) * howmany(len, sizeof(long));
122 op->fcn.cont = mr_cont_send;
123 arg->mr_size = mr_size;
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;
132 mr_cont_recv(op, hcon, argp)
134 HALF_CONNECTION hcon;
141 register mr_params *arg = *argp;
144 switch (arg->mr_state) {
146 arg->mr_state = S_RECV_DATA;
147 if (gdb_receive_data(hcon, (caddr_t)&arg->mr_size,
148 sizeof(long)) == OP_COMPLETE)
154 /* Should validate that length is reasonable */
155 arg->mr_size = ntohl(arg->mr_size);
156 if (arg->mr_size > 65536) {
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));
163 if (gdb_receive_data(hcon,
164 arg->mr_flattened + sizeof(long),
165 arg->mr_size - sizeof(long))
171 cp = arg->mr_flattened;
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 *));
185 for (i = 0; i<arg->mr_argc; ++i) {
186 u_short nlen = ntohl(* (int *) cp);
188 if (cp + nlen > arg->mr_flattened + arg->mr_size) {
189 free(arg->mr_flattened);
190 arg->mr_flattened = NULL;
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));
198 free(arg->mr_flattened);
199 arg->mr_flattened = NULL;
207 mr_start_recv(op, hcon, argp)
209 HALF_CONNECTION hcon;
210 struct mr_params **argp;
212 register mr_params *arg = *argp;
214 *argp = arg = (mr_params *)malloc(sizeof(mr_params));
217 arg->mr_flattened = NULL;
219 arg->mr_state = S_RECV_START;
220 op->fcn.cont = mr_cont_recv;
221 return mr_cont_recv(op, hcon, argp);
224 mr_destroy_reply(reply)
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;
241 free(reply->mr_argv);
243 reply->mr_argv = NULL;