Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * SCCP management code |
| 3 | * |
Holger Hans Peter Freyther | 9b51b1b | 2010-01-27 12:29:49 +0100 | [diff] [blame] | 4 | * (C) 2009, 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 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 2 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 along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #ifndef SCCP_H |
| 25 | #define SCCP_H |
| 26 | |
| 27 | #include <stdlib.h> |
| 28 | |
| 29 | #include <sys/socket.h> |
Holger Hans Peter Freyther | 7f301de | 2010-02-12 22:44:50 +0100 | [diff] [blame] | 30 | #include <sys/types.h> |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 31 | |
| 32 | #include "sccp_types.h" |
| 33 | |
Holger Hans Peter Freyther | 7f301de | 2010-02-12 22:44:50 +0100 | [diff] [blame] | 34 | struct msgb; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 35 | struct sccp_system; |
| 36 | |
| 37 | enum { |
| 38 | SCCP_CONNECTION_STATE_NONE, |
| 39 | SCCP_CONNECTION_STATE_REQUEST, |
| 40 | SCCP_CONNECTION_STATE_CONFIRM, |
| 41 | SCCP_CONNECTION_STATE_ESTABLISHED, |
| 42 | SCCP_CONNECTION_STATE_RELEASE, |
| 43 | SCCP_CONNECTION_STATE_RELEASE_COMPLETE, |
| 44 | SCCP_CONNECTION_STATE_REFUSED, |
| 45 | SCCP_CONNECTION_STATE_SETUP_ERROR, |
| 46 | }; |
| 47 | |
| 48 | struct sockaddr_sccp { |
| 49 | sa_family_t sccp_family; /* AF_SCCP in the future??? */ |
| 50 | u_int8_t sccp_ssn; /* subssystem number for routing */ |
| 51 | |
| 52 | /* TODO fill in address indicator... if that is ever needed */ |
| 53 | |
| 54 | /* not sure about these */ |
| 55 | /* u_int8_t sccp_class; */ |
| 56 | }; |
| 57 | |
| 58 | /* |
| 59 | * parsed structure of an address |
| 60 | */ |
| 61 | struct sccp_address { |
| 62 | struct sccp_called_party_address address; |
| 63 | u_int8_t ssn; |
| 64 | u_int8_t poi[2]; |
| 65 | }; |
| 66 | |
| 67 | struct sccp_optional_data { |
| 68 | u_int8_t data_len; |
| 69 | u_int8_t data_start; |
| 70 | }; |
| 71 | |
| 72 | struct sccp_connection { |
| 73 | /* public */ |
| 74 | void *data_ctx; |
| 75 | void (*data_cb)(struct sccp_connection *conn, struct msgb *msg, unsigned int len); |
| 76 | |
| 77 | void *state_ctx; |
| 78 | void (*state_cb)(struct sccp_connection *, int old_state); |
| 79 | |
| 80 | struct sccp_source_reference source_local_reference; |
| 81 | struct sccp_source_reference destination_local_reference; |
| 82 | |
| 83 | int connection_state; |
| 84 | |
| 85 | /* private */ |
| 86 | /* list of active connections */ |
| 87 | struct llist_head list; |
| 88 | struct sccp_system *system; |
| 89 | int incoming; |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * system functionality to implement on top of any other transport layer: |
| 94 | * call sccp_system_incoming for incoming data (from the network) |
| 95 | * sccp will call outgoing whenever outgoing data exists |
| 96 | */ |
Holger Hans Peter Freyther | 9f2f471 | 2010-03-26 05:44:21 +0100 | [diff] [blame] | 97 | int sccp_system_init(void (*outgoing)(struct msgb *data, void *ctx), void *context); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 98 | int sccp_system_incoming(struct msgb *data); |
| 99 | |
| 100 | /** |
| 101 | * Send data on an existing connection |
| 102 | */ |
| 103 | int sccp_connection_write(struct sccp_connection *connection, struct msgb *data); |
Holger Hans Peter Freyther | 9343821 | 2009-11-18 22:11:28 +0100 | [diff] [blame] | 104 | int sccp_connection_send_it(struct sccp_connection *connection); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 105 | int sccp_connection_close(struct sccp_connection *connection, int cause); |
| 106 | int sccp_connection_free(struct sccp_connection *connection); |
| 107 | |
| 108 | /** |
Holger Hans Peter Freyther | c07274e | 2010-04-05 18:13:40 +0200 | [diff] [blame] | 109 | * internal.. |
| 110 | */ |
| 111 | int sccp_connection_force_free(struct sccp_connection *conn); |
| 112 | |
| 113 | /** |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 114 | * Create a new socket. Set your callbacks and then call bind to open |
| 115 | * the connection. |
| 116 | */ |
| 117 | struct sccp_connection *sccp_connection_socket(void); |
| 118 | |
| 119 | /** |
| 120 | * Open the connection and send additional data |
| 121 | */ |
| 122 | int sccp_connection_connect(struct sccp_connection *conn, |
| 123 | const struct sockaddr_sccp *sccp_called, |
| 124 | struct msgb *data); |
| 125 | |
| 126 | /** |
| 127 | * mostly for testing purposes only. Set the accept callback. |
| 128 | * TODO: add true routing information... in analogy to socket, bind, accept |
| 129 | */ |
| 130 | int sccp_connection_set_incoming(const struct sockaddr_sccp *sock, |
| 131 | int (*accept_cb)(struct sccp_connection *connection, void *data), |
| 132 | void *user_data); |
| 133 | |
| 134 | /** |
| 135 | * Send data in terms of unit data. A fixed address indicator will be used. |
| 136 | */ |
| 137 | int sccp_write(struct msgb *data, |
| 138 | const struct sockaddr_sccp *sock_sender, |
| 139 | const struct sockaddr_sccp *sock_target, int class); |
| 140 | int sccp_set_read(const struct sockaddr_sccp *sock, |
| 141 | int (*read_cb)(struct msgb *msgb, unsigned int, void *user_data), |
| 142 | void *user_data); |
| 143 | |
| 144 | /* generic sock addresses */ |
| 145 | extern const struct sockaddr_sccp sccp_ssn_bssap; |
| 146 | |
| 147 | /* helpers */ |
| 148 | u_int32_t sccp_src_ref_to_int(struct sccp_source_reference *ref); |
| 149 | struct sccp_source_reference sccp_src_ref_from_int(u_int32_t); |
| 150 | |
Holger Hans Peter Freyther | 0b3011c | 2010-05-15 05:35:48 +0800 | [diff] [blame] | 151 | struct msgb *sccp_create_refuse(struct sccp_source_reference *src_ref, int cause, uint8_t *data, int length); |
Holger Hans Peter Freyther | 3ca6065 | 2010-05-16 17:55:07 +0800 | [diff] [blame] | 152 | struct msgb *sccp_create_cc(struct sccp_source_reference *src_ref, struct sccp_source_reference *dst_ref); |
Holger Hans Peter Freyther | 656fe86 | 2010-05-16 18:33:26 +0800 | [diff] [blame] | 153 | struct msgb *sccp_create_rlsd(struct sccp_source_reference *src_ref, struct sccp_source_reference *dst_ref, int cause); |
Holger Hans Peter Freyther | 6d464dd | 2010-05-16 20:41:48 +0800 | [diff] [blame] | 154 | struct msgb *sccp_create_dt1(struct sccp_source_reference *dst_ref, uint8_t *data, uint8_t len); |
Holger Hans Peter Freyther | 34ec49d | 2010-05-14 03:34:35 +0800 | [diff] [blame] | 155 | |
Holger Hans Peter Freyther | 9b51b1b | 2010-01-27 12:29:49 +0100 | [diff] [blame] | 156 | /** |
| 157 | * Below this are helper functions and structs for parsing SCCP messages |
| 158 | */ |
| 159 | struct sccp_parse_result { |
| 160 | struct sccp_address called; |
| 161 | struct sccp_address calling; |
| 162 | |
| 163 | /* point to the msg packet */ |
| 164 | struct sccp_source_reference *source_local_reference; |
| 165 | struct sccp_source_reference *destination_local_reference; |
| 166 | |
| 167 | /* data pointer */ |
| 168 | int data_len; |
| 169 | }; |
Holger Hans Peter Freyther | 481e10b | 2010-01-13 09:55:43 +0100 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * helper functions for the nat code |
| 173 | */ |
| 174 | int sccp_determine_msg_type(struct msgb *msg); |
Holger Hans Peter Freyther | 9b51b1b | 2010-01-27 12:29:49 +0100 | [diff] [blame] | 175 | int sccp_parse_header(struct msgb *msg, struct sccp_parse_result *result); |
Holger Hans Peter Freyther | 481e10b | 2010-01-13 09:55:43 +0100 | [diff] [blame] | 176 | |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 177 | #endif |