Skip to content

Instantly share code, notes, and snippets.

@javiermon
Last active April 7, 2024 17:22
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save javiermon/6272065 to your computer and use it in GitHub Desktop.
Save javiermon/6272065 to your computer and use it in GitHub Desktop.
get default gateway
#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>
#include <net/if.h>
#define BUFFER_SIZE 4096
int getgatewayandiface(in_addr_t * addr, char *interface)
{
long destination, gateway;
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;
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 = 0;
char iface[IF_NAMESIZE];
memset(iface, 0, sizeof(iface));
getgatewayandiface(&addr, iface);
printf("%s\n", inet_ntoa(*(struct in_addr *) &addr));
printf("%s\n", iface);
return 0;
}
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.
@mmunz
Copy link

mmunz commented Oct 5, 2013

@javiermon Hi, what is the license of the netlink code?

@javiermon
Copy link
Author

gplv2

@mmunz
Copy link

mmunz commented Oct 7, 2013

Would you be willing to relicense it to fit olsrd's BSD-style license? See http://olsr.org/git/?p=olsrd.git;a=blob;f=license.txt

@javiermon
Copy link
Author

Changed to Apache 2.0 license. I hope this suits your needs.

@FernandoS27
Copy link

FernandoS27 commented Aug 13, 2021

Why making an even worse license that's compatible with just gplv3?

We need it for a gplv2+ project.

@javiermon
Copy link
Author

javiermon commented Aug 13, 2021

Changed to MIT License, I hope this works for everyone and we can all move on with our lives 😌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment