9 #include <sys/socket.h>
10 #include <netinet/in.h>
12 #include <net/gen/in.h>
13 #include <net/gen/tcp.h>
14 #include <net/gen/tcp_io.h>
15 #include <net/gen/udp.h>
16 #include <net/gen/udp_io.h>
21 static int _tcp_getsockname(int fd
, struct sockaddr
*__restrict address
,
22 socklen_t
*__restrict address_len
, nwio_tcpconf_t
*tcpconfp
);
24 static int _udp_getsockname(int fd
, struct sockaddr
*__restrict address
,
25 socklen_t
*__restrict address_len
, nwio_udpopt_t
*udpopt
);
27 static int _uds_getsockname(int fd
, struct sockaddr
*__restrict address
,
28 socklen_t
*__restrict address_len
, struct sockaddr_un
*uds_addr
);
31 * Get the local address of a socket.
34 __getsockname(int fd
, struct sockaddr
* __restrict address
,
35 socklen_t
* __restrict address_len
)
39 if (address_len
== NULL
) {
44 memset(&m
, 0, sizeof(m
));
45 m
.m_lc_vfs_sockaddr
.fd
= fd
;
46 m
.m_lc_vfs_sockaddr
.addr
= (vir_bytes
)address
;
47 m
.m_lc_vfs_sockaddr
.addr_len
= *address_len
;
49 if (_syscall(VFS_PROC_NR
, VFS_GETSOCKNAME
, &m
) < 0)
52 *address_len
= m
.m_vfs_lc_socklen
.len
;
56 int getsockname(int fd
, struct sockaddr
*__restrict address
,
57 socklen_t
*__restrict address_len
)
60 nwio_tcpconf_t tcpconf
;
62 struct sockaddr_un uds_addr
;
64 r
= __getsockname(fd
, address
, address_len
);
65 if (r
!= -1 || (errno
!= ENOTSOCK
&& errno
!= ENOSYS
))
69 fprintf(stderr
,"mnx_getsockname: ioctl fd %d.\n", fd
);
72 r
= ioctl(fd
, NWIOGTCPCONF
, &tcpconf
);
73 if (r
!= -1 || errno
!= ENOTTY
)
77 /* Bad file descriptor */
81 return _tcp_getsockname(fd
, address
, address_len
, &tcpconf
);
84 r
= ioctl(fd
, NWIOGUDPOPT
, &udpopt
);
85 if (r
!= -1 || errno
!= ENOTTY
)
89 /* Bad file descriptor */
93 return _udp_getsockname(fd
, address
, address_len
, &udpopt
);
96 r
= ioctl(fd
, NWIOGUDSADDR
, &uds_addr
);
97 if (r
!= -1 || errno
!= ENOTTY
)
101 /* Bad file descriptor */
105 return _uds_getsockname(fd
, address
, address_len
, &uds_addr
);
113 static int _tcp_getsockname(int fd
, struct sockaddr
*__restrict address
,
114 socklen_t
*__restrict address_len
, nwio_tcpconf_t
*tcpconf
)
117 struct sockaddr_in sin
;
120 fprintf(stderr
, "mnx_getsockname: from %s, %u",
121 inet_ntoa(tcpconf
->nwtc_remaddr
),
122 ntohs(tcpconf
->nwtc_remport
));
123 fprintf(stderr
," for %s, %u\n",
124 inet_ntoa(tcpconf
->nwtc_locaddr
),
125 ntohs(tcpconf
->nwtc_locport
));
128 memset(&sin
, '\0', sizeof(sin
));
129 sin
.sin_family
= AF_INET
;
130 sin
.sin_addr
.s_addr
= tcpconf
->nwtc_locaddr
;
131 sin
.sin_port
= tcpconf
->nwtc_locport
;
132 sin
.sin_len
= sizeof(sin
);
135 if (len
> sizeof(sin
))
137 memcpy(address
, &sin
, len
);
143 static int _udp_getsockname(int fd
, struct sockaddr
*__restrict address
,
144 socklen_t
*__restrict address_len
, nwio_udpopt_t
*udpopt
)
147 struct sockaddr_in sin
;
150 fprintf(stderr
, "mnx_getsockname: from %s, %u",
151 inet_ntoa(udpopt
->nwuo_remaddr
),
152 ntohs(udpopt
->nwuo_remport
));
153 fprintf(stderr
," for %s, %u\n",
154 inet_ntoa(udpopt
->nwuo_locaddr
),
155 ntohs(udpopt
->nwuo_locport
));
158 memset(&sin
, '\0', sizeof(sin
));
159 sin
.sin_family
= AF_INET
;
160 sin
.sin_addr
.s_addr
= udpopt
->nwuo_locaddr
;
161 sin
.sin_port
= udpopt
->nwuo_locport
;
162 sin
.sin_len
= sizeof(sin
);
165 if (len
> sizeof(sin
))
167 memcpy(address
, &sin
, len
);
173 static int _uds_getsockname(int fd
, struct sockaddr
*__restrict address
,
174 socklen_t
*__restrict address_len
, struct sockaddr_un
*uds_addr
)
178 if (uds_addr
->sun_family
!= AF_UNIX
)
185 if (len
> sizeof(struct sockaddr_un
))
186 len
= sizeof(struct sockaddr_un
);
188 memcpy(address
, uds_addr
, len
);