Page MenuHomeFreeBSD
Authored By
tuexen
Jan 13 2017, 10:37 AM
Size
3 KB
Referenced Files
None
Subscribers
None

connect_test.c

/*-
* Copyright (c) 2017 Michael Tuexen
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
struct sockaddr_in6 addr;
const int off = 0;
int fd, n;
if ((fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("socket");
}
if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(int)) < 0) {
perror("setsockopt");
}
if (argc == 5) {
memset(&addr, 0, sizeof(struct sockaddr_in6));
addr.sin6_family = AF_INET6;
#if defined(__FreeBSD__) || defined(__APPLE__)
addr.sin6_len = sizeof(struct sockaddr_in6);
#endif
addr.sin6_port = htons(atoi(argv[2]));
if ((n = inet_pton(AF_INET6, argv[1], &addr.sin6_addr)) < 1) {
if (n < 0) {
perror("inet_pton");
} else {
fprintf(stderr, "%s\n", "Address not parsable by inet_pton()");
}
}
if (bind(fd, (struct sockaddr *)&addr, (socklen_t)sizeof(struct sockaddr_in6)) < 0) {
perror("bind");
}
}
memset(&addr, 0, sizeof(struct sockaddr_in6));
addr.sin6_family = AF_INET6;
#if defined(__FreeBSD__) || defined(__APPLE__)
addr.sin6_len = sizeof(struct sockaddr_in6);
#endif
addr.sin6_port = htons(atoi(argv[argc == 5 ? 4 : 2]));
n = inet_pton(AF_INET6, argv[argc == 5 ? 3 : 1], &addr.sin6_addr);
if (n < 1) {
if (n < 0) {
perror("inet_pton");
} else {
fprintf(stderr, "%s\n", "Address not parsable by inet_pton()");
}
}
/*
* | errno
* local addr remote addr | FreeBSD Linux MacOS Solaris
* ============================================================================
* ipv4-mapped ipv6 | ETIMEDOUT ETIMEDOUT EHOSTUNREACH EADDRNOTAVAIL
* ipv6 ipv4-mapped | ETIMEDOUT ENETUNREACH EADDRNOTAVAIL EADDRNOTAVAIL
*
* Wouldn't reporting EAFNOSUPPORT make more sense?
* http://pubs.opengroup.org/onlinepubs/009695399/functions/connect.html
* https://tools.ietf.org/html/rfc6890
*/
if (connect(fd, (const struct sockaddr *)&addr, (socklen_t)sizeof(struct sockaddr_in6)) < 0) {
perror("connect");
}
if (close(fd) < 0) {
perror("close");
}
return (0);
}

File Metadata

Mime Type
text/x-c
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
719667
Default Alt Text
connect_test.c (3 KB)

Event Timeline