ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Would you know a possible code to get the PC's hostname in C language of a given IP?
Here a possible code to check an ip.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <arpa/inet.h>
int isIP(char *ip)
{
char *data_ptr = ip; // Create a pointer to passed data
int orig_str_size = 0; // Create an int to hold passed data size
int str_index = 0; // Create an int to iterate individual ip characters
int dot_count = 0; // Create an int to check for the number of dots
// Count the number of characters in data_ptr
while (*data_ptr++ != '\0'){ orig_str_size++; }
if(orig_str_size <= 0) // If nothing
{
printf("Get a grip, ip is empty\n\n");
exit(0);
}
else // If within IPv4 size range
if(orig_str_size >= 7 && orig_str_size <= INET_ADDRSTRLEN)
{
char *data1_ptr = ip; // Create a pointer to passed data
printf("Within IPv4 range, %i characters in length\n\n", orig_str_size);
// Count the number of dots in the string, 3 for IPv4
for(str_index; str_index < orig_str_size; str_index++)
{
if(data1_ptr[str_index] == '.'){ dot_count++; }
}
// If theres 3 dots, while ignoring dots, check each char is a digit
if(dot_count == 3)
{
printf("There's three dots in the string\n\n");
data1_ptr = ip;
str_index = 0;
// Iterate the string char by char
for(str_index; str_index < orig_str_size; str_index++)
{
// Ignoring dots
if(data1_ptr[str_index] != '.')
{
// If isdigit() is happy its a digit and isalpha() happy not alphabetic
if(isdigit(data1_ptr[str_index]) && !isalpha(data1_ptr[str_index]))
{
printf("Digit found and is not alphabetic\n\n");
continue;
}
else
if(!isdigit(data1_ptr[str_index]) && isalpha(data1_ptr[str_index]))
{
printf("Not a recognised IPv4 address, character detected in string\n\n");
exit(0);
}
}
}
return 0;
}
}
else // If IPv6
if(orig_str_size > 0 && orig_str_size > INET_ADDRSTRLEN && orig_str_size <= INET6_ADDRSTRLEN)
{
printf("Within IPv6 range %i\n\n", orig_str_size);
return 0;
}
else
{
printf("Unknown target format, the format you provided as a target is not implemented\n\n");
exit(0);
}
}
int main(){
isIP( "10.0.0.9" );
}
it looks nice but give error while compiling with gcc.
herewith the code:
Code:
#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
char *GETHOSTBYADDR(const char *INADDR_ANY)
{
struct sockaddr_in
{
short sin_family; // e.g. AF_INET, AF_INET6
unsigned short sin_port; // e.g. htons(80)
struct in_addr sin_addr; // see struct in_addr, below
char sin_zero[8]; // zero this if you want to
};
struct in_addr
{
unsigned long s_addr; // load with inet_pton()
};
struct sockaddr_in *SA;
SA.sin_addr.s_addr = INADDR_ANY; // set the local IP address
SA.sin_port = htons(80); // set the port number
static char HOSTNAME[128] = "";
getnameinfo(SA, sizeof(SA), HOSTNAME, sizeof(HOSTNAME), NULL, 0, NI_NAMEREQD);
return HOSTNAME;
}
int main(void)
{
puts(GETHOSTBYADDR("209.85.229.106")); // testing on google's IP
return 0;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.