r1985: take advantage of the new talloc in a few more places
[nivanova/samba-autobuild/.git] / source4 / 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
83         return True;
84 }
85
86
87 /****************************************************************************
88  mark the socket as dead
89 ****************************************************************************/
90 void smbcli_sock_dead(struct smbcli_socket *sock)
91 {
92         if (sock->fd != -1) {
93                 close(sock->fd);
94                 sock->fd = -1;
95         }
96 }
97
98 /****************************************************************************
99  reduce socket reference count - if it becomes zero then close
100 ****************************************************************************/
101 void smbcli_sock_close(struct smbcli_socket *sock)
102 {
103         sock->reference_count--;
104         if (sock->reference_count <= 0) {
105                 smbcli_sock_dead(sock);
106         }
107 }
108
109 /****************************************************************************
110  Set socket options on a open connection.
111 ****************************************************************************/
112 void smbcli_sock_set_options(struct smbcli_socket *sock, const char *options)
113 {
114         set_socket_options(sock->fd, options);
115 }
116
117 /****************************************************************************
118  Write to socket. Return amount written.
119 ****************************************************************************/
120 ssize_t smbcli_sock_write(struct smbcli_socket *sock, const char *data, size_t len)
121 {
122         if (sock->fd == -1) {
123                 errno = EIO;
124                 return -1;
125         }
126
127         return write_data(sock->fd, data, len);
128 }
129
130
131 /****************************************************************************
132  Read from socket. return amount read
133 ****************************************************************************/
134 ssize_t smbcli_sock_read(struct smbcli_socket *sock, char *data, size_t len)
135 {
136         if (sock->fd == -1) {
137                 errno = EIO;
138                 return -1;
139         }
140
141         return read_data(sock->fd, data, len);
142 }
143
144 /****************************************************************************
145 resolve a hostname and connect 
146 ****************************************************************************/
147 BOOL smbcli_sock_connect_byname(struct smbcli_socket *sock, const char *host, int port)
148 {
149         int name_type = 0x20;
150         struct in_addr ip;
151         char *name, *p;
152         BOOL ret;
153
154         if (getenv("LIBSMB_PROG")) {
155                 sock->fd = sock_exec(getenv("LIBSMB_PROG"));
156                 return sock->fd != -1;
157         }
158
159         name = talloc_strdup(sock, host);
160
161         /* allow hostnames of the form NAME#xx and do a netbios lookup */
162         if ((p = strchr(name, '#'))) {
163                 name_type = strtol(p+1, NULL, 16);
164                 *p = 0;
165         }
166
167         if (!resolve_name(name, name, &ip, name_type)) {
168                 talloc_free(name);
169                 return False;
170         }
171
172         ret = smbcli_sock_connect(sock, &ip, port);
173
174         if (ret) {
175                 sock->hostname = talloc_steal(sock, name);
176         }
177
178         talloc_destroy(name);
179
180         return ret;
181 }