Advertisement
Guest User

Untitled

a guest
Nov 19th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. *
  2.    Copyright (C) 2000  Daniel Ryde
  3.  
  4.    This library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Lesser General Public
  6.    License as published by the Free Software Foundation; either
  7.    version 2.1 of the License, or (at your option) any later version.
  8.  
  9.    This library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Lesser General Public License for more details.
  13. */
  14.  
  15. /*
  16.    LD_PRELOAD library to make bind and connect to use a virtual
  17.    IP address as localaddress. Specified via the enviroment
  18.    variable BIND_ADDR.
  19.  
  20.    Compile on Linux with:
  21.    gcc -nostartfiles -fpic -shared bind.c -o bind.so -ldl -D_GNU_SOURCE
  22.  
  23.  
  24.    Example in bash to make inetd only listen to the localhost
  25.    lo interface, thus disabling remote connections and only
  26.    enable to/from localhost:
  27.  
  28.    BIND_ADDR="127.0.0.1" LD_PRELOAD=./bind.so /sbin/inetd
  29.  
  30.  
  31.    Example in bash to use your virtual IP as your outgoing
  32.    sourceaddress for ircII:
  33.  
  34.    BIND_ADDR="your-virt-ip" LD_PRELOAD=./bind.so ircII
  35.  
  36.    Note that you have to set up your servers virtual IP first.
  37.  
  38.  
  39.    This program was made by Daniel Ryde
  40.    email: [email protected]
  41.    web:   http://www.ryde.net/
  42.  
  43.    TODO: I would like to extend it to the accept calls too, like a
  44.    general tcp-wrapper. Also like an junkbuster for web-banners.
  45.    For libc5 you need to replace socklen_t with int.
  46. */
  47.  
  48.  
  49.  
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <sys/types.h>
  53. #include <sys/socket.h>
  54. #include <netinet/in.h>
  55. #include <dlfcn.h>
  56. #include <errno.h>
  57.  
  58. int (*real_bind)(int, const struct sockaddr *, socklen_t);
  59. int (*real_connect)(int, const struct sockaddr *, socklen_t);
  60.  
  61. char *bind_addr_env;
  62. unsigned long int bind_addr_saddr;
  63. unsigned long int inaddr_any_saddr;
  64. struct sockaddr_in local_sockaddr_in[] = { 0 };
  65.  
  66. void _init (void)
  67. {
  68.         const char *err;
  69.  
  70.         real_bind = dlsym (RTLD_NEXT, "bind");
  71.         if ((err = dlerror ()) != NULL) {
  72.                 fprintf (stderr, "dlsym (bind): %s\n", err);
  73.         }
  74.  
  75.         real_connect = dlsym (RTLD_NEXT, "connect");
  76.         if ((err = dlerror ()) != NULL) {
  77.                 fprintf (stderr, "dlsym (connect): %s\n", err);
  78.         }
  79.  
  80.         inaddr_any_saddr = htonl (INADDR_ANY);
  81.         if (bind_addr_env = getenv ("BIND_ADDR")) {
  82.                 bind_addr_saddr = inet_addr (bind_addr_env);
  83.                 local_sockaddr_in->sin_family = AF_INET;
  84.                 local_sockaddr_in->sin_addr.s_addr = bind_addr_saddr;
  85.                 local_sockaddr_in->sin_port = htons (0);
  86.         }
  87. }
  88.  
  89. int bind (int fd, const struct sockaddr *sk, socklen_t sl)
  90. {
  91.         static struct sockaddr_in *lsk_in;
  92.  
  93.         lsk_in = (struct sockaddr_in *)sk;
  94. /*      printf("bind: %d %s:%d\n", fd, inet_ntoa (lsk_in->sin_addr.s_addr),
  95.                 ntohs (lsk_in->sin_port));*/
  96.         if ((lsk_in->sin_family == AF_INET)
  97.                 && (lsk_in->sin_addr.s_addr == inaddr_any_saddr)
  98.                 && (bind_addr_env)) {
  99.                 lsk_in->sin_addr.s_addr = bind_addr_saddr;
  100.         }
  101.         return real_bind (fd, sk, sl);
  102. }
  103.  
  104. int connect (int fd, const struct sockaddr *sk, socklen_t sl)
  105. {
  106.         static struct sockaddr_in *rsk_in;
  107.  
  108.         rsk_in = (struct sockaddr_in *)sk;
  109. /*      printf("connect: %d %s:%d\n", fd, inet_ntoa (rsk_in->sin_addr.s_addr),
  110.                 ntohs (rsk_in->sin_port));*/
  111.         if ((rsk_in->sin_family == AF_INET)
  112.                 && (bind_addr_env)) {
  113.                 real_bind (fd, (struct sockaddr *)local_sockaddr_in, sizeof (struct sockaddr));
  114.         }
  115.         return real_connect (fd, sk, sl);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement