r4063: - change char * -> uint8_t in struct request_buffer
[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 #include "libcli/raw/libcliraw.h"
24
25 /*
26   create a smbcli_socket context
27 */
28 struct smbcli_socket *smbcli_sock_init(TALLOC_CTX *mem_ctx)
29 {
30         struct smbcli_socket *sock;
31
32         sock = talloc_p(mem_ctx, struct smbcli_socket);
33         if (!sock) {
34                 return NULL;
35         }
36
37         ZERO_STRUCTP(sock);
38         sock->sock = NULL;
39         sock->port = 0;
40
41         /* 20 second default timeout */
42         sock->timeout = 20000;
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 ipv4_addr *ip, int port)
53 {
54         NTSTATUS status;
55
56         if (port == 0) {
57                 int i;
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)) {
62                                 return True;
63                         }
64                 }
65                 return False;
66         }
67
68         status = socket_create("ip", SOCKET_TYPE_STREAM, &sock->sock, 0);
69         if (!NT_STATUS_IS_OK(status)) {
70                 return False;
71         }
72         talloc_steal(sock, sock->sock);
73
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);
77                 sock->sock = NULL;
78                 return False;
79         }
80
81         sock->dest_ip = *ip;
82         sock->port = port;
83
84         socket_set_option(sock->sock, lp_socket_options(), NULL);
85
86         return True;
87 }
88
89
90 /****************************************************************************
91  mark the socket as dead
92 ****************************************************************************/
93 void smbcli_sock_dead(struct smbcli_socket *sock)
94 {
95         if (sock->sock != NULL) {
96                 talloc_free(sock->sock);
97                 sock->sock = NULL;
98         }
99 }
100
101 /****************************************************************************
102  Set socket options on a open connection.
103 ****************************************************************************/
104 void smbcli_sock_set_options(struct smbcli_socket *sock, const char *options)
105 {
106         socket_set_option(sock->sock, options, NULL);
107 }
108
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)
113 {
114         NTSTATUS status;
115         DATA_BLOB blob;
116         size_t nsent;
117
118         if (sock->sock == NULL) {
119                 errno = EIO;
120                 return -1;
121         }
122
123         blob.data = discard_const(data);
124         blob.length = len;
125
126         status = socket_send(sock->sock, &blob, &nsent, 0);
127         if (NT_STATUS_IS_ERR(status)) {
128                 return -1;
129         }
130
131         return nsent;
132 }
133
134
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)
139 {
140         NTSTATUS status;
141         size_t nread;
142
143         if (sock->sock == NULL) {
144                 errno = EIO;
145                 return -1;
146         }
147
148         status = socket_recv(sock->sock, data, len, &nread, 0);
149         if (NT_STATUS_IS_ERR(status)) {
150                 return -1;
151         }
152
153         return nread;
154 }
155
156 /****************************************************************************
157 resolve a hostname and connect 
158 ****************************************************************************/
159 BOOL smbcli_sock_connect_byname(struct smbcli_socket *sock, const char *host, int port)
160 {
161         int name_type = 0x20;
162         struct ipv4_addr ip;
163         char *name, *p;
164         BOOL ret;
165
166 #if 0
167         if (getenv("LIBSMB_PROG")) {
168                 sock->fd = sock_exec(getenv("LIBSMB_PROG"));
169                 return sock->fd != -1;
170         }
171 #endif
172
173         name = talloc_strdup(sock, host);
174
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);
178                 *p = 0;
179         }
180
181         if (!resolve_name(name, name, &ip, name_type)) {
182                 talloc_free(name);
183                 return False;
184         }
185
186         ret = smbcli_sock_connect(sock, &ip, port);
187
188         if (ret) {
189                 sock->hostname = talloc_steal(sock, name);
190         } else {
191                 talloc_free(name);
192         }
193
194         return ret;
195 }