Solaris - 我的进程监听所有接口,但其他进程监听 localhost:6011
我的服务器程序被设计为侦听所有接口,并且它也在侦听工作。
一种奇怪的行为正在发生。说明端口与sshd进程冲突。因为我需要使我的应用程序和 sshd 侦听相同的端口范围。(例如:6000 - 6100)
MyProg 代码:
MyPort = 6011;
#if TARGET_HAS_IPV6_SUPPORT
/* IPv6 socket */
{
struct addrinfo *ai = NULL;
r = ipw_getaddrinfo("::", NULL, AF_INET6, SOCK_STREAM, IPPROTO_TCP, AI_PASSIVE, &ai);
if (r == 0)
{
Create_listen_socket(&ai, IpcMyPort);
}
}
#endif
{
struct addrinfo ailocal = { 0 };
struct sockaddr_in sin = { 0 };
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
ailocal.ai_family = AF_INET;
ailocal.ai_socktype = SOCK_STREAM;
ailocal.ai_protocol = IPPROTO_TCP;
ailocal.ai_flags = AI_PASSIVE;
ailocal.ai_addrlen = sizeof(struct sockaddr_in);
ailocal.ai_addr = (struct sockaddr *)&sin;
Create_listen_socket(&ailocal, MyPort);
}
Create_listen_socket(struct addrinfo *ai,
unsigned int MyPort)
{
SOCKET fd = INVALID_SOCKET;
while (ai != NULL)
{
fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (fd == INVALID_SOCKET)
{
Exit()
break;
}
switch (switch (sa->sa_family)
{
case AF_INET:
{
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
sin->sin_port = htons((unsigned short)port);
}
break;
#if TARGET_HAS_IPV6_SUPPORT
case AF_INET6:
{
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
sin6->sin6_port = htons((unsigned short)port);
}
break;
#endif
}
if (ipw_bind(fd, ai->ai_addr, ai->ai_addrlen) < 0)
{
exit();
Break;
}
else
{
r = ipw_listen(fd, IPC_MAX_LISTEN_COUNT);
}
ai = ai->ai_next;
}
}
以下是“sudo lsof -i -P | grep :60”的输出
:: :: ::
sshd 23038 fin22495 11u IPv4 0x60026f7c740 0t0 TCP 本地主机:6011(听)
myProg 23108 root 4u IPv6 0x60026e46000 0t0 TCP *:6011 (听)
myProg 23108 root 5u IPv4 0x6002658e3c0 0t0 TCP *:6011 (侦听)
如果您看到,sshd 守护进程正在侦听同一端口 6011,但在本地主机接口上。但我的程序正在监听 *.6011。
如何确保 myProg 与 *:6011 一起侦听“localhost:6011”,以及如果已经有某个进程正在侦听“localhost:6011”如何检测它。它发生在 Solaris 中。
请告诉我您的看法。
谢谢, 娜迦
My server program is designed to listen on all the interfaces and it is listening working also.
One strange behavior is happening. It means port conflict with sshd process. Because I need to make my application and sshd to listen on same port range.(ex: 6000 - 6100)
MyProg code:
MyPort = 6011;
#if TARGET_HAS_IPV6_SUPPORT
/* IPv6 socket */
{
struct addrinfo *ai = NULL;
r = ipw_getaddrinfo("::", NULL, AF_INET6, SOCK_STREAM, IPPROTO_TCP, AI_PASSIVE, &ai);
if (r == 0)
{
Create_listen_socket(&ai, IpcMyPort);
}
}
#endif
{
struct addrinfo ailocal = { 0 };
struct sockaddr_in sin = { 0 };
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
ailocal.ai_family = AF_INET;
ailocal.ai_socktype = SOCK_STREAM;
ailocal.ai_protocol = IPPROTO_TCP;
ailocal.ai_flags = AI_PASSIVE;
ailocal.ai_addrlen = sizeof(struct sockaddr_in);
ailocal.ai_addr = (struct sockaddr *)&sin;
Create_listen_socket(&ailocal, MyPort);
}
Create_listen_socket(struct addrinfo *ai,
unsigned int MyPort)
{
SOCKET fd = INVALID_SOCKET;
while (ai != NULL)
{
fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (fd == INVALID_SOCKET)
{
Exit()
break;
}
switch (switch (sa->sa_family)
{
case AF_INET:
{
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
sin->sin_port = htons((unsigned short)port);
}
break;
#if TARGET_HAS_IPV6_SUPPORT
case AF_INET6:
{
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
sin6->sin6_port = htons((unsigned short)port);
}
break;
#endif
}
if (ipw_bind(fd, ai->ai_addr, ai->ai_addrlen) < 0)
{
exit();
Break;
}
else
{
r = ipw_listen(fd, IPC_MAX_LISTEN_COUNT);
}
ai = ai->ai_next;
}
}
Following is the output of "sudo lsof -i -P | grep :60"
:: :: ::
sshd 23038 fin22495 11u IPv4 0x60026f7c740 0t0 TCP
localhost:6011 (LISTEN)
myProg 23108 root 4u IPv6 0x60026e46000 0t0 TCP *:6011
(LISTEN)
myProg 23108 root 5u IPv4 0x6002658e3c0 0t0 TCP *:6011
(LISTEN)
If you see, the sshd daemon is listening on same port 6011 but on local host interface. but my program is listening on *.6011.
How to make sure myProg listening on "localhost:6011" along with *:6011 and if already some process is listening on "localhost:6011" how to detect it. It happens in Solaris.
Please let me know your views.
Thanks,
Naga
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定你在这里做了什么,因为 sshd 通常应该侦听端口 22,而不是 6011。第一个打开该端口的进程将获得独占访问权限。造成这种情况有充分的、与安全相关的原因。有关详细信息,请参见 tcp(7P) 联机帮助页 - 查找 SO_REUSEADDR 和 SO_EXECLBIND。
I'm not sure what you have done here because sshd should normally be listening on port 22, not 6011. The first process opening the port will get exclusive access. There are good, security-related reasons for this being so. See the tcp(7P) manpage for details - look for SO_REUSEADDR and SO_EXECLBIND.
您的应用程序的端口号与 ssh X 转发使用的端口冲突。更改您的端口号并完成此操作。
Your application's port numbers conflict with ports used by ssh X-forwarding. Change your port numbers and be done with that.