Exemple #1
0
static int common_ping_main(sa_family_t af, char **argv)
{
	len_and_sockaddr *lsa;

	INIT_G();

#if ENABLE_PING6
	while ((++argv)[0] && argv[0][0] == '-') {
		if (argv[0][1] == '4') {
			af = AF_INET;
			continue;
		}
		if (argv[0][1] == '6') {
			af = AF_INET6;
			continue;
		}
		bb_show_usage();
	}
#else
	argv++;
#endif

	G.hostname = *argv;
	if (!G.hostname)
		bb_show_usage();

#if ENABLE_PING6
	lsa = xhost_and_af2sockaddr(G.hostname, 0, af);
#else
	lsa = xhost_and_af2sockaddr(G.hostname, 0, AF_INET);
#endif
	/* Set timer _after_ DNS resolution */
	signal(SIGALRM, noresp);
	alarm(5); /* give the host 5000ms to respond */

	create_icmp_socket(lsa);
	G.myid = (uint16_t) getpid();
#if ENABLE_PING6
	if (lsa->u.sa.sa_family == AF_INET6)
		ping6(lsa);
	else
#endif
		ping4(lsa);
	if (ENABLE_FEATURE_CLEAN_UP)
		close(pingsock);
	printf("%s is alive!\n", G.hostname);
	return EXIT_SUCCESS;
}
Exemple #2
0
/*!
 * \brief
 * This function pings an IP-Address with a specified Time-To-Live-value.
 * 
 * \param targetIP
 * The textual representation of the IP-Address to ping (both IPv4 and IPv6 are accepted)
 * 
 * \param ttl
 * The Time-To-Live-value to perform the ping with
 * 
 * \param out
 * The Buffer into which the result of the ping-command will be written
 * 
 * \param outSize
 * The maximum number of characters that can be written into "out"
 * 
 * \returns
 * True if the execution succeded and false otherwise. The following will be written in "out": "TARGET "+TargetIP if the target was reached, "HOP "+HopIP if an intermediate Host was reached or "NO_REPLY" if an execution error occurred. If an internal error occured, a message describing that error will be written.
 *
 * \remarks
 * Writing the "ping"-function in Javascript and calling the iphlpapi.dll (and others) using c-types turned out to be impossible. The reason for that is the fact that calling GetLastError() is not possible using c-types. Therefore it is implemented in c-code and compiled into the crossbear.dll.
 * 
 */
extern "C" __declspec( dllexport ) bool ping(WCHAR * targetIP, int ttl, WCHAR * out, int  outSize){

	// Validate the Time-To-Live-Parameter: less than 1 is invalid and more than 255 doesn't make sense and might cause _itow problems
	if(ttl<1 || ttl>255){
		outPutWstring(std::wstring(L"TTL Parameter invalid"),out,outSize);
		return false;
	}

	// Verify if targetIP is a valid address if yes convert it into a addrinfo-struct
	struct addrinfoW *ai;
	if(! (0==GetAddrInfoW(targetIP,NULL,NULL,&ai))){
		outPutWstring(std::wstring(L"could not parse IP Address : ")+getFormatedLastError(),out,outSize);
		return false;
	}

	// Check if the IP-version of the targetIP is v6 ...
	if(ai->ai_family == AF_INET6){

		// ... and if it is perform a ping using ICMPv4
		return ping6(ai, ttl, out, outSize);

	}

	// Check if the IP-version of the targetIP is v4 ...
	else if(ai->ai_family == AF_INET){

		// ... and if it is perform a ping using ICMPv6
		return ping4(ai, ttl, out, outSize);

	}

	// In case the passed address is neither IPv4 nor IPv6: Return an error message
	outPutWstring(std::wstring(L"Unknown type of IPAddress"),out,outSize);
	FreeAddrInfoW(ai);
	return false;
}