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