| 1 | /* | 
|---|
| 2 | Unix SMB/CIFS implementation. | 
|---|
| 3 |  | 
|---|
| 4 | a raw async NBT library | 
|---|
| 5 |  | 
|---|
| 6 | Copyright (C) Andrew Tridgell 2005 | 
|---|
| 7 |  | 
|---|
| 8 | This program is free software; you can redistribute it and/or modify | 
|---|
| 9 | it under the terms of the GNU General Public License as published by | 
|---|
| 10 | the Free Software Foundation; either version 3 of the License, or | 
|---|
| 11 | (at your option) any later version. | 
|---|
| 12 |  | 
|---|
| 13 | This program is distributed in the hope that it will be useful, | 
|---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 | GNU General Public License for more details. | 
|---|
| 17 |  | 
|---|
| 18 | You should have received a copy of the GNU General Public License | 
|---|
| 19 | along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 20 | */ | 
|---|
| 21 |  | 
|---|
| 22 | #ifndef __LIBNBT_H__ | 
|---|
| 23 | #define __LIBNBT_H__ | 
|---|
| 24 |  | 
|---|
| 25 | #include "librpc/gen_ndr/nbt.h" | 
|---|
| 26 | #include "librpc/ndr/libndr.h" | 
|---|
| 27 | #include "system/network.h" | 
|---|
| 28 | /* | 
|---|
| 29 | possible states for pending requests | 
|---|
| 30 | */ | 
|---|
| 31 | enum nbt_request_state {NBT_REQUEST_SEND, | 
|---|
| 32 | NBT_REQUEST_WAIT, | 
|---|
| 33 | NBT_REQUEST_DONE, | 
|---|
| 34 | NBT_REQUEST_TIMEOUT, | 
|---|
| 35 | NBT_REQUEST_ERROR}; | 
|---|
| 36 |  | 
|---|
| 37 | /* | 
|---|
| 38 | a nbt name request | 
|---|
| 39 | */ | 
|---|
| 40 | struct nbt_name_request { | 
|---|
| 41 | struct nbt_name_request *next, *prev; | 
|---|
| 42 |  | 
|---|
| 43 | enum nbt_request_state state; | 
|---|
| 44 |  | 
|---|
| 45 | NTSTATUS status; | 
|---|
| 46 |  | 
|---|
| 47 | /* the socket this was on */ | 
|---|
| 48 | struct nbt_name_socket *nbtsock; | 
|---|
| 49 |  | 
|---|
| 50 | /* where to send the request */ | 
|---|
| 51 | struct socket_address *dest; | 
|---|
| 52 |  | 
|---|
| 53 | /* timeout between retries */ | 
|---|
| 54 | int timeout; | 
|---|
| 55 |  | 
|---|
| 56 | /* how many retries to send on timeout */ | 
|---|
| 57 | int num_retries; | 
|---|
| 58 |  | 
|---|
| 59 | /* whether we have received a WACK */ | 
|---|
| 60 | bool received_wack; | 
|---|
| 61 |  | 
|---|
| 62 | /* the timeout event */ | 
|---|
| 63 | struct tevent_timer *te; | 
|---|
| 64 |  | 
|---|
| 65 | /* the name transaction id */ | 
|---|
| 66 | uint16_t name_trn_id; | 
|---|
| 67 |  | 
|---|
| 68 | /* is it a reply? */ | 
|---|
| 69 | bool is_reply; | 
|---|
| 70 |  | 
|---|
| 71 | /* the encoded request */ | 
|---|
| 72 | DATA_BLOB encoded; | 
|---|
| 73 |  | 
|---|
| 74 | /* shall we allow multiple replies? */ | 
|---|
| 75 | bool allow_multiple_replies; | 
|---|
| 76 |  | 
|---|
| 77 | unsigned int num_replies; | 
|---|
| 78 | struct nbt_name_reply { | 
|---|
| 79 | struct nbt_name_packet *packet; | 
|---|
| 80 | struct socket_address *dest; | 
|---|
| 81 | } *replies; | 
|---|
| 82 |  | 
|---|
| 83 | /* information on what to do on completion */ | 
|---|
| 84 | struct { | 
|---|
| 85 | void (*fn)(struct nbt_name_request *); | 
|---|
| 86 | void *private_data; | 
|---|
| 87 | } async; | 
|---|
| 88 | }; | 
|---|
| 89 |  | 
|---|
| 90 |  | 
|---|
| 91 |  | 
|---|
| 92 | /* | 
|---|
| 93 | context structure for operations on name queries | 
|---|
| 94 | */ | 
|---|
| 95 | struct nbt_name_socket { | 
|---|
| 96 | struct socket_context *sock; | 
|---|
| 97 | struct tevent_context *event_ctx; | 
|---|
| 98 |  | 
|---|
| 99 | /* a queue of requests pending to be sent */ | 
|---|
| 100 | struct nbt_name_request *send_queue; | 
|---|
| 101 |  | 
|---|
| 102 | /* the fd event */ | 
|---|
| 103 | struct tevent_fd *fde; | 
|---|
| 104 |  | 
|---|
| 105 | /* mapping from name_trn_id to pending event */ | 
|---|
| 106 | struct idr_context *idr; | 
|---|
| 107 |  | 
|---|
| 108 | /* how many requests are waiting for a reply */ | 
|---|
| 109 | uint16_t num_pending; | 
|---|
| 110 |  | 
|---|
| 111 | /* what to do with incoming request packets */ | 
|---|
| 112 | struct { | 
|---|
| 113 | void (*handler)(struct nbt_name_socket *, struct nbt_name_packet *, | 
|---|
| 114 | struct socket_address *); | 
|---|
| 115 | void *private_data; | 
|---|
| 116 | } incoming; | 
|---|
| 117 |  | 
|---|
| 118 | /* what to do with unexpected replies */ | 
|---|
| 119 | struct { | 
|---|
| 120 | void (*handler)(struct nbt_name_socket *, struct nbt_name_packet *, | 
|---|
| 121 | struct socket_address *); | 
|---|
| 122 | void *private_data; | 
|---|
| 123 | } unexpected; | 
|---|
| 124 | }; | 
|---|
| 125 |  | 
|---|
| 126 |  | 
|---|
| 127 | /* a simple name query */ | 
|---|
| 128 | struct nbt_name_query { | 
|---|
| 129 | struct { | 
|---|
| 130 | struct nbt_name name; | 
|---|
| 131 | const char *dest_addr; | 
|---|
| 132 | uint16_t dest_port; | 
|---|
| 133 | bool broadcast; | 
|---|
| 134 | bool wins_lookup; | 
|---|
| 135 | int timeout; /* in seconds */ | 
|---|
| 136 | int retries; | 
|---|
| 137 | } in; | 
|---|
| 138 | struct { | 
|---|
| 139 | const char *reply_from; | 
|---|
| 140 | struct nbt_name name; | 
|---|
| 141 | int16_t num_addrs; | 
|---|
| 142 | const char **reply_addrs; | 
|---|
| 143 | } out; | 
|---|
| 144 | }; | 
|---|
| 145 |  | 
|---|
| 146 | /* a simple name status query */ | 
|---|
| 147 | struct nbt_name_status { | 
|---|
| 148 | struct { | 
|---|
| 149 | struct nbt_name name; | 
|---|
| 150 | const char *dest_addr; | 
|---|
| 151 | uint16_t dest_port; | 
|---|
| 152 | int timeout; /* in seconds */ | 
|---|
| 153 | int retries; | 
|---|
| 154 | } in; | 
|---|
| 155 | struct { | 
|---|
| 156 | const char *reply_from; | 
|---|
| 157 | struct nbt_name name; | 
|---|
| 158 | struct nbt_rdata_status status; | 
|---|
| 159 | } out; | 
|---|
| 160 | }; | 
|---|
| 161 |  | 
|---|
| 162 | /* a name registration request */ | 
|---|
| 163 | struct nbt_name_register { | 
|---|
| 164 | struct { | 
|---|
| 165 | struct nbt_name name; | 
|---|
| 166 | const char *dest_addr; | 
|---|
| 167 | uint16_t dest_port; | 
|---|
| 168 | const char *address; | 
|---|
| 169 | uint16_t nb_flags; | 
|---|
| 170 | bool register_demand; | 
|---|
| 171 | bool broadcast; | 
|---|
| 172 | bool multi_homed; | 
|---|
| 173 | uint32_t ttl; | 
|---|
| 174 | int timeout; /* in seconds */ | 
|---|
| 175 | int retries; | 
|---|
| 176 | } in; | 
|---|
| 177 | struct { | 
|---|
| 178 | const char *reply_from; | 
|---|
| 179 | struct nbt_name name; | 
|---|
| 180 | const char *reply_addr; | 
|---|
| 181 | uint8_t rcode; | 
|---|
| 182 | } out; | 
|---|
| 183 | }; | 
|---|
| 184 |  | 
|---|
| 185 | /* a send 3 times then demand name broadcast name registration */ | 
|---|
| 186 | struct nbt_name_register_bcast { | 
|---|
| 187 | struct { | 
|---|
| 188 | struct nbt_name name; | 
|---|
| 189 | const char *dest_addr; | 
|---|
| 190 | uint16_t dest_port; | 
|---|
| 191 | const char *address; | 
|---|
| 192 | uint16_t nb_flags; | 
|---|
| 193 | uint32_t ttl; | 
|---|
| 194 | } in; | 
|---|
| 195 | }; | 
|---|
| 196 |  | 
|---|
| 197 |  | 
|---|
| 198 | /* wins name register with multiple wins servers to try and multiple | 
|---|
| 199 | addresses to register */ | 
|---|
| 200 | struct nbt_name_register_wins { | 
|---|
| 201 | struct { | 
|---|
| 202 | struct nbt_name name; | 
|---|
| 203 | const char **wins_servers; | 
|---|
| 204 | uint16_t wins_port; | 
|---|
| 205 | const char **addresses; | 
|---|
| 206 | uint16_t nb_flags; | 
|---|
| 207 | uint32_t ttl; | 
|---|
| 208 | } in; | 
|---|
| 209 | struct { | 
|---|
| 210 | const char *wins_server; | 
|---|
| 211 | uint8_t rcode; | 
|---|
| 212 | } out; | 
|---|
| 213 | }; | 
|---|
| 214 |  | 
|---|
| 215 |  | 
|---|
| 216 |  | 
|---|
| 217 | /* a name refresh request */ | 
|---|
| 218 | struct nbt_name_refresh { | 
|---|
| 219 | struct { | 
|---|
| 220 | struct nbt_name name; | 
|---|
| 221 | const char *dest_addr; | 
|---|
| 222 | uint16_t dest_port; | 
|---|
| 223 | const char *address; | 
|---|
| 224 | uint16_t nb_flags; | 
|---|
| 225 | bool broadcast; | 
|---|
| 226 | uint32_t ttl; | 
|---|
| 227 | int timeout; /* in seconds */ | 
|---|
| 228 | int retries; | 
|---|
| 229 | } in; | 
|---|
| 230 | struct { | 
|---|
| 231 | const char *reply_from; | 
|---|
| 232 | struct nbt_name name; | 
|---|
| 233 | const char *reply_addr; | 
|---|
| 234 | uint8_t rcode; | 
|---|
| 235 | } out; | 
|---|
| 236 | }; | 
|---|
| 237 |  | 
|---|
| 238 | /* wins name refresh with multiple wins servers to try and multiple | 
|---|
| 239 | addresses to register */ | 
|---|
| 240 | struct nbt_name_refresh_wins { | 
|---|
| 241 | struct { | 
|---|
| 242 | struct nbt_name name; | 
|---|
| 243 | const char **wins_servers; | 
|---|
| 244 | uint16_t wins_port; | 
|---|
| 245 | const char **addresses; | 
|---|
| 246 | uint16_t nb_flags; | 
|---|
| 247 | uint32_t ttl; | 
|---|
| 248 | } in; | 
|---|
| 249 | struct { | 
|---|
| 250 | const char *wins_server; | 
|---|
| 251 | uint8_t rcode; | 
|---|
| 252 | } out; | 
|---|
| 253 | }; | 
|---|
| 254 |  | 
|---|
| 255 |  | 
|---|
| 256 | /* a name release request */ | 
|---|
| 257 | struct nbt_name_release { | 
|---|
| 258 | struct { | 
|---|
| 259 | struct nbt_name name; | 
|---|
| 260 | const char *dest_addr; | 
|---|
| 261 | uint16_t dest_port; | 
|---|
| 262 | const char *address; | 
|---|
| 263 | uint16_t nb_flags; | 
|---|
| 264 | bool broadcast; | 
|---|
| 265 | int timeout; /* in seconds */ | 
|---|
| 266 | int retries; | 
|---|
| 267 | } in; | 
|---|
| 268 | struct { | 
|---|
| 269 | const char *reply_from; | 
|---|
| 270 | struct nbt_name name; | 
|---|
| 271 | const char *reply_addr; | 
|---|
| 272 | uint8_t rcode; | 
|---|
| 273 | } out; | 
|---|
| 274 | }; | 
|---|
| 275 |  | 
|---|
| 276 | struct nbt_name_socket *nbt_name_socket_init(TALLOC_CTX *mem_ctx, | 
|---|
| 277 | struct tevent_context *event_ctx); | 
|---|
| 278 | void nbt_name_socket_handle_response_packet(struct nbt_name_request *req, | 
|---|
| 279 | struct nbt_name_packet *packet, | 
|---|
| 280 | struct socket_address *src); | 
|---|
| 281 | struct nbt_name_request *nbt_name_query_send(struct nbt_name_socket *nbtsock, | 
|---|
| 282 | struct nbt_name_query *io); | 
|---|
| 283 | NTSTATUS nbt_name_query_recv(struct nbt_name_request *req, | 
|---|
| 284 | TALLOC_CTX *mem_ctx, struct nbt_name_query *io); | 
|---|
| 285 | NTSTATUS nbt_name_query(struct nbt_name_socket *nbtsock, | 
|---|
| 286 | TALLOC_CTX *mem_ctx, struct nbt_name_query *io); | 
|---|
| 287 | struct nbt_name_request *nbt_name_status_send(struct nbt_name_socket *nbtsock, | 
|---|
| 288 | struct nbt_name_status *io); | 
|---|
| 289 | NTSTATUS nbt_name_status_recv(struct nbt_name_request *req, | 
|---|
| 290 | TALLOC_CTX *mem_ctx, struct nbt_name_status *io); | 
|---|
| 291 | NTSTATUS nbt_name_status(struct nbt_name_socket *nbtsock, | 
|---|
| 292 | TALLOC_CTX *mem_ctx, struct nbt_name_status *io); | 
|---|
| 293 |  | 
|---|
| 294 | NTSTATUS nbt_name_dup(TALLOC_CTX *mem_ctx, struct nbt_name *name, struct nbt_name *newname); | 
|---|
| 295 | NTSTATUS nbt_name_to_blob(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, struct nbt_name *name); | 
|---|
| 296 | NTSTATUS nbt_name_from_blob(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, struct nbt_name *name); | 
|---|
| 297 | void nbt_choose_called_name(TALLOC_CTX *mem_ctx, struct nbt_name *n, const char *name, int type); | 
|---|
| 298 | char *nbt_name_string(TALLOC_CTX *mem_ctx, const struct nbt_name *name); | 
|---|
| 299 | NTSTATUS nbt_name_register(struct nbt_name_socket *nbtsock, | 
|---|
| 300 | TALLOC_CTX *mem_ctx, struct nbt_name_register *io); | 
|---|
| 301 | NTSTATUS nbt_name_refresh(struct nbt_name_socket *nbtsock, | 
|---|
| 302 | TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io); | 
|---|
| 303 | NTSTATUS nbt_name_release(struct nbt_name_socket *nbtsock, | 
|---|
| 304 | TALLOC_CTX *mem_ctx, struct nbt_name_release *io); | 
|---|
| 305 | NTSTATUS nbt_name_register_wins(struct nbt_name_socket *nbtsock, | 
|---|
| 306 | TALLOC_CTX *mem_ctx, | 
|---|
| 307 | struct nbt_name_register_wins *io); | 
|---|
| 308 | NTSTATUS nbt_name_refresh_wins(struct nbt_name_socket *nbtsock, | 
|---|
| 309 | TALLOC_CTX *mem_ctx, | 
|---|
| 310 | struct nbt_name_refresh_wins *io); | 
|---|
| 311 | NTSTATUS nbt_name_register_recv(struct nbt_name_request *req, | 
|---|
| 312 | TALLOC_CTX *mem_ctx, struct nbt_name_register *io); | 
|---|
| 313 | struct nbt_name_request *nbt_name_register_send(struct nbt_name_socket *nbtsock, | 
|---|
| 314 | struct nbt_name_register *io); | 
|---|
| 315 | NTSTATUS nbt_name_release_recv(struct nbt_name_request *req, | 
|---|
| 316 | TALLOC_CTX *mem_ctx, struct nbt_name_release *io); | 
|---|
| 317 |  | 
|---|
| 318 | struct nbt_name_request *nbt_name_release_send(struct nbt_name_socket *nbtsock, | 
|---|
| 319 | struct nbt_name_release *io); | 
|---|
| 320 |  | 
|---|
| 321 | NTSTATUS nbt_name_refresh_recv(struct nbt_name_request *req, | 
|---|
| 322 | TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io); | 
|---|
| 323 |  | 
|---|
| 324 | NTSTATUS nbt_set_incoming_handler(struct nbt_name_socket *nbtsock, | 
|---|
| 325 | void (*handler)(struct nbt_name_socket *, struct nbt_name_packet *, | 
|---|
| 326 | struct socket_address *), | 
|---|
| 327 | void *private_data); | 
|---|
| 328 | NTSTATUS nbt_set_unexpected_handler(struct nbt_name_socket *nbtsock, | 
|---|
| 329 | void (*handler)(struct nbt_name_socket *, struct nbt_name_packet *, | 
|---|
| 330 | struct socket_address *), | 
|---|
| 331 | void *private_data); | 
|---|
| 332 | NTSTATUS nbt_name_reply_send(struct nbt_name_socket *nbtsock, | 
|---|
| 333 | struct socket_address *dest, | 
|---|
| 334 | struct nbt_name_packet *request); | 
|---|
| 335 |  | 
|---|
| 336 |  | 
|---|
| 337 | NDR_SCALAR_PROTO(wrepl_nbt_name, const struct nbt_name *) | 
|---|
| 338 | NDR_SCALAR_PROTO(nbt_string, const char *) | 
|---|
| 339 | NDR_BUFFER_PROTO(nbt_name, struct nbt_name) | 
|---|
| 340 | NTSTATUS nbt_rcode_to_ntstatus(uint8_t rcode); | 
|---|
| 341 |  | 
|---|
| 342 | struct tevent_context; | 
|---|
| 343 | struct tevent_req; | 
|---|
| 344 | struct tevent_req *nbt_name_register_bcast_send(TALLOC_CTX *mem_ctx, | 
|---|
| 345 | struct tevent_context *ev, | 
|---|
| 346 | struct nbt_name_socket *nbtsock, | 
|---|
| 347 | struct nbt_name_register_bcast *io); | 
|---|
| 348 | NTSTATUS nbt_name_register_bcast_recv(struct tevent_req *req); | 
|---|
| 349 | struct tevent_req *nbt_name_register_wins_send(TALLOC_CTX *mem_ctx, | 
|---|
| 350 | struct tevent_context *ev, | 
|---|
| 351 | struct nbt_name_socket *nbtsock, | 
|---|
| 352 | struct nbt_name_register_wins *io); | 
|---|
| 353 | NTSTATUS nbt_name_register_wins_recv(struct tevent_req *req, | 
|---|
| 354 | TALLOC_CTX *mem_ctx, | 
|---|
| 355 | struct nbt_name_register_wins *io); | 
|---|
| 356 | struct tevent_req *nbt_name_refresh_wins_send(TALLOC_CTX *mem_ctx, | 
|---|
| 357 | struct tevent_context *ev, | 
|---|
| 358 | struct nbt_name_socket *nbtsock, | 
|---|
| 359 | struct nbt_name_refresh_wins *io); | 
|---|
| 360 | NTSTATUS nbt_name_refresh_wins_recv(struct tevent_req *req, | 
|---|
| 361 | TALLOC_CTX *mem_ctx, | 
|---|
| 362 | struct nbt_name_refresh_wins *io); | 
|---|
| 363 |  | 
|---|
| 364 | XFILE *startlmhosts(const char *fname); | 
|---|
| 365 | bool getlmhostsent(TALLOC_CTX *ctx, XFILE *fp, char **pp_name, int *name_type, | 
|---|
| 366 | struct sockaddr_storage *pss); | 
|---|
| 367 | void endlmhosts(XFILE *fp); | 
|---|
| 368 |  | 
|---|
| 369 | NTSTATUS resolve_lmhosts_file_as_sockaddr(const char *lmhosts_file, | 
|---|
| 370 | const char *name, int name_type, | 
|---|
| 371 | TALLOC_CTX *mem_ctx, | 
|---|
| 372 | struct sockaddr_storage **return_iplist, | 
|---|
| 373 | int *return_count); | 
|---|
| 374 |  | 
|---|
| 375 | NTSTATUS resolve_dns_hosts_file_as_sockaddr(const char *dns_hosts_file, | 
|---|
| 376 | const char *name, bool srv_lookup, | 
|---|
| 377 | TALLOC_CTX *mem_ctx, | 
|---|
| 378 | struct sockaddr_storage **return_iplist, | 
|---|
| 379 | int *return_count); | 
|---|
| 380 |  | 
|---|
| 381 | #endif /* __LIBNBT_H__ */ | 
|---|