コード例 #1
0
ファイル: nl.c プロジェクト: joninvski/aodvuu-ts7500
void nl_init(void)
{
	int status;

	nlsock = socket(PF_NETLINK, SOCK_RAW, NETLINK_AODV);
	
	if (nlsock < 0) {
		perror("Unable to create netlink socket");
		exit(-1);
	}
	
	rtnlsock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
	
	if (rtnlsock < 0) {
		perror("Unable to create netlink socket");
		exit(-1);
	}
	
	memset(&local, 0, sizeof(struct sockaddr_nl));
	local.nl_family = AF_NETLINK;
	local.nl_pid = getpid();
	local.nl_groups = AODVGRP_NOTIFY;

	memset(&peer, 0, sizeof(struct sockaddr_nl));
	peer.nl_family = AF_NETLINK;
	peer.nl_pid = 0;
	peer.nl_groups = 0;

	status = bind(nlsock, (struct sockaddr *)&local, sizeof(local));
	
	if (status == -1) {
		perror("Bind failed");
		exit(-1);
	}
	
	local.nl_groups = RTMGRP_NOTIFY | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
	
	status = bind(rtnlsock, (struct sockaddr *)&local, sizeof(local));
	
	if (status == -1) {
		perror("Bind failed");
		exit(-1);
	}

	if (attach_callback_func(nlsock, nl_callback) < 0) {
		alog(LOG_ERR, 0, __FUNCTION__, "Could not attach callback.");
	}

	if (attach_callback_func(rtnlsock, nl_callback) < 0) {
		alog(LOG_ERR, 0, __FUNCTION__, "Could not attach callback.");
	}
}
コード例 #2
0
ファイル: llf.c プロジェクト: ankitnd043/VANETProject
void llf_init()
{

    if (llf_rtnl_open(&rth, RTMGRP_LINK) < 0) {
	DEBUG(LOG_ERR, 0, "Can't initialize rtnetlink socket");
	return;
    }
    if (attach_callback_func(rth.fd, llf_callback) < 0) {
	alog(LOG_ERR, 0, __FUNCTION__, "Could not attach callback");
	return;
    }
}
コード例 #3
0
void NS_CLASS packet_input_init()
{
#ifndef NS_PORT
    int status;
    int bufsize = 0;
    int optlen = sizeof(int);

    h = ipq_create_handle(0);

    if (h == NULL) {
	fprintf(stderr, "Initialization failed!\n");
	exit(1);
    }

    status = ipq_set_mode(h, IPQ_COPY_PACKET, BUFSIZE);

    if (status < 0) {
	die(h);
    }
    
    bufsize = 65535 *4;
    
    status = setsockopt(h->fd, SOL_SOCKET, SO_RCVBUF,
			(char *) &bufsize, sizeof(int));
    
    if (status != 0) {
	DEBUG(LOG_WARNING, 0, "Could not set netlink buffer size");
    }
    
    status = getsockopt(h->fd, SOL_SOCKET, SO_RCVBUF, 
			(char *) &bufsize, &optlen);
    
    if (status == 0) {
	DEBUG(LOG_DEBUG, 0, "Netlink socket buffer is %d", bufsize);
    }
    
    if (attach_callback_func(h->fd, packet_input) < 0) {
	alog(LOG_ERR, 0, __FUNCTION__, "Could not attach callback.");
    }
#endif				/* NS_PORT */
}
コード例 #4
0
void NS_CLASS packet_input_init()
{
#ifndef NS_PORT
    int status;

    h = ipq_create_handle(0);

    if (h == NULL) {
        fprintf(stderr, "Initialization failed!\n");
        exit(1);
    }
    /* Copy 100 bytes of payload: ip header 60 bytes + max 20 bytes
       Transport Layer header (TCP) + 20 extra bytes just to be
       sure.... */
    status = ipq_set_mode(h, IPQ_COPY_PACKET, BUFSIZE);
    if (status < 0) {
        die(h);
    }
    if (attach_callback_func(h->fd, packet_input) < 0) {
        log(LOG_ERR, 0, "packet_input_init:");
    }
#endif				/* NS_PORT */
}
コード例 #5
0
void NS_CLASS aodv_socket_init()
{
#ifndef NS_PORT
    struct sockaddr_in aodv_addr;
    struct ifreq ifr;
    int i, retval = 0;
    int on = 1;
    int tos = IPTOS_LOWDELAY;
    int bufsize = SO_RECVBUF_SIZE;

    /* Create a UDP socket */

    if (this_host.nif == 0) {
	fprintf(stderr, "No interfaces configured\n");
	exit(-1);
    }

    /* Open a socket for every AODV enabled interface */
    for (i = 0; i < MAX_NR_INTERFACES; i++) {
	if (!DEV_NR(i).enabled)
	    continue;
#ifdef RAW_SOCKET
	DEV_NR(i).sock = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
#else
	DEV_NR(i).sock = socket(PF_INET, SOCK_DGRAM, 0);
#endif

	if (DEV_NR(i).sock < 0) {
	    perror("");
	    exit(-1);
	}

	/* Bind the socket to the AODV port number */
	memset(&aodv_addr, 0, sizeof(aodv_addr));
	aodv_addr.sin_family = AF_INET;
	aodv_addr.sin_port = htons(AODV_PORT);
	aodv_addr.sin_addr.s_addr = htonl(INADDR_ANY);

	retval = bind(DEV_NR(i).sock, (struct sockaddr *) &aodv_addr,
		      sizeof(struct sockaddr));

	if (retval < 0) {
	    perror("Bind failed ");
	    exit(-1);
	}
	if (setsockopt(DEV_NR(i).sock, SOL_SOCKET, SO_BROADCAST,
		       &on, sizeof(int)) < 0) {
	    perror("SO_BROADCAST failed ");
	    exit(-1);
	}

	memset(&ifr, 0, sizeof(struct ifreq));
	strcpy(ifr.ifr_name, DEV_NR(i).ifname);

	if (setsockopt(DEV_NR(i).sock, SOL_SOCKET, SO_BINDTODEVICE,
		       &ifr, sizeof(ifr)) < 0) {
	    fprintf(stderr, "SO_BINDTODEVICE failed for %s", DEV_NR(i).ifname);
	    perror(" ");
	    exit(-1);
	}
	if (setsockopt(DEV_NR(i).sock, SOL_SOCKET, SO_PRIORITY,
		       &tos, sizeof(int)) < 0) {
	    perror("Setsockopt SO_PRIORITY failed ");
	    exit(-1);
	}
#ifdef RAW_SOCKET
	/* We provide our own IP header... */

	if (setsockopt(DEV_NR(i).sock, SOL_IP, IP_HDRINCL,
		       &on, sizeof(int)) < 0) {
	    perror("Setsockopt IP_HDRINCL failed ");
	    exit(-1);
	}
#else

	if (setsockopt(DEV_NR(i).sock, SOL_IP, IP_PKTINFO,
		       &on, sizeof(int)) < 0) {
	    perror("Setsockopt IP_PKTINFO failed ");
	    exit(-1);
	}
	if (setsockopt(DEV_NR(i).sock, SOL_IP, IP_RECVTTL,
		       &on, sizeof(int)) < 0) {
	    perror("Setsockopt IP_TTL failed ");
	    exit(-1);
	}
#endif
	/* Set max allowable receive buffer size... */
	for (;; bufsize -= 1024) {
	    if (setsockopt(DEV_NR(i).sock, SOL_SOCKET, SO_RCVBUF,
			   (char *) &bufsize, sizeof(bufsize)) == 0) {
		log(LOG_NOTICE, 0, __FUNCTION__,
		    "Receive buffer size set to %d", bufsize);
		break;
	    }
	    if (bufsize < RECV_BUF_SIZE) {
		log(LOG_ERR, 0, __FUNCTION__,
		    "Could not set receive buffer size");
		exit(-1);
	    }
	}

	retval = attach_callback_func(DEV_NR(i).sock, aodv_socket_read);

	if (retval < 0) {
	    perror("register input handler failed ");
	    exit(-1);
	}
    }
#endif				/* NS_PORT */
    time_last_rreq.tv_sec = 0;
    time_last_rreq.tv_usec = 0;
    time_last_rerr.tv_sec = 0;
    time_last_rerr.tv_usec = 0;
/*      gettimeofday(&time_last_rerr, NULL); */
    num_rreq = 0;
    num_rerr = 0;
}