6 * Copyright (C) 1987 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 "sms_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 sms_cont_send(op, hcon, arg)
34 struct sms_params *arg;
36 op->result = OP_SUCCESS;
37 free(arg->sms_flattened);
38 arg->sms_flattened = NULL;
43 sms_start_send(op, hcon, arg)
46 register struct sms_params *arg;
49 unsigned int sms_size;
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 sms_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 sms_size = 4 * sizeof(long);
75 argl = (int *)malloc((unsigned)(sizeof(int) * arg->sms_argc));
78 * For each argument, figure out how much space is needed.
81 for (i = 0; i < arg->sms_argc; ++i) {
83 argl[i] = len = arg->sms_argl[i];
85 argl[i] = len = strlen(arg->sms_argv[i]) + 1;
86 sms_size += sizeof(long) + len;
87 /* Round up to next longword boundary.. */
88 sms_size = sizeof(long) * howmany(sms_size, sizeof(long));
91 arg->sms_flattened = buf = malloc(sms_size);
93 bzero(arg->sms_flattened, sms_size);
95 arg->sms_size = sms_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(sms_size);
104 ((long *)buf)[1] = htonl(arg->sms_version_no);
105 ((long *)buf)[2] = htonl(arg->sms_procno);
106 ((long *)buf)[3] = htonl(arg->sms_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->sms_argc; ++i) {
117 *((long *)bp) = htonl(len);
119 bcopy(arg->sms_argv[i], bp, len);
120 bp += sizeof(long) * howmany(len, sizeof(long));
122 op->fcn.cont = sms_cont_send;
123 arg->sms_size = sms_size;
127 if (gdb_send_data(hcon, arg->sms_flattened, sms_size) == OP_COMPLETE)
128 return sms_cont_send(op, hcon, arg);
129 else return OP_RUNNING;
132 sms_cont_recv(op, hcon, argp)
134 HALF_CONNECTION hcon;
141 register sms_params *arg = *argp;
144 switch (arg->sms_state) {
146 arg->sms_state = S_RECV_DATA;
147 if (gdb_receive_data(hcon, (caddr_t)&arg->sms_size,
148 sizeof(long)) == OP_COMPLETE)
154 /* Should validate that length is reasonable */
155 arg->sms_size = ntohl(arg->sms_size);
156 if (arg->sms_size > 65536) {
159 arg->sms_flattened = malloc(arg->sms_size);
160 arg->sms_state = S_DECODE_DATA;
161 bcopy((caddr_t)&arg->sms_size, arg->sms_flattened, sizeof(long));
163 if (gdb_receive_data(hcon,
164 arg->sms_flattened + sizeof(long),
165 arg->sms_size - sizeof(long))
171 cp = arg->sms_flattened;
173 /* we already got the overall length.. */
174 for(i=1; i <4; i++) ip[i] = ntohl(ip[i]);
175 arg->sms_version_no = ip[1];
176 if (arg->sms_version_no != SMS_VERSION_1 &&
177 arg->sms_version_no != SMS_VERSION_2)
178 arg->sms_status = SMS_VERSION_MISMATCH;
179 else arg->sms_status = ip[2];
180 arg->sms_argc = ip[3];
181 cp += 4 * sizeof(int);
182 arg->sms_argv=(char **)malloc(arg->sms_argc *sizeof(char **));
183 arg->sms_argl=(int *)malloc(arg->sms_argc *sizeof(int *));
185 for (i = 0; i<arg->sms_argc; ++i) {
186 u_short nlen = ntohl(* (int *) cp);
188 if (cp + nlen > arg->sms_flattened + arg->sms_size) {
189 free(arg->sms_flattened);
190 arg->sms_flattened = NULL;
193 arg->sms_argv[i] = (char *)malloc(nlen);
194 bcopy(cp, arg->sms_argv[i], nlen);
195 arg->sms_argl[i]=nlen;
196 cp += sizeof(long) * howmany(nlen, sizeof(long));
198 free(arg->sms_flattened);
199 arg->sms_flattened = NULL;
207 sms_start_recv(op, hcon, argp)
209 HALF_CONNECTION hcon;
210 struct sms_params **argp;
212 register sms_params *arg = *argp;
214 *argp = arg = (sms_params *)malloc(sizeof(sms_params));
215 arg->sms_argl = NULL;
216 arg->sms_argv = NULL;
217 arg->sms_flattened = NULL;
219 arg->sms_state = S_RECV_START;
220 op->fcn.cont = sms_cont_recv;
221 return sms_cont_recv(op, hcon, argp);
224 sms_destroy_reply(reply)
230 free(reply->sms_argl);
231 reply->sms_argl = NULL;
232 if (reply->sms_flattened)
233 free(reply->sms_flattened);
234 reply->sms_flattened = NULL;
235 if (reply->sms_argv) {
236 for (i=0; i<reply->sms_argc; i++) {
237 if (reply->sms_argv[i])
238 free (reply->sms_argv[i]);
239 reply->sms_argv[i] = NULL;
241 free(reply->sms_argv);
243 reply->sms_argv = NULL;
252 * c-continued-statement-offset: 4
254 * c-argdecl-indent: 4