22
22
#include "includes.h"
24
/****************************************************************************
25
Get a port number in host byte order from a sockaddr_storage.
26
****************************************************************************/
28
uint16_t get_sockaddr_port(const struct sockaddr_storage *pss)
32
if (pss->ss_family != AF_INET) {
33
#if defined(HAVE_IPV6)
35
const struct sockaddr_in6 *sa6 =
36
(const struct sockaddr_in6 *)pss;
37
port = ntohs(sa6->sin6_port);
40
const struct sockaddr_in *sa =
41
(const struct sockaddr_in *)pss;
42
port = ntohs(sa->sin_port);
47
/****************************************************************************
48
Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
49
****************************************************************************/
51
static char *print_sockaddr_len(char *dest,
53
const struct sockaddr *psa,
59
(void)sys_getnameinfo(psa,
67
/****************************************************************************
68
Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
69
****************************************************************************/
71
char *print_sockaddr(char *dest,
73
const struct sockaddr_storage *psa)
75
return print_sockaddr_len(dest, destlen, (struct sockaddr *)psa,
76
sizeof(struct sockaddr_storage));
79
/****************************************************************************
80
Print out a canonical IPv4 or IPv6 address from a struct sockaddr_storage.
81
****************************************************************************/
83
char *print_canonical_sockaddr(TALLOC_CTX *ctx,
84
const struct sockaddr_storage *pss)
86
char addr[INET6_ADDRSTRLEN];
90
/* Linux getnameinfo() man pages says port is unitialized if
91
service name is NULL. */
93
ret = sys_getnameinfo((const struct sockaddr *)pss,
94
sizeof(struct sockaddr_storage),
102
if (pss->ss_family != AF_INET) {
103
#if defined(HAVE_IPV6)
104
dest = talloc_asprintf(ctx, "[%s]", addr);
109
dest = talloc_asprintf(ctx, "%s", addr);
115
/****************************************************************************
116
Return the string of an IP address (IPv4 or IPv6).
117
****************************************************************************/
119
static const char *get_socket_addr(int fd, char *addr_buf, size_t addr_len)
121
struct sockaddr_storage sa;
122
socklen_t length = sizeof(sa);
124
/* Ok, returning a hard coded IPv4 address
125
* is bogus, but it's just as bogus as a
126
* zero IPv6 address. No good choice here.
129
strlcpy(addr_buf, "0.0.0.0", addr_len);
135
if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
136
DEBUG(0,("getsockname failed. Error was %s\n",
141
return print_sockaddr_len(addr_buf, addr_len, (struct sockaddr *)&sa, length);
144
/****************************************************************************
145
Return the port number we've bound to on a socket.
146
****************************************************************************/
148
int get_socket_port(int fd)
150
struct sockaddr_storage sa;
151
socklen_t length = sizeof(sa);
157
if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
158
DEBUG(0,("getpeername failed. Error was %s\n",
163
#if defined(HAVE_IPV6)
164
if (sa.ss_family == AF_INET6) {
165
return ntohs(((struct sockaddr_in6 *)&sa)->sin6_port);
168
if (sa.ss_family == AF_INET) {
169
return ntohs(((struct sockaddr_in *)&sa)->sin_port);
23
#include "system/filesys.h"
25
#include "../lib/async_req/async_sock.h"
26
#include "../lib/util/select.h"
27
#include "interfaces.h"
28
#include "../lib/util/tevent_unix.h"
29
#include "../lib/util/tevent_ntstatus.h"
174
31
const char *client_name(int fd)
490
331
system performance will suffer severely as
491
332
select always returns true on disk files */
493
/* Set initial timeout */
494
timeout.tv_sec = (time_t)(time_out / 1000);
495
timeout.tv_usec = (long)(1000 * (time_out % 1000));
497
334
for (nread=0; nread < mincnt; ) {
498
if (fd < 0 || fd >= FD_SETSIZE) {
500
return map_nt_error_from_unix(EBADF);
506
selrtn = sys_select_intr(fd+1,&fds,NULL,NULL,&timeout);
337
pollrtn = poll_intr_one_fd(fd, POLLIN|POLLHUP, time_out,
508
340
/* Check if error */
511
/* something is wrong. Maybe the socket is dead? */
512
if (fd == get_client_fd()) {
513
/* Try and give an error message saying
514
* what client failed. */
515
DEBUG(0,("read_fd_with_timeout: timeout "
516
"read for client %s. select error = %s.\n",
517
get_peer_addr(fd,addr,sizeof(addr)),
518
strerror(save_errno) ));
520
DEBUG(0,("read_fd_with_timeout: timeout "
521
"read. select error = %s.\n",
522
strerror(save_errno) ));
524
return map_nt_error_from_unix(save_errno);
342
return map_nt_error_from_unix(errno);
527
345
/* Did we timeout ? */
346
if ((pollrtn == 0) ||
347
((revents & (POLLIN|POLLHUP|POLLERR)) == 0)) {
529
348
DEBUG(10,("read_fd_with_timeout: timeout read. "
530
349
"select timed out.\n"));
531
350
return NT_STATUS_IO_TIMEOUT;
660
465
ssize_t write_data(int fd, const char *buffer, size_t N)
663
467
struct iovec iov;
665
469
iov.iov_base = CONST_DISCARD(void *, buffer);
668
ret = write_data_iov(fd, &iov, 1);
673
if (fd == get_client_fd()) {
674
char addr[INET6_ADDRSTRLEN];
676
* Try and give an error message saying what client failed.
678
DEBUG(0, ("write_data: write failure in writing to client %s. "
679
"Error %s\n", get_peer_addr(fd,addr,sizeof(addr)),
682
DEBUG(0,("write_data: write failure. Error = %s\n",
471
return write_data_iov(fd, &iov, 1);
689
474
/****************************************************************************
736
521
/****************************************************************************
737
Read 4 bytes of a smb packet and return the smb length of the packet.
738
Store the result in the buffer. This version of the function will
739
never return a session keepalive (length of zero).
740
Timeout is in milliseconds.
741
****************************************************************************/
743
NTSTATUS read_smb_length(int fd, char *inbuf, unsigned int timeout,
746
uint8_t msgtype = SMBkeepalive;
748
while (msgtype == SMBkeepalive) {
751
status = read_smb_length_return_keepalive(fd, inbuf, timeout,
753
if (!NT_STATUS_IS_OK(status)) {
757
msgtype = CVAL(inbuf, 0);
760
DEBUG(10,("read_smb_length: got smb length of %lu\n",
761
(unsigned long)len));
766
/****************************************************************************
767
522
Read an smb from a fd.
768
523
The timeout is in milliseconds.
769
524
This function will return on receipt of a session keepalive packet.
875
633
#endif /* SO_REUSEPORT */
638
* As IPV6_V6ONLY is the default on some systems,
639
* we better try to be consistent and always use it.
641
* This also avoids using IPv4 via AF_INET6 sockets
642
* and makes sure %I never resolves to a '::ffff:192.168.0.1'
645
if (sock.ss_family == AF_INET6) {
649
ret = setsockopt(res, IPPROTO_IPV6, IPV6_V6ONLY,
650
(const void *)&val, sizeof(val));
653
dbgtext("open_socket_in(): IPV6_ONLY failed: ");
654
dbgtext("%s\n", strerror(errno));
878
662
/* now we've got a socket - we need to bind it */
879
663
if (bind(res, (struct sockaddr *)&sock, slen) == -1 ) {
880
664
if( DEBUGLVL(dlevel) && (port == SMB_PORT1 ||
1196
990
return NT_STATUS_OK;
1199
/*******************************************************************
1200
Create an outgoing TCP socket to the first addr that connects.
1202
This is for simultaneous connection attempts to port 445 and 139 of a host
1203
or for simultatneous connection attempts to multiple DCs at once. We return
1204
a socket fd of the first successful connection.
1206
@param[in] addrs list of Internet addresses and ports to connect to
1207
@param[in] num_addrs number of address/port pairs in the addrs list
1208
@param[in] timeout time after which we stop waiting for a socket connection
1209
to succeed, given in milliseconds
1210
@param[out] fd_index the entry in addrs which we successfully connected to
1211
@param[out] fd fd of the open and connected socket
1212
@return true on a successful connection, false if all connection attempts
1213
failed or we timed out
1214
*******************************************************************/
1216
bool open_any_socket_out(struct sockaddr_storage *addrs, int num_addrs,
1217
int timeout, int *fd_index, int *fd)
1219
int i, resulting_index, res;
1223
fd_set r_fds, wr_fds;
1227
int connect_loop = 10000; /* 10 milliseconds */
1229
timeout *= 1000; /* convert to microseconds */
1231
sockets = SMB_MALLOC_ARRAY(int, num_addrs);
1233
if (sockets == NULL)
1236
resulting_index = -1;
1238
for (i=0; i<num_addrs; i++)
1241
for (i=0; i<num_addrs; i++) {
1242
sockets[i] = socket(addrs[i].ss_family, SOCK_STREAM, 0);
1243
if (sockets[i] < 0 || sockets[i] >= FD_SETSIZE)
1245
set_blocking(sockets[i], false);
1249
good_connect = false;
1251
for (i=0; i<num_addrs; i++) {
1252
const struct sockaddr * a =
1253
(const struct sockaddr *)&(addrs[i]);
1255
if (sockets[i] == -1)
1258
if (sys_connect(sockets[i], a) == 0) {
1259
/* Rather unlikely as we are non-blocking, but it
1260
* might actually happen. */
1261
resulting_index = i;
1265
if (errno == EINPROGRESS || errno == EALREADY ||
1269
errno == EAGAIN || errno == EINTR) {
1270
/* These are the error messages that something is
1272
good_connect = true;
1273
} else if (errno != 0) {
1274
/* There was a direct error */
1280
if (!good_connect) {
1281
/* All of the connect's resulted in real error conditions */
1285
/* Lets see if any of the connect attempts succeeded */
1291
for (i=0; i<num_addrs; i++) {
1292
if (sockets[i] < 0 || sockets[i] >= FD_SETSIZE) {
1293
/* This cannot happen - ignore if so. */
1296
FD_SET(sockets[i], &wr_fds);
1297
FD_SET(sockets[i], &r_fds);
1298
if (sockets[i]>maxfd)
1303
tv.tv_usec = connect_loop;
1305
res = sys_select_intr(maxfd+1, &r_fds, &wr_fds, NULL, &tv);
1313
for (i=0; i<num_addrs; i++) {
1315
if (sockets[i] < 0 || sockets[i] >= FD_SETSIZE) {
1316
/* This cannot happen - ignore if so. */
1320
/* Stevens, Network Programming says that if there's a
1321
* successful connect, the socket is only writable. Upon an
1322
* error, it's both readable and writable. */
1324
if (FD_ISSET(sockets[i], &r_fds) &&
1325
FD_ISSET(sockets[i], &wr_fds)) {
1326
/* readable and writable, so it's an error */
1332
if (!FD_ISSET(sockets[i], &r_fds) &&
1333
FD_ISSET(sockets[i], &wr_fds)) {
1334
/* Only writable, so it's connected */
1335
resulting_index = i;
1342
timeout -= connect_loop;
1345
connect_loop *= 1.5;
1346
if (connect_loop > timeout)
1347
connect_loop = timeout;
1351
for (i=0; i<num_addrs; i++) {
1352
if (i == resulting_index)
1354
if (sockets[i] >= 0)
1358
if (resulting_index >= 0) {
1359
*fd_index = resulting_index;
1360
*fd = sockets[*fd_index];
1361
set_blocking(*fd, true);
1366
return (resulting_index >= 0);
1368
993
/****************************************************************************
1369
994
Open a connected UDP socket to host on port
1370
995
**************************************************************************/