While technically sizeof is an operator and doesn't need the parentheses
around expressions it operates on, except if they are type names, code
formatters don't seem to handle this very well.
utcp_set_sndbuf(c, 10240);
char buf[20480] = "buf";
utcp_set_sndbuf(c, 10240);
char buf[20480] = "buf";
- len = utcp_send(c, buf, sizeof buf);
+ len = utcp_send(c, buf, sizeof(buf));
if(len != 10240)
fprintf(stderr, "Error: utcp_send() returned %zd, expected 10240\n", len);
if(len != 10240)
fprintf(stderr, "Error: utcp_send() returned %zd, expected 10240\n", len);
while(!connected || utcp_is_active(u)) {
size_t max = c ? utcp_get_sndbuf_free(c) : 0;
while(!connected || utcp_is_active(u)) {
size_t max = c ? utcp_get_sndbuf_free(c) : 0;
- if(max > sizeof buf)
- max = sizeof buf;
+ if(max > sizeof(buf))
+ max = sizeof(buf);
if(max > read_size)
max = read_size;
if(max > read_size)
max = read_size;
fds[1].revents = 0;
debug("netin\n");
struct sockaddr_storage ss;
fds[1].revents = 0;
debug("netin\n");
struct sockaddr_storage ss;
- socklen_t sl = sizeof ss;
- int len = recvfrom(s, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
+ socklen_t sl = sizeof(ss);
+ int len = recvfrom(s, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
if(len <= 0) {
debug("Error receiving UDP packet: %s\n", strerror(errno));
break;
if(len <= 0) {
debug("Error receiving UDP packet: %s\n", strerror(errno));
break;
static void print_packet(struct utcp *utcp, const char *dir, const void *pkt, size_t len) {
struct hdr hdr;
static void print_packet(struct utcp *utcp, const char *dir, const void *pkt, size_t len) {
struct hdr hdr;
+ if(len < sizeof(hdr)) {
debug("%p %s: short packet (%lu bytes)\n", utcp, dir, (unsigned long)len);
return;
}
debug("%p %s: short packet (%lu bytes)\n", utcp, dir, (unsigned long)len);
return;
}
- memcpy(&hdr, pkt, sizeof hdr);
+ memcpy(&hdr, pkt, sizeof(hdr));
debug("%p %s: len=%lu, src=%u dst=%u seq=%u ack=%u wnd=%u aux=%x ctl=", utcp, dir, (unsigned long)len, hdr.src, hdr.dst, hdr.seq, hdr.ack, hdr.wnd, hdr.aux);
if(hdr.ctl & SYN)
debug("SYN");
debug("%p %s: len=%lu, src=%u dst=%u seq=%u ack=%u wnd=%u aux=%x ctl=", utcp, dir, (unsigned long)len, hdr.src, hdr.dst, hdr.seq, hdr.ack, hdr.wnd, hdr.aux);
if(hdr.ctl & SYN)
debug("SYN");
if(hdr.ctl & ACK)
debug("ACK");
if(hdr.ctl & ACK)
debug("ACK");
- if(len > sizeof hdr) {
- uint32_t datalen = len - sizeof hdr;
- const uint8_t *data = (uint8_t *)pkt + sizeof hdr;
+ if(len > sizeof(hdr)) {
+ uint32_t datalen = len - sizeof(hdr);
+ const uint8_t *data = (uint8_t *)pkt + sizeof(hdr);
char str[datalen * 2 + 1];
char *p = str;
char str[datalen * 2 + 1];
char *p = str;
}
static bool buffer_init(struct buffer *buf, uint32_t len, uint32_t maxlen) {
}
static bool buffer_init(struct buffer *buf, uint32_t len, uint32_t maxlen) {
- memset(buf, 0, sizeof *buf);
+ memset(buf, 0, sizeof(*buf));
if(len) {
buf->data = malloc(len);
if(!buf->data)
if(len) {
buf->data = malloc(len);
if(!buf->data)
static void buffer_exit(struct buffer *buf) {
free(buf->data);
static void buffer_exit(struct buffer *buf) {
free(buf->data);
- memset(buf, 0, sizeof *buf);
+ memset(buf, 0, sizeof(*buf));
}
static uint32_t buffer_free(const struct buffer *buf) {
}
static uint32_t buffer_free(const struct buffer *buf) {
.src = src,
.dst = dst,
}, *keyp = &key;
.src = src,
.dst = dst,
}, *keyp = &key;
- struct utcp_connection **match = bsearch(&keyp, utcp->connections, utcp->nconnections, sizeof *utcp->connections, compare);
+ struct utcp_connection **match = bsearch(&keyp, utcp->connections, utcp->nconnections, sizeof(*utcp->connections), compare);
return match ? *match : NULL;
}
static void free_connection(struct utcp_connection *c) {
struct utcp *utcp = c->utcp;
return match ? *match : NULL;
}
static void free_connection(struct utcp_connection *c) {
struct utcp *utcp = c->utcp;
- struct utcp_connection **cp = bsearch(&c, utcp->connections, utcp->nconnections, sizeof *utcp->connections, compare);
+ struct utcp_connection **cp = bsearch(&c, utcp->connections, utcp->nconnections, sizeof(*utcp->connections), compare);
assert(cp);
int i = cp - utcp->connections;
assert(cp);
int i = cp - utcp->connections;
- memmove(cp, cp + 1, (utcp->nconnections - i - 1) * sizeof *cp);
+ memmove(cp, cp + 1, (utcp->nconnections - i - 1) * sizeof(*cp));
utcp->nconnections--;
buffer_exit(&c->rcvbuf);
utcp->nconnections--;
buffer_exit(&c->rcvbuf);
utcp->nallocated = 4;
else
utcp->nallocated *= 2;
utcp->nallocated = 4;
else
utcp->nallocated *= 2;
- struct utcp_connection **new_array = realloc(utcp->connections, utcp->nallocated * sizeof *utcp->connections);
+ struct utcp_connection **new_array = realloc(utcp->connections, utcp->nallocated * sizeof(*utcp->connections));
if(!new_array)
return NULL;
utcp->connections = new_array;
}
if(!new_array)
return NULL;
utcp->connections = new_array;
}
- struct utcp_connection *c = calloc(1, sizeof *c);
+ struct utcp_connection *c = calloc(1, sizeof(*c));
// Add it to the sorted list of connections
utcp->connections[utcp->nconnections++] = c;
// Add it to the sorted list of connections
utcp->connections[utcp->nconnections++] = c;
- qsort(utcp->connections, utcp->nconnections, sizeof *utcp->connections, compare);
+ qsort(utcp->connections, utcp->nconnections, sizeof(*utcp->connections), compare);
- print_packet(utcp, "send", &pkt, sizeof pkt);
- utcp->send(utcp, &pkt, sizeof pkt);
+ print_packet(utcp, "send", &pkt, sizeof(pkt));
+ utcp->send(utcp, &pkt, sizeof(pkt));
gettimeofday(&c->conn_timeout, NULL);
c->conn_timeout.tv_sec += utcp->timeout;
gettimeofday(&c->conn_timeout, NULL);
c->conn_timeout.tv_sec += utcp->timeout;
- pkt = malloc(sizeof pkt->hdr + c->utcp->mtu);
+ pkt = malloc(sizeof(pkt->hdr) + c->utcp->mtu);
debug("Starting RTT measurement, expecting ack %u\n", c->rtt_seq);
}
debug("Starting RTT measurement, expecting ack %u\n", c->rtt_seq);
}
- print_packet(c->utcp, "send", pkt, sizeof pkt->hdr + seglen);
- c->utcp->send(c->utcp, pkt, sizeof pkt->hdr + seglen);
+ print_packet(c->utcp, "send", pkt, sizeof(pkt->hdr) + seglen);
+ c->utcp->send(c->utcp, pkt, sizeof(pkt->hdr) + seglen);
} while(left);
free(pkt);
} while(left);
free(pkt);
- pkt = malloc(sizeof pkt->hdr + c->utcp->mtu);
+ pkt = malloc(sizeof(pkt->hdr) + c->utcp->mtu);
pkt->data[1] = 0;
pkt->data[2] = 0;
pkt->data[3] = c->flags & 0x7;
pkt->data[1] = 0;
pkt->data[2] = 0;
pkt->data[3] = c->flags & 0x7;
- print_packet(c->utcp, "rtrx", pkt, sizeof pkt->hdr + 4);
- utcp->send(utcp, pkt, sizeof pkt->hdr + 4);
+ print_packet(c->utcp, "rtrx", pkt, sizeof(pkt->hdr) + 4);
+ utcp->send(utcp, pkt, sizeof(pkt->hdr) + 4);
break;
case SYN_RECEIVED:
break;
case SYN_RECEIVED:
pkt->hdr.seq = c->snd.nxt;
pkt->hdr.ack = c->rcv.nxt;
pkt->hdr.ctl = SYN | ACK;
pkt->hdr.seq = c->snd.nxt;
pkt->hdr.ack = c->rcv.nxt;
pkt->hdr.ctl = SYN | ACK;
- print_packet(c->utcp, "rtrx", pkt, sizeof pkt->hdr);
- utcp->send(utcp, pkt, sizeof pkt->hdr);
+ print_packet(c->utcp, "rtrx", pkt, sizeof(pkt->hdr));
+ utcp->send(utcp, pkt, sizeof(pkt->hdr));
c->snd.nxt = c->snd.una + len;
c->snd.cwnd = utcp->mtu; // reduce cwnd on retransmit
buffer_copy(&c->sndbuf, pkt->data, 0, len);
c->snd.nxt = c->snd.una + len;
c->snd.cwnd = utcp->mtu; // reduce cwnd on retransmit
buffer_copy(&c->sndbuf, pkt->data, 0, len);
- print_packet(c->utcp, "rtrx", pkt, sizeof pkt->hdr + len);
- utcp->send(utcp, pkt, sizeof pkt->hdr + len);
+ print_packet(c->utcp, "rtrx", pkt, sizeof(pkt->hdr) + len);
+ utcp->send(utcp, pkt, sizeof(pkt->hdr) + len);
i++;
} else {
if(i < NSACKS - 1) {
i++;
} else {
if(i < NSACKS - 1) {
- memmove(&c->sacks[i], &c->sacks[i + 1], (NSACKS - 1 - i) * sizeof c->sacks[i]);
+ memmove(&c->sacks[i], &c->sacks[i + 1], (NSACKS - 1 - i) * sizeof(c->sacks)[i]);
c->sacks[NSACKS - 1].len = 0;
} else {
c->sacks[i].len = 0;
c->sacks[NSACKS - 1].len = 0;
} else {
c->sacks[i].len = 0;
if(offset + rxd < c->sacks[i].offset) { // insert before
if(!c->sacks[NSACKS - 1].len) { // only if room left
debug("Insert SACK entry at %d\n", i);
if(offset + rxd < c->sacks[i].offset) { // insert before
if(!c->sacks[NSACKS - 1].len) { // only if room left
debug("Insert SACK entry at %d\n", i);
- memmove(&c->sacks[i + 1], &c->sacks[i], (NSACKS - i - 1) * sizeof c->sacks[i]);
+ memmove(&c->sacks[i + 1], &c->sacks[i], (NSACKS - i - 1) * sizeof(c->sacks)[i]);
c->sacks[i].offset = offset;
c->sacks[i].len = rxd;
} else {
c->sacks[i].offset = offset;
c->sacks[i].len = rxd;
} else {
// Drop packets smaller than the header
struct hdr hdr;
// Drop packets smaller than the header
struct hdr hdr;
+ if(len < sizeof(hdr)) {
errno = EBADMSG;
return -1;
}
// Make a copy from the potentially unaligned data to a struct hdr
errno = EBADMSG;
return -1;
}
// Make a copy from the potentially unaligned data to a struct hdr
- memcpy(&hdr, data, sizeof hdr);
- data += sizeof hdr;
- len -= sizeof hdr;
+ memcpy(&hdr, data, sizeof(hdr));
+ data += sizeof(hdr);
+ len -= sizeof(hdr);
// Drop packets with an unknown CTL flag
// Drop packets with an unknown CTL flag
pkt.data[1] = 0;
pkt.data[2] = 0;
pkt.data[3] = c->flags & 0x7;
pkt.data[1] = 0;
pkt.data[2] = 0;
pkt.data[3] = c->flags & 0x7;
- print_packet(c->utcp, "send", &pkt, sizeof hdr + 4);
- utcp->send(utcp, &pkt, sizeof hdr + 4);
+ print_packet(c->utcp, "send", &pkt, sizeof(hdr) + 4);
+ utcp->send(utcp, &pkt, sizeof(hdr) + 4);
} else {
pkt.hdr.aux = 0;
} else {
pkt.hdr.aux = 0;
- print_packet(c->utcp, "send", &pkt, sizeof hdr);
- utcp->send(utcp, &pkt, sizeof hdr);
+ print_packet(c->utcp, "send", &pkt, sizeof(hdr));
+ utcp->send(utcp, &pkt, sizeof(hdr));
}
} else {
// No, we don't want your packets, send a RST back
}
} else {
// No, we don't want your packets, send a RST back
hdr.seq = 0;
hdr.ctl = RST | ACK;
}
hdr.seq = 0;
hdr.ctl = RST | ACK;
}
- print_packet(utcp, "send", &hdr, sizeof hdr);
- utcp->send(utcp, &hdr, sizeof hdr);
+ print_packet(utcp, "send", &hdr, sizeof(hdr));
+ utcp->send(utcp, &hdr, sizeof(hdr));
hdr.wnd = 0;
hdr.ctl = RST;
hdr.wnd = 0;
hdr.ctl = RST;
- print_packet(c->utcp, "send", &hdr, sizeof hdr);
- c->utcp->send(c->utcp, &hdr, sizeof hdr);
+ print_packet(c->utcp, "send", &hdr, sizeof(hdr));
+ c->utcp->send(c->utcp, &hdr, sizeof(hdr));
- struct utcp *utcp = calloc(1, sizeof *utcp);
+ struct utcp *utcp = calloc(1, sizeof(*utcp));