3

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.

4

3 に答える 3

8

引数の順序を に逆にすると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. 2 つのエンドポイント間でデータを転送し、終了します。

逆に書くと

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. 接続が確立されると、2 つのアドレス間でデータを転送するワーカーの子を生成します。
  5. 親は引き続き 2 番目のアドレスで接続を受け入れますが、最初のアドレスは使用できなくなります。最初のアドレスは最初の子に与えられたものです。したがって、この時点から何も有用なことはできません。
于 2009-05-21T21:29:49.803 に答える
0

はい、Perl でこれを行うことができます。

perlipc 、IO::SelectIO::Socket、およびBeej's Guide to Network Programming を見てください

于 2009-05-21T17:17:26.193 に答える
0

POE での実行を検討することをお勧めします。これは、イベントを処理するための非同期ライブラリであるため、このタスクには最適のようです。

100% の関連性はありませんが、私は POE を使用して、ステートレス プロトコル (HTTP) とステートフル プロトコル (telnet セッション、より具体的には MUD セッション) の間のプロキシを作成しました。コードはこちらで確認できます: http ://www.depesz.com/index.php/2009/04/08/learning-poe-http-2-mud-proxy/ .

コメントで、誰かが Coro/AnyEvent も提案しました - 私はまだ試していませんが、チェックしてみてください。

于 2009-05-21T19:15:10.387 に答える