Home | History | Annotate | Line # | Download | only in ipc
ip_trans.c revision 1.2
      1  1.2  christos /*	$NetBSD: ip_trans.c,v 1.2 2013/11/22 15:52:05 christos Exp $	*/
      2  1.1  christos /*-
      3  1.1  christos  * Copyright (c) 1996
      4  1.1  christos  *	Keith Bostic.  All rights reserved.
      5  1.1  christos  *
      6  1.1  christos  * See the LICENSE file for redistribution information.
      7  1.1  christos  */
      8  1.1  christos 
      9  1.1  christos #include "config.h"
     10  1.1  christos 
     11  1.1  christos #ifndef lint
     12  1.1  christos 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 ";
     13  1.1  christos #endif /* not lint */
     14  1.1  christos 
     15  1.1  christos #include <sys/types.h>
     16  1.1  christos #include <sys/queue.h>
     17  1.1  christos #ifdef HAVE_SYS_SELECT_H
     18  1.1  christos #include <sys/select.h>
     19  1.1  christos #endif
     20  1.1  christos 
     21  1.1  christos #include <bitstring.h>
     22  1.1  christos #include <stdio.h>
     23  1.1  christos #include <string.h>
     24  1.1  christos #include <unistd.h>
     25  1.1  christos #include <netinet/in.h>
     26  1.1  christos 
     27  1.1  christos #ifdef __STDC__
     28  1.1  christos #include <stdarg.h>
     29  1.1  christos #else
     30  1.1  christos #include <varargs.h>
     31  1.1  christos #endif
     32  1.1  christos 
     33  1.1  christos #include "../common/common.h"
     34  1.1  christos #include "ip.h"
     35  1.1  christos #include "ipc_def.h"
     36  1.1  christos 
     37  1.1  christos static char ibuf[2048];				/* Input buffer. */
     38  1.1  christos static size_t ibuf_len;				/* Length of current input. */
     39  1.1  christos 
     40  1.1  christos extern IPFUNLIST const ipfuns[];
     41  1.1  christos 
     42  1.1  christos /*
     43  1.1  christos  * vi_input --
     44  1.1  christos  *	Read from the vi message queue.
     45  1.1  christos  *
     46  1.1  christos  * PUBLIC: int vi_input __P((IPVIWIN *, int));
     47  1.1  christos  */
     48  1.1  christos int
     49  1.1  christos vi_input(IPVIWIN *ipviwin, int fd)
     50  1.1  christos {
     51  1.1  christos 	ssize_t nr;
     52  1.1  christos 
     53  1.1  christos 	/* Read waiting vi messages and translate to X calls. */
     54  1.1  christos 	switch (nr = read(fd, ibuf + ibuf_len, sizeof(ibuf) - ibuf_len)) {
     55  1.1  christos 	case 0:
     56  1.1  christos 		return (0);
     57  1.1  christos 	case -1:
     58  1.1  christos 		return (-1);
     59  1.1  christos 	default:
     60  1.1  christos 		break;
     61  1.1  christos 	}
     62  1.1  christos 	ibuf_len += nr;
     63  1.1  christos 
     64  1.1  christos 	/* Parse to data end or partial message. */
     65  1.1  christos 	(void)vi_translate(ipviwin, ibuf, &ibuf_len, NULL);
     66  1.1  christos 
     67  1.1  christos 	return (ibuf_len > 0);
     68  1.1  christos }
     69  1.1  christos 
     70  1.1  christos /*
     71  1.1  christos  * vi_wsend --
     72  1.1  christos  *	Construct and send an IP buffer, and wait for an answer.
     73  1.1  christos  *
     74  1.1  christos  * PUBLIC: int vi_wsend __P((IPVIWIN*, char *, IP_BUF *));
     75  1.1  christos  */
     76  1.1  christos int
     77  1.1  christos vi_wsend(IPVIWIN *ipviwin, char *fmt, IP_BUF *ipbp)
     78  1.1  christos {
     79  1.1  christos 	fd_set rdfd;
     80  1.1  christos 	ssize_t nr;
     81  1.1  christos 
     82  1.1  christos 	if (vi_send(ipviwin->ofd, fmt, ipbp))
     83  1.1  christos 		return (1);
     84  1.1  christos 
     85  1.1  christos 	FD_ZERO(&rdfd);
     86  1.1  christos 	ipbp->code = CODE_OOB;
     87  1.1  christos 
     88  1.1  christos 	for (;;) {
     89  1.1  christos 		FD_SET(ipviwin->ifd, &rdfd);
     90  1.1  christos 		if (select(ipviwin->ifd + 1, &rdfd, NULL, NULL, NULL) != 0)
     91  1.1  christos 			return (-1);
     92  1.1  christos 
     93  1.1  christos 		/* Read waiting vi messages and translate to X calls. */
     94  1.1  christos 		switch (nr =
     95  1.1  christos 		    read(ipviwin->ifd, ibuf + ibuf_len, sizeof(ibuf) - ibuf_len)) {
     96  1.1  christos 		case 0:
     97  1.1  christos 			return (0);
     98  1.1  christos 		case -1:
     99  1.1  christos 			return (-1);
    100  1.1  christos 		default:
    101  1.1  christos 			break;
    102  1.1  christos 		}
    103  1.1  christos 		ibuf_len += nr;
    104  1.1  christos 
    105  1.1  christos 		/* Parse to data end or partial message. */
    106  1.1  christos 		(void)vi_translate(ipviwin, ibuf, &ibuf_len, ipbp);
    107  1.1  christos 
    108  1.1  christos 		if (ipbp->code != CODE_OOB)
    109  1.1  christos 			break;
    110  1.1  christos 	}
    111  1.1  christos 	return (0);
    112  1.1  christos }
    113  1.1  christos 
    114  1.1  christos /*
    115  1.1  christos  * vi_translate --
    116  1.1  christos  *	Translate vi messages into function calls.
    117  1.1  christos  *
    118  1.1  christos  * PUBLIC: int vi_translate __P((IPVIWIN *, char *, size_t *, IP_BUF *));
    119  1.1  christos  */
    120  1.1  christos int
    121  1.1  christos vi_translate(IPVIWIN *ipviwin, char *bp, size_t *lenp, IP_BUF *ipbp)
    122  1.1  christos {
    123  1.1  christos 	IP_BUF ipb;
    124  1.1  christos 	size_t len, needlen;
    125  1.1  christos 	u_int32_t *vp;
    126  1.2  christos 	const char *fmt;
    127  1.2  christos 	char *p, *s_bp;
    128  1.1  christos 	const char **vsp;
    129  1.1  christos 	IPFunc fun;
    130  1.1  christos 
    131  1.1  christos 	for (s_bp = bp, len = *lenp; len > 0;) {
    132  1.1  christos 		fmt = ipfuns[(ipb.code = bp[0])-1].format;
    133  1.1  christos 
    134  1.1  christos 		p = bp + IPO_CODE_LEN;
    135  1.1  christos 		needlen = IPO_CODE_LEN;
    136  1.1  christos 		for (; *fmt != '\0'; ++fmt)
    137  1.1  christos 			switch (*fmt) {
    138  1.1  christos 			case '1':				/* Value #1. */
    139  1.1  christos 				vp = &ipb.val1;
    140  1.1  christos 				goto value;
    141  1.1  christos 			case '2':				/* Value #2. */
    142  1.1  christos 				vp = &ipb.val2;
    143  1.1  christos 				goto value;
    144  1.1  christos 			case '3':				/* Value #3. */
    145  1.1  christos 				vp = &ipb.val3;
    146  1.1  christos value:				needlen += IPO_INT_LEN;
    147  1.1  christos 				if (len < needlen)
    148  1.1  christos 					goto partial;
    149  1.1  christos 				memcpy(vp, p, IPO_INT_LEN);
    150  1.1  christos 				*vp = ntohl(*vp);
    151  1.1  christos 				p += IPO_INT_LEN;
    152  1.1  christos 				break;
    153  1.1  christos 			case 'a':				/* String #1. */
    154  1.1  christos 				vp = &ipb.len1;
    155  1.1  christos 				vsp = &ipb.str1;
    156  1.1  christos 				goto string;
    157  1.1  christos 			case 'b':				/* String #2. */
    158  1.1  christos 				vp = &ipb.len2;
    159  1.1  christos 				vsp = &ipb.str2;
    160  1.1  christos string:				needlen += IPO_INT_LEN;
    161  1.1  christos 				if (len < needlen)
    162  1.1  christos 					goto partial;
    163  1.1  christos 				memcpy(vp, p, IPO_INT_LEN);
    164  1.1  christos 				*vp = ntohl(*vp);
    165  1.1  christos 				p += IPO_INT_LEN;
    166  1.1  christos 				needlen += *vp;
    167  1.1  christos 				if (len < needlen)
    168  1.1  christos 					goto partial;
    169  1.1  christos 				*vsp = p;
    170  1.1  christos 				p += *vp;
    171  1.1  christos 				break;
    172  1.1  christos 			}
    173  1.1  christos 		bp += needlen;
    174  1.1  christos 		len -= needlen;
    175  1.1  christos 
    176  1.1  christos 		/*
    177  1.1  christos 		 * XXX
    178  1.1  christos 		 * Protocol error!?!?
    179  1.1  christos 		 */
    180  1.1  christos 		if (ipb.code >= SI_EVENT_SUP) {
    181  1.1  christos 			len = 0;
    182  1.1  christos 			break;
    183  1.1  christos 		}
    184  1.1  christos 
    185  1.1  christos 		/*
    186  1.1  christos 		 * If we're waiting for a reply and we got it, return it, and
    187  1.1  christos 		 * leave any unprocessed data in the buffer.  If we got a reply
    188  1.1  christos 		 * and we're not waiting for one, discard it -- callers wait
    189  1.1  christos 		 * for responses.
    190  1.1  christos 		 */
    191  1.1  christos 		if (ipb.code == SI_REPLY) {
    192  1.1  christos 			if (ipbp == NULL)
    193  1.1  christos 				continue;
    194  1.1  christos 			*ipbp = ipb;
    195  1.1  christos 			break;
    196  1.1  christos 		}
    197  1.1  christos 
    198  1.1  christos 		/* Call the underlying routine. */
    199  1.1  christos 		fun = *(IPFunc *)
    200  1.1  christos 		    (((char *)ipviwin->si_ops)+ipfuns[ipb.code - 1].offset);
    201  1.1  christos 		if (fun != NULL &&
    202  1.1  christos 		    ipfuns[ipb.code - 1].unmarshall(ipviwin, &ipb, fun))
    203  1.1  christos 			break;
    204  1.1  christos 	}
    205  1.1  christos partial:
    206  1.1  christos 	if ((*lenp = len) != 0)
    207  1.1  christos 		memmove(s_bp, bp, len);
    208  1.1  christos 	return (0);
    209  1.1  christos }
    210