Bind to IPv6+IPv4 adresses.
This commit is contained in:
parent
03848777b2
commit
b98c794591
1 changed files with 31 additions and 24 deletions
55
netcalc.c
55
netcalc.c
|
@ -21,11 +21,31 @@ void cleanup(int signum)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void printaddr(struct addrinfo *ainfo)
|
||||||
|
{
|
||||||
|
char ipstr[INET6_ADDRSTRLEN]; // will hold the IP adress, humanreadable
|
||||||
|
void *addr;
|
||||||
|
char *ipver;
|
||||||
|
for (struct addrinfo *p = ainfo; p->ai_next != NULL; p = p->ai_next) {
|
||||||
|
if (p->ai_family == AF_INET) { // IPv4
|
||||||
|
struct sockaddr_in *ipv4 = (struct sockaddr_in *) p->ai_addr;
|
||||||
|
addr = &(ipv4->sin_addr);
|
||||||
|
ipver = "IPv4";
|
||||||
|
} else { // IPv6
|
||||||
|
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *) p->ai_addr;
|
||||||
|
addr = &(ipv6->sin6_addr);
|
||||||
|
ipver = "IPv6";
|
||||||
|
}
|
||||||
|
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
|
||||||
|
printf("Listening on %s: %s\n", ipver, ipstr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int prepaddr(char *host, char *port, struct addrinfo **ainfo)
|
int prepaddr(char *host, char *port, struct addrinfo **ainfo)
|
||||||
{
|
{
|
||||||
struct addrinfo hints; // hints to what we want
|
struct addrinfo hints; // hints to what we want
|
||||||
memset(&hints, 0, sizeof hints);
|
memset(&hints, 0, sizeof hints);
|
||||||
hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
|
hints.ai_family = AF_INET6; // AF_INET or AF_INET6 to force version
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
hints.ai_flags = AI_PASSIVE;
|
hints.ai_flags = AI_PASSIVE;
|
||||||
|
|
||||||
|
@ -34,31 +54,14 @@ int prepaddr(char *host, char *port, struct addrinfo **ainfo)
|
||||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printaddr(*ainfo);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void printaddr(struct addrinfo *ainfo)
|
|
||||||
{
|
|
||||||
|
|
||||||
char ipstr[INET6_ADDRSTRLEN]; // will hold the IP adress, humanreadable
|
|
||||||
void *addr;
|
|
||||||
char *ipver;
|
|
||||||
if (ainfo->ai_family == AF_INET) { // IPv4
|
|
||||||
struct sockaddr_in *ipv4 = (struct sockaddr_in *) ainfo->ai_addr;
|
|
||||||
addr = &(ipv4->sin_addr);
|
|
||||||
ipver = "IPv4";
|
|
||||||
} else { // IPv6
|
|
||||||
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *) ainfo->ai_addr;
|
|
||||||
addr = &(ipv6->sin6_addr);
|
|
||||||
ipver = "IPv6";
|
|
||||||
}
|
|
||||||
inet_ntop(ainfo->ai_family, addr, ipstr, sizeof ipstr);
|
|
||||||
printf("Listening on %s: %s\n", ipver, ipstr);
|
|
||||||
}
|
|
||||||
|
|
||||||
int prepsocket(struct addrinfo *ainfo)
|
int prepsocket(struct addrinfo *ainfo)
|
||||||
{
|
{
|
||||||
|
|
||||||
sockfd = socket(ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
|
sockfd = socket(ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
|
||||||
|
|
||||||
if (sockfd == -1) {
|
if (sockfd == -1) {
|
||||||
|
@ -72,6 +75,12 @@ int prepsocket(struct addrinfo *ainfo)
|
||||||
cleanup(0);
|
cleanup(0);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
// yes means no!
|
||||||
|
//if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof yes) == -1) {
|
||||||
|
// perror("setsockopt");
|
||||||
|
// cleanup(0);
|
||||||
|
// return -1;
|
||||||
|
//}
|
||||||
|
|
||||||
if (bind(sockfd, ainfo->ai_addr, ainfo->ai_addrlen) != 0) {
|
if (bind(sockfd, ainfo->ai_addr, ainfo->ai_addrlen) != 0) {
|
||||||
perror("bind");
|
perror("bind");
|
||||||
|
@ -229,7 +238,7 @@ int server()
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
char* host = "localhost";
|
char* host = NULL;
|
||||||
char* port = "5000";
|
char* port = "5000";
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
|
@ -242,8 +251,6 @@ int main(int argc, char *argv[])
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
printaddr(ainfo);
|
|
||||||
|
|
||||||
if ((sockfd = prepsocket(ainfo)) == -1) {
|
if ((sockfd = prepsocket(ainfo)) == -1) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue