1 /* $NetBSD: ip_trans.c,v 1.3 2013/11/27 17:53:00 christos Exp $ */
4 * Keith Bostic. All rights reserved.
6 * See the LICENSE file for redistribution information.
12 static const char sccsid
[] = "Id: ip_trans.c,v 8.18 2001/06/25 15:19:25 skimo Exp (Berkeley) Date: 2001/06/25 15:19:25 ";
15 #include <sys/types.h>
16 #include <sys/queue.h>
17 #ifdef HAVE_SYS_SELECT_H
18 #include <sys/select.h>
21 #include <bitstring.h>
25 #include <netinet/in.h>
33 #include "../common/common.h"
37 static char ibuf
[2048]; /* Input buffer. */
38 static size_t ibuf_len
; /* Length of current input. */
40 extern IPFUNLIST
const ipfuns
[];
44 * Read from the vi message queue.
46 * PUBLIC: int vi_input __P((IPVIWIN *, int));
49 vi_input(IPVIWIN
*ipviwin
, int fd
)
53 /* Read waiting vi messages and translate to X calls. */
54 switch (nr
= read(fd
, ibuf
+ ibuf_len
, sizeof(ibuf
) - ibuf_len
)) {
64 /* Parse to data end or partial message. */
65 (void)vi_translate(ipviwin
, ibuf
, &ibuf_len
, NULL
);
67 return (ibuf_len
> 0);
72 * Construct and send an IP buffer, and wait for an answer.
74 * PUBLIC: int vi_wsend __P((IPVIWIN*, char *, IP_BUF *));
77 vi_wsend(IPVIWIN
*ipviwin
, char *fmt
, IP_BUF
*ipbp
)
82 if (vi_send(ipviwin
->ofd
, fmt
, ipbp
))
86 ipbp
->code
= CODE_OOB
;
89 FD_SET(ipviwin
->ifd
, &rdfd
);
90 if (select(ipviwin
->ifd
+ 1, &rdfd
, NULL
, NULL
, NULL
) != 0)
93 /* Read waiting vi messages and translate to X calls. */
95 read(ipviwin
->ifd
, ibuf
+ ibuf_len
, sizeof(ibuf
) - ibuf_len
)) {
105 /* Parse to data end or partial message. */
106 (void)vi_translate(ipviwin
, ibuf
, &ibuf_len
, ipbp
);
108 if (ipbp
->code
!= CODE_OOB
)
116 * Translate vi messages into function calls.
118 * PUBLIC: int vi_translate __P((IPVIWIN *, char *, size_t *, IP_BUF *));
121 vi_translate(IPVIWIN
*ipviwin
, char *bp
, size_t *lenp
, IP_BUF
*ipbp
)
131 memset(&ipb
, 0, sizeof(ipb
));
132 for (s_bp
= bp
, len
= *lenp
; len
> 0;) {
133 fmt
= ipfuns
[(ipb
.code
= bp
[0])-1].format
;
135 p
= bp
+ IPO_CODE_LEN
;
136 needlen
= IPO_CODE_LEN
;
137 for (; *fmt
!= '\0'; ++fmt
)
139 case '1': /* Value #1. */
142 case '2': /* Value #2. */
145 case '3': /* Value #3. */
147 value
: needlen
+= IPO_INT_LEN
;
150 memcpy(vp
, p
, IPO_INT_LEN
);
154 case 'a': /* String #1. */
158 case 'b': /* String #2. */
161 string
: needlen
+= IPO_INT_LEN
;
164 memcpy(vp
, p
, IPO_INT_LEN
);
181 if (ipb
.code
>= SI_EVENT_SUP
) {
187 * If we're waiting for a reply and we got it, return it, and
188 * leave any unprocessed data in the buffer. If we got a reply
189 * and we're not waiting for one, discard it -- callers wait
192 if (ipb
.code
== SI_REPLY
) {
199 /* Call the underlying routine. */
201 (((char *)ipviwin
->si_ops
)+ipfuns
[ipb
.code
- 1].offset
);
203 ipfuns
[ipb
.code
- 1].unmarshall(ipviwin
, &ipb
, fun
))
207 if ((*lenp
= len
) != 0)
208 memmove(s_bp
, bp
, len
);