Name server IP of linux machine using C language - c

I have to get name server IP of my system using C language.I am using Linux machine.
I have tried.
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/cdefs.h>
int main()
{
int res_init(void);
printf("_res.nscount %d\n",_res.nscount);
//printf("_res.nsaddr_list[0] %s\n",_res.nsaddr_list[0]);
return 0;
}
But I am getting _res.nscount as 0.Am I doing anything wrong?

You declared res_init() instead of calling it. Try:
Int main()
{
res_init();
/* ... */
However, nsaddr_list[0] isn't a string, so you won't be able to print it with printf("%s"). You'll have to use inet_ntoa() or similar to convert its sin_addr.s_addr value to a printable string.

Related

netdb.h: No such file or directory while trying to run a socket program (in C language) on windows

I have the following C code and I'm required to run it. but I always have the following Error in the used libraries. I tried downloading the MinGW and I added its path to the Environmental variables, but still same problem.
[Error] netdb.h: No such file or directory
I'm trying to run my code on Windows. It is a C language Socket program.
The libraries are:
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
Another question, If I want to run a socket on my laptop as a client and a server (windows), how do I do so?
I do the same stuff at these moment.
I had the problem too. If you run the script on windows, you have to install the right header files. I do it with...
#ifdef _WIN32
//For Windows
int betriebssystem = 1;
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <ws2def.h>
#pragma comment(lib, "Ws2_32.lib")
#include <windows.h>
#include <io.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
int betriebssystem = 2;
#endif
Try to run it. Maybe you have to link the libary for "ws2_32".

Getter & Setter in C

i'm facing a problem in C where i'm trying to use some getter and setter to share a variable between multiple source file.
I declare here my variable (ok_button) with a getter and a setter:
variable.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include "../libhd_src/libhd.h"
int ok_button;
void set_ok_button(int value){
ok_button=value;
printf("Setting ok");
}
int get_ok_button(){
return ok_button;
}
Here, when i push a button, it sets the variable to 1. (Can't upload the full code of this source file, but i see in my logs that the function set_ok_button is correctly execuded when i press (i see the printf "Setting OK" everytime i press my button))
button.c
#include "../libhd_src/libhd.h"
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
void * button_back_center_short(void *arg){
set_ok_button(1);
return 0;
}
And here, i simply check the value of my variable with the getter function.
read.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include "../../libhd_src/libhd.h"
int main(int argc, char **argv){
while(1){
printf("Value %d", get_ok_button());
usleep(500000);
}
}
The problem is that the value shown in read.c is always "0" even when i press my button and set the value to 1...
Does someone understand what's wrong ? Feel free to tell me if you see a better solution to do that :)
I think your problem may be that you have multiple set_ok_button and get_ok_button functions in different files. Make sure you only have them defined in one file, and in a header add 2 lines declaring (but not defining) the functions:
void set_ok_button(int value);
int get_ok_button();

cant get libsensors to work properly

here is my code concerning libsensors.
libraries:
#include <unistd.h>
#include <sensors/sensors.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
code concerning libsensors:
char sd[16384]="^\0",bf[1];
char buf2[8192]="^\0";
sensors_chip_name const* scn;
int c=0;
int t4=1;
while((scn=sensors_get_detected_chips(0,&c))!=0)
{
sensors_feature const *fea;
int f=0;
strcat(sd,scn->prefix);
printf("%s",scn->prefix);
strcat(sd,":");
strcat(sd,scn->path);
strcat(sd,"(");
while((fea=sensors_get_features(scn,&f))!=0)
{
strcat(sd,fea->name);
strcat(sd,"(");
sensors_subfeature const *sb;
int s=0;
while((sb=sensors_get_all_subfeatures(scn,fea,&s))!=0)
{
t4++;
strcat(sd,sb->name);
strcat(sd,",");
int t3=-1;
int i=0;
char t8[sizeof(sb->number)];
memcpy(&t8,&(sb->number),sizeof(sb->number));
strcat(sd,t8);
strcat(sd,"!");
}
strcat(sd,")");
}
strcat(sd,")");
}
so when I try to print anything nothing happens. char array called sd returns empty. it simply seems that there are no sensors to be read.
when I run sensors from terminal it works perfectly fine. I see a couple of cores and chips temps.
I implemented this code from some post on here and to be frank I don't totally understand it.
Posting #user3629249 comment as a community answer.
It it required to first call sensors_init() otherwise the chips list will be empty.
This function expects a sensors configuration file as argument, or NULL to use the default one.
Also, you can find an usage example in this related question: Has anyone been able to use libsensors properly?

Storage size of sockaddr_in variable isn't known

I have a piece of code that used to work in some environment a long time ago. I'm pretty sure it was a FreeBSD machine so I got FreeBSD 8.3 and I'm trying to make this file but it's not working.
When I try to compile it it complains with:
f.c: In function 'tcp'>
f.c:24: error: storage size of 'socket_stru' isn't known
f.c:29: error: 'IPPROTO_TCP' undeclared (first use in this function)
...
I've been looking around and I see these are all specified in the sys/socket.h file. This is my actual file:
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#include "f.h"
int tcp4 (in_addr_t ip, int port, int qsize )
{
struct sockaddr_in socket_stru; // line 24
socket_stru.sin_family = AF_INET;
socket_stru.sin_port = htons(port);
socket_stru.sin_addr.s_addr = ip;
int actual_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); // line 29
...
I feel like my code somehow doesn't "read" the sys/socket.h file so it doesn't know about socket_stru and IPPROTO_TCP, but I'm just really lost.
Any ideas?
None of the other answers worked for me. After taking a look inside the sys/socket.h file, I didn't even see a definition for struct sockaddr_in.
What worked for me was to #include one of the following files when using the corresponding struct sockaddr_* type:
if you're using struct sockaddr_in, #include <netinet/in.h>
if you're using struct sockaddr_un, #include <sys/un.h>
if you're using struct sockaddr_ns, #include <netns/ns.h>
if you're using struct sockaddr_ndd, #include <sys/ndd_var.h>
More information on the header files for socket programming can be found here.
I cut and paste your code into a file (removing only the #include f.h and closed off the function call.) It compiles just fine on Linux.
I suspect there may be header files differences on BSD. For socket programming, I typically include ALL these header files. And I know my socket code compiles on BSD as well. I suspect one of these header files brings in the definition for sockaddr_in. I recall when I ported by socket code to BSD, I had to explicitly add a few of these.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include <memory.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <stdarg.h>
/* the next two includes probably aren't relevant for you, but I typically use them all anyway */
#include <math.h>
#include <sys/termios.h>
Hope this helps
I had the same problem, but the following include fixed the issue for me
#include <arpa/inet.h>
Just add #include <resolv.h> to your source and you are good to go.
According to freebsd developer's handbook you need
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

‘sockaddr_in6_t’ undeclared error even after inclusing the header files

I have following simple piece of code, which is a part of ipv6 handling module in a big project.
#include <ctype.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/stat.h>
int main(){
sockaddr_in6_t* pSadrIn6 = (sockaddr_in6_t*) malloc(sizeof sockaddr_in6_t);
return 0;
}
It gives me following not error:
error: ‘sockaddr_in6_t’ undeclared (first use in this function)
Is there any special library installation or linking that I need to access the library?
It looks like you copied this code from the Linux IPv6 HOWTO but didn't copy the additional typedefs:
/*
** Type definitions (for convenience).
*/
typedef enum { false = 0, true } boolean;
typedef struct sockaddr_in sockaddr_in_t;
typedef struct sockaddr_in6 sockaddr_in6_t;
Personally I would just use the types as they are (instead of extra typedefs to avoid typing struct), but whatever

Resources