]>
Commit | Line | Data |
---|---|---|
6ff26f0b | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
8defc06b | 6 | * Copyright (C) 1987, 1990 by the Massachusetts Institute of Technology |
babbc197 | 7 | * For copying and distribution information, please see the file |
8 | * <mit-copyright.h>. | |
6ff26f0b | 9 | * |
6ff26f0b | 10 | */ |
11 | ||
12 | #ifndef lint | |
13 | static char *rcsid_sms_param_c = "$Header$"; | |
14 | #endif lint | |
15 | ||
babbc197 | 16 | #include <mit-copyright.h> |
9f56cedf | 17 | #include <sys/types.h> |
18 | #include <netinet/in.h> | |
8defc06b | 19 | #include "mr_private.h" |
6ff26f0b | 20 | |
21 | /* | |
22 | * GDB operations to send and recieve RPC requests and replies. | |
23 | */ | |
24 | ||
25 | /* | |
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. | |
29 | */ | |
30 | ||
8defc06b | 31 | mr_cont_send(op, hcon, arg) |
6ff26f0b | 32 | OPERATION op; |
33 | HALF_CONNECTION hcon; | |
8defc06b | 34 | struct mr_params *arg; |
6ff26f0b | 35 | { |
36 | op->result = OP_SUCCESS; | |
8defc06b | 37 | free(arg->mr_flattened); |
38 | arg->mr_flattened = NULL; | |
9f56cedf | 39 | |
6ff26f0b | 40 | return OP_COMPLETE; |
41 | } | |
42 | ||
8defc06b | 43 | mr_start_send(op, hcon, arg) |
6ff26f0b | 44 | OPERATION op; |
45 | HALF_CONNECTION hcon; | |
8defc06b | 46 | register struct mr_params *arg; |
6ff26f0b | 47 | { |
48 | int i, len; | |
8defc06b | 49 | unsigned int mr_size; |
6ff26f0b | 50 | int *argl; |
51 | char *buf, *bp; | |
52 | ||
53 | /* | |
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 | |
8defc06b | 57 | * from the strings. Anyone for a scatter/gather mr_send_data? |
6ff26f0b | 58 | * |
59 | * that would look a lot like the uio stuff in the kernel.. hmm. | |
60 | */ | |
61 | ||
62 | /* | |
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. | |
71 | */ | |
72 | ||
8defc06b | 73 | mr_size = 4 * sizeof(long); |
6ff26f0b | 74 | |
8defc06b | 75 | argl = (int *)malloc((unsigned)(sizeof(int) * arg->mr_argc)); |
6ff26f0b | 76 | |
77 | /* | |
78 | * For each argument, figure out how much space is needed. | |
79 | */ | |
80 | ||
8defc06b | 81 | for (i = 0; i < arg->mr_argc; ++i) { |
82 | if (arg->mr_argl) | |
83 | argl[i] = len = arg->mr_argl[i]; | |
6ff26f0b | 84 | else |
8defc06b | 85 | argl[i] = len = strlen(arg->mr_argv[i]) + 1; |
86 | mr_size += sizeof(long) + len; | |
6ff26f0b | 87 | /* Round up to next longword boundary.. */ |
8defc06b | 88 | mr_size = sizeof(long) * howmany(mr_size, sizeof(long)); |
6ff26f0b | 89 | } |
90 | ||
8defc06b | 91 | arg->mr_flattened = buf = malloc(mr_size); |
6ff26f0b | 92 | |
8defc06b | 93 | bzero(arg->mr_flattened, mr_size); |
6ff26f0b | 94 | |
8defc06b | 95 | arg->mr_size = mr_size; |
6ff26f0b | 96 | |
97 | /* | |
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. | |
101 | */ | |
102 | ||
8defc06b | 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); | |
6ff26f0b | 107 | |
108 | /* | |
109 | * bp is a pointer into the point in the buffer to put | |
110 | * the next argument. | |
111 | */ | |
112 | ||
113 | bp = (char *)(((long *)buf) + 4); | |
114 | ||
8defc06b | 115 | for (i = 0; i<arg->mr_argc; ++i) { |
6ff26f0b | 116 | len = argl[i]; |
117 | *((long *)bp) = htonl(len); | |
118 | bp += sizeof(long); | |
8defc06b | 119 | bcopy(arg->mr_argv[i], bp, len); |
6ff26f0b | 120 | bp += sizeof(long) * howmany(len, sizeof(long)); |
121 | } | |
8defc06b | 122 | op->fcn.cont = mr_cont_send; |
123 | arg->mr_size = mr_size; | |
6ff26f0b | 124 | |
125 | free(argl); | |
126 | ||
8defc06b | 127 | if (gdb_send_data(hcon, arg->mr_flattened, mr_size) == OP_COMPLETE) |
128 | return mr_cont_send(op, hcon, arg); | |
6ff26f0b | 129 | else return OP_RUNNING; |
130 | } | |
131 | ||
8defc06b | 132 | mr_cont_recv(op, hcon, argp) |
6ff26f0b | 133 | OPERATION op; |
134 | HALF_CONNECTION hcon; | |
8defc06b | 135 | mr_params **argp; |
6ff26f0b | 136 | { |
137 | int done = FALSE; | |
138 | char *cp; | |
139 | int *ip; | |
140 | int i; | |
8defc06b | 141 | register mr_params *arg = *argp; |
6ff26f0b | 142 | |
143 | while (!done) { | |
8defc06b | 144 | switch (arg->mr_state) { |
6ff26f0b | 145 | case S_RECV_START: |
8defc06b | 146 | arg->mr_state = S_RECV_DATA; |
147 | if (gdb_receive_data(hcon, (caddr_t)&arg->mr_size, | |
6ff26f0b | 148 | sizeof(long)) == OP_COMPLETE) |
149 | continue; | |
150 | done = TRUE; | |
151 | break; | |
152 | case S_RECV_DATA: | |
153 | fflush(stdout); | |
154 | /* Should validate that length is reasonable */ | |
8defc06b | 155 | arg->mr_size = ntohl(arg->mr_size); |
156 | if (arg->mr_size > 65536) { | |
9f56cedf | 157 | return OP_CANCELLED; |
158 | } | |
8defc06b | 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)); | |
6ff26f0b | 162 | |
163 | if (gdb_receive_data(hcon, | |
8defc06b | 164 | arg->mr_flattened + sizeof(long), |
165 | arg->mr_size - sizeof(long)) | |
6ff26f0b | 166 | == OP_COMPLETE) |
167 | continue; | |
168 | done = TRUE; | |
169 | break; | |
170 | case S_DECODE_DATA: | |
8defc06b | 171 | cp = arg->mr_flattened; |
6ff26f0b | 172 | ip = (int *) cp; |
173 | /* we already got the overall length.. */ | |
174 | for(i=1; i <4; i++) ip[i] = ntohl(ip[i]); | |
8defc06b | 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]; | |
6ff26f0b | 181 | cp += 4 * sizeof(int); |
8defc06b | 182 | arg->mr_argv=(char **)malloc(arg->mr_argc *sizeof(char **)); |
183 | arg->mr_argl=(int *)malloc(arg->mr_argc *sizeof(int *)); | |
6ff26f0b | 184 | |
8defc06b | 185 | for (i = 0; i<arg->mr_argc; ++i) { |
9f56cedf | 186 | u_short nlen = ntohl(* (int *) cp); |
6ff26f0b | 187 | cp += sizeof (long); |
8defc06b | 188 | if (cp + nlen > arg->mr_flattened + arg->mr_size) { |
189 | free(arg->mr_flattened); | |
190 | arg->mr_flattened = NULL; | |
9f56cedf | 191 | return OP_CANCELLED; |
192 | } | |
8defc06b | 193 | arg->mr_argv[i] = (char *)malloc(nlen); |
194 | bcopy(cp, arg->mr_argv[i], nlen); | |
195 | arg->mr_argl[i]=nlen; | |
6ff26f0b | 196 | cp += sizeof(long) * howmany(nlen, sizeof(long)); |
197 | } | |
8defc06b | 198 | free(arg->mr_flattened); |
199 | arg->mr_flattened = NULL; | |
6ff26f0b | 200 | return OP_COMPLETE; |
201 | } | |
202 | } | |
203 | return OP_RUNNING; | |
204 | } | |
205 | ||
206 | ||
8defc06b | 207 | mr_start_recv(op, hcon, argp) |
6ff26f0b | 208 | OPERATION op; |
209 | HALF_CONNECTION hcon; | |
8defc06b | 210 | struct mr_params **argp; |
6ff26f0b | 211 | { |
8defc06b | 212 | register mr_params *arg = *argp; |
6ff26f0b | 213 | if (!arg) { |
8defc06b | 214 | *argp = arg = (mr_params *)malloc(sizeof(mr_params)); |
215 | arg->mr_argl = NULL; | |
216 | arg->mr_argv = NULL; | |
217 | arg->mr_flattened = NULL; | |
6ff26f0b | 218 | } |
8defc06b | 219 | arg->mr_state = S_RECV_START; |
220 | op->fcn.cont = mr_cont_recv; | |
221 | return mr_cont_recv(op, hcon, argp); | |
6ff26f0b | 222 | } |
223 | ||
8defc06b | 224 | mr_destroy_reply(reply) |
225 | mr_params *reply; | |
6ff26f0b | 226 | { |
227 | int i; | |
228 | if (reply) { | |
8defc06b | 229 | if (reply->mr_argl) |
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; | |
6ff26f0b | 240 | } |
8defc06b | 241 | free(reply->mr_argv); |
6ff26f0b | 242 | } |
8defc06b | 243 | reply->mr_argv = NULL; |
6ff26f0b | 244 | free(reply); |
245 | } | |
246 | } |