Hi future readers, 

This is just a note for others who might be in a similar situation. The 
reason Sachin had to use IPv6 was because grpc.core uses dual-stack sockets 
(at least on windows and that is what I cared for). There is no IPv4 socket 
to mutate. The socket file descriptor returned by `info->fd` is for a 
dual-stack socket. Grpc.core will use the same socket to connect to IPv4 
and IPv6 remote addresses. Just translate your IPv4 address to IPv6 format 
and use that as the source. This should do the trick. Read more about 
dual-stack sockets to understand what I am talking about.

I hope this helps others who are new to this stuff.

~ Yasoob

On Monday, February 6, 2023 at 12:07:39 PM UTC-8 Sachin Bharadwaj S wrote:

Hi Manish,

Yes it worked by binding to IPv6 (Prepend "::ffff:" to your IPv4 address)

The code looks something like this:

bool mutate_fd_callback(const grpc_mutate_socket_info* info,

                    grpc_socket_mutator* mutator) {


   int ret = 0;
struct sockaddr_in6 addr; memset(&addr, '\0', sizeof(addr)); addr.
sin6_family = AF_INET6; addr.sin6_port = htons(51234); std::string src = 
"::ffff:20.0.0.1"; inet_pton(AF_INET6, src.c_str(), &addr.sin6_addr); ret = 
bind(info->fd, (struct sockaddr *)&addr, sizeof(addr)); 
Regards,Sachin

On Thu, Feb 2, 2023 at 8:20 PM Manish Khandelwal <[email protected]> wrote:

Hi Sachin, 

I am having the same use case scenario.
Did your issue got resolved or in case if you followed any other approach 
to achieve this.

Requesting to please share the details. 

Thanks & Regards
Manish Khandelwal

On Saturday, October 1, 2022 at 3:52:17 AM UTC+5:30 [email protected] 
wrote:

Hi,

Just wanted to check on this again. Is this something possible or not 
supported in grpc c++?
Any inputs regarding this is highly appreciated.

Regards,
Sachin

On Tuesday, 27 September 2022 at 15:03:42 UTC-7 Sachin Bharadwaj S wrote:

Hi All,

Is there a way to designate a source IP and PORT from grpc c++ *client*?

Generally, on the client side, we specify the IP and PORT of the server's 
listening port to connect to and we don't care about the client's IP and 
port. When a client tries to create a channel, it assigns a random unused 
port for the outgoing connection and uses the IP which has reachability 
towards the server.

But, generally, in systems that have multiple interfaces and multiple IP 
addresses, can the application decide which outgoing interface/IP/port to 
be used for the connection?

I have used SetSocketMutator() 
<https://grpc.github.io/grpc/cpp/classgrpc_1_1_channel_arguments.html#a520690f499be85159a20200ddb986a96>
 and 
during the creation of fd, grpc stack gives a callback to the application 
and the application can mutate the fd to perform any add-on setsockopt() on 
the fd to change its behavior.
In the same callback, if I try to perform bind() to a source address and 
port, it gives an "Invalid argument" error.

*Code snippet for reference:*

bool mutate_fd_callback(const grpc_mutate_socket_info* info,

                    grpc_socket_mutator* mutator) {


    int ret = 0;

    struct   sockaddr_in addr;

    memset(&addr, '\0', sizeof(addr));


    addr.sin_addr.s_addr = inet_addr("20.0.0.1");

    addr.sin_port = htons(51234);

    addr.sin_family = AF_INET;


    ret = ::bind(info->fd, (struct sockaddr *)&addr, sizeof(struct 
sockaddr));

Does the grpc stack itself does a bind() internally and the application has 
no control over choosing its own IP/port? Or is there any other way of 
specifying src IP/PORT instead of bind()?

Please correct me if I'm wrong in any of the above understanding.

Regards,
Sachin

-- 
You received this message because you are subscribed to the Google Groups "
grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an 
email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/grpc-io/3c8c001a-e278-4c59-aeac-d07f8f51b286n%40googlegroups.com
 
<https://groups.google.com/d/msgid/grpc-io/3c8c001a-e278-4c59-aeac-d07f8f51b286n%40googlegroups.com?utm_medium=email&utm_source=footer>
.

-- 
You received this message because you are subscribed to the Google Groups 
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/grpc-io/2ebe3352-359d-47c6-88d7-f865bb755522n%40googlegroups.com.

Reply via email to