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
 #undef ERR
170
 #undef ERR
171
 
171
 
172
 static int camd_send_ecm(struct ts *ts, struct camd_msg *msg) {
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
 	struct camd *c = &ts->camd;
173
 	struct camd *c = &ts->camd;
178
 	int ret = c->ops.do_ecm(c, msg);
174
 	int ret = c->ops.do_ecm(c, msg);
179
 	if (ret <= 0) {
175
 	if (ret <= 0) {
189
 		ts->is_cw_error = 1;
185
 		ts->is_cw_error = 1;
190
 		if (ts->key.ts && now - ts->key.ts > KEY_VALID_TIME) {
186
 		if (ts->key.ts && now - ts->key.ts > KEY_VALID_TIME) {
191
 			if (c->key->is_valid_cw) {
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
 				ts->cw_last_warn = time(NULL);
194
 				ts->cw_last_warn = time(NULL);
197
 				ts->cw_next_warn = ts->cw_last_warn + ts->cw_warn_sec;
195
 				ts->cw_next_warn = ts->cw_last_warn + ts->cw_warn_sec;
198
 				ts->cw_next_warn -= now - ts->key.ts;
196
 				ts->cw_next_warn -= now - ts->key.ts;

+ 3
- 0
data.c View File

85
 	ts->ecm_report_interval = 60;
85
 	ts->ecm_report_interval = 60;
86
 	ts->ecm_last_report     = time(NULL);
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
 	ts->irdeto_ecm_idx         = 0;
91
 	ts->irdeto_ecm_idx         = 0;
89
 	ts->irdeto_ecm_filter_type = IRDETO_FILTER_IDX;
92
 	ts->irdeto_ecm_filter_type = IRDETO_FILTER_IDX;
90
 
93
 

+ 3
- 1
data.h View File

276
 	time_t				cw_next_warn;
276
 	time_t				cw_next_warn;
277
 	struct timeval		ecm_change_time;
277
 	struct timeval		ecm_change_time;
278
 
278
 
279
-	unsigned int		stream_is_encrypted;
279
+	unsigned int		stream_is_not_scrambled;
280
 	time_t				last_scrambled_packet_ts;
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
 	unsigned int		pid_report;
284
 	unsigned int		pid_report;
283
 	unsigned int		pid_stats[MAX_PIDS];
285
 	unsigned int		pid_stats[MAX_PIDS];

+ 18
- 11
process.c View File

130
 		uint8_t *ts_packet = data + (i * 188);
130
 		uint8_t *ts_packet = data + (i * 188);
131
 
131
 
132
 		uint16_t pid = ts_packet_get_pid(ts_packet);
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
 			if (ts->key.is_valid_cw) {
152
 			if (ts->key.is_valid_cw) {
139
 				int scramble_idx = ts_packet_get_scrambled(ts_packet);
153
 				int scramble_idx = ts_packet_get_scrambled(ts_packet);
140
 				if (!scramble_idx_old)
154
 				if (!scramble_idx_old)
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
 	// Decode packets
198
 	// Decode packets
192
 	if (even_packets) {
199
 	if (even_packets) {
193
 		if (use_dvbcsa) {
200
 		if (use_dvbcsa) {

+ 2
- 2
tsdecrypt.1 View File

344
                   the stream is encrypted and there are no new code words
344
                   the stream is encrypted and there are no new code words
345
                   from the CAMD server.
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
   \fBNO_EMM_RECEIVED\fR    No EMM packet have been received for X seconds.
350
   \fBNO_EMM_RECEIVED\fR    No EMM packet have been received for X seconds.
351
 
351
 

+ 9
- 1
tsdecrypt.c View File

861
 }
861
 }
862
 
862
 
863
 static void report_ecms(struct ts *ts, time_t now) {
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
 	ts_LOGf("ECM | Received %u (%u dup) and processed %u in %lu seconds.\n",
866
 	ts_LOGf("ECM | Received %u (%u dup) and processed %u in %lu seconds.\n",
865
 		ts->ecm_seen_count,
867
 		ts->ecm_seen_count,
866
 		ts->ecm_duplicate_count,
868
 		ts->ecm_duplicate_count,
873
 }
875
 }
874
 
876
 
875
 static void report_cw_warn(struct ts *ts, time_t now) {
877
 static void report_cw_warn(struct ts *ts, time_t now) {
878
+	if (ts->stream_is_not_scrambled)
879
+		return;
876
 	if (now - ts->key.ts > 1) {
880
 	if (now - ts->key.ts > 1) {
877
 		notify(ts, "NO_CODE_WORD", "No valid code word was received in %ld sec.",
881
 		notify(ts, "NO_CODE_WORD", "No valid code word was received in %ld sec.",
878
 			now - ts->key.ts);
882
 			now - ts->key.ts);
905
 			report_ecms(ts, now);
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
 	if (ts->process_ecm && !ts->key.is_valid_cw) {
917
 	if (ts->process_ecm && !ts->key.is_valid_cw) {
910
 		if (ts->cw_warn_sec && now >= ts->cw_next_warn) {
918
 		if (ts->cw_warn_sec && now >= ts->cw_next_warn) {
911
 			report_cw_warn(ts, now);
919
 			report_cw_warn(ts, now);

Loading…
Cancel
Save