INADDR_ANY 接口是否包含 VMware 网络适配器?
Does INADDR_ANY interfaces include VMware Network Adapter?
我的代码目前正在使用 INADDR_ANY,我正在测试是否能够通过 Ubuntu 路由器从 VM 客户端发送数据包,[使用 IPTables] 到我的主机机.
我能够从 Linux 路由器后面的客户端和主机的客户端 ping 主机,但是当我发送 UDP 数据包时,它永远不会到达主机。
问题要么在这两个地方之一,我为路由设置的 IPTables 不能正常工作,要么在我绑定到 INADDR_ANY 的服务器代码中,INADDR_ANY不包括 VMware 接口。
我四处寻找,没有找到具体的信息。据我了解,没有理由不工作。
扩展问题:
根据 this 如果我 运行 命令我将能够复制相应的 NAT,但是如果它说 public ip 在这里 和 private ip 在这里 是指目标 IP 还是 public和私有接口IP;即 eth0 和 eth1 的 IP 地址?
已解决
我在错误的 eth 卡上设置了 iptable 规则!因此,请确保您在正确的 eth 上应用命令。
以下代码是UDP连接的客户端。
这适用于 ubuntu linux,VM,windows XP。
注意:左边的代码语句(通常)用于调试
注意:系统函数'sendto()'和'recvfrom()'是UDP通信的正确调用。
注意:函数大小写等根据当地使用的编码风格而定。
注意:与任何 'real life' 项目一样,大部分代码用于处理错误。
您可能对 create_UDP_socket()
函数最感兴趣。
/* *************** Included files************************************* */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h> // for system() and itoa() utilities
#include <stdarg.h> // for va_list(), etc
#include <string.h> // string handling utilities
#include <semaphore.h> // semaphores handling utilities
#include <fcntl.h> // symbolic definitions for flags bits
#include <sys/types.h> // contains definitions of data types used in system calls
#include <sys/socket.h>// type definitions (I.E. struct sockaddr_in)
#include <sys/stat.h> // symbolic definitions for permission bits and open()
#include <sys/ipc.h> // ipc interface utilities
#include <sys/shm.h> // shared memory utilities
#include <arpa/inet.h> // defines INADDR_ANY, etc
#include <errno.h> // for accessing errno and related functions
#include <stdbool.h> // define true, false, bool
#include <time.h>
// contains common data type definitions
#include "localTypes.h"
// contains data definitions for ON and OFF, enum enumReturnStatus
#include "Defines.h"
// contains prototypes for writing to Log file
#include "CommonWriteLog.h"
// contains prototypes, and data type definitions
#include "CommonConfiguration.h"
//contains prototypes for globally visible functions in this file
#include "Common_UDP_Utilities.h"
/* *************** Global variables *********************************** */
/* *************** File Static Variables ****************************** */
/* *************** Code *********************************************** */
enum enumReturnStatus create_UDP_socket( INT32 taskSelector, INT32 *pUDPsocket_FD )
{
enum enumReturnStatus returnStatus = eRS_Success; // indicate success
INT32 LocalPort;
INT32 socket_FD;
struct sockaddr_in *pLocal_sockaddr;
char *txtAddress;
if( is_taskSelector_Valid( taskSelector ) )
{ // then parameters valid
socket_FD = get_UDPsocket_FD( eTask_Retrieve_GPS );
if( 0 >= socket_FD )
{ // then socket not yet opened
pLocal_sockaddr = get_pUDPsocket_sockaddr( taskSelector );
txtAddress = inet_ntoa((*pLocal_sockaddr).sin_addr);
fprintf(stdout, "OPENING SOCKET on socket addresss: %s \n", txtAddress );
if( NULL != pLocal_sockaddr )
{ // then, sockaddr configured
LocalPort = get_UDPsocket_LocalPort( taskSelector );
fprintf(stdout, "OPENING SOCKET on PORT: %d \n", LocalPort );
if( 0 <= LocalPort )
{ // then port configured
socket_FD = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
if( -1 != socket_FD )
{ // then, socket creation successful
int TurnOn = 1;
returnStatus = setsockopt( socket_FD, SOL_SOCKET, SO_REUSEADDR, &TurnOn, sizeof( TurnOn ) );
if ( eRS_Success == returnStatus )
{ // then setsockopt() successful
bzero((char *) pLocal_sockaddr, sizeof(struct sockaddr_in) );
pLocal_sockaddr->sin_family = AF_INET;
pLocal_sockaddr->sin_port = htons( LocalPort );
pLocal_sockaddr->sin_addr.s_addr = htonl(INADDR_ANY); // todo: change to specific GPS IP address
CommonWriteLog( eLL_Info,
"INFO:File:%s: Line:%d:\n\t%s 0X%08X %d\n\n",
__FILE__, __LINE__,
"addr.sin_port =",
pLocal_sockaddr->sin_port,
pLocal_sockaddr->sin_port );
returnStatus = bind( socket_FD, (struct sockaddr *)pLocal_sockaddr, sizeof(struct sockaddr) );
if( eRS_Success == returnStatus )
{ // then bind successful
returnStatus = set_UDPsocket_FD( taskSelector, socket_FD );
if( eRS_Success != returnStatus )
{ // then set_UDPsocket_FD failed
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_string ),
__FILE__, __LINE__,
"Function:set_UDPsocket_FD() failed" );
returnStatus = eRS_UDP_Create_Failure; // indicate error occurred
close( socket_FD );
socket_FD = -1;
}
// set callers data
*pUDPsocket_FD = socket_FD;
}
else
{ // else, bind() failed
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_taskSelector_dataIndex_string_string ),
__FILE__, __LINE__,
taskSelector, 0,
"LibFunc:bind() failed",
strerror(errno) );
} // endif( bind )
}
else
{ // else setsockopt() failed
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_taskSelector_dataIndex_string_string ),
__FILE__, __LINE__,
taskSelector, 0,
"LibFunc:setsockopt( SO_REUSEADDR ) failed",
strerror(errno) );
} // endif( SO_REUSEADDR )
}
else
{ // else failed to create socket
fprintf(stdout, "FAILED TO OPEN SOCKET: \n" );
fflush(stdout);
system("sync;sync;");
exit(EXIT_FAILURE);
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_taskSelector_dataIndex_string_string ),
__FILE__, __LINE__,
taskSelector, 0,
"LibFunc:socket() failed",
strerror(errno) );
returnStatus = eRS_SystemCall_Failure; // indicate error occured
} // endif( socket )
}
else
{ // else port not configured
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_taskSelector_dataIndex_string ),
__FILE__, __LINE__,
taskSelector, 0,
"port not configured" );
returnStatus = eRS_Configuration_Failure; // indicate error occurred
}
}
else
{ // else get pUDP sockaddr failed
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_taskSelector_dataIndex_string ),
__FILE__, __LINE__,
taskSelector, 0,
"Function:get_pUDPsocket_sockaddr() failed" );
returnStatus = eRS_UDP_Create_Failure; // indicate error occurred
} // endif( get_pUDPsocket_sockaddr() successful )
} // endif( socket already open )
}
else
{ // else, bad parameters
returnStatus = eRS_UDP_Create_Failure;
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_digit_string ),
__FILE__, __LINE__,
taskSelector,
"Parameter:taskSelector out of range" );
}
return( returnStatus );
} // end create_UDP_socket()
enum enumReturnStatus write_UDP_socket( INT32 Socket_FD, char *pSocket_OutBuffer, INT32 Size, struct sockaddr *pTo_sockaddr, INT32 *pWriteCount )
{
enum enumReturnStatus returnStatus = eRS_Success; // indicate success
INT32 flags = 0;
INT32 sendtoStatus;
// do not call CommonWriteLog() to help avoid extranious 'recursion' messages in logs
// and to avoid recrusive operation
sendtoStatus =
sendto( Socket_FD, pSocket_OutBuffer, Size, flags,pTo_sockaddr, sizeof(struct sockaddr) );
if( 0 <= sendtoStatus )
{ // then, sendto() successful
*pWriteCount = sendtoStatus;
if( sendtoStatus != Size )
{ // then not all char sent
CommonWriteLog( eLL_Error,
"ERROR:File:%s: Line:%d:\n\t%s:num2Send:%d, numSent:%d\n\n",
__FILE__, __LINE__,
"LibFunc:sendto() failed",
Size, sendtoStatus );
returnStatus = eRS_UDP_Write_Failure; // indicate error occurred
}
}
else
{
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_string_string ),
__FILE__, __LINE__,
"LibFunc:sendto() failed",
strerror(errno) );
returnStatus = eRS_SystemCall_Failure; // indicate error occurred
*pWriteCount = 0;
}
return( returnStatus );
} // end write_UDP_socket()
enum enumReturnStatus read_UDP_socket( INT32 Socket_FD, char *pSocket_InBuffer, INT32 Size, INT32 *pReadCount )
{
enum enumReturnStatus returnStatus = eRS_Success;
INT32 readStatus = 0; // result of call to recvfrom()
INT32 flags = 0; // option parameter for call to recvfrom()
struct sockaddr_in from_sockaddr;
UINT32 slen = 0; // sizeof struct sockaddr_in
memset( &from_sockaddr, 0x00, sizeof(from_sockaddr) );
slen = sizeof( struct sockaddr_in );
readStatus = recvfrom( Socket_FD, pSocket_InBuffer,Size, flags,(struct sockaddr*)&from_sockaddr, &slen ); // filled during call
if( 0 > readStatus )
{ // then, an I/O error occurred
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_string_string ),
__FILE__, __LINE__,
"LibFunc:recvfrom() failed",
strerror(errno) );
*pReadCount = 0;
returnStatus = eRS_SystemCall_Failure; // indicate error occurred
}
else
{ //else 0 or more bytes read
// update callers' info
*pReadCount = readStatus;
}
return( returnStatus );
} // end read_UDP_socket()
/* ******************************************************************** **
** End of source file: Common_UDP_Utilities.c
** ******************************************************************** */
我的代码目前正在使用 INADDR_ANY,我正在测试是否能够通过 Ubuntu 路由器从 VM 客户端发送数据包,[使用 IPTables] 到我的主机机.
我能够从 Linux 路由器后面的客户端和主机的客户端 ping 主机,但是当我发送 UDP 数据包时,它永远不会到达主机。
问题要么在这两个地方之一,我为路由设置的 IPTables 不能正常工作,要么在我绑定到 INADDR_ANY 的服务器代码中,INADDR_ANY不包括 VMware 接口。
我四处寻找,没有找到具体的信息。据我了解,没有理由不工作。
扩展问题:
根据 this 如果我 运行 命令我将能够复制相应的 NAT,但是如果它说 public ip 在这里 和 private ip 在这里 是指目标 IP 还是 public和私有接口IP;即 eth0 和 eth1 的 IP 地址?
已解决
我在错误的 eth 卡上设置了 iptable 规则!因此,请确保您在正确的 eth 上应用命令。
以下代码是UDP连接的客户端。
这适用于 ubuntu linux,VM,windows XP。
注意:左边的代码语句(通常)用于调试
注意:系统函数'sendto()'和'recvfrom()'是UDP通信的正确调用。
注意:函数大小写等根据当地使用的编码风格而定。
注意:与任何 'real life' 项目一样,大部分代码用于处理错误。
您可能对 create_UDP_socket()
函数最感兴趣。
/* *************** Included files************************************* */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h> // for system() and itoa() utilities
#include <stdarg.h> // for va_list(), etc
#include <string.h> // string handling utilities
#include <semaphore.h> // semaphores handling utilities
#include <fcntl.h> // symbolic definitions for flags bits
#include <sys/types.h> // contains definitions of data types used in system calls
#include <sys/socket.h>// type definitions (I.E. struct sockaddr_in)
#include <sys/stat.h> // symbolic definitions for permission bits and open()
#include <sys/ipc.h> // ipc interface utilities
#include <sys/shm.h> // shared memory utilities
#include <arpa/inet.h> // defines INADDR_ANY, etc
#include <errno.h> // for accessing errno and related functions
#include <stdbool.h> // define true, false, bool
#include <time.h>
// contains common data type definitions
#include "localTypes.h"
// contains data definitions for ON and OFF, enum enumReturnStatus
#include "Defines.h"
// contains prototypes for writing to Log file
#include "CommonWriteLog.h"
// contains prototypes, and data type definitions
#include "CommonConfiguration.h"
//contains prototypes for globally visible functions in this file
#include "Common_UDP_Utilities.h"
/* *************** Global variables *********************************** */
/* *************** File Static Variables ****************************** */
/* *************** Code *********************************************** */
enum enumReturnStatus create_UDP_socket( INT32 taskSelector, INT32 *pUDPsocket_FD )
{
enum enumReturnStatus returnStatus = eRS_Success; // indicate success
INT32 LocalPort;
INT32 socket_FD;
struct sockaddr_in *pLocal_sockaddr;
char *txtAddress;
if( is_taskSelector_Valid( taskSelector ) )
{ // then parameters valid
socket_FD = get_UDPsocket_FD( eTask_Retrieve_GPS );
if( 0 >= socket_FD )
{ // then socket not yet opened
pLocal_sockaddr = get_pUDPsocket_sockaddr( taskSelector );
txtAddress = inet_ntoa((*pLocal_sockaddr).sin_addr);
fprintf(stdout, "OPENING SOCKET on socket addresss: %s \n", txtAddress );
if( NULL != pLocal_sockaddr )
{ // then, sockaddr configured
LocalPort = get_UDPsocket_LocalPort( taskSelector );
fprintf(stdout, "OPENING SOCKET on PORT: %d \n", LocalPort );
if( 0 <= LocalPort )
{ // then port configured
socket_FD = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
if( -1 != socket_FD )
{ // then, socket creation successful
int TurnOn = 1;
returnStatus = setsockopt( socket_FD, SOL_SOCKET, SO_REUSEADDR, &TurnOn, sizeof( TurnOn ) );
if ( eRS_Success == returnStatus )
{ // then setsockopt() successful
bzero((char *) pLocal_sockaddr, sizeof(struct sockaddr_in) );
pLocal_sockaddr->sin_family = AF_INET;
pLocal_sockaddr->sin_port = htons( LocalPort );
pLocal_sockaddr->sin_addr.s_addr = htonl(INADDR_ANY); // todo: change to specific GPS IP address
CommonWriteLog( eLL_Info,
"INFO:File:%s: Line:%d:\n\t%s 0X%08X %d\n\n",
__FILE__, __LINE__,
"addr.sin_port =",
pLocal_sockaddr->sin_port,
pLocal_sockaddr->sin_port );
returnStatus = bind( socket_FD, (struct sockaddr *)pLocal_sockaddr, sizeof(struct sockaddr) );
if( eRS_Success == returnStatus )
{ // then bind successful
returnStatus = set_UDPsocket_FD( taskSelector, socket_FD );
if( eRS_Success != returnStatus )
{ // then set_UDPsocket_FD failed
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_string ),
__FILE__, __LINE__,
"Function:set_UDPsocket_FD() failed" );
returnStatus = eRS_UDP_Create_Failure; // indicate error occurred
close( socket_FD );
socket_FD = -1;
}
// set callers data
*pUDPsocket_FD = socket_FD;
}
else
{ // else, bind() failed
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_taskSelector_dataIndex_string_string ),
__FILE__, __LINE__,
taskSelector, 0,
"LibFunc:bind() failed",
strerror(errno) );
} // endif( bind )
}
else
{ // else setsockopt() failed
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_taskSelector_dataIndex_string_string ),
__FILE__, __LINE__,
taskSelector, 0,
"LibFunc:setsockopt( SO_REUSEADDR ) failed",
strerror(errno) );
} // endif( SO_REUSEADDR )
}
else
{ // else failed to create socket
fprintf(stdout, "FAILED TO OPEN SOCKET: \n" );
fflush(stdout);
system("sync;sync;");
exit(EXIT_FAILURE);
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_taskSelector_dataIndex_string_string ),
__FILE__, __LINE__,
taskSelector, 0,
"LibFunc:socket() failed",
strerror(errno) );
returnStatus = eRS_SystemCall_Failure; // indicate error occured
} // endif( socket )
}
else
{ // else port not configured
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_taskSelector_dataIndex_string ),
__FILE__, __LINE__,
taskSelector, 0,
"port not configured" );
returnStatus = eRS_Configuration_Failure; // indicate error occurred
}
}
else
{ // else get pUDP sockaddr failed
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_taskSelector_dataIndex_string ),
__FILE__, __LINE__,
taskSelector, 0,
"Function:get_pUDPsocket_sockaddr() failed" );
returnStatus = eRS_UDP_Create_Failure; // indicate error occurred
} // endif( get_pUDPsocket_sockaddr() successful )
} // endif( socket already open )
}
else
{ // else, bad parameters
returnStatus = eRS_UDP_Create_Failure;
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_digit_string ),
__FILE__, __LINE__,
taskSelector,
"Parameter:taskSelector out of range" );
}
return( returnStatus );
} // end create_UDP_socket()
enum enumReturnStatus write_UDP_socket( INT32 Socket_FD, char *pSocket_OutBuffer, INT32 Size, struct sockaddr *pTo_sockaddr, INT32 *pWriteCount )
{
enum enumReturnStatus returnStatus = eRS_Success; // indicate success
INT32 flags = 0;
INT32 sendtoStatus;
// do not call CommonWriteLog() to help avoid extranious 'recursion' messages in logs
// and to avoid recrusive operation
sendtoStatus =
sendto( Socket_FD, pSocket_OutBuffer, Size, flags,pTo_sockaddr, sizeof(struct sockaddr) );
if( 0 <= sendtoStatus )
{ // then, sendto() successful
*pWriteCount = sendtoStatus;
if( sendtoStatus != Size )
{ // then not all char sent
CommonWriteLog( eLL_Error,
"ERROR:File:%s: Line:%d:\n\t%s:num2Send:%d, numSent:%d\n\n",
__FILE__, __LINE__,
"LibFunc:sendto() failed",
Size, sendtoStatus );
returnStatus = eRS_UDP_Write_Failure; // indicate error occurred
}
}
else
{
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_string_string ),
__FILE__, __LINE__,
"LibFunc:sendto() failed",
strerror(errno) );
returnStatus = eRS_SystemCall_Failure; // indicate error occurred
*pWriteCount = 0;
}
return( returnStatus );
} // end write_UDP_socket()
enum enumReturnStatus read_UDP_socket( INT32 Socket_FD, char *pSocket_InBuffer, INT32 Size, INT32 *pReadCount )
{
enum enumReturnStatus returnStatus = eRS_Success;
INT32 readStatus = 0; // result of call to recvfrom()
INT32 flags = 0; // option parameter for call to recvfrom()
struct sockaddr_in from_sockaddr;
UINT32 slen = 0; // sizeof struct sockaddr_in
memset( &from_sockaddr, 0x00, sizeof(from_sockaddr) );
slen = sizeof( struct sockaddr_in );
readStatus = recvfrom( Socket_FD, pSocket_InBuffer,Size, flags,(struct sockaddr*)&from_sockaddr, &slen ); // filled during call
if( 0 > readStatus )
{ // then, an I/O error occurred
CommonWriteLog( eLL_Error,
get_pFormatString( eFormat_EFFL_string_string ),
__FILE__, __LINE__,
"LibFunc:recvfrom() failed",
strerror(errno) );
*pReadCount = 0;
returnStatus = eRS_SystemCall_Failure; // indicate error occurred
}
else
{ //else 0 or more bytes read
// update callers' info
*pReadCount = readStatus;
}
return( returnStatus );
} // end read_UDP_socket()
/* ******************************************************************** **
** End of source file: Common_UDP_Utilities.c
** ******************************************************************** */