PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/nsock/src/nsock_connect.c

https://gitlab.com/g10h4ck/nmap-gsoc2015
C | 560 lines | 327 code | 105 blank | 128 comment | 96 complexity | 55944fc3da18d0fef50e90108d701b34 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Apache-2.0, LGPL-2.0, LGPL-2.1, MIT
  1. /***************************************************************************
  2. * nsock_connect.c -- This contains the functions for requesting TCP *
  3. * connections from the nsock parallel socket event library *
  4. ***********************IMPORTANT NSOCK LICENSE TERMS***********************
  5. * *
  6. * The nsock parallel socket event library is (C) 1999-2015 Insecure.Com *
  7. * LLC This library is free software; you may redistribute and/or *
  8. * modify it under the terms of the GNU General Public License as *
  9. * published by the Free Software Foundation; Version 2. This guarantees *
  10. * your right to use, modify, and redistribute this software under certain *
  11. * conditions. If this license is unacceptable to you, Insecure.Com LLC *
  12. * may be willing to sell alternative licenses (contact *
  13. * sales@insecure.com ). *
  14. * *
  15. * As a special exception to the GPL terms, Insecure.Com LLC grants *
  16. * permission to link the code of this program with any version of the *
  17. * OpenSSL library which is distributed under a license identical to that *
  18. * listed in the included docs/licenses/OpenSSL.txt file, and distribute *
  19. * linked combinations including the two. You must obey the GNU GPL in all *
  20. * respects for all of the code used other than OpenSSL. If you modify *
  21. * this file, you may extend this exception to your version of the file, *
  22. * but you are not obligated to do so. *
  23. * *
  24. * If you received these files with a written license agreement stating *
  25. * terms other than the (GPL) terms above, then that alternative license *
  26. * agreement takes precedence over this comment. *
  27. * *
  28. * Source is provided to this software because we believe users have a *
  29. * right to know exactly what a program is going to do before they run it. *
  30. * This also allows you to audit the software for security holes. *
  31. * *
  32. * Source code also allows you to port Nmap to new platforms, fix bugs, *
  33. * and add new features. You are highly encouraged to send your changes *
  34. * to the dev@nmap.org mailing list for possible incorporation into the *
  35. * main distribution. By sending these changes to Fyodor or one of the *
  36. * Insecure.Org development mailing lists, or checking them into the Nmap *
  37. * source code repository, it is understood (unless you specify otherwise) *
  38. * that you are offering the Nmap Project (Insecure.Com LLC) the *
  39. * unlimited, non-exclusive right to reuse, modify, and relicense the *
  40. * code. Nmap will always be available Open Source, but this is important *
  41. * because the inability to relicense code has caused devastating problems *
  42. * for other Free Software projects (such as KDE and NASM). We also *
  43. * occasionally relicense the code to third parties as discussed above. *
  44. * If you wish to specify special license conditions of your *
  45. * contributions, just say so when you send them. *
  46. * *
  47. * This program is distributed in the hope that it will be useful, but *
  48. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  49. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  50. * General Public License v2.0 for more details *
  51. * (http://www.gnu.org/licenses/gpl-2.0.html). *
  52. * *
  53. ***************************************************************************/
  54. /* $Id$ */
  55. #include "nsock.h"
  56. #include "nsock_internal.h"
  57. #include "nsock_log.h"
  58. #include "nsock_proxy.h"
  59. #include "netutils.h"
  60. #include <sys/types.h>
  61. #include <errno.h>
  62. #include <string.h>
  63. static int mksock_bind_addr(struct npool *ms, struct niod *iod) {
  64. int rc;
  65. int one = 1;
  66. rc = setsockopt(iod->sd, SOL_SOCKET, SO_REUSEADDR, (const char *)&one, sizeof(one));
  67. if (rc == -1) {
  68. int err = socket_errno();
  69. nsock_log_error("Setting of SO_REUSEADDR failed (#%li): %s (%d)", iod->id,
  70. socket_strerror(err), err);
  71. }
  72. nsock_log_info("Binding to %s (IOD #%li)", get_localaddr_string(iod), iod->id);
  73. rc = bind(iod->sd, (struct sockaddr *)&iod->local, (int) iod->locallen);
  74. if (rc == -1) {
  75. int err = socket_errno();
  76. nsock_log_error("Bind to %s failed (IOD #%li): %s (%d)",
  77. get_localaddr_string(iod), iod->id,
  78. socket_strerror(err), err);
  79. }
  80. return 0;
  81. }
  82. static int mksock_set_ipopts(struct npool *ms, struct niod *iod) {
  83. int rc;
  84. errno = 0;
  85. rc = setsockopt(iod->sd, IPPROTO_IP, IP_OPTIONS, (const char *)iod->ipopts,
  86. iod->ipoptslen);
  87. if (rc == -1) {
  88. int err = socket_errno();
  89. nsock_log_error("Setting of IP options failed (IOD #%li): %s (%d)",
  90. iod->id, socket_strerror(err), err);
  91. }
  92. return 0;
  93. }
  94. static int mksock_bind_device(struct npool *ms, struct niod *iod) {
  95. int rc;
  96. rc = socket_bindtodevice(iod->sd, ms->device);
  97. if (!rc) {
  98. int err = socket_errno();
  99. if (err != EPERM)
  100. nsock_log_error("Setting of SO_BINDTODEVICE failed (IOD #%li): %s (%d)",
  101. iod->id, socket_strerror(err), err);
  102. else
  103. nsock_log_debug_all("Setting of SO_BINDTODEVICE failed (IOD #%li): %s (%d)",
  104. iod->id, socket_strerror(err), err);
  105. }
  106. return 0;
  107. }
  108. static int mksock_set_broadcast(struct npool *ms, struct niod *iod) {
  109. int rc;
  110. int one = 1;
  111. rc = setsockopt(iod->sd, SOL_SOCKET, SO_BROADCAST,
  112. (const char *)&one, sizeof(one));
  113. if (rc == -1) {
  114. int err = socket_errno();
  115. nsock_log_error("Setting of SO_BROADCAST failed (IOD #%li): %s (%d)",
  116. iod->id, socket_strerror(err), err);
  117. }
  118. return 0;
  119. }
  120. /* Create the actual socket (nse->iod->sd) underlying the iod. This unblocks the
  121. * socket, binds to the localaddr address, sets IP options, and sets the
  122. * broadcast flag. Trying to change these functions after making this call will
  123. * not have an effect. This function needs to be called before you try to read
  124. * or write on the iod. */
  125. static int nsock_make_socket(struct npool *ms, struct niod *iod, int family, int type, int proto) {
  126. /* inheritable_socket is from nbase */
  127. iod->sd = (int)inheritable_socket(family, type, proto);
  128. if (iod->sd == -1) {
  129. nsock_log_error("Socket trouble: %s", socket_strerror(socket_errno()));
  130. return -1;
  131. }
  132. unblock_socket(iod->sd);
  133. iod->lastproto = proto;
  134. if (iod->locallen)
  135. mksock_bind_addr(ms, iod);
  136. if (iod->ipoptslen && family == AF_INET)
  137. mksock_set_ipopts(ms, iod);
  138. if (ms->device)
  139. mksock_bind_device(ms, iod);
  140. if (ms->broadcast && type != SOCK_STREAM)
  141. mksock_set_broadcast(ms, iod);
  142. /* mksock_* functions can raise warnings/errors
  143. * but we don't let them stop us for now. */
  144. return iod->sd;
  145. }
  146. int nsock_setup_udp(nsock_pool nsp, nsock_iod ms_iod, int af) {
  147. struct npool *ms = (struct npool *)nsp;
  148. struct niod *nsi = (struct niod *)ms_iod;
  149. assert(nsi->state == NSIOD_STATE_INITIAL || nsi->state == NSIOD_STATE_UNKNOWN);
  150. nsock_log_info("UDP unconnected socket (IOD #%li)", nsi->id);
  151. if (nsock_make_socket(ms, nsi, af, SOCK_DGRAM, IPPROTO_UDP) == -1)
  152. return -1;
  153. return nsi->sd;
  154. }
  155. /* This does the actual logistics of requesting a TCP connection. It is shared
  156. * by nsock_connect_tcp and nsock_connect_ssl */
  157. void nsock_connect_internal(struct npool *ms, struct nevent *nse, int type, int proto, struct sockaddr_storage *ss, size_t sslen,
  158. unsigned short port) {
  159. struct sockaddr_in *sin;
  160. #if HAVE_IPV6
  161. struct sockaddr_in6 *sin6;
  162. #endif
  163. struct niod *iod = nse->iod;
  164. if (iod->px_ctx /* proxy enabled */
  165. && proto == IPPROTO_TCP /* restrict proxying to TCP connections */
  166. && (nse->handler != nsock_proxy_ev_dispatch)) { /* for reentrancy */
  167. struct proxy_node *current;
  168. nsock_log_debug_all("TCP connection request (EID %lu) redirected through proxy chain",
  169. (long)nse->id);
  170. current = iod->px_ctx->px_current;
  171. assert(current != NULL);
  172. memcpy(&iod->px_ctx->target_ss, ss, sslen);
  173. iod->px_ctx->target_sslen = sslen;
  174. iod->px_ctx->target_port = port;
  175. ss = &current->ss;
  176. sslen = current->sslen;
  177. port = current->port;
  178. iod->px_ctx->target_handler = nse->handler;
  179. nse->handler = nsock_proxy_ev_dispatch;
  180. iod->px_ctx->target_ev_type = nse->type;
  181. nse->type = NSE_TYPE_CONNECT;
  182. }
  183. sin = (struct sockaddr_in *)ss;
  184. #if HAVE_IPV6
  185. sin6 = (struct sockaddr_in6 *)ss;
  186. #endif
  187. /* Now it is time to actually attempt the connection */
  188. if (nsock_make_socket(ms, iod, ss->ss_family, type, proto) == -1) {
  189. nse->event_done = 1;
  190. nse->status = NSE_STATUS_ERROR;
  191. nse->errnum = socket_errno();
  192. } else {
  193. if (ss->ss_family == AF_INET) {
  194. sin->sin_port = htons(port);
  195. }
  196. #if HAVE_IPV6
  197. else if (ss->ss_family == AF_INET6) {
  198. sin6->sin6_port = htons(port);
  199. }
  200. #endif
  201. #if HAVE_SYS_UN_H
  202. else if (ss->ss_family == AF_UNIX) {
  203. }
  204. #endif
  205. else {
  206. fatal("Unknown address family %d\n", ss->ss_family);
  207. }
  208. assert(sslen <= sizeof(iod->peer));
  209. if (&iod->peer != ss)
  210. memcpy(&iod->peer, ss, sslen);
  211. iod->peerlen = sslen;
  212. if (connect(iod->sd, (struct sockaddr *)ss, sslen) == -1) {
  213. int err = socket_errno();
  214. if (proto == IPPROTO_UDP || (err != EINPROGRESS && err != EAGAIN)) {
  215. nse->event_done = 1;
  216. nse->status = NSE_STATUS_ERROR;
  217. nse->errnum = err;
  218. }
  219. }
  220. /* The callback handle_connect_result handles the connection once it completes. */
  221. }
  222. }
  223. #if HAVE_SYS_UN_H
  224. /* Request a UNIX domain sockets connection to the same system (by path to socket).
  225. * This function connects to the socket of type SOCK_STREAM. ss should be a
  226. * sockaddr_storage, sockaddr_un as appropriate (just like what you would pass to
  227. * connect). sslen should be the sizeof the structure you are passing in. */
  228. nsock_event_id nsock_connect_unixsock_stream(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handler handler, int timeout_msecs,
  229. void *userdata, struct sockaddr *saddr, size_t sslen) {
  230. struct niod *nsi = (struct niod *)nsiod;
  231. struct npool *ms = (struct npool *)nsp;
  232. struct nevent *nse;
  233. struct sockaddr_storage *ss = (struct sockaddr_storage *)saddr;
  234. assert(nsi->state == NSIOD_STATE_INITIAL || nsi->state == NSIOD_STATE_UNKNOWN);
  235. nse = event_new(ms, NSE_TYPE_CONNECT, nsi, timeout_msecs, handler, userdata);
  236. assert(nse);
  237. nsock_log_info("UNIX domain socket (STREAM) connection requested to %s (IOD #%li) EID %li",
  238. get_unixsock_path(ss), nsi->id, nse->id);
  239. nsock_connect_internal(ms, nse, SOCK_STREAM, 0, ss, sslen, 0);
  240. nsock_pool_add_event(ms, nse);
  241. return nse->id;
  242. }
  243. /* Request a UNIX domain sockets connection to the same system (by path to socket).
  244. * This function connects to the socket of type SOCK_DGRAM. ss should be a
  245. * sockaddr_storage, sockaddr_un as appropriate (just like what you would pass to
  246. * connect). sslen should be the sizeof the structure you are passing in. */
  247. nsock_event_id nsock_connect_unixsock_datagram(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handler handler,
  248. void *userdata, struct sockaddr *saddr, size_t sslen) {
  249. struct niod *nsi = (struct niod *)nsiod;
  250. struct npool *ms = (struct npool *)nsp;
  251. struct nevent *nse;
  252. struct sockaddr_storage *ss = (struct sockaddr_storage *)saddr;
  253. assert(nsi->state == NSIOD_STATE_INITIAL || nsi->state == NSIOD_STATE_UNKNOWN);
  254. nse = event_new(ms, NSE_TYPE_CONNECT, nsi, -1, handler, userdata);
  255. assert(nse);
  256. nsock_log_info("UNIX domain socket (DGRAM) connection requested to %s (IOD #%li) EID %li",
  257. get_unixsock_path(ss), nsi->id, nse->id);
  258. nsock_connect_internal(ms, nse, SOCK_DGRAM, 0, ss, sslen, 0);
  259. nsock_pool_add_event(ms, nse);
  260. return nse->id;
  261. }
  262. #endif /* HAVE_SYS_UN_H */
  263. /* Request a TCP connection to another system (by IP address). The in_addr is
  264. * normal network byte order, but the port number should be given in HOST BYTE
  265. * ORDER. ss should be a sockaddr_storage, sockaddr_in6, or sockaddr_in as
  266. * appropriate (just like what you would pass to connect). sslen should be the
  267. * sizeof the structure you are passing in. */
  268. nsock_event_id nsock_connect_tcp(nsock_pool nsp, nsock_iod ms_iod, nsock_ev_handler handler, int timeout_msecs,
  269. void *userdata, struct sockaddr *saddr, size_t sslen, unsigned short port) {
  270. struct niod *nsi = (struct niod *)ms_iod;
  271. struct npool *ms = (struct npool *)nsp;
  272. struct nevent *nse;
  273. struct sockaddr_storage *ss = (struct sockaddr_storage *)saddr;
  274. assert(nsi->state == NSIOD_STATE_INITIAL || nsi->state == NSIOD_STATE_UNKNOWN);
  275. nse = event_new(ms, NSE_TYPE_CONNECT, nsi, timeout_msecs, handler, userdata);
  276. assert(nse);
  277. nsock_log_info("TCP connection requested to %s:%hu (IOD #%li) EID %li",
  278. inet_ntop_ez(ss, sslen), port, nsi->id, nse->id);
  279. /* Do the actual connect() */
  280. nsock_connect_internal(ms, nse, SOCK_STREAM, IPPROTO_TCP, ss, sslen, port);
  281. nsock_pool_add_event(ms, nse);
  282. return nse->id;
  283. }
  284. /* Request an SCTP association to another system (by IP address). The in_addr
  285. * is normal network byte order, but the port number should be given in HOST
  286. * BYTE ORDER. ss should be a sockaddr_storage, sockaddr_in6, or sockaddr_in as
  287. * appropriate (just like what you would pass to connect). sslen should be the
  288. * sizeof the structure you are passing in. */
  289. nsock_event_id nsock_connect_sctp(nsock_pool nsp, nsock_iod ms_iod, nsock_ev_handler handler, int timeout_msecs,
  290. void *userdata, struct sockaddr *saddr, size_t sslen, unsigned short port) {
  291. struct niod *nsi = (struct niod *)ms_iod;
  292. struct npool *ms = (struct npool *)nsp;
  293. struct nevent *nse;
  294. struct sockaddr_storage *ss = (struct sockaddr_storage *)saddr;
  295. assert(nsi->state == NSIOD_STATE_INITIAL || nsi->state == NSIOD_STATE_UNKNOWN);
  296. nse = event_new(ms, NSE_TYPE_CONNECT, nsi, timeout_msecs, handler, userdata);
  297. assert(nse);
  298. nsock_log_info("SCTP association requested to %s:%hu (IOD #%li) EID %li",
  299. inet_ntop_ez(ss, sslen), port, nsi->id, nse->id);
  300. /* Do the actual connect() */
  301. nsock_connect_internal(ms, nse, SOCK_STREAM, IPPROTO_SCTP, ss, sslen, port);
  302. nsock_pool_add_event(ms, nse);
  303. return nse->id;
  304. }
  305. /* Request an SSL over TCP/SCTP connection to another system (by IP address).
  306. * The in_addr is normal network byte order, but the port number should be given
  307. * in HOST BYTE ORDER. This function will call back only after it has made the
  308. * connection AND done the initial SSL negotiation. From that point on, you use
  309. * the normal read/write calls and decryption will happen transparently. ss
  310. * should be a sockaddr_storage, sockaddr_in6, or sockaddr_in as appropriate
  311. * (just like what you would pass to connect). sslen should be the sizeof the
  312. * structure you are passing in. */
  313. nsock_event_id nsock_connect_ssl(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handler handler, int timeout_msecs,
  314. void *userdata, struct sockaddr *saddr, size_t sslen, int proto, unsigned short port, nsock_ssl_session ssl_session) {
  315. #ifndef HAVE_OPENSSL
  316. fatal("nsock_connect_ssl called - but nsock was built w/o SSL support. QUITTING");
  317. return (nsock_event_id)0; /* UNREACHED */
  318. #else
  319. struct sockaddr_storage *ss = (struct sockaddr_storage *)saddr;
  320. struct niod *nsi = (struct niod *)nsiod;
  321. struct npool *ms = (struct npool *)nsp;
  322. struct nevent *nse;
  323. if (!ms->sslctx)
  324. nsock_pool_ssl_init(ms, 0);
  325. assert(nsi->state == NSIOD_STATE_INITIAL || nsi->state == NSIOD_STATE_UNKNOWN);
  326. nse = event_new(ms, NSE_TYPE_CONNECT_SSL, nsi, timeout_msecs, handler, userdata);
  327. assert(nse);
  328. /* Set our SSL_SESSION so we can benefit from session-id reuse. */
  329. nsi_set_ssl_session(nsi, (SSL_SESSION *)ssl_session);
  330. nsock_log_info("SSL connection requested to %s:%hu/%s (IOD #%li) EID %li",
  331. inet_ntop_ez(ss, sslen), port, (proto == IPPROTO_TCP ? "tcp" : "sctp"),
  332. nsi->id, nse->id);
  333. /* Do the actual connect() */
  334. nsock_connect_internal(ms, nse, SOCK_STREAM, proto, ss, sslen, port);
  335. nsock_pool_add_event(ms, nse);
  336. return nse->id;
  337. #endif /* HAVE_OPENSSL */
  338. }
  339. /* Request ssl connection over already established connection. nsiod must be
  340. * socket that is already connected to target using nsock_connect_tcp or
  341. * nsock_connect_sctp. All parameters have the same meaning as in
  342. * 'nsock_connect_ssl' */
  343. nsock_event_id nsock_reconnect_ssl(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handler handler, int timeout_msecs,
  344. void *userdata, nsock_ssl_session ssl_session) {
  345. #ifndef HAVE_OPENSSL
  346. fatal("nsock_reconnect_ssl called - but nsock was built w/o SSL support. QUITTING");
  347. return (nsock_event_id) 0; /* UNREACHED */
  348. #else
  349. struct niod *nsi = (struct niod *)nsiod;
  350. struct npool *ms = (struct npool *)nsp;
  351. struct nevent *nse;
  352. if (!ms->sslctx)
  353. nsock_pool_ssl_init(ms, 0);
  354. nse = event_new(ms, NSE_TYPE_CONNECT_SSL, nsi, timeout_msecs, handler, userdata);
  355. assert(nse);
  356. /* Set our SSL_SESSION so we can benefit from session-id reuse. */
  357. nsi_set_ssl_session(nsi, (SSL_SESSION *)ssl_session);
  358. nsock_log_info("SSL reconnection requested (IOD #%li) EID %li",
  359. nsi->id, nse->id);
  360. /* Do the actual connect() */
  361. nse->event_done = 0;
  362. nse->status = NSE_STATUS_SUCCESS;
  363. nsock_pool_add_event(ms, nse);
  364. return nse->id;
  365. #endif /* HAVE_OPENSSL */
  366. }
  367. /* Request a UDP "connection" to another system (by IP address). The in_addr is
  368. * normal network byte order, but the port number should be given in HOST BYTE
  369. * ORDER. Since this is UDP, no packets are actually sent. The destination IP
  370. * and port are just associated with the nsiod (an actual OS connect() call is
  371. * made). You can then use the normal nsock write calls on the socket. There
  372. * is no timeout since this call always calls your callback at the next
  373. * opportunity. The advantages to having a connected UDP socket (as opposed to
  374. * just specifying an address with sendto() are that we can now use a consistent
  375. * set of write/read calls for TCP/UDP, received packets from the non-partner
  376. * are automatically dropped by the OS, and the OS can provide asynchronous
  377. * errors (see Unix Network Programming pp224). ss should be a
  378. * sockaddr_storage, sockaddr_in6, or sockaddr_in as appropriate (just like what
  379. * you would pass to connect). sslen should be the sizeof the structure you are
  380. * passing in. */
  381. nsock_event_id nsock_connect_udp(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handler handler, void *userdata,
  382. struct sockaddr *saddr, size_t sslen, unsigned short port) {
  383. struct niod *nsi = (struct niod *)nsiod;
  384. struct npool *ms = (struct npool *)nsp;
  385. struct nevent *nse;
  386. struct sockaddr_storage *ss = (struct sockaddr_storage *)saddr;
  387. assert(nsi->state == NSIOD_STATE_INITIAL || nsi->state == NSIOD_STATE_UNKNOWN);
  388. nse = event_new(ms, NSE_TYPE_CONNECT, nsi, -1, handler, userdata);
  389. assert(nse);
  390. nsock_log_info("UDP connection requested to %s:%hu (IOD #%li) EID %li",
  391. inet_ntop_ez(ss, sslen), port, nsi->id, nse->id);
  392. nsock_connect_internal(ms, nse, SOCK_DGRAM, IPPROTO_UDP, ss, sslen, port);
  393. nsock_pool_add_event(ms, nse);
  394. return nse->id;
  395. }
  396. /* Returns that host/port/protocol information for the last communication (or
  397. * comm. attempt) this nsi has been involved with. By "involved" with I mean
  398. * interactions like establishing (or trying to) a connection or sending a UDP
  399. * datagram through an unconnected nsock_iod. AF is the address family (AF_INET
  400. * or AF_INET6), Protocl is IPPROTO_TCP or IPPROTO_UDP. Pass NULL for
  401. * information you do not need. If ANY of the information you requested is not
  402. * available, 0 will be returned and the unavailable sockets are zeroed. If
  403. * protocol or af is requested but not available, it will be set to -1 (and 0
  404. * returned). The pointers you pass in must be NULL or point to allocated
  405. * address space. The sockaddr members should actually be sockaddr_storage,
  406. * sockaddr_in6, or sockaddr_in with the socklen of them set appropriately (eg
  407. * sizeof(sockaddr_storage) if that is what you are passing). */
  408. int nsock_iod_get_communication_info(nsock_iod iod, int *protocol, int *af,
  409. struct sockaddr *local,
  410. struct sockaddr *remote, size_t socklen) {
  411. struct niod *nsi = (struct niod *)iod;
  412. int ret = 1;
  413. struct sockaddr_storage ss;
  414. socklen_t slen = sizeof(ss);
  415. int res;
  416. assert(socklen > 0);
  417. if (nsi->peerlen > 0) {
  418. if (remote)
  419. memcpy(remote, &(nsi->peer), MIN((unsigned)socklen, nsi->peerlen));
  420. if (protocol) {
  421. *protocol = nsi->lastproto;
  422. if (*protocol == -1) res = 0;
  423. }
  424. if (af) {
  425. *af = nsi->peer.ss_family;
  426. }
  427. if (local) {
  428. if (nsi->sd >= 0) {
  429. res = getsockname(nsi->sd, (struct sockaddr *)&ss, &slen);
  430. if (res == -1) {
  431. memset(local, 0, socklen);
  432. ret = 0;
  433. } else {
  434. assert(slen > 0);
  435. memcpy(local, &ss, MIN((unsigned)slen, socklen));
  436. }
  437. } else {
  438. memset(local, 0, socklen);
  439. ret = 0;
  440. }
  441. }
  442. } else {
  443. if (local || remote || protocol || af)
  444. ret = 0;
  445. if (remote)
  446. memset(remote, 0, socklen);
  447. if (local)
  448. memset(local, 0, socklen);
  449. if (protocol)
  450. *protocol = -1;
  451. if (af)
  452. *af = -1;
  453. }
  454. return ret;
  455. }