MCPcopy Create free account
hub / github.com/ElementsProject/lightning / get_local_sockname

Function get_local_sockname

connectd/netaddress.c:199–224  ·  view source on GitHub ↗

Trick I learned from Harald Welte: create UDP socket, connect() and * then query address. */ Returns 0 if protocol completely unsupported, ADDR_LISTEN if we * can't reach addr, ADDR_LISTEN_AND_ANNOUNCE if we can (and fill saddr). */

Source from the content-addressed store, hash-verified

197/* Returns 0 if protocol completely unsupported, ADDR_LISTEN if we
198 * can't reach addr, ADDR_LISTEN_AND_ANNOUNCE if we can (and fill saddr). */
199static bool get_local_sockname(int af, void *saddr, socklen_t saddrlen)
200{
201 int fd = socket(af, SOCK_DGRAM, 0);
202 if (fd < 0) {
203 status_debug("Failed to create %u socket: %s",
204 af, strerror(errno));
205 return false;
206 }
207
208 if (connect(fd, saddr, saddrlen) != 0) {
209 status_debug("Failed to connect %u socket: %s",
210 af, strerror(errno));
211 close(fd);
212 return false;
213 }
214
215 if (getsockname(fd, saddr, &saddrlen) != 0) {
216 status_debug("Failed to get %u socket name: %s",
217 af, strerror(errno));
218 close(fd);
219 return false;
220 }
221
222 close(fd);
223 return true;
224}
225
226bool guess_address(struct wireaddr *addr)
227{

Callers 1

guess_addressFunction · 0.85

Calls 1

connectFunction · 0.50

Tested by

no test coverage detected