ClientCollectors/ARODataCollector/external/libpcap-1.1.1/pcap-linux.c
static int
activate_old(pcap_t *handle)
{
	int		arptype;
	struct ifreq	ifr;
	const char	*device = handle->opt.source;
	struct utsname	utsname;
	int		mtu;

	/* Open the socket */

	handle->fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
	if (handle->fd == -1) {
	        printf("\n socket connection failed \n");
		__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "socket connection failed");  	
		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
			 "socket: %s", pcap_strerror(errno));
		return PCAP_ERROR_PERM_DENIED;
	}

	/* It worked - we are using the old interface */
	handle->md.sock_packet = 1;

	/* ...which means we get the link-layer header. */
	handle->md.cooked = 0;

	/* Bind to the given device */

	if (strcmp(device, "any") == 0) {
		strncpy(handle->errbuf, "pcap_activate: The \"any\" device isn't supported on 2.0[.x]-kernel systems",
			PCAP_ERRBUF_SIZE);
		return PCAP_ERROR;
	}
	if (iface_bind_old(handle->fd, device, handle->errbuf) == -1)
		return PCAP_ERROR;

	/*
	 * Try to get the link-layer type.
	 */
	arptype = iface_get_arptype(handle->fd, device, handle->errbuf);
	if (arptype < 0)
		return PCAP_ERROR;

	/*
	 * Try to find the DLT_ type corresponding to that
	 * link-layer type.
	 */
	map_arphrd_to_dlt(handle, arptype, 0);
	if (handle->linktype == -1) {
		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
			 "unknown arptype %d", arptype);
		return PCAP_ERROR;
	}

	/* Go to promisc mode if requested */

	if (handle->opt.promisc) {
		memset(&ifr, 0, sizeof(ifr));
		strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
		if (ioctl(handle->fd, SIOCGIFFLAGS, &ifr) == -1) {
			snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
				 "SIOCGIFFLAGS: %s", pcap_strerror(errno));
			return PCAP_ERROR;
		}
		if ((ifr.ifr_flags & IFF_PROMISC) == 0) {
			/*
			 * Promiscuous mode isn't currently on,
			 * so turn it on, and remember that
			 * we should turn it off when the
			 * pcap_t is closed.
			 */

			/*
			 * If we haven't already done so, arrange
			 * to have "pcap_close_all()" called when
			 * we exit.
			 */
			if (!pcap_do_addexit(handle)) {
				/*
				 * "atexit()" failed; don't put
				 * the interface in promiscuous
				 * mode, just give up.
				 */
				return PCAP_ERROR;
			}

			ifr.ifr_flags |= IFF_PROMISC;
			if (ioctl(handle->fd, SIOCSIFFLAGS, &ifr) == -1) {
			        snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
					 "SIOCSIFFLAGS: %s",
					 pcap_strerror(errno));
				return PCAP_ERROR;
			}
			handle->md.must_do_on_close |= MUST_CLEAR_PROMISC;

			/*
			 * Add this to the list of pcaps
			 * to close when we exit.
			 */
			pcap_add_to_pcaps_to_close(handle);
		}
	}

	/*
	 * Compute the buffer size.
	 *
	 * We're using SOCK_PACKET, so this might be a 2.0[.x]
	 * kernel, and might require special handling - check.
	 */
	if (uname(&utsname) < 0 ||
	    strncmp(utsname.release, "2.0", 3) == 0) {
		/*
		 * Either we couldn't find out what kernel release
		 * this is, or it's a 2.0[.x] kernel.
		 *
		 * In the 2.0[.x] kernel, a "recvfrom()" on
		 * a SOCK_PACKET socket, with MSG_TRUNC set, will
		 * return the number of bytes read, so if we pass
		 * a length based on the snapshot length, it'll
		 * return the number of bytes from the packet
		 * copied to userland, not the actual length
		 * of the packet.
		 *
		 * This means that, for example, the IP dissector
		 * in tcpdump will get handed a packet length less
		 * than the length in the IP header, and will
		 * complain about "truncated-ip".
		 *
		 * So we don't bother trying to copy from the
		 * kernel only the bytes in which we're interested,
		 * but instead copy them all, just as the older
		 * versions of libpcap for Linux did.
		 *
		 * The buffer therefore needs to be big enough to
		 * hold the largest packet we can get from this
		 * device.  Unfortunately, we can't get the MRU
		 * of the network; we can only get the MTU.  The
		 * MTU may be too small, in which case a packet larger
		 * than the buffer size will be truncated *and* we
		 * won't get the actual packet size.
		 *
		 * However, if the snapshot length is larger than
		 * the buffer size based on the MTU, we use the
		 * snapshot length as the buffer size, instead;
		 * this means that with a sufficiently large snapshot
		 * length we won't artificially truncate packets
		 * to the MTU-based size.
		 *
		 * This mess just one of many problems with packet
		 * capture on 2.0[.x] kernels; you really want a
		 * 2.2[.x] or later kernel if you want packet capture
		 * to work well.
		 */
		mtu = iface_get_mtu(handle->fd, device, handle->errbuf);
		if (mtu == -1)
			return PCAP_ERROR;
		handle->bufsize = MAX_LINKHEADER_SIZE + mtu;
		if (handle->bufsize < handle->snapshot)
			handle->bufsize = handle->snapshot;
	} else {
		/*
		 * This is a 2.2[.x] or later kernel.
		 *
		 * We can safely pass "recvfrom()" a byte count
		 * based on the snapshot length.
		 */
		handle->bufsize = handle->snapshot;
	}

	/*
	 * Default value for offset to align link-layer payload
	 * on a 4-byte boundary.
	 */
	handle->offset	 = 0;

	return 1;
}
/*
 * Try to open a packet socket using the old kernel interface.
 * Returns 1 on success and a PCAP_ERROR_ value on an error.
 */
