summaryrefslogtreecommitdiff
path: root/sqlQueries.go
blob: 7bea09822a7701e18ca1bbeb93f62611f8bff64e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main

import (
	"database/sql"
	_ "github.com/Go-SQL-Driver/MySQL"
	"log"
	"os"
	"time"
)

const (
	TIMEZONE = "UTC"
)

var (
	slogger *log.Logger
)

func init() {
	slogger = log.New(os.Stdout, "[ SQL ]", log.LstdFlags)
}

//Retrieves limit Rawdata entries that are older than tim
//limit <= 0 returns all entries that are older than tim
func fetchRawData(db *sql.DB, cfg *Config, tim time.Time) (rDat []RawData, err error) {

	var prepSel *sql.Stmt
	if cfg.Limit > 0 {
		prepSel, err = db.Prepare("SELECT ip_src,ip_dst,as_src,as_dst,port_src,port_dst,packets,pkt_len_distrib,stamp_inserted FROM " + cfg.RawTable + " WHERE stamp_processed IS NULL AND stamp_inserted < ? LIMIT ?")
	} else {
		prepSel, err = db.Prepare("SELECT ip_src,ip_dst,as_src,as_dst,port_src,port_dst,packets,pkt_len_distrib,stamp_inserted FROM " + cfg.RawTable + " WHERE stamp_processed IS NULL AND stamp_inserted < ?")
	}
	if err != nil {
		slogger.Println("Failed to prepare select")
		return
	}

	var rows *sql.Rows
	if cfg.Limit > 0 {
		rows, err = prepSel.Query(tim, cfg.Limit)
	} else {
		rows, err = prepSel.Query(tim)
	}
	if err != nil {
		slogger.Println("Failed to query prepared selection")
		return
	}
	defer rows.Close()

	tx, err := db.Begin()
	if err != nil {
		slogger.Println("Failed to initialize transaction")
		return
	}

	prepUp, err := tx.Prepare("UPDATE " + cfg.RawTable + " SET stamp_processed = ? where ip_src = ? AND ip_dst = ? AND as_src = ? AND as_dst = ? AND port_src = ? AND port_dst = ? AND packets = ? AND pkt_len_distrib = ? AND stamp_inserted = ?")
	if err != nil {
		slogger.Println("Failed to prepare update")
		return
	}

	loc, err := time.LoadLocation(TIMEZONE)
	if err != nil {
		slogger.Println("Couldn't load timezone")
		return
	}
	for rows.Next() {
		var r RawData
		var tim []byte
		err = rows.Scan(&r.Ip_src, &r.Ip_dst, &r.As_src, &r.As_dst, &r.Port_src, &r.Port_dst, &r.Packets, &r.Pkt_len_distrib, &tim)
		if err != nil {
			slogger.Println("Failed to scan result of query")
			return
		}
		r.time, err = time.ParseInLocation("2006-01-02 15:04:05", string(tim), loc)
		if err != nil {
			slogger.Println("Failed to parse timestamp")
			return
		}

		_, err = prepUp.Exec(time.Now(), r.Ip_src, r.Ip_dst, r.As_src, r.As_dst, r.Port_src, r.Port_dst, r.Packets, r.Pkt_len_distrib, tim)
		if err != nil {
			slogger.Println("Failed to query prepared update")
			tx.Rollback()
			return
		}

		rDat = append(rDat, r)
	}
	tx.Commit()
	return
}

//Removes the stamp_processed from every entry that started being proccesed before tim
func reprocess(db *sql.DB, cfg *Config, tim time.Time) (err error) {
	stmt, err := db.Prepare("UPDATE " + cfg.RawTable + " SET stamp_processed = NULL WHERE stamp_processed < ?")
	if err != nil {
		return
	}
	_, err = stmt.Exec(tim)

	return
}

//Removes all entries in the database that started being processed before tim
func purgeAllProcessed(db *sql.DB, cfg *Config, tim time.Time) (err error) {
	stmt, err := db.Prepare("DELETE FROM " + cfg.RawTable + " WHERE stamp_processed < ? ")
	if err != nil {
		return
	}
	_, err = stmt.Exec(tim)

	return
}

//Removes all Rawdata that is in rDat from the database
func purgeRawData(tx *sql.Tx, cfg *Config, rDat []RawData) (err error) {
	prepStmt, err := tx.Prepare("DELETE FROM " + cfg.RawTable + " WHERE ip_src = ? AND ip_dst = ? AND as_src = ? AND as_dst = ? AND port_src = ? AND port_dst = ? AND packets = ? AND pkt_len_distrib = ? AND stamp_processed IS NOT NULL LIMIT 1")
	if err != nil {
		return
	}

	for _, r := range rDat {
		_, err = prepStmt.Exec(r.Ip_src, r.Ip_dst, r.As_src, r.As_dst, r.Port_src, r.Port_dst, r.Packets, r.Pkt_len_distrib)
		if err != nil {
			return
		}
	}
	return
}

func insertCleanData(tx *sql.Tx, cfg *Config, cd []cleanedData) error {
	prepStmt, err := tx.Prepare("INSERT INTO " + cfg.CleanTable + " (ipb_src, ipb_dst, as_src, as_dst, port_src, port_dst, occurences, volume, time_added) VALUES ( ? , ? , ? , ? , ? , ? , ? , ? , ? ) ON DUPLICATE KEY UPDATE occurences = occurences + ?")
	if err != nil {
		slogger.Println("Failed to prepare statement")
		return err
	}

	for ix := range cd {
		_, err = prepStmt.Exec(cd[ix].ipbSrc, cd[ix].ipbDst, cd[ix].asSrc, cd[ix].asDst, cd[ix].portSrc, cd[ix].portDst, cd[ix].occurences, cd[ix].volume, cd[ix].time, cd[ix].occurences)
		if err != nil {
			slogger.Println("Failed to execute statement")
			return err
		}
	}

	return err
}

