Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F3616495
tcp_sendto_test.c
tuexen (Michael Tuexen)
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
tuexen
Jul 26 2018, 2:52 PM
2018-07-26 14:52:21 (UTC+0)
Size
3 KB
Referenced Files
None
Subscribers
None
tcp_sendto_test.c
View Options
/*-
* 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
[])
{
char
buffer
[
100
];
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
*/
memset
(
buffer
,
'A'
,
100
);
if
(
sendto
(
fd
,
buffer
,
100
,
0
,
(
const
struct
sockaddr
*
)
&
addr
,
(
socklen_t
)
sizeof
(
struct
sockaddr_in6
))
<
0
)
{
perror
(
"sendto"
);
}
sleep
(
3600
);
if
(
close
(
fd
)
<
0
)
{
perror
(
"close"
);
}
return
(
0
);
}
File Metadata
Details
Attached
Mime Type
text/x-c
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1507981
Default Alt Text
tcp_sendto_test.c (3 KB)
Attached To
Mode
D16458: Allow implicit connection setup for TCP/IPv6
Attached
Detach File
Event Timeline
Log In to Comment