1 /* $NetBSD: ip_trans.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */
4 * Keith Bostic. All rights reserved.
6 * See the LICENSE file for redistribution information.
11 #include <sys/cdefs.h>
14 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 ";
17 __RCSID("$NetBSD: ip_trans.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
20 #include <sys/types.h>
21 #include <sys/queue.h>
22 #ifdef HAVE_SYS_SELECT_H
23 #include <sys/select.h>
26 #include <bitstring.h>
30 #include <netinet/in.h>
38 #include "../common/common.h"
42 static char ibuf
[2048]; /* Input buffer. */
43 static size_t ibuf_len
; /* Length of current input. */
45 extern IPFUNLIST
const ipfuns
[];
49 * Read from the vi message queue.
51 * PUBLIC: int vi_input __P((IPVIWIN *, int));
54 vi_input(IPVIWIN
*ipviwin
, int fd
)
58 /* Read waiting vi messages and translate to X calls. */
59 switch (nr
= read(fd
, ibuf
+ ibuf_len
, sizeof(ibuf
) - ibuf_len
)) {
69 /* Parse to data end or partial message. */
70 (void)vi_translate(ipviwin
, ibuf
, &ibuf_len
, NULL
);
72 return (ibuf_len
> 0);
77 * Construct and send an IP buffer, and wait for an answer.
79 * PUBLIC: int vi_wsend __P((IPVIWIN*, char *, IP_BUF *));
82 vi_wsend(IPVIWIN
*ipviwin
, char *fmt
, IP_BUF
*ipbp
)
87 if (vi_send(ipviwin
->ofd
, fmt
, ipbp
))
91 ipbp
->code
= CODE_OOB
;
94 FD_SET(ipviwin
->ifd
, &rdfd
);
95 if (select(ipviwin
->ifd
+ 1, &rdfd
, NULL
, NULL
, NULL
) != 0)
98 /* Read waiting vi messages and translate to X calls. */
100 read(ipviwin
->ifd
, ibuf
+ ibuf_len
, sizeof(ibuf
) - ibuf_len
)) {
110 /* Parse to data end or partial message. */
111 (void)vi_translate(ipviwin
, ibuf
, &ibuf_len
, ipbp
);
113 if (ipbp
->code
!= CODE_OOB
)
121 * Translate vi messages into function calls.
123 * PUBLIC: int vi_translate __P((IPVIWIN *, char *, size_t *, IP_BUF *));
126 vi_translate(IPVIWIN
*ipviwin
, char *bp
, size_t *lenp
, IP_BUF
*ipbp
)
136 memset(&ipb
, 0, sizeof(ipb
));
137 for (s_bp
= bp
, len
= *lenp
; len
> 0;) {
138 fmt
= ipfuns
[(ipb
.code
= bp
[0])-1].format
;
140 p
= bp
+ IPO_CODE_LEN
;
141 needlen
= IPO_CODE_LEN
;
142 for (; *fmt
!= '\0'; ++fmt
)
144 case '1': /* Value #1. */
147 case '2': /* Value #2. */
150 case '3': /* Value #3. */
152 value
: needlen
+= IPO_INT_LEN
;
155 memcpy(vp
, p
, IPO_INT_LEN
);
159 case 'a': /* String #1. */
163 case 'b': /* String #2. */
166 string
: needlen
+= IPO_INT_LEN
;
169 memcpy(vp
, p
, IPO_INT_LEN
);
186 if (ipb
.code
>= SI_EVENT_SUP
) {
192 * If we're waiting for a reply and we got it, return it, and
193 * leave any unprocessed data in the buffer. If we got a reply
194 * and we're not waiting for one, discard it -- callers wait
197 if (ipb
.code
== SI_REPLY
) {
204 /* Call the underlying routine. */
206 (((char *)ipviwin
->si_ops
)+ipfuns
[ipb
.code
- 1].offset
);
208 ipfuns
[ipb
.code
- 1].unmarshall(ipviwin
, &ipb
, fun
))
212 if ((*lenp
= len
) != 0)
213 memmove(s_bp
, bp
, len
);