summaryrefslogtreecommitdiff
path: root/source4/libcli/raw/clisocket.c
blob: 14862a39f068c29138cd63cb7f4e758210ec48be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* 
   Unix SMB/CIFS implementation.
   SMB client socket context management functions
   Copyright (C) Andrew Tridgell 1994-2003
   Copyright (C) James Myers 2003 <myersjj@samba.org>
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

/*
  destroy a socket
 */
static int sock_destructor(void *ptr)
{
	struct smbcli_socket *sock = ptr;
	if (sock->fd != -1) {
		close(sock->fd);
		sock->fd = -1;
	}
	return 0;
}

/*
  create a smbcli_socket context
*/
struct smbcli_socket *smbcli_sock_init(TALLOC_CTX *mem_ctx)
{
	struct smbcli_socket *sock;

	sock = talloc_p(mem_ctx, struct smbcli_socket);
	if (!sock) {
		return NULL;
	}

	ZERO_STRUCTP(sock);
	sock->fd = -1;
	sock->port = 0;

	/* 20 second default timeout */
	sock->timeout = 20000;
	sock->hostname = NULL;

	talloc_set_destructor(sock, sock_destructor);

	return sock;
}

/*
  connect a smbcli_socket context to an IP/port pair
  if port is 0 then choose 445 then 139
*/
BOOL smbcli_sock_connect(struct smbcli_socket *sock, struct in_addr *ip, int port)
{
	if (getenv("LIBSMB_PROG")) {
		sock->fd = sock_exec(getenv("LIBSMB_PROG"));
		return sock->fd != -1;
	}

	if (port == 0) {
		int i;
		const char **ports = lp_smb_ports();
		for (i=0;ports[i];i++) {
			port = atoi(ports[i]);
			if (port != 0 && smbcli_sock_connect(sock, ip, port)) {
				return True;
			}
		}
		return False;
	}

	sock->dest_ip = *ip;
	sock->port = port;
	sock->fd = open_socket_out(SOCK_STREAM,
				   &sock->dest_ip,
				   sock->port, 
				   LONG_CONNECT_TIMEOUT);
	if (sock->fd == -1) {
		return False;
	}

	set_blocking(sock->fd, False);
	set_socket_options(sock->fd, lp_socket_options());

	return True;
}


/****************************************************************************
 mark the socket as dead
****************************************************************************/
void smbcli_sock_dead(struct smbcli_socket *sock)
{
	if (sock->fd != -1) {
		close(sock->fd);
		sock->fd = -1;
	}
}

/****************************************************************************
 Set socket options on a open connection.
****************************************************************************/
void smbcli_sock_set_options(struct smbcli_socket *sock, const char *options)
{
	set_socket_options(sock->fd, options);
}

/****************************************************************************
 Write to socket. Return amount written.
****************************************************************************/
ssize_t smbcli_sock_write(struct smbcli_socket *sock, const char *data, size_t len)
{
	if (sock->fd == -1) {
		errno = EIO;
		return -1;
	}

	return write(sock->fd, data, len);
}


/****************************************************************************
 Read from socket. return amount read
****************************************************************************/
ssize_t smbcli_sock_read(struct smbcli_socket *sock, char *data, size_t len)
{
	if (sock->fd == -1) {
		errno = EIO;
		return -1;
	}

	return read(sock->fd, data, len);
}

/****************************************************************************
resolve a hostname and connect 
****************************************************************************/
BOOL smbcli_sock_connect_byname(struct smbcli_socket *sock, const char *host, int port)
{
	int name_type = 0x20;
	struct in_addr ip;
	char *name, *p;
	BOOL ret;

	if (getenv("LIBSMB_PROG")) {
		sock->fd = sock_exec(getenv("LIBSMB_PROG"));
		return sock->fd != -1;
	}

	name = talloc_strdup(sock, host);

	/* allow hostnames of the form NAME#xx and do a netbios lookup */
	if ((p = strchr(name, '#'))) {
		name_type = strtol(p+1, NULL, 16);
		*p = 0;
	}

	if (!resolve_name(name, name, &ip, name_type)) {
		talloc_free(name);
		return False;
	}

	ret = smbcli_sock_connect(sock, &ip, port);

	if (ret) {
		sock->hostname = talloc_steal(sock, name);
	} else {
		talloc_free(name);
	}

	return ret;
}