📄 rnrcs.c
字号:
return FALSE;
}
GlobalFree(nsclass);
return TRUE;
}
//
// Function: GetIPInterfaceList
//
// Description:
// This function returns an array of SOCKADDR_IN structures,
// one for every local IP interface on the machine. We use
// the ioctl command SIO_GET_INTERFACE_LIST to do this although
// we could have used any number of method such as gethostbyname(),
// SIO_INTERFACE_LIST_QUERY, or the IP helper APIs.
//
SOCKADDR_IN *GetIPInterfaceList(SOCKET s, int *count)
{
static SOCKADDR_IN sa_in[MAX_NUM_CSADDRS];
INTERFACE_INFO iflist[MAX_INTERFACE_LIST];
DWORD dwBytes;
int ret,
i;
*count = 0;
ret = WSAIoctl(s, SIO_GET_INTERFACE_LIST, NULL, 0, &iflist,
sizeof(iflist), &dwBytes, NULL, NULL);
if (ret == SOCKET_ERROR)
{
printf("WSAIoctl(SIO_GET_INTERFACE_LIST) failed: %d\n",
WSAGetLastError());
return NULL;
}
// Loop through the interfaces and copy them into the SOCKADDR_IN
// array.
//
*count = dwBytes / sizeof(INTERFACE_INFO);
for(i=0; i < *count ;i++)
{
memcpy(&sa_in[i], &iflist[i].iiAddress.AddressIn, sizeof(SOCKADDR_IN));
}
return sa_in;
}
//
// Function: GetIPXInterfaceList
//
// Description:
// This function enumerates the local IPX interfaces and returns
// them in an array of SOCKADDR_IPX structures. The only way to
// do this is to use the socket option IPX_MAX_ADAPTER_NUM to
// find out how many interfaces there are and then call
// IPX_ADDRESS on each one to find the address.
//
SOCKADDR_IPX *GetIPXInterfaceList(SOCKET s, int *count)
{
static SOCKADDR_IPX sa_ipx[MAX_NUM_CSADDRS];
IPX_ADDRESS_DATA ipxdata;
int ifcount,
nSize,
ret,
i;
*count = 0;
ifcount = 0;
//
// Find out how many IPX interfaces are available
//
nSize = sizeof(ifcount);
ret = getsockopt(s, NSPROTO_IPX, IPX_MAX_ADAPTER_NUM,
(char *)&ifcount, &nSize);
if (ret == SOCKET_ERROR)
{
printf("getsockopt(IPX_MAX_ADAPTER_NUM) failed: %d\n",
WSAGetLastError());
return NULL;
}
// Loop through and get the address of each interface
//
for(i=0; i < ifcount ;i++)
{
nSize = sizeof(ipxdata);
memset(&ipxdata, 0, sizeof(ipxdata));
ipxdata.adapternum = i;
ret = getsockopt(s, NSPROTO_IPX, IPX_ADDRESS,
(char *)&ipxdata, &nSize);
if (ret == SOCKET_ERROR)
{
printf("getsockopt(IPX_ADDRESS) failed: %d\n", WSAGetLastError());
continue;
}
// Make sure that this interface is actually up and running
//
if (ipxdata.status == TRUE)
{
memcpy(&sa_ipx[i].sa_netnum, &ipxdata.netnum, sizeof(ipxdata.netnum));
memcpy(&sa_ipx[i].sa_nodenum, &ipxdata.nodenum, sizeof(ipxdata.nodenum));
}
}
*count = ifcount;
return sa_ipx;
}
//
// Function: Advertise
//
// Description:
// This function advertises an instance of the server. This
// function also creates the server for each available name
// space. To advertise you need all the local interfaces that
// the client can connect the to the server. This is done
// by filling out a WSAQUERYSET structure along with the
// appropriate CSADDR_INFO structures. The CSADDR_INFO
// structures define the interfaces the service is listening on.
//
BOOL Advertise(GUID *guid, WSANAMESPACE_INFO *nsinfo,
int nscount, char *servicename)
{
WSAQUERYSET qs;
CSADDR_INFO csaddrs[MAX_NUM_CSADDRS];
int ret,
i, j,
iSize,
addrcnt;
// Initialize the WSAQUERYSET structure
//
memset(&qs, 0, sizeof(WSAQUERYSET));
qs.dwSize = sizeof(WSAQUERYSET);
qs.lpszServiceInstanceName = servicename;
qs.lpServiceClassId = guid;
qs.dwNameSpace = NS_ALL;
qs.lpNSProviderId = NULL;
qs.lpcsaBuffer = csaddrs;
qs.lpBlob = NULL;
addrcnt=0;
//
// For each valid name space we create an instance of the
// service and find out what local interfaces are available
// that the client can connect to and communicate with the server.
//
for (i=0; i < nscount ;i++)
{
if (nsinfo[i].dwNameSpace == NS_NTDS)
{
SOCKADDR_IN localip;
SOCKADDR_IN *iflist=NULL;
int ipifcount;
// Create a UDP based server
//
printf("Setting up NTDS entry...\n");
socks[i] = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
NULL, 0, WSA_FLAG_OVERLAPPED);
if (socks[i] == INVALID_SOCKET)
{
printf("WSASocket() failed: %d\n", WSAGetLastError());
return FALSE;
}
localip.sin_family = AF_INET;
localip.sin_port = htons(0);
localip.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(socks[i], (SOCKADDR *)&localip, sizeof(localip));
if (ret == SOCKET_ERROR)
{
printf("bind() failed: %d\n", WSAGetLastError());
return FALSE;
}
// Get the port number back since we don't specifically give one
//
iSize = sizeof(localip);
ret = getsockname(socks[i], (SOCKADDR *)&localip, &iSize);
if (ret == SOCKET_ERROR)
{
printf("getsockname(IP) failed: %d\n", WSAGetLastError());
return FALSE;
}
// Get a list of the IP interfaces
//
iflist = GetIPInterfaceList(socks[i], &ipifcount);
if (!iflist)
{
printf("Unable to enumerate IP interfaces!\n");
return FALSE;
}
// Fill out the CSADDR_INFO structures with each IP interface
//
for (j=0; j < ipifcount ;j++)
{
iflist[j].sin_family = AF_INET;
iflist[j].sin_port = localip.sin_port;
csaddrs[addrcnt].iSocketType = SOCK_DGRAM;
csaddrs[addrcnt].iProtocol = IPPROTO_UDP;
csaddrs[addrcnt].LocalAddr.lpSockaddr = (SOCKADDR *)&iflist[j];
csaddrs[addrcnt].LocalAddr.iSockaddrLength = sizeof(iflist[j]);
csaddrs[addrcnt].RemoteAddr.lpSockaddr = (SOCKADDR *)&iflist[j];
csaddrs[addrcnt].RemoteAddr.iSockaddrLength = sizeof(iflist[j]);
printf("\t[%d] Local IP [%s:%d]\n", j,
inet_ntoa(((SOCKADDR_IN *)(csaddrs[addrcnt].LocalAddr.lpSockaddr))->sin_addr),
ntohs(((SOCKADDR_IN *)(csaddrs[addrcnt].LocalAddr.lpSockaddr))->sin_port));
printf("\t[%d] Remote IP [%s:%d]\n", j,
inet_ntoa(((SOCKADDR_IN *)(csaddrs[addrcnt].RemoteAddr.lpSockaddr))->sin_addr),
ntohs(((SOCKADDR_IN *)(csaddrs[addrcnt].RemoteAddr.lpSockaddr))->sin_port));
addrcnt++;
}
}
else if (nsinfo[i].dwNameSpace == NS_SAP)
{
SOCKADDR_IPX localipx,
*ipxlist;
int ipxifcount;
// Create an intance of our IPX server
//
printf("Setting up IPX entry...\n");
socks[i] = WSASocket(AF_IPX, SOCK_DGRAM, NSPROTO_IPX,
NULL, 0, WSA_FLAG_OVERLAPPED);
if (socks[i] == INVALID_SOCKET)
{
printf("WSASocket() failed: %d\n", WSAGetLastError());
return FALSE;
}
memset(&localipx, 0, sizeof(SOCKADDR_IPX));
localipx.sa_family = AF_IPX;
ret = bind(socks[i], (SOCKADDR *)&localipx, sizeof(localipx));
if (ret == SOCKET_ERROR)
{
printf("bind() failed: %d\n", WSAGetLastError());
return FALSE;
}
// Find out the socket number that the IPX server is listening
// on since we didn't explicitly specify one.
//
iSize = sizeof(localipx);
ret = getsockname(socks[i], (SOCKADDR *)&localipx, &iSize);
if (ret == SOCKET_ERROR)
{
printf("getsockname(IPX) failed: %d\n", WSAGetLastError());
return FALSE;
}
// Enumerate the local IPX interfaces on which clients can
// connect to us
//
ipxlist = GetIPXInterfaceList(socks[i], &ipxifcount);
if (!ipxlist)
{
printf("Unable to get the IPX interface list!\n");
return FALSE;
}
printf("Number IPX interfaces: %d\n", ipxifcount);
for(j=0; j < ipxifcount ;j++)
{
ipxlist[j].sa_family = AF_IPX;
ipxlist[j].sa_socket = localipx.sa_socket;
csaddrs[addrcnt].iSocketType = SOCK_DGRAM;
csaddrs[addrcnt].iProtocol = NSPROTO_IPX;
csaddrs[addrcnt].LocalAddr.lpSockaddr = (SOCKADDR *)&ipxlist[j];
csaddrs[addrcnt].LocalAddr.iSockaddrLength = sizeof(ipxlist[j]);
csaddrs[addrcnt].RemoteAddr.lpSockaddr = (SOCKADDR *)&ipxlist[j];
csaddrs[addrcnt].RemoteAddr.iSockaddrLength = sizeof(ipxlist[j]);
printf("\t[%d] Local IPX [%s]\n", j,
FormatIPXAddr((SOCKADDR_IPX *)(csaddrs[addrcnt].LocalAddr.lpSockaddr)));
printf("\t[%d] Remote IPX [%s]\n", j,
FormatIPXAddr((SOCKADDR_IPX *)(csaddrs[addrcnt].RemoteAddr.lpSockaddr)));
addrcnt++;
}
}
// Create an event for the server to use with WSAEventSelect
//
hEvents[i] = WSACreateEvent();
if (hEvents[i] == WSA_INVALID_EVENT)
{
printf("WSACreateEvent() failed: %d\n", WSAGetLastError());
return FALSE;
}
}
qs.dwNumberOfCsAddrs = addrcnt;
//
// Register our service(s)
//
ret = WSASetService(&qs, RNRSERVICE_REGISTER, 0L);
if (ret == SOCKET_ERROR)
{
printf("WSASetService() failed: %d\n", WSAGetLastError());
return FALSE;
}
printf("WSASetService() succeeded\n");
return TRUE;
}
//
// Function: LookupService
//
// Description:
// This function queries for an instance of the given service
// running on the network. You can either query for a specific
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -