2 Unix SMB/CIFS implementation.
3 SMB client socket context management functions
4 Copyright (C) Andrew Tridgell 1994-2003
5 Copyright (C) James Myers 2003 <myersjj@samba.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 create a smbcli_socket context
28 struct smbcli_socket *smbcli_sock_init(void)
30 struct smbcli_socket *sock;
33 mem_ctx = talloc_init("smbcli_socket");
34 if (!mem_ctx) return NULL;
36 sock = talloc_zero(mem_ctx, sizeof(*sock));
38 talloc_destroy(mem_ctx);
42 sock->mem_ctx = mem_ctx;
45 /* 20 second default timeout */
46 sock->timeout = 20000;
48 sock->hostname = NULL;
54 connect a smbcli_socket context to an IP/port pair
55 if port is 0 then choose 445 then 139
57 BOOL smbcli_sock_connect(struct smbcli_socket *sock, struct in_addr *ip, int port)
59 if (getenv("LIBSMB_PROG")) {
60 sock->fd = sock_exec(getenv("LIBSMB_PROG"));
61 return sock->fd != -1;
65 return smbcli_sock_connect(sock, ip, 445) ||
66 smbcli_sock_connect(sock, ip, 139);
71 sock->fd = open_socket_out(SOCK_STREAM,
74 LONG_CONNECT_TIMEOUT);
79 set_blocking(sock->fd, False);
85 /****************************************************************************
86 mark the socket as dead
87 ****************************************************************************/
88 void smbcli_sock_dead(struct smbcli_socket *sock)
96 /****************************************************************************
97 reduce socket reference count - if it becomes zero then close
98 ****************************************************************************/
99 void smbcli_sock_close(struct smbcli_socket *sock)
101 sock->reference_count--;
102 if (sock->reference_count <= 0) {
103 smbcli_sock_dead(sock);
107 /****************************************************************************
108 Set socket options on a open connection.
109 ****************************************************************************/
110 void smbcli_sock_set_options(struct smbcli_socket *sock, const char *options)
112 set_socket_options(sock->fd, options);
115 /****************************************************************************
116 Write to socket. Return amount written.
117 ****************************************************************************/
118 ssize_t smbcli_sock_write(struct smbcli_socket *sock, const char *data, size_t len)
120 if (sock->fd == -1) {
125 return write_data(sock->fd, data, len);
129 /****************************************************************************
130 Read from socket. return amount read
131 ****************************************************************************/
132 ssize_t smbcli_sock_read(struct smbcli_socket *sock, char *data, size_t len)
134 if (sock->fd == -1) {
139 return read_data(sock->fd, data, len);
142 /****************************************************************************
143 resolve a hostname and connect
144 ****************************************************************************/
145 BOOL smbcli_sock_connect_byname(struct smbcli_socket *sock, const char *host, int port)
147 int name_type = 0x20;
153 if (getenv("LIBSMB_PROG")) {
154 sock->fd = sock_exec(getenv("LIBSMB_PROG"));
155 return sock->fd != -1;
158 mem_ctx = talloc_init("smbcli_sock_connect_byname");
159 if (!mem_ctx) return False;
161 name = talloc_strdup(mem_ctx, host);
163 /* allow hostnames of the form NAME#xx and do a netbios lookup */
164 if ((p = strchr(name, '#'))) {
165 name_type = strtol(p+1, NULL, 16);
169 if (!resolve_name(mem_ctx, name, &ip, name_type)) {
170 talloc_destroy(mem_ctx);
174 ret = smbcli_sock_connect(sock, &ip, port);
177 sock->hostname = talloc_steal(mem_ctx, sock->mem_ctx, name);
180 talloc_destroy(mem_ctx);