fixed default port handling pointed out by Tom Jansen
[kai/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 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         return sock;
49 }
50
51 /*
52   connect a cli_socket context to an IP/port pair
53   if port is 0 then choose 445 then 139
54 */
55 BOOL cli_sock_connect(struct cli_socket *sock, struct in_addr *ip, int port)
56 {
57         if (getenv("LIBSMB_PROG")) {
58                 sock->fd = sock_exec(getenv("LIBSMB_PROG"));
59                 return sock->fd != -1;
60         }
61
62         if (port == 0) {
63                 return cli_sock_connect(sock, ip, 445) ||
64                         cli_sock_connect(sock, ip, 139);
65         }
66
67         sock->dest_ip = *ip;
68         sock->port = port;
69         sock->fd = open_socket_out(SOCK_STREAM,
70                                    &sock->dest_ip,
71                                    sock->port, 
72                                    LONG_CONNECT_TIMEOUT);
73         return (sock->fd != -1);
74 }
75
76
77 /****************************************************************************
78  reduce socket reference count - if it becomes zero then close
79 ****************************************************************************/
80 void cli_sock_close(struct cli_socket *sock)
81 {
82         sock->reference_count--;
83         if (sock->reference_count <= 0 && sock->fd != -1) {
84                 close(sock->fd);
85                 sock->fd = -1;
86         }
87 }
88
89 /****************************************************************************
90  Set socket options on a open connection.
91 ****************************************************************************/
92 void cli_sock_set_options(struct cli_socket *sock, const char *options)
93 {
94         set_socket_options(sock->fd, options);
95 }
96
97 /****************************************************************************
98  Write to socket. Return amount written.
99 ****************************************************************************/
100 ssize_t cli_sock_write(struct cli_socket *sock, const char *data, size_t len)
101 {
102         return write_data(sock->fd, data, len);
103 }
104
105
106 /****************************************************************************
107  Read from socket. return amount read
108 ****************************************************************************/
109 ssize_t cli_sock_read(struct cli_socket *sock, char *data, size_t len)
110 {
111         return read_data(sock->fd, data, len);
112 }
113
114 /****************************************************************************
115 resolve a hostname and connect 
116 ****************************************************************************/
117 BOOL cli_sock_connect_byname(struct cli_socket *sock, const char *host, int port)
118 {
119         int name_type = 0x20;
120         struct in_addr ip;
121         TALLOC_CTX *mem_ctx;
122         char *name, *p;
123
124         if (getenv("LIBSMB_PROG")) {
125                 sock->fd = sock_exec(getenv("LIBSMB_PROG"));
126                 return sock->fd != -1;
127         }
128
129         mem_ctx = talloc_init("cli_sock_connect_byname");
130         if (!mem_ctx) return False;
131
132         name = talloc_strdup(mem_ctx, host);
133
134         /* allow hostnames of the form NAME#xx and do a netbios lookup */
135         if ((p = strchr(name, '#'))) {
136                 name_type = strtol(p+1, NULL, 16);
137                 *p = 0;
138         }
139
140         if (!resolve_name(mem_ctx, name, &ip, name_type)) {
141                 talloc_destroy(mem_ctx);
142                 return False;
143         }
144
145         talloc_destroy(mem_ctx);
146
147         return cli_sock_connect(sock, &ip, port);
148 }