· 5 years ago · Jul 24, 2020, 01:08 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 // Important: Note the "+1". This is because strlen excludes the terminating NULL character
38 // so you need to particularly allocate space for it
39 size_t final_cmd_l = cmd_l + cmd_args_l + 1;
40
41 char *final_cmd = malloc(final_cmd_l);
42 char *ret = final_cmd;
43
44 strcpy(final_cmd, command);
45 strcpy(final_cmd, cmd_args);
46
47 return ret;
48}
49
50
51int main(int argc, char **argv)
52{
53 MYSQL *con = mysql_init(NULL);
54
55 FILE * fPtrOut;
56 fPtrOut = fopen("data.txt", "w");
57
58 char final_command[MAXLEN * 2];
59 char command[MAXLEN];
60 char ip[15];
61
62
63 if (fPtrOut == NULL){
64 printf("Unable to write to file.\nCheck datasions.\n" );
65 }
66
67 if (con == NULL){
68 finish_with_error(con);
69 }
70 if (mysql_real_connect(con, "localhost", "root", "PASSWORD",
71 NULL, 0, NULL, 0) == NULL){
72 finish_with_error(con);
73 }
74
75 send_query(con, "CREATE DATABASE IF NOT EXISTS cTest");
76 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))");
77 send_query(con, "TRUNCATE TABLE cTest.data");
78
79 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)");
80 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)");
81 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)");
82 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)");
83
84 send_query(con, "SELECT * FROM cTest.data WHERE in_range = 1 AND flag = 'GB' AND hops_taken <= 9");
85
86 MYSQL_RES *result = mysql_store_result(con);
87 if (result == NULL) {
88 finish_with_error(con);
89 }
90
91 int num_fields = mysql_num_fields(result);
92 int i, x = 0;
93 MYSQL_ROW row;
94
95 while ((row = mysql_fetch_row(result))){
96 for (i = 0; i < num_fields; i++){
97 fprintf(fPtrOut, "%s\t", row[i] ? row[i] : "NULL");
98 if (x == 2){
99 strcpy(ip, row[i]);
100
101 // Important: See how malloc-free are paired? The malloc in create_command allocates the
102 // space, we write things into it from "echo" and ip, then we read it in `snprintf` and
103 // `printf`, then we finally free it. In the next iteration, a separate buffer will be
104 // allocated and freed.
105 const char *format_string = create_command("echo", ip);
106 snprintf(final_command, sizeof(final_command), format_string);
107 printf("%s", final_command);
108 free(format_string);
109 }
110 x++;
111 if (x == 7){
112 fprintf(fPtrOut, "\n");
113 printf("\n");
114 x = 0;
115 }
116 }
117 }
118
119 fclose(fPtrOut);
120 mysql_free_result(result);
121 mysql_close(con);
122 return 0;
123
124}
125