How to connect through socket to Virtual Machine - sockets

I have a java client on Windows and c++ server running on Linux (VM). I'm trying to connect client to the server. But I don't know what IP address I should use.
My Virtual Machine configuration is default, I have NAT network on it.
Here is what I get from ifconfig
enp0s3 Link encap:Ethernet HWaddr 08:00:27:97:86:ce
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::8651:2c9d:6683:e4fc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:855910 errors:0 dropped:0 overruns:0 frame:0
TX packets:267095 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:940974525 (940.9 MB) TX bytes:24398399 (24.3 MB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:9710 errors:0 dropped:0 overruns:0 frame:0
TX packets:9710 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:875898 (875.8 KB) TX bytes:875898 (875.8 KB)
And here is what i get from ipconfig:
Ethernet adapter Ethernet:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Połączenie lokalne* 2:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Wi-Fi:
Connection-specific DNS Suffix . : home
Link-local IPv6 Address . . . . . : fe80::ddca:c63d:1b9e:6255%14
IPv4 Address. . . . . . . . . . . : 192.168.1.3
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.1.254
Ethernet adapter Połączenie sieciowe Bluetooth:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Tunnel adapter Połączenie lokalne* 4:
Connection-specific DNS Suffix . :
IPv6 Address. . . . . . . . . . . : 2001:0:9d38:90d7:10c9:14d5:da7f:e55c
Link-local IPv6 Address . . . . . : fe80::10c9:14d5:da7f:e55c%13
Default Gateway . . . . . . . . . : ::
I tried to connect using:
socket = new Socket("10.0.2.15", 1236);
When Im using client on VM with localhost everything is fine.
So I assume there is a problem with connection between Windows and VM.

you should read https://blogs.oracle.com/scoter/networking-in-virtualbox-v2
Basically, you have to configure the port forwarding. Follow the instructions provided in the link, by the end of the page.
The configuration you shoud use is:
Name: Rule 1 (or any actually)
Protocol: TCP
Host IP: 127.0.0.1
Host Port: 1236
Guest IP: (leave blank)
Guest Port: 1236
The guest port could be any, but must be the same you are using on your server application. Leave the Guest IP blank.
Now, in order to you connect from the host to the guest, you should use in the client application:
socket = new Socket("127.0.0.1", 1236);
If you want to connect from the guest VM to the host machine, using this configuration, you should use in the client application instead:
socket = new Socket("10.0.2.2", 1236);
Alternatively, you could also use the NIC in bridge mode, also explained in the article. But you should configure the router properly.
Btw, don't forget to disable your firewall, or configure it, otherwise it can block the incoming connections.
You can disable ufw using the command:
sudo ufw disable

Related

How to trace L2 packet in linux kernel bridge and interface?

I create a Linux bridge and add two virtual interface to this bridge.
ip link add br0 address 01:02:03:04:00:00 type bridge
ip link add veth0 address 01:02:03:04:00:10 type dummy
ip link add veth1 address 01:02:03:04:00:20 type dummy
ip link set dev veth0 master br0
ip link set dev veth1 master br0
ip link set br0 up
ip link set veth0 up
ip link set veth1 up
Then write a raw socket listening program that bind the veth0.
socket = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))
struct sockaddr_ll sockaddr;
memset(&sockaddr, 0x0, sizeof(sockaddr));
sockaddr.sll_family = AF_PACKET;
sockaddr.sll_protocol = htons(ETH_P_ALL);
sockaddr.sll_pkttype = PACKET_OUTGOING;
sockaddr.sll_ifindex = if_nametoindex("veth0");
bind(socket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)
Then write a sender program that same way created as the listener, using veth1 interface. That program send eth frame.
struct ethhdr_frame* eth_packet;
eth_packet->h_dest = "01:02:03:04:00:10" //it not string, just show purpose
eth_packet->h_source = "01:02:03:04:00:20"
eth_packet->h_proto = 0x88b5
eth_packet->payload = "hello world!"
Now my problem is listener can't capture eth frame which is send by my sender. To find where is my packet dropped, I used ftrace filtered by br*, but even any bridge handler function wasn't call.
$cd /sys/kernel/debug/tracing
$echo br* > set_ftrace_filter
$echo function_graph > current_tracer
$echo 1 > tracing_on ; ./my_sender ; echo 0 > tracing_on

