6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
9 * Revision 1.1 1987-06-16 17:48:21 wesommer
12 * Revision 1.4 87/06/04 01:32:18 wesommer
15 * Revision 1.3 87/06/01 03:33:54 wesommer
16 * Added destroy_reply.
18 * Revision 1.2 87/05/31 22:03:37 wesommer
19 * Fixed numerous bugs; still shaky.
24 static char *rcsid_sms_param_c = "$Header$";
27 #include "sms_private.h"
30 * GDB operations to send and recieve RPC requests and replies.
34 * This doesn't get called until after the actual buffered write completes.
35 * In a non-preflattening version of this, this would then queue the
36 * write of the next bunch of data.
39 sms_cont_send(op, hcon, arg)
42 struct sms_params *arg;
44 op->result = OP_SUCCESS;
45 free(arg->sms_flattened);
49 sms_start_send(op, hcon, arg)
52 register struct sms_params *arg;
55 unsigned int sms_size;
60 * This should probably be split into several routines.
61 * It could also probably be made more efficient (punting most
62 * of the argument marshalling stuff) by doing I/O directly
63 * from the strings. Anyone for a scatter/gather sms_send_data?
65 * that would look a lot like the uio stuff in the kernel.. hmm.
69 * Marshall the entire data right now..
70 * We are sending the version number,
71 * total request size, request number,
72 * argument count, and then each argument.
73 * At least for now, each argument is a string, which is
74 * sent as a count of bytes followed by the bytes
75 * (including the trailing '\0'), padded
76 * to a longword boundary.
79 sms_size = 4 * sizeof(long);
81 argl = (int *)malloc((unsigned)(sizeof(int) * arg->sms_argc));
84 * For each argument, figure out how much space is needed.
87 for (i = 0; i < arg->sms_argc; ++i) {
89 argl[i] = len = arg->sms_argl[i];
91 argl[i] = len = strlen(arg->sms_argv[i]) + 1;
92 sms_size += sizeof(long) + len;
93 /* Round up to next longword boundary.. */
94 sms_size = sizeof(long) * howmany(sms_size, sizeof(long));
97 arg->sms_flattened = buf = malloc(sms_size);
99 bzero(arg->sms_flattened, sms_size);
101 arg->sms_size = sms_size;
104 * This is gross. Any better suggestions, anyone?
105 * It should work on the RT's, since malloc is guaranteed to
106 * return a pointer which is aligned correctly for any data.
109 ((long *)buf)[0] = htonl(sms_size);
110 ((long *)buf)[1] = htonl((u_int)SMS_VERSION_1);
111 ((long *)buf)[2] = htonl(arg->sms_procno);
112 ((long *)buf)[3] = htonl(arg->sms_argc);
115 * bp is a pointer into the point in the buffer to put
119 bp = (char *)(((long *)buf) + 4);
121 for (i = 0; i<arg->sms_argc; ++i) {
123 *((long *)bp) = htonl(len);
125 bcopy(arg->sms_argv[i], bp, len);
126 bp += sizeof(long) * howmany(len, sizeof(long));
128 op->fcn.cont = sms_cont_send;
129 arg->sms_size = sms_size;
133 if (gdb_send_data(hcon, arg->sms_flattened, sms_size) == OP_COMPLETE)
134 return sms_cont_send(op, hcon, arg);
135 else return OP_RUNNING;
138 sms_cont_recv(op, hcon, argp)
140 HALF_CONNECTION hcon;
147 register sms_params *arg = *argp;
150 switch (arg->sms_state) {
152 arg->sms_state = S_RECV_DATA;
153 if (gdb_receive_data(hcon, (caddr_t)&arg->sms_size,
154 sizeof(long)) == OP_COMPLETE)
160 /* Should validate that length is reasonable */
161 arg->sms_size = ntohl(arg->sms_size);
162 arg->sms_flattened = malloc(arg->sms_size);
163 arg->sms_state = S_DECODE_DATA;
164 bcopy((caddr_t)&arg->sms_size, arg->sms_flattened, sizeof(long));
166 if (gdb_receive_data(hcon,
167 arg->sms_flattened + sizeof(long),
168 arg->sms_size - sizeof(long))
174 cp = arg->sms_flattened;
176 /* we already got the overall length.. */
177 for(i=1; i <4; i++) ip[i] = ntohl(ip[i]);
178 if (ip[1] != SMS_VERSION_1)
179 arg->sms_status = SMS_VERSION_MISMATCH;
180 else arg->sms_status = ip[2];
181 arg->sms_argc = ip[3];
182 cp += 4 * sizeof(int);
183 arg->sms_argv=(char **)malloc(arg->sms_argc *sizeof(char **));
184 arg->sms_argl=(int *)malloc(arg->sms_argc *sizeof(int *));
186 for (i = 0; i<arg->sms_argc; ++i) {
187 int nlen = ntohl(* (int *) cp);
189 arg->sms_argv[i] = (char *)malloc(nlen);
190 bcopy(cp, arg->sms_argv[i], nlen);
191 arg->sms_argl[i]=nlen;
192 cp += sizeof(long) * howmany(nlen, sizeof(long));
194 free(arg->sms_flattened);
195 arg->sms_flattened = NULL;
203 sms_start_recv(op, hcon, argp)
205 HALF_CONNECTION hcon;
206 struct sms_params **argp;
208 register sms_params *arg = *argp;
210 *argp = arg = (sms_params *)malloc(sizeof(sms_params));
211 arg->sms_argl = NULL;
212 arg->sms_argv = NULL;
213 arg->sms_flattened = NULL;
215 arg->sms_state = S_RECV_START;
216 op->fcn.cont = sms_cont_recv;
217 return sms_cont_recv(op, hcon, argp);
220 sms_destroy_reply(reply)
226 free(reply->sms_argl);
227 reply->sms_argl = NULL;
228 if (reply->sms_flattened)
229 free(reply->sms_flattened);
230 reply->sms_flattened = NULL;
231 if (reply->sms_argv) {
232 for (i=0; i<reply->sms_argc; i++) {
233 if (reply->sms_argv[i])
234 free (reply->sms_argv[i]);
235 reply->sms_argv[i] = NULL;
237 free(reply->sms_argv);
239 reply->sms_argv = NULL;
248 * c-continued-statement-offset: 4
250 * c-argdecl-indent: 4