r1983: a completely new implementation of talloc
[bbaumbach/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         TALLOC_CTX *mem_ctx;
32
33         mem_ctx = talloc_init("smbcli_socket");
34         if (!mem_ctx) return NULL;
35
36         sock = talloc_zero(mem_ctx, sizeof(*sock));
37         if (!sock) {
38                 talloc_destroy(mem_ctx);
39                 return NULL;
40         }
41
42         sock->mem_ctx = mem_ctx;
43         sock->fd = -1;
44         sock->port = 0;
45         /* 20 second default timeout */
46         sock->timeout = 20000;
47
48         sock->hostname = NULL;
49
50         return sock;
51 }
52
53 /*
54   connect a smbcli_socket context to an IP/port pair
55   if port is 0 then choose 445 then 139
56 */
57 BOOL smbcli_sock_connect(struct smbcli_socket *sock, struct in_addr *ip, int port)
58 {
59         if (getenv("LIBSMB_PROG")) {
60                 sock->fd = sock_exec(getenv("LIBSMB_PROG"));
61                 return sock->fd != -1;
62         }
63
64         if (port == 0) {
65                 int i;
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)) {
70                                 return True;
71                         }
72                 }
73                 return False;
74         }
75
76         sock->dest_ip = *ip;
77         sock->port = port;
78         sock->fd = open_socket_out(SOCK_STREAM,
79                                    &sock->dest_ip,
80                                    sock->port, 
81                                    LONG_CONNECT_TIMEOUT);
82         if (sock->fd == -1) {
83                 return False;
84         }
85
86         set_blocking(sock->fd, False);
87
88         return True;
89 }
90
91
92 /****************************************************************************
93  mark the socket as dead
94 ****************************************************************************/
95 void smbcli_sock_dead(struct smbcli_socket *sock)
96 {
97         if (sock->fd != -1) {
98                 close(sock->fd);
99                 sock->fd = -1;
100         }
101 }
102
103 /****************************************************************************
104  reduce socket reference count - if it becomes zero then close
105 ****************************************************************************/
106 void smbcli_sock_close(struct smbcli_socket *sock)
107 {
108         sock->reference_count--;
109         if (sock->reference_count <= 0) {
110                 smbcli_sock_dead(sock);
111         }
112 }
113
114 /****************************************************************************
115  Set socket options on a open connection.
116 ****************************************************************************/
117 void smbcli_sock_set_options(struct smbcli_socket *sock, const char *options)
118 {
119         set_socket_options(sock->fd, options);
120 }
121
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)
126 {
127         if (sock->fd == -1) {
128                 errno = EIO;
129                 return -1;
130         }
131
132         return write_data(sock->fd, data, len);
133 }
134
135
136 /****************************************************************************
137  Read from socket. return amount read
138 ****************************************************************************/
139 ssize_t smbcli_sock_read(struct smbcli_socket *sock, char *data, size_t len)
140 {
141         if (sock->fd == -1) {
142                 errno = EIO;
143                 return -1;
144         }
145
146         return read_data(sock->fd, data, len);
147 }
148
149 /****************************************************************************
150 resolve a hostname and connect 
151 ****************************************************************************/
152 BOOL smbcli_sock_connect_byname(struct smbcli_socket *sock, const char *host, int port)
153 {
154         int name_type = 0x20;
155         struct in_addr ip;
156         TALLOC_CTX *mem_ctx;
157         char *name, *p;
158         BOOL ret;
159
160         if (getenv("LIBSMB_PROG")) {
161                 sock->fd = sock_exec(getenv("LIBSMB_PROG"));
162                 return sock->fd != -1;
163         }
164
165         mem_ctx = talloc_init("smbcli_sock_connect_byname");
166         if (!mem_ctx) return False;
167
168         name = talloc_strdup(mem_ctx, host);
169
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);
173                 *p = 0;
174         }
175
176         if (!resolve_name(mem_ctx, name, &ip, name_type)) {
177                 talloc_destroy(mem_ctx);
178                 return False;
179         }
180
181         ret = smbcli_sock_connect(sock, &ip, port);
182
183         if (ret) {
184                 sock->hostname = talloc_steal(sock->mem_ctx, name);
185         }
186
187         talloc_destroy(mem_ctx);
188
189         return ret;
190 }