/force_bind.c (1e9bdd93f2381f63b35ac82a345dc8dff167e17d) (2350 bytes) (mode 100644) (type blob)
/*
* Description: Force bind on a specified address
* Author: Catalin(ux) M. BOIE
* E-mail: catab at embedromix dot ro
* Web: http://kernel.embedromix.ro/us/
*/
#define __USE_GNU
#define _GNU_SOURCE
#define __USE_XOPEN2K
#define __USE_LARGEFILE64
#define __USE_FILE_OFFSET64
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <dirent.h>
#include <asm/unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
static int (*old_bind)(int sockfd, const struct sockaddr *addr, socklen_t addrlen) = NULL;
static char *force_address = NULL;
static int force_port = -1;
/* Functions */
void init(void)
{
static unsigned char inited = 0;
char *x;
if (inited == 1)
return;
inited = 1;
x = getenv("FORCE_BIND_ADDRESS");
if (x != NULL)
force_address = x;
x = getenv("FORCE_BIND_PORT");
if (x != NULL)
force_port = strtol(x, NULL, 10);
syslog(LOG_INFO, "force_bind: Force bind to %s/%d.\n",
force_address, force_port);
old_bind = dlsym(RTLD_NEXT, "bind");
if (old_bind == NULL) {
syslog(LOG_ERR, "force_bind: Cannot resolve 'bind'!\n");
exit(1);
}
}
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
int err;
struct sockaddr new;
struct sockaddr_in *sa4;
struct sockaddr_in6 *sa6;
void *p;
unsigned short *pport;
init();
if ((addr->sa_family != AF_INET) && (addr->sa_family != AF_INET6)) {
syslog(LOG_INFO, "force_bind: unsupported family=%u!\n",
addr->sa_family);
return old_bind(sockfd, addr, addrlen);
}
memcpy(&new, addr, sizeof(struct sockaddr));
switch (new.sa_family) {
case AF_INET:
sa4 = (struct sockaddr_in *) &new;
p = &sa4->sin_addr;
pport = &sa4->sin_port;
break;
case AF_INET6:
sa6 = (struct sockaddr_in6 *) &new;
p = &sa6->sin6_addr.s6_addr;
pport = &sa6->sin6_port;
break;
}
if (force_address != NULL) {
err = inet_pton(new.sa_family, force_address, p);
if (err != 1) {
syslog(LOG_INFO, "force_bind: cannot convert [%s] (%d)!\n",
force_address, err);
return old_bind(sockfd, addr, addrlen);
}
}
if (force_port != -1)
*pport = htons(force_port);
return old_bind(sockfd, &new, addrlen);
}
Mode |
Type |
Size |
Ref |
File |
100644 |
blob |
43 |
9108d94cb8e6f127c99b7099975be00532899907 |
.gitignore |
100644 |
blob |
30 |
d987fa5df957830331139935d517009e2911b0cf |
INSTALL |
100644 |
blob |
35147 |
94a9ed024d3859793618152ea559a168bbcbb5e2 |
LICENSE |
100644 |
blob |
597 |
603959b1ef1649b49250ce692eabf9d7e47bcc00 |
Makefile.in |
100644 |
blob |
606 |
06e92472969ddaeaec5be3f55a8a47219252159c |
README |
100644 |
blob |
4 |
e58503ce9f92926671d80cf3d5cf3c842d9ab4d9 |
TODO |
100755 |
blob |
23 |
d33bb6c4ecdce1390ce1db3c79ea3b93e22ea755 |
configure |
100755 |
blob |
12258 |
458452cb19a5320f307e81b126734bdc54cb2490 |
duilder |
100644 |
blob |
309 |
dc770d3c8778ecc463ae5fb9a5bb2f115394e01a |
duilder.conf |
100755 |
blob |
514 |
a64be06e8246b4b7fe5e3bfe60b8db01fb64bd2e |
duilder_release |
100644 |
blob |
2350 |
1e9bdd93f2381f63b35ac82a345dc8dff167e17d |
force_bind.c |
100644 |
blob |
804 |
b7ef7798c25d781fc2c7790aa80f8f431703e781 |
force_bind.spec.in |
100755 |
blob |
165 |
83d21d25e27dc624569788a2467ff66002d3b4a3 |
test1.sh |
100644 |
blob |
1008 |
117d3cf0dad27a8e05566d07968a246f6f8b8b0e |
test_bind.c |
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"
Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/catalinux/force_bind
Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/catalinux/force_bind
Clone this repository using git:
git clone git://git.rocketgit.com/user/catalinux/force_bind
You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a
merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main