| 1 | /*
|
|---|
| 2 | * Copyright (c) 2015 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup udp
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file UDP type definitions
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #ifndef UDP_TYPE_H
|
|---|
| 36 | #define UDP_TYPE_H
|
|---|
| 37 |
|
|---|
| 38 | #include <async.h>
|
|---|
| 39 | #include <fibril.h>
|
|---|
| 40 | #include <fibril_synch.h>
|
|---|
| 41 | #include <inet/endpoint.h>
|
|---|
| 42 | #include <ipc/loc.h>
|
|---|
| 43 | #include <sys/types.h>
|
|---|
| 44 | #include <inet/addr.h>
|
|---|
| 45 |
|
|---|
| 46 | #define UDP_FRAGMENT_SIZE 65535
|
|---|
| 47 |
|
|---|
| 48 | typedef enum {
|
|---|
| 49 | UDP_EOK,
|
|---|
| 50 | /* Insufficient resources */
|
|---|
| 51 | UDP_ENORES,
|
|---|
| 52 | /* Remote endpoint unspecified */
|
|---|
| 53 | UDP_EUNSPEC,
|
|---|
| 54 | /* No route to destination */
|
|---|
| 55 | UDP_ENOROUTE,
|
|---|
| 56 | /** Association reset by user */
|
|---|
| 57 | UDP_ERESET
|
|---|
| 58 | } udp_error_t;
|
|---|
| 59 |
|
|---|
| 60 | typedef enum {
|
|---|
| 61 | XF_DUMMY = 0x1
|
|---|
| 62 | } xflags_t;
|
|---|
| 63 |
|
|---|
| 64 | /** Unencoded UDP message (datagram) */
|
|---|
| 65 | typedef struct {
|
|---|
| 66 | /** Message data */
|
|---|
| 67 | void *data;
|
|---|
| 68 | /** Message data size */
|
|---|
| 69 | size_t data_size;
|
|---|
| 70 | } udp_msg_t;
|
|---|
| 71 |
|
|---|
| 72 | /** Encoded PDU */
|
|---|
| 73 | typedef struct {
|
|---|
| 74 | /** IP link (optional) */
|
|---|
| 75 | service_id_t iplink;
|
|---|
| 76 | /** Source address */
|
|---|
| 77 | inet_addr_t src;
|
|---|
| 78 | /** Destination address */
|
|---|
| 79 | inet_addr_t dest;
|
|---|
| 80 | /** Encoded PDU data including header */
|
|---|
| 81 | void *data;
|
|---|
| 82 | /** Encoded PDU data size */
|
|---|
| 83 | size_t data_size;
|
|---|
| 84 | } udp_pdu_t;
|
|---|
| 85 |
|
|---|
| 86 | typedef struct {
|
|---|
| 87 | void (*recv_msg)(void *, inet_ep2_t *, udp_msg_t *);
|
|---|
| 88 | } udp_assoc_cb_t;
|
|---|
| 89 |
|
|---|
| 90 | /** UDP association
|
|---|
| 91 | *
|
|---|
| 92 | * This is a rough equivalent of a TCP connection endpoint. It allows
|
|---|
| 93 | * sending and receiving UDP datagrams and it is uniquely identified
|
|---|
| 94 | * by an endpoint pair.
|
|---|
| 95 | */
|
|---|
| 96 | typedef struct {
|
|---|
| 97 | char *name;
|
|---|
| 98 | link_t link;
|
|---|
| 99 |
|
|---|
| 100 | /** Association identification (endpoint pair) */
|
|---|
| 101 | inet_ep2_t ident;
|
|---|
| 102 |
|
|---|
| 103 | /** True if association was reset by user */
|
|---|
| 104 | bool reset;
|
|---|
| 105 |
|
|---|
| 106 | /** True if association was deleted by user */
|
|---|
| 107 | bool deleted;
|
|---|
| 108 |
|
|---|
| 109 | /** Protects access to association structure */
|
|---|
| 110 | fibril_mutex_t lock;
|
|---|
| 111 | /** Reference count */
|
|---|
| 112 | atomic_t refcnt;
|
|---|
| 113 |
|
|---|
| 114 | /** Receive queue */
|
|---|
| 115 | list_t rcv_queue;
|
|---|
| 116 | /** Receive queue CV. Broadcast when new datagram is inserted */
|
|---|
| 117 | fibril_condvar_t rcv_queue_cv;
|
|---|
| 118 |
|
|---|
| 119 | udp_assoc_cb_t *cb;
|
|---|
| 120 | void *cb_arg;
|
|---|
| 121 | } udp_assoc_t;
|
|---|
| 122 |
|
|---|
| 123 | typedef struct {
|
|---|
| 124 | } udp_assoc_status_t;
|
|---|
| 125 |
|
|---|
| 126 | typedef struct {
|
|---|
| 127 | /** Link to receive queue */
|
|---|
| 128 | link_t link;
|
|---|
| 129 | /** Endpoint pair */
|
|---|
| 130 | inet_ep2_t epp;
|
|---|
| 131 | /** Message */
|
|---|
| 132 | udp_msg_t *msg;
|
|---|
| 133 | } udp_rcv_queue_entry_t;
|
|---|
| 134 |
|
|---|
| 135 | typedef struct udp_cassoc {
|
|---|
| 136 | /** Association */
|
|---|
| 137 | udp_assoc_t *assoc;
|
|---|
| 138 | /** Association ID for the client */
|
|---|
| 139 | sysarg_t id;
|
|---|
| 140 | /** Client */
|
|---|
| 141 | struct udp_client *client;
|
|---|
| 142 | link_t lclient;
|
|---|
| 143 | } udp_cassoc_t;
|
|---|
| 144 |
|
|---|
| 145 | typedef struct {
|
|---|
| 146 | /** Link to receive queue */
|
|---|
| 147 | link_t link;
|
|---|
| 148 | /** Endpoint pair */
|
|---|
| 149 | inet_ep2_t epp;
|
|---|
| 150 | /** Message */
|
|---|
| 151 | udp_msg_t *msg;
|
|---|
| 152 | /** Client association */
|
|---|
| 153 | udp_cassoc_t *cassoc;
|
|---|
| 154 | } udp_crcv_queue_entry_t;
|
|---|
| 155 |
|
|---|
| 156 | typedef struct udp_client {
|
|---|
| 157 | /** Client callback session */
|
|---|
| 158 | async_sess_t *sess;
|
|---|
| 159 | /** Client assocations */
|
|---|
| 160 | list_t cassoc; /* of udp_cassoc_t */
|
|---|
| 161 | /** Client receive queue */
|
|---|
| 162 | list_t crcv_queue;
|
|---|
| 163 | } udp_client_t;
|
|---|
| 164 |
|
|---|
| 165 | #endif
|
|---|
| 166 |
|
|---|
| 167 | /** @}
|
|---|
| 168 | */
|
|---|