summaryrefslogtreecommitdiff
path: root/source4/libcli/raw/clisocket.c
blob: eb5d3c034259e3f2b517b38f8ed8589ff92b870c (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
/* 
   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"


/*
  create a cli_socket context
*/
struct cli_socket *cli_sock_init(void)
{
	struct cli_socket *sock;
	TALLOC_CTX *mem_ctx;

	mem_ctx = talloc_init("cli_socket");
	if (!mem_ctx) return NULL;

	sock = talloc_zero(mem_ctx, sizeof(*sock));
	if (!sock) {
		talloc_destroy(mem_ctx);
		return NULL;
	}

	sock->mem_ctx = mem_ctx;
	sock->fd = -1;
	sock->port = 0;
	/* 20 second default timeout */
	sock->timeout = 20000;

	sock->hostname = NULL;

	return sock;
}

/*
  connect a cli_socket context to an IP/port pair
  if port is 0 then choose 445 then 139
*/
BOOL cli_sock_connect(struct cli_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) {
		return cli_sock_connect(sock, ip, 445) ||
			cli_sock_connect(sock, ip, 139);
	}

	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);

	return True;
}


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

/****************************************************************************
 reduce socket reference count - if it becomes zero then close
****************************************************************************/
void cli_sock_close(struct cli_socket *sock)
{
	sock->reference_count--;
	if (sock->reference_count <= 0) {
		cli_sock_dead(sock);
	}
}

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

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

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


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

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

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

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

	mem_ctx = talloc_init("cli_sock_connect_byname");
	if (!mem_ctx) return False;

	name = talloc_strdup(mem_ctx, 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(mem_ctx, name, &ip, name_type)) {
		talloc_destroy(mem_ctx);
		return False;
	}

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

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

	talloc_destroy(mem_ctx);

	return ret;
}