summaryrefslogtreecommitdiff
path: root/overlays/libdscp/src/libdscp.c
blob: 1bc9a16dc9e21d039da025d97d4d9c595ee3fa70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <arpa/inet.h>

void _init(void);
int (*sys_connect)(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

int dscp_val = 0;

void _init(void) {
  const char* err;

  char* env_dscp;

  env_dscp = getenv("DSCP");

  if (env_dscp)
    dscp_val = atoi(env_dscp) << 2;

  sys_connect = dlsym(RTLD_NEXT, "connect");

  err = dlerror();
  if (err != NULL)
    (void)fprintf(stderr, "libdscp:dlsym (connect): %s\n", err);
}

int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
  int oerrno = errno;

  setsockopt(sockfd, IPPROTO_IP, IP_TOS, &dscp_val, sizeof dscp_val);
  setsockopt(sockfd, IPPROTO_IPV6, IPV6_TCLASS, &dscp_val, sizeof dscp_val);

  errno = oerrno;
  return sys_connect(sockfd, addr, addrlen);
}