tog: group state assignments and zap dup repo assignment
[got-portable.git] / lib / pkt.c
blob4766c8fa97b29e1042f1c64af20a4354a8b0e505
1 /*
2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
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.
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.
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"
27 #include "got_lib_poll.h"
29 const struct got_error *
30 got_pkt_readn(ssize_t *off, int fd, void *buf, size_t n,
31 int timeout)
33 const struct got_error *err;
34 size_t len;
36 err = got_poll_read_full_timeout(fd, &len, buf, n, n,
37 timeout);
38 if (err)
39 return err;
41 /* XXX size_t -> ssize_t */
42 if (len > SSIZE_MAX)
43 return got_error(GOT_ERR_RANGE);
44 *off = len;
45 return NULL;
48 const struct got_error *
49 got_pkt_flushpkt(int fd, int chattygot)
51 ssize_t w;
53 if (chattygot > 1)
54 fprintf(stderr, "%s: writepkt: 0000\n", getprogname());
56 w = write(fd, "0000", 4);
57 if (w == -1)
58 return got_error_from_errno("write");
59 if (w != 4)
60 return got_error(GOT_ERR_IO);
61 return NULL;
64 const struct got_error *
65 got_pkt_readlen(int *len, const char *str, int chattygot)
67 int i;
69 *len = 0;
70 for (i = 0; i < 4; i++) {
71 if ('0' <= str[i] && str[i] <= '9') {
72 *len *= 16;
73 *len += str[i] - '0';
74 } else if ('a' <= str[i] && str[i] <= 'f') {
75 *len *= 16;
76 *len += str[i] - 'a' + 10;
77 } else {
78 if (chattygot)
79 fprintf(stderr, "%s: bad length: '.4%s'\n",
80 getprogname(), str);
81 return got_error_msg(GOT_ERR_BAD_PACKET,
82 "packet length has invalid format");
85 return NULL;
89 * Packet header contains a 4-byte hexstring which specifies the length
90 * of data which follows.
92 const struct got_error *
93 got_pkt_readhdr(int *datalen, int fd, int chattygot, int timeout)
95 static const struct got_error *err;
96 char lenstr[4];
97 ssize_t r;
98 int n;
100 *datalen = 0;
102 err = got_pkt_readn(&r, fd, lenstr, 4, timeout);
103 if (err)
104 return err;
105 if (r == 0) {
106 /* implicit "0000" */
107 if (chattygot > 1)
108 fprintf(stderr, "%s: readpkt: 0000\n", getprogname());
109 return NULL;
111 if (r != 4)
112 return got_error_msg(GOT_ERR_BAD_PACKET,
113 "wrong packet header length");
115 err = got_pkt_readlen(&n, lenstr, chattygot);
116 if (n == 0)
117 return err;
118 if (n <= 4)
119 return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
120 n -= 4;
122 *datalen = n;
123 return NULL;
126 const struct got_error *
127 got_pkt_readpkt(int *outlen, int fd, char *buf, int buflen, int chattygot,
128 int timeout)
130 const struct got_error *err = NULL;
131 int datalen, i;
132 ssize_t n;
134 err = got_pkt_readhdr(&datalen, fd, chattygot, timeout);
135 if (err)
136 return err;
138 if (datalen > buflen)
139 return got_error(GOT_ERR_NO_SPACE);
141 err = got_pkt_readn(&n, fd, buf, datalen, timeout);
142 if (err)
143 return err;
144 if (n != datalen)
145 return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
147 if (chattygot > 1) {
148 fprintf(stderr, "%s: readpkt: %zd:\t", getprogname(), n);
149 for (i = 0; i < n; i++) {
150 if (isprint((unsigned char)buf[i]))
151 fputc(buf[i], stderr);
152 else
153 fprintf(stderr, "[0x%.2x]", buf[i]);
155 fputc('\n', stderr);
158 *outlen = n;
159 return NULL;
162 const struct got_error *
163 got_pkt_writepkt(int fd, char *buf, int nbuf, int chattygot)
165 char len[5];
166 int i, ret;
167 ssize_t w;
169 ret = snprintf(len, sizeof(len), "%04x", nbuf + 4);
170 if (ret < 0 || (size_t)ret >= sizeof(len))
171 return got_error(GOT_ERR_NO_SPACE);
172 w = write(fd, len, 4);
173 if (w == -1)
174 return got_error_from_errno("write");
175 if (w != 4)
176 return got_error(GOT_ERR_IO);
177 w = write(fd, buf, nbuf);
178 if (w == -1)
179 return got_error_from_errno("write");
180 if (w != nbuf)
181 return got_error(GOT_ERR_IO);
182 if (chattygot > 1) {
183 fprintf(stderr, "%s: writepkt: %s:\t", getprogname(), len);
184 for (i = 0; i < nbuf; i++) {
185 if (isprint((unsigned char)buf[i]))
186 fputc(buf[i], stderr);
187 else
188 fprintf(stderr, "[0x%.2x]", buf[i]);
190 fputc('\n', stderr);
192 return NULL;