gpt4 book ai didi

c - INADDR_ANY 接口(interface)是否包括 VMware Network Adapter?

转载 作者:行者123 更新时间:2023-11-30 15:23:12 24 4
gpt4 key购买 nike

我的代码当前正在使用 INADDR_ANY,我正在测试是否能够通过 Ubuntu 路由器[使用 IPTables] 将数据包从 VM 客户端发送到我的主机。

我能够从 Linux 路由器后面的客户端和主机上的客户端 ping 通主机,但是当我发送 UDP 数据包时,它永远不会到达主机。

问题要么出在这两个地方之一,我为路由设置的 IPTable 无法正常工作,要么出在我绑定(bind)到 INADDR_ANY 的服务器代码中,INADDR_ANY 不包括 VMware 接口(interface)。

我查了一下,但没有找到具体信息。据我了解,没有理由它不起作用。

扩展问题:

根据this如果我运行这些命令,我​​将能够复制相应的 NAT,但是是否显示公共(public) ip 位于此处私有(private) ip 位于此处是指目的IP还是公私接口(interface)IP?即 eth0 和 eth1 的 IP 地址?

已解决

我在错误的 eth 卡上设置了 iptable 规则!因此,请确保您在正确的 eth 上应用命令。

最佳答案

以下代码是 UDP 连接的客户端。

这适用于 ubuntu linux、VM、Windows XP。

注意:左边距的代码语句(通常)用于调试

注意:系统函数“sendto()”和“recvfrom()”是 UDP 通信的正确调用。

注意:函数大小写等取决于本地使用的编码风格。

注意:与任何“现实生活”项目一样,大部分代码都是为了处理错误。

您可能对 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
** ******************************************************************** */

关于c - INADDR_ANY 接口(interface)是否包括 VMware Network Adapter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28916598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com