func insertCleanDataToDB(cfg *Config, cd []cleanedData) error {
	db, err := sql.Open("mysql", cfg.DBUser+":"+cfg.DBPass+"@"+cfg.DBConn+"/"+cfg.DBName)
	if err != nil {
		slogger.Println("Failed to connect to db")
		return err
	}
	defer db.Close()

	prepStmt, err := db.Prepare("INSERT INTO " + cfg.CleanTable + " (ipb_src, ipb_dst, as_src, as_dst, port_src, port_dst, occurences, volume, time_added) VALUES ( ? , ? , ? , ? , ? , ? , ? , ? , ? ) ON DUPLICATE KEY UPDATE occurences = occurences + ?")
	if err != nil {
		slogger.Println("Failed to prepare statement")
		return err
	}

	for ix := range cd {
		_, err = prepStmt.Exec(cd[ix].ipbSrc, cd[ix].ipbDst, cd[ix].asSrc, cd[ix].asDst, cd[ix].portSrc, cd[ix].portDst, cd[ix].occurences, cd[ix].volume, cd[ix].time, cd[ix].occurences)
		if err != nil {
			slogger.Println("Failed to execute statement")
			return err
		}
	}
	return nil
}

func insertASNIP(db *sql.DB, asn int, ipBlock string) error {
	prepCheck, err := db.Prepare("SELECT asn FROM asnip WHERE ip_block = ?")
	if err != nil {
		return err
	}
	defer prepCheck.Close()

	rows, err := prepCheck.Exec(ipBlock)
	if err != nil {
		return err
	}
	if rows != nil {
		return nil
	}

	prepIns, err := db.Prepare("INSERT INTO asnip VALUES ( ? , ? )")
	if err != nil {
		return err
	}
	defer prepIns.Close()

	_, err = prepIns.Exec(asn, ipBlock)
	if err != nil {
		return err
	}

	return nil
}

func removeASNIP(db *sql.DB, asn int, ipBlock string) error {
	prepStmt, err := db.Prepare("DELETE FROM anip WHERE asn = ? AND ip_block = ?")
	if err != nil {
		return err
	}
	defer prepStmt.Close()

	_, err = prepStmt.Exec(asn, ipBlock)
	if err != nil {
		return err
	}

	return nil
}

// Adds differential privacy to all entries in the
// database that is older than t and haven't had
// differential privacy added to them yet.
func privatizeCleaned(db *sql.DB, t time.Time, cfg *Config) (err error) {
	if cfg.Epsilon <= 0 {
		return
	}
	query, err := db.Prepare("SELECT ipb_src,ipb_dst,as_src,as_dst,port_src,port_dst,volume,time_added,occurences FROM " + cfg.CleanTable + " WHERE time_added < ?")
	if err != nil {
		slogger.Println("Failed to prepare query")
		return
	}

	rows, err := query.Query(t)
	if err != nil {
		slogger.Println("Failed to query for unprivitized rows")
		return
	}
	defer rows.Close()

	update, err := db.Prepare("UPDATE " + cfg.CleanTable + " SET occurences = ? , time_privatized = ? WHERE ipb_src = ? AND ipb_dst = ? AND as_src = ? AND as_dst = ? AND port_src = ? AND port_dst = ? AND volume = ? AND time_added = ? ")
	if err != nil {
		slogger.Println("Failed to prepare update")
		return
	}

	loc, err := time.LoadLocation(TIMEZONE)
	if err != nil {
		slogger.Println("Couldn't load timezone")
		return
	}
	var cd cleanedData
	for rows.Next() {
		var tim []byte
		err = rows.Scan(&cd.ipbSrc, &cd.ipbDst, &cd.asSrc, &cd.asDst, &cd.portSrc, &cd.portDst, &cd.volume, &tim, &cd.occurences)
		if err != nil {
			slogger.Println("Failed to scan row")
			return
		}
		cd.time, err = time.ParseInLocation("2006-01-02 15:04:05", string(tim), loc)
		if err != nil {
			slogger.Println("Failed to parse timestamp")
			return
		}
		// Add differential privacy noise
		cd.occurences = diffpriv(cd.occurences, 1, cfg.Epsilon)

		// Update the entry
		_, err := update.Exec(cd.occurences, time.Now(), cd.ipbSrc, cd.ipbDst, cd.asSrc, cd.asDst, cd.portSrc, cd.portDst, cd.volume, cd.time)
		if err != nil {
			slogger.Println("Failed to update an entry:", err)
		}
	}
	return
}

func availableRows(tx *sql.Tx, cfg *Config, timeLimit time.Time) (numRows int, err error) {
	stmt, err := tx.Prepare("SELECT COUNT(*) FROM " + cfg.RawTable + " WHERE stamp_inserted < ? ")
	if err != nil {
		slogger.Println("Could not prepare statement")
		return
	}
	row := stmt.QueryRow(timeLimit)

	err = row.Scan(&numRows)
	if err != nil {
		slogger.Println("Failed to scan result")
	}
	return
}