<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Soul Of Free Loop &#187; bind.so</title>
	<atom:link href="https://zohead.com/archives/tag/bind-so/feed" rel="self" type="application/rss+xml" />
	<link>https://zohead.com</link>
	<description>Uranus Zhou&#039;s Blog</description>
	<lastBuildDate>Sat, 19 Jul 2025 15:42:46 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.8</generator>
	<item>
		<title>使用libc封装库修改程序绑定端口</title>
		<link>https://zohead.com/archives/libc-bind-wrapper/</link>
		<comments>https://zohead.com/archives/libc-bind-wrapper/#comments</comments>
		<pubDate>Mon, 11 Aug 2014 14:15:01 +0000</pubDate>
		<dc:creator><![CDATA[Uranus Zhou]]></dc:creator>
				<category><![CDATA[技术]]></category>
		<category><![CDATA[网络技术]]></category>
		<category><![CDATA[bind.so]]></category>
		<category><![CDATA[libc]]></category>
		<category><![CDATA[socket]]></category>
		<category><![CDATA[wrapper]]></category>
		<category><![CDATA[端口]]></category>
		<category><![CDATA[绑定]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[网络]]></category>

		<guid isPermaLink="false">http://zohead.com/?p=764</guid>
		<description><![CDATA[本文同步自（最佳显示效果请点击）：https://zohead.com/archives/libc-bind-wrapper/ 最近在使用一个第三方程序的时候发现程序绑定的 UDP 端口和现有 Linux 系统中的程序有冲突，系统自带的程序又不好修改端口，而第三方程序更没有源码或者配置文件来指定端口。 这种情况下想到可以用 libc 的封装库自己实现 bind 之类的函数来修改端口号，而网上也找到了 Daniel Ryde 类似的实现： http://www.ryde.net/code/bind.c.txt 编译这个 bind 库可以通过环境变量指定绑定的本地 IP 地址，但不支持端口号修改， [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>本文同步自（最佳显示效果请点击）：<a href="https://zohead.com/archives/libc-bind-wrapper/" target="_blank">https://zohead.com/archives/libc-bind-wrapper/</a></p>
<p>最近在使用一个第三方程序的时候发现程序绑定的 UDP 端口和现有 Linux 系统中的程序有冲突，系统自带的程序又不好修改端口，而第三方程序更没有源码或者配置文件来指定端口。</p>
<p>这种情况下想到可以用 libc 的封装库自己实现 bind 之类的函数来修改端口号，而网上也找到了 Daniel Ryde 类似的实现：</p>
<p><a href="http://www.ryde.net/code/bind.c.txt" target="_blank">http://www.ryde.net/code/bind.c.txt</a></p>
<p>编译这个 bind 库可以通过环境变量指定绑定的本地 IP 地址，但不支持端口号修改，而且是直接修改程序所有绑定主机地址不好过滤定制。因此，我对这个 bind 的封装库做了以下改进：</p>
<ul>
<li>支持 socket 类型过滤，可以指定是 TCP、UDP socket 等；</li>
<li>支持指定是本地监听请求还是连接远程的请求；</li>
<li>支持修改本地使用的 IP 地址及端口号；</li>
<li>支持通过环境变量过滤掉某些端口</li>
</ul>
<p>支持上面这些新增的功能之后，我们就可以根据实际情况只修改特定请求的绑定地址或者绑定端口以满足需求。</p>
<p>我修改的 bind 封装库代码如下，也比较简单：</p>
<pre class="brush: cpp; title: bind.c; notranslate">
/*
   Copyright (C) 2000  Daniel Ryde

   Modified by Uranus Zhou (2014)

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
*/
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;dlfcn.h&gt;
#include &lt;errno.h&gt;
#include &lt;string.h&gt;

int (*real_bind)(int, const struct sockaddr *, socklen_t);
int (*real_connect)(int, const struct sockaddr *, socklen_t);

char *bind_addr_env, *connect_bind_addr_env, *bind_port_env, *bind_type_env, *exclude_ports_env, *connect_port_env, *connect_type_env, *connect_bind_port_env;
unsigned long int bind_addr_saddr;
struct sockaddr_in local_sockaddr_in[] = { 0 };
int bind_port_ns = -1, connect_port_ns = -1;
int bind_type = -1, connect_type = -1;

static int get_sock_type(const char *str)
{
	if (strcasecmp(str, &quot;TCP&quot;) == 0)
		return SOCK_STREAM;
	else if (strcasecmp(str, &quot;UDP&quot;) == 0)
		return SOCK_DGRAM;
	else if (strcasecmp(str, &quot;RAW&quot;) == 0)
		return SOCK_RAW;
	else if (strcasecmp(str, &quot;PACKET&quot;) == 0)
		return SOCK_PACKET;
	else
		return -1;
}

void _init (void)
{
	const char *err;

	real_bind = dlsym (RTLD_NEXT, &quot;bind&quot;);
	if ((err = dlerror ()) != NULL) {
		fprintf (stderr, &quot;dlsym (bind): %s\n&quot;, err);
	}

	real_connect = dlsym (RTLD_NEXT, &quot;connect&quot;);
	if ((err = dlerror ()) != NULL) {
		fprintf (stderr, &quot;dlsym (connect): %s\n&quot;, err);
	}

	if (bind_addr_env = getenv (&quot;BIND_ADDR&quot;))
		bind_addr_saddr = inet_addr (bind_addr_env);

	if (bind_port_env = getenv (&quot;BIND_PORT&quot;))
		bind_port_ns = htons (atoi(bind_port_env));

	if (bind_type_env = getenv (&quot;BIND_TYPE&quot;))
		bind_type = get_sock_type(bind_type_env);

	exclude_ports_env = getenv (&quot;EXCLUDE_PORTS&quot;);

	if (connect_port_env = getenv (&quot;CONNECT_PORT&quot;))
		connect_port_ns = htons (atoi(connect_port_env));

	if (connect_type_env = getenv (&quot;CONNECT_TYPE&quot;))
		connect_type = get_sock_type(connect_type_env);

	if (connect_bind_addr_env = getenv (&quot;CONNECT_BIND_ADDR&quot;)) {
		local_sockaddr_in-&gt;sin_family = AF_INET;
		local_sockaddr_in-&gt;sin_addr.s_addr = inet_addr (connect_bind_addr_env);
		local_sockaddr_in-&gt;sin_port = htons (0);
	}

	if (connect_bind_port_env = getenv (&quot;CONNECT_BIND_PORT&quot;))
		local_sockaddr_in-&gt;sin_port = htons (atoi(connect_bind_port_env));
}

static int check_port(int port)
{
	char *tmp = exclude_ports_env, *str = NULL;
	if (tmp == NULL) return 0;

	while (1) {
		char szPort[50] = {0};
		str = strchr(tmp, ',');
		if (str == NULL)
			strncpy(szPort, tmp, sizeof(szPort));
		else
			strncpy(szPort, tmp, str - tmp);
		if (atoi(szPort) == port) return 1;
		if (str == NULL) break;
		tmp = str + 1;
	}

	return 0;
}

int bind (int fd, const struct sockaddr *sk, socklen_t sl)
{
	static struct sockaddr_in *lsk_in;

	lsk_in = (struct sockaddr_in *)sk;
	if (lsk_in-&gt;sin_family == AF_INET || lsk_in-&gt;sin_family == AF_INET6) {
		int type, length = sizeof(int);

		getsockopt(fd, SOL_SOCKET, SO_TYPE, &amp;type, &amp;length);

		if (check_port(ntohs(lsk_in-&gt;sin_port)) == 0 &amp;&amp; (bind_type_env == NULL || bind_type == type)) {
			// change bind address
			if (lsk_in-&gt;sin_addr.s_addr == htonl (INADDR_ANY) &amp;&amp; bind_addr_env)
				lsk_in-&gt;sin_addr.s_addr = bind_addr_saddr;

			// change bind port
			if (bind_port_env)
				lsk_in-&gt;sin_port = bind_port_ns;
		}
	}
	return real_bind (fd, sk, sl);
}

int connect (int fd, const struct sockaddr *sk, socklen_t sl)
{
	static struct sockaddr_in *rsk_in;

	rsk_in = (struct sockaddr_in *)sk;
	if (rsk_in-&gt;sin_family == AF_INET || rsk_in-&gt;sin_family == AF_INET6) {
		int type, length = sizeof(int);

		getsockopt(fd, SOL_SOCKET, SO_TYPE, &amp;type, &amp;length);

		if ((connect_port_env == NULL || connect_port_ns == rsk_in-&gt;sin_port) &amp;&amp; (connect_type_env == NULL || connect_type == type)) {
			// change connect bind address or bind port
			if (connect_bind_addr_env)
				real_bind (fd, (struct sockaddr *)local_sockaddr_in, sizeof (struct sockaddr));
		}
	}
	return real_connect (fd, sk, sl);
}
</pre>
<p>编译方法：</p>
<pre><strong>gcc -nostartfiles -fpic -shared bind.c -o bind.so -ldl -D_GNU_SOURCE</strong></pre>
<p>运行指定程序时需要通过 LD_PRELOAD 预先加载 bind.so 库，并通过环境变量指定如何过滤修改网络参数：</p>
<ul>
<li>BIND_ADDR 指定本地监听的 IP 地址；</li>
<li>BIND_PORT 指定本地监听的端口；</li>
<li>BIND_TYPE 指定本地监听的 socket 类型（TCP、UDP、RAW、PACKET）；</li>
<li>CONNECT_PORT 指定连接远程主机的端口；</li>
<li>CONNECT_TYPE 指定远程主机 socket 连接类型（与 BIND_TYPE 类似）；</li>
<li>CONNECT_BIND_ADDR 指定连接远程主机时本地使用的 IP 地址；</li>
<li>CONNECT_BIND_PORT 指定连接远程主机时本地使用的端口；</li>
<li>EXCLUDE_PORTS 需要排除的端口列表（以逗号隔开）</li>
</ul>
<p>这里举个例子说明如何使用 bind.so：</p>
<p>假设第三方程序为 testapp，testapp 启动时会本地监听 UDP 775 端口，另外还会在本地 eth0 网卡上监听一个随机的 UDP 端口。现在我们需要将后面这个随机的 UDP 端口固定为 666，而且需要指定使用 eth2 网卡，就可以这样运行 testapp 程序：</p>
<pre><strong>BIND_ADDR="eth2-ip" BIND_PORT=666 BIND_TYPE=UDP EXCLUDE_PORTS=775 LD_PRELOAD=bind.so testapp</strong></pre>
<p>上面的命令中指定了本地 IP 地址（eth2-ip）、绑定端口（666）、socket 类型（UDP），并且使用 EXCLUDE_PORTS 环境变量过滤了固定监听的 775 端口，这样 testapp 程序运行使用的随机 UDP 端口就固定在 666 上了。</p>
<p>我修改的 bind.c 源代码可以从我的 Gist 下载：</p>
<p><a href="https://gist.github.com/zohead/9950663ca01952c940eb" target="_blank">https://gist.github.com/zohead/9950663ca01952c940eb</a></p>
<p>实际使用如果有任何问题欢迎提出指正哦，玩的开心 ^_^</p>
]]></content:encoded>
			<wfw:commentRss>https://zohead.com/archives/libc-bind-wrapper/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
