int create_socket(char *device)
{
        /* create socket */
        int sock_fd, device_id;
        sock_fd = socket(PF_PACKET, SOCK_DGRAM, 0); //2008.06.27 Yau change to UDP Socket
                                                                                                                                             
        if(sock_fd < 0)
                perror("create socket ERR:");
                                                                                                                                             
        device_id = iface_get_id(sock_fd, device);
                                                                                                                                             
        if (device_id == -1)
               printf("iface_get_id REEOR\n");
                                                                                                                                             
        if ( iface_bind(sock_fd, device_id) < 0)
                printf("iface_bind ERROR\n");
                                                                                                                                             
        return sock_fd;
}
Beispiel #2
0
static int open_packet_engine (void) {
	int	sock_type;

	sock_type = (device == NULL) ? SOCK_DGRAM : SOCK_RAW;

	do {
		if ((sockfd = socket (PF_PACKET, sock_type,
						htons (ETH_P_IP))) < 0) {
						// htons (ETH_P_ALL))) < 0) {
			ebuf = strerror (errno);
			break;
		}


		if (device != NULL) {
			if ((device_id = iface_get_id (sockfd, device)) < 0)
				break;
			if (iface_bind (sockfd, device_id) < 0) break;
		} else {
		}
	} while (sockfd < 0);

	if (sockfd < 0) return 0;

	// -------

	go_promiscuous (pflag);

	if (device != NULL) {
		fprintf (stderr, "listen on %s (pf_packet)\n", device);
	} else {
		fprintf (stderr, "listen on all interfaces (pf_packet)\n");
	}

	return 1;
}