2 f024663d 2021-09-05 stsp * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 f024663d 2021-09-05 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
5 f024663d 2021-09-05 stsp * Permission to use, copy, modify, and distribute this software for any
6 f024663d 2021-09-05 stsp * purpose with or without fee is hereby granted, provided that the above
7 f024663d 2021-09-05 stsp * copyright notice and this permission notice appear in all copies.
9 f024663d 2021-09-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 f024663d 2021-09-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 f024663d 2021-09-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 f024663d 2021-09-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 f024663d 2021-09-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 f024663d 2021-09-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 f024663d 2021-09-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 f024663d 2021-09-05 stsp #include <ctype.h>
19 f024663d 2021-09-05 stsp #include <errno.h>
20 f024663d 2021-09-05 stsp #include <limits.h>
21 f024663d 2021-09-05 stsp #include <stdio.h>
22 f024663d 2021-09-05 stsp #include <stdlib.h>
23 f024663d 2021-09-05 stsp #include <unistd.h>
25 f024663d 2021-09-05 stsp #include "got_error.h"
26 336075a4 2022-06-25 op #include "got_lib_pkt.h"
28 f024663d 2021-09-05 stsp const struct got_error *
29 f024663d 2021-09-05 stsp got_pkt_readn(ssize_t *off, int fd, void *buf, size_t n)
34 f024663d 2021-09-05 stsp while (*off != n) {
35 f024663d 2021-09-05 stsp r = read(fd, buf + *off, n - *off);
36 f024663d 2021-09-05 stsp if (r == -1)
37 f024663d 2021-09-05 stsp return got_error_from_errno("read");
39 f024663d 2021-09-05 stsp return NULL;
42 f024663d 2021-09-05 stsp return NULL;
45 f024663d 2021-09-05 stsp const struct got_error *
46 f024663d 2021-09-05 stsp got_pkt_flushpkt(int fd, int chattygot)
50 f024663d 2021-09-05 stsp if (chattygot > 1)
51 f024663d 2021-09-05 stsp fprintf(stderr, "%s: writepkt: 0000\n", getprogname());
53 f024663d 2021-09-05 stsp w = write(fd, "0000", 4);
54 f024663d 2021-09-05 stsp if (w == -1)
55 f024663d 2021-09-05 stsp return got_error_from_errno("write");
57 f024663d 2021-09-05 stsp return got_error(GOT_ERR_IO);
58 f024663d 2021-09-05 stsp return NULL;
62 f024663d 2021-09-05 stsp * Packet header contains a 4-byte hexstring which specifies the length
63 f024663d 2021-09-05 stsp * of data which follows.
65 f024663d 2021-09-05 stsp const struct got_error *
66 f024663d 2021-09-05 stsp got_pkt_readhdr(int *datalen, int fd, int chattygot)
68 f024663d 2021-09-05 stsp static const struct got_error *err = NULL;
69 f024663d 2021-09-05 stsp char lenstr[5];
75 f024663d 2021-09-05 stsp *datalen = 0;
77 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, lenstr, 4);
80 f024663d 2021-09-05 stsp if (r == 0) {
81 f024663d 2021-09-05 stsp /* implicit "0000" */
82 f024663d 2021-09-05 stsp if (chattygot > 1)
83 f024663d 2021-09-05 stsp fprintf(stderr, "%s: readpkt: 0000\n", getprogname());
84 f024663d 2021-09-05 stsp return NULL;
87 f024663d 2021-09-05 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
88 f024663d 2021-09-05 stsp "wrong packet header length");
90 f024663d 2021-09-05 stsp lenstr[4] = '\0';
91 f024663d 2021-09-05 stsp for (i = 0; i < 4; i++) {
92 f024663d 2021-09-05 stsp if (!isprint((unsigned char)lenstr[i]))
93 f024663d 2021-09-05 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
94 f024663d 2021-09-05 stsp "unprintable character in packet length field");
96 f024663d 2021-09-05 stsp for (i = 0; i < 4; i++) {
97 f024663d 2021-09-05 stsp if (!isxdigit((unsigned char)lenstr[i])) {
98 f024663d 2021-09-05 stsp if (chattygot)
99 f024663d 2021-09-05 stsp fprintf(stderr, "%s: bad length: '%s'\n",
100 f024663d 2021-09-05 stsp getprogname(), lenstr);
101 f024663d 2021-09-05 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
102 f024663d 2021-09-05 stsp "packet length not specified in hex");
106 f024663d 2021-09-05 stsp len = strtol(lenstr, &e, 16);
107 f024663d 2021-09-05 stsp if (lenstr[0] == '\0' || *e != '\0')
108 f024663d 2021-09-05 stsp return got_error(GOT_ERR_BAD_PACKET);
109 f024663d 2021-09-05 stsp if (errno == ERANGE && (len == LONG_MAX || len == LONG_MIN))
110 f024663d 2021-09-05 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
111 f024663d 2021-09-05 stsp if (len > INT_MAX || len < INT_MIN)
112 f024663d 2021-09-05 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
114 f024663d 2021-09-05 stsp if (n == 0)
115 f024663d 2021-09-05 stsp return NULL;
116 f024663d 2021-09-05 stsp if (n <= 4)
117 f024663d 2021-09-05 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
120 f024663d 2021-09-05 stsp *datalen = n;
121 f024663d 2021-09-05 stsp return NULL;
124 f024663d 2021-09-05 stsp const struct got_error *
125 f024663d 2021-09-05 stsp got_pkt_readpkt(int *outlen, int fd, char *buf, int buflen, int chattygot)
127 f024663d 2021-09-05 stsp const struct got_error *err = NULL;
128 f024663d 2021-09-05 stsp int datalen, i;
131 f024663d 2021-09-05 stsp err = got_pkt_readhdr(&datalen, fd, chattygot);
133 f024663d 2021-09-05 stsp return err;
135 f024663d 2021-09-05 stsp if (datalen > buflen)
136 f024663d 2021-09-05 stsp return got_error(GOT_ERR_NO_SPACE);
138 f024663d 2021-09-05 stsp err = got_pkt_readn(&n, fd, buf, datalen);
140 f024663d 2021-09-05 stsp return err;
141 f024663d 2021-09-05 stsp if (n != datalen)
142 f024663d 2021-09-05 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
144 f024663d 2021-09-05 stsp if (chattygot > 1) {
145 f024663d 2021-09-05 stsp fprintf(stderr, "%s: readpkt: %zd:\t", getprogname(), n);
146 f024663d 2021-09-05 stsp for (i = 0; i < n; i++) {
147 f024663d 2021-09-05 stsp if (isprint(buf[i]))
148 f024663d 2021-09-05 stsp fputc(buf[i], stderr);
150 f024663d 2021-09-05 stsp fprintf(stderr, "[0x%.2x]", buf[i]);
152 f024663d 2021-09-05 stsp fputc('\n', stderr);
155 f024663d 2021-09-05 stsp *outlen = n;
156 f024663d 2021-09-05 stsp return NULL;
159 f024663d 2021-09-05 stsp const struct got_error *
160 f024663d 2021-09-05 stsp got_pkt_writepkt(int fd, char *buf, int nbuf, int chattygot)
162 f024663d 2021-09-05 stsp char len[5];
166 f024663d 2021-09-05 stsp if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
167 f024663d 2021-09-05 stsp return got_error(GOT_ERR_NO_SPACE);
168 f024663d 2021-09-05 stsp w = write(fd, len, 4);
169 f024663d 2021-09-05 stsp if (w == -1)
170 f024663d 2021-09-05 stsp return got_error_from_errno("write");
171 f024663d 2021-09-05 stsp if (w != 4)
172 f024663d 2021-09-05 stsp return got_error(GOT_ERR_IO);
173 f024663d 2021-09-05 stsp w = write(fd, buf, nbuf);
174 f024663d 2021-09-05 stsp if (w == -1)
175 f024663d 2021-09-05 stsp return got_error_from_errno("write");
176 f024663d 2021-09-05 stsp if (w != nbuf)
177 f024663d 2021-09-05 stsp return got_error(GOT_ERR_IO);
178 f024663d 2021-09-05 stsp if (chattygot > 1) {
179 f024663d 2021-09-05 stsp fprintf(stderr, "%s: writepkt: %s:\t", getprogname(), len);
180 f024663d 2021-09-05 stsp for (i = 0; i < nbuf; i++) {
181 f024663d 2021-09-05 stsp if (isprint(buf[i]))
182 f024663d 2021-09-05 stsp fputc(buf[i], stderr);
184 f024663d 2021-09-05 stsp fprintf(stderr, "[0x%.2x]", buf[i]);
186 f024663d 2021-09-05 stsp fputc('\n', stderr);
188 f024663d 2021-09-05 stsp return NULL;