6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
9 * Revision 1.2 1987-08-02 21:49:15 wesommer
10 * Clean up error handling, memory handling.
12 * Revision 1.1 87/06/16 17:48:21 wesommer
15 * Revision 1.4 87/06/04 01:32:18 wesommer
18 * Revision 1.3 87/06/01 03:33:54 wesommer
19 * Added destroy_reply.
21 * Revision 1.2 87/05/31 22:03:37 wesommer
22 * Fixed numerous bugs; still shaky.
27 static char *rcsid_sms_param_c = "$Header$";
30 #include <sys/types.h>
31 #include <netinet/in.h>
32 #include "sms_private.h"
35 * GDB operations to send and recieve RPC requests and replies.
39 * This doesn't get called until after the actual buffered write completes.
40 * In a non-preflattening version of this, this would then queue the
41 * write of the next bunch of data.
44 sms_cont_send(op, hcon, arg)
47 struct sms_params *arg;
49 op->result = OP_SUCCESS;
50 free(arg->sms_flattened);
51 arg->sms_flattened = NULL;
56 sms_start_send(op, hcon, arg)
59 register struct sms_params *arg;
62 unsigned int sms_size;
67 * This should probably be split into several routines.
68 * It could also probably be made more efficient (punting most
69 * of the argument marshalling stuff) by doing I/O directly
70 * from the strings. Anyone for a scatter/gather sms_send_data?
72 * that would look a lot like the uio stuff in the kernel.. hmm.
76 * Marshall the entire data right now..
77 * We are sending the version number,
78 * total request size, request number,
79 * argument count, and then each argument.
80 * At least for now, each argument is a string, which is
81 * sent as a count of bytes followed by the bytes
82 * (including the trailing '\0'), padded
83 * to a longword boundary.
86 sms_size = 4 * sizeof(long);
88 argl = (int *)malloc((unsigned)(sizeof(int) * arg->sms_argc));
91 * For each argument, figure out how much space is needed.
94 for (i = 0; i < arg->sms_argc; ++i) {
96 argl[i] = len = arg->sms_argl[i];
98 argl[i] = len = strlen(arg->sms_argv[i]) + 1;
99 sms_size += sizeof(long) + len;
100 /* Round up to next longword boundary.. */
101 sms_size = sizeof(long) * howmany(sms_size, sizeof(long));
104 arg->sms_flattened = buf = malloc(sms_size);
106 bzero(arg->sms_flattened, sms_size);
108 arg->sms_size = sms_size;
111 * This is gross. Any better suggestions, anyone?
112 * It should work on the RT's, since malloc is guaranteed to
113 * return a pointer which is aligned correctly for any data.
116 ((long *)buf)[0] = htonl(sms_size);
117 ((long *)buf)[1] = htonl((u_int)SMS_VERSION_1);
118 ((long *)buf)[2] = htonl(arg->sms_procno);
119 ((long *)buf)[3] = htonl(arg->sms_argc);
122 * bp is a pointer into the point in the buffer to put
126 bp = (char *)(((long *)buf) + 4);
128 for (i = 0; i<arg->sms_argc; ++i) {
130 *((long *)bp) = htonl(len);
132 bcopy(arg->sms_argv[i], bp, len);
133 bp += sizeof(long) * howmany(len, sizeof(long));
135 op->fcn.cont = sms_cont_send;
136 arg->sms_size = sms_size;
140 if (gdb_send_data(hcon, arg->sms_flattened, sms_size) == OP_COMPLETE)
141 return sms_cont_send(op, hcon, arg);
142 else return OP_RUNNING;
145 sms_cont_recv(op, hcon, argp)
147 HALF_CONNECTION hcon;
154 register sms_params *arg = *argp;
157 switch (arg->sms_state) {
159 arg->sms_state = S_RECV_DATA;
160 if (gdb_receive_data(hcon, (caddr_t)&arg->sms_size,
161 sizeof(long)) == OP_COMPLETE)
167 /* Should validate that length is reasonable */
168 arg->sms_size = ntohl(arg->sms_size);
169 if (arg->sms_size > 65536) {
172 arg->sms_flattened = malloc(arg->sms_size);
173 arg->sms_state = S_DECODE_DATA;
174 bcopy((caddr_t)&arg->sms_size, arg->sms_flattened, sizeof(long));
176 if (gdb_receive_data(hcon,
177 arg->sms_flattened + sizeof(long),
178 arg->sms_size - sizeof(long))
184 cp = arg->sms_flattened;
186 /* we already got the overall length.. */
187 for(i=1; i <4; i++) ip[i] = ntohl(ip[i]);
188 if (ip[1] != SMS_VERSION_1)
189 arg->sms_status = SMS_VERSION_MISMATCH;
190 else arg->sms_status = ip[2];
191 arg->sms_argc = ip[3];
192 cp += 4 * sizeof(int);
193 arg->sms_argv=(char **)malloc(arg->sms_argc *sizeof(char **));
194 arg->sms_argl=(int *)malloc(arg->sms_argc *sizeof(int *));
196 for (i = 0; i<arg->sms_argc; ++i) {
197 u_short nlen = ntohl(* (int *) cp);
199 if (cp + nlen > arg->sms_flattened + arg->sms_size) {
200 free(arg->sms_flattened);
201 arg->sms_flattened = NULL;
204 arg->sms_argv[i] = (char *)malloc(nlen);
205 bcopy(cp, arg->sms_argv[i], nlen);
206 arg->sms_argl[i]=nlen;
207 cp += sizeof(long) * howmany(nlen, sizeof(long));
209 free(arg->sms_flattened);
210 arg->sms_flattened = NULL;
218 sms_start_recv(op, hcon, argp)
220 HALF_CONNECTION hcon;
221 struct sms_params **argp;
223 register sms_params *arg = *argp;
225 *argp = arg = (sms_params *)malloc(sizeof(sms_params));
226 arg->sms_argl = NULL;
227 arg->sms_argv = NULL;
228 arg->sms_flattened = NULL;
230 arg->sms_state = S_RECV_START;
231 op->fcn.cont = sms_cont_recv;
232 return sms_cont_recv(op, hcon, argp);
235 sms_destroy_reply(reply)
241 free(reply->sms_argl);
242 reply->sms_argl = NULL;
243 if (reply->sms_flattened)
244 free(reply->sms_flattened);
245 reply->sms_flattened = NULL;
246 if (reply->sms_argv) {
247 for (i=0; i<reply->sms_argc; i++) {
248 if (reply->sms_argv[i])
249 free (reply->sms_argv[i]);
250 reply->sms_argv[i] = NULL;
252 free(reply->sms_argv);
254 reply->sms_argv = NULL;
263 * c-continued-statement-offset: 4
265 * c-argdecl-indent: 4