common: Adding common library for sample vnf
[samplevnf.git] / common / VIL / alg / lib_sip_alg.h
1 /*
2 // Copyright (c) 2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #ifndef __INCLUDE_LIB_ALG_H__
18 #define __INCLUDE_LIB_ALG_H__
19
20 #include "rte_ether.h"
21
22 uint16_t sip_session_number;/* SIP session count */
23 #define IS_STRING_SAME(pStr, strId) (bcmp((pStr), strId, strlen(strId)) == 0)
24 #define TAG_TO_DATAPOS(str) (strlen(str) + 1)
25 #define SKIP_SPACES(pStr)               \
26 {                                       \
27         while (*(char *)(pStr) == ' ')  \
28         (char *)(pStr)++;               \
29 }
30
31 enum pkt_dir {PRIVATE, PUBLIC};
32
33 /* enum for  SIP Call direction - NAT ALG */
34 enum sip_alg_call_direction {
35         SIP_CALL_INCOMING, /* Incoming call public to private */
36         SIP_CALL_OUTGOING /* Outgoing call private to public */
37 };
38
39 /* enum of  SIP port type - NAT ALG */
40 enum sip_alg_port_type {
41         SIP_UDP, /* SIP SDP port 5460 */
42         SIP_RTP, /* RTP port number */
43         SIP_RTCP /* RTCP port number */
44 };
45
46 /*
47  * Data structure for NAT SIP ALG table key
48  * Key - IP address & L4 port number.
49  */
50 struct sip_alg_key {
51         /*
52          *  IP address based on direction.
53          *  outgoing - public IP, incoming - destinatio IP of pkt
54          */
55         uint32_t ip_address;
56         uint16_t l4port; /* SIP SDP, RTP, RTCP port number */
57         uint8_t filler1;
58         uint8_t filler2;
59 };
60
61 /*
62  * Data structure for NAT SIP ALG table entry.
63  * Placeholder for storing SIP ALG entries.
64  */
65 struct sip_alg_table_entry {
66         uint32_t ip_address;
67         /*
68          * IP address based on direction.
69          * outgoing - public IP, incoming - destinatio IP of pkt
70          */
71         uint16_t l4port; /* SIP UDP (5061), RTP, RTCP port number */
72         uint8_t sip_alg_call_direction;
73         /* Call incoming (pub to prv) or outgoing (prv to pub) */
74         uint8_t sip_alg_call_id[100];/* unique identfier for a SIP call */
75         uint8_t l4port_type;/* SIP_UDP or RTP or RTCP */
76         uint8_t filler1;
77         uint16_t filler2;
78         uint32_t filler3;
79 } __rte_cache_aligned;
80
81
82 /* Function declarations */
83
84 /**
85  * To initalize SIP ALG library and should be called-
86  * - before other SIP ALG library funcitons
87  * @param params
88  * pipeline parameter structure pointer
89  * @param app
90  * pipeline application conext structure pointer
91  * @return
92  * void return
93  */
94 void lib_sip_alg_init(void);
95
96 /**
97  * Main SIP ALG DPI function for processing SIP ALG functionlity
98  * @param pkt
99  * mbuf packet pointer
100  * @param pkt_direction
101  * Indicates whether pkt is from PRIVATE or PUBLIC direction
102  * @param modIp
103  * NAPT tranlated IP address based on direction
104  * @param modL4Port
105  * NAPT translated L4 port based on direction
106  * @param pubIP
107  * Original IP address before translation
108  * @param pubL4Port
109  * Original L4 port before translation
110  * @param modRtpPort
111  * RTP port
112  * @param modRtcpPort
113  * RTCP port
114  * @return
115  * 0 means success, -1 means failure
116  */
117 int sip_alg_dpi(struct rte_mbuf *pkt, enum pkt_dir pkt_direction,
118                 uint32_t modIp, uint16_t modL4Port,
119                 uint32_t pubIp, uint16_t pubL4Port,
120                 uint16_t modRtpPort, uint16_t modRtcpPort);
121
122 /**
123  * To get audio ports from SIP Packet
124  * @param pkt
125  * mbuf packet pointer
126  * @param rtpPort
127  * rtp port in parameter
128  * @param rtcpPort
129  * rtcp port in parameter
130  * @return
131  * 0 means success, -1 means failre
132  */
133 int natSipAlgGetAudioPorts(
134         struct rte_mbuf *pkt,
135         uint16_t *rtpPort,
136         uint16_t *rtcp_port);
137 int natSipAlgMsgFieldPos(
138         char *pData,
139         const char *pIdStr,
140         int *pos,
141         int searchLen);
142 int natSipAlgMsgFieldPosFindCrlf(
143         char *pData,
144         const char *pIdStr,
145         int *pPos,
146         int searchLen);
147 int natSipAlgMsgFieldPosFindSpace(
148         char *pData,
149         const char *pIdStr,
150         int *pPos,
151         int searchLen);
152 int remove_sip_alg_entry(
153         uint32_t ipaddr,
154         uint16_t portid);
155
156 #endif