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