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; }
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; }
ShortestDistance::ShortestDistance(char const* OutputFileName, char const* airportsFileName, char const* routesFileName, char const* airlinesFilename):filename(OutputFileName) { ifstream in(OutputFileName); getline(in,line); if(line.compare("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) { in.close(); clock_t start= clock(); out.open(OutputFileName); airports.open(airportsFileName); routes.open(routesFileName); airlines.open(airlinesFilename); parseAirports(dataMap); parseRoutes(dataMap); parseAirline(carrierNames); writeXML(dataMap, carrierNames); cout << "Parsing: " << (clock() - start)/(double)CLOCKS_PER_SEC << " seconds." << endl; } in.close(); createTable(); }
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; }
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; }
int rawsock_get_default_interface(char *ifname, size_t sizeof_ifname) { int fd; struct nlmsghdr *nlMsg; char msgBuf[16384]; int len; int msgSeq = 0; unsigned ipv4 = 0; /* * Create 'netlink' socket to query kernel */ fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); if (fd < 0) { fprintf(stderr, "%s:%d: socket(NETLINK_ROUTE): %d\n", __FILE__, __LINE__, errno); return errno; } /* * format the netlink buffer */ memset(msgBuf, 0, sizeof(msgBuf)); 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(); /* * send first request to kernel */ if (send(fd, nlMsg, nlMsg->nlmsg_len, 0) < 0) { fprintf(stderr, "%s:%d: send(NETLINK_ROUTE): %d\n", __FILE__, __LINE__, errno); return errno; } /* * Now read all the responses */ len = read_netlink(fd, msgBuf, sizeof(msgBuf), msgSeq, getpid()); if (len <= 0) { fprintf(stderr, "%s:%d: read_netlink: %d\n", __FILE__, __LINE__, errno); return errno; } /* * Parse the response */ for (; NLMSG_OK(nlMsg, len); nlMsg = NLMSG_NEXT(nlMsg, len)) { struct route_info rtInfo[1]; int err; memset(rtInfo, 0, sizeof(struct route_info)); err = parseRoutes(nlMsg, rtInfo); if (err != 0) continue; /* make sure destination = 0.0.0.0 for "default route" */ if (rtInfo->dstAddr.s_addr != 0) continue; /* found the gateway! */ ipv4 = ntohl(rtInfo->gateWay.s_addr); if (ipv4 == 0) continue; strcpy_s(ifname, sizeof_ifname, rtInfo->ifName); } close(fd); return 0; }