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.
23 #include "libcli/raw/libcliraw.h"
26 create a smbcli_socket context
28 struct smbcli_socket *smbcli_sock_init(TALLOC_CTX *mem_ctx)
30 struct smbcli_socket *sock;
32 sock = talloc_p(mem_ctx, struct smbcli_socket);
41 /* 20 second default timeout */
42 sock->timeout = 20000;
43 sock->hostname = NULL;
49 connect a smbcli_socket context to an IP/port pair
50 if port is 0 then choose 445 then 139
52 BOOL smbcli_sock_connect(struct smbcli_socket *sock, struct ipv4_addr *ip, int port)
58 const char **ports = lp_smb_ports();
59 for (i=0;ports[i];i++) {
60 port = atoi(ports[i]);
61 if (port != 0 && smbcli_sock_connect(sock, ip, port)) {
68 status = socket_create("ip", SOCKET_TYPE_STREAM, &sock->sock, 0);
69 if (!NT_STATUS_IS_OK(status)) {
72 talloc_steal(sock, sock->sock);
74 status = socket_connect(sock->sock, NULL, 0, sys_inet_ntoa(*ip), port, 0);
75 if (!NT_STATUS_IS_OK(status)) {
76 talloc_free(sock->sock);
84 socket_set_option(sock->sock, lp_socket_options(), NULL);
90 /****************************************************************************
91 mark the socket as dead
92 ****************************************************************************/
93 void smbcli_sock_dead(struct smbcli_socket *sock)
95 if (sock->sock != NULL) {
96 talloc_free(sock->sock);
101 /****************************************************************************
102 Set socket options on a open connection.
103 ****************************************************************************/
104 void smbcli_sock_set_options(struct smbcli_socket *sock, const char *options)
106 socket_set_option(sock->sock, options, NULL);
109 /****************************************************************************
110 Write to socket. Return amount written.
111 ****************************************************************************/
112 ssize_t smbcli_sock_write(struct smbcli_socket *sock, const uint8_t *data, size_t len)
118 if (sock->sock == NULL) {
123 blob.data = discard_const(data);
126 status = socket_send(sock->sock, &blob, &nsent, 0);
127 if (NT_STATUS_IS_ERR(status)) {
135 /****************************************************************************
136 Read from socket. return amount read
137 ****************************************************************************/
138 ssize_t smbcli_sock_read(struct smbcli_socket *sock, uint8_t *data, size_t len)
143 if (sock->sock == NULL) {
148 status = socket_recv(sock->sock, data, len, &nread, 0);
149 if (NT_STATUS_IS_ERR(status)) {
156 /****************************************************************************
157 resolve a hostname and connect
158 ****************************************************************************/
159 BOOL smbcli_sock_connect_byname(struct smbcli_socket *sock, const char *host, int port)
161 int name_type = 0x20;
167 if (getenv("LIBSMB_PROG")) {
168 sock->fd = sock_exec(getenv("LIBSMB_PROG"));
169 return sock->fd != -1;
173 name = talloc_strdup(sock, host);
175 /* allow hostnames of the form NAME#xx and do a netbios lookup */
176 if ((p = strchr(name, '#'))) {
177 name_type = strtol(p+1, NULL, 16);
181 if (!resolve_name(name, name, &ip, name_type)) {
186 ret = smbcli_sock_connect(sock, &ip, port);
189 sock->hostname = talloc_steal(sock, name);