2 Unix SMB/CIFS implementation.
3 SMB client socket context management functions
4 Copyright (C) Andrew Tridgell 1994-1998
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 cli_socket context
28 struct cli_socket *cli_sock_init(void)
30 struct cli_socket *sock;
33 mem_ctx = talloc_init("cli_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;
52 connect a cli_socket context to an IP/port pair
53 if port is 0 then choose 445 then 139
55 BOOL cli_sock_connect(struct cli_socket *sock, struct in_addr *ip, int port)
57 if (getenv("LIBSMB_PROG")) {
58 sock->fd = sock_exec(getenv("LIBSMB_PROG"));
59 return sock->fd != -1;
63 return cli_sock_connect(sock, ip, 445) ||
64 cli_sock_connect(sock, ip, 139);
69 sock->fd = open_socket_out(SOCK_STREAM,
72 LONG_CONNECT_TIMEOUT);
73 return (sock->fd != -1);
77 /****************************************************************************
78 reduce socket reference count - if it becomes zero then close
79 ****************************************************************************/
80 void cli_sock_close(struct cli_socket *sock)
82 sock->reference_count--;
83 if (sock->reference_count <= 0 && sock->fd != -1) {
89 /****************************************************************************
90 Set socket options on a open connection.
91 ****************************************************************************/
92 void cli_sock_set_options(struct cli_socket *sock, const char *options)
94 set_socket_options(sock->fd, options);
97 /****************************************************************************
98 Write to socket. Return amount written.
99 ****************************************************************************/
100 ssize_t cli_sock_write(struct cli_socket *sock, const char *data, size_t len)
102 return write_data(sock->fd, data, len);
106 /****************************************************************************
107 Read from socket. return amount read
108 ****************************************************************************/
109 ssize_t cli_sock_read(struct cli_socket *sock, char *data, size_t len)
111 return read_data(sock->fd, data, len);
114 /****************************************************************************
115 resolve a hostname and connect
116 ****************************************************************************/
117 BOOL cli_sock_connect_byname(struct cli_socket *sock, const char *host, int port)
119 int name_type = 0x20;
124 if (getenv("LIBSMB_PROG")) {
125 sock->fd = sock_exec(getenv("LIBSMB_PROG"));
126 return sock->fd != -1;
129 mem_ctx = talloc_init("cli_sock_connect_byname");
130 if (!mem_ctx) return False;
132 name = talloc_strdup(mem_ctx, host);
134 /* allow hostnames of the form NAME#xx and do a netbios lookup */
135 if ((p = strchr(name, '#'))) {
136 name_type = strtol(p+1, NULL, 16);
140 if (!resolve_name(mem_ctx, name, &ip, name_type)) {
141 talloc_destroy(mem_ctx);
145 talloc_destroy(mem_ctx);
147 return cli_sock_connect(sock, &ip, port);