#include #include #include #include #include #include #include #include #include // Max UDP payload size (safe cross-platform limit) #define MAX_PACKET_LEN 65507 // (65535 - 8 byte UDP header - 20 byte IP header) // MAX_PACKET_LEN 65507 - 15 => fails // MAX_PACKET_LEN 65507 - 19 => okay // MAX_PACKET_LEN 65507 - 20 => okay int main() { int fd; struct sockaddr_in6 sa6; socklen_t sa_len; char *buf; ssize_t n; size_t len; // Create IPv6 UDP socket fd = socket(AF_INET6, SOCK_DGRAM, 0); if (fd < 0) { perror("socket"); exit(EXIT_FAILURE); } // Setup target IPv6 address (loopback) memset(&sa6, 0, sizeof(sa6)); sa6.sin6_family = AF_INET6; sa6.sin6_port = htons(9999); // Change if needed if (inet_pton(AF_INET6, "::1", &sa6.sin6_addr) <= 0) { perror("inet_pton"); close(fd); exit(EXIT_FAILURE); } sa_len = sizeof(sa6); // Allocate buffer (can adjust to go beyond MAX_PACKET_LEN) len = MAX_PACKET_LEN; // Try MAX_PACKET_LEN + 1 to force EMSGSIZE buf = malloc(len); if (!buf) { perror("malloc"); close(fd); exit(EXIT_FAILURE); } memset(buf, 'B', len); // Fill buffer with dummy data // Send the packet n = sendto(fd, buf, len, 0, (struct sockaddr *)&sa6, sa_len); if (n < 0) { perror("sendto"); if (errno == EMSGSIZE) { fprintf(stderr, "Error: Message too long (EMSGSIZE)\n"); } } else { printf("Sent %zd bytes successfully over IPv6\n", n); } // Cleanup free(buf); close(fd); return 0; }