如何将本地 unix 套接字映射到 inet 套接字?

发布于 2024-07-22 19:20:13 字数 632 浏览 4 评论 0原文

我很好奇是否可以将 UNIX 套接字映射到 INET 套接字。 情况很简单,我想连接到 MySQL 服务器。 不幸的是它禁用了 INET 套接字,因此我只能使用 UNIX 套接字进行连接。 我正在使用/编写的工具必须连接到 INET 套接字,因此我试图看看是否可以将一个工具映射到另一个工具。

我花了相当多的搜索时间,但我确实找到了 socat ,据说它做了我的事情我在寻找。 我想知道是否有人对如何实现这一目标有任何建议。 我一直在使用的命令行(部分成功)是:

socat -v UNIX-CONNECT:/var/lib/mysql/mysql.sock TCP-LISTEN:6666,reuseaddr

现在我可以建立连接并与服务器交谈。 不幸的是,任何建立多个连接的尝试都会失败,因为我需要使用 fork 选项,但此选项似乎会使连接不起作用。

我知道我可以使用 Perl(我的首选语言)解决这个问题,但我宁愿避免自己编写整个实现。 我熟悉 IO::Socket 库,我只是希望任何人都有做这类事情的经验。 接受建议/想法。

谢谢。

I'm curious if it is possible to map a UNIX socket on to an INET socket. The situation is simply that I'd like to connect to a MySQL server. Unfortunately it has INET sockets disabled and therefore I can only connect with UNIX sockets. The tools I'm using/writing have to connect on an INET socket, so I'm trying to see if I can map one on to the other.

It took a fair amount of searching but I did find socat, which purportedly does what I'm looking for. I was wondering if anyone has any suggestions on how to accomplish this. The command-line I've been using (with partial success) is:

socat -v UNIX-CONNECT:/var/lib/mysql/mysql.sock TCP-LISTEN:6666,reuseaddr

Now I can make connections and talk to the server. Unfortunately any attempts at making multiple connections fail as I need to use the fork option but this option seems to render the connections nonfunctional.

I know I can tackle the issue with Perl (my preferred language), but I'd rather avoid writing the entire implementation myself. I familiar with the IO::Socket libraries, I am simply hoping anyone has experience doing this sort of thing. Open to suggestions/ideas.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾其所爱 2024-07-29 19:20:13

将参数的顺序颠倒到 socat 即可,这样就可以了。

socat -v tcp-l:6666,reuseaddr,fork unix:/var/lib/mysql/mysql.sock

这指示 socat 侦听

  1. TCP 端口 6666(使用 SO_REUSEADDR
  2. 等待接受连接
  3. 当建立连接时,分叉。 在孩子中,继续执行以下步骤。 在父级中,转到 2.
  4. 打开到 /var/lib/mysql/mysql.sock 套接字的 UNIX 域连接。
  5. 在两个端点之间传输数据,然后退出。

以相反的方式编写它

socat -v unix:/var/lib/mysql/mysql.sock tcp-l:6666,reuseaddr,fork

是行不通的,因为这指示socat

  1. 打开到/var/lib/mysql/mysql.sock套接字的UNIX域连接。
  2. 侦听 TCP 端口 6666(使用 SO_REUSEADDR)
  3. 等待接受连接
  4. 建立连接后,生成一个工作子级以在两个地址之间传输数据。
  5. 父级继续接受第二个地址上的连接,但不再拥有可用的第一个地址:它已提供给第一个子级。 所以从现在开始就不能做任何有用的事情了。

Reverse the order of your arguments to socat, and it works.

socat -v tcp-l:6666,reuseaddr,fork unix:/var/lib/mysql/mysql.sock

This instructs socat to

  1. Listen on TCP port 6666 (with SO_REUSEADDR)
  2. Wait to accept a connection
  3. When a connection is made, fork. In the child, continue the steps below. In the parent, go to 2.
  4. Open a UNIX domain connection to the /var/lib/mysql/mysql.sock socket.
  5. Transfer data between the two endpoints, then exit.

Writing it the other way around

socat -v unix:/var/lib/mysql/mysql.sock tcp-l:6666,reuseaddr,fork

doesn't work, because this instructs socat to

  1. Open a UNIX domain connection to the /var/lib/mysql/mysql.sock socket.
  2. Listen on TCP port 6666 (with SO_REUSEADDR)
  3. Wait to accept a connection
  4. When a connection is made, spawn a worker child to transfer data between the two addresses.
  5. The parent continues to accept connections on the second address, but no longer has the first address available: it was given to the first child. So nothing useful can be done from this point on.
昇り龍 2024-07-29 19:20:13

是的,您可以在 Perl 中做到这一点。

看看perlipcIO::Select, IO ::SocketBeej 网络编程指南

Yes, you can do this in Perl.

Look at perlipc, IO::Select, IO::Socket and Beej's Guide to Network Programming.

笑看君怀她人 2024-07-29 19:20:13

您可能需要考虑在 POE 中执行此操作 - 它是用于处理事件的异步库,因此它看起来非常适合该任务。

它不是 100% 相关,但我使用 POE 在无状态协议(HTTP)和有状态协议(telnet 会话,更具体地说 - MUD 会话)之间编写代理,并且它相当简单 - 您可以在此处查看代码:< a href="http://www.depesz.com/index.php/2009/04/08/learning-poe-http-2-mud-proxy/" rel="nofollow noreferrer">http://www.depesz.com/index.php/2009/04/08/learning-poe-http-2-mud-proxy/" depesz.com/index.php/2009/04/08/learning-poe-http-2-mud-proxy/。

在评论中,有人还建议了 Coro/AnyEvent - 我还没有玩过它,但你可能想检查一下。

You might want to consider doing it in POE - it's asynchronous library for dealing with events, so it looks like great for the task.

It is not 100% relevant, but I use POE to write proxy between stateless protocol (HTTP) and statefull protocol (telnet session, and more specifically - MUD session), and it was rather simple - You can check the code in here: http://www.depesz.com/index.php/2009/04/08/learning-poe-http-2-mud-proxy/.

In the comments somebody also suggested Coro/AnyEvent - I haven't played with it yet, but you might want to check it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文