· 5 years ago · Jul 24, 2020, 12:30 PM
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <mysql/mysql.h>
5
6const int MAXLEN = 100;
7
8/* Function definitions, in order of appearence */
9void finish_with_error(MYSQL *con);
10int send_query(MYSQL *con, char query[MAXLEN]);
11const char * create_command(char command[MAXLEN], char cmd_args[MAXLEN]);
12
13void finish_with_error(MYSQL *con)
14{
15 fprintf(stderr, "%s\n", mysql_error(con));
16 mysql_close(con);
17 exit(1);
18}
19
20int send_query(MYSQL *con, char query[MAXLEN])
21{
22 if (mysql_query(con, query)) {
23 finish_with_error(con);
24 }
25}
26
27const char * create_command(char command[MAXLEN], char cmd_args[MAXLEN])
28{
29 /*
30 This function is basically made redundant, but this is one of the solutions
31 That I tried... Basically the aim was to extrapolate the string addition process
32 So that I didn't have to do it in main()... Directly underneath the calling of this
33 function, there's a one-line snprintf which aims to do the exact same thing as this function.
34*/
35 size_t cmd_l = strlen(command);
36 size_t cmd_args_l = strlen(cmd_args);
37 size_t final_cmd_l = cmd_l + cmd_args_l;
38
39 char *final_cmd = malloc(final_cmd_l);
40 char *ret = final_cmd;
41
42 strcpy(final_cmd, command);
43 strcpy(final_cmd, cmd_args);
44
45 return ret;
46}
47
48
49int main(int argc, char **argv)
50{
51 MYSQL *con = mysql_init(NULL);
52
53 FILE * fPtrOut;
54 fPtrOut = fopen("data.txt", "w");
55
56 char final_command[MAXLEN * 2];
57 char command[MAXLEN];
58 char ip[15];
59
60
61 if (fPtrOut == NULL){
62 printf("Unable to write to file.\nCheck datasions.\n" );
63 }
64
65 if (con == NULL){
66 finish_with_error(con);
67 }
68 if (mysql_real_connect(con, "localhost", "root", "PASSWORD",
69 NULL, 0, NULL, 0) == NULL){
70 finish_with_error(con);
71 }
72
73 send_query(con, "CREATE DATABASE IF NOT EXISTS cTest");
74 send_query(con, "CREATE TABLE IF NOT EXISTS cTest.data (id INT(1), domain VARCHAR(32), ip VARCHAR(15), flag VARCHAR(2), internal_node TINYINT(1), in_range TINYINT(1), hops_taken INT(3))");
75 send_query(con, "TRUNCATE TABLE cTest.data");
76
77 send_query(con, "INSERT INTO cTest.data (id,domain,ip,flag,internal_node,in_range,hops_taken) VALUES (0, 'test1.com', '192.168.0.1', 'GB', 1, 1, 8)");
78 send_query(con, "INSERT INTO cTest.data (id,domain,ip,flag,internal_node,in_range,hops_taken) VALUES (1, 'test2.com', '192.168.0.2', 'GB', 1, 1, 7)");
79 send_query(con, "INSERT INTO cTest.data (id,domain,ip,flag,internal_node,in_range,hops_taken) VALUES (2, 'test3.com', '192.168.0.3', 'GB', 1, 1, 6)");
80 send_query(con, "INSERT INTO cTest.data (id,domain,ip,flag,internal_node,in_range,hops_taken) VALUES (3, 'test4.com', '192.168.0.4', 'GB', 1, 1, 5)");
81
82 send_query(con, "SELECT * FROM cTest.data WHERE in_range = 1 AND flag = 'GB' AND hops_taken <= 9");
83
84 MYSQL_RES *result = mysql_store_result(con);
85 if (result == NULL) {
86 finish_with_error(con);
87 }
88
89 int num_fields = mysql_num_fields(result);
90 int i, x = 0;
91 MYSQL_ROW row;
92
93 while ((row = mysql_fetch_row(result))){
94 for (i = 0; i < num_fields; i++){
95 fprintf(fPtrOut, "%s\t", row[i] ? row[i] : "NULL");
96 if (x == 2){
97 strcpy(ip, row[i]);
98 snprintf(final_command, sizeof(final_command), create_command("echo", ip));
99 // snprintf(final_command, sizeof(final_command), command, ip);
100 printf("%s", final_command);
101 }
102 x++;
103 if (x == 7){
104 fprintf(fPtrOut, "\n");
105 printf("\n");
106 x = 0;
107 }
108 }
109 }
110
111 fclose(fPtrOut);
112 mysql_free_result(result);
113 mysql_close(con);
114 free(final_command);
115 return 0;
116
117}
118