Request from Arduino to API Docker - docker

I have an Arduino project. I want to make a request from Arduino (outside the container) to an API in a Docker container.
Everything is on localhost. I try to connect from Arduino with WifiClient to 127.0.0.1:3001 (host and port of my API in Docker). But the client does not connect.
My code:
WifiCliente client;
char url5[] = "127.0.0.1";
const int port = 3001;
if (!client.connect(url5, port)) {
Serial.println("Fallo al conectar");
return;
}
¡Thanks a lot!

Related

Port forwarding not working on Huawei B535-232 4G LTE router for ssh connection

I have a Huawei B535-232 4G LTE router with the following configuration:
set ddns with NO-IP,
set static ip of the server to which I want to connect remotely via SSH,
set SSH port forwarding with: Wan port 8888 and Lan port 22 for server static ip.
I do not understand why I can not connect, the port 8888 is closed,where I am wrong ?
Thank you in advance for your help
I do not understand why I can not connect, the port 8888 is closed,where I am wrong ?

freeradius docker Ignoring request to auth address when build through jenkins

I am using freeradius docker for AAA server authentication. For integration tests I am using docker-compose which contains freeradius and other services also. When I build, it creates the containers and test the authentication and after that stops the containers.
From one docker container I am sending request to freeradius docker container for authentication, which is working fine on my local machine but when I am trying to build through jenkins, I am getting
Ignoring request to auth address * port 1812 bound to server default from unknown client 192.168.96.1 port 36096 proto udp
below is my client.conf file -
client dockernet {
ipaddr = x.x.0.0
secret = testing123
netmask = 24
shortname = dockernet
require_message_authenticator = no
}
client jenkins {
ipaddr = 192.168.0.0
secret = testing123
netmask = 24
shortname = jenkins
require_message_authenticator = no
}
Your client subnet/netmask definition is incorrect.
192.168.0.0/24 will match addresses in the subnet 192.168.0.x (192.168.0.0 to 192.168.0.255), but the request is coming from 192.168.96.1.
Either change the jenkins client definition to 192.168.96.0 (leaving the netmask as 24), or use netmask = 16 which will include all addresses from 192.168.0.0 to 192.168.255.255.
I would recommend limiting the range to the exact IP, or as small a range as possible, so therefore suggest the former.

Host to Docker Container Remote Socket Failure

I'm trying to communicate a simple message between my host machine and my docker container.
I began by creating my docker container with the host network driver:
docker run -t -d --network host --name cfalcon ubuntu
Next I entered the container:
docker exec -it cfalcon
I identified my host IP as 192.168.0.109 and my container's IP as 172.17.0.1 using:
hostname -I
I made a file at ~/Documents/tcp_server.c in my container with the following code:
TCP_SERVER.C
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char* argv[]) {
char address[] = "192.168.0.109";
// Creating Server Socket
int server_socket;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
// Defining Server Address
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
// Specify the port, 9001 seems safe
server_address.sin_port = htons(9001);
server_address.sin_addr.s_addr = inet_addr(address);
// Binding our Socket to our Specified IP, 192.168.0.109
bind(server_socket, (struct sockaddr *) &server_address, sizeof(server_address));
// Listen() Function Begins Listening for a Connection
listen(server_socket, 1);
// Ability to Accept Connections
int client_socket;
client_socket = accept(server_socket, NULL, NULL);
char server_message[256] = "Its-a me, a-mario!";
// Send a message with send()
send(client_socket, server_message, sizeof(server_message), 0);
close(server_socket);
return 0;
}
Returning to my host machine, I decided to make a new file at ~/Documents/tcp_client.c with the following code:
TCP_CLIENT.C
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char* argv[]) {
char address[] = "172.17.0.1";
// Creating a Socket
int network_socket;
// socket(domain of socket (generally AF_INET, a constant), TCP socket, protocol)
network_socket = socket(AF_INET, SOCK_STREAM, 0);
// Specifying an Address for the Socket (Address family is same as parameter one of socket())
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
// Specify the port, 9001 seems safe
server_address.sin_port = htons(9001);
server_address.sin_addr.s_addr = inet_addr(address);
// Returns an integer for error handling
int connection_status = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
// Error Handling
if (connection_status == -1) {
printf("There was an error making a connection to the remote socket.\n\n");
}
// If we send data, we receive a response from the server in recv
char server_response[256];
recv(network_socket, &server_response, sizeof(server_response), 0);
// Printing the Server's Response
printf("The server responded with: %s\n", server_response);
// Avoiding data leaks
close(network_socket);
return 0;
}
Unfortunately, I did not receive "Its-a me, a-Mario!", but rather "There was an error making a connection to the remote socket".
I believed I perfectly linked the two using respective IP's and identical port numbers, but it would appear I haven't. Please let me know if you have any guidance toward my predicament.
Edit: Update! I installed Netcat on my container and established a connection between my host and my container.
Host: $ nc -l 9001
Container: $ nc localhost 9001
Any message sent from the container in the following prompt reaches my host machine, which tells me this is solely an error with my code.
At the lowest level, your server code bind(2) to 192.168.0.9, but your client code connect(2) to 172.17.0.1. The server will only accept connections to the single address it's bound to. If you're using Docker host networking, the server process is indistinguishable from any other process running on the host from a network point of view, and while 172.17.0.1 will presumably reach the host as well, since it's not exactly the same address, your connection will be refused.
If the server is running in a Docker container, and the client is running directly on the same host:
bind(2) to 0.0.0.0. (Binding to 127.0.0.1 will make the process unreachable from outside the container; the container-private IP address is unpredictable.)
Do not start the container with --net host. Do start it with an appropriate -p option mapping the container port to some host port; for example, -p 9001:9001.
From the client, running not in Docker, on the same host, connect(2) to 127.0.0.1 port 9001. From a different host (Docker or otherwise), use the first host's name.
If the client is running in a different container on the same host:
bind(2) to 0.0.0.0.
docker create network something. You don't need any additional arguments, but you do need to create the network.
When you docker run both containers, start them with --net something matching the network you previously created, and a useful --name.
In the client, use getaddrinfo(3) to look up the server's container name; then connect(2) to that sockaddr.
Note that neither of these paths involve looking up the container-private IP address (except as a low-level detail in an ordinary "connect to this hostname" path). This changes routinely (whenever you delete and recreate a container), isn't accessible from off-host, and isn't even accessible from the host console on some Docker setups. There's never a need to do look this up.
Also note that neither path involves docker exec. It's a useful debugging tool but it isn't usually part the core container workflow. (If you find yourself "docker run a container and then immediately docker exec", generally you should change the container startup sequence to do whatever the docker exec thing is itself.)

