static void print_packet(struct utcp *utcp, const char *dir, const void *pkt, size_t len) {
struct hdr hdr;
- if(len < sizeof hdr) {
+ if(len < sizeof(hdr)) {
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");
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;
}
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)
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) {
.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;
- 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;
- 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->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;
}
- struct utcp_connection *c = calloc(1, sizeof *c);
+ struct utcp_connection *c = calloc(1, sizeof(*c));
if(!c)
return NULL;
// 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);
return c;
}
set_state(c, SYN_SENT);
- 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;
uint8_t data[];
} *pkt;
- pkt = malloc(sizeof pkt->hdr + c->utcp->mtu);
+ pkt = malloc(sizeof(pkt->hdr) + c->utcp->mtu);
if(!pkt)
return;
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);
uint8_t data[];
} *pkt;
- pkt = malloc(sizeof pkt->hdr + c->utcp->mtu);
+ pkt = malloc(sizeof(pkt->hdr) + c->utcp->mtu);
if(!pkt)
return;
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:
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));
break;
case ESTABLISHED:
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);
break;
case CLOSED:
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;
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 {
// Drop packets smaller than the header
struct hdr hdr;
- if(len < sizeof hdr) {
+ if(len < sizeof(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
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;
- 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
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));
return 0;
}
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));
return 0;
}
return NULL;
}
- struct utcp *utcp = calloc(1, sizeof *utcp);
+ struct utcp *utcp = calloc(1, sizeof(*utcp));
if(!utcp)
return NULL;