staticchar *afstr(int af, int proto)
{ if (proto == IPPROTO_TCP) return af == AF_INET ? "TCP/IPv4" : "TCP/IPv6"; else return af == AF_INET ? "UDP/IPv4" : "UDP/IPv6";
}
int sk_peek_offset_probe(sa_family_t af, int proto)
{ int type = (proto == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM); int optv = 0; int ret = 0; int s;
s = socket(af, type, proto); if (s < 0) {
ksft_perror("Temporary TCP socket creation failed");
} else { if (!setsockopt(s, SOL_SOCKET, SO_PEEK_OFF, &optv, sizeof(int)))
ret = 1; else
printf("%s does not support SO_PEEK_OFF\n", afstr(af, proto));
close(s);
} return ret;
}
staticvoid sk_peek_offset_set(int s, int offset)
{ if (setsockopt(s, SOL_SOCKET, SO_PEEK_OFF, &offset, sizeof(offset)))
ksft_perror("Failed to set SO_PEEK_OFF value\n");
}
staticint sk_peek_offset_get(int s)
{ int offset;
socklen_t len = sizeof(offset);
if (getsockopt(s, SOL_SOCKET, SO_PEEK_OFF, &offset, &len))
ksft_perror("Failed to get SO_PEEK_OFF value\n"); return offset;
}
staticint sk_peek_offset_test(sa_family_t af, int proto)
{ int type = (proto == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM); union { struct sockaddr sa; struct sockaddr_in a4; struct sockaddr_in6 a6;
} a; int res = 0; int s[2] = {0, 0}; int recv_sock = 0; int offset = 0;
ssize_t len; char buf[2];
/* Some basic tests of getting/setting offset */
offset = sk_peek_offset_get(recv_sock); if (offset != -1) {
ksft_perror("Initial value of socket offset not -1\n"); goto out;
}
sk_peek_offset_set(recv_sock, 0);
offset = sk_peek_offset_get(recv_sock); if (offset != 0) {
ksft_perror("Failed to set socket offset to 0\n"); goto out;
}
/* Transfer a message */ if (send(s[1], (char *)("ab"), 2, 0) != 2) {
ksft_perror("Temporary probe socket send() failed\n"); goto out;
} /* Read first byte */
len = recv(recv_sock, buf, 1, MSG_PEEK); if (len != 1 || buf[0] != 'a') {
ksft_perror("Failed to read first byte of message\n"); goto out;
}
offset = sk_peek_offset_get(recv_sock); if (offset != 1) {
ksft_perror("Offset not forwarded correctly at first byte\n"); goto out;
} /* Try to read beyond last byte */
len = recv(recv_sock, buf, 2, MSG_PEEK); if (len != 1 || buf[0] != 'b') {
ksft_perror("Failed to read last byte of message\n"); goto out;
}
offset = sk_peek_offset_get(recv_sock); if (offset != 2) {
ksft_perror("Offset not forwarded correctly at last byte\n"); goto out;
} /* Flush message */
len = recv(recv_sock, buf, 2, MSG_TRUNC); if (len != 2) {
ksft_perror("Failed to flush message\n"); goto out;
}
offset = sk_peek_offset_get(recv_sock); if (offset != 0) {
ksft_perror("Offset not reverted correctly after flush\n"); goto out;
}
printf("%s with MSG_PEEK_OFF works correctly\n", afstr(af, proto));
res = 1;
out: if (proto == IPPROTO_TCP && recv_sock >= 0)
close(recv_sock); if (s[1] >= 0)
close(s[1]); if (s[0] >= 0)
close(s[0]); return res;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.