Try to open a packet socket using the old kernel interface.
Returns 1 on success and a PCAP_ERROR_ value on an error.
Try to open a packet socket using the old kernel interface . Returns 1 on success and a PCAP_ERROR_ value on an error .
static int
activate_old(pcap_t *handle)
{
	int		arptype;
	struct ifreq	ifr;
	const char	*device = handle->opt.source;
	struct utsname	utsname;
	int		mtu;
	handle->fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
	if (handle->fd == -1) {
	        printf("\n socket connection failed \n");
		__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "socket connection failed");  	
		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
			 "socket: %s", pcap_strerror(errno));
		return PCAP_ERROR_PERM_DENIED;
	}
	handle->md.sock_packet = 1;
	handle->md.cooked = 0;
	if (strcmp(device, "any") == 0) {
		strncpy(handle->errbuf, "pcap_activate: The \"any\" device isn't supported on 2.0[.x]-kernel systems",
			PCAP_ERRBUF_SIZE);
		return PCAP_ERROR;
	}
	if (iface_bind_old(handle->fd, device, handle->errbuf) == -1)
		return PCAP_ERROR;
	arptype = iface_get_arptype(handle->fd, device, handle->errbuf);
	if (arptype < 0)
		return PCAP_ERROR;
	map_arphrd_to_dlt(handle, arptype, 0);
	if (handle->linktype == -1) {
		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
			 "unknown arptype %d", arptype);
		return PCAP_ERROR;
	}
	if (handle->opt.promisc) {
		memset(&ifr, 0, sizeof(ifr));
		strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
		if (ioctl(handle->fd, SIOCGIFFLAGS, &ifr) == -1) {
			snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
				 "SIOCGIFFLAGS: %s", pcap_strerror(errno));
			return PCAP_ERROR;
		}
		if ((ifr.ifr_flags & IFF_PROMISC) == 0) {
			if (!pcap_do_addexit(handle)) {
				return PCAP_ERROR;
			}
			ifr.ifr_flags |= IFF_PROMISC;
			if (ioctl(handle->fd, SIOCSIFFLAGS, &ifr) == -1) {
			        snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
					 "SIOCSIFFLAGS: %s",
					 pcap_strerror(errno));
				return PCAP_ERROR;
			}
			handle->md.must_do_on_close |= MUST_CLEAR_PROMISC;
			pcap_add_to_pcaps_to_close(handle);
		}
	}
	if (uname(&utsname) < 0 ||
	    strncmp(utsname.release, "2.0", 3) == 0) {
		mtu = iface_get_mtu(handle->fd, device, handle->errbuf);
		if (mtu == -1)
			return PCAP_ERROR;
		handle->bufsize = MAX_LINKHEADER_SIZE + mtu;
		if (handle->bufsize < handle->snapshot)
			handle->bufsize = handle->snapshot;
	} else {
		handle->bufsize = handle->snapshot;
	}
	handle->offset	 = 0;
	return 1;
}
static int activate_old ( pcap_t * handle ) { int arptype ; struct ifreq ifr ; const char * device = handle -> opt . source ; struct utsname utsname ; int mtu ; handle -> fd = socket ( PF_INET , SOCK_PACKET , htons ( ETH_P_ALL ) ) ; if ( handle -> fd == -1 ) { printf ( " \n \n " ) ; __android_log_print ( ANDROID_LOG_DEBUG , DEBUG_TAG , " " ) ; snprintf ( handle -> errbuf , PCAP_ERRBUF_SIZE , " " , pcap_strerror ( errno ) ) ; return PCAP_ERROR_PERM_DENIED ; } handle -> md . sock_packet = 1 ; handle -> md . cooked = 0 ; if ( strcmp ( device , " " ) == 0 ) { strncpy ( handle -> errbuf , " \" \" " , PCAP_ERRBUF_SIZE ) ; return PCAP_ERROR ; } if ( iface_bind_old ( handle -> fd , device , handle -> errbuf ) == -1 ) return PCAP_ERROR ; arptype = iface_get_arptype ( handle -> fd , device , handle -> errbuf ) ; if ( arptype < 0 ) return PCAP_ERROR ; map_arphrd_to_dlt ( handle , arptype , 0 ) ; if ( handle -> linktype == -1 ) { snprintf ( handle -> errbuf , PCAP_ERRBUF_SIZE , " " , arptype ) ; return PCAP_ERROR ; } if ( handle -> opt . promisc ) { memset ( & ifr , 0 , sizeof ( ifr ) ) ; strncpy ( ifr . ifr_name , device , sizeof ( ifr . ifr_name ) ) ; if ( ioctl ( handle -> fd , SIOCGIFFLAGS , & ifr ) == -1 ) { snprintf ( handle -> errbuf , PCAP_ERRBUF_SIZE , " " , pcap_strerror ( errno ) ) ; return PCAP_ERROR ; } if ( ( ifr . ifr_flags & IFF_PROMISC ) == 0 ) { if ( ! pcap_do_addexit ( handle ) ) { return PCAP_ERROR ; } ifr . ifr_flags |= IFF_PROMISC ; if ( ioctl ( handle -> fd , SIOCSIFFLAGS , & ifr ) == -1 ) { snprintf ( handle -> errbuf , PCAP_ERRBUF_SIZE , " " , pcap_strerror ( errno ) ) ; return PCAP_ERROR ; } handle -> md . must_do_on_close |= MUST_CLEAR_PROMISC ; pcap_add_to_pcaps_to_close ( handle ) ; } } if ( uname ( & utsname ) < 0 || strncmp ( utsname . release , " " , 3 ) == 0 ) { mtu = iface_get_mtu ( handle -> fd , device , handle -> errbuf ) ; if ( mtu == -1 ) return PCAP_ERROR ; handle -> bufsize = MAX_LINKHEADER_SIZE + mtu ; if ( handle -> bufsize < handle -> snapshot ) handle -> bufsize = handle -> snapshot ; } else { handle -> bufsize = handle -> snapshot ; } handle -> offset = 0 ; return 1 ; }
Try to open a packet socket using the old kernel interface.
Try to open a packet socket using the old kernel interface .
/* Open the socket */ /* It worked - we are using the old interface */ /* ...which means we get the link-layer header. */ /* Bind to the given device */ /*
	 * Try to get the link-layer type.
	 */ /*
	 * Try to find the DLT_ type corresponding to that
	 * link-layer type.
	 */ /* Go to promisc mode if requested */ /*
			 * Promiscuous mode isn't currently on,
			 * so turn it on, and remember that
			 * we should turn it off when the
			 * pcap_t is closed.
			 */ /*
			 * If we haven't already done so, arrange
			 * to have "pcap_close_all()" called when
			 * we exit.
			 */ /*
				 * "atexit()" failed; don't put
				 * the interface in promiscuous
				 * mode, just give up.
				 */ /*
			 * Add this to the list of pcaps
			 * to close when we exit.
			 */ /*
	 * Compute the buffer size.
	 *
	 * We're using SOCK_PACKET, so this might be a 2.0[.x]
	 * kernel, and might require special handling - check.
	 */ /*
		 * Either we couldn't find out what kernel release
		 * this is, or it's a 2.0[.x] kernel.
		 *
		 * In the 2.0[.x] kernel, a "recvfrom()" on
		 * a SOCK_PACKET socket, with MSG_TRUNC set, will
		 * return the number of bytes read, so if we pass
		 * a length based on the snapshot length, it'll
		 * return the number of bytes from the packet
		 * copied to userland, not the actual length
		 * of the packet.
		 *
		 * This means that, for example, the IP dissector
		 * in tcpdump will get handed a packet length less
		 * than the length in the IP header, and will
		 * complain about "truncated-ip".
		 *
		 * So we don't bother trying to copy from the
		 * kernel only the bytes in which we're interested,
		 * but instead copy them all, just as the older
		 * versions of libpcap for Linux did.
		 *
		 * The buffer therefore needs to be big enough to
		 * hold the largest packet we can get from this
		 * device.  Unfortunately, we can't get the MRU
		 * of the network; we can only get the MTU.  The
		 * MTU may be too small, in which case a packet larger
		 * than the buffer size will be truncated *and* we
		 * won't get the actual packet size.
		 *
		 * However, if the snapshot length is larger than
		 * the buffer size based on the MTU, we use the
		 * snapshot length as the buffer size, instead;
		 * this means that with a sufficiently large snapshot
		 * length we won't artificially truncate packets
		 * to the MTU-based size.
		 *
		 * This mess just one of many problems with packet
		 * capture on 2.0[.x] kernels; you really want a
		 * 2.2[.x] or later kernel if you want packet capture
		 * to work well.
		 */ /*
		 * This is a 2.2[.x] or later kernel.
		 *
		 * We can safely pass "recvfrom()" a byte count
		 * based on the snapshot length.
		 */ /*
	 * Default value for offset to align link-layer payload
	 * on a 4-byte boundary.
	 */