tech-net archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

code for RFC 6056 - test program for IPv6



#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <netdb.h>

#define UDP_RFC6056ALGO 200

#define AF AF_INET6
#define DOMAIN PF_INET6
#define SOCKADDR sockaddr_in6
#define PORT sin6_port
#define FAMILY sin6_family

int
main(int argc, char **argv)
{
        int sockfd, sockfd2, error;
        struct SOCKADDR localaddr;
        struct addrinfo hints, *res;
        int i = 0;
        
        
        if (argc < 2) {
                printf("usage test1 hostname [algorithm] \n");
                return EINVAL;
        }
        if (argc > 2) {
                i = atoi(argv[2]);
                printf("Using algorithm %d \n", i);
        }
        
        bzero(&localaddr, sizeof(localaddr));
        localaddr.FAMILY = AF;
        
        memset(&hints, 0, sizeof(struct addrinfo));
        hints.ai_family = AF;
        hints.ai_socktype = SOCK_DGRAM;

        error =getaddrinfo(argv[1], "http", &hints, &res);
        if (error) printf("getaddrinfo error %d\n", error);
        
        printf("First we try with the delayed bind\n");

        sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
        if (socket < 0) printf("socket call failed");
        
        if (i > 0) {
                int error =setsockopt(sockfd, IPPROTO_UDP,
                                       UDP_RFC6056ALGO, &i, sizeof(int));
                if (error) printf("setsockopt error %d\n", error);
        }
        error =bind(sockfd, (struct sockaddr *) & localaddr,
                     sizeof(localaddr));
        
        if (error) printf("bind returned %d \n", error);

        error =sendto(sockfd, "hello\n", strlen("hello\n"), 0,
            res->ai_addr, res->ai_addrlen);
        printf("sendto returned %d \n", error);

        error = close(sockfd);
        if (error) printf("close returned %d \n", error);

        printf("Secondly, we connect first and send directly \n");

        sockfd2 = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
        if (socket < 0) printf("socket call failed");

        if (i > 0) {
                int error =setsockopt(sockfd2, IPPROTO_UDP,
                                       UDP_RFC6056ALGO, &i, sizeof(int));
                if (error)
                        printf("setsockopt error %d\n", error);
        }
        error =connect(sockfd2, res->ai_addr,
            res->ai_addrlen);
        if (error) printf("connect returned %d \n", error);

        send(sockfd2, "hello\n", strlen("hello\n"), 0);
        printf("send returned %d \n", error);

        error =close(sockfd2);
        if (error) printf("close returned %d \n", error);

        freeaddrinfo(res);
        
        printf("Done with testing... \n");

        return 0;
}


Home | Main Index | Thread Index | Old Index