r2184: use the smb.conf socket options for client code too
[jelmer/samba4-debian.git] / source / libcli / raw / clisocket.c
1 /* 
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>
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24
25 /*
26   create a smbcli_socket context
27 */
28 struct smbcli_socket *smbcli_sock_init(void)
29 {
30         struct smbcli_socket *sock;
31
32         sock = talloc_named(NULL, sizeof(*sock), "smbcli_socket");
33         if (!sock) {
34                 return NULL;
35         }
36
37         ZERO_STRUCTP(sock);
38         sock->fd = -1;
39         sock->port = 0;
40         /* 20 second default timeout */
41         sock->timeout = 20000;
42
43         sock->hostname = NULL;
44
45         return sock;
46 }
47
48 /*
49   connect a smbcli_socket context to an IP/port pair
50   if port is 0 then choose 445 then 139
51 */
52 BOOL smbcli_sock_connect(struct smbcli_socket *sock, struct in_addr *ip, int port)
53 {
54         if (getenv("LIBSMB_PROG")) {
55                 sock->fd = sock_exec(getenv("LIBSMB_PROG"));
56                 return sock->fd != -1;
57         }
58
59         if (port == 0) {
60                 int i;
61                 const char **ports = lp_smb_ports();
62                 for (i=0;ports[i];i++) {
63                         port = atoi(ports[i]);
64                         if (port != 0 && smbcli_sock_connect(sock, ip, port)) {
65                                 return True;
66                         }
67                 }
68                 return False;
69         }
70
71         sock->dest_ip = *ip;
72         sock->port = port;
73         sock->fd = open_socket_out(SOCK_STREAM,
74                                    &sock->dest_ip,
75                                    sock->port, 
76                                    LONG_CONNECT_TIMEOUT);
77         if (sock->fd == -1) {
78                 return False;
79         }
80
81         set_blocking(sock->fd, False);
82         set_socket_options(sock->fd, lp_socket_options());
83
84         return True;
85 }
86
87
88 /****************************************************************************
89  mark the socket as dead
90 ****************************************************************************/
91 void smbcli_sock_dead(struct smbcli_socket *sock)
92 {
93         if (sock->fd != -1) {
94                 close(sock->fd);
95                 sock->fd = -1;
96         }
97 }
98
99 /****************************************************************************
100  reduce socket reference count - if it becomes zero then close
101 ****************************************************************************/
102 void smbcli_sock_close(struct smbcli_socket *sock)
103 {
104         sock->reference_count--;
105         if (sock->reference_count <= 0) {
106                 smbcli_sock_dead(sock);
107         }
108 }
109
110 /****************************************************************************
111  Set socket options on a open connection.
112 ****************************************************************************/
113 void smbcli_sock_set_options(struct smbcli_socket *sock, const char *options)
114 {
115         set_socket_options(sock->fd, options);
116 }
117
118 /****************************************************************************
119  Write to socket. Return amount written.
120 ****************************************************************************/
121 ssize_t smbcli_sock_write(struct smbcli_socket *sock, const char *data, size_t len)
122 {
123         if (sock->fd == -1) {
124                 errno = EIO;
125                 return -1;
126         }
127
128         return write_data(sock->fd, data, len);
129 }
130
131
132 /****************************************************************************
133  Read from socket. return amount read
134 ****************************************************************************/
135 ssize_t smbcli_sock_read(struct smbcli_socket *sock, char *data, size_t len)
136 {
137         if (sock->fd == -1) {
138                 errno = EIO;
139                 return -1;
140         }
141
142         return read_data(sock->fd, data, len);
143 }
144
145 /****************************************************************************
146 resolve a hostname and connect 
147 ****************************************************************************/
148 BOOL smbcli_sock_connect_byname(struct smbcli_socket *sock, const char *host, int port)
149 {
150         int name_type = 0x20;
151         struct in_addr ip;
152         char *name, *p;
153         BOOL ret;
154
155         if (getenv("LIBSMB_PROG")) {
156                 sock->fd = sock_exec(getenv("LIBSMB_PROG"));
157                 return sock->fd != -1;
158         }
159
160         name = talloc_strdup(sock, host);
161
162         /* allow hostnames of the form NAME#xx and do a netbios lookup */
163         if ((p = strchr(name, '#'))) {
164                 name_type = strtol(p+1, NULL, 16);
165                 *p = 0;
166         }
167
168         if (!resolve_name(name, name, &ip, name_type)) {
169                 talloc_free(name);
170                 return False;
171         }
172
173         ret = smbcli_sock_connect(sock, &ip, port);
174
175         if (ret) {
176                 sock->hostname = talloc_steal(sock, name);
177         } else {
178                 talloc_free(name);
179         }
180
181         return ret;
182 }