How to get client's IP in a golang thrift server - go

I'm writing a thrift service in golang and I would like to understand how I can get the client's IP address in the handler functions context.
Thanks, Love.

I think you're using this Godoc Thrift library. It would be good to have code snippets in your question; such as type of thrift server, handler definition, etc.
As per Thrift Go library doc-
func (p *TSocket) Addr() net.Addr
Returns the remote address of the socket.
So if you have access to TSocket then you can get remote address.

Related

How do I return the IP address of a client who is accessing a Go program in /cgi-bin?

I am trying to simply display the IP address of a client accessing a go program located in our server's /cgi-bin directory. The line in a C program that does the same might look something like:
printf("Your current IP address is: %s", getenv("REMOTE_ADDR"));
I am extremely new to Go, with little to no introduction. I imagine it will have something to accessing the http request header, but I am unsure how to do this, and I'm having a hard time understanding the tutorials that I have found.
Using the "os" package I was able to get the client's IP with os.Getenv("REMOTE_ADDR").

Unable to make a UDP request

I am trying to build a BitTorrent client in go. I need to make UDP requests to connect to the various trackers. For this I use the net package and do this:
net.Dial("udp", "udp://hostname:1337/announce")
I get a "too many colons in address" error.
If I try this:
net.Dial("udp", "hostname:1337/announce")
I get a "nodename nor servname provided, or not known" error.
How do I fix this?
So you'll need to send it to the IP address and port as provided by the .torrent metafile (announce field).
And once you open the net.Conn you can conn.Write() to the socket and similarly conn.Read()
So you've just about gotten i:
conn, err := net.Dial("udp", announceAddr:Port)
When connecting with HTTP, yeah you use the /announce endpoint, but not with UDP
The specs explain how many bytes to read and write (it is fixed at first, but later dynamic when it comes to reading the peer list). I've found this link, rather, the most useful: https://github.com/naim94a/udpt/wiki/The-BitTorrent-UDP-tracker-protocol

ZMQError: Cannot assign requested address

I'm using zeromq to develop a dynamic application which operate like upnp protocol( autoconfiguration and auto-discovery of equipment on a living room).
To do it so, i use zeromq to distribute messages, my problem is that when i create a socket and binding on a adress "169.254.1.0 through 169.254.254.255", I receive a error messages like this: ZMQError: Cannot assign requested address.
I've tried to figure out why by changing the address by local adress (127.0.0.1),and it's works !!.
The problem is that i'm implementing autoip, it's means that i'm oblliged to use the adresse on range 169.254.1.0 through 169.254.254.255.
Thanks in advance for your help!
I got this error, too... and I realized I had the wrong idea of who was connecting to whom in the PUB/SUB model.
This was working: Host A as the ZMQ PUB, and could easily connect with Host A as the ZMQ SUB.
Then I tried to set up Host B as the SUB, and have Host A "send" to that... and I kept getting this error. After all don't you need to tell the PUB where to send the data?
No! Host A as the PUB should still bind to IP_ANY (0.0.0.0); it is Host B as the SUB who has to be configured with the address of Host A.
Once I got that straight in my code (and in my head), I was up and running.
After you bring the interface up, you need to get its IP address and then bind on that. Alternatively you may be able to bind on "*", meaning all interfaces.

Assign specific port to RPC server\client

I am writting a RPC client server application on windows. I have gone through RPC sample programs MS has given. But none of them mention port specifically. This probably because RPC uses dynamic port above 1024. But what if I wanted to assign specific port/port range to specific service (my server and client app for example). How can I do that? I can use RPCCFG to assign range but that range will be for all RPC programs (http://support.microsoft.com/kb/908472) right? How can I control a single program? I know it's possible because exchange seem to able to do it for Client Access Service?
Thanks in advance,
-Neel.
You can define ports in the code or use a config file which you read in the code.
status = RpcServerUseProtseqEp(
(char *)"ncacn_ip_tcp", // Use TCP/IP
RPC_C_PROTSEQ_MAX_REQS_DEFAULT, // Backlog queue length for TCP/IP.
(char *)"4747", // TCP/IP port to use.
NULL); // No security.
Success. Jasper

Receiving datagrams using Udp connection

In order to receive datagrams through an UDP connection I have created an object of type UDPClient.
receivedNotificationSock = new UdpClient();
However once done and on using the receive method:
receivedHostNameBuffer=receivedNotificationSock.Receive(ref receivedNotificationIP);
I am getting an exception saying that I must call the bind method.
But there is no bind method in the UDPClient class.
Could You guys please provide me with the code if possible as to what should be done to overcome this exception.
You need I think to know some more about sockets.
All sockets possess a port number. First, you create a socket - which is almost useless on its own. It just floats there. But then you bind it - you assign it a port number. Now it's useful - now you can send and receive data on it.
Remember, all UDP communications are defined by the quad data set of the IP and port of the source and the IP and port of the destination. A freshly created socket doesn't have an IP address or port; binding gives it an IP address and port.
Unfortunately, I'm not a C# programmer, so I can't properly answer your question. But at least you know why it's important.
Pass the port number into the constructor of your UDP client.
receivedNotificationSock = new UdpClient(21000);
You may need to change firewall settings to allow the bind, though a popup window normally opens when you first run this on your dev machine.
For Socket proramming you need to know the sequence of syscalls you need to do on client side and on the server side.
If you are writting a client :
you open a socket with a socket call.
you then connect to the server port with a connect call
once connect is successful
then you send the request to the server using either a send or sendto or a write
which results in reception of data that you can read using a receive or read
On Server Side
you create a socket
bind it to a port
start listening on the socket for incoming connections from various clients using a listen.
There is a non blocking way of listening for connections as well with a select syscall.
Once the you establish a connection you can essentially read the request and start processing it.
Here's an example in C# that may be useful to you.
http://www.developerfusion.com/article/3918/socket-programming-in-c-part-1/

Resources