Skip to content

Instantly share code, notes, and snippets.

@TuxSH
Created February 19, 2018 08:35
Show Gist options
  • Save TuxSH/4751127340f98ad795f2f71b5c993d59 to your computer and use it in GitHub Desktop.
Save TuxSH/4751127340f98ad795f2f71b5c993d59 to your computer and use it in GitHub Desktop.
ifcfg
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <switch.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
//Result rr;
void test(void) {
struct ifconf ifcfg;
struct ifreq ifs[5];
int fd = socket(AF_INET, SOCK_STREAM, 0); // bsd:u restricts our choices...
if(fd == -1) { perror("socket "); printf("%x\n", socketGetLastBsdResult()); goto ret; }
ifcfg.ifc_len = (int)sizeof(ifs);
ifcfg.ifc_req = ifs;
if(ioctl(fd, SIOCGIFCONF, &ifcfg) == -1) { perror("ifconf "); printf("%x\n", socketGetLastBsdResult()); goto ret; }
for(int i = 0; i < ifcfg.ifc_len / (int)sizeof(ifs); i++) {
printf("%s\t", ifs[i].ifr_name);
if(ioctl(fd, SIOCGIFADDR, &ifs[i].ifr_addr) == -1) { perror("ifreq "); printf("%x\n", socketGetLastBsdResult()); goto ret; }
if(ifs[i].ifr_addr.sa_family == AF_INET) {
in_addr_t cur = ((struct sockaddr_in *)&ifs[i].ifr_addr)->sin_addr.s_addr;
printf("%s\n", inet_ntoa(((struct sockaddr_in *)&ifs[i].ifr_addr)->sin_addr));
}
}
ret:
close(fd);
}
int main(int argc, char **argv)
{
gfxInitDefault();
//Initialize console. Using NULL as the second argument tells the console library to use the internal console structure as current one.
consoleInit(NULL);
//Move the cursor to row 16 and column 20 and then prints "Hello World!"
//To move the cursor you have to print "\x1b[r;cH", where r and c are respectively
//the row and column where you want your cursor to move
socketInitializeDefault();
test();
socketExit();
while(true)
{
//Scan all the inputs. This should be done once for each frame
hidScanInput();
if (hidKeysDown(CONTROLLER_P1_AUTO) & KEY_PLUS) break;
gfxFlushBuffers();
gfxSwapBuffers();
gfxWaitForVsync();
}
gfxExit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment