Ray Satiro via curl-library writes: > On 4/26/2020 10:40 AM, Jean-Francois Dockes via curl-library wrote: > > My apologies if this is a frequent question, I could not find an answer. > > > > It would be convenient for me to retrieve the local address of the > > connected socket used for an HTTP GET before the HTTP transaction > > begins. > > > > The pseudo HTTP protocol which my code implements (UPnP) needs to set a > > HTTP header which contains a callback URL for the remote host to send > > events to. In case the local machine has multiple interfaces, the easiest > > way to build an URL which the remote is guaranteed to be able to connect to > > seems to be just using the connected socket local address. > > > > Unfortunately, it seems that the two callbacks in this area, > > CURLOPT_SOCKOPTFUNCTION and CURLOPT_OPENSOCKETFUNCTION, occur before the > > socket is connected. > > > > I could connect the socket myself (losing the nice CURL code which does it > > for me), or find another way to derive the right interface from the > > remote address I am connecting to, but a getsockname() on the connected > > socket would be much simpler. So I was hoping that there was a way. > > > > This is assuming that I can call CURLOPT_HTTPHEADER from this point, which > > may also not be the case. So, questions... > > I don't think that is possible without doing the connect yourself. There > is CURLINFO_ACTIVESOCKET [1] but it doesn't set the socket until the > transfer is done. It is really meant to be used with > CURLOPT_CONNECT_ONLY [2]. > > Try doing the connection yourself, set CURLOPT_HTTPHEADER and then in > CURLOPT_SOCKOPTFUNCTION [3] return CURL_SOCKOPT_ALREADY_CONNECTED.
Thanks for the CURLOPT_CONNECT_ONLY pointer. Is there something to prevent me from using the resulting socket fd with CURLOPT_OPENSOCKETFUNCTION / CURLOPT_SOCKOPTFUNCTION ? I see that this is a bit convoluted, but it gains me the use of the libcurl URL parsing and connect code instead of DIY. Does it make sense ? Performance is no big issue in my use case. jf > [1]: https://curl.haxx.se/libcurl/c/CURLINFO_ACTIVESOCKET.html > [2]: https://curl.haxx.se/libcurl/c/CURLOPT_CONNECT_ONLY.html > [3]: https://curl.haxx.se/libcurl/c/CURLOPT_SOCKOPTFUNCTION.html > > ------------------------------------------------------------------- > Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library > Etiquette: https://curl.haxx.se/mail/etiquette.html ------------------------------------------------------------------- Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library Etiquette: https://curl.haxx.se/mail/etiquette.html