aboutsummaryrefslogtreecommitdiffstats
path: root/metis/ccnx/forwarder/metis/io/metis_LocalListener.c
blob: 6e1889665753575d0781d9114426cbe4325b3949 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Implements a listener that works with stream connections over a named pipe.
 *
 */

#include <config.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <sys/un.h>
#include <errno.h>
#include <unistd.h>

#include <ccnx/forwarder/metis/core/metis_ConnectionTable.h>
#include <ccnx/forwarder/metis/core/metis_Forwarder.h>
#include <ccnx/forwarder/metis/io/metis_Listener.h>
#include <ccnx/forwarder/metis/io/metis_LocalListener.h>
#include <ccnx/forwarder/metis/io/metis_StreamConnection.h>

#include <LongBow/runtime.h>
#include <parc/algol/parc_Memory.h>

struct metis_local_listener {
    MetisForwarder *metis;
    MetisLogger *logger;
    PARCEventSocket *listener;
    CPIAddress *localAddress;
    unsigned id;
};

static void               _metisLocalListener_OpsDestroy(MetisListenerOps **listenerOpsPtr);
static unsigned           _metisLocalListener_OpsGetInterfaceIndex(const MetisListenerOps *ops);
static const CPIAddress  *_metisLocalListener_OpsGetListenAddress(const MetisListenerOps *ops);
static MetisEncapType     _metisLocalListener_OpsGetEncapType(const MetisListenerOps *ops);

static MetisListenerOps localTemplate = {
    .context           = NULL,
    .destroy           = &_metisLocalListener_OpsDestroy,
    .getInterfaceIndex = &_metisLocalListener_OpsGetInterfaceIndex,
    .getListenAddress  = &_metisLocalListener_OpsGetListenAddress,
    .getEncapType      = &_metisLocalListener_OpsGetEncapType,
};

// STREAM daemon listener callback
static void metisListenerLocal_Listen(int, struct sockaddr *, int socklen, void *localVoid);

MetisListenerOps *
metisLocalListener_Create(MetisForwarder *metis, const char *path)
{
    MetisLocalListener *local = parcMemory_AllocateAndClear(sizeof(MetisLocalListener));
    assertNotNull(local, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(MetisLocalListener));
    local->metis = metis;
    local->logger = metisLogger_Acquire(metisForwarder_GetLogger(metis));

    struct sockaddr_un addr_unix;
    memset(&addr_unix, 0, sizeof(addr_unix));

    addr_unix.sun_family = PF_UNIX;
    strcpy(addr_unix.sun_path, path);

    unlink(path);

    local->listener = metisDispatcher_CreateListener(metisForwarder_GetDispatcher(metis), metisListenerLocal_Listen,
                                                     (void *) local, -1, (struct sockaddr*) &addr_unix, sizeof(addr_unix));

    assertNotNull(local->listener, "Got null listener from metisDispatcher_CreateListener: (%d) %s", errno, strerror(errno));

    struct sockaddr_un addr_un;
    memset(&addr_un, 0, sizeof(addr_un));
    addr_un.sun_family = AF_UNIX;
    strcpy(addr_un.sun_path, path);

    local->localAddress = cpiAddress_CreateFromUnix(&addr_un);
    local->id = metisForwarder_GetNextConnectionId(metis);

    MetisListenerOps *ops = parcMemory_AllocateAndClear(sizeof(MetisListenerOps));
    assertNotNull(ops, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(MetisListenerOps));
    memcpy(ops, &localTemplate, sizeof(MetisListenerOps));
    ops->context = local;

    return ops;
}

void
metisLocalListener_Destroy(MetisLocalListener **listenerPtr)
{
    assertNotNull(listenerPtr, "Parameter must be non-null double pointer");
    assertNotNull(*listenerPtr, "Parameter must dereference to non-null pointer");

    MetisLocalListener *local = *listenerPtr;

    metisLogger_Release(&local->logger);

    cpiAddress_Destroy(&local->localAddress);
    metisDispatcher_DestroyListener(metisForwarder_GetDispatcher(local->metis), &local->listener);

    parcMemory_Deallocate((void **) &local);
    *listenerPtr = NULL;
}

// ==================================================

/**
 * @function metisListenerLocal_Listen
 * @abstract Called when a client connects to the server socket
 * @discussion
 *   Accepts a client connection.  Creates a new Stream connection and adds it
 *   to the connection table.
 *
 * @param fd the remote client socket (it will be AF_UNIX type)
 * @param sa the remote client address
 * @param socklen the bytes of sa
 * @param localVoid a void point to the MetisLocalListener that owns the server socket
 */
static void
metisListenerLocal_Listen(int fd,
                          struct sockaddr *sa, int socklen, void *localVoid)
{
    MetisLocalListener *local = (MetisLocalListener *) localVoid;
    assertTrue(sa->sa_family == AF_UNIX, "Got wrong address family, expected %d got %d", AF_UNIX, sa->sa_family);

    CPIAddress *remote = cpiAddress_CreateFromUnix((struct sockaddr_un *) sa);
    MetisAddressPair *pair = metisAddressPair_Create(local->localAddress, remote);

    MetisIoOperations *ops = metisStreamConnection_AcceptConnection(local->metis, fd, pair, true);
    MetisConnection *conn = metisConnection_Create(ops);

    metisConnectionTable_Add(metisForwarder_GetConnectionTable(local->metis), conn);

    if (metisLogger_IsLoggable(local->logger, MetisLoggerFacility_IO, PARCLogLevel_Debug)) {
        char *str = metisAddressPair_ToString(pair);
        metisLogger_Log(local->logger, MetisLoggerFacility_IO, PARCLogLevel_Debug, __func__,
                        "Listener %p started on address pair %s", (void *) local, str);
        free(str);
    }

    cpiAddress_Destroy(&remote);
}

static void
_metisLocalListener_OpsDestroy(MetisListenerOps **listenerOpsPtr)
{
    MetisListenerOps *ops = *listenerOpsPtr;
    MetisLocalListener *local = (MetisLocalListener *) ops->context;

    if (metisLogger_IsLoggable(local->logger, MetisLoggerFacility_IO, PARCLogLevel_Debug)) {
        metisLogger_Log(local->logger, MetisLoggerFacility_IO, PARCLogLevel_Debug, __func__,
                        "Listener %p destroyed", (void *) local);
    }

    metisLocalListener_Destroy(&local);
    parcMemory_Deallocate((void **) &ops);
    *listenerOpsPtr = NULL;
}

static unsigned
_metisLocalListener_OpsGetInterfaceIndex(const MetisListenerOps *ops)
{
    MetisLocalListener *local = (MetisLocalListener *) ops->context;
    return local->id;
}

static const CPIAddress *
_metisLocalListener_OpsGetListenAddress(const MetisListenerOps *ops)
{
    MetisLocalListener *local = (MetisLocalListener *) ops->context;
    return local->localAddress;
}

static MetisEncapType
_metisLocalListener_OpsGetEncapType(const MetisListenerOps *ops)
{
    return METIS_ENCAP_LOCAL;
}