$Revision: 1.94 $
Query instructions (qi). This is where the queries are executed.
Status: NOT REVUED, TESTED
Included Files
Preprocessor definitions
#define USE_TEMPORARY_TABLE
#define TEMPORARY "TEMPORARY"
#define report_sql_error( condat, sql_connection, sql_command )
pthread_mutex_t Lock
Local Variables
sql_error_text
static const char* sql_error_text
Execute the query instructions. This is called for each source.
This is linked into MySQL by the fact that MySQL doesn't have sub selects
(yet). The queries are done in two stages. Make some temporary tables and
insert into them. Then use them in the next select.
More:
Authors:
ottrey - original version,
marek - the rest.
er_ret_t QI_execute ( ca_dbSource_t* dbhdl, Query_instructions* qis, Query_environ* qe, acc_st* acc_credit, acl_st* acl )
- ca_dbSource_t* dbhdl
- source-specific identifier (defined in CA)
- Query_instructions* qis
- query instructions.
- Query_environ* qe
- query environment.
- acc_st* acc_credit
- object display credit
- acl_st* acl
- copy of the original acl for this client
Prototyped in:
| /home/shane/code/RIP/include/query_instructions.h
|
Calls:
| ER_perror() | er/er.c
|
| __report_sql_error() | qi/query_instructions.c
|
| ca_srchandle2Intelement() | ca/ca_configFns.c
|
| ca_srchandle2Strelement() | ca/ca_configFns.c
|
| insert_radix_serials() | qi/query_instructions.c
|
| mnt_irt_filter() | qi/query_instructions.c
|
| qi_collect_ids() | qi/query_instructions.c
|
| qi_fetch_references() | qi/query_instructions.c
|
| write_objects() | qi/query_instructions.c
|
| write_radix_immediate() | qi/query_instructions.c
|
| SK_watch_setclear(), SK_watchstart(), SK_watchstop(), SQ_close_connection(), SQ_errno(), SQ_error(), SQ_execute_query(), SQ_get_connection(), UT_free_real(), mysql_thread_id(), pthread_self(), sprintf()
|
Called by:
| PW_process_qc() | pw/protocol_whois.c
|
This is for the '-F' flag.
It assumes lines starting with ' ', '\t' or '+' belong to the prior attribute.
Fast isn't fast anymore - it's just there for compatibility reasons.
More:
Authors:
ottrey,
marek - glib strings + small changes
char* QI_fast_output ( const char* str )
- const char* str
- The object to be "fast output'ed".
Free the query_instructions.
More:
Authors:
ottrey, marek
void QI_free ( Query_instructions* qis )
- Query_instructions* qis
- Query_instructions to be freed.
Create a new set of query_instructions. Returns an allocated structure which
must be freed after use with QI_free().
Authors:
ottrey,
marek.
Query_instructions* QI_new ( const Query_command* qc, const Query_environ* qe )
- const Query_command* qc
- The query_command that the instructions are created
from.
- const Query_environ* qe
- The environmental variables that they query is being
performed under.
Author:
marek.
char* QI_queries_to_string ( Query_instructions* qis )
- char* QI_queries_to_string
- returns a list of descriptions for queries
that will be performed (debugging only).
Allocated text, must be freed after use.
- Query_instructions* qis
- query instructions structure
void __report_sql_error ( sk_conn_st* condat, MYSQL* sql_connection, const char* sql_command, const char* file, int line, pthread_t tid )
- sk_conn_st* condat
- connection with user
SQ_connection_t *sql_connection connection to MySQL
const char *sql_command SQL command sent to MySQL
- MYSQL* sql_connection
- &nbs;
- const char* sql_command
- &nbs;
- const char* file
- &nbs;
- int line
- &nbs;
- pthread_t tid
- &nbs;
Basically it's for the '-K' flag for non-set (and non-radix) objects.
It assumes lines starting with ' ', '\t' or '+' belong to the prior attribute.
This could be speed up if there were breaks out of the loops, once it matched something.
More:
Authors:
ottrey
char* filter ( const char* str )
- const char* str
- ng The string to be filtered.
Find the most specific entry in the list of network nodes passed
and remove all entries but that one from the list.
condat connection data for the client
sql_connection connection to the database
datlist list of data from the radix tree (nodes of *rx_datcpy_t)
Note: The "-c" query flag specifies that the most specific inetnum or
inet6num with an "mnt-irt:" attribute should be returned.
To do this, we get a list of encompassing networks by sending the
same options to the RP/RX module that the '-L' query does. Then
we call this function, mnt_irt_filter(), and search the list for
the first entry that has an "mnt-irt:" attribute, by looking in
the "mnt_irt" table in MySQL for a reference (see the
rx_node_has_mnt_irt() for details).
If a reference is found, the list is replaced with a list
containing the single entry. If no reference is found, the list
is deleted.
void mnt_irt_filter ( sk_conn_st* condat, MYSQL* sql_connection, GList** datlist )
invoked when no such domain found. Goes through the domain table
and searches for shorter domains, then if it finds one with referral
it performs it, otherwise it just returns nothing.
to perform referral, it actually composes the referral query
for a given host/port/type and calls the whois query function.
Well, it returns nothing anyway (void). It just prints to the socket.
Author:
marek
void run_referral ( Query_environ* qe, char* ref_host, unsigned ref_port_int, char* qry )
- Query_environ* qe
- &nbs;
- char* ref_host
- referral server host name
- unsigned ref_port_int
- referral server port number
- char* qry
- query to be run
See if the node has an "mnt-irt:" attribute.
condat connection data for the client
sql_connection connection to the database
rx_data data node returned from the RX module
gboolean rx_node_has_mnt_irt ( sk_conn_st* condat, MYSQL* sql_connection, int object_id )
wrapper around sq_execute_query: starts a query
in a separate thread and starts the socket watchdog to cancel the query
if the socket is closed. If the socket has problems already (its
reason-to-close flag is set) no query is attempted.
The execution of the query or watchdog is not guaranteed at all!
SQ_connection_t **sql_connection sql connection
SQ_result_set_t **result_ptr storage for the query result structure
(passed to SQ_execute_query). Must either
be NULL, or the pointer it points to must
be NULL - in that case the result struct.
will be allocated in SQ.
Author:
marek.
int sql_execute_watched ( sk_conn_st* condat, MYSQL** sql_connection, const char* query, MYSQL_RES** result_ptr )
- int sql_execute_watched
- Returns the return code of SQ_execute_query,
Returns 0 for cancelled queries.
- sk_conn_st* condat
- connection to watch
- MYSQL** sql_connection
- &nbs;
- const char* query
- sql query to execute
- MYSQL_RES** result_ptr
- &nbs;
add_filter(): construct a query to limit the objects returned from the last
table to predefined types.
char *query_str buffer for the final query, containing the initial
part of the query (must be big enough)
Author:
ottrey.
static void add_filter ( GString* query_str, const Query_command* qc )
- GString* query_str
- &nbs;
- const Query_command* qc
- query command structure with the bitmap of
object types to be included.
Creates a SQL query for a reference-by-name lookup. Uses standard name
lookup query generator (create_name_query), so the order of the names
doesn't matter.
SQ_connection_t *sql_connection sql connection dedicated to this thread
static int add_ref_name ( MYSQL* sql_connection, char* rectable, char* allnames, sk_conn_st* condat )
- MYSQL* sql_connection
- &nbs;
- char* rectable
- table in which to look up
- char* allnames
- all name words to be looked up, space delimited.
- sk_conn_st* condat
- &nbs;
static char* asctime_r ( const struct tm* __tm, char* __buf )
construct a range query for the as_block table
(a query for an AS block object) given a string like:
AS1
AS1 - AS10
AS1-AS10
char *query_str buffer for the final query (must be big enough)
Author:
marek
static int create_asblock_query ( GString* query_str, const char* sql_query, const char* keys )
- int create_asblock_query
- Returns 0 on success, -1 on failure
(search term not an AS# nor range)
- GString* query_str
- &nbs;
- const char* sql_query
- rest of the sql query (with %d %d formats for
AS numbers)
- const char* keys
- user-supplied search term.
Create an sql query for the names table.
char *query_str
More:
Authors:
ottrey
static void create_name_query ( GString* query_str, const char* sql_query, const char* keys )
Create an sql query from the query_command and the matching keytype and the
selected inverse attributes.
Note this clears the first inv_attribute it sees, so is called sequentially
until there are no inv_attributes left.
WK_Type keytype The matching keytype.
mask_t *inv_attrs_bitmap The selected inverse attributes.
More:
Authors:
ottrey
static char* create_query ( const Query_t q, const Query_command* qc )
- const Query_t q
- &nbs;
- const Query_command* qc
- The query command.
static char* ctime_r ( const time_t* __time, char* __buf )
static int getlogin_r ( char* __name, int __len )
Insert the radix serial numbers into a temporary table in the database.
mask_t bitmap The bitmap of attribute to be converted.
SQ_connection_t *sql_connection The connection to the database.
More:
Authors:
ottrey,
marek
static int insert_radix_serials ( sk_conn_st* condat, MYSQL* sql_connection, char* id_table, GList* datlist )
- sk_conn_st* condat
- &nbs;
- MYSQL* sql_connection
- &nbs;
- char* id_table
- The id of the temporary table (This is a result of the hacky
way we've tried to get MySQL to do sub-selects.)
- GList* datlist
- The list of data from the radix tree.
Free the instruction.
More:
Authors:
ottrey
static void instruction_free ( Query_instruction* qi )
- Query_instruction* qi
- query_instruction to be freed.
The mapping between a query_command and a radix query.
More:
Authors:
ottrey,
marek - simplified the logic, added stealth -S option
static int map_qc2rx ( Query_instruction* qi, const Query_command* qc )
- Query_instruction* qi
- The Query Instruction to be created from the mapping
of the query command.
- const Query_command* qc
- The query command to be mapped.
specific case of the object ID collection: the domains.
Checks to see if the domain exists, and runs the referral if it is defined
and the domain is missing.
Arguments:
SQ_connection_t *sql_connection sql connection dedicated to this thread
Author:
marek.
static int qi_collect_domain ( char* sourcename, MYSQL* sql_connection, char* id_table, char* sub_table, Query_instructions* qis, Query_environ* qe, Query_instruction* qi, acc_st* acc_credit, int* sql_error )
- char* sourcename
- name of the database "source"
- MYSQL* sql_connection
- &nbs;
- char* id_table
- name of the temporary table to be used
- char* sub_table
- name of the temporary subtable
- Query_instructions* qis
- original query instructions structure
- Query_environ* qe
- original query environment structure
- Query_instruction* qi
- specific query instruction triggered
- acc_st* acc_credit
- credit for this client
- int* sql_error
- &nbs;
Calls:
| ER_dbg_va() | er/er.c
|
| __report_sql_error() | qi/query_instructions.c
|
| qi_prep_run_refer() | qi/query_instructions.c
|
| SQ_errno(), SQ_execute_query(), SQ_free_result(), SQ_get_affected_rows(), SQ_num_rows(), SQ_row_next(), fprintf(), g_string_free(), g_string_sized_new(), g_string_sprintf(), index(), pthread_self()
|
Called by:
| qi_collect_ids() | qi/query_instructions.c
|
References Variables:
| Query | df/defs.c
|
collects object ID's from all queries defined in the Query_instructions
array. The results from RADIX trees are maintained in a linked list, the
results from SQL lookups are kept in a temporary table. For domains,
a specific function is invoked that may run the referral.
Any sql lookup will be limited to the maximum number of objects allowed
for the client (acl and credit are checked for this).
The routine uses its own temporary _S table, destroyed at exit.
SQ_connection_t **sql_connection sql connection dedicated to this thread
(replaced on cancel)
static int qi_collect_ids ( ca_dbSource_t* dbhdl, char* sourcename, MYSQL** sql_connection, Query_instructions* qis, Query_environ* qe, char* id_table, GList** datlist, acc_st* acc_credit, acl_st* acl )
- ca_dbSource_t* dbhdl
- source-specific identifier (defined in CA)
- char* sourcename
- name of the database "source"
- MYSQL** sql_connection
- &nbs;
- Query_instructions* qis
- original query instructions structure
- Query_environ* qe
- original query environment structure
- char* id_table
- the table to store the ID's found
- GList** datlist
- the list to store the Radix leaves found
- acc_st* acc_credit
- credit for this client
- acl_st* acl
- acl for this client
Calls:
| AC_get_higher_limit() | ac/access_control.c
|
| ER_dbg_va() | er/er.c
|
| ER_inf_va() | er/er.c
|
| ER_is_traced() | er/er.c
|
| NOERR() | er/er.c
|
| __report_sql_error() | qi/query_instructions.c
|
| qi_collect_domain() | qi/query_instructions.c
|
| sql_execute_watched() | qi/query_instructions.c
|
| RP_asc_search(), SQ_errno(), SQ_execute_query(), SQ_get_affected_rows(), fprintf(), g_list_length(), g_string_free(), g_string_sized_new(), g_string_sprintf(), pthread_self(), sprintf(), strcpy()
|
Called by:
| QI_execute() | qi/query_instructions.c
|
References Variables:
| Query | df/defs.c
|
given the list of object ID's collects the references from these objects
to person and role objects. Uses its own temporary SQL table (_R)
and upon completion transfers the results from it to the main
temporary table. Runs queries in watched mode, to be able to cancel them.
SQ_connection_t **sql_connection sql connection dedicated to this thread
(replaced on cancel)
static int qi_fetch_references ( MYSQL** sql_connection, Query_environ* qe, char* id_table, acc_st* acc_credit, acl_st* acl )
- MYSQL** sql_connection
- &nbs;
- Query_environ* qe
- original query environment structure
- char* id_table
- the table with the ID's found
- acc_st* acc_credit
- credit for this client
- acl_st* acl
- acl for this client
Function invoked on query cancellation by the watchdog,
used from the sql_execute_watched() function.
It aborts the running query (the abort function in sq kills and
reestablished the connection).
Author:
marek.
static void* qi_kill_body ( void* arg )
- void* qi_kill_body
- result of sq_execute_query, int cast to (void*)
- void* arg
- pointer to sql connection
prepare and run the referral, displaying the results directly to the
client's connection.
XXX still missing protection against a referral loop
XXX handling inverse flag not needed, to be removed
SQ_result_set_t *result result of the lookup containing referral details
SQ_row_t *row first row (should be only 1) of the result
this should contain columns: type, port, host
Author:
marek
static void qi_prep_run_refer ( char* domain, Query_instructions* qis, Query_environ* qe, Query_instruction* qi, MYSQL_RES* result, MYSQL_ROW* row, char* sourcename )
- char* domain
- domain being looked up
- Query_instructions* qis
- original query instructions structure
- Query_environ* qe
- original query environment structure
- Query_instruction* qi
- specific query instruction triggered
- MYSQL_RES* result
- &nbs;
- MYSQL_ROW* row
- &nbs;
- char* sourcename
- name of the database "source"
static int readdir_r ( DIR* __dp, struct dirent* __ent, struct dirent** __res )
static int sigwait ( const sigset_t* __setp, int* __signo )
static int ttyname_r ( int __fildes, char* __buf, size_t __size )
Determine if this query should be conducted or not.
If it was an inverse query - if the attribute appears in the query command's bitmap.
If it was a lookup query - if the attribute appears in the object type bitmap or
disregard if there is no object_type bitmap (Ie object filter).
mask_t bitmap The bitmap of attribute to be converted.
static int valid_query ( const Query_command* qc, const Query_t q )
- const Query_command* qc
- The query_command that the instructions are created
from.
- const Query_t q
- The query being considered.
Authors:
ottrey,
marek.
SQ_connection_t *sql_connection The connection to the database.
More:
Authors:
ottrey,
marek.
static int write_objects ( MYSQL** sql_connection, char* id_table, unsigned int filtered, unsigned int fast, sk_conn_st* condat, acc_st* acc_credit, acl_st* acl )
- MYSQL** sql_connection
- &nbs;
- char* id_table
- The id of the temporary table (This is a result of the hacky
way we've tried to get MySQL to do sub-selects.)
- unsigned int filtered
- &nbs;
- unsigned int fast
- &nbs;
- sk_conn_st* condat
- Connection data for the client
- acc_st* acc_credit
- &nbs;
- acl_st* acl
- &nbs;
Display the immediate data carried with the objects returned by the
radix tree.
More:
Authors:
marek
Also free the list of answers.
static void write_radix_immediate ( GList* datlist, sk_conn_st* condat, acc_st* acc_credit, acl_st* acl )
- GList* datlist
- The linked list of dataleaf copies
- sk_conn_st* condat
- Connection data for the client
- acc_st* acc_credit
- Accounting struct
- acl_st* acl
- &nbs;
Write the results to the client socket.
SQ_result_set_t *result The result set returned from the sql query.
unsigned filtered if the objects should go through a filter (-K)
sk_conn_st *condat Connection data for the client
More:
Authors:
ottrey - initial design
marek - rewritten for accounting and cancellation.
static int write_results ( MYSQL_RES* result, unsigned filtered, unsigned fast, sk_conn_st* condat, acc_st* acc_credit, acl_st* acl )