Browse Source

Fix detection of non-encrypted streams

Georgi Chorbadzhiyski 7 years ago
parent
commit
0113f790c2
6 changed files with 41 additions and 23 deletions
  1. 6
    8
      camd.c
  2. 3
    0
      data.c
  3. 3
    1
      data.h
  4. 18
    11
      process.c
  5. 2
    2
      tsdecrypt.1
  6. 9
    1
      tsdecrypt.c

+ 6
- 8
camd.c View File

@@ -170,10 +170,6 @@ static int camd_recv_cw(struct ts *ts) {
170 170
 #undef ERR
171 171
 
172 172
 static int camd_send_ecm(struct ts *ts, struct camd_msg *msg) {
173
-	if (!ts->stream_is_encrypted) {
174
-		return 0;
175
-	}
176
-
177 173
 	struct camd *c = &ts->camd;
178 174
 	int ret = c->ops.do_ecm(c, msg);
179 175
 	if (ret <= 0) {
@@ -189,10 +185,12 @@ static int camd_send_ecm(struct ts *ts, struct camd_msg *msg) {
189 185
 		ts->is_cw_error = 1;
190 186
 		if (ts->key.ts && now - ts->key.ts > KEY_VALID_TIME) {
191 187
 			if (c->key->is_valid_cw) {
192
-				notify(ts, "NO_CODE_WORD", "No code word was set in %ld sec. Decryption is disabled.",
193
-					now - ts->key.ts);
194
-				ts_LOGf("CW  | *ERR* No valid code word was received in %ld seconds. Decryption is disabled.\n",
195
-					now - ts->key.ts);
188
+				if (!ts->stream_is_not_scrambled) {
189
+					notify(ts, "NO_CODE_WORD", "No code word was set in %ld sec. Decryption is disabled.",
190
+						now - ts->key.ts);
191
+					ts_LOGf("CW  | *ERR* No valid code word was received in %ld seconds. Decryption is disabled.\n",
192
+						now - ts->key.ts);
193
+				}
196 194
 				ts->cw_last_warn = time(NULL);
197 195
 				ts->cw_next_warn = ts->cw_last_warn + ts->cw_warn_sec;
198 196
 				ts->cw_next_warn -= now - ts->key.ts;

+ 3
- 0
data.c View File

@@ -85,6 +85,9 @@ void data_init(struct ts *ts) {
85 85
 	ts->ecm_report_interval = 60;
86 86
 	ts->ecm_last_report     = time(NULL);
87 87
 
88
+	ts->last_scrambled_packet_ts     = time(NULL);
89
+	ts->last_not_scrambled_packet_ts = time(NULL);
90
+
88 91
 	ts->irdeto_ecm_idx         = 0;
89 92
 	ts->irdeto_ecm_filter_type = IRDETO_FILTER_IDX;
90 93
 

+ 3
- 1
data.h View File

@@ -276,8 +276,10 @@ struct ts {
276 276
 	time_t				cw_next_warn;
277 277
 	struct timeval		ecm_change_time;
278 278
 
279
-	unsigned int		stream_is_encrypted;
279
+	unsigned int		stream_is_not_scrambled;
280 280
 	time_t				last_scrambled_packet_ts;
281
+	time_t				last_not_scrambled_packet_ts;
282
+	time_t				last_not_scrambled_report_ts;
281 283
 
282 284
 	unsigned int		pid_report;
283 285
 	unsigned int		pid_stats[MAX_PIDS];

+ 18
- 11
process.c View File

@@ -130,11 +130,25 @@ static void decode_buffer(struct ts *ts, uint8_t *data, int data_len) {
130 130
 		uint8_t *ts_packet = data + (i * 188);
131 131
 
132 132
 		uint16_t pid = ts_packet_get_pid(ts_packet);
133
-		if (pidmap_get(&ts->pidmap, pid) && ts_packet_is_scrambled(ts_packet)) {
134
-			if (ts_packet_is_scrambled(ts_packet) && ts->last_scrambled_packet_ts != now) {
135
-				ts->stream_is_encrypted = 1;
136
-				ts->last_scrambled_packet_ts = now;
133
+		bool in_pidmap = pidmap_get(&ts->pidmap, pid);
134
+		bool is_scrambled = ts_packet_is_scrambled(ts_packet);
135
+		if (in_pidmap) {
136
+			if (is_scrambled) {
137
+				if (ts->last_scrambled_packet_ts < now) {
138
+					ts->stream_is_not_scrambled = 0;
139
+					ts->last_scrambled_packet_ts = now;
140
+				}
141
+			} else {
142
+				if (now - 5 >= ts->last_scrambled_packet_ts) {
143
+					if (ts->last_not_scrambled_packet_ts < now) {
144
+						ts->camd.key->is_valid_cw = 0;
145
+						ts->stream_is_not_scrambled = 1;
146
+						ts->last_not_scrambled_packet_ts = now;
147
+					}
148
+				}
137 149
 			}
150
+		}
151
+		if (in_pidmap && is_scrambled) {
138 152
 			if (ts->key.is_valid_cw) {
139 153
 				int scramble_idx = ts_packet_get_scrambled(ts_packet);
140 154
 				if (!scramble_idx_old)
@@ -181,13 +195,6 @@ static void decode_buffer(struct ts *ts, uint8_t *data, int data_len) {
181 195
 		}
182 196
 	}
183 197
 
184
-	if (ts->last_scrambled_packet_ts && ts->last_scrambled_packet_ts < now - 5) {
185
-		ts_LOGf("N/E | No encrypted packet came during the last 5 seconds, disabling NO_CODE_WORD notifcation\n");
186
-		notify(ts, "STREAM_NOT_ENCRYPTED", "No encrypted packets were seen in the last 5 seconds.");
187
-		ts->stream_is_encrypted = 0;
188
-		ts->last_scrambled_packet_ts = 0;
189
-	}
190
-
191 198
 	// Decode packets
192 199
 	if (even_packets) {
193 200
 		if (use_dvbcsa) {

+ 2
- 2
tsdecrypt.1 View File

@@ -344,8 +344,8 @@ currently defined events are:
344 344
                   the stream is encrypted and there are no new code words
345 345
                   from the CAMD server.
346 346
 
347
-  \fBSTREAM_NOT_ENCRYPTED\fR    No encrypted packets arrived in the input stream
348
-                  for the last 5 seconds. The stream is probably not encrypted.
347
+  \fBSTREAM_CLEAR\fR    No encrypted packets arrived in the input stream.
348
+                  The stream is not encrypted and clear to view.
349 349
 
350 350
   \fBNO_EMM_RECEIVED\fR    No EMM packet have been received for X seconds.
351 351
 

+ 9
- 1
tsdecrypt.c View File

@@ -861,6 +861,8 @@ static void report_emms(struct ts *ts, time_t now) {
861 861
 }
862 862
 
863 863
 static void report_ecms(struct ts *ts, time_t now) {
864
+	if (ts->stream_is_not_scrambled && ts->ecm_seen_count == 0)
865
+		return;
864 866
 	ts_LOGf("ECM | Received %u (%u dup) and processed %u in %lu seconds.\n",
865 867
 		ts->ecm_seen_count,
866 868
 		ts->ecm_duplicate_count,
@@ -873,6 +875,8 @@ static void report_ecms(struct ts *ts, time_t now) {
873 875
 }
874 876
 
875 877
 static void report_cw_warn(struct ts *ts, time_t now) {
878
+	if (ts->stream_is_not_scrambled)
879
+		return;
876 880
 	if (now - ts->key.ts > 1) {
877 881
 		notify(ts, "NO_CODE_WORD", "No valid code word was received in %ld sec.",
878 882
 			now - ts->key.ts);
@@ -905,7 +909,11 @@ static void do_reports(struct ts *ts) {
905 909
 			report_ecms(ts, now);
906 910
 		}
907 911
 	}
908
-
912
+	if (ts->stream_is_not_scrambled && ts->last_not_scrambled_report_ts <= now - 60) {
913
+		ts_LOGf("CLR | No encrypted packets in the last %ld seconds. Stream is clear.\n", now - ts->last_scrambled_packet_ts);
914
+		notify(ts, "STREAM_CLEAR", "No encrypted packets in the last %ld seconds. Stream is clear.", now - ts->last_scrambled_packet_ts);
915
+		ts->last_not_scrambled_report_ts = now;
916
+	}
909 917
 	if (ts->process_ecm && !ts->key.is_valid_cw) {
910 918
 		if (ts->cw_warn_sec && now >= ts->cw_next_warn) {
911 919
 			report_cw_warn(ts, now);

Loading…
Cancel
Save