How to subscribe to external API from docker container, without knowing host ip?

I want to subscribe to an external publish/subscribe API from within a docker container. For subscribing I need to give the IP address, port and endpoint on which my service can receive data when an event happens.
The problem is that from inside of the container I don't know the IP address of the docker host, to which the external API has to send the data.
I think the problem can be solved by passing the host IP as an environment variable to the container, but because the service has to be installed on many different hosts I don't want to manually setup environment variables for each host.
Any help would be appreciated :)
I finally ended up running the service on the host network by adding
network_mode: host
to my service in docker-compose.yml
This way the docker container gets an ip address in the hosts network, so it just register to the external API using it's own ip address.
Note that this solution does not work on OS X or Windows, since docker runs on a virtual server on these operating systems.
I retrieve the IP address for my service as follows in java.
public static InetAddress getCurrentIp() {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface ni = networkInterfaces.nextElement();
Enumeration<InetAddress> nias = ni.getInetAddresses();
while(nias.hasMoreElements()) {
InetAddress ia= nias.nextElement();
if (!ia.isLinkLocalAddress()
&& !ia.isLoopbackAddress()
&& ia instanceof Inet4Address) {
return ia;
}
}
}
} catch (SocketException e) {
LOG.error("unable to get current IP " + e.getMessage(), e);
}
return null;
}

Portforwarding not working

I recently tried to portforward port 80 on my local IP, but as the tutorial said, it should be open on my external IP, which it's not. But it is open on my local ip thought.
I have portforwarded port 80, range 80 UDP and TCP on my local IP: 192.168.1.170
This is the tutorial i followed: https://www.youtube.com/watch?v=RZTYqTGqtjI
I portforwarded my IP in the router settings.
https://image.ibb.co/hGW4fm/Sk_rmbild_345.png
https://image.ibb.co/exWFmR/Sk_rmbild_344.png
IF THE suggested METHOD DOESN'T WORK FOR YOU , DO THIS :-
GOTO -> SECURITY TAB -> REMOTE MANAGEMENT
Enable the Remote Management (or enter 255.255.255.255 in the field) .
This will enable the you to access your LAN from WAN.
For the record :-
Internal Port is the PORT on which the device in your LAN is serving .
External Port is the port which the user enters in the browser . exp :- 127.184.184.19:8080 .
Here 8080 is the external port . And if a device in your LAN runs a http web server at port 80 , then the internal port would be 22.
If the Above methods don't work , then your ISP might be using Carrier NAT which means you would have a different PUBLIC and WAP ip address .
In this case , you should use the WAN ip address shown in your router configuration page to access your LAN from internet. s
Kindly Try to open same URL from different Internet Connection other than LAN

Resources