Skip to content

Instantly share code, notes, and snippets.

@javiermon
Last active July 8, 2024 06:36
Show Gist options
  • Select an option

  • Save javiermon/6272065 to your computer and use it in GitHub Desktop.

Select an option

Save javiermon/6272065 to your computer and use it in GitHub Desktop.

Revisions

  1. javiermon revised this gist Aug 13, 2021. 3 changed files with 17 additions and 31 deletions.
    17 changes: 17 additions & 0 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
    15 changes: 0 additions & 15 deletions gateway_netlink.c
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,3 @@
    /*
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    */

    #include <sys/socket.h>
    #include <stdlib.h>
    #include <stdio.h>
    16 changes: 0 additions & 16 deletions gateway_proc.c
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,3 @@
    /*
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    */


    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
  2. javiermon revised this gist Oct 7, 2013. 2 changed files with 25 additions and 26 deletions.
    25 changes: 12 additions & 13 deletions gateway_netlink.c
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,16 @@
    /*
    * This program is free software; you can redistribute it and/or modify
    * it under the terms of the GNU General Public License as published by
    * the Free Software Foundation; either version 2 of the License, or
    * (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details.
    *
    * You should have received a copy of the GNU General Public License
    * along with this program; if not, write to the Free Software
    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    */

    #include <sys/socket.h>
    26 changes: 13 additions & 13 deletions gateway_proc.c
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,19 @@
    /*
    * This program is free software; you can redistribute it and/or modify
    * it under the terms of the GNU General Public License as published by
    * the Free Software Foundation; either version 2 of the License, or
    * (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details.
    *
    * You should have received a copy of the GNU General Public License
    * along with this program; if not, write to the Free Software
    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    */


    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
  3. javiermon revised this gist Oct 7, 2013. 2 changed files with 32 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions gateway_netlink.c
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,19 @@
    /*
    * This program is free software; you can redistribute it and/or modify
    * it under the terms of the GNU General Public License as published by
    * the Free Software Foundation; either version 2 of the License, or
    * (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details.
    *
    * You should have received a copy of the GNU General Public License
    * along with this program; if not, write to the Free Software
    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    */

    #include <sys/socket.h>
    #include <stdlib.h>
    #include <stdio.h>
    16 changes: 16 additions & 0 deletions gateway_proc.c
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,19 @@
    /*
    * This program is free software; you can redistribute it and/or modify
    * it under the terms of the GNU General Public License as published by
    * the Free Software Foundation; either version 2 of the License, or
    * (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details.
    *
    * You should have received a copy of the GNU General Public License
    * along with this program; if not, write to the Free Software
    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    */

    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
  4. jmonteagudo revised this gist Aug 20, 2013. 1 changed file with 12 additions and 4 deletions.
    16 changes: 12 additions & 4 deletions gateway_proc.c
    Original file line number Diff line number Diff line change
    @@ -7,14 +7,20 @@
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <string.h>
    #include <net/if.h>

    #define BUFFER_SIZE 4096

    int getgatewayandiface(in_addr_t * addr, char *interface)
    {
    long destination, gateway;
    char iface[256];
    char buf[256];
    char iface[IF_NAMESIZE];
    char buf[BUFFER_SIZE];
    FILE * file;

    memset(iface, 0, sizeof(iface));
    memset(buf, 0, sizeof(buf));

    file = fopen("/proc/net/route", "r");
    if (!file)
    return -1;
    @@ -38,8 +44,10 @@ int getgatewayandiface(in_addr_t * addr, char *interface)

    int main(int argc, char **argv)
    {
    in_addr_t addr;
    char iface[256];
    in_addr_t addr = 0;
    char iface[IF_NAMESIZE];

    memset(iface, 0, sizeof(iface));

    getgatewayandiface(&addr, iface);
    printf("%s\n", inet_ntoa(*(struct in_addr *) &addr));
  5. jmonteagudo revised this gist Aug 20, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gateway_netlink.c
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@

    #define BUFFER_SIZE 4096

    int GetGatewayAddress ()
    int getgatewayandiface()
    {
    int received_bytes = 0, msg_len = 0, route_attribute_len = 0;
    int sock = -1, msgseq = 0;
    @@ -128,6 +128,6 @@ int GetGatewayAddress ()

    int main(int argc, char **argv)
    {
    GetGatewayAddress();
    getgatewayandiface();
    return 0;
    }
  6. jmonteagudo revised this gist Aug 20, 2013. 1 changed file with 49 additions and 0 deletions.
    49 changes: 49 additions & 0 deletions gateway_proc.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <ctype.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <string.h>

    int getgatewayandiface(in_addr_t * addr, char *interface)
    {
    long destination, gateway;
    char iface[256];
    char buf[256];
    FILE * file;

    file = fopen("/proc/net/route", "r");
    if (!file)
    return -1;

    while (fgets(buf, sizeof(buf), file)) {
    if (sscanf(buf, "%s %lx %lx", iface, &destination, &gateway) == 3) {
    if (destination == 0) { /* default */
    *addr = gateway;
    strcpy(interface, iface);
    fclose(file);
    return 0;
    }
    }
    }

    /* default route not found */
    if (file)
    fclose(file);
    return -1;
    }

    int main(int argc, char **argv)
    {
    in_addr_t addr;
    char iface[256];

    getgatewayandiface(&addr, iface);
    printf("%s\n", inet_ntoa(*(struct in_addr *) &addr));
    printf("%s\n", iface);

    return 0;
    }
  7. javiermon renamed this gist Aug 20, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. jmonteagudo revised this gist Aug 19, 2013. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions gateway.c
    Original file line number Diff line number Diff line change
    @@ -28,10 +28,10 @@ int GetGatewayAddress ()
    return EXIT_FAILURE;
    }

    bzero(msgbuf, sizeof(msgbuf));
    bzero(gateway_address, sizeof(gateway_address));
    bzero(interface, sizeof(interface));
    bzero(buffer, sizeof(buffer));
    memset(msgbuf, 0, sizeof(msgbuf));
    memset(gateway_address, 0, sizeof(gateway_address));
    memset(interface, 0, sizeof(interface));
    memset(buffer, 0, sizeof(buffer));

    /* point the header and the msg structure pointers into the buffer */
    nlmsg = (struct nlmsghdr *)msgbuf;
  9. jmonteagudo revised this gist Aug 19, 2013. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions gateway.c
    Original file line number Diff line number Diff line change
    @@ -21,6 +21,7 @@ int GetGatewayAddress ()
    char gateway_address[INET_ADDRSTRLEN], interface[IF_NAMESIZE];
    char msgbuf[BUFFER_SIZE], buffer[BUFFER_SIZE];
    char *ptr = buffer;
    struct timeval tv;

    if ((sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) {
    perror("socket failed");
    @@ -42,10 +43,13 @@ int GetGatewayAddress ()
    nlmsg->nlmsg_seq = msgseq++; // Sequence of the message packet.
    nlmsg->nlmsg_pid = getpid(); // PID of process sending the request.

    /* 1 Sec Timeout to avoid stall */
    tv.tv_sec = 1;
    setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval));
    /* send msg */
    if (send(sock, nlmsg, nlmsg->nlmsg_len, 0) < 0) {
    printf("send failed");
    return -1;
    perror("send failed");
    return EXIT_FAILURE;
    }

    /* receive response */
  10. jmonteagudo revised this gist Aug 19, 2013. 1 changed file with 37 additions and 41 deletions.
    78 changes: 37 additions & 41 deletions gateway.c
    Original file line number Diff line number Diff line change
    @@ -10,23 +10,45 @@

    #define BUFFER_SIZE 4096

    int GetGatewayAddress (int sock, struct nlmsghdr *nlhdr, int seqnum)
    int GetGatewayAddress ()
    {
    int received_bytes = 0, msg_len = 0;
    struct nlmsghdr *nlh;
    char gateway_address[INET_ADDRSTRLEN];
    char interface[IF_NAMESIZE];
    int received_bytes = 0, msg_len = 0, route_attribute_len = 0;
    int sock = -1, msgseq = 0;
    struct nlmsghdr *nlh, *nlmsg;
    struct rtmsg *route_entry;
    // This struct contain route attributes (route type)
    struct rtattr *route_attribute;
    int route_attribute_len = 0;
    char buffer[BUFFER_SIZE];
    char gateway_address[INET_ADDRSTRLEN], interface[IF_NAMESIZE];
    char msgbuf[BUFFER_SIZE], buffer[BUFFER_SIZE];
    char *ptr = buffer;

    if ((sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) {
    perror("socket failed");
    return EXIT_FAILURE;
    }

    bzero(msgbuf, sizeof(msgbuf));
    bzero(gateway_address, sizeof(gateway_address));
    bzero(interface, sizeof(interface));
    bzero(buffer, sizeof(buffer));

    /* point the header and the msg structure pointers into the buffer */
    nlmsg = (struct nlmsghdr *)msgbuf;

    /* Fill in the nlmsg header*/
    nlmsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
    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 msg */
    if (send(sock, nlmsg, nlmsg->nlmsg_len, 0) < 0) {
    printf("send failed");
    return -1;
    }

    /* receive response */
    do
    {
    received_bytes = recv(sock, ptr, sizeof(buffer) - msg_len, 0);
    @@ -38,8 +60,8 @@ int GetGatewayAddress (int sock, struct nlmsghdr *nlhdr, int seqnum)
    nlh = (struct nlmsghdr *) ptr;

    /* Check if the header is valid */
    if((NLMSG_OK(nlhdr, received_bytes) == 0) ||
    (nlhdr->nlmsg_type == NLMSG_ERROR))
    if((NLMSG_OK(nlmsg, received_bytes) == 0) ||
    (nlmsg->nlmsg_type == NLMSG_ERROR))
    {
    perror("Error in received packet");
    return EXIT_FAILURE;
    @@ -54,11 +76,12 @@ int GetGatewayAddress (int sock, struct nlmsghdr *nlhdr, int seqnum)
    }

    /* Break if its not a multi part message */
    if ((nlhdr->nlmsg_flags & NLM_F_MULTI) == 0)
    if ((nlmsg->nlmsg_flags & NLM_F_MULTI) == 0)
    break;
    }
    while ((nlhdr->nlmsg_seq != seqnum) || (nlhdr->nlmsg_pid != getpid()));
    while ((nlmsg->nlmsg_seq != msgseq) || (nlmsg->nlmsg_pid != getpid()));

    /* parse response */
    for ( ; NLMSG_OK(nlh, received_bytes); nlh = NLMSG_NEXT(nlh, received_bytes))
    {
    /* Get the route data */
    @@ -94,40 +117,13 @@ int GetGatewayAddress (int sock, struct nlmsghdr *nlhdr, int seqnum)
    }
    }

    close(sock);

    return 0;
    }

    int main(int argc, char **argv)
    {
    struct nlmsghdr *nlmsg;
    char msgbuf[BUFFER_SIZE];
    int sock, msgseq = 0;

    if ((sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) {
    perror("socket failed");
    return EXIT_FAILURE;
    }

    memset(msgbuf, 0, BUFFER_SIZE);

    /* point the header and the msg structure pointers into the buffer */
    nlmsg = (struct nlmsghdr *)msgbuf;

    /* Fill in the nlmsg header*/
    nlmsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
    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.

    if (send(sock, nlmsg, nlmsg->nlmsg_len, 0) < 0) {
    printf("send failed");
    return -1;
    }

    GetGatewayAddress(sock, nlmsg, msgseq);

    close(sock);

    GetGatewayAddress();
    return 0;
    }
  11. javiermon revised this gist Aug 19, 2013. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions gateway.c
    Original file line number Diff line number Diff line change
    @@ -53,11 +53,9 @@ int GetGatewayAddress (int sock, struct nlmsghdr *nlhdr, int seqnum)
    msg_len += received_bytes;
    }

    /* Check if its a multi part message */
    if ((nlhdr->nlmsg_flags & NLM_F_MULTI) == 0) {
    /* return if its not */
    /* Break if its not a multi part message */
    if ((nlhdr->nlmsg_flags & NLM_F_MULTI) == 0)
    break;
    }
    }
    while ((nlhdr->nlmsg_seq != seqnum) || (nlhdr->nlmsg_pid != getpid()));

  12. javiermon revised this gist Aug 19, 2013. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions gateway.c
    Original file line number Diff line number Diff line change
    @@ -41,11 +41,11 @@ int GetGatewayAddress (int sock, struct nlmsghdr *nlhdr, int seqnum)
    if((NLMSG_OK(nlhdr, received_bytes) == 0) ||
    (nlhdr->nlmsg_type == NLMSG_ERROR))
    {
    perror("Error in recieved packet");
    perror("Error in received packet");
    return EXIT_FAILURE;
    }

    /* If we received all data ---> break */
    /* If we received all data break */
    if (nlh->nlmsg_type == NLMSG_DONE)
    break;
    else {
    @@ -72,6 +72,7 @@ int GetGatewayAddress (int sock, struct nlmsghdr *nlhdr, int seqnum)

    route_attribute = (struct rtattr *) RTM_RTA(route_entry);
    route_attribute_len = RTM_PAYLOAD(nlh);

    /* Loop through all attributes */
    for ( ; RTA_OK(route_attribute, route_attribute_len);
    route_attribute = RTA_NEXT(route_attribute, route_attribute_len))
    @@ -104,24 +105,24 @@ int main(int argc, char **argv)
    char msgbuf[BUFFER_SIZE];
    int sock, msgseq = 0;

    if ((sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0)
    {
    if ((sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) {
    perror("socket failed");
    return EXIT_FAILURE;
    }

    memset(msgbuf, 0, BUFFER_SIZE);

    /* point the header and the msg structure pointers into the buffer */
    nlmsg = (struct nlmsghdr *)msgbuf;

    /* Fill in the nlmsg header*/
    nlmsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); // Length of message.
    nlmsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
    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.

    if(send(sock, nlmsg, nlmsg->nlmsg_len, 0) < 0){
    if (send(sock, nlmsg, nlmsg->nlmsg_len, 0) < 0) {
    printf("send failed");
    return -1;
    }
  13. jmonteagudo revised this gist Aug 19, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gateway.c
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    #include <unistd.h>
    #include <net/if.h>

    #define BUFFER_SIZE 4095
    #define BUFFER_SIZE 4096

    int GetGatewayAddress (int sock, struct nlmsghdr *nlhdr, int seqnum)
    {
  14. jmonteagudo revised this gist Aug 19, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gateway.c
    Original file line number Diff line number Diff line change
    @@ -14,8 +14,8 @@ int GetGatewayAddress (int sock, struct nlmsghdr *nlhdr, int seqnum)
    {
    int received_bytes = 0, msg_len = 0;
    struct nlmsghdr *nlh;
    char gateway_address[32];
    char interface[32];
    char gateway_address[INET_ADDRSTRLEN];
    char interface[IF_NAMESIZE];
    struct rtmsg *route_entry;
    // This struct contain route attributes (route type)
    struct rtattr *route_attribute;
  15. jmonteagudo revised this gist Aug 19, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion gateway.c
    Original file line number Diff line number Diff line change
    @@ -90,7 +90,6 @@ int GetGatewayAddress (int sock, struct nlmsghdr *nlhdr, int seqnum)
    }

    if ((*gateway_address) && (*interface)) {
    /* Now we can dump the routing attributes */
    fprintf(stdout, "Gateway %s for interface %s\n", gateway_address, interface);
    break;
    }
  16. jmonteagudo revised this gist Aug 19, 2013. 1 changed file with 15 additions and 8 deletions.
    23 changes: 15 additions & 8 deletions gateway.c
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,7 @@
    #include <linux/rtnetlink.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <net/if.h>

    #define BUFFER_SIZE 4095

    @@ -14,6 +15,7 @@ int GetGatewayAddress (int sock, struct nlmsghdr *nlhdr, int seqnum)
    int received_bytes = 0, msg_len = 0;
    struct nlmsghdr *nlh;
    char gateway_address[32];
    char interface[32];
    struct rtmsg *route_entry;
    // This struct contain route attributes (route type)
    struct rtattr *route_attribute;
    @@ -22,6 +24,7 @@ int GetGatewayAddress (int sock, struct nlmsghdr *nlhdr, int seqnum)
    char *ptr = buffer;

    bzero(gateway_address, sizeof(gateway_address));
    bzero(interface, sizeof(interface));
    bzero(buffer, sizeof(buffer));

    do
    @@ -73,19 +76,23 @@ int GetGatewayAddress (int sock, struct nlmsghdr *nlhdr, int seqnum)
    for ( ; RTA_OK(route_attribute, route_attribute_len);
    route_attribute = RTA_NEXT(route_attribute, route_attribute_len))
    {
    /* Get the gateway (Next hop) */
    if (route_attribute->rta_type == RTA_GATEWAY)
    {
    switch(route_attribute->rta_type) {
    case RTA_OIF:
    if_indextoname(*(int *)RTA_DATA(route_attribute), interface);
    break;
    case RTA_GATEWAY:
    inet_ntop(AF_INET, RTA_DATA(route_attribute),
    gateway_address, sizeof(gateway_address));

    /* Now we can dump the routing attributes */
    fprintf(stdout, "Gateway %s\n", gateway_address);
    break;
    default:
    break;
    }
    }

    if (*gateway_address)
    break;
    if ((*gateway_address) && (*interface)) {
    /* Now we can dump the routing attributes */
    fprintf(stdout, "Gateway %s for interface %s\n", gateway_address, interface);
    break;
    }
    }

  17. javiermon revised this gist Aug 19, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gateway.c
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@

    #define BUFFER_SIZE 4095

    int GetGatewayAddress (int sock, struct nlmsghdr * nlhdr, int seqnum)
    int GetGatewayAddress (int sock, struct nlmsghdr *nlhdr, int seqnum)
    {
    int received_bytes = 0, msg_len = 0;
    struct nlmsghdr *nlh;
  18. jmonteagudo revised this gist Aug 19, 2013. 1 changed file with 28 additions and 16 deletions.
    44 changes: 28 additions & 16 deletions gateway.c
    Original file line number Diff line number Diff line change
    @@ -9,36 +9,54 @@

    #define BUFFER_SIZE 4095

    int GetGatewayAddress (int sock, struct sockaddr_nl *addr)
    int GetGatewayAddress (int sock, struct nlmsghdr * nlhdr, int seqnum)
    {
    int received_bytes = 0;
    int received_bytes = 0, msg_len = 0;
    struct nlmsghdr *nlh;
    char gateway_address[32];
    struct rtmsg *route_entry;
    // This struct contain route attributes (route type)
    struct rtattr *route_attribute;
    int route_attribute_len = 0;
    char buffer[BUFFER_SIZE];
    char *ptr = buffer;

    bzero(gateway_address, sizeof(gateway_address));
    bzero(buffer, sizeof(buffer));

    while (1)
    do
    {
    received_bytes = recv(sock, buffer, sizeof(buffer), 0);
    received_bytes = recv(sock, ptr, sizeof(buffer) - msg_len, 0);
    if (received_bytes < 0) {
    perror("recv");
    perror("Error in recv");
    return EXIT_FAILURE;
    }

    nlh = (struct nlmsghdr *) ptr;

    /* Check if the header is valid */
    if((NLMSG_OK(nlhdr, received_bytes) == 0) ||
    (nlhdr->nlmsg_type == NLMSG_ERROR))
    {
    perror("Error in recieved packet");
    return EXIT_FAILURE;
    }

    nlh = (struct nlmsghdr *) buffer;
    /* If we received all data ---> break */
    if (nlh->nlmsg_type == NLMSG_DONE)
    break;
    /* We are just interested in Routing information */
    if (addr->nl_groups == RTMGRP_IPV4_ROUTE)
    else {
    ptr += received_bytes;
    msg_len += received_bytes;
    }

    /* Check if its a multi part message */
    if ((nlhdr->nlmsg_flags & NLM_F_MULTI) == 0) {
    /* return if its not */
    break;
    }
    }
    while ((nlhdr->nlmsg_seq != seqnum) || (nlhdr->nlmsg_pid != getpid()));

    for ( ; NLMSG_OK(nlh, received_bytes); nlh = NLMSG_NEXT(nlh, received_bytes))
    {
    @@ -76,22 +94,16 @@ int GetGatewayAddress (int sock, struct sockaddr_nl *addr)

    int main(int argc, char **argv)
    {
    struct sockaddr_nl addr;
    struct nlmsghdr *nlmsg;
    char msgbuf[BUFFER_SIZE];
    int sock, msgseq = 0;

    bzero (&addr, sizeof(addr));

    if ((sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0)
    {
    perror("socket failed");
    return EXIT_FAILURE;
    }

    addr.nl_family = AF_NETLINK;
    addr.nl_groups = RTMGRP_IPV4_ROUTE;

    memset(msgbuf, 0, BUFFER_SIZE);
    /* point the header and the msg structure pointers into the buffer */
    nlmsg = (struct nlmsghdr *)msgbuf;
    @@ -108,9 +120,9 @@ int main(int argc, char **argv)
    return -1;
    }

    GetGatewayAddress(sock, &addr);
    GetGatewayAddress(sock, nlmsg, msgseq);

    close(sock);

    return 0;
    }
    }
  19. javiermon revised this gist Aug 19, 2013. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions gateway.c
    Original file line number Diff line number Diff line change
    @@ -81,7 +81,6 @@ int main(int argc, char **argv)
    char msgbuf[BUFFER_SIZE];
    int sock, msgseq = 0;

    /* Zeroing addr */
    bzero (&addr, sizeof(addr));

    if ((sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0)
    @@ -104,15 +103,13 @@ int main(int argc, char **argv)
    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("send failed");
    return -1;
    }

    GetGatewayAddress(sock, &addr);

    /* Close socket */
    close(sock);

    return 0;
  20. javiermon revised this gist Aug 19, 2013. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions gateway.c
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,6 @@ int GetGatewayAddress (int sock, struct sockaddr_nl *addr)
    bzero(gateway_address, sizeof(gateway_address));
    bzero(buffer, sizeof(buffer));

    /* Receiving netlink socket data */
    while (1)
    {
    received_bytes = recv(sock, buffer, sizeof(buffer), 0);
    @@ -36,7 +35,7 @@ int GetGatewayAddress (int sock, struct sockaddr_nl *addr)
    /* If we received all data ---> break */
    if (nlh->nlmsg_type == NLMSG_DONE)
    break;
    /* We are just intrested in Routing information */
    /* We are just interested in Routing information */
    if (addr->nl_groups == RTMGRP_IPV4_ROUTE)
    break;
    }
    @@ -46,7 +45,7 @@ int GetGatewayAddress (int sock, struct sockaddr_nl *addr)
    /* Get the route data */
    route_entry = (struct rtmsg *) NLMSG_DATA(nlh);

    /* We are just intrested in main routing table */
    /* We are just interested in main routing table */
    if (route_entry->rtm_table != RT_TABLE_MAIN)
    continue;

  21. javiermon created this gist Aug 19, 2013.
    120 changes: 120 additions & 0 deletions gateway.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,120 @@
    #include <sys/socket.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <linux/netlink.h>
    #include <linux/rtnetlink.h>
    #include <arpa/inet.h>
    #include <unistd.h>

    #define BUFFER_SIZE 4095

    int GetGatewayAddress (int sock, struct sockaddr_nl *addr)
    {
    int received_bytes = 0;
    struct nlmsghdr *nlh;
    char gateway_address[32];
    struct rtmsg *route_entry;
    // This struct contain route attributes (route type)
    struct rtattr *route_attribute;
    int route_attribute_len = 0;
    char buffer[BUFFER_SIZE];

    bzero(gateway_address, sizeof(gateway_address));
    bzero(buffer, sizeof(buffer));

    /* Receiving netlink socket data */
    while (1)
    {
    received_bytes = recv(sock, buffer, sizeof(buffer), 0);
    if (received_bytes < 0) {
    perror("recv");
    return EXIT_FAILURE;
    }

    nlh = (struct nlmsghdr *) buffer;
    /* If we received all data ---> break */
    if (nlh->nlmsg_type == NLMSG_DONE)
    break;
    /* We are just intrested in Routing information */
    if (addr->nl_groups == RTMGRP_IPV4_ROUTE)
    break;
    }

    for ( ; NLMSG_OK(nlh, received_bytes); nlh = NLMSG_NEXT(nlh, received_bytes))
    {
    /* Get the route data */
    route_entry = (struct rtmsg *) NLMSG_DATA(nlh);

    /* We are just intrested in main routing table */
    if (route_entry->rtm_table != RT_TABLE_MAIN)
    continue;

    route_attribute = (struct rtattr *) RTM_RTA(route_entry);
    route_attribute_len = RTM_PAYLOAD(nlh);
    /* Loop through all attributes */
    for ( ; RTA_OK(route_attribute, route_attribute_len);
    route_attribute = RTA_NEXT(route_attribute, route_attribute_len))
    {
    /* Get the gateway (Next hop) */
    if (route_attribute->rta_type == RTA_GATEWAY)
    {
    inet_ntop(AF_INET, RTA_DATA(route_attribute),
    gateway_address, sizeof(gateway_address));

    /* Now we can dump the routing attributes */
    fprintf(stdout, "Gateway %s\n", gateway_address);
    break;
    }

    if (*gateway_address)
    break;
    }
    }

    return 0;
    }

    int main(int argc, char **argv)
    {
    struct sockaddr_nl addr;
    struct nlmsghdr *nlmsg;
    char msgbuf[BUFFER_SIZE];
    int sock, msgseq = 0;

    /* Zeroing addr */
    bzero (&addr, sizeof(addr));

    if ((sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0)
    {
    perror("socket failed");
    return EXIT_FAILURE;
    }

    addr.nl_family = AF_NETLINK;
    addr.nl_groups = RTMGRP_IPV4_ROUTE;

    memset(msgbuf, 0, BUFFER_SIZE);
    /* point the header and the msg structure pointers into the buffer */
    nlmsg = (struct nlmsghdr *)msgbuf;

    /* 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("send failed");
    return -1;
    }

    GetGatewayAddress(sock, &addr);

    /* Close socket */
    close(sock);

    return 0;
    }