How to set a flow label in IPv6 TCP server

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

 



Hi,

I am tring to set a flow label in IPv6 TCP message which TCP server send.
I could set a flow label in IPv6 TCP message which TCP client send.
Does anyone know how to set a flow label in IPv6 TCP server?

There seems to be no documentation available about IPv6 flow lable in Linux.

Attached you can find my simple TCP server program, which does not compile.
Of course I don't expect you to debug my code,
but I suppose it may be helpful.

Regards,
Kengo

// tcpserver6.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define BACKLOG 5
#define PORT "12345"

int tcp_listen(const char* service) {
    int err;
    struct addrinfo hints;
    struct addrinfo* res = NULL;
    struct addrinfo* ai;
    int sockfd;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET6;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;

    err = getaddrinfo(NULL, service, &hints, &res);
    if (err != 0) {
        printf("getaddrinfo(): %s\n", gai_strerror(err));
        return -1;
    }

    ai = res;
    sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    if (sockfd < 0)
        return -1;

    if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)
        return -1;

    if (listen(sockfd, BACKLOG) < 0)
        return -1;

    freeaddrinfo(res);
    return sockfd;
}

int main() {
    int sockfd;

    sockfd = tcp_listen(PORT);
    if (sockfd < 0) {
        perror("server");
        exit(1);
    }

    printf("wait...\n");

    while (1) {
        int cs;
        struct sockaddr_storage sa;
        socklen_t len = sizeof(sa);
        cs = accept(sockfd, (struct sockaddr*) &sa, &len);
        if (cs < 0) {
            if (errno == EINTR)
                continue;
            perror("accept");
            exit(1);
        }

        printf("accepted.\n");

        char ch;

        (void)set_flow_label(cs, &sa);
        read(cs, &ch, 1);
        write(cs, &ch, 1); // I expect that the flowlabel is set to the packet.

        close(cs);
    }
    return 0;
}
// tcpserver6.c end

// flowlabel.c
#include <sys/socket.h>
#include <linux/in.h>
#include <linux/in6.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

int set_flow_label(int sock, struct sockaddr_in6 *addr)
{
    char freq_buf[sizeof(struct in6_flowlabel_req)];
    struct in6_flowlabel_req *freq = (struct in6_flowlabel_req *)freq_buf;
    int freq_len = sizeof(*freq);
    int on = 1;

    if (setsockopt(sock, IPPROTO_IPV6, IPV6_FLOWINFO_SEND,
               &on, sizeof(on)) == -1) {
        printf("setsockopt: %s\n", strerror(errno));
        return 1;
    }

    memset(freq, 0, sizeof(*freq));
    freq->flr_label = 0;
    freq->flr_action = IPV6_FL_A_GET;
    freq->flr_flags = IPV6_FL_F_CREATE;
    freq->flr_share = IPV6_FL_S_EXCL;
    memcpy(&freq->flr_dst, &addr->sin6_addr, 16);
    if (setsockopt(sock, IPPROTO_IPV6, IPV6_FLOWLABEL_MGR, freq, freq_len)
            == -1) {
        printf("setsockopt: %s\n", strerror(errno));
        return 1;
    }
    return 0;
}
// flowlabel.c end
--
To unsubscribe from this list: send the line "unsubscribe linux-net" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux 802.1Q VLAN]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Git]     [Bugtraq]     [Yosemite News and Information]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux PCI]     [Linux Admin]     [Samba]

  Powered by Linux