Home | History | Annotate | Line # | Download | only in ipc
ip_send.c revision 1.2
      1  1.2  christos /*	$NetBSD: ip_send.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_send.c,v 8.10 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 
     18  1.1  christos #include <bitstring.h>
     19  1.1  christos #include <stdio.h>
     20  1.1  christos #include <stdlib.h>
     21  1.1  christos #include <string.h>
     22  1.1  christos #include <unistd.h>
     23  1.1  christos 
     24  1.1  christos #include "../common/common.h"
     25  1.1  christos #include "ip.h"
     26  1.1  christos 
     27  1.1  christos /*
     28  1.1  christos  * vi_send --
     29  1.1  christos  *	Construct and send an IP buffer.
     30  1.1  christos  *
     31  1.2  christos  * PUBLIC: int vi_send __P((int, const char *, IP_BUF *));
     32  1.1  christos  */
     33  1.1  christos int
     34  1.2  christos vi_send(int ofd, const char *fmt, IP_BUF *ipbp)
     35  1.1  christos {
     36  1.1  christos 	static char *bp;
     37  1.1  christos 	static size_t blen;
     38  1.1  christos 	size_t off;
     39  1.1  christos 	u_int32_t ilen;
     40  1.2  christos 	size_t nlen;
     41  1.2  christos 	int n, nw;
     42  1.1  christos 	char *p;
     43  1.1  christos 
     44  1.1  christos 	/*
     45  1.1  christos 	 * Have not created the channel to vi yet?  -- RAZ
     46  1.1  christos 	 *
     47  1.1  christos 	 * XXX
     48  1.1  christos 	 * How is that possible!?!?
     49  1.1  christos 	 *
     50  1.1  christos 	 * We'll soon find out.
     51  1.1  christos 	 */
     52  1.1  christos 	if (ofd == 0) {
     53  1.1  christos 		fprintf(stderr, "No channel\n");
     54  1.1  christos 		abort();
     55  1.1  christos 	}
     56  1.1  christos 
     57  1.1  christos 	if (blen == 0 && (bp = malloc(blen = 512)) == NULL)
     58  1.1  christos 		return (1);
     59  1.1  christos 
     60  1.1  christos 	p = bp;
     61  1.1  christos 	nlen = 0;
     62  1.1  christos 	*p++ = ipbp->code;
     63  1.1  christos 	nlen += IPO_CODE_LEN;
     64  1.1  christos 
     65  1.1  christos 	if (fmt != NULL)
     66  1.1  christos 		for (; *fmt != '\0'; ++fmt)
     67  1.1  christos 			switch (*fmt) {
     68  1.1  christos 			case '1':				/* Value #1. */
     69  1.1  christos 				ilen = htonl(ipbp->val1);
     70  1.1  christos 				goto value;
     71  1.1  christos 			case '2':				/* Value #2. */
     72  1.1  christos 				ilen = htonl(ipbp->val2);
     73  1.1  christos 				goto value;
     74  1.1  christos 			case '3':				/* Value #3. */
     75  1.1  christos 				ilen = htonl(ipbp->val3);
     76  1.1  christos value:				nlen += IPO_INT_LEN;
     77  1.1  christos 				if (nlen >= blen) {
     78  1.1  christos 					blen = blen * 2 + nlen;
     79  1.1  christos 					off = p - bp;
     80  1.1  christos 					if ((bp = realloc(bp, blen)) == NULL)
     81  1.1  christos 						return (1);
     82  1.1  christos 					p = bp + off;
     83  1.1  christos 				}
     84  1.1  christos 				memcpy(p, &ilen, IPO_INT_LEN);
     85  1.1  christos 				p += IPO_INT_LEN;
     86  1.1  christos 				break;
     87  1.1  christos 			case 'a':				/* String #1. */
     88  1.1  christos 			case 'b':				/* String #2. */
     89  1.1  christos 				ilen = *fmt == 'a' ? ipbp->len1 : ipbp->len2;
     90  1.1  christos 				nlen += IPO_INT_LEN + ilen;
     91  1.1  christos 				if (nlen >= blen) {
     92  1.1  christos 					blen = blen * 2 + nlen;
     93  1.1  christos 					off = p - bp;
     94  1.1  christos 					if ((bp = realloc(bp, blen)) == NULL)
     95  1.1  christos 						return (1);
     96  1.1  christos 					p = bp + off;
     97  1.1  christos 				}
     98  1.1  christos 				ilen = htonl(ilen);
     99  1.1  christos 				memcpy(p, &ilen, IPO_INT_LEN);
    100  1.1  christos 				p += IPO_INT_LEN;
    101  1.1  christos 				if (*fmt == 'a') {
    102  1.1  christos 					memcpy(p, ipbp->str1, ipbp->len1);
    103  1.1  christos 					p += ipbp->len1;
    104  1.1  christos 				} else {
    105  1.1  christos 					memcpy(p, ipbp->str2, ipbp->len2);
    106  1.1  christos 					p += ipbp->len2;
    107  1.1  christos 				}
    108  1.1  christos 				break;
    109  1.1  christos 			}
    110  1.1  christos 	for (n = p - bp, p = bp; n > 0; n -= nw, p += nw)
    111  1.1  christos 		if ((nw = write(ofd, p, n)) < 0)
    112  1.1  christos 			return (1);
    113  1.1  christos 	return (0);
    114  1.1  christos }
    115