1 /*
2  * usrloc_db.h
3  *
4  *  Created on: Nov 11, 2013
5  *      Author: carlos
6  */
7 
8 #ifndef USRLOC_DB_H_
9 #define USRLOC_DB_H_
10 
11 typedef enum location_pcscf_fields_idx {
12 //	LP_ID_IDX = 0,
13 	LP_DOMAIN_IDX = 0,
14 	LP_AOR_IDX,
15 	LP_RECEIVED_IDX,
16 	LP_RECEIVED_PORT_IDX,
17 	LP_RECEIVED_PROTO_IDX,
18 	LP_PATH_IDX,
19         LP_RINSTANCE_IDX,
20 	LP_RX_SESSION_ID_IDX,
21 	LP_REG_STATE_IDX,
22 	LP_EXPIRES_IDX,
23 	LP_SERVICE_ROUTES_IDX,
24 	LP_SOCKET_IDX,
25 	LP_PUBLIC_IPS_IDX,
26                 LP_HOST_IDX,
27                 LP_PORT_IDX,
28                 LP_PROTOCOL_IDX
29 
30 } location_pcscf_fields_idx_t;
31 
32 #define GET_FIELD_IDX(_val, _idx)\
33 						(_val + _idx)
34 
35 #define SET_PROPER_NULL_FLAG(_str, _vals, _index)\
36 	do{\
37 		if( (_str).len == 0)\
38 			VAL_NULL( (_vals)+(_index) ) = 1;\
39 		else\
40 			VAL_NULL( (_vals)+(_index) ) = 0;\
41 	}while(0);
42 
43 #define SET_STR_VALUE(_val, _str)\
44 	do{\
45 			VAL_STR((_val)).s 		= (_str).s;\
46 			VAL_STR((_val)).len 	= (_str).len;\
47 	}while(0);
48 
49 #define SET_NULL_FLAG(_vals, _i, _max, _flag)\
50 	do{\
51 		for((_i) = 0;(_i)<(_max); (_i)++)\
52 			VAL_NULL((_vals)+(_i)) = (_flag);\
53 	}while(0);
54 
55 
56 #define ID_COL				"id"
57 #define DOMAIN_COL			"domain"
58 #define HOST_COL			"host"
59 #define PORT_COL			"port"
60 #define PROTOCOL_COL		"protocol"
61 #define AOR_COL				"aor"
62 #define RECEIVED_COL		"received"
63 #define RECEIVED_PORT_COL	"received_port"
64 #define RECEIVED_PROTO_COL	"received_proto"
65 #define PATH_COL			"path"
66 #define RINSTANCE_COL           "rinstance"
67 #define RX_SESSION_ID_COL	"rx_session_id"
68 #define REG_STATE_COL		"reg_state"
69 #define EXPIRES_COL			"expires"
70 #define SERVICE_ROUTES_COL	"service_routes"
71 #define SOCKET_COL			"socket"
72 #define PUBLIC_IDS_COL		"public_ids"
73 #define SECURITY_TYPE_COL	"security_type"
74 
75 #define MODE_COL			"mode"
76 #define CK_COL				"ck"
77 #define IK_COL				"ik"
78 #define EALG_COL			"ealg"
79 #define IALG_COL			"ialg"
80 #define PORTPC_COL			"port_pc"
81 #define PORTPS_COL			"port_ps"
82 #define PORTUC_COL			"port_uc"
83 #define PORTUS_COL			"port_us"
84 #define SPIPC_COL			"spi_pc"
85 #define SPIPS_COL			"spi_ps"
86 #define SPIUC_COL			"spi_uc"
87 #define SPIUS_COL			"spi_us"
88 #define T_SECURITY_TYPE_COL	"t_security_type"
89 #define T_PROTOCOL_COL		"t_protocol"
90 #define T_MODE_COL			"t_mode"
91 #define T_CK_COL			"t_ck"
92 #define T_IK_COL			"t_ik"
93 #define T_EALG_COL			"t_ealg"
94 #define T_IALG_COL			"t_ialg"
95 #define T_PORTPC_COL		"t_port_pc"
96 #define T_PORTPS_COL		"t_port_ps"
97 #define T_PORTUC_COL		"t_port_uc"
98 #define T_PORTUS_COL		"t_port_us"
99 #define T_SPIPC_COL			"t_spi_pc"
100 #define T_SPIPS_COL			"t_spi_ps"
101 #define T_SPIUC_COL			"t_spi_uc"
102 #define T_SPIUS_COL			"t_spi_us"
103 
104 extern db1_con_t* ul_dbh;
105 extern db_func_t ul_dbf;
106 
107 extern str id_col;
108 extern str domain_col;
109 extern str aor_col;
110 extern str host_col;
111 extern str port_col;
112 extern str protocol_col;
113 extern str received_col;
114 extern str received_port_col;
115 extern str received_proto_col;
116 extern str path_col;
117 extern str rx_session_id_col;
118 extern str reg_state_col;
119 extern str expires_col;
120 extern str service_routes_col;
121 extern str socket_col;
122 extern str public_ids_col;
123 
124 typedef struct reusable_buffer{
125 	char* buf;
126 	int buf_len;
127 	int data_len;
128 } t_reusable_buffer;
129 
130 int use_location_pcscf_table();
131 void destroy_db();
132 int init_db(const str *db_url, int db_update_period, int fetch_num_rows);
133 int connect_db(const str *db_url);
134 
135 int impus_as_string(struct pcontact* _c, t_reusable_buffer* buffer);
136 int service_routes_as_string(struct pcontact* _c, t_reusable_buffer *buffer);
137 void free_service_route_buf(void);
138 void free_impu_buf(void);
139 
140 int db_insert_pcontact(pcontact_t* _c);
141 int db_delete_pcontact(pcontact_t* _c);
142 int db_update_pcontact(pcontact_t* _c);
143 int db_update_pcontact_security_temp(struct pcontact* _c, security_type _t, security_t* _s);
144 int db_update_pcontact_security(struct pcontact* _c, security_type _t, security_t* _s);
145 
146 #endif /* USRLOC_DB_H_ */
147