0

I have application which uses sockets to transfer data between two clients. It uses a single socket to communicate control and data traffic (over UDP).

Qos and tos fields of IP header can be changed using

setsockopt(sockfd, IPPROTO_IP, IP_TOS,  &tos, toslen);
setsockopt(sockfd, SOL_SOCKET, SO_PRIORITY, &cos, coslen);

But how many calls to setsockopt (to the same socket) is too many? For example, lets assume it will be called every 1ms.

To narrow question scope, I am asking about modern linux system (generic explanation is more than welcomed).

Here is an example to demonstrate it (this is the sending-only part of the application):

#include <stdlib.h>
#include <memory.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

#define NPACK 10000
#define PORT 44444
#define BUFLEN 128

void diep(char *s) {
    perror(s);
    exit(1);
}

#define SRV_IP "12.34.56.78"

int main(void) {
    struct sockaddr_in si_other, si_me;
    int s, i, slen = sizeof(si_other);
    int toss[2] = { 56, 160 }, coss[] = { 1, 3 };
    char buf[BUFLEN];

    //Create UDP socket
    if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
        diep("socket");

    //Create remote socket
    memset((char *) &si_other, 0, sizeof(si_other));
    si_other.sin_family = AF_INET;
    si_other.sin_port = htons(PORT);
    if (inet_aton(SRV_IP, &si_other.sin_addr) == 0) {
        fprintf(stderr, "inet_aton() failed\n");
        exit(1);
    }

    //Create local socket and bind to it.
    memset((char *) &si_me, 0, sizeof(si_me));
    si_me.sin_family = AF_INET;
    si_me.sin_port = htons(PORT);
    si_me.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(s, &si_me, sizeof(si_me)) == -1)
        diep("bind");

    //Loop on number of packets
    for (i = 0; i < NPACK; i++) {
        sprintf(buf, "This is packet %d, %d\n", i, toss[i % 2]);
        printf("Sending packet %d. %s", i, buf);

        //Change tos and cos. odd packets high priority , even packets low priority.
        setsockopt(s, IPPROTO_IP, IP_TOS, &toss[i % 2], sizeof(int));
        setsockopt(s, SOL_SOCKET, SO_PRIORITY, &coss[i % 2], sizeof(int));

        //Send!
        if (sendto(s, buf, strlen(buf), 0, &si_other, slen) == -1)
            diep("sendto()");
    }

    close(s);
    return 0;
}

NOTES:

  • Both control and data should share the same socket (same UDP source port).
  • Multiple threads will use the same socket (so some locking mechanism needed between setsockopt and sendto; but this is outside the scope of the question).
  • SO_PRIORITY is linux only.
ItsMe
  • 363
  • 2
  • 13

0 Answers0