?? rnrcs.c
字號:
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: 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, TCHAR *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_MYNSP;
qs.lpNSProviderId = &nsinfo[0].NSProviderId;
qs.lpcsaBuffer = csaddrs;
qs.lpBlob = NULL;
qs.lpszComment = "Dork this";
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_MYNSP)
{
SOCKADDR_IN localip;
SOCKADDR_IN *iflist=NULL;
int ipifcount;
// Create a TCP based server
//
printf("Setting up NS_MYNSP entry...\n");
sock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP,
NULL, 0, WSA_FLAG_OVERLAPPED);
if (sock == INVALID_SOCKET)
{
printf("WSASocket() failed: %d\n", WSAGetLastError());
return FALSE;
}
localip.sin_family = AF_INET;
localip.sin_port = htons((short)dwUniqueId);
localip.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(sock, (SOCKADDR *)&localip, sizeof(localip));
if (ret == SOCKET_ERROR)
{
printf("bind() failed: %d\n", WSAGetLastError());
return FALSE;
}
// Get a list of the IP interfaces
//
iflist = GetIPInterfaceList(sock, &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 = htons((short)dwUniqueId);
csaddrs[addrcnt].iSocketType = SOCK_STREAM;
csaddrs[addrcnt].iProtocol = IPPROTO_TCP;
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++;
}
}
}
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
// service name or specify the wildcard string "*". If an instance
// is found, send some data to it.
//
void LookupService(GUID *guid, int sapid, int ns, TCHAR *servername)
{
WSAQUERYSET qs,
*pqs;
AFPROTOCOLS afp[1] = { {AF_INET, IPPROTO_TCP} };
char querybuf[sizeof(WSAQUERYSET) + 4096];
DWORD nSize = sizeof(WSAQUERYSET) + 4096,
i;
HANDLE hLookup;
int ret, err;
// Initialize the WSAQUERYSET structure
//
pqs = (WSAQUERYSET *)querybuf;
memset(&qs, 0, sizeof(WSAQUERYSET));
qs.dwSize = sizeof(WSAQUERYSET);
qs.lpszServiceInstanceName = servername;
qs.lpServiceClassId = guid;
qs.lpNSProviderId = &MY_NAMESPACE_GUID;
qs.dwNameSpace = NS_MYNSP;
qs.dwNumberOfProtocols = ns;
qs.lpafpProtocols = afp;
//
// Begin the lookup. We want the name and address back
//
ret = WSALookupServiceBegin(&qs, LUP_RETURN_ADDR | LUP_RETURN_NAME,
&hLookup);
if (ret == SOCKET_ERROR)
{
printf("WSALookupServiceBegin failed: %d\n", WSAGetLastError());
return;
}
while (1)
{
// Loop, calling WSALookupServiceNext until WSA_E_NO_MORE is
// returned.
//
nSize = sizeof(WSAQUERYSET) + 4096;
memset(querybuf, 0, nSize);
pqs->dwSize = sizeof(WSAQUERYSET);
ret = WSALookupServiceNext(hLookup, 0, &nSize, pqs);
if (ret == SOCKET_ERROR)
{
err = WSAGetLastError();
if ((err == WSA_E_NO_MORE) || (err == WSAENOMORE))
{
printf("No more data found!\n");
break;
}
else if (err == WSASERVICE_NOT_FOUND)
{
printf("Service not found!\n");
break;
}
printf("WSALookupServiceNext() failed: %d\n", WSAGetLastError());
WSALookupServiceEnd(hLookup);
return;
}
// Now that we've found a server out there, print some info and
// send some data to it.
//
printf("\nFound service: %s\n\n", pqs->lpszServiceInstanceName);
printf("Returned %d CSADDR structures\n", pqs->dwNumberOfCsAddrs);
for(i=0; i < pqs->dwNumberOfCsAddrs ;i++)
{
switch (pqs->lpcsaBuffer[i].iProtocol)
{
case IPPROTO_TCP:
printf("IPPROTO_TCP: '%s'\n", inet_ntoa(((SOCKADDR_IN *)pqs->lpcsaBuffer[i].RemoteAddr.lpSockaddr)->sin_addr));
((SOCKADDR_IN *)pqs->lpcsaBuffer[i].RemoteAddr.lpSockaddr)->sin_family = AF_INET;
break;
default:
printf("Unknown!: %d\n", pqs->lpcsaBuffer[i].iProtocol);
break;
}
// Send data
//
}
if (bDeleteService)
ret = WSASetService(pqs, RNRSERVICE_DELETE, 0L);
}
WSALookupServiceEnd(hLookup);
printf("DONE!\n");
return;
}
//
// Function: main
//
// Description:
// Initialize Winsock, parse the arguments, and start either the
// client or server depending on the arguments.
//
int main(int argc, char **argv)
{
WSANAMESPACE_INFO *nsinfo=NULL;
WSADATA wsd;
GUID svcguid;
int nscount,
ret,
i;
WCHAR szTemp[256];
ValidateArgs(argc, argv);
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
printf("WSAStartup() failed: %d\n", GetLastError());
return -1;
}
// Generate a GUID for our service class
//
SET_TCP_SVCID(&svcguid, dwUniqueId);
//
// Enumerate the name spaces that we can use
//
nsinfo = EnumNameSpaceProviders(&nscount);
if (!nsinfo)
{
printf("unable to enumerate name space info!\n");
return -1;
}
for(i=0; i < nscount ;i++)
printf("Found NS: %s\n", nsinfo[i].lpszIdentifier);
if (bServer)
{
if (szServerName[0] == '*')
{
printf("You must specify a server name!\n");
usage(argv[0]);
return -1;
}
// Install the service class
//
if (!InstallServiceClass(&svcguid, nsinfo, nscount))
{
printf("Unable to install service class!\n");
return -1;
}
// Advertise our service
//
if (!Advertise(&svcguid, nsinfo, nscount, szServerName))
{
printf("Unable to advertise service!\n");
return -1;
}
}
else
{
// Lookup the service
//
LookupService(&svcguid, dwUniqueId, NS_MYNSP, szServerName);
}
HeapFree(GetProcessHeap(), 0, nsinfo);
WSACleanup();
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -