Exemplo n.º 1
0
int
get_gateway (char *gateway)
{
	struct nlmsghdr *nlMsg;
	//struct rtmsg *rtMsg;
	struct route_info *rtInfo;
	char msgBuf[BUFSIZE];

	int sock, len, msgSeq = 0;
	//?? Socket
	if ((sock = socket (PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
		cfg_error("Socket Creation: \n");
		return -1;
	}

	/* Initialize the buffer */
	memset (msgBuf, 0, BUFSIZE);

	/* point the header and the msg structure pointers into the buffer */
	nlMsg = (struct nlmsghdr *)msgBuf;
	//rtMsg = (struct rtmsg *)NLMSG_DATA(nlMsg);

	/* Fill in the nlmsg header */
	nlMsg->nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg)); // Length of message.
	nlMsg->nlmsg_type = RTM_GETROUTE; // Get the routes from kernel routing table .

	nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST; // The message is a request for dump.
	nlMsg->nlmsg_seq = msgSeq++;/*lint !e632*/
	// Sequence of the message packet.

	nlMsg->nlmsg_pid = getpid ();/*lint !e632*/
// PID of process sending the request.

	/* Send the request */
	if (send (sock, nlMsg, (size_t)(nlMsg->nlmsg_len), 0) < 0) {
		cfg_error("Write To Socket Failed...\n");
		return -1;
	}

	/* Read the response */
	if ((len = readNlSock (sock, msgBuf, msgSeq, getpid ())) < 0) {
		cfg_error("Read From Socket Failed...\n");
		return -1;
	}
	/* Parse and print the response */
	rtInfo = (struct route_info *)malloc (sizeof (struct route_info));
	if (NULL != rtInfo) {
		for (; NLMSG_OK (nlMsg, len); nlMsg = NLMSG_NEXT (nlMsg, len)) { /*lint !e574 */
			/*lint !e574 */
			memset (rtInfo, 0, sizeof (struct route_info));
			parseRoutes (nlMsg, rtInfo, gateway);
		}
		free (rtInfo);
	}
	close (sock);
	return 0;
}
Exemplo n.º 2
0
    QString NetworkHelperImplLinux::getDefaultGatewayAsString() {
        QString ret;

        int sock, msgSeq = 0;
        if ((sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
            perror("Socket Creation: ");
            return ret;
        }

        char msgBuf[BUFSIZE];
        memset(msgBuf, 0, BUFSIZE);
        struct nlmsghdr *nlMsg = (struct nlmsghdr *)msgBuf;

        nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
        nlMsg->nlmsg_type = RTM_GETROUTE;

        nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
        nlMsg->nlmsg_seq = msgSeq++;
        nlMsg->nlmsg_pid = getpid();

        if (send(sock, nlMsg, nlMsg->nlmsg_len, 0) < 0)
            return ret;

        int len;
        if ((len = readNlSock(sock, msgBuf, msgSeq, getpid())) < 0)
            return ret;

        struct route_info *rtInfo = (struct route_info *)malloc(sizeof(struct route_info));

        for (; NLMSG_OK(nlMsg, len); nlMsg = NLMSG_NEXT(nlMsg, len)) {
            memset(rtInfo, 0, sizeof(struct route_info));
            parseRoutes(nlMsg, rtInfo);

            if (strstr((char *)inet_ntoa(rtInfo->dstAddr), "0.0.0.0") && !strstr((char *)inet_ntoa(rtInfo->gateWay), "0.0.0.0")) {
                char buf[64];
                inet_ntop(AF_INET, &rtInfo->gateWay, buf, sizeof(buf));
                ret = QString(buf);
                break;
            }
        }

        free(rtInfo);
        close(sock);

        return ret;
    }
Exemplo n.º 3
0
int get_gateway(char *gateway)
{
	 struct nlmsghdr *nlMsg;
	  struct rtmsg *rtMsg;
	   struct route_info *rtInfo;
	    char msgBuf[BUFSIZE];
		 
		 int sock, len, msgSeq = 0;
		  char buff[1024];
		   
		   
		   /* Create Socket */
		   if((sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0)
			    perror("Socket Creation: ");
		    
		    /* Initialize the buffer */
		    memset(msgBuf, 0, BUFSIZE);
			 
			 /* point the header and the msg structure pointers into the buffer */
			 nlMsg = (struct nlmsghdr *)msgBuf;
			  rtMsg = (struct rtmsg *)NLMSG_DATA(nlMsg);
			   
			   /* Fill in the nlmsg header*/
			   nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); // Length of message.

			    nlMsg->nlmsg_type = RTM_GETROUTE; // Get the routes from kernel routing table .

				 
				 nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST; // The message is a request for dump.

				  nlMsg->nlmsg_seq = msgSeq++; // Sequence of the message packet.

				   nlMsg->nlmsg_pid = getpid(); // PID of process sending the request.

				    
				    /* Send the request */
				    if(send(sock, nlMsg, nlMsg->nlmsg_len, 0) < 0){
						  printf("Write To Socket Failed...\n");
						    return -1;
							 }
					 
					 /* Read the response */
					 if((len = readNlSock(sock, msgBuf, msgSeq, getpid())) < 0) {
						   printf("Read From Socket Failed...\n");
						     return -1;
							  }
					  /* Parse and print the response */
					  rtInfo = (struct route_info *)malloc(sizeof(struct route_info));
					   // ADDED BY BOB

					   /* THIS IS THE NETTSTAT -RL code I commented out the printing here and in parse routes */
					   //fprintf(stdout, "Destination\tGateway\tInterface\tSource\n");

					   for(;NLMSG_OK(nlMsg,len);nlMsg = NLMSG_NEXT(nlMsg,len)){
						     memset(rtInfo, 0, sizeof(struct route_info));
							   parseRoutes(nlMsg, rtInfo,gateway);
							    }
					    free(rtInfo);
						 close(sock);

						  return 0;
}
Exemplo n.º 4
0
int getSenderInterface(unsigned int targetIP, char* device, char* mac)
{
    struct nlmsghdr* nlMsg;

    char               msgBuf[BUFSIZE];

    int sock;
    ssize_t len = 0;
    uint32_t msgSeq = 0;

    if ((sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0)
    {
        syslog(LOG_ERR, "unable to create socket: '%s'", strerror(errno));
        return -1;
    }

    memset(msgBuf, 0, BUFSIZE);

    nlMsg = (struct nlmsghdr *)msgBuf;
    nlMsg->nlmsg_len   = NLMSG_LENGTH(sizeof(struct rtmsg));
    nlMsg->nlmsg_type  = RTM_GETROUTE;
    nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
    nlMsg->nlmsg_seq   = msgSeq++;
    nlMsg->nlmsg_pid   = getpid();

    if (send(sock, nlMsg, nlMsg->nlmsg_len, 0) < 0)
    {
        syslog(LOG_ERR, "unable to write to socket: '%s'", strerror(errno));
        close(sock);
        return -1;
    }

    if ((len = readNlSock(sock, msgBuf, msgSeq, getpid())) < 0)
    {
        syslog(LOG_ERR, "unable to read from socket: '%s'", strerror(errno));
        close(sock);
        return -1;
    }

    for (; nlmsg_ok(nlMsg, len); nlMsg = NLMSG_NEXT(nlMsg, len)) {
        struct rtmsg*  rtMsg = (struct rtmsg *)NLMSG_DATA(nlMsg);

        if (rtMsg->rtm_family == AF_INET || rtMsg->rtm_table == RT_TABLE_MAIN)
        {
            struct rtattr* rtAttr = (struct rtattr *)RTM_RTA(rtMsg);
            int            rtLen  = RTM_PAYLOAD(nlMsg);
            char           ifName[IF_NAMESIZE] = {0};
            unsigned int   dstAddr = 0, dstMask = 1;
            for (; RTA_OK(rtAttr, rtLen); rtAttr = RTA_NEXT(rtAttr, rtLen)) {
                switch (rtAttr->rta_type)
                {
                    case RTA_OIF:
                        if_indextoname(*(int *)RTA_DATA(rtAttr), ifName);
                        break;
                    case RTA_DST:
                        dstAddr = *(u_int *)RTA_DATA(rtAttr);
                        dstMask = rtLen;
                        break;
                }
            }
            if (dstMask <= 32)
            {
                dstMask = htonl(ntohl(inet_addr("255.255.255.255")) << (32 - dstMask));
                if ((dstAddr & dstMask) == (targetIP & dstMask))
                {
                    if (getInterfaceMac(ifName, mac) == 0)
                    {
                        close(sock);
                        snprintf(device, IFNAMSIZ, "%s", ifName);
                        syslog(LOG_INFO, "sending from device '%s' with MAC address '%s'",
                                device,
                                printMACStr(mac));
                        return 0;
                    }
                }
            }
        }
    }
    close(sock);
    return 1;
}
Exemplo n.º 5
0
static int retrieveDefGateway(const char * deviceName, char * defGateway)
{
    struct nlmsghdr *nlMsg = NULL;
    struct rtmsg *rtMsg = NULL;
    struct route_info *rtInfo = NULL;
    char msgBuf[BUFSIZE];

    int sock = -1, len, msgSeq = 0;
    int i_ret = ZQERROR;

    /* Create Socket */
    if((sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0)
    {
        ZError( DBG_ZDEVCFG, "%s", strerror(errno) );
        return i_ret;
    }

    /* Initialize the buffer */
    memset(msgBuf, 0, BUFSIZE);

    /* point the header and the msg structure pointers into the buffer */
    nlMsg = (struct nlmsghdr *)msgBuf;
    rtMsg = (struct rtmsg *)NLMSG_DATA(nlMsg);

    /* Fill in the nlmsg header*/
    nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); /* Length of message.*/
    nlMsg->nlmsg_type = RTM_GETROUTE; /* Get the routes from kernel routing table .*/

    nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST; /* The message is a request for dump.*/
    nlMsg->nlmsg_seq = msgSeq++; /* Sequence of the message packet.*/
    nlMsg->nlmsg_pid = getpid(); /* PID of process sending the request.*/

    /* Send the request */
    if(send(sock, nlMsg, nlMsg->nlmsg_len, 0) < 0)
    {
#ifdef DEBUG
        printf("Write To Socket Failed...\n");
#endif

        goto funcOut;
    }

    /* Read the response */
    if((len = readNlSock(sock, msgBuf, msgSeq, getpid())) < 0)
    {
#ifdef DEBUG
        ZInfo4( DBG_ZDEVCFG,"Read From Socket Failed");
#endif

        goto funcOut;
    }
    /* Parse and print the response */
    rtInfo = (struct route_info *)malloc(sizeof(struct route_info));
    if(!rtInfo)
    {
        ZInfo4( DBG_ZDEVCFG,"Allocation error");
        goto funcOut;
    }

    /* THIS IS THE NETTSTAT -RL code I commented out the printing here and in parse routes */
    //fprintf(stdout, "Destination\tGateway\tInterface\tSource\n");
    for(;NLMSG_OK(nlMsg,len);nlMsg = NLMSG_NEXT(nlMsg,len))
    {
        memset(rtInfo, 0, sizeof(struct route_info));
        if (parseRoutes(deviceName, nlMsg, rtInfo) == ZQSUCCESS)
        {
            i_ret = ZQSUCCESS;
            ZInfo4( DBG_ZDEVCFG, "interface->%s default gateway->%s", deviceName, gateway );
            strcpy(defGateway,gateway);
        }
    }


funcOut:
    if ( rtInfo )
    {free(rtInfo);rtInfo=NULL;}
    if (sock > 0)
	{close(sock);}

    return i_ret;
}