LCOV - code coverage report
Current view: top level - source4/lib/socket - connect.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 43 48 89.6 %
Date: 2024-04-21 15:09:00 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    implements a non-blocking connect operation that is aware of the samba4
       5             :    events system
       6             : 
       7             :    Copyright (C) Andrew Tridgell 2005
       8             :    Copyright (C) Volker Lendecke 2005
       9             :    
      10             :    This program is free software; you can redistribute it and/or modify
      11             :    it under the terms of the GNU General Public License as published by
      12             :    the Free Software Foundation; either version 3 of the License, or
      13             :    (at your option) any later version.
      14             :    
      15             :    This program is distributed in the hope that it will be useful,
      16             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      17             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      18             :    GNU General Public License for more details.
      19             :    
      20             :    You should have received a copy of the GNU General Public License
      21             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      22             : */
      23             : 
      24             : #include "includes.h"
      25             : #include "lib/socket/socket.h"
      26             : #include "lib/events/events.h"
      27             : #include "libcli/composite/composite.h"
      28             : 
      29             : 
      30             : struct connect_state {
      31             :         struct socket_context *sock;
      32             :         const struct socket_address *my_address;
      33             :         const struct socket_address *server_address;
      34             :         uint32_t flags;
      35             : };
      36             : 
      37             : static void socket_connect_handler(struct tevent_context *ev,
      38             :                                    struct tevent_fd *fde, 
      39             :                                    uint16_t flags, void *private_data);
      40             : 
      41             : /*
      42             :   call the real socket_connect() call, and setup event handler
      43             : */
      44      102744 : static void socket_send_connect(struct composite_context *result)
      45             : {
      46        2970 :         struct tevent_fd *fde;
      47      102744 :         struct connect_state *state = talloc_get_type(result->private_data, 
      48             :                                                       struct connect_state);
      49             : 
      50      102744 :         result->status = socket_connect(state->sock,
      51             :                                         state->my_address,
      52             :                                         state->server_address,
      53             :                                         state->flags);
      54      102744 :         if (NT_STATUS_IS_ERR(result->status) && 
      55           0 :             !NT_STATUS_EQUAL(result->status,
      56             :                              NT_STATUS_MORE_PROCESSING_REQUIRED)) {
      57           0 :                 composite_error(result, result->status);
      58           0 :                 return;
      59             :         }
      60             : 
      61      102744 :         fde = tevent_add_fd(result->event_ctx, result,
      62             :                            socket_get_fd(state->sock),
      63             :                            TEVENT_FD_READ|TEVENT_FD_WRITE,
      64             :                            socket_connect_handler, result);
      65      102744 :         composite_nomem(fde, result);
      66             : }
      67             : 
      68             : 
      69             : /*
      70             :   send a socket connect, potentially doing some name resolution first
      71             : */
      72      102744 : struct composite_context *socket_connect_send(struct socket_context *sock,
      73             :                                               struct socket_address *my_address,
      74             :                                               struct socket_address *server_address, 
      75             :                                               uint32_t flags,
      76             :                                               struct tevent_context *event_ctx)
      77             : {
      78        2970 :         struct composite_context *result;
      79        2970 :         struct connect_state *state;
      80             : 
      81      102744 :         result = composite_create(sock, event_ctx);
      82      102744 :         if (result == NULL) return NULL;
      83             : 
      84      102744 :         state = talloc_zero(result, struct connect_state);
      85      102744 :         if (composite_nomem(state, result)) return result;
      86      102744 :         result->private_data = state;
      87             : 
      88      102744 :         state->sock = talloc_reference(state, sock);
      89      102744 :         if (composite_nomem(state->sock, result)) return result;
      90             : 
      91      102744 :         if (my_address) {
      92         480 :                 void *ref = talloc_reference(state, my_address);
      93         480 :                 if (composite_nomem(ref, result)) {
      94           0 :                         return result;
      95             :                 }
      96         480 :                 state->my_address = my_address;
      97             :         }
      98             : 
      99             :         {
     100      102744 :                 void *ref = talloc_reference(state, server_address);
     101      102744 :                 if (composite_nomem(ref, result)) {
     102           0 :                         return result;
     103             :                 }
     104      102744 :                 state->server_address = server_address;
     105             :         }
     106             : 
     107      102744 :         state->flags = flags;
     108             : 
     109      102744 :         set_blocking(socket_get_fd(sock), false);
     110             : 
     111      102744 :         socket_send_connect(result);
     112             : 
     113      102744 :         return result;
     114             : }
     115             : 
     116             : /*
     117             :   handle write events on connect completion
     118             : */
     119      545445 : static void socket_connect_handler(struct tevent_context *ev,
     120             :                                    struct tevent_fd *fde, 
     121             :                                    uint16_t flags, void *private_data)
     122             : {
     123        2970 :         struct composite_context *result =
     124      545445 :                 talloc_get_type(private_data, struct composite_context);
     125      545445 :         struct connect_state *state = talloc_get_type(result->private_data,
     126             :                                                       struct connect_state);
     127             : 
     128      545445 :         result->status = socket_connect_complete(state->sock, state->flags);
     129      545445 :         if (!composite_is_ok(result)) return;
     130             : 
     131      545445 :         composite_done(result);
     132             : }
     133             : 
     134             : /*
     135             :   wait for a socket_connect_send() to finish
     136             : */
     137      102671 : NTSTATUS socket_connect_recv(struct composite_context *result)
     138             : {
     139      102671 :         NTSTATUS status = composite_wait(result);
     140      102671 :         talloc_free(result);
     141      102671 :         return status;
     142             : }
     143             : 
     144             : 
     145             : /*
     146             :   like socket_connect() but takes an event context, doing a semi-async connect
     147             : */
     148       47952 : NTSTATUS socket_connect_ev(struct socket_context *sock,
     149             :                            struct socket_address *my_address,
     150             :                            struct socket_address *server_address, 
     151             :                            uint32_t flags,
     152             :                            struct tevent_context *ev)
     153             : {
     154        1756 :         struct composite_context *ctx;
     155       47952 :         ctx = socket_connect_send(sock, my_address, 
     156             :                                   server_address, flags, ev);
     157       47952 :         return socket_connect_recv(ctx);
     158             : }

Generated by: LCOV version 1.14