Blame


1 f024663d 2021-09-05 stsp /*
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>
4 f024663d 2021-09-05 stsp *
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.
8 f024663d 2021-09-05 stsp *
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.
16 f024663d 2021-09-05 stsp */
17 f024663d 2021-09-05 stsp
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>
24 f024663d 2021-09-05 stsp
25 f024663d 2021-09-05 stsp #include "got_error.h"
26 ef20f542 2022-06-26 thomas #include "got_lib_pkt.h"
27 f024663d 2021-09-05 stsp
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)
30 f024663d 2021-09-05 stsp {
31 f024663d 2021-09-05 stsp ssize_t r;
32 f024663d 2021-09-05 stsp
33 f024663d 2021-09-05 stsp *off = 0;
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");
38 f024663d 2021-09-05 stsp if (r == 0)
39 23ac5000 2023-01-23 thomas return got_error(GOT_ERR_EOF);
40 f024663d 2021-09-05 stsp *off += r;
41 f024663d 2021-09-05 stsp }
42 f024663d 2021-09-05 stsp return NULL;
43 f024663d 2021-09-05 stsp }
44 f024663d 2021-09-05 stsp
45 f024663d 2021-09-05 stsp const struct got_error *
46 f024663d 2021-09-05 stsp got_pkt_flushpkt(int fd, int chattygot)
47 f024663d 2021-09-05 stsp {
48 f024663d 2021-09-05 stsp ssize_t w;
49 f024663d 2021-09-05 stsp
50 f024663d 2021-09-05 stsp if (chattygot > 1)
51 f024663d 2021-09-05 stsp fprintf(stderr, "%s: writepkt: 0000\n", getprogname());
52 f024663d 2021-09-05 stsp
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");
56 f024663d 2021-09-05 stsp if (w != 4)
57 f024663d 2021-09-05 stsp return got_error(GOT_ERR_IO);
58 f024663d 2021-09-05 stsp return NULL;
59 f024663d 2021-09-05 stsp }
60 f024663d 2021-09-05 stsp
61 564cf664 2024-04-25 thomas.ad const struct got_error *
62 564cf664 2024-04-25 thomas.ad got_pkt_readlen(int *len, const char *str, int chattygot)
63 564cf664 2024-04-25 thomas.ad {
64 564cf664 2024-04-25 thomas.ad int i;
65 564cf664 2024-04-25 thomas.ad
66 564cf664 2024-04-25 thomas.ad *len = 0;
67 564cf664 2024-04-25 thomas.ad for (i = 0; i < 4; i++) {
68 564cf664 2024-04-25 thomas.ad if ('0' <= str[i] && str[i] <= '9') {
69 564cf664 2024-04-25 thomas.ad *len *= 16;
70 564cf664 2024-04-25 thomas.ad *len += str[i] - '0';
71 564cf664 2024-04-25 thomas.ad } else if ('a' <= str[i] && str[i] <= 'f') {
72 564cf664 2024-04-25 thomas.ad *len *= 16;
73 564cf664 2024-04-25 thomas.ad *len += str[i] - 'a' + 10;
74 564cf664 2024-04-25 thomas.ad } else {
75 564cf664 2024-04-25 thomas.ad if (chattygot)
76 564cf664 2024-04-25 thomas.ad fprintf(stderr, "%s: bad length: '.4%s'\n",
77 564cf664 2024-04-25 thomas.ad getprogname(), str);
78 564cf664 2024-04-25 thomas.ad return got_error_msg(GOT_ERR_BAD_PACKET,
79 564cf664 2024-04-25 thomas.ad "packet length has invalid format");
80 564cf664 2024-04-25 thomas.ad }
81 564cf664 2024-04-25 thomas.ad }
82 564cf664 2024-04-25 thomas.ad return NULL;
83 564cf664 2024-04-25 thomas.ad }
84 564cf664 2024-04-25 thomas.ad
85 f024663d 2021-09-05 stsp /*
86 f024663d 2021-09-05 stsp * Packet header contains a 4-byte hexstring which specifies the length
87 f024663d 2021-09-05 stsp * of data which follows.
88 f024663d 2021-09-05 stsp */
89 f024663d 2021-09-05 stsp const struct got_error *
90 f024663d 2021-09-05 stsp got_pkt_readhdr(int *datalen, int fd, int chattygot)
91 f024663d 2021-09-05 stsp {
92 564cf664 2024-04-25 thomas.ad static const struct got_error *err;
93 564cf664 2024-04-25 thomas.ad char lenstr[4];
94 f024663d 2021-09-05 stsp ssize_t r;
95 564cf664 2024-04-25 thomas.ad int n;
96 f024663d 2021-09-05 stsp
97 f024663d 2021-09-05 stsp *datalen = 0;
98 f024663d 2021-09-05 stsp
99 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, lenstr, 4);
100 f024663d 2021-09-05 stsp if (err)
101 f024663d 2021-09-05 stsp return err;
102 f024663d 2021-09-05 stsp if (r == 0) {
103 f024663d 2021-09-05 stsp /* implicit "0000" */
104 f024663d 2021-09-05 stsp if (chattygot > 1)
105 f024663d 2021-09-05 stsp fprintf(stderr, "%s: readpkt: 0000\n", getprogname());
106 f024663d 2021-09-05 stsp return NULL;
107 f024663d 2021-09-05 stsp }
108 f024663d 2021-09-05 stsp if (r != 4)
109 f024663d 2021-09-05 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
110 f024663d 2021-09-05 stsp "wrong packet header length");
111 f024663d 2021-09-05 stsp
112 564cf664 2024-04-25 thomas.ad err = got_pkt_readlen(&n, lenstr, chattygot);
113 f024663d 2021-09-05 stsp if (n == 0)
114 564cf664 2024-04-25 thomas.ad return err;
115 f024663d 2021-09-05 stsp if (n <= 4)
116 f024663d 2021-09-05 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
117 564cf664 2024-04-25 thomas.ad n -= 4;
118 f024663d 2021-09-05 stsp
119 f024663d 2021-09-05 stsp *datalen = n;
120 f024663d 2021-09-05 stsp return NULL;
121 f024663d 2021-09-05 stsp }
122 f024663d 2021-09-05 stsp
123 f024663d 2021-09-05 stsp const struct got_error *
124 f024663d 2021-09-05 stsp got_pkt_readpkt(int *outlen, int fd, char *buf, int buflen, int chattygot)
125 f024663d 2021-09-05 stsp {
126 f024663d 2021-09-05 stsp const struct got_error *err = NULL;
127 f024663d 2021-09-05 stsp int datalen, i;
128 f024663d 2021-09-05 stsp ssize_t n;
129 f024663d 2021-09-05 stsp
130 f024663d 2021-09-05 stsp err = got_pkt_readhdr(&datalen, fd, chattygot);
131 f024663d 2021-09-05 stsp if (err)
132 f024663d 2021-09-05 stsp return err;
133 f024663d 2021-09-05 stsp
134 f024663d 2021-09-05 stsp if (datalen > buflen)
135 f024663d 2021-09-05 stsp return got_error(GOT_ERR_NO_SPACE);
136 f024663d 2021-09-05 stsp
137 f024663d 2021-09-05 stsp err = got_pkt_readn(&n, fd, buf, datalen);
138 f024663d 2021-09-05 stsp if (err)
139 f024663d 2021-09-05 stsp return err;
140 f024663d 2021-09-05 stsp if (n != datalen)
141 f024663d 2021-09-05 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
142 f024663d 2021-09-05 stsp
143 f024663d 2021-09-05 stsp if (chattygot > 1) {
144 f024663d 2021-09-05 stsp fprintf(stderr, "%s: readpkt: %zd:\t", getprogname(), n);
145 f024663d 2021-09-05 stsp for (i = 0; i < n; i++) {
146 6771d425 2022-11-17 thomas if (isprint((unsigned char)buf[i]))
147 f024663d 2021-09-05 stsp fputc(buf[i], stderr);
148 f024663d 2021-09-05 stsp else
149 f024663d 2021-09-05 stsp fprintf(stderr, "[0x%.2x]", buf[i]);
150 f024663d 2021-09-05 stsp }
151 f024663d 2021-09-05 stsp fputc('\n', stderr);
152 f024663d 2021-09-05 stsp }
153 f024663d 2021-09-05 stsp
154 f024663d 2021-09-05 stsp *outlen = n;
155 f024663d 2021-09-05 stsp return NULL;
156 f024663d 2021-09-05 stsp }
157 f024663d 2021-09-05 stsp
158 f024663d 2021-09-05 stsp const struct got_error *
159 f024663d 2021-09-05 stsp got_pkt_writepkt(int fd, char *buf, int nbuf, int chattygot)
160 f024663d 2021-09-05 stsp {
161 f024663d 2021-09-05 stsp char len[5];
162 717a78d4 2022-08-16 thomas int i, ret;
163 f024663d 2021-09-05 stsp ssize_t w;
164 f024663d 2021-09-05 stsp
165 717a78d4 2022-08-16 thomas ret = snprintf(len, sizeof(len), "%04x", nbuf + 4);
166 717a78d4 2022-08-16 thomas if (ret < 0 || (size_t)ret >= 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 6771d425 2022-11-17 thomas if (isprint((unsigned char)buf[i]))
182 f024663d 2021-09-05 stsp fputc(buf[i], stderr);
183 f024663d 2021-09-05 stsp else
184 f024663d 2021-09-05 stsp fprintf(stderr, "[0x%.2x]", buf[i]);
185 f024663d 2021-09-05 stsp }
186 f024663d 2021-09-05 stsp fputc('\n', stderr);
187 f024663d 2021-09-05 stsp }
188 f024663d 2021-09-05 stsp return NULL;
189 f024663d 2021-09-05 stsp }