How to send an IP packet to directly connected Peer through broadcast?

let's two machines are directly connected on a Point-to-point link:
A -192.168.4.1/24---------------------------192.168.4.2/24--B
How A can send an IP packet to B through broadcast only ?
if A cooks up the packet with Dest mac = ff:ff:ff:ff:ff:ff and dest ip : 192.168.4.255, Would this make the packet destined to B ? If yes, Can somebody explains how this works ?
I figured out - we can send the packet with dest IP = 192.168.4.255. In addition, set the broadcast privileges on socket using
int on=1;
setsockopt(igmp_sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
It will work.

Unable to to connect remotely to mongodb

Currently I am having trouble remote connecting a mongodb server on my virtual Ubuntu machine. I am unable to connect to with with Robomongo client that is running on my Windows PC that is also running the vm.
Here are the IP Addressees for both pcs
Windows:
Ethernet adapter Ethernet:
Connection-specific DNS Suffix . :
IPv4 Address. . . . . . . . . . . : 192.168.1.137
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.1.1
Ubuntu:
enp0s3 Link encap:Ethernet HWaddr 08:00:27:6c:fc:9c
inet addr:192.168.1.134 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::9785:55d7:130:6618/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:5021 errors:0 dropped:0 overruns:0 frame:0
TX packets:465 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1231057 (1.2 MB) TX bytes:45237 (45.2 KB)
The error that I am getting on windows that show it can't connect:
Netstat that shows mongo is running:
netstat -tulpn | grep 27017
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 3611/mongod
From this point I tried to connect locally to the server and it works:
mongo --port 27017
MongoDB shell version: 2.6.10
connecting to: 127.0.0.1:27017/test
Then I tried to add port 27017 to the ufw and still unable to connect:
sudo ufw status
To Action From
-- ------ ----
27017 ALLOW Anywhere
27017 (v6) ALLOW Anywhere (v6)
Thanks for the help in advance.
Found the problem. It was due to the bind_ip flag in my mongodb.conf being set to 127.0.0.1. This is why I as only able to connect locally to the mongodb server.
Resolution was to comment out the bind_ip flag so it would not white list the IP Addresses.
Mongodb.conf
# mongodb.conf
# Where to store the data.
dbpath=/home/<username>/mongodb
#where to log
logpath=/home/<username>/mongodb/logs/mongodb.log
logappend=true
#bind_ip = 127.0.0.1
port = 27017
# Enable journaling, http://www.mongodb.org/display/DOCS/Journaling
journal=true
# Enables periodic logging of CPU utilization and I/O wait
#cpu = true
# Turn on/off security. Off is currently the default
#noauth = true
#auth = true
# Verbose logging output.
#verbose = true
# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck = true
# Enable db quota management
#quota = true
# Set oplogging level where n is
# 0=off (default)
# 1=W
# 2=R
# 3=both
# 7=W+some reads
#oplog = 0
# Diagnostic/debugging option
#nocursors = true
# Ignore query hints
#nohints = true
# Disable the HTTP interface (Defaults to localhost:27018).
#nohttpinterface = true
# Turns off server-side scripting. This will result in greatly limited
# functionality
#noscripting = true
# Turns off table scans. Any query that would do a table scan fails.
#notablescan = true
# Disable data file preallocation.
#noprealloc = true
# Specify .ns file size for new databases.
# nssize = <size>
# Accout token for Mongo monitoring server.
#mms-token = <token>
# Server name for Mongo monitoring server.
#mms-name = <server-name>
# Ping interval for Mongo monitoring server.
#mms-interval = <seconds>
# Replication Options
# in replicated mongo databases, specify here whether this is a slave or master
#slave = true
#source = master.example.com
# Slave only: specify a single database to replicate
#only = master.example.com
# or
#master = true
#source = slave.example.com
# Address of a server to pair with.
#pairwith = <server:port>
# Address of arbiter server.
#arbiter = <server:port>
# Automatically resync if slave data is stale
#autoresync
# Custom size for replication operation log.
#oplogSize = <MB>
# Size limit for in-memory storage of op ids.
#opIdMem = <bytes>
# SSL options
# Enable SSL on normal ports
#sslOnNormalPorts = true
# SSL Key file and password
#sslPEMKeyFile = /etc/ssl/mongodb.pem
#sslPEMKeyPassword = pass

Raspberry Pi GPRS Connection: Unable to browse after ppp connection is established

I have a persistent problem browsing after connecting my GPRS/GSM Module to Raspberry Pi.
I bought the GSM/GPRS Module on amazon at http://www.amazon.com/SainSmart-Small-GPRS-SIM900-Arduino/dp/B00TEUAJMQ/ref=pd_sim_147_4?ie=UTF8&dpID=517DbMGK%2B9L&dpSrc=sims&preST=_AC_UL160_SR160%2C160_&refRID=17HH346MFMXS37D4TX8H.
I have followed the instructions as given on this site
https://learn.adafruit.com/fona-tethering-to-raspberry-pi-or-beaglebone-black?view=all
The results were as expected. I obtained the local and remote IP address needed, i got the DNS addresses too.
Running "ifconfig", i have the result below which clearly shows i can communicate via ppp0.
ppp0 Link encap:Point-to-Point Protocol
inet addr:21.144.145.193 P-t-P:10.64.64.64 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:72 (72.0 B) TX bytes:111 (111.0 B)
The problem now is that i cannot successfully ping any IP address. I keep getting "Networking not reachable" or "unknown host" in case of a url.
More so, the LED light that shows the connection is meant to be blinked twice each second but stops blinking after ppp connection in established.
Please if anyone has a suggestion on what might possibly be causing this, help me out!

TCL script cannot configure multicast socket

I'm working with tcl script under ubuntu 12.04, and I'm facing some problem when I try to configure a multicast socket. What I'm trying to do is to forward traffic from some socket to a multicast one, but I don't know why although the multicast socket is created well,apparently; it isn't bound to the multicast group I want to.
This is the script I'm using
#!/bin/sh
# test.tcl \
exec tclsh "$0" ${1+"$#"}
package require udp
set multicastPort "50003"
proc connector {unicastIP multicastIP port {protocol tcp}} {
if { [string equal $protocol "tcp"] } {
socket -server serverTCP -myaddr $unicastIP $port
puts "tcp"
} elseif {[string equal $protocol "udp" ] } {
serverUDP $unicastIP $multicastIP $port
puts "udp"
}
}
proc serverUDP {unicastIP multicastIP port} {
global multicastPort
set socketUDP [udp_open $port]
puts " $unicastIP"
fconfigure $socketUDP -blocking false -translation binary -buffering none -remote [list $unicastIP $port]
#fileevent $socketUDP readable [list gettingData $socketUDP]
set multicastSocket [udp_open $multicastPort]
udp_conf $multicastSocket -ttl 4
fconfigure $multicastSocket -blocking false -translation binary -buffering none -mcastadd $multicastIP -remote [list $multicastIP $port]
fileevent $socketUDP readable [list forwarding $socketUDP $multicastSocket ]
#puts $socketUDP "hello!"
#flush $socketUDP
}
proc forwarding {socketSrc socketDst} {
set data [read -nonewline $socketSrc]
puts "Read data-> $data"
puts -nonewline $socketDst $data
puts "Written data-> [read -nonewline $socketDst]"
}
connector 127.0.0.1 224.0.1.1 50000 udp
vwait forever
However if I run the script and check out the ports in my system, the multicast port is not assigned the proper multicast IP as you can see
~$ netstat -ptnlu
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
udp 0 0 0.0.0.0:50000 0.0.0.0:* 3334/tclsh
udp 0 0 0.0.0.0:50003 0.0.0.0:* 3334/tclsh
Could anyone tell me the reason?
THanks in advance,
Regards!
AFAIK, that is OK. I have a multicast daemon in production using Tcl and its udp package, and netstat and ss tools also show me the socket as listening on the wildcard address.
"The trick" here, I suppose, is that multicasting is one level up the stack: joining a multicast group is not merely opening a socket or an endpoint on the group address but rather sending a very real IGMP "join" message to the local transport segment (Ethernet, in most deployments) and further communicating with the nearby IGMP routers (again, on Ethernet, they're mostly switches).
So, in your case, just fire up tcpdump and see what it dumps when you start your program. A useful call to tcpdump looks something like this:
tcpdump -i eth0 -n 'igmp and host 224.0.1.1'
To observe UDP traffic exchanges use
tcpdump -i eth0 -n 'udp and host 224.0.1.1 and port 50000'