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"
24 * GDB operations to send and recieve RPC requests and replies.
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.
34 mr_cont_send(op, hcon, arg)
37 struct mr_params *arg;
39 op->result = OP_SUCCESS;
40 free(arg->mr_flattened);
41 arg->mr_flattened = NULL;
46 mr_start_send(op, hcon, arg)
49 register struct mr_params *arg;
57 * This should probably be split into several routines.
58 * It could also probably be made more efficient (punting most
59 * of the argument marshalling stuff) by doing I/O directly
60 * from the strings. Anyone for a scatter/gather mr_send_data?
62 * that would look a lot like the uio stuff in the kernel.. hmm.
66 * Marshall the entire data right now..
67 * We are sending the version number,
68 * total request size, request number,
69 * argument count, and then each argument.
70 * At least for now, each argument is a string, which is
71 * sent as a count of bytes followed by the bytes
72 * (including the trailing '\0'), padded
73 * to a 32-bit boundary.
76 mr_size = 4 * sizeof(int32);
78 argl = (int *)malloc((unsigned)(sizeof(int) * arg->mr_argc));
81 * For each argument, figure out how much space is needed.
84 for (i = 0; i < arg->mr_argc; ++i) {
86 argl[i] = len = arg->mr_argl[i];
88 argl[i] = len = strlen(arg->mr_argv[i]) + 1;
89 mr_size += sizeof(int32) + len;
90 /* Round up to next 32-bit boundary.. */
91 mr_size = sizeof(int32) * howmany(mr_size, sizeof(int32));
94 arg->mr_flattened = buf = malloc(mr_size);
96 memset(arg->mr_flattened, 0, mr_size);
98 arg->mr_size = mr_size;
100 ((int32 *)buf)[0] = htonl(mr_size);
101 ((int32 *)buf)[1] = htonl(arg->mr_version_no);
102 ((int32 *)buf)[2] = htonl(arg->mr_procno);
103 ((int32 *)buf)[3] = htonl(arg->mr_argc);
106 * bp is a pointer into the point in the buffer to put
110 bp = (char *)(((int32 *)buf) + 4);
112 for (i = 0; i<arg->mr_argc; ++i) {
114 *((int32 *)bp) = htonl(len);
116 memcpy(bp, arg->mr_argv[i], len);
117 bp += sizeof(int32) * howmany(len, sizeof(int32));
119 op->fcn.cont = mr_cont_send;
120 arg->mr_size = mr_size;
124 if (gdb_send_data(hcon, arg->mr_flattened, mr_size) == OP_COMPLETE)
125 return mr_cont_send(op, hcon, arg);
126 else return OP_RUNNING;
130 mr_cont_recv(op, hcon, argp)
132 HALF_CONNECTION hcon;
139 register mr_params *arg = *argp;
142 switch (arg->mr_state) {
144 arg->mr_state = S_RECV_DATA;
145 if (gdb_receive_data(hcon, (caddr_t)&arg->mr_size,
146 sizeof(int32)) == OP_COMPLETE)
152 /* Should validate that length is reasonable */
153 arg->mr_size = ntohl(arg->mr_size);
154 if (arg->mr_size > 65536) {
157 arg->mr_flattened = malloc(arg->mr_size);
158 arg->mr_state = S_DECODE_DATA;
159 memcpy(arg->mr_flattened, (caddr_t)&arg->mr_size, sizeof(int32));
161 if (gdb_receive_data(hcon,
162 arg->mr_flattened + sizeof(int32),
163 arg->mr_size - sizeof(int32))
169 cp = arg->mr_flattened;
171 /* we already got the overall length.. */
172 for(i=1; i <4; i++) ip[i] = ntohl(ip[i]);
173 arg->mr_version_no = ip[1];
174 if (arg->mr_version_no != MR_VERSION_1 &&
175 arg->mr_version_no != MR_VERSION_2)
176 arg->mr_status = MR_VERSION_MISMATCH;
177 else arg->mr_status = ip[2];
178 arg->mr_argc = ip[3];
179 cp += 4 * sizeof(int);
180 arg->mr_argv=(char **)malloc(arg->mr_argc *sizeof(char **));
181 arg->mr_argl=(int *)malloc(arg->mr_argc *sizeof(int *));
183 for (i = 0; i<arg->mr_argc; ++i) {
184 u_short nlen = ntohl(* (int *) cp);
185 cp += sizeof (int32);
186 if (cp + nlen > arg->mr_flattened + arg->mr_size) {
187 free(arg->mr_flattened);
188 arg->mr_flattened = NULL;
191 arg->mr_argv[i] = (char *)malloc(nlen);
192 memcpy(arg->mr_argv[i], cp, nlen);
193 arg->mr_argl[i]=nlen;
194 cp += sizeof(int32) * howmany(nlen, sizeof(int32));
196 free(arg->mr_flattened);
197 arg->mr_flattened = NULL;
205 mr_start_recv(op, hcon, argp)
207 HALF_CONNECTION hcon;
208 struct mr_params **argp;
210 register mr_params *arg = *argp;
212 *argp = arg = (mr_params *)malloc(sizeof(mr_params));
215 arg->mr_flattened = NULL;
217 arg->mr_state = S_RECV_START;
218 op->fcn.cont = mr_cont_recv;
219 return mr_cont_recv(op, hcon, argp);
222 mr_destroy_reply(reply)
228 free(reply->mr_argl);
229 reply->mr_argl = NULL;
230 if (reply->mr_flattened)
231 free(reply->mr_flattened);
232 reply->mr_flattened = NULL;
233 if (reply->mr_argv) {
234 for (i=0; i<reply->mr_argc; i++) {
235 if (reply->mr_argv[i])
236 free (reply->mr_argv[i]);
237 reply->mr_argv[i] = NULL;
239 free(reply->mr_argv);
241 reply->mr_argv = NULL;