2 8d1b399b 2021-07-22 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 8d1b399b 2021-07-22 op * Permission to use, copy, modify, and distribute this software for any
5 8d1b399b 2021-07-22 op * purpose with or without fee is hereby granted, provided that the above
6 8d1b399b 2021-07-22 op * copyright notice and this permission notice appear in all copies.
8 8d1b399b 2021-07-22 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 8d1b399b 2021-07-22 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 8d1b399b 2021-07-22 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 8d1b399b 2021-07-22 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 8d1b399b 2021-07-22 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 8d1b399b 2021-07-22 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 8d1b399b 2021-07-22 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 d41445b9 2021-07-26 op #include "compat.h"
19 78b94752 2021-08-01 op #include <ctype.h>
20 78b94752 2021-08-01 op #include <stdio.h>
21 8d1b399b 2021-07-22 op #include <stdlib.h>
22 8d1b399b 2021-07-22 op #include <string.h>
24 d41445b9 2021-07-26 op #include "kamid.h"
25 8d1b399b 2021-07-22 op #include "log.h"
26 8d1b399b 2021-07-22 op #include "utils.h"
29 8d1b399b 2021-07-22 op xmalloc(size_t size)
33 8d1b399b 2021-07-22 op if ((r = malloc(size)) == NULL)
34 8d1b399b 2021-07-22 op fatal("malloc");
39 8d1b399b 2021-07-22 op xcalloc(size_t nmemb, size_t size)
43 8d1b399b 2021-07-22 op if ((r = calloc(nmemb, size)) == NULL)
44 8d1b399b 2021-07-22 op fatal("calloc");
49 8d1b399b 2021-07-22 op xstrdup(const char *s)
53 8d1b399b 2021-07-22 op if ((r = strdup(s)) == NULL)
54 8d1b399b 2021-07-22 op fatal("strdup");
59 8d1b399b 2021-07-22 op xmemdup(const void *d, size_t len)
63 8d1b399b 2021-07-22 op if ((r = malloc(len)) == NULL)
64 8d1b399b 2021-07-22 op fatal("malloc");
65 8d1b399b 2021-07-22 op memcpy(r, d, len);
70 d41445b9 2021-07-26 op pp_msg_type(uint8_t type)
72 d41445b9 2021-07-26 op switch (type) {
73 50428ac0 2021-07-28 op case Tversion: return "Tversion";
74 50428ac0 2021-07-28 op case Rversion: return "Rversion";
75 50428ac0 2021-07-28 op case Tauth: return "Tauth";
76 50428ac0 2021-07-28 op case Rauth: return "Rauth";
77 50428ac0 2021-07-28 op case Tattach: return "Tattach";
78 50428ac0 2021-07-28 op case Rattach: return "Rattach";
79 594eb2b8 2021-07-28 op case Terror: return "Terror"; /* illegal */
80 594eb2b8 2021-07-28 op case Rerror: return "Rerror";
81 50428ac0 2021-07-28 op case Tflush: return "Tflush";
82 50428ac0 2021-07-28 op case Rflush: return "Rflush";
83 50428ac0 2021-07-28 op case Twalk: return "Twalk";
84 50428ac0 2021-07-28 op case Rwalk: return "Rwalk";
85 50428ac0 2021-07-28 op case Topen: return "Topen";
86 50428ac0 2021-07-28 op case Ropen: return "Ropen";
87 50428ac0 2021-07-28 op case Tcreate: return "Tcreate";
88 50428ac0 2021-07-28 op case Rcreate: return "Rcreate";
89 50428ac0 2021-07-28 op case Tread: return "Tread";
90 50428ac0 2021-07-28 op case Rread: return "Rread";
91 50428ac0 2021-07-28 op case Twrite: return "Twrite";
92 50428ac0 2021-07-28 op case Rwrite: return "Rwrite";
93 50428ac0 2021-07-28 op case Tclunk: return "Tclunk";
94 50428ac0 2021-07-28 op case Rclunk: return "Rclunk";
95 50428ac0 2021-07-28 op case Tremove: return "Tremove";
96 50428ac0 2021-07-28 op case Rremove: return "Rremove";
97 50428ac0 2021-07-28 op case Tstat: return "Tstat";
98 50428ac0 2021-07-28 op case Rstat: return "Rstat";
99 50428ac0 2021-07-28 op case Twstat: return "Twstat";
100 50428ac0 2021-07-28 op case Rwstat: return "Rwstat";
101 d41445b9 2021-07-26 op default: return "unknown";
106 78b94752 2021-08-01 op hexdump_ppline(int x, uint8_t *data, size_t len)
108 78b94752 2021-08-01 op for (; x < 50; x++)
113 78b94752 2021-08-01 op for (x = 0; x < (int)len; ++x) {
114 78b94752 2021-08-01 op if (isgraph(data[x]))
115 78b94752 2021-08-01 op printf("%c", data[x]);
120 78b94752 2021-08-01 op printf("|\n");
124 78b94752 2021-08-01 op hexdump(const char *label, uint8_t *data, size_t len)
131 78b94752 2021-08-01 op * === first block === == second block == |........|\n
132 78b94752 2021-08-01 op * first and second block are 8 bytes long (for a total of 48
133 78b94752 2021-08-01 op * columns), plus two separator plus two | plus 16 chars, for
134 78b94752 2021-08-01 op * a total of 68 characters.
137 78b94752 2021-08-01 op printf("\nhexdump \"%s\": (%zu bytes)\n", label, len);
138 78b94752 2021-08-01 op for (x = 0, n = 0, i = 0; i < len; ++i) {
139 78b94752 2021-08-01 op if (i != 0 && i % 8 == 0) {
144 78b94752 2021-08-01 op if (n == 16) {
145 78b94752 2021-08-01 op hexdump_ppline(x, &data[i - 16], 16);
150 78b94752 2021-08-01 op printf("%02x ", data[i]);
156 78b94752 2021-08-01 op hexdump_ppline(x, &data[i - n], n);
158 78b94752 2021-08-01 op printf("\n");
162 eff5e4e8 2021-08-06 op imsg_event_add(struct imsgev *iev)
164 eff5e4e8 2021-08-06 op iev->events = EV_READ;
165 eff5e4e8 2021-08-06 op if (iev->ibuf.w.queued)
166 eff5e4e8 2021-08-06 op iev->events |= EV_WRITE;
168 eff5e4e8 2021-08-06 op event_del(&iev->ev);
169 eff5e4e8 2021-08-06 op event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
170 eff5e4e8 2021-08-06 op event_add(&iev->ev, NULL);
174 eff5e4e8 2021-08-06 op imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
175 eff5e4e8 2021-08-06 op pid_t pid, int fd, const void *data, uint16_t datalen)
179 eff5e4e8 2021-08-06 op if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
180 eff5e4e8 2021-08-06 op datalen) != -1))
181 eff5e4e8 2021-08-06 op imsg_event_add(iev);