I am trying to get host information about the host with IP address 89.249.207.231. I know that it exists, because when I type the IP address in my browser's url field it finds the page. Here is my code in C.
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
int main()
{
struct in_addr addr;
inet_aton("89.249.207.231", &addr);
struct hostent* esu = gethostbyaddr((const char*)&addr),sizeof(addr), AF_INET);
printf("%s\n", esu->h_name);
return 0;
}
When I compile and run it, it gives "Segmentation fault". I can not understand the problem with my code.
Any hints and suggestions would be appreciated.
Thanks!
Even if the host exists, you may not be able to extract its hostname.
For example, the following code, without the deprecated functions that you use gives the result host=google-public-dns-a.google.com whereas with your host address gives could not resolve hostname.
The reason of your segfault, is that esu is NULL, because the function could not resolve a hostname by the given IP.
Here is the code:
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main()
{
struct sockaddr_in sa; /* input */
socklen_t len; /* input */
char hbuf[NI_MAXHOST];
memset(&sa, 0, sizeof(struct sockaddr_in));
/* For IPv4*/
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr("8.8.8.8");
len = sizeof(struct sockaddr_in);
if (getnameinfo((struct sockaddr *) &sa, len, hbuf, sizeof(hbuf),
NULL, 0, NI_NAMEREQD)) {
printf("could not resolve hostname\n");
}
else {
printf("host=%s\n", hbuf);
}
return 0;
}
Related
I made a TCP Client/Server and I can run it one time. But when I try to run it more than one time, it gives me the following error: "Connection refused". Here's my code.
Client
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main() {
int net_socket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in net_addr;
net_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
net_addr.sin_family = AF_INET;
net_addr.sin_port = htons(3250);
int connection_state = connect(net_socket, (struct sockaddr*)&net_addr, sizeof(net_addr));
if (connection_state == 0) {
printf("Connected with the server.\n");
}
else {
printf("Connection with the server failed. [%s]\n", strerror(errno));
}
char net_message[256];
recv(net_socket, net_message, sizeof(net_message), 0);
close(net_socket);
shutdown(net_socket, SHUT_RDWR);
return 0;
}
Server
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main() {
int net_socket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in net_addr;
net_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
net_addr.sin_family = AF_INET;
net_addr.sin_port = htons(3250);
bind(net_socket, (struct sockaddr*)&net_addr, sizeof(net_addr));
listen(net_socket, 1);
struct sockaddr_in cl_addr;
int cl_size = sizeof(cl_addr);
int cl_socket = accept(net_socket, (struct sockaddr*)&cl_addr, (socklen_t*)&cl_size);
int loop = 1;
char net_message[256];
send(cl_socket, net_message, sizeof(net_message), 0);
close(net_socket);
close(cl_socket);
shutdown(net_socket, SHUT_RDWR);
shutdown(cl_socket, SHUT_RDWR);
return 0;
}
I've been trying to solve the problem by many ways but I'm a newbie. Thanks for the help in advance!
The problem is with your server lacking the SO_REUSEADDR socket option which leads to the bind() syscall failing. The reason is, that after a client disconnects from the server, the connection is still known by the system in TIME_WAIT state, to wait for late packets. These lead to the bind() to fail with EADDRINUSE if the above mentioned socket option is not set.
Use the following:
...
int one = 1;
setsockopt(net_socket, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
bind(net_socket, (struct sockaddr*)&net_addr, sizeof(net_addr));
...
and do error checks for all syscalls! That would have shown you the problem much earlier and had saved you much time.
Some more issues:
You are sending an uninitialized buffer net_message
A shutdown on a listen socket isn't sensible at all, since no connection is established on it (this here: shutdown(net_socket, SHUT_RDWR);)
The shutdown on the client socket has - if at all - to be performed before the close(cl_socket);. But it is not necessary at all in that case.
I'm trying to use a mavlink sample developed by Parrot to control a Bebop2 using mavlink protocol (link here).
In order to send a message to the drone, they use the sendto function but I encounter an issue I cannot resolve : everytime I try to make the program works, I recieve an error and after a bit of investigation, I found that it's the 'sendto' used in this code (in the mavlink_comm_send_msg function) that returns an EINVAL error type (code located inside the mavlink_comm.c file in the src directory) :
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <sys/time.h>
#include <poll.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <netdb.h>
#include <mavlink.h>
#include "mavlink_comm.h"
#define MAVLINK_COMM_BUFSIZE 4096
struct mavlink_comm {
int sock;
struct sockaddr_in remote_addr;
unsigned char tx_buffer[MAVLINK_COMM_BUFSIZE];
unsigned int tx_bufidx;
unsigned char rx_buffer[MAVLINK_COMM_BUFSIZE];
unsigned int rx_bufidx;
void (*cb)(mavlink_message_t *msg, void *user_data);
void *user_data;
};
struct mavlink_comm *mavlink_comm_new(struct mavlink_comm_cfg *cfg)
{
struct sockaddr_in locAddr;
struct mavlink_comm *c;
struct hostent *server;
if (!cfg)
return NULL;
c = calloc(1, sizeof(*c));
if (!c)
return NULL;
c->cb = cfg->cb;
c->user_data = cfg->user_data;
c->sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
memset(&locAddr, 0, sizeof(locAddr));
locAddr.sin_family = AF_INET;
locAddr.sin_addr.s_addr = INADDR_ANY;
locAddr.sin_port = htons(cfg->local_port);
if (cfg->remote_addr && cfg->remote_addr[0] != '\0') {
server = gethostbyname(cfg->remote_addr);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host %s\n",
cfg->remote_addr);
exit(0);
}
bzero((char *) &c->remote_addr, sizeof(c->remote_addr));
c->remote_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&c->remote_addr.sin_addr.s_addr,
server->h_length);
c->remote_addr.sin_port = htons(cfg->remote_port);
}
/* Bind the socket to port 14551
* necessary to receive packets from qgroundcontrol */
if (-1 == bind(c->sock,(struct sockaddr *)&locAddr,
sizeof(struct sockaddr))) {
perror("error bind failed");
goto exit_close;
}
return c;
exit_close:
close(c->sock);
free(c);
return NULL;
}
static inline int mavlink_comm_send_data_internal(struct mavlink_comm *c)
{
int size = sizeof(struct sockaddr_in);
return sendto(c->sock, c->tx_buffer, c->tx_bufidx, 0,
(struct sockaddr*)&c->remote_addr,
sizeof(struct sockaddr_in));
}
int mavlink_comm_send_msg(struct mavlink_comm *c, mavlink_message_t *msg)
{
if (!c || !msg)
return -EINVAL;
int len = mavlink_msg_to_send_buffer(c->tx_buffer, msg);
return sendto(c->sock, c->tx_buffer, len, 0,
(struct sockaddr*)&c->remote_addr,
sizeof(struct sockaddr_in));
}
I looked at other related posts and tried several things to make it work but none of them were successful. The cast seems correct and I verified dest_len argument, so I'm quite lost.
Thanks a lot for your help.
This might look basic to you, but since this is my first experience with networking in C, I wanted to make sure I'm not doing anything stupid, so I figured... better safe than running into some undefined, bizarre behaviour somewhere along the way.
Consider the following code for setting a server socket in C:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
int main(int argc, char* argv[]) {
int listenfd, connfd;
struct sockaddr_in serv_addr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
// setting 'serv_addr'
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr));
if (listen(listenfd, 10) == -1) {
printf("Failed to listen\n");
return -1;
}
while(1) {
connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL);
// do some writing/reading with connfd...
close(connfd);
}
return 0;
}
Notice that the bind() function takes &struct sockaddr_in as argument.
According to the doc:
bind() assigns
the address specified by addr to the socket referred to by the file
descriptor sockfd.
Now suppose that no explicit references are made by me to this serv_addr again, during the execution of the while loop.
Is the struct sockaddr_in needed by the socket, or, after bind() returns, struct sockaddr_in is not needed anymore?
Are any implicit references somehow made by the socket?
To be more precise, is the following code safe?
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
int some_other_function() {
int listenfd;
/* struct sockaddr_in is defined locally in some_other_function()*/
struct sockaddr_in serv_addr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
// setting 'serv_addr'
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr));
if (listen(listenfd, 10) == -1) {
printf("Failed to listen\n");
return -1;
}
return listenfd;
} /* if the socket will refer to &serv_addr from now on, it'll dereference a dangling pointer */
int main(int argc, char* argv[]) {
int listenfd=some_other_function();
int connfd;
while(1) {
/** is sockaddr_in serv_addr being referenced here? is it needed by accept()? **/
connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL);
// do some writing/reading with connfd...
close(connfd);
}
return 0;
}
No, it does not. The contents of the address parameter are copied by the kernel; you can reuse or free that memory as soon as the call returns.
In fact, it's possible to make a more general statement here: unless the documentation specifically says otherwise, arguments passed to the kernel by pointer reference only need to be kept 'alive' for the duration of the system call. Their values will be copied by the kernel if needed.
It is safe. bind() does not keep a reference to the pointer after it returns. It's only taking its input as a pointer because it's receiving a variable-sized struct, which can't be passed by value. (It's polymorphism in C, essentially.)
i've done a simple client/server program where the server wait for an external connection and return the connection-socket if the port number of the client is in the range of [1025-2048] otherwise return -1. The problem is that when i get the port number by the client adress (which should be stored in the sockaddr structure) it says me that the client port number is zero, but in the client program i've set the client portnumber to 1999.
SERVER
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
int function(int fd_socket) {
int fd_socket_acc;
int len;
int port;
struct sockaddr_in client_addr;
puts("WAITING FOR CLIENT...");
fd_socket_acc = accept(fd_socket, (struct sockaddr*)&client_addr, &len);
puts("CONNECTION DONE.");
port = ntohs (client_addr.sin_port);
printf("client port number: %d \n", port);
if (port >= 1024 && port <= 2048) {
close (fd_socket_acc);
return fd_socket_acc;
}
else {
close(fd_socket_acc);
return -1;
}
}
int main(int argc, char *argv[]) {
int fd_socket;
struct sockaddr_in local_addr;
fd_socket = socket(AF_INET, SOCK_STREAM, 0);
local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(1887);
local_addr.sin_addr.s_addr = INADDR_ANY;
bind(fd_socket, (struct sockaddr*)&local_addr, sizeof(local_addr));
listen(fd_socket, 3);
function(fd_socket);
//close(fd_socket);
}
CLIENT
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
int main(int argc, char *argv[]) {
int fd_socket;
struct sockaddr_in local_addr;
struct sockaddr_in server_addr;
struct hostent *hp;
fd_socket = socket(AF_INET, SOCK_STREAM, 0);
local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(1999);
local_addr.sin_addr.s_addr = INADDR_ANY;
bind(fd_socket, (struct sockaddr*)&local_addr, sizeof(local_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(1887);
//hostname is "ubuntu"
hp = gethostbyname("ubuntu");
bcopy(hp->h_addr, &server_addr.sin_addr, 4);
printf("%d \n", ntohs(local_addr.sin_port));
connect(fd_socket, (struct sockaddr*)&server_addr, sizeof(server_addr));
wait(2);
close(fd_socket);
}
If i get the port number in client with a printf("%d", ntohs(local_addr.sin_port)) it stamps correctly 1999, but if i get the port number of client in server with printf("%d", ntohs(client_addr.sin_port)) it stamps 0. Why?
thanks in advance!
In order to obtain the client port number in client_addr through accept you have to tell accept how big that buffer is by setting
socklen_t len = sizeof(client_addr);
You can alternatively retrieve it by calling afterwards
len = sizeof(client_addr);
getpeername(fd_socket_acc, (struct sockaddr*)&client_addr, &len);
Maybe because you do not set the variable len to anything, and I suspect that your compiler sets it to 0.
What happens is that you try to accept with an undefined len size.
Adding len=sizeof( struct sockaddr_in ); before making a call to accept would help to fill the passed client_addr correctly.
I'd like to get a list of available devices (their IP address or hostname should be fine) connected to my local network. I've got some examples in C#, but I couldn't find any sample using C/C++. I'm familiar with the BSD socket API, just to clarify.
I have a "stub" kind of idea: maybe I should determine the range of the IP addresses that the devices on my LAN can potentially belong to, and then determine if they e. g. respond to PING, or something like that. How could I achieve these?
I want my application to run on Linux and iOS. That's why I'd prefer BSD sockets, or at a maximum, Foundation/GNUstep (although they are written in Objective-C).
You can also e.g. send an ICMP echo request packet to 224.0.0.1. This is a special all-nodes multicast address every node should respond to (except if a firewall rule or network policy setting prevents it).
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/ip_icmp.h>
#include <time.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
// Define the Packet Constants
// ping packet size
#define PING_PKT_S 64
// Automatic port number
#define PORT_NO 0
// Automatic port number
#define PING_SLEEP_RATE 1000000
// Gives the timeout delay for receiving packets
// in seconds
#define RECV_TIMEOUT 1
// Performs a DNS lookup
char *dns_lookup(char *addr_host, struct sockaddr_in *addr_con)
{
//printf("\nResolving DNS..\n");
struct hostent *host_entity;
char *ip=(char*)malloc(NI_MAXHOST*sizeof(char));
int i;
if ((host_entity = gethostbyname(addr_host)) == NULL)
{
// No ip found for hostname
return NULL;
}
//filling up address structure
strcpy(ip, inet_ntoa(*(struct in_addr *)
host_entity->h_addr));
(*addr_con).sin_family = host_entity->h_addrtype;
(*addr_con).sin_port = htons (PORT_NO);
(*addr_con).sin_addr.s_addr = *(long*)host_entity->h_addr;
return ip;
}
// Resolves the reverse lookup of the hostname
char* reverse_dns_lookup(char *ip_addr)
{
struct sockaddr_in temp_addr;
socklen_t len;
char buf[NI_MAXHOST], *ret_buf;
temp_addr.sin_family = AF_INET;
temp_addr.sin_addr.s_addr = inet_addr(ip_addr);
len = sizeof(struct sockaddr_in);
if (getnameinfo((struct sockaddr *) &temp_addr, len, buf,
sizeof(buf), NULL, 0, NI_NAMEREQD))
{
//printf("Could not resolve reverse lookup of hostname\n");
return NULL;
}
ret_buf = (char*)malloc((strlen(buf) +1)*sizeof(char) );
strcpy(ret_buf, buf);
return ret_buf;
}
// Driver Code
int main(int argc, char *argv[])
{
int sockfd;
char *ip_addr, *reverse_hostname;
struct sockaddr_in addr_con;
int addrlen = sizeof(addr_con);
char net_buf[NI_MAXHOST];
int i=0;
for(int i=1;i<255;++i)
{
char ip[80];
sprintf(ip, "192.168.2.%d", i);
ip_addr = dns_lookup(ip, &addr_con);
if(ip_addr==NULL)
{
//printf("\nDNS lookup failed! Could not resolve hostname!\n");
continue;
}
reverse_hostname = reverse_dns_lookup(ip_addr);
if(reverse_hostname==NULL)
{
//printf("\nDNS lookup failed! Could not resolve hostname!\n");
continue;
}
//printf("\nTrying to connect to '%s' IP: %s\n",ip, ip_addr);
printf("\nReverse Lookup domain: %s",
reverse_hostname);
printf("\n %s \n", ip);
}
return 0;
}