the first stage of the async client rewrite.
[samba-svnmirror.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 cli_socket context
27 */
28 struct cli_socket *cli_sock_init(void)
29 {
30         struct cli_socket *sock;
31         TALLOC_CTX *mem_ctx;
32
33         mem_ctx = talloc_init("cli_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 cli_socket context to an IP/port pair
55   if port is 0 then choose 445 then 139
56 */
57 BOOL cli_sock_connect(struct cli_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                 return cli_sock_connect(sock, ip, 445) ||
66                         cli_sock_connect(sock, ip, 139);
67         }
68
69         sock->dest_ip = *ip;
70         sock->port = port;
71         sock->fd = open_socket_out(SOCK_STREAM,
72                                    &sock->dest_ip,
73                                    sock->port, 
74                                    LONG_CONNECT_TIMEOUT);
75         if (sock->fd == -1) {
76                 return False;
77         }
78
79         set_blocking(sock->fd, False);
80
81         return True;
82 }
83
84
85 /****************************************************************************
86  mark the socket as dead
87 ****************************************************************************/
88 void cli_sock_dead(struct cli_socket *sock)
89 {
90         if (sock->fd != -1) {
91                 close(sock->fd);
92                 sock->fd = -1;
93         }
94 }
95
96 /****************************************************************************
97  reduce socket reference count - if it becomes zero then close
98 ****************************************************************************/
99 void cli_sock_close(struct cli_socket *sock)
100 {
101         sock->reference_count--;
102         if (sock->reference_count <= 0) {
103                 cli_sock_dead(sock);
104         }
105 }
106
107 /****************************************************************************
108  Set socket options on a open connection.
109 ****************************************************************************/
110 void cli_sock_set_options(struct cli_socket *sock, const char *options)
111 {
112         set_socket_options(sock->fd, options);
113 }
114
115 /****************************************************************************
116  Write to socket. Return amount written.
117 ****************************************************************************/
118 ssize_t cli_sock_write(struct cli_socket *sock, const char *data, size_t len)
119 {
120         if (sock->fd == -1) {
121                 errno = EIO;
122                 return -1;
123         }
124
125         return write_data(sock->fd, data, len);
126 }
127
128
129 /****************************************************************************
130  Read from socket. return amount read
131 ****************************************************************************/
132 ssize_t cli_sock_read(struct cli_socket *sock, char *data, size_t len)
133 {
134         if (sock->fd == -1) {
135                 errno = EIO;
136                 return -1;
137         }
138
139         return read_data(sock->fd, data, len);
140 }
141
142 /****************************************************************************
143 resolve a hostname and connect 
144 ****************************************************************************/
145 BOOL cli_sock_connect_byname(struct cli_socket *sock, const char *host, int port)
146 {
147         int name_type = 0x20;
148         struct in_addr ip;
149         TALLOC_CTX *mem_ctx;
150         char *name, *p;
151         BOOL ret;
152
153         if (getenv("LIBSMB_PROG")) {
154                 sock->fd = sock_exec(getenv("LIBSMB_PROG"));
155                 return sock->fd != -1;
156         }
157
158         mem_ctx = talloc_init("cli_sock_connect_byname");
159         if (!mem_ctx) return False;
160
161         name = talloc_strdup(mem_ctx, host);
162
163         /* allow hostnames of the form NAME#xx and do a netbios lookup */
164         if ((p = strchr(name, '#'))) {
165                 name_type = strtol(p+1, NULL, 16);
166                 *p = 0;
167         }
168
169         if (!resolve_name(mem_ctx, name, &ip, name_type)) {
170                 talloc_destroy(mem_ctx);
171                 return False;
172         }
173
174         ret = cli_sock_connect(sock, &ip, port);
175
176         if (ret) {
177                 sock->hostname = talloc_steal(mem_ctx, sock->mem_ctx, name);
178         }
179
180         talloc_destroy(mem_ctx);
181
182         return ret;
183 }