Blob


1 /*
2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <ctype.h>
19 #include <errno.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
25 #include "got_error.h"
26 #include "got_lib_pkt.h"
28 const struct got_error *
29 got_pkt_readn(ssize_t *off, int fd, void *buf, size_t n)
30 {
31 ssize_t r;
33 *off = 0;
34 while (*off != n) {
35 r = read(fd, buf + *off, n - *off);
36 if (r == -1)
37 return got_error_from_errno("read");
38 if (r == 0)
39 return got_error(GOT_ERR_EOF);
40 *off += r;
41 }
42 return NULL;
43 }
45 const struct got_error *
46 got_pkt_flushpkt(int fd, int chattygot)
47 {
48 ssize_t w;
50 if (chattygot > 1)
51 fprintf(stderr, "%s: writepkt: 0000\n", getprogname());
53 w = write(fd, "0000", 4);
54 if (w == -1)
55 return got_error_from_errno("write");
56 if (w != 4)
57 return got_error(GOT_ERR_IO);
58 return NULL;
59 }
61 const struct got_error *
62 got_pkt_readlen(int *len, const char *str, int chattygot)
63 {
64 int i;
66 *len = 0;
67 for (i = 0; i < 4; i++) {
68 if ('0' <= str[i] && str[i] <= '9') {
69 *len *= 16;
70 *len += str[i] - '0';
71 } else if ('a' <= str[i] && str[i] <= 'f') {
72 *len *= 16;
73 *len += str[i] - 'a' + 10;
74 } else {
75 if (chattygot)
76 fprintf(stderr, "%s: bad length: '.4%s'\n",
77 getprogname(), str);
78 return got_error_msg(GOT_ERR_BAD_PACKET,
79 "packet length has invalid format");
80 }
81 }
82 return NULL;
83 }
85 /*
86 * Packet header contains a 4-byte hexstring which specifies the length
87 * of data which follows.
88 */
89 const struct got_error *
90 got_pkt_readhdr(int *datalen, int fd, int chattygot)
91 {
92 static const struct got_error *err;
93 char lenstr[4];
94 ssize_t r;
95 int n;
97 *datalen = 0;
99 err = got_pkt_readn(&r, fd, lenstr, 4);
100 if (err)
101 return err;
102 if (r == 0) {
103 /* implicit "0000" */
104 if (chattygot > 1)
105 fprintf(stderr, "%s: readpkt: 0000\n", getprogname());
106 return NULL;
108 if (r != 4)
109 return got_error_msg(GOT_ERR_BAD_PACKET,
110 "wrong packet header length");
112 err = got_pkt_readlen(&n, lenstr, chattygot);
113 if (n == 0)
114 return err;
115 if (n <= 4)
116 return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
117 n -= 4;
119 *datalen = n;
120 return NULL;
123 const struct got_error *
124 got_pkt_readpkt(int *outlen, int fd, char *buf, int buflen, int chattygot)
126 const struct got_error *err = NULL;
127 int datalen, i;
128 ssize_t n;
130 err = got_pkt_readhdr(&datalen, fd, chattygot);
131 if (err)
132 return err;
134 if (datalen > buflen)
135 return got_error(GOT_ERR_NO_SPACE);
137 err = got_pkt_readn(&n, fd, buf, datalen);
138 if (err)
139 return err;
140 if (n != datalen)
141 return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
143 if (chattygot > 1) {
144 fprintf(stderr, "%s: readpkt: %zd:\t", getprogname(), n);
145 for (i = 0; i < n; i++) {
146 if (isprint((unsigned char)buf[i]))
147 fputc(buf[i], stderr);
148 else
149 fprintf(stderr, "[0x%.2x]", buf[i]);
151 fputc('\n', stderr);
154 *outlen = n;
155 return NULL;
158 const struct got_error *
159 got_pkt_writepkt(int fd, char *buf, int nbuf, int chattygot)
161 char len[5];
162 int i, ret;
163 ssize_t w;
165 ret = snprintf(len, sizeof(len), "%04x", nbuf + 4);
166 if (ret < 0 || (size_t)ret >= sizeof(len))
167 return got_error(GOT_ERR_NO_SPACE);
168 w = write(fd, len, 4);
169 if (w == -1)
170 return got_error_from_errno("write");
171 if (w != 4)
172 return got_error(GOT_ERR_IO);
173 w = write(fd, buf, nbuf);
174 if (w == -1)
175 return got_error_from_errno("write");
176 if (w != nbuf)
177 return got_error(GOT_ERR_IO);
178 if (chattygot > 1) {
179 fprintf(stderr, "%s: writepkt: %s:\t", getprogname(), len);
180 for (i = 0; i < nbuf; i++) {
181 if (isprint((unsigned char)buf[i]))
182 fputc(buf[i], stderr);
183 else
184 fprintf(stderr, "[0x%.2x]", buf[i]);
186 fputc('\n', stderr);
188 return NULL;