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;
66 const char **ports = lp_smb_ports();
67 for (i=0;ports[i];i++) {
68 port = atoi(ports[i]);
69 if (port != 0 && smbcli_sock_connect(sock, ip, port)) {
78 sock->fd = open_socket_out(SOCK_STREAM,
81 LONG_CONNECT_TIMEOUT);
86 set_blocking(sock->fd, False);
92 /****************************************************************************
93 mark the socket as dead
94 ****************************************************************************/
95 void smbcli_sock_dead(struct smbcli_socket *sock)
103 /****************************************************************************
104 reduce socket reference count - if it becomes zero then close
105 ****************************************************************************/
106 void smbcli_sock_close(struct smbcli_socket *sock)
108 sock->reference_count--;
109 if (sock->reference_count <= 0) {
110 smbcli_sock_dead(sock);
114 /****************************************************************************
115 Set socket options on a open connection.
116 ****************************************************************************/
117 void smbcli_sock_set_options(struct smbcli_socket *sock, const char *options)
119 set_socket_options(sock->fd, options);
122 /****************************************************************************
123 Write to socket. Return amount written.
124 ****************************************************************************/
125 ssize_t smbcli_sock_write(struct smbcli_socket *sock, const char *data, size_t len)
127 if (sock->fd == -1) {
132 return write_data(sock->fd, data, len);
136 /****************************************************************************
137 Read from socket. return amount read
138 ****************************************************************************/
139 ssize_t smbcli_sock_read(struct smbcli_socket *sock, char *data, size_t len)
141 if (sock->fd == -1) {
146 return read_data(sock->fd, data, len);
149 /****************************************************************************
150 resolve a hostname and connect
151 ****************************************************************************/
152 BOOL smbcli_sock_connect_byname(struct smbcli_socket *sock, const char *host, int port)
154 int name_type = 0x20;
160 if (getenv("LIBSMB_PROG")) {
161 sock->fd = sock_exec(getenv("LIBSMB_PROG"));
162 return sock->fd != -1;
165 mem_ctx = talloc_init("smbcli_sock_connect_byname");
166 if (!mem_ctx) return False;
168 name = talloc_strdup(mem_ctx, host);
170 /* allow hostnames of the form NAME#xx and do a netbios lookup */
171 if ((p = strchr(name, '#'))) {
172 name_type = strtol(p+1, NULL, 16);
176 if (!resolve_name(mem_ctx, name, &ip, name_type)) {
177 talloc_destroy(mem_ctx);
181 ret = smbcli_sock_connect(sock, &ip, port);
184 sock->hostname = talloc_steal(sock->mem_ctx, name);
187 talloc_destroy(mem_ctx);