1*0a6a1f1dSLionel Sambuc /* $NetBSD: ip_send.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc * Copyright (c) 1996
484d9c625SLionel Sambuc * Keith Bostic. All rights reserved.
584d9c625SLionel Sambuc *
684d9c625SLionel Sambuc * See the LICENSE file for redistribution information.
784d9c625SLionel Sambuc */
884d9c625SLionel Sambuc
984d9c625SLionel Sambuc #include "config.h"
1084d9c625SLionel Sambuc
11*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
12*0a6a1f1dSLionel Sambuc #if 0
1384d9c625SLionel Sambuc #ifndef lint
1484d9c625SLionel Sambuc 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 ";
1584d9c625SLionel Sambuc #endif /* not lint */
16*0a6a1f1dSLionel Sambuc #else
17*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ip_send.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
18*0a6a1f1dSLionel Sambuc #endif
1984d9c625SLionel Sambuc
2084d9c625SLionel Sambuc #include <sys/types.h>
2184d9c625SLionel Sambuc #include <sys/queue.h>
2284d9c625SLionel Sambuc
2384d9c625SLionel Sambuc #include <bitstring.h>
2484d9c625SLionel Sambuc #include <stdio.h>
2584d9c625SLionel Sambuc #include <stdlib.h>
2684d9c625SLionel Sambuc #include <string.h>
2784d9c625SLionel Sambuc #include <unistd.h>
2884d9c625SLionel Sambuc
2984d9c625SLionel Sambuc #include "../common/common.h"
3084d9c625SLionel Sambuc #include "ip.h"
3184d9c625SLionel Sambuc
3284d9c625SLionel Sambuc /*
3384d9c625SLionel Sambuc * vi_send --
3484d9c625SLionel Sambuc * Construct and send an IP buffer.
3584d9c625SLionel Sambuc *
3684d9c625SLionel Sambuc * PUBLIC: int vi_send __P((int, const char *, IP_BUF *));
3784d9c625SLionel Sambuc */
3884d9c625SLionel Sambuc int
vi_send(int ofd,const char * fmt,IP_BUF * ipbp)3984d9c625SLionel Sambuc vi_send(int ofd, const char *fmt, IP_BUF *ipbp)
4084d9c625SLionel Sambuc {
4184d9c625SLionel Sambuc static char *bp;
4284d9c625SLionel Sambuc static size_t blen;
4384d9c625SLionel Sambuc size_t off;
4484d9c625SLionel Sambuc u_int32_t ilen;
4584d9c625SLionel Sambuc size_t nlen;
4684d9c625SLionel Sambuc int n, nw;
4784d9c625SLionel Sambuc char *p;
4884d9c625SLionel Sambuc
4984d9c625SLionel Sambuc /*
5084d9c625SLionel Sambuc * Have not created the channel to vi yet? -- RAZ
5184d9c625SLionel Sambuc *
5284d9c625SLionel Sambuc * XXX
5384d9c625SLionel Sambuc * How is that possible!?!?
5484d9c625SLionel Sambuc *
5584d9c625SLionel Sambuc * We'll soon find out.
5684d9c625SLionel Sambuc */
5784d9c625SLionel Sambuc if (ofd == 0) {
5884d9c625SLionel Sambuc fprintf(stderr, "No channel\n");
5984d9c625SLionel Sambuc abort();
6084d9c625SLionel Sambuc }
6184d9c625SLionel Sambuc
6284d9c625SLionel Sambuc if (blen == 0 && (bp = malloc(blen = 512)) == NULL)
6384d9c625SLionel Sambuc return (1);
6484d9c625SLionel Sambuc
6584d9c625SLionel Sambuc p = bp;
6684d9c625SLionel Sambuc nlen = 0;
6784d9c625SLionel Sambuc *p++ = ipbp->code;
6884d9c625SLionel Sambuc nlen += IPO_CODE_LEN;
6984d9c625SLionel Sambuc
7084d9c625SLionel Sambuc if (fmt != NULL)
7184d9c625SLionel Sambuc for (; *fmt != '\0'; ++fmt)
7284d9c625SLionel Sambuc switch (*fmt) {
7384d9c625SLionel Sambuc case '1': /* Value #1. */
7484d9c625SLionel Sambuc ilen = htonl(ipbp->val1);
7584d9c625SLionel Sambuc goto value;
7684d9c625SLionel Sambuc case '2': /* Value #2. */
7784d9c625SLionel Sambuc ilen = htonl(ipbp->val2);
7884d9c625SLionel Sambuc goto value;
7984d9c625SLionel Sambuc case '3': /* Value #3. */
8084d9c625SLionel Sambuc ilen = htonl(ipbp->val3);
8184d9c625SLionel Sambuc value: nlen += IPO_INT_LEN;
8284d9c625SLionel Sambuc if (nlen >= blen) {
8384d9c625SLionel Sambuc blen = blen * 2 + nlen;
8484d9c625SLionel Sambuc off = p - bp;
8584d9c625SLionel Sambuc if ((bp = realloc(bp, blen)) == NULL)
8684d9c625SLionel Sambuc return (1);
8784d9c625SLionel Sambuc p = bp + off;
8884d9c625SLionel Sambuc }
8984d9c625SLionel Sambuc memcpy(p, &ilen, IPO_INT_LEN);
9084d9c625SLionel Sambuc p += IPO_INT_LEN;
9184d9c625SLionel Sambuc break;
9284d9c625SLionel Sambuc case 'a': /* String #1. */
9384d9c625SLionel Sambuc case 'b': /* String #2. */
9484d9c625SLionel Sambuc ilen = *fmt == 'a' ? ipbp->len1 : ipbp->len2;
9584d9c625SLionel Sambuc nlen += IPO_INT_LEN + ilen;
9684d9c625SLionel Sambuc if (nlen >= blen) {
9784d9c625SLionel Sambuc blen = blen * 2 + nlen;
9884d9c625SLionel Sambuc off = p - bp;
9984d9c625SLionel Sambuc if ((bp = realloc(bp, blen)) == NULL)
10084d9c625SLionel Sambuc return (1);
10184d9c625SLionel Sambuc p = bp + off;
10284d9c625SLionel Sambuc }
10384d9c625SLionel Sambuc ilen = htonl(ilen);
10484d9c625SLionel Sambuc memcpy(p, &ilen, IPO_INT_LEN);
10584d9c625SLionel Sambuc p += IPO_INT_LEN;
10684d9c625SLionel Sambuc if (*fmt == 'a') {
10784d9c625SLionel Sambuc memcpy(p, ipbp->str1, ipbp->len1);
10884d9c625SLionel Sambuc p += ipbp->len1;
10984d9c625SLionel Sambuc } else {
11084d9c625SLionel Sambuc memcpy(p, ipbp->str2, ipbp->len2);
11184d9c625SLionel Sambuc p += ipbp->len2;
11284d9c625SLionel Sambuc }
11384d9c625SLionel Sambuc break;
11484d9c625SLionel Sambuc }
11584d9c625SLionel Sambuc for (n = p - bp, p = bp; n > 0; n -= nw, p += nw)
11684d9c625SLionel Sambuc if ((nw = write(ofd, p, n)) < 0)
11784d9c625SLionel Sambuc return (1);
11884d9c625SLionel Sambuc return (0);
11984d9c625SLionel Sambuc }
120