File: redirect_bind.c

package info (click to toggle)
lizardfs 3.12.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,064 kB
  • sloc: cpp: 91,899; sh: 9,341; python: 3,878; ansic: 3,109; pascal: 128; makefile: 57
file content (26 lines) | stat: -rw-r--r-- 699 bytes parent folder | download | duplicates (3)
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
#define _GNU_SOURCE
#include <dlfcn.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>

int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
	static int (*_bind)(int, const struct sockaddr*, socklen_t) = NULL;
	struct sockaddr_in addr_in = * (const struct sockaddr_in*) addr;
	int type;
	socklen_t length = sizeof(type);
	getsockopt(sockfd, SOL_SOCKET, SO_TYPE, &type, &length);
	if (type == SOCK_STREAM) {
		int port = ntohs(addr_in.sin_port);
		if (port) {
			port += 1000;
			addr_in.sin_port = htons(port);
		}
	}
	if (!_bind) {
		_bind = dlsym(RTLD_NEXT, "bind");
	}
	return _bind(sockfd, (const struct sockaddr*) &addr_in, addrlen);
}