Cannot assign requested address bind linux

I got error in run time that «Cannot assign requested address» in C under Linux (Centos)

When I assign this address , it says cannot assign requested address . But when I put local address (127.0.0.1) it accepts it. Why.

int sockfd; struct sockaddr_in my_addr; // my address information struct sockaddr_in their_addr; // connector's address information socklen_t addr_len; int numbytes; char buf[MAXBUFLEN]; int port =5000; if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) < perror("socket"); exit(1); >try < my_addr.sin_family = AF_INET; // host byte order my_addr.sin_addr.s_addr = inet_addr(hostname); printf("Accepted/n"); // automatically fill with my IP my_addr.sin_port = htons(5000); // short, network byte order memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) < perror("bind"); exit(1); >while (1) < addr_len = sizeof(struct sockaddr); if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) < perror("recvfrom"); exit(1); >//printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr)); //printf("packet is %d bytes long\n",numbytes); buf[numbytes] = '\0'; //printf("packet contains \"%s\"\n",buf); > close(sockfd); > catch(. )  

1 Answer 1

If the error is happening on the bind (it's not that obvious based on your question content since the error message you state does not appear in the code), it's likely to be because the address is not available.

That's usually because it's already in use, or not available on the current host.

With a few exceptions, you can generally only bind to IP addresses that are assigned to your local interfaces. You should check that 192.168.1.8 is in that class. It's a given that 127.0.0.1 will be a local interface (hence why it works), and that INADDR_ANY will work as well - that's probably the "address" you should use unless you have a real specific need to limit yourself to one interface.

You should check the errno following the failing function and match it against the possibilities.

As an aside, and this is probably irrelevant to your problem, the way in which you initialise the sockaddr_in structure (setting fields then clearing the rest) seems to be less than portable to me.

I think it would be safer to clear the lot then simply set what you desire after that, something like:

memset (&my_addr, 0, sizeof (my_addr)); my_addr.sin_family = AF_INET; my_addr.sin_addr.s_addr = inet_addr (hostname); my_addr.sin_port = htons (5000); 

At least that way, the order of fields within the structure won't affect your code.

You can see the problem with the following code. First off, the requisite headers:

#define __USE_GNU #include #include #include #include #include #include 

Then the argument checking and socket creation.

int main (int argc, char *argv[]) < int sockfd; struct sockaddr_in me; if (argc < 2) < printf ("Need argument with IP address\n"); return 1; >if ((sockfd = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
 memset (&me, 0, sizeof (me)); me.sin_family = AF_INET; me.sin_addr.s_addr = inet_addr (argv[1]); me.sin_port = htons (5000); if (bind (sockfd, (struct sockaddr *)&me, sizeof(struct sockaddr)) == -1) < fprintf (stderr, "errno = %d ", errno); perror("bind"); exit(1); >close(sockfd); return 0; > 

When you run that with certain arguments, you can see it works okay for the ones where the IP addresses belong to local interfaces ( 127.0.0.1 and 192.168.0.101 ) but not for those that do not, like 192.168.0.102 :

pax> ifconfig | grep 'inet addr' inet addr:192.168.0.101 Bcast:192.168.0.255 Mask:255.255.255.0 inet addr:127.0.0.1 Mask:255.0.0.0 inet addr:192.168.99.1 Bcast:192.168.99.255 Mask:255.255.255.0 inet addr:192.168.72.1 Bcast:192.168.72.255 Mask:255.255.255.0 pax> ./testprog 127.0.0.1 pax> ./testprog 192.168.0.101 pax> ./testprog 192.168.0.102 errno = 99 bind: Cannot assign requested address pax> grep '#define.*99' /usr/include/asm-generic/errno.h #define EADDRNOTAVAIL 99 /* Cannot assign requested address */ 

And, from the link to the bind man page above, we see:

EADDRNOTAVAIL
A nonexistent interface was requested or the requested address was not local.

Источник

start etcd failed by "bind: cannot assign requested address"

i run etcd as docker container ,and 10.132.41.234 is my host ip which i run docker container on ,and i get error info like this ,i do not know if it is right and i do now familiar with etcd,some one can help? thx!

2017-09-13 08:55:03.339612 I | etcdmain: etcd Version: 3.0.17 2017-09-13 08:55:03.339891 I | etcdmain: Git SHA: cc198e2 2017-09-13 08:55:03.339902 I | etcdmain: Go Version: go1.6.4 2017-09-13 08:55:03.339912 I | etcdmain: Go OS/Arch: linux/amd64 2017-09-13 08:55:03.339921 I | etcdmain: setting maximum number of CPUs to 2, total number of available CPUs is 2 2017-09-13 08:55:03.340059 I | etcdmain: peerTLS: cert = /etc/ssl/certs/server.pem, key = /etc/ssl/certs/server-key.pem, ca = , trusted-ca = /etc/ssl/certs/ca.pem, client-cert-auth = true 2017-09-13 08:55:03.342794 I | etcdmain: listening for peers on https://127.0.0.1:2380 2017-09-13 08:55:03.342844 I | etcdmain: clientTLS: cert = /etc/ssl/certs/server.pem, key = /etc/ssl/certs/server-key.pem, ca = , trusted-ca = /etc/ssl/certs/ca.pem, client-cert-auth = true 2017-09-13 08:55:03.345340 I | etcdmain: stopping listening for peers on https://127.0.0.1:2380 2017-09-13 08:55:03.345386 C | etcdmain: listen tcp 10.132.41.234:2379: bind: cannot assign requested address docker run -d -v /opt/certs:/etc/ssl/certs -v /opt/etcd-data:/etcd-data -p 2380:2380 -p 2379:2379 \ --name etcd gcr.io/google_containers/etcd-amd64:3.0.17 \ /usr/local/bin/etcd \ --name etcd0 \ --data-dir=/etcd-data \ --advertise-client-urls https://10.132.41.234:2379,https://127.0.0.1:2379 \ --listen-client-urls https://10.132.41.234:2379,https://127.0.0.1:2379 \ --initial-advertise-peer-urls https://127.0.0.1:2380 \ --listen-peer-urls https://127.0.0.1:2380 \ --initial-cluster-token etcd-cluster-1 \ --initial-cluster etcd0=https://127.0.0.1:2380,etcd1=https://127.0.0.1:2380,etcd2=https://127.0.0.1:2380 \ --initial-cluster-state new \ --cert-file=/etc/ssl/certs/server.pem \ --key-file=/etc/ssl/certs/server-key.pem \ --peer-cert-file=/etc/ssl/certs/server.pem \ --peer-key-file=/etc/ssl/certs/server-key.pem \ --trusted-ca-file=/etc/ssl/certs/ca.pem \ --peer-trusted-ca-file=/etc/ssl/certs/ca.pem \ --peer-client-cert-auth=true \ --client-cert-auth=true 

Источник

ssh tunnel - bind: Cannot assign requested address

Trying to create a socks (-D) ssh tunnel - Linux box to Linux box (both centos): sshd running on remote side ok. From local machine we do / see this:

ssh -D 1080 user@8.8.8.8. user@8.8.8.8's password: bind: Cannot assign requested address 

(where 8.8.8.8 is really my server's IP and 'user' is my real username) I am logged into the remote side in this terminal-window. I can verify that the local port was unused prior to this command, and then used by an ssh process, after the command, via:

So, unlike most googled-responses with this error, the problem would not seem to be the loopback interface assignment. If I try to use this tunnel with a mail client, the local-side permits the attempt (no 'proxy-failed' error), but no data / reply is returned. On the remote side, I do have "PermitTunnel yes" in my sshd_config (though 'yes' should be the default, anyway). Ideas or Clues? Here is the relevant debug-output

OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010 debug1: Reading configuration data /etc/ssh/ssh_config debug1: Applying options for * . debug1: Authentication succeeded (password). debug1: Local connections to LOCALHOST:1080 forwarded to remote address socks:0 debug1: Local forwarding listening on 127.0.0.1 port 1080. debug1: channel 0: new [port listener] debug1: Local forwarding listening on ::1 port 1080. bind: Cannot assign requested address debug1: channel 1: new [client-session] debug1: Entering interactive session. debug1: Sending environment. debug1: Sending env LANG = en_US.utf8 

Other clue: If I run a Virtual Box on the client running Windows, open a tunnel with putty in that box, that tunnel, to the same remote server, works. Stranger Still" If I use Putty (for linux) running directly on the Linux Client, it does NOT work, even if the settings are an exact duplicate of the putty settings which DO WORK in Putty running on Windows in a Virtual Box on the same Client Machine?? There is something fishy . still trying experiments to figure out what it is.

Источник

Socket bind failed errno = 99

I'm trying to bind the server socket so I can receive and listen for incoming messages from other clients. But I can't bind, it returns an error - Socket bind failed: 99. I read up what does it mean and it says that errno 99 indicates that the socket does not exist? Any ideas? Thanks

 UDP_socketID = socket(AF_INET, SOCK_DGRAM, 0); if (UDP_socketID < 0) < printf("Socket creation failed! Error = %d\n\n", errno); exit(0); >//specify server address, port and IP bzero((char *)&serverAddr, sizeof(serverAddr)); serverAddr.sin_family = AF_INET; serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); serverAddr.sin_port = htons(SERV_PORT); check = inet_aton(SERVER_IP, &serverAddr.sin_addr); if (check == 0) printf("IP conversion error!\n\n"); start = bind(UDP_socketID, (struct sockaddr *) &serverAddr, sizeof(serverAddr)); if (start < 0) < printf("Socket bind failed = %d\n", errno); exit(0); >else printf("Socket bind successful!\n"); 

1 Answer 1

99 is EADDRNOTAVAIL. Which means (from man bind(2)):

A nonexistent interface was requested or the requested address was not local.

Maybe the SERVER_IP is not IP of your host.

Oh no! It returned: "Cannot assign requested address." The server IP is on another host, not me. How can I fix this?

@Broccoli : Assuming that this is server side code. If you don't care which interface your server listens, then you can remove SERVER_IP stuff and simply bind the server socket with INADDR_ANY.

Источник

Читайте также:  Linux удалить все пустые файлы
Оцените статью
Adblock
detector