C/drv_tuntap.c
int tuntap_openmon_cb(lorcon_t *context) {
	char pcaperr[PCAP_ERRBUF_SIZE];
	struct mac80211_lorcon *extras = (struct mac80211_lorcon *) context->auxptr;
	struct ifreq if_req;
	struct sockaddr_ll sa_ll;

	if (ifconfig_delta_flags(context->ifname, context->errstr,
							 (IFF_UP | IFF_RUNNING | IFF_PROMISC)) < 0) {
		return -1;
	}

	pcaperr[0] = '\0';

	if ((context->pcap = pcap_open_live(context->ifname, LORCON_MAX_PACKET_LEN, 
										1, 1000, pcaperr)) == NULL) {
		snprintf(context->errstr, LORCON_STATUS_MAX, "%s", pcaperr);
		return -1;
	}

	context->capture_fd = pcap_get_selectable_fd(context->pcap);

	context->dlt = pcap_datalink(context->pcap);

	context->inject_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));

	if (context->inject_fd < 0) {
		snprintf(context->errstr, LORCON_STATUS_MAX, "failed to create injection "
				 "socket: %s", strerror(errno));
		pcap_close(context->pcap);
		return -1;
	}

	memset(&if_req, 0, sizeof(if_req));
	memcpy(if_req.ifr_name, context->ifname, IFNAMSIZ);
	if_req.ifr_name[IFNAMSIZ - 1] = 0;
	if (ioctl(context->inject_fd, SIOCGIFINDEX, &if_req) < 0) {
		snprintf(context->errstr, LORCON_STATUS_MAX, "failed to get interface idex: %s",
				 strerror(errno));
		close(context->inject_fd);
		pcap_close(context->pcap);
		return -1;
	}

	memset(&sa_ll, 0, sizeof(sa_ll));
	sa_ll.sll_family = AF_PACKET;
	sa_ll.sll_protocol = htons(ETH_P_80211_RAW);
	sa_ll.sll_ifindex = if_req.ifr_ifindex;
	if (bind(context->inject_fd, (struct sockaddr *) &sa_ll, sizeof(sa_ll)) != 0) {
		snprintf(context->errstr, LORCON_STATUS_MAX, "failed to bind injection "
				 "socket: %s", strerror(errno));
		close(context->inject_fd);
		pcap_close(context->pcap);
		return -1;
	}

	return 1;
}
/* Monitor, inject, and injmon are all the same method, open a new vap */
Monitor, inject, and injmon are all the same method, open a new vap
Monitor inject and injmon are all the same method open a new vap
int tuntap_openmon_cb(lorcon_t *context) {
	char pcaperr[PCAP_ERRBUF_SIZE];
	struct mac80211_lorcon *extras = (struct mac80211_lorcon *) context->auxptr;
	struct ifreq if_req;
	struct sockaddr_ll sa_ll;
	if (ifconfig_delta_flags(context->ifname, context->errstr,
							 (IFF_UP | IFF_RUNNING | IFF_PROMISC)) < 0) {
		return -1;
	}
	pcaperr[0] = '\0';
	if ((context->pcap = pcap_open_live(context->ifname, LORCON_MAX_PACKET_LEN, 
										1, 1000, pcaperr)) == NULL) {
		snprintf(context->errstr, LORCON_STATUS_MAX, "%s", pcaperr);
		return -1;
	}
	context->capture_fd = pcap_get_selectable_fd(context->pcap);
	context->dlt = pcap_datalink(context->pcap);
	context->inject_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
	if (context->inject_fd < 0) {
		snprintf(context->errstr, LORCON_STATUS_MAX, "failed to create injection "
				 "socket: %s", strerror(errno));
		pcap_close(context->pcap);
		return -1;
	}
	memset(&if_req, 0, sizeof(if_req));
	memcpy(if_req.ifr_name, context->ifname, IFNAMSIZ);
	if_req.ifr_name[IFNAMSIZ - 1] = 0;
	if (ioctl(context->inject_fd, SIOCGIFINDEX, &if_req) < 0) {
		snprintf(context->errstr, LORCON_STATUS_MAX, "failed to get interface idex: %s",
				 strerror(errno));
		close(context->inject_fd);
		pcap_close(context->pcap);
		return -1;
	}
	memset(&sa_ll, 0, sizeof(sa_ll));
	sa_ll.sll_family = AF_PACKET;
	sa_ll.sll_protocol = htons(ETH_P_80211_RAW);
	sa_ll.sll_ifindex = if_req.ifr_ifindex;
	if (bind(context->inject_fd, (struct sockaddr *) &sa_ll, sizeof(sa_ll)) != 0) {
		snprintf(context->errstr, LORCON_STATUS_MAX, "failed to bind injection "
				 "socket: %s", strerror(errno));
		close(context->inject_fd);
		pcap_close(context->pcap);
		return -1;
	}
	return 1;
}
int tuntap_openmon_cb ( lorcon_t * context ) { char pcaperr [ PCAP_ERRBUF_SIZE ] ; struct mac80211_lorcon * extras = ( struct mac80211_lorcon * ) context -> auxptr ; struct ifreq if_req ; struct sockaddr_ll sa_ll ; if ( ifconfig_delta_flags ( context -> ifname , context -> errstr , ( IFF_UP | IFF_RUNNING | IFF_PROMISC ) ) < 0 ) { return -1 ; } pcaperr [ 0 ] = ' \0 ' ; if ( ( context -> pcap = pcap_open_live ( context -> ifname , LORCON_MAX_PACKET_LEN , 1 , 1000 , pcaperr ) ) == NULL ) { snprintf ( context -> errstr , LORCON_STATUS_MAX , " " , pcaperr ) ; return -1 ; } context -> capture_fd = pcap_get_selectable_fd ( context -> pcap ) ; context -> dlt = pcap_datalink ( context -> pcap ) ; context -> inject_fd = socket ( PF_PACKET , SOCK_RAW , htons ( ETH_P_ALL ) ) ; if ( context -> inject_fd < 0 ) { snprintf ( context -> errstr , LORCON_STATUS_MAX , " " " " , strerror ( errno ) ) ; pcap_close ( context -> pcap ) ; return -1 ; } memset ( & if_req , 0 , sizeof ( if_req ) ) ; memcpy ( if_req . ifr_name , context -> ifname , IFNAMSIZ ) ; if_req . ifr_name [ IFNAMSIZ - 1 ] = 0 ; if ( ioctl ( context -> inject_fd , SIOCGIFINDEX , & if_req ) < 0 ) { snprintf ( context -> errstr , LORCON_STATUS_MAX , " " , strerror ( errno ) ) ; close ( context -> inject_fd ) ; pcap_close ( context -> pcap ) ; return -1 ; } memset ( & sa_ll , 0 , sizeof ( sa_ll ) ) ; sa_ll . sll_family = AF_PACKET ; sa_ll . sll_protocol = htons ( ETH_P_80211_RAW ) ; sa_ll . sll_ifindex = if_req . ifr_ifindex ; if ( bind ( context -> inject_fd , ( struct sockaddr * ) & sa_ll , sizeof ( sa_ll ) ) != 0 ) { snprintf ( context -> errstr , LORCON_STATUS_MAX , " " " " , strerror ( errno ) ) ; close ( context -> inject_fd ) ; pcap_close ( context -> pcap ) ; return -1 ; } return 1 ; }
Monitor, inject, and injmon are all the same method, open a new vap
Monitor inject and injmon are all the same method open a new vap