| 1 | /*
|
|---|
| 2 | * Copyright (c) 2012 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 <fibril_synch.h>
|
|---|
| 39 | #include <socket_core.h>
|
|---|
| 40 | #include <sys/types.h>
|
|---|
| 41 |
|
|---|
| 42 | typedef enum {
|
|---|
| 43 | UDP_EOK,
|
|---|
| 44 | /* Insufficient resources */
|
|---|
| 45 | UDP_ENORES,
|
|---|
| 46 | /* Foreign socket unspecified */
|
|---|
| 47 | UDP_EUNSPEC,
|
|---|
| 48 | /* No route to destination */
|
|---|
| 49 | UDP_ENOROUTE
|
|---|
| 50 | } udp_error_t;
|
|---|
| 51 |
|
|---|
| 52 | typedef enum {
|
|---|
| 53 | XF_DUMMY = 0x1
|
|---|
| 54 | } xflags_t;
|
|---|
| 55 |
|
|---|
| 56 | typedef struct {
|
|---|
| 57 | uint32_t ipv4;
|
|---|
| 58 | } netaddr_t;
|
|---|
| 59 |
|
|---|
| 60 | enum netaddr {
|
|---|
| 61 | UDP_IPV4_ANY = 0
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| 64 | typedef struct {
|
|---|
| 65 | netaddr_t addr;
|
|---|
| 66 | uint16_t port;
|
|---|
| 67 | } udp_sock_t;
|
|---|
| 68 |
|
|---|
| 69 | typedef struct {
|
|---|
| 70 | udp_sock_t local;
|
|---|
| 71 | udp_sock_t foreign;
|
|---|
| 72 | } udp_sockpair_t;
|
|---|
| 73 |
|
|---|
| 74 | /** Unencoded UDP message (datagram) */
|
|---|
| 75 | typedef struct {
|
|---|
| 76 | /** Message data */
|
|---|
| 77 | void *data;
|
|---|
| 78 | /** Message data size */
|
|---|
| 79 | size_t data_size;
|
|---|
| 80 | } udp_msg_t;
|
|---|
| 81 |
|
|---|
| 82 | /** Encoded PDU */
|
|---|
| 83 | typedef struct {
|
|---|
| 84 | /** Source address */
|
|---|
| 85 | netaddr_t src;
|
|---|
| 86 | /** Destination address */
|
|---|
| 87 | netaddr_t dest;
|
|---|
| 88 |
|
|---|
| 89 | /** Encoded PDU data including header */
|
|---|
| 90 | void *data;
|
|---|
| 91 | /** Encoded PDU data size */
|
|---|
| 92 | size_t data_size;
|
|---|
| 93 | } udp_pdu_t;
|
|---|
| 94 |
|
|---|
| 95 | typedef struct {
|
|---|
| 96 | async_sess_t *sess;
|
|---|
| 97 | socket_cores_t sockets;
|
|---|
| 98 | } udp_client_t;
|
|---|
| 99 |
|
|---|
| 100 | /** UDP association
|
|---|
| 101 | *
|
|---|
| 102 | * This is a rough equivalent of a TCP connection endpoint. It allows
|
|---|
| 103 | * sending and receiving UDP datagrams and it is uniquely identified
|
|---|
| 104 | * by a socket pair.
|
|---|
| 105 | */
|
|---|
| 106 | typedef struct {
|
|---|
| 107 | char *name;
|
|---|
| 108 | link_t link;
|
|---|
| 109 |
|
|---|
| 110 | /** Association identification (local and foreign socket) */
|
|---|
| 111 | udp_sockpair_t ident;
|
|---|
| 112 |
|
|---|
| 113 | /** True if association was deleted by user */
|
|---|
| 114 | bool deleted;
|
|---|
| 115 |
|
|---|
| 116 | /** Protects access to association structure */
|
|---|
| 117 | fibril_mutex_t lock;
|
|---|
| 118 | /** Reference count */
|
|---|
| 119 | atomic_t refcnt;
|
|---|
| 120 |
|
|---|
| 121 | /** Receive queue */
|
|---|
| 122 | list_t rcv_queue;
|
|---|
| 123 | /** Receive queue CV. Broadcast when new datagram is inserted */
|
|---|
| 124 | fibril_condvar_t rcv_queue_cv;
|
|---|
| 125 | } udp_assoc_t;
|
|---|
| 126 |
|
|---|
| 127 | typedef struct {
|
|---|
| 128 | } udp_assoc_status_t;
|
|---|
| 129 |
|
|---|
| 130 | typedef struct udp_sockdata {
|
|---|
| 131 | /** Lock */
|
|---|
| 132 | fibril_mutex_t lock;
|
|---|
| 133 | /** Socket core */
|
|---|
| 134 | socket_core_t *sock_core;
|
|---|
| 135 | /** Client */
|
|---|
| 136 | udp_client_t *client;
|
|---|
| 137 | /** Connection */
|
|---|
| 138 | udp_assoc_t *assoc;
|
|---|
| 139 | /** Local address */
|
|---|
| 140 | netaddr_t laddr;
|
|---|
| 141 | } udp_sockdata_t;
|
|---|
| 142 |
|
|---|
| 143 | #endif
|
|---|
| 144 |
|
|---|
| 145 | /** @}
|
|---|
| 146 | */
|
|---|