Skip to content

Instantly share code, notes, and snippets.

@amarullz
Last active May 18, 2022 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amarullz/144d19925df827b6ce43f0194177652b to your computer and use it in GitHub Desktop.
Save amarullz/144d19925df827b6ce43f0194177652b to your computer and use it in GitHub Desktop.
Get IPv4 Address Programatically

Get IPv4 Address Programatically

For more detail info: https://amarullz.com/2022/05/18/linux-c-fungsi-untuk-mengetahui-ip-address-secara-program/

License

Copyright 2022 Ahmad Amarullah (https://amarullz.com)

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.

/*
* Copyright 2022 Ahmad Amarullah (https://amarullz.com)
*
* 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 <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
/**
* Dapatkan ip address untuk interface tertentu
*
* @param const char * nama interface (eth0, wlan0, lo)
* @param char * output buffer untuk menampung ip address
* @param int ukuran buffer
* @return int Bila berhasil berisi panjang output. 0 bila gagal
*/
int get_ipv4address(const char* iface, char* out, int sz) {
// Pastikan output buffer tidak null
if (!out) return 0;
struct ifaddrs *ifaddr, *ifa;
char host[NI_MAXHOST];
int len = 0;
// Ambil semua alamat interface
if (getifaddrs(&ifaddr) == -1) return 0;
// Loop untuk mencari interface yang dicari
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL) continue;
// Cek kecocokan string
if ((strcmp(ifa->ifa_name, iface) == 0) &&
(ifa->ifa_addr->sa_family == AF_INET)) {
// Copy ip address kedalam variabel
if (getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host,
NI_MAXHOST, NULL, 0, NI_NUMERICHOST) != 0)
return 0;
// Simpan ip address pada output buffer
len = snprintf(out, sz, "%s", host);
// Break agar tidak perlu mencari yang lain
break;
}
}
// Free linked list interface
freeifaddrs(ifaddr);
return len;
}
// Contoh cara penggunaan
int main(int argc, char *argv[]){
char ip[16];
if (get_ipv4address("eth0", ip, 16)>0)
printf("IP Address untuk eth0 adalah: %s\n",ip);
else
printf("IP Address tidak ditemukan\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment