]> andersk Git - moira.git/blame - lib/mr_param.c
remove dependancy on resolver internals
[moira.git] / lib / mr_param.c
CommitLineData
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
13static 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
24582af9 31/*ARGSUSED*/
8defc06b 32mr_cont_send(op, hcon, arg)
6ff26f0b 33 OPERATION op;
34 HALF_CONNECTION hcon;
8defc06b 35 struct mr_params *arg;
6ff26f0b 36{
37 op->result = OP_SUCCESS;
8defc06b 38 free(arg->mr_flattened);
39 arg->mr_flattened = NULL;
9f56cedf 40
6ff26f0b 41 return OP_COMPLETE;
42}
43
8defc06b 44mr_start_send(op, hcon, arg)
6ff26f0b 45 OPERATION op;
46 HALF_CONNECTION hcon;
8defc06b 47 register struct mr_params *arg;
6ff26f0b 48{
49 int i, len;
8defc06b 50 unsigned int mr_size;
6ff26f0b 51 int *argl;
52 char *buf, *bp;
53
54 /*
55 * This should probably be split into several routines.
56 * It could also probably be made more efficient (punting most
57 * of the argument marshalling stuff) by doing I/O directly
8defc06b 58 * from the strings. Anyone for a scatter/gather mr_send_data?
6ff26f0b 59 *
60 * that would look a lot like the uio stuff in the kernel.. hmm.
61 */
62
63 /*
64 * Marshall the entire data right now..
65 * We are sending the version number,
66 * total request size, request number,
67 * argument count, and then each argument.
68 * At least for now, each argument is a string, which is
69 * sent as a count of bytes followed by the bytes
70 * (including the trailing '\0'), padded
71 * to a longword boundary.
72 */
73
8defc06b 74 mr_size = 4 * sizeof(long);
6ff26f0b 75
8defc06b 76 argl = (int *)malloc((unsigned)(sizeof(int) * arg->mr_argc));
6ff26f0b 77
78 /*
79 * For each argument, figure out how much space is needed.
80 */
81
8defc06b 82 for (i = 0; i < arg->mr_argc; ++i) {
83 if (arg->mr_argl)
84 argl[i] = len = arg->mr_argl[i];
6ff26f0b 85 else
8defc06b 86 argl[i] = len = strlen(arg->mr_argv[i]) + 1;
87 mr_size += sizeof(long) + len;
6ff26f0b 88 /* Round up to next longword boundary.. */
8defc06b 89 mr_size = sizeof(long) * howmany(mr_size, sizeof(long));
6ff26f0b 90 }
91
8defc06b 92 arg->mr_flattened = buf = malloc(mr_size);
6ff26f0b 93
8defc06b 94 bzero(arg->mr_flattened, mr_size);
6ff26f0b 95
8defc06b 96 arg->mr_size = mr_size;
6ff26f0b 97
98 /*
99 * This is gross. Any better suggestions, anyone?
100 * It should work on the RT's, since malloc is guaranteed to
101 * return a pointer which is aligned correctly for any data.
102 */
103
8defc06b 104 ((long *)buf)[0] = htonl(mr_size);
105 ((long *)buf)[1] = htonl(arg->mr_version_no);
106 ((long *)buf)[2] = htonl(arg->mr_procno);
107 ((long *)buf)[3] = htonl(arg->mr_argc);
6ff26f0b 108
109 /*
110 * bp is a pointer into the point in the buffer to put
111 * the next argument.
112 */
113
114 bp = (char *)(((long *)buf) + 4);
115
8defc06b 116 for (i = 0; i<arg->mr_argc; ++i) {
6ff26f0b 117 len = argl[i];
118 *((long *)bp) = htonl(len);
119 bp += sizeof(long);
8defc06b 120 bcopy(arg->mr_argv[i], bp, len);
6ff26f0b 121 bp += sizeof(long) * howmany(len, sizeof(long));
122 }
8defc06b 123 op->fcn.cont = mr_cont_send;
124 arg->mr_size = mr_size;
6ff26f0b 125
126 free(argl);
127
8defc06b 128 if (gdb_send_data(hcon, arg->mr_flattened, mr_size) == OP_COMPLETE)
129 return mr_cont_send(op, hcon, arg);
6ff26f0b 130 else return OP_RUNNING;
131}
132
24582af9 133/*ARGSUSED*/
8defc06b 134mr_cont_recv(op, hcon, argp)
6ff26f0b 135 OPERATION op;
136 HALF_CONNECTION hcon;
8defc06b 137 mr_params **argp;
6ff26f0b 138{
139 int done = FALSE;
140 char *cp;
141 int *ip;
142 int i;
8defc06b 143 register mr_params *arg = *argp;
6ff26f0b 144
145 while (!done) {
8defc06b 146 switch (arg->mr_state) {
6ff26f0b 147 case S_RECV_START:
8defc06b 148 arg->mr_state = S_RECV_DATA;
149 if (gdb_receive_data(hcon, (caddr_t)&arg->mr_size,
6ff26f0b 150 sizeof(long)) == OP_COMPLETE)
151 continue;
152 done = TRUE;
153 break;
154 case S_RECV_DATA:
155 fflush(stdout);
156 /* Should validate that length is reasonable */
8defc06b 157 arg->mr_size = ntohl(arg->mr_size);
158 if (arg->mr_size > 65536) {
9f56cedf 159 return OP_CANCELLED;
160 }
8defc06b 161 arg->mr_flattened = malloc(arg->mr_size);
162 arg->mr_state = S_DECODE_DATA;
163 bcopy((caddr_t)&arg->mr_size, arg->mr_flattened, sizeof(long));
6ff26f0b 164
165 if (gdb_receive_data(hcon,
8defc06b 166 arg->mr_flattened + sizeof(long),
167 arg->mr_size - sizeof(long))
6ff26f0b 168 == OP_COMPLETE)
169 continue;
170 done = TRUE;
171 break;
172 case S_DECODE_DATA:
8defc06b 173 cp = arg->mr_flattened;
6ff26f0b 174 ip = (int *) cp;
175 /* we already got the overall length.. */
176 for(i=1; i <4; i++) ip[i] = ntohl(ip[i]);
8defc06b 177 arg->mr_version_no = ip[1];
178 if (arg->mr_version_no != MR_VERSION_1 &&
179 arg->mr_version_no != MR_VERSION_2)
180 arg->mr_status = MR_VERSION_MISMATCH;
181 else arg->mr_status = ip[2];
182 arg->mr_argc = ip[3];
6ff26f0b 183 cp += 4 * sizeof(int);
8defc06b 184 arg->mr_argv=(char **)malloc(arg->mr_argc *sizeof(char **));
185 arg->mr_argl=(int *)malloc(arg->mr_argc *sizeof(int *));
6ff26f0b 186
8defc06b 187 for (i = 0; i<arg->mr_argc; ++i) {
9f56cedf 188 u_short nlen = ntohl(* (int *) cp);
6ff26f0b 189 cp += sizeof (long);
8defc06b 190 if (cp + nlen > arg->mr_flattened + arg->mr_size) {
191 free(arg->mr_flattened);
192 arg->mr_flattened = NULL;
9f56cedf 193 return OP_CANCELLED;
194 }
8defc06b 195 arg->mr_argv[i] = (char *)malloc(nlen);
196 bcopy(cp, arg->mr_argv[i], nlen);
197 arg->mr_argl[i]=nlen;
6ff26f0b 198 cp += sizeof(long) * howmany(nlen, sizeof(long));
199 }
8defc06b 200 free(arg->mr_flattened);
201 arg->mr_flattened = NULL;
6ff26f0b 202 return OP_COMPLETE;
203 }
204 }
205 return OP_RUNNING;
206}
207
208
8defc06b 209mr_start_recv(op, hcon, argp)
6ff26f0b 210 OPERATION op;
211 HALF_CONNECTION hcon;
8defc06b 212 struct mr_params **argp;
6ff26f0b 213{
8defc06b 214 register mr_params *arg = *argp;
6ff26f0b 215 if (!arg) {
8defc06b 216 *argp = arg = (mr_params *)malloc(sizeof(mr_params));
217 arg->mr_argl = NULL;
218 arg->mr_argv = NULL;
219 arg->mr_flattened = NULL;
6ff26f0b 220 }
8defc06b 221 arg->mr_state = S_RECV_START;
222 op->fcn.cont = mr_cont_recv;
223 return mr_cont_recv(op, hcon, argp);
6ff26f0b 224}
225
8defc06b 226mr_destroy_reply(reply)
227 mr_params *reply;
6ff26f0b 228{
229 int i;
230 if (reply) {
8defc06b 231 if (reply->mr_argl)
232 free(reply->mr_argl);
233 reply->mr_argl = NULL;
234 if (reply->mr_flattened)
235 free(reply->mr_flattened);
236 reply->mr_flattened = NULL;
237 if (reply->mr_argv) {
238 for (i=0; i<reply->mr_argc; i++) {
239 if (reply->mr_argv[i])
240 free (reply->mr_argv[i]);
241 reply->mr_argv[i] = NULL;
6ff26f0b 242 }
8defc06b 243 free(reply->mr_argv);
6ff26f0b 244 }
8defc06b 245 reply->mr_argv = NULL;
6ff26f0b 246 free(reply);
247 }
248}
This page took 0.100593 seconds and 5 git commands to generate.