Ryu: convert datapath to switch IP address - ryu

Currently, I'm writing an application on top of Ryu, the Open-source OpenFlow Controller.
To create an OF-Config connection (or OVSDB connection), I think I have to get the IP address of each switch connected to the Ryu controller. However, I cannot find the API that converts the Datapath object or datapath id to the IP address of the switch.
So, if there is such an API, I want to know about it. If not, I'm looking forward to receiving some comments about the way to make the connections without the IP addresses.

#set_ev_cls(event.EventSwitchEnter)
def switch_features_handler(self, ev):
address = ev.switch.dp.address
dpid = ev.switch.dp.id
"address" is a tuple of (ip_address, port) and "dpid" is the datapath id.

Byungjoon are you using mininet?
If you are, all the switches are instantiated with the localhost ip address (This is mininet's default behavior). The controller distinguishes between switches using the tcp port.
As far as I know, you only need to know the dpid of the switch in order to send OF messages. This is what the sample l2-learning switch is doing: https://github.com/osrg/ryu/blob/master/ryu/app/simple_switch_13.py
I am also try to communicate with the switches using Ryu controller. I am using the above sample as my basic code and adding on top of it. It is not done yet (so you might see some bugs) but it's good starting point. Here is the link: https://github.com/Ehsan70/RyuApps/blob/master/l2.py

for latest version of ryu, you should use following code.
#set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def _switch_features_handler(self, ev):
print(ev.msg.datapath.address)

Related

Finding server in LAN

How to find, in python, server without having it's IP in LAN?
I assume that port will be configured in file so its doesn't have to find port. I tried to search on google but I couldn't find anything useful or that could help me with it.
The server IP will be changing because it will not run constantly on the same computer.
So basically I got app with server that is on random computer in network, and I want to find its IP from another computer.
I would be really thankful for either explanation how to do it or link that could help me to do it.
Not certain if this is what you want to do, I think you want to find the IP of a server by running some python execution on the server?
You could try :
from subprocess import call
call (["ipconfig"])
This will dump the IP config and you can parse out the IP address. This assumes a windows server. You would have to modify the call for your OS.
Updated :
Check this out : TCP Communications or UDP Communications ... it looks like what you are looking for. You will still have the mess of determining the available addresses on the network (arp -a), and testing each one - this would be your client side app. Your server side app, when it receives the right query on the TCP or UDP port, would then determine it's address (ipconfig) and return the value.

Changing IP address at runtime

I am creating a tcp connection using the function socket(), bind(), and then listen().
Our customers would like to be able to define an IP address of the server at runtime. Is there a way of changing the IP at runtime or must it be done in the BIOS?
Thanks for any tips
I've changed the IP address using ifAddrSet(..) many times. Usually I call this function from within the startup script before my application is running so I have no idea how calling this function affects already connected sockets.
But have a look at the functions provided by ifLib.h. I'm sure you'll find something that suits your needs (ifAddrAdd(..) looks promising).
I am not sure what you mean by defining 'IP address of the server at runtime?'. Obviously for a given socket it's IP address cannot be changed. It's an endpoint of a connection, it cannot be changed run time. If you just want to assign multiple IP addresses to a host that's possible.
In general - you can add as many IP addresses as you want to your machine (ok not exactly) but certainly a hundred or so (ie. statically allocated). That's not the problem (management of that is a nightmare, but sure not impossible). The problem is how those IP addresses are reached, that is not in your control, that depends upon the settings on client especially the routing entries. eg. you could use all of the IP addresses in a Subnet (say 10.1.2/24).
Not recommended - but possible.
Once you have those IP addresses - you bind on the port and address as INADDR_ANY, which says accept connection on 'any ' of the local addresses. On which address the connection was made to can be determined on server using getsockname.

Can we choose a network adapter when sending data

When programming in linux sockets, we are invoking the standard libraries socket(), connect(), send() and so on, but if we have two network adapters connected to the same LAN, can we choose one manually or it depends on the route table configured by the administrator that we can't change or something else?
Well, you can specify interface with bind(), since every interface has its unique IP address.

Assemble full URL of own server

I'm using Node.js and Express as backend to my app, and wish to upload a photo to facebook.
I came across this method of uploading an image by url: https://developers.facebook.com/blog/post/526/
Now all I need to figure out is what is my own base url, so I can assemble the full address of mt photo (say /photos/12345 should be http://www.mydomain.com/photos/12345)
Is there any way to do so? some Node env variable perhaps?
Thank
Basically, you bind Node.js / Express to a port, and perhaps to an IP address. So your question comes down to:
Is it possible to find out just by an IP address which domain name it is used by?
And the answer to that is simply: No, unfortunately not.
Reason: You can bind several domain names to the same IP address. So all you could do is query the DNS server of your choice for a reverse-lookup of the IP address to get a list of domain names. If you are lucky, there is only one. If not, there are more.
And, as an additional problem: If your code does not explicitly bind itself to a specific IP address you do not even know which IP address to use - there will be more than one: At least a IPv4 one, loopback, probably IPv6, ... potentially more if there is more than one network card, ...
So, bottom line: If you have a lot of good luck, yes, but there is no reliable method that works under every circumstance.

Same server, same program but started once using one network card and after with another

I have a Linux server with multiple ips (so, multiple eth0, eth0:0, eth0:1 etc).
The script I'm trying to start is a php CLI script which is downloading stuff from an another server API, and I would like to change the IP based on different parameters. Once the script is started, I don't need anymore to change the ip OF THAT SPECIFIC script until his end.
Do you have any clue if it is possible to achieve it?
My other solution was to install Xen or OpenVZ and create N different VPS per each IP, but as you can see is definitely a PITA :-)
You don't specify how you connect to the other server, but with sockets you can try socket_bind.
EDIT:
With curl you can try curl_setopt.
CURLOPT_INTERFACE The name of the outgoing network interface to use. This can be an interface name, an IP address or a host name.
I know how to do it in C - you use bind() on your socket before you call connect(), and you bind to the IP address assigned to the desired interface, passing 0 for port. I don't know how to do it in PHP.

Resources