Changeset 5361


Ignore:
Timestamp:
Jun 6, 2011, 1:52:08 AM (8 years ago)
Author:
Admin
Message:

Some internal restructuring regarding globals.h file to make it a bit more structured.

Location:
trunk
Files:
1 added
1 deleted
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/globals.h

    r5356 r5361  
    1 #define _GNU_SOURCE //prevents "implicit" warning for asprintf
    21#include <stdlib.h>
    32#include <stdio.h>
     
    1918#include <pwd.h>
    2019#include <netinet/tcp.h>
    21 
    2220#include <sys/socket.h>
    2321#include <netinet/in.h>
     
    2725#include <signal.h>
    2826#include <errno.h>
    29 
    3027#include <pthread.h>
    3128
     
    3532#include "cscrypt/idea.h"
    3633
     34#include "oscam-config.h"
     35
     36#ifndef USE_CMAKE
     37#  include "oscam-ostype.h"
     38#endif
     39#include "oscam-types.h"
     40#include "cscrypt/cscrypt.h"
     41
     42#ifdef HAVE_PCSC
     43  #ifdef OS_CYGWIN32
     44    #define __reserved
     45    #define __nullnullterminated
     46    #include <specstrings.h>
     47    #include "cygwin/WinSCard.h"
     48  #else
     49    #include <PCSC/pcsclite.h>
     50    #ifdef OS_MACOSX
     51        #include <PCSC/wintypes.h>
     52    #else
     53        #include <PCSC/reader.h>
     54    #endif
     55  #endif
     56#endif
     57
     58#if defined(LIBUSB)
     59#ifdef __FreeBSD__
     60#include <libusb.h>
     61#else
     62#include <libusb-1.0/libusb.h>
     63#endif
     64#include "csctapi/smartreader_types.h"
     65#endif
     66
     67/* ===========================
     68 *         macros
     69 * =========================== */
    3770// Prevent use of unsafe functions (doesn't work for MacOSX)
    3871#ifndef OS_MACOSX
     
    4174#define strtok(a,b,c) UNSAFE_STRTOK_USE_STRTOK_R_INSTEAD()
    4275#endif
    43 
    44 #ifndef CS_GLOBALS
    45 #define CS_GLOBALS
    46 #define CS_VERSION    "1.00-unstable_svn"
    47 #ifndef CS_SVN_VERSION
    48 #   define CS_SVN_VERSION "test"
    49 #endif
    50 
    51 #include "oscam-config.h"
    5276
    5377#ifdef UNUSED
     
    77101#endif
    78102
    79 #ifndef USE_CMAKE
    80 #  include "oscam-ostype.h"
    81 #endif
    82 #include "oscam-types.h"
    83 #include "cscrypt/cscrypt.h"
    84 
    85 #ifdef HAVE_PCSC
    86   #ifdef OS_CYGWIN32
    87     #define __reserved
    88     #define __nullnullterminated
    89     #include <specstrings.h>
    90     #include "cygwin/WinSCard.h"
    91   #else
    92     #include <PCSC/pcsclite.h>
    93     #ifdef OS_MACOSX
    94         #include <PCSC/wintypes.h>
    95     #else
    96         #include <PCSC/reader.h>
    97     #endif
    98   #endif
    99 #endif
    100 
    101 #if defined(LIBUSB)
    102 #ifdef __FreeBSD__
    103 #include <libusb.h>
    104 #else
    105 #include <libusb-1.0/libusb.h>
    106 #endif
    107 #include "csctapi/smartreader_types.h"
    108 #endif
    109 
    110 // DM500 and Dbox2 have toolchains which don't match their plattform. Therefore pthread_cleanup_push can't be used (binaries compile but only work on DM 7020)
    111 #if defined(TUXBOX) && defined(PPC)
    112 #define NO_PTHREAD_CLEANUP_PUSH
    113 #endif
    114 
     103//checking if (X) free(X) unneccessary since freeing a null pointer doesnt do anything
     104#define NULLFREE(X) {if (X) {void *tmpX=X; X=NULL; free(tmpX); }}
     105
     106/* ===========================
     107 *         constants
     108 * =========================== */
     109#ifndef CS_GLOBALS
     110#define CS_GLOBALS
     111#define CS_VERSION    "1.00-unstable_svn"
     112#ifndef CS_SVN_VERSION
     113#   define CS_SVN_VERSION "test"
     114#endif
    115115#ifndef CS_CONFDIR
    116116#define CS_CONFDIR    "/usr/local/etc"
     
    148148
    149149#define CS_EMMCACHESIZE  64 //nr of EMMs that each client will cache; cache is per client, so memory-expensive...
     150#define MSGLOGSIZE 64   //size of string buffer for a ecm to return messages
    150151
    151152#define D_TRACE     1 // Generate very detailed error/trace messages per routine
     
    217218#define MOD_ADDON       32
    218219
    219 
    220220#ifdef HAVE_DVBAPI
    221221#define BOXTYPE_DREAMBOX    1
     
    238238#define EMM_UNKNOWN 8
    239239
     240//EMM types:
     241#define UNKNOWN 0
     242#define UNIQUE  1
     243#define SHARED  2
     244#define GLOBAL  3
     245
    240246#ifdef CS_CORE
    241247char *PIP_ID_TXT[] = { "ECM", "EMM", "CIN", "KCL", "UDP", NULL  };
     
    256262#define PIP_ID_UDP    4
    257263#define PIP_ID_MAX    PIP_ID_UDP
    258 
    259 
    260264#define PIP_ID_ERR    (-1)
    261265#define PIP_ID_DIR    (-2)
     
    294298      E2_CCCAM_NOCARD=0x27, E2_CCCAM_NOK1=0x28, E2_CCCAM_NOK2=0x29, E2_CCCAM_LOOP=0x30};
    295299
    296 pid_t server_pid; //alno: PID of server - set while startup
    297 
    298 // constants
    299300#define CTA_RES_LEN 512
    300301
     
    313314
    314315#ifdef QBOXHD_LED
    315 typedef struct {
    316     uint16_t H;  // range 0-359
    317     unsigned char S;   // range 0-99
    318     unsigned char V;   // range 0-99
    319 } qboxhd_led_color_struct;
    320 typedef struct {
    321     unsigned char red;  // first 5 bit used (&0x1F)
    322     unsigned char green; // first 5 bit used (&0x1F)
    323     unsigned char blue; // first 5 bit used (&0x1F)
    324 } qboxhdmini_led_color_struct;
    325 
    326 extern void qboxhd_led_blink(int32_t color, int32_t duration);
    327 
    328316#define QBOXHD_LED_DEVICE               "/dev/sw0"
    329317#define QBOXHD_SET_LED_ALL_PANEL_COLOR  _IO(0xBC, 13)    // payload = 3byte [H][S][V]
     
    362350#define BAN_DUPLICATE 8         //failban mask for duplicate user
    363351
    364 //checking if (X) free(X) unneccessary since freeing a null pointer doesnt do anything
    365 #define NULLFREE(X) {if (X) {void *tmpX=X; X=NULL; free(tmpX); }}
    366 
     352#define AVAIL_CHECK_CONNECTED 0
     353#define AVAIL_CHECK_LOADBALANCE 1
     354
     355#define LB_MAX_STAT_TIME 10
     356
     357/* ===========================
     358 *      global structures
     359 * =========================== */
    367360typedef struct s_caidvaluetab
    368361{
     
    469462    time_t v_time;
    470463} V_BAN;
    471 
    472 #define AVAIL_CHECK_CONNECTED 0
    473 #define AVAIL_CHECK_LOADBALANCE 1
    474464
    475465struct s_client ;
     
    558548#endif
    559549
    560 #define MSGLOGSIZE 64
    561550typedef struct ecm_request_t
    562551{
     
    564553  uchar         cw[16];
    565554  uchar         ecmd5[CS_ECMSTORESIZE];
    566 //  uchar         l;
    567555  int16_t         l;
    568556  uint16_t        caid;
     
    11121100    struct s_auth   *account;
    11131101    struct s_srvid  *srvid[16];
    1114         struct s_tierid *tierid;
     1102    struct s_tierid *tierid;
    11151103    //Todo #ifdef CCCAM
    11161104    struct s_provid *provid;
     
    12691257};
    12701258
    1271 //Loadbalance constants:
    1272 #define LB_NONE 0
    1273 #define LB_FASTEST_READER_FIRST 1
    1274 #define LB_OLDEST_READER_FIRST 2
    1275 #define LB_LOWEST_USAGELEVEL 3
    1276 #define LB_LOG_ONLY 10
    1277 
    1278 #define LB_MAX_STAT_TIME 10
    1279 
    12801259typedef struct reader_stat_t
    12811260{
     
    13071286} EMM_PACKET;
    13081287
    1309 //EMM types:
    1310 #define UNKNOWN 0
    1311 #define UNIQUE  1
    1312 #define SHARED  2
    1313 #define GLOBAL  3
    1314 
    1315 // oscam-simples
    1316 extern char *remote_txt(void);
    1317 extern char *trim(char *);
    1318 extern char *strtolower(char *);
    1319 extern int32_t gethexval(char);
    1320 extern int32_t cs_atob(uchar *, char *, int32_t);
    1321 extern uint32_t cs_atoi(char *, int32_t, int32_t);
    1322 extern int32_t byte_atob(char *);
    1323 extern int32_t word_atob(char *);
    1324 extern int32_t dyn_word_atob(char *asc);
    1325 extern int32_t key_atob_l(char *, uchar *, int32_t);
    1326 extern char *key_btoa(char *, uchar *);
    1327 extern char *cs_hexdump(int32_t, const uchar *, int32_t);
    1328 extern char *cs_hexdump_buf(int32_t, const uchar *, int32_t, char *target, int32_t len);
    1329 extern in_addr_t cs_inet_order(in_addr_t);
    1330 extern char *cs_inet_ntoa(in_addr_t);
    1331 extern in_addr_t cs_inet_addr(char *txt);
    1332 extern uint32_t b2i(int32_t, uchar *);
    1333 extern uint64_t b2ll(int32_t, uchar *);
    1334 extern uchar *i2b_buf(int32_t n, uint32_t i, uchar *b);
    1335 
    1336 extern uint32_t a2i(char *, int32_t);
    1337 extern int32_t boundary(int32_t, int32_t);
    1338 extern void cs_ftime(struct timeb *);
    1339 extern void cs_sleepms(uint32_t);
    1340 extern void cs_sleepus(uint32_t);
    1341 extern void cs_setpriority(int32_t);
    1342 extern struct s_auth *find_user(char *);
    1343 extern int32_t check_filled(uchar *value, int32_t length);
    1344 extern void *cs_malloc(void *result, size_t size, int32_t quiterror);
    1345 extern void *cs_realloc(void *result, size_t size, int32_t quiterror);
    1346 #ifdef WEBIF
    1347 extern char to_hex(char code);
    1348 extern void char_to_hex(const unsigned char* p_array, uint32_t p_array_len, unsigned char *result);
    1349 extern void create_rand_str(char *dst, int32_t size);
    1350 #endif
    1351 extern int32_t file_exists(const char * filename);
    1352 extern void clear_sip(struct s_ip **sip);
    1353 extern void clear_ptab(struct s_ptab *ptab);
    1354 extern void clear_ftab(struct s_ftab *ftab);
    1355 void clear_caidtab(struct s_caidtab *ctab);
    1356 void clear_tuntab(struct s_tuntab *ttab);
    1357 extern int32_t file_copy(char *srcfile, char *destfile);
    1358 extern int32_t safe_overwrite_with_bak(char *destfile, char *tmpfile, char *bakfile, int32_t forceBakOverWrite);
    1359 extern void fprintf_conf(FILE *f, int32_t varnameWidth, const char *varname, const char *fmtstring, ...);
    1360 extern void cs_strncpy(char * destination, const char * source, size_t num);
    1361 extern char *get_servicename(struct s_client *cl, int32_t srvid, int32_t caid, char *buf);
    1362 extern char *get_tiername(int32_t tierid, int32_t caid, char *buf);
    1363 extern char *get_provider(int32_t caid, uint32_t provid, char *buf);
    1364 extern void make_non_blocking(int32_t fd);
    1365 extern uchar fast_rnd(void);
    1366 extern void init_rnd(void);
    1367 extern int32_t hexserialset(struct s_reader *rdr);
    1368 extern char *monitor_get_proto(struct s_client *);
    1369 extern char *reader_get_type_desc(struct s_reader * rdr, int32_t extended);
    1370 extern char *get_ncd_client_name(char *client_id);
    1371 extern int32_t cs_strnicmp(const char * str1, const char * str2, size_t num);
    1372 extern char *strnew(char *str);
    1373 extern void hexserial_to_newcamd(uchar *source, uchar *dest, uint16_t caid);
    1374 extern void newcamd_to_hexserial(uchar *source, uchar *dest, uint16_t caid);
    1375 extern int32_t check_ip(struct s_ip *ip, in_addr_t n);
    1376 #ifdef WITH_MUTEXDEBUG
    1377 extern int32_t cs_lock_debug(pthread_mutex_t *mutex, char *file, uint16_t line);
    1378 extern int32_t cs_trylock_debug(pthread_mutex_t *mutex, char *file, uint16_t line);
    1379 extern int32_t cs_unlock_debug(pthread_mutex_t *mutex, char *file, uint16_t line);
    1380 #define cs_lock(x) cs_lock_debug(x,__FILE__, __LINE__)
    1381 #define cs_trylock(x) cs_trylock_debug(x,__FILE__, __LINE__)
    1382 #define cs_unlock(x) cs_unlock_debug(x,__FILE__, __LINE__)
    1383 #else
    1384 extern int32_t cs_lock(pthread_mutex_t *mutex);
    1385 extern int32_t cs_trylock(pthread_mutex_t *mutex);
    1386 extern int32_t cs_unlock(pthread_mutex_t *mutex);
    1387 #endif
    1388 extern void cs_cleanlocks();
    1389 extern uint32_t cs_getIPfromHost(const char *hostname);
    1390 
     1288#ifdef QBOXHD_LED
     1289typedef struct {
     1290    uint16_t H;  // range 0-359
     1291    unsigned char S;   // range 0-99
     1292    unsigned char V;   // range 0-99
     1293} qboxhd_led_color_struct;
     1294typedef struct {
     1295    unsigned char red;  // first 5 bit used (&0x1F)
     1296    unsigned char green; // first 5 bit used (&0x1F)
     1297    unsigned char blue; // first 5 bit used (&0x1F)
     1298} qboxhdmini_led_color_struct;
     1299#endif
     1300
     1301
     1302/* ===========================
     1303 *      global variables
     1304 * =========================== */
     1305extern char cs_tmpdir[200];
    13911306extern pthread_key_t getclient;
    1392 extern struct s_client * cur_client(void);
    13931307extern struct s_client *first_client;
    13941308extern struct s_reader *first_active_reader; //points to list of _active_ readers (enable = 1, deleted = 0)
    13951309extern LLIST *configured_readers;
    1396 
    1397 // oscam variables
    1398 
    13991310extern int32_t cs_dblevel;
    1400 
    14011311extern uint16_t len4caid[256];
    1402 
    1403 extern struct card_struct *Cards;
    1404 //extern struct idstore_struct *idstore;
    1405 extern uint32_t *IgnoreList;
    1406 
    14071312extern struct s_config cfg;
    14081313extern char cs_confdir[];
     
    14111316extern struct s_cardsystem cardsystem[CS_MAX_MOD];
    14121317extern struct s_cardreader cardreader[CS_MAX_MOD];
    1413 //extern ECM_REQUEST *ecmtask;
     1318extern pthread_mutex_t gethostbyname_lock;
     1319
     1320pid_t server_pid; // PID of server - set while startup
    14141321
    14151322#ifdef CS_ANTICASC
    14161323extern FILE *fpa;
    14171324#endif
    1418 extern pthread_mutex_t gethostbyname_lock;
    1419 
    1420 // oscam
    1421 #ifdef WEBIF
    1422 extern void cs_exit_oscam();
    1423 extern void cs_restart_oscam();
    1424 extern int32_t cs_get_restartmode();
    1425 
    1426 //reset stats for webif:
    1427 extern void clear_account_stats(struct s_auth *account);
    1428 extern void clear_all_account_stats();
    1429 extern void clear_system_stats();
    1430 
    1431 #endif
    1432 extern void start_thread(void * startroutine, char * nameroutine);
    1433 extern void cs_reload_config();
    1434 extern int32_t recv_from_udpipe(uchar *);
    1435 extern char* username(struct s_client *);
    1436 extern struct s_client * get_client_by_tid(uint32_t);
    1437 extern int32_t chk_bcaid(ECM_REQUEST *, CAIDTAB *);
    1438 extern void cs_exit(int32_t sig);
    1439 extern int32_t comp_timeb(struct timeb *tpa, struct timeb *tpb);
    1440 extern struct s_client * create_client(in_addr_t);
    1441 extern int32_t cs_auth_client(struct s_client *, struct s_auth *, const char*);
    1442 extern void cs_disconnect_client(struct s_client *);
    1443 extern int32_t check_cwcache2(ECM_REQUEST *, uint64_t grp);
    1444 extern int32_t write_to_pipe(int32_t, int32_t, uchar *, int32_t);
    1445 extern int32_t read_from_pipe(int32_t, uchar **);
    1446 extern int32_t write_ecm_answer(struct s_reader *, ECM_REQUEST *);
    1447 extern void log_emm_request(struct s_reader *);
    1448 extern uint32_t chk_provid(uchar *, uint16_t);
    1449 extern void convert_to_beta(struct s_client *cl, ECM_REQUEST *er, uint16_t caidto);
    1450 #ifdef IRDETO_GUESSING
    1451 extern void guess_irdeto(ECM_REQUEST *);
    1452 #endif
    1453 extern void get_cw(struct s_client *, ECM_REQUEST *);
    1454 extern void do_emm(struct s_client *, EMM_PACKET *);
    1455 extern ECM_REQUEST *get_ecmtask(void);
    1456 extern void request_cw(ECM_REQUEST *, int32_t, int32_t);
    1457 extern void send_reader_stat(struct s_reader *rdr, ECM_REQUEST *er, int32_t rc);
    1458 extern int32_t send_dcw(struct s_client *, ECM_REQUEST *);
    1459 extern int32_t process_input(uchar *, int32_t, int32_t);
    1460 extern int32_t has_srvid(struct s_client *cl, ECM_REQUEST *er);
    1461 extern int32_t chk_srvid(struct s_client *, ECM_REQUEST *);
    1462 extern int32_t chk_srvid_match(ECM_REQUEST *, SIDTAB *);
    1463 extern int32_t chk_sfilter(ECM_REQUEST *, PTAB*);
    1464 extern int32_t chk_ufilters(ECM_REQUEST *);
    1465 extern int32_t chk_rsfilter(struct s_reader * reader, ECM_REQUEST *);
    1466 extern int32_t matching_reader(ECM_REQUEST *, struct s_reader *);
    1467 extern int32_t emm_reader_match(struct s_reader *reader, uint16_t caid, uint32_t provid);
    1468 extern void set_signal_handler(int32_t , int32_t , void (*));
    1469 extern void cs_log_config(void);
    1470 extern void cs_waitforcardinit(void);
    1471 extern void cs_reinit_clients(struct s_auth *new_accounts);
    1472 extern int32_t process_client_pipe(struct s_client *cl, uchar *buf, int32_t l);
    1473 extern void update_reader_config(uchar *ptr);
    1474 extern int32_t chk_ctab(uint16_t caid, CAIDTAB *ctab);
    1475 extern int32_t chk_srvid_by_caid_prov(struct s_client *, uint16_t caid, uint32_t provid);
    1476 extern void nullclose(int32_t *fd);
    1477 extern void *clientthread_init(void * init);
    1478 extern void cleanup_thread(void *var);
    1479 extern void kill_thread(struct s_client *cl);
    1480 extern void remove_reader_from_active(struct s_reader *rdr);
    1481 extern void add_reader_to_active(struct s_reader *rdr);
    1482 extern int32_t get_threadnum(struct s_client *client);
    1483 extern void cs_add_violation(uint32_t ip);
    1484 
    1485 extern void cs_card_info(void);
    1486 extern void cs_debug_level(void);
    1487 
    1488 #ifdef CS_ANTICASC
    1489 extern void init_ac(void);
    1490 extern void ac_init_stat();
    1491 extern void ac_clear();
    1492 extern void ac_done_stat();
    1493 extern int32_t  ac_init_log();
    1494 extern void ac_do_stat(void);
    1495 extern void ac_init_client(struct s_client *, struct s_auth *);
    1496 extern void ac_chk(struct s_client *,ECM_REQUEST*, int32_t);
    1497 #endif
    1498 
    1499 // oscam-config
    1500 extern int32_t  init_config(void);
    1501 extern int32_t  init_free_userdb(struct s_auth *auth);
    1502 extern struct s_auth *init_userdb();
    1503 extern int32_t  init_readerdb(void);
    1504 extern void free_reader(struct s_reader *rdr);
    1505 extern int32_t  init_sidtab(void);
    1506 extern void free_sidtab(struct s_sidtab *sidtab);
    1507 extern void init_free_sidtab();
    1508 extern int32_t  init_srvid(void);
    1509 extern int32_t  init_tierid(void);
    1510 extern int32_t  search_boxkey(uint16_t, char *);
    1511 extern void init_len4caid(void);
    1512 #ifdef IRDETO_GUESSING
    1513 extern int32_t  init_irdeto_guess_tab(void);
    1514 #endif
    1515 extern void chk_caidtab(char *caidasc, CAIDTAB *ctab);
    1516 extern void chk_tuntab(char *tunasc, TUNTAB *ttab);
    1517 extern void chk_services(char *labels, SIDTABBITS *sidok, SIDTABBITS *sidno);
    1518 extern void chk_ftab(char *zFilterAsc, FTAB *ftab, const char *zType, const char *zName, const char *zFiltName);
    1519 extern void chk_cltab(char *classasc, CLASSTAB *clstab);
    1520 extern void chk_iprange(char *value, struct s_ip **base);
    1521 extern void chk_port_tab(char *portasc, PTAB *ptab);
    1522 #ifdef CS_ANTICASC
    1523 extern void chk_t_ac(char *token, char *value);
    1524 #endif
    1525 extern void chk_t_camd33(char *token, char *value);
    1526 extern void chk_t_camd35(char *token, char *value);
    1527 extern void chk_t_camd35_tcp(char *token, char *value);
    1528 extern void chk_t_newcamd(char *token, char *value);
    1529 extern void chk_t_radegast(char *token, char *value);
    1530 extern void chk_t_serial(char *token, char *value);
    1531 extern void chk_t_gbox(char *token, char *value);
    1532 #ifdef MODULE_CCCAM
    1533 extern void chk_t_cccam(char *token, char *value);
    1534 #endif
    1535 extern void chk_t_global(const char *token, char *value);
    1536 extern void chk_t_monitor(char *token, char *value);
    1537 extern void chk_reader(char *token, char *value, struct s_reader *rdr);
    1538 
    1539 #ifdef HAVE_DVBAPI
    1540 extern void chk_t_dvbapi(char *token, char *value);
    1541 void dvbapi_chk_caidtab(char *caidasc, char type);
    1542 void dvbapi_read_priority();
    1543 #endif
    1544 
    1545 #ifdef WEBIF
    1546 extern void chk_t_webif(char *token, char *value);
    1547 #endif
    1548 
    1549 #ifdef LCDSUPPORT
    1550 extern void chk_t_lcd(char *token, char *value);
    1551 #endif
    1552 
    1553 extern void cs_accounts_chk(void);
    1554 extern void chk_account(const char *token, char *value, struct s_auth *account);
    1555 extern void chk_sidtab(char *token, char *value, struct s_sidtab *sidtab);
    1556 extern int32_t write_services();
    1557 extern int32_t write_userdb(struct s_auth *authptr);
    1558 extern int32_t write_config();
    1559 extern int32_t write_server();
    1560 extern void write_versionfile();
    1561 extern char *mk_t_caidtab(CAIDTAB *ctab);
    1562 extern char *mk_t_caidvaluetab(CAIDVALUETAB *tab);
    1563 extern char *mk_t_tuntab(TUNTAB *ttab);
    1564 extern char *mk_t_group(uint64_t grp);
    1565 extern char *mk_t_ftab(FTAB *ftab);
    1566 extern char *mk_t_camd35tcp_port();
    1567 extern char *mk_t_cccam_port();
    1568 extern char *mk_t_aeskeys(struct s_reader *rdr);
    1569 extern char *mk_t_newcamd_port();
    1570 extern char *mk_t_aureader(struct s_auth *account);
    1571 extern char *mk_t_nano(struct s_reader *rdr, uchar flag);
    1572 extern char *mk_t_service( uint64_t sidtabok, uint64_t sidtabno);
    1573 extern char *mk_t_logfile();
    1574 extern char *mk_t_iprange(struct s_ip *range);
    1575 extern char *mk_t_ecmwhitelist(struct s_ecmWhitelist *whitelist);
    1576 extern char *mk_t_cltab(CLASSTAB *clstab);
    1577 extern void free_mk_t(char *value);
    1578 
    1579 //Todo #ifdef CCCAM
    1580 extern int32_t init_provid();
    1581 extern char * get_tmp_dir();
    1582 extern void init_share();
    1583 extern void done_share();
    1584 
    1585 // oscam-reader
    1586 extern int32_t logfd;
    1587 extern int32_t reader_cmd2icc(struct s_reader * reader, const uchar *buf, const int32_t l, uchar *response, uint16_t *response_length);
    1588 extern int32_t card_write(struct s_reader * reader, const uchar *, const uchar *, uchar *, uint16_t *);
    1589 extern int32_t check_sct_len(const unsigned char *data, int32_t off);
    1590 extern void cs_ri_brk(struct s_reader * reader, int32_t);
    1591 extern void cs_ri_log(struct s_reader * reader, char *,...);
    1592 extern void * start_cardreader(void *);
    1593 extern void reader_card_info(struct s_reader * reader);
    1594 extern int32_t hostResolve(struct s_reader * reader);
    1595 extern int32_t network_tcp_connection_open();
    1596 extern void network_tcp_connection_close(struct s_client *, int32_t);
    1597 extern int32_t casc_recv_timer(struct s_reader * reader, uchar *buf, int32_t l, int32_t msec);
    1598 extern void clear_reader_pipe(struct s_reader * reader);
    1599 extern void block_connect(struct s_reader *rdr);
    1600 extern int32_t is_connect_blocked(struct s_reader *rdr);
    1601 
    1602 // oscam-log
    1603 extern int32_t  cs_init_log();
    1604 extern void cs_reinit_loghist(uint32_t size);
    1605 extern int32_t cs_open_logfiles();
    1606 extern void cs_write_log(char *);
    1607 extern void cs_log(const char *,...);
    1608 #ifdef WITH_DEBUG
    1609 extern void cs_debug_mask(uint16_t, const char *,...);
    1610 extern void cs_ddump_mask(uint16_t, const uchar *, int32_t, char *, ...);
    1611 #else
    1612 #define nop() asm volatile("nop")
    1613 #define cs_debug(...) nop()
    1614 #define cs_debug_mask(...) nop()
    1615 #define cs_ddump(...) nop()
    1616 #define cs_ddump_mask(...) nop()
    1617 #endif
    1618 extern void cs_close_log(void);
    1619 extern int32_t  cs_init_statistics();
    1620 extern void cs_dump(const uchar *, int32_t, char *, ...);
    1621 
    1622 // oscam-aes
    1623 extern void aes_set_key(char *);
    1624 extern void aes_encrypt_idx(struct s_client *, uchar *, int32_t);
    1625 extern void aes_decrypt(uchar *, int32_t);
    1626 extern int32_t aes_decrypt_from_list(AES_ENTRY *list, uint16_t caid, uint32_t provid,int32_t keyid, uchar *buf, int32_t n);
    1627 extern int32_t aes_present(AES_ENTRY *list, uint16_t caid, uint32_t provid,int32_t keyid);
    1628 extern void parse_aes_keys(struct s_reader *rdr,char *value);
    1629 
    1630 #define aes_encrypt(b, n) aes_encrypt_idx(cur_client(), b, n)
    1631 
    1632 // reader-common
    1633 extern int32_t reader_device_init(struct s_reader * reader);
    1634 extern int32_t reader_checkhealth(struct s_reader * reader);
    1635 extern void reader_post_process(struct s_reader * reader);
    1636 extern int32_t reader_ecm(struct s_reader * reader, ECM_REQUEST *);
    1637 extern int32_t reader_emm(struct s_reader * reader, EMM_PACKET *);
    1638 int32_t reader_get_emm_type(EMM_PACKET *ep, struct s_reader * reader);
    1639 struct s_cardsystem *get_cardsystem_by_caid(uint16_t caid);
    1640 extern void reader_device_close(struct s_reader * reader);
    1641 
    1642 #ifdef WITH_LB
    1643 //module-stat
    1644 extern void init_stat();
    1645 extern int32_t get_best_reader(ECM_REQUEST *er);
    1646 extern void clear_reader_stat(struct s_reader *reader);
    1647 extern void add_stat(struct s_reader *rdr, ECM_REQUEST *er, int32_t ecm_time, int32_t rc);
    1648 extern void load_stat_from_file();
    1649 extern void save_stat_to_file(int32_t);
    1650 extern void clear_all_stat();
    1651 extern void housekeeping_stat(int32_t force);
    1652 extern void sort_stat(struct s_reader *rdr, int32_t reverse);
    1653 #endif
    1654 
    1655 #ifdef HAVE_PCSC
    1656 // reader-pcsc
    1657 extern void pcsc_close(struct s_reader *pcsc_reader);
    1658 #endif
    1659 
    1660 void reader_nagra();
    1661 void reader_irdeto();
    1662 void reader_cryptoworks();
    1663 void reader_viaccess();
    1664 void reader_conax();
    1665 void reader_seca();
    1666 void reader_videoguard1();
    1667 void reader_videoguard2();
    1668 void reader_videoguard12();
    1669 void reader_dre();
    1670 void reader_tongfang();
    1671 
    1672 void cardreader_mouse(struct s_cardreader *crdr);
    1673 void cardreader_smargo(struct s_cardreader *crdr);
    1674 #ifdef WITH_STAPI
    1675 void cardreader_stapi(struct s_cardreader *crdr);
    1676 #endif
    1677 
    1678 // protocol modules
    1679 extern int32_t  monitor_send_idx(struct s_client *, char *);
    1680 extern void module_monitor(struct s_module *);
    1681 extern void module_camd35(struct s_module *);
    1682 extern void module_camd35_tcp(struct s_module *);
    1683 extern void module_camd33(struct s_module *);
    1684 extern void module_newcamd(struct s_module *);
    1685 extern void module_radegast(struct s_module *);
    1686 extern void module_oscam_ser(struct s_module *);
    1687 #ifdef MODULE_CCCAM
    1688 extern void module_cccam(struct s_module *);
    1689 #endif
    1690 extern void module_gbox(struct s_module *);
    1691 extern void module_constcw(struct s_module *);
    1692 extern int32_t chk_pending(int32_t timeout);
    1693 #ifdef HAVE_DVBAPI
    1694 extern void module_dvbapi(struct s_module *);
    1695 #endif
    1696 
    1697 #ifdef WEBIF
    1698 // oscam-http
    1699 extern void http_srv();
    1700 #endif
    1701 
    1702 // oscam-garbage
    1703 #ifdef WITH_DEBUG
    1704 extern void add_garbage_debug(void *data, char *file, uint16_t line);
    1705 #define add_garbage(x)  add_garbage_debug(x,__FILE__, __LINE__)
    1706 #else
    1707 extern void add_garbage(void *data);
    1708 #endif
    1709 extern void start_garbage_collector(int32_t);
    1710 extern void stop_garbage_collector();
    1711 
    1712 #ifdef LCDSUPPORT
    1713 // oscam-lcd
    1714 extern void start_lcd_thread();
    1715 extern void end_lcd_thread();
    1716 #endif
     1325
     1326/* ===========================
     1327 *      global functions
     1328 * =========================== */
     1329#include "global-functions.h"
    17171330
    17181331#endif  // CS_GLOBALS
  • trunk/module-stat.c

    r5352 r5361  
    22
    33#ifdef WITH_LB
    4 #include "module-stat.h"
    54#include "module-cccam.h"
    65
     
    109#define LB_REOPEN_MODE_STANDARD 0
    1110#define LB_REOPEN_MODE_FAST 1
     11
     12#define LB_NONE 0
     13#define LB_FASTEST_READER_FIRST 1
     14#define LB_OLDEST_READER_FIRST 2
     15#define LB_LOWEST_USAGELEVEL 3
     16#define LB_LOG_ONLY 10
    1217
    1318static int32_t stat_load_save;
  • trunk/oscam-ac.c

    r4994 r5361  
    112112}
    113113
     114/* Starts the anticascader thread. */
     115void start_anticascader(){
     116  struct s_client * cl = create_client(first_client->ip);
     117  if (cl == NULL) return;
     118  cl->thread = pthread_self();
     119  pthread_setspecific(getclient, cl);
     120  cl->typ = 'a';
     121
     122  ac_init_stat();
     123  while(1)
     124  {
     125    int32_t i;
     126    for( i=0; i<cfg.ac_stime*60; i++ )
     127        cs_sleepms(1000); //FIXME this is a cpu-killer!
     128    ac_do_stat();
     129  }
     130}
     131
    114132void ac_init_client(struct s_client *client, struct s_auth *account)
    115133{
  • trunk/oscam-http.c

    r5354 r5361  
    1414#include "module-cccam.h"
    1515#include "module-cccshare.h"
    16 #include "module-stat.h"
    1716
    1817extern void restart_cardreader(struct s_reader *rdr, int32_t restart);
  • trunk/oscam-log.c

    r5309 r5361  
    348348}
    349349#endif
     350
     351void log_emm_request(struct s_reader *rdr){
     352    cs_log("%s emm-request sent (reader=%s, caid=%04X, auprovid=%06lX)",
     353            username(cur_client()), rdr->label, rdr->caid,
     354            rdr->auprovid ? rdr->auprovid : b2i(4, rdr->prid[0]));
     355}
     356
     357/*
     358 * This function writes the current CW from ECM struct to a cwl file.
     359 * The filename is re-calculated and file re-opened every time.
     360 * This will consume a bit cpu time, but nothing has to be stored between
     361 * each call. If not file exists, a header is prepended
     362 */
     363void logCWtoFile(ECM_REQUEST *er){
     364    FILE *pfCWL;
     365    char srvname[128];
     366    /* %s / %s   _I  %04X  _  %s  .cwl  */
     367    char buf[256 + sizeof(srvname)];
     368    char date[7];
     369    unsigned char  i, parity, writeheader = 0;
     370    time_t t;
     371    struct tm timeinfo;
     372
     373    /*
     374    * search service name for that id and change characters
     375    * causing problems in file name
     376    */
     377
     378    get_servicename(cur_client(), er->srvid, er->caid, srvname);
     379
     380    for (i = 0; srvname[i]; i++)
     381        if (srvname[i] == ' ') srvname[i] = '_';
     382
     383    /* calc log file name */
     384    time(&t);
     385    localtime_r(&t, &timeinfo);
     386    strftime(date, sizeof(date), "%Y%m%d", &timeinfo);
     387    snprintf(buf, sizeof(buf), "%s/%s_I%04X_%s.cwl", cfg.cwlogdir, date, er->srvid, srvname);
     388
     389    /* open failed, assuming file does not exist, yet */
     390    if((pfCWL = fopen(buf, "r")) == NULL) {
     391        writeheader = 1;
     392    } else {
     393    /* we need to close the file if it was opened correctly */
     394        fclose(pfCWL);
     395    }
     396
     397    if ((pfCWL = fopen(buf, "a+")) == NULL) {
     398        /* maybe this fails because the subdir does not exist. Is there a common function to create it?
     399            for the moment do not print32_t to log on every ecm
     400            cs_log(""error opening cw logfile for writing: %s (errno=%d %s)", buf, errno, strerror(errno)); */
     401        return;
     402    }
     403    if (writeheader) {
     404        /* no global macro for cardserver name :( */
     405        fprintf(pfCWL, "# OSCam cardserver v%s - http://streamboard.gmc.to/oscam/\n", CS_VERSION);
     406        fprintf(pfCWL, "# control word log file for use with tsdec offline decrypter\n");
     407        strftime(buf, sizeof(buf),"DATE %Y-%m-%d, TIME %H:%M:%S, TZ %Z\n", &timeinfo);
     408        fprintf(pfCWL, "# %s", buf);
     409        fprintf(pfCWL, "# CAID 0x%04X, SID 0x%04X, SERVICE \"%s\"\n", er->caid, er->srvid, srvname);
     410    }
     411
     412    parity = er->ecm[0]&1;
     413    fprintf(pfCWL, "%d ", parity);
     414    for (i = parity * 8; i < 8 + parity * 8; i++)
     415        fprintf(pfCWL, "%02X ", er->cw[i]);
     416    /* better use incoming time er->tps rather than current time? */
     417    strftime(buf,sizeof(buf),"%H:%M:%S\n", &timeinfo);
     418    fprintf(pfCWL, "# %s", buf);
     419    fflush(pfCWL);
     420    fclose(pfCWL);
     421}
     422
     423void cs_log_config()
     424{
     425  uchar buf[20];
     426
     427  if (cfg.nice!=99)
     428    snprintf((char *)buf, sizeof(buf), ", nice=%d", cfg.nice);
     429  else
     430    buf[0]='\0';
     431  cs_log("version=%s, build #%s, system=%s-%s-%s%s", CS_VERSION, CS_SVN_VERSION, CS_OS_CPU, CS_OS_HW, CS_OS_SYS, buf);
     432  cs_log("client max. idle=%d sec, debug level=%d", cfg.cmaxidle, cs_dblevel);
     433
     434  if( cfg.max_log_size )
     435    snprintf((char *)buf, sizeof(buf), "%d Kb", cfg.max_log_size);
     436  else
     437    cs_strncpy((char *)buf, "unlimited", sizeof(buf));
     438  cs_log("max. logsize=%s, loghistorysize=%d bytes", buf, cfg.loghistorysize);
     439  cs_log("client timeout=%lu ms, fallback timeout=%lu ms, cache delay=%d ms",
     440         cfg.ctimeout, cfg.ftimeout, cfg.delay);
     441}
     442
    350443int32_t cs_init_statistics(void)
    351444{
  • trunk/oscam-simples.c

    r5358 r5361  
    33#include "globals.h"
    44#include "module-cccam.h"
     5
     6/* Gets the client associated to the calling thread. */
     7struct s_client *cur_client(void){
     8    return (struct s_client *) pthread_getspecific(getclient);
     9}
     10
     11/* Gets the unique thread number from the client. Used in monitor and newcamd. */
     12int32_t get_threadnum(struct s_client *client) {
     13    struct s_client *cl;
     14    int32_t count=0;
     15
     16    for (cl=first_client->next; cl ; cl=cl->next) {
     17        if (cl->typ==client->typ)
     18            count++;
     19        if(cl==client)
     20            return count;
     21    }
     22    return 0;
     23}
     24
     25/* Gets the tmp dir */
     26char *get_tmp_dir(){
     27  if (cs_tmpdir[0])
     28    return cs_tmpdir;
     29
     30#ifdef OS_CYGWIN32
     31  char *d = getenv("TMPDIR");
     32  if (!d || !d[0])
     33    d = getenv("TMP");
     34  if (!d || !d[0])
     35    d = getenv("TEMP");
     36  if (!d || !d[0])
     37    getcwd(cs_tmpdir, sizeof(cs_tmpdir)-1);
     38
     39  cs_strncpy(cs_tmpdir, d, sizeof(cs_tmpdir));
     40  char *p = cs_tmpdir;
     41  while(*p) p++;
     42  p--;
     43  if (*p != '/' && *p != '\\')
     44    strcat(cs_tmpdir, "/");
     45  strcat(cs_tmpdir, "_oscam");
     46#else
     47  cs_strncpy(cs_tmpdir, "/tmp/.oscam", sizeof(cs_tmpdir));
     48#endif
     49  mkdir(cs_tmpdir, S_IRWXU);
     50  return cs_tmpdir;
     51}
    552
    653void aes_set_key(char *key)
     
    250297}
    251298
     299int32_t comp_timeb(struct timeb *tpa, struct timeb *tpb)
     300{
     301  if (tpa->time>tpb->time) return(1);
     302  if (tpa->time<tpb->time) return(-1);
     303  if (tpa->millitm>tpb->millitm) return(1);
     304  if (tpa->millitm<tpb->millitm) return(-1);
     305  return(0);
     306}
     307
    252308int32_t cs_atob(uchar *buf, char *asc, int32_t n)
    253309{
  • trunk/oscam.c

    r5353 r5361  
    1515#endif
    1616
    17 extern void cs_statistics(struct s_client * client);
    18 extern int32_t ICC_Async_Close (struct s_reader *reader);
    1917static void cs_fake_client(struct s_client *client, char *usr, int32_t uniq, in_addr_t ip);
    2018
     
    3331uint16_t  len4caid[256];    // table for guessing caid (by len)
    3432char  cs_confdir[128]=CS_CONFDIR;
    35 int32_t cs_dblevel=0;   // Debug Level (TODO !!)
     33int32_t cs_dblevel=0;   // Debug Level
    3634#ifdef WEBIF
    37 int32_t cs_restart_mode=1; //Restartmode: 0=off, no restart fork, 1=(default)restart fork, restart by webif, 2=like=1, but also restart on segfaults
    38 #endif
    39 int32_t cs_capture_SEGV=0;
     35int8_t cs_restart_mode=1; //Restartmode: 0=off, no restart fork, 1=(default)restart fork, restart by webif, 2=like=1, but also restart on segfaults
     36#endif
     37int8_t cs_capture_SEGV=0;
    4038char  cs_tmpdir[200]={0x00};
    4139pthread_mutex_t gethostbyname_lock;
     
    5553char    *loghist = NULL;     // ptr of log-history
    5654char    *loghistptr = NULL;
    57 
    58 int32_t get_threadnum(struct s_client *client) {
    59     struct s_client *cl;
    60     int32_t count=0;
    61 
    62     for (cl=first_client->next; cl ; cl=cl->next) {
    63         if (cl->typ==client->typ)
    64             count++;
    65         if(cl==client)
    66             return count;
    67     }
    68     return 0;
    69 }
    70 
    71 struct s_client * cur_client(void)
    72 {
    73     return (struct s_client *) pthread_getspecific(getclient);
    74 }
    7555
    7656int32_t cs_check_v(uint32_t ip, int32_t add) {
     
    149129static const char *logo = "  ___  ____   ___                \n / _ \\/ ___| / __|__ _ _ __ ___  \n| | | \\___ \\| |  / _` | '_ ` _ \\ \n| |_| |___) | |_| (_| | | | | | |\n \\___/|____/ \\___\\__,_|_| |_| |_|\n";
    150130
     131/* Prints usage information and information about the built-in modules. */
    151132static void usage()
    152133{
     
    346327}
    347328
     329/* Returns the username from the client. You will always get a char reference back (no NULLs but it may be string containting "NULL")
     330   which you should never modify and not free()! */
    348331char *username(struct s_client * client)
    349332{
     
    412395  er->caid=caid;
    413396  return(1);
    414 }
    415 
    416 /*
    417  * void set_signal_handler(int32_t sig, int32_t flags, void (*sighandler)(int))
    418  * flags: 1 = restart, 2 = don't modify if SIG_IGN, may be combined
    419  */
    420 void set_signal_handler(int32_t sig, int32_t flags, void (*sighandler))
    421 {
    422 #ifdef CS_SIGBSD
    423   if ((signal(sig, sighandler)==SIG_IGN) && (flags & 2))
    424   {
    425     signal(sig, SIG_IGN);
    426     siginterrupt(sig, 0);
    427   }
    428   else
    429     siginterrupt(sig, (flags & 1) ? 0 : 1);
    430 #else
    431   struct sigaction sa;
    432   sigaction(sig, (struct sigaction *) 0, &sa);
    433   if (!((flags & 2) && (sa.sa_handler==SIG_IGN)))
    434   {
    435     sigemptyset(&sa.sa_mask);
    436     sa.sa_flags=(flags & 1) ? SA_RESTART : 0;
    437     sa.sa_handler=sighandler;
    438     sigaction(sig, &sa, (struct sigaction *) 0);
    439   }
    440 #endif
    441 }
    442 
    443 static void cs_master_alarm()
    444 {
    445   cs_log("PANIC: master deadlock!");
    446   fprintf(stderr, "PANIC: master deadlock!");
    447   fflush(stderr);
    448 }
    449 
    450 static void cs_sigpipe()
    451 {
    452     if (cs_dblevel & D_ALL_DUMP)
    453         cs_log("Got sigpipe signal -> captured");
    454397}
    455398
     
    585528        if(cl->pfd)     nullclose(&cl->pfd); //Closing Network socket
    586529        if(cl->fd_m2c_c)    nullclose(&cl->fd_m2c_c); //Closing client read fd
    587         if(cl->fd_m2c)  nullclose(&cl->fd_m2c); //Closing client read fd
     530        if(cl->fd_m2c)  nullclose(&cl->fd_m2c); //Closing master write fd
    588531
    589532        if(cl->typ == 'r' && cl->reader){
     
    644587    cfg.account = NULL;
    645588    init_free_sidtab();
     589}
     590
     591/*
     592 * flags: 1 = restart, 2 = don't modify if SIG_IGN, may be combined
     593 */
     594void set_signal_handler(int32_t sig, int32_t flags, void (*sighandler))
     595{
     596#ifdef CS_SIGBSD
     597  if ((signal(sig, sighandler)==SIG_IGN) && (flags & 2))
     598  {
     599    signal(sig, SIG_IGN);
     600    siginterrupt(sig, 0);
     601  }
     602  else
     603    siginterrupt(sig, (flags & 1) ? 0 : 1);
     604#else
     605  struct sigaction sa;
     606  sigaction(sig, (struct sigaction *) 0, &sa);
     607  if (!((flags & 2) && (sa.sa_handler==SIG_IGN)))
     608  {
     609    sigemptyset(&sa.sa_mask);
     610    sa.sa_flags=(flags & 1) ? SA_RESTART : 0;
     611    sa.sa_handler=sighandler;
     612    sigaction(sig, &sa, (struct sigaction *) 0);
     613  }
     614#endif
     615}
     616
     617static void cs_master_alarm()
     618{
     619  cs_log("PANIC: master deadlock!");
     620  fprintf(stderr, "PANIC: master deadlock!");
     621  fflush(stderr);
     622}
     623
     624static void cs_sigpipe()
     625{
     626    if (cs_dblevel & D_ALL_DUMP)
     627        cs_log("Got sigpipe signal -> captured");
     628}
     629
     630/* Switch debuglevel forward one step (called when receiving SIGUSR1). */
     631void cs_debug_level(){ 
     632    switch (cs_dblevel) {
     633        case 0:
     634            cs_dblevel = 1;
     635            break;
     636        case 128:
     637            cs_dblevel = 255;
     638            break;
     639        case 255:
     640            cs_dblevel = 0;
     641            break;
     642        default:
     643            cs_dblevel <<= 1;
     644    }
     645
     646    cs_log("debug_level=%d", cs_dblevel);
     647}
     648
     649void cs_card_info()
     650{
     651  uchar dummy[1]={0x00};
     652    struct s_client *cl;
     653    for (cl=first_client->next; cl ; cl=cl->next)
     654    if( cl->typ=='r' && cl->fd_m2c )
     655      write_to_pipe(cl->fd_m2c, PIP_ID_CIN, dummy, 1);
     656      //kill(client[i].pid, SIGUSR2);
     657}
     658
     659/**
     660 * called by signal SIGHUP
     661 *
     662 * reloads configs:
     663 *  - useraccounts (oscam.user)
     664 *  - services ids (oscam.srvid)
     665 *  - tier ids     (oscam.tiers)
     666 *  Also clears anticascading stats.
     667 **/
     668void cs_reload_config()
     669{
     670        cs_accounts_chk();
     671        init_srvid();
     672        init_tierid();
     673        #ifdef CS_ANTICASC
     674        ac_init_stat();
     675        #endif
     676}
     677
     678/* Sets signal handlers to ignore for early startup of OSCam because for example log
     679   could cause SIGPIPE errors and the normal signal handlers can't be used at this point. */
     680static void init_signal_pre()
     681{
     682        set_signal_handler(SIGPIPE , 1, SIG_IGN);
     683        set_signal_handler(SIGWINCH, 1, SIG_IGN);
     684        set_signal_handler(SIGALRM , 1, SIG_IGN);
     685        set_signal_handler(SIGHUP  , 1, SIG_IGN);
     686}
     687
     688/* Sets the signal handlers.*/
     689static void init_signal()
     690{
     691        set_signal_handler(SIGINT, 3, cs_exit);
     692        //set_signal_handler(SIGKILL, 3, cs_exit);
     693#ifdef OS_MACOSX
     694        set_signal_handler(SIGEMT, 3, cs_exit);
     695#else
     696        //set_signal_handler(SIGPOLL, 3, cs_exit);
     697#endif
     698        //set_signal_handler(SIGPROF, 3, cs_exit);
     699        set_signal_handler(SIGTERM, 3, cs_exit);
     700        //set_signal_handler(SIGVTALRM, 3, cs_exit);
     701
     702        set_signal_handler(SIGWINCH, 1, SIG_IGN);
     703        //  set_signal_handler(SIGPIPE , 0, SIG_IGN);
     704        set_signal_handler(SIGPIPE , 0, cs_sigpipe);
     705        //  set_signal_handler(SIGALRM , 0, cs_alarm);
     706        set_signal_handler(SIGALRM , 0, cs_master_alarm);
     707        // set_signal_handler(SIGCHLD , 1, cs_child_chk);
     708        set_signal_handler(SIGHUP  , 1, cs_reload_config);
     709        //set_signal_handler(SIGHUP , 1, cs_sighup);
     710        set_signal_handler(SIGUSR1, 1, cs_debug_level);
     711        set_signal_handler(SIGUSR2, 1, cs_card_info);
     712        set_signal_handler(SIGCONT, 1, SIG_IGN);
     713
     714        if (cs_capture_SEGV)
     715            set_signal_handler(SIGSEGV, 1, cs_exit);
     716
     717        cs_log("signal handling initialized (type=%s)",
     718#ifdef CS_SIGBSD
     719        "bsd"
     720#else
     721        "sysv"
     722#endif
     723        );
     724    return;
    646725}
    647726
     
    797876}
    798877
    799 void cs_debug_level()
    800 {
    801     //switch debuglevel forward one step if not set from outside
    802     switch (cs_dblevel) {
    803         case 0:
    804             cs_dblevel = 1;
    805             break;
    806         case 128:
    807             cs_dblevel = 255;
    808             break;
    809         case 255:
    810             cs_dblevel = 0;
    811             break;
    812         default:
    813             cs_dblevel <<= 1;
    814     }
    815 
    816     cs_log("%sdebug_level=%d", "all", cs_dblevel);
    817 }
    818 
    819 void cs_card_info()
    820 {
    821   uchar dummy[1]={0x00};
    822     struct s_client *cl;
    823     for (cl=first_client->next; cl ; cl=cl->next)
    824     if( cl->typ=='r' && cl->fd_m2c )
    825       write_to_pipe(cl->fd_m2c, PIP_ID_CIN, dummy, 1);
    826       //kill(client[i].pid, SIGUSR2);
    827 }
    828878
    829879struct s_client * create_client(in_addr_t ip) {
     
    842892        //make_non_blocking(fdp[1]);
    843893        cl->fd_m2c_c = fdp[0]; //store client read fd
    844         cl->fd_m2c = fdp[1]; //store client read fd
     894        cl->fd_m2c = fdp[1]; //store master write fd
    845895        cl->ip=ip;
    846896        cl->account = first_client->account;
     
    867917}
    868918
    869 /**
    870  * called by signal SIGHUB
    871  *
    872  * reloads configs:
    873  *  - useraccounts (oscom.user)
    874  *  - services ids (oscam.srvid)
    875  *  - tier ids     (oscam.tiers)
    876  **/
    877 void cs_reload_config()
    878 {
    879         cs_accounts_chk();
    880         init_srvid();
    881         init_tierid();
    882         #ifdef CS_ANTICASC
    883         ac_init_stat();
    884         #endif
    885 }
    886 
    887 static void init_signal_pre()
    888 {
    889         set_signal_handler(SIGPIPE , 1, SIG_IGN);
    890         set_signal_handler(SIGWINCH, 1, SIG_IGN);
    891         set_signal_handler(SIGALRM , 1, SIG_IGN);
    892         set_signal_handler(SIGHUP  , 1, SIG_IGN);
    893 }
    894 
    895 static void init_signal()
    896 {
    897         set_signal_handler(SIGINT, 3, cs_exit);
    898         //set_signal_handler(SIGKILL, 3, cs_exit);
    899 #ifdef OS_MACOSX
    900         set_signal_handler(SIGEMT, 3, cs_exit);
    901 #else
    902         //set_signal_handler(SIGPOLL, 3, cs_exit);
    903 #endif
    904         //set_signal_handler(SIGPROF, 3, cs_exit);
    905         set_signal_handler(SIGTERM, 3, cs_exit);
    906         //set_signal_handler(SIGVTALRM, 3, cs_exit);
    907 
    908         set_signal_handler(SIGWINCH, 1, SIG_IGN);
    909         //  set_signal_handler(SIGPIPE , 0, SIG_IGN);
    910         set_signal_handler(SIGPIPE , 0, cs_sigpipe);
    911         //  set_signal_handler(SIGALRM , 0, cs_alarm);
    912         set_signal_handler(SIGALRM , 0, cs_master_alarm);
    913         // set_signal_handler(SIGCHLD , 1, cs_child_chk);
    914         set_signal_handler(SIGHUP  , 1, cs_reload_config);
    915         //set_signal_handler(SIGHUP , 1, cs_sighup);
    916         set_signal_handler(SIGUSR1, 1, cs_debug_level);
    917         set_signal_handler(SIGUSR2, 1, cs_card_info);
    918         set_signal_handler(SIGCONT, 1, SIG_IGN);
    919 
    920         if (cs_capture_SEGV)
    921             set_signal_handler(SIGSEGV, 1, cs_exit);
    922 
    923         cs_log("signal handling initialized (type=%s)",
    924 #ifdef CS_SIGBSD
    925         "bsd"
    926 #else
    927         "sysv"
    928 #endif
    929         );
    930     return;
    931 }
    932 
     919
     920/* Creates the master client of OSCam and inits some global variables/mutexes. */
    933921static void init_first_client()
    934922{
     
    987975}
    988976
     977/* Checks if the date of the system is correct and waits if necessary. */
    989978static void init_check(){
    990979    char *ptr = __DATE__;
     
    11271116}
    11281117
    1129 int32_t cs_user_resolve(struct s_auth *account)
    1130 {
     1118/* Resolves the ip of the hostname of the specified account and saves it in account->dynip.
     1119   If the hostname is not configured, the ip is set to 0. */
     1120void cs_user_resolve(struct s_auth *account){
    11311121    if (account->dyndns[0]){
    11321122        in_addr_t lastip = account->dynip;
     
    11371127        }
    11381128    } else account->dynip=0;
    1139     return account->dynip?1:0;
    11401129}
    11411130
     
    11591148#pragma GCC diagnostic warning "-Wempty-body"
    11601149
     1150/* Starts a thread named nameroutine with the start function startroutine. */
    11611151void start_thread(void * startroutine, char * nameroutine) {
    11621152    pthread_t temp;
     
    11771167}
    11781168
    1179 static void kill_thread_int(struct s_client *cl) { //cs_exit is used to let thread kill itself, this routine is for a thread to kill other thread
     1169/* Allows to kill another thread specified through the client cl without locking.
     1170  If the own thread has to be cancelled, cs_exit or cs_disconnect_client has to be used. */
     1171static void kill_thread_int(struct s_client *cl) {
    11801172
    11811173    if (!cl) return;
     
    12171209}
    12181210
    1219 void kill_thread(struct s_client *cl) { //cs_exit is used to let thread kill itself, this routine is for a thread to kill other thread
     1211/* Allows to kill another thread specified through the client cl with locking.
     1212  If the own thread has to be cancelled, cs_exit or cs_disconnect_client has to be used. */
     1213void kill_thread(struct s_client *cl) {
    12201214    cs_lock(&system_lock);
    12211215    kill_thread_int(cl);
    12221216    cs_unlock(&system_lock);
    12231217}
    1224 #ifdef CS_ANTICASC
    1225 void start_anticascader()
    1226 {
    1227   struct s_client * cl = create_client(first_client->ip);
    1228   if (cl == NULL) return;
    1229   cl->thread = pthread_self();
    1230   pthread_setspecific(getclient, cl);
    1231   cl->typ = 'a';
    1232 
    1233   //set_signal_handler(SIGHUP, 1, ac_init_stat);
    1234   ac_init_stat();
    1235   while(1)
    1236   {
    1237     int32_t i;
    1238     for( i=0; i<cfg.ac_stime*60; i++ )
    1239         cs_sleepms(1000); //FIXME this is a cpu-killer!
    1240     ac_do_stat();
    1241   }
    1242 }
    1243 #endif
    1244 
     1218
     1219/* Removes a reader from the list of active readers so that no ecms can be requested anymore. */
    12451220void remove_reader_from_active(struct s_reader *rdr) {
    12461221    struct s_reader *rdr2, *prv = NULL;
     
    12571232}
    12581233
     1234/* Adds a reader to the list of active readers so that it can serve ecms. */
    12591235void add_reader_to_active(struct s_reader *rdr) {
    12601236    struct s_reader *rdr2;
     
    12681244}
    12691245
     1246/* Starts or restarts a cardreader without locking. If restart=1, the existing thread is killed before restarting,
     1247   if restart=0 the cardreader is only started. */
    12701248static int32_t restart_cardreader_int(struct s_reader *rdr, int32_t restart) {
    12711249
     
    13421320}
    13431321
     1322/* Starts or restarts a cardreader with locking. If restart=1, the existing thread is killed before restarting,
     1323   if restart=0 the cardreader is only started. */
    13441324int32_t restart_cardreader(struct s_reader *rdr, int32_t restart) {
    13451325    cs_lock(&system_lock);
     
    17651745}
    17661746
    1767 /*
    1768  * This function writes the current CW from ECM struct to a cwl file.
    1769  * The filename is re-calculated and file re-opened every time.
    1770  * This will consume a bit cpu time, but nothing has to be stored between
    1771  * each call. If not file exists, a header is prepended
    1772  */
    1773 void logCWtoFile(ECM_REQUEST *er)
    1774 {
    1775     FILE *pfCWL;
    1776     char srvname[128];
    1777     /* %s / %s   _I  %04X  _  %s  .cwl  */
    1778     char buf[256 + sizeof(srvname)];
    1779     char date[7];
    1780     unsigned char  i, parity, writeheader = 0;
    1781     time_t t;
    1782     struct tm timeinfo;
    1783 
    1784     /*
    1785     * search service name for that id and change characters
    1786     * causing problems in file name
    1787     */
    1788 
    1789     get_servicename(cur_client(), er->srvid, er->caid, srvname);
    1790 
    1791     for (i = 0; srvname[i]; i++)
    1792         if (srvname[i] == ' ') srvname[i] = '_';
    1793 
    1794     /* calc log file name */
    1795     time(&t);
    1796     localtime_r(&t, &timeinfo);
    1797     strftime(date, sizeof(date), "%Y%m%d", &timeinfo);
    1798     snprintf(buf, sizeof(buf), "%s/%s_I%04X_%s.cwl", cfg.cwlogdir, date, er->srvid, srvname);
    1799 
    1800     /* open failed, assuming file does not exist, yet */
    1801     if((pfCWL = fopen(buf, "r")) == NULL) {
    1802         writeheader = 1;
    1803     } else {
    1804     /* we need to close the file if it was opened correctly */
    1805         fclose(pfCWL);
    1806     }
    1807 
    1808     if ((pfCWL = fopen(buf, "a+")) == NULL) {
    1809         /* maybe this fails because the subdir does not exist. Is there a common function to create it?
    1810             for the moment do not print32_t to log on every ecm
    1811             cs_log(""error opening cw logfile for writing: %s (errno=%d %s)", buf, errno, strerror(errno)); */
    1812         return;
    1813     }
    1814     if (writeheader) {
    1815         /* no global macro for cardserver name :( */
    1816         fprintf(pfCWL, "# OSCam cardserver v%s - http://streamboard.gmc.to/oscam/\n", CS_VERSION_X);
    1817         fprintf(pfCWL, "# control word log file for use with tsdec offline decrypter\n");
    1818         strftime(buf, sizeof(buf),"DATE %Y-%m-%d, TIME %H:%M:%S, TZ %Z\n", &timeinfo);
    1819         fprintf(pfCWL, "# %s", buf);
    1820         fprintf(pfCWL, "# CAID 0x%04X, SID 0x%04X, SERVICE \"%s\"\n", er->caid, er->srvid, srvname);
    1821     }
    1822 
    1823     parity = er->ecm[0]&1;
    1824     fprintf(pfCWL, "%d ", parity);
    1825     for (i = parity * 8; i < 8 + parity * 8; i++)
    1826         fprintf(pfCWL, "%02X ", er->cw[i]);
    1827     /* better use incoming time er->tps rather than current time? */
    1828     strftime(buf,sizeof(buf),"%H:%M:%S\n", &timeinfo);
    1829     fprintf(pfCWL, "# %s", buf);
    1830     fflush(pfCWL);
    1831     fclose(pfCWL);
    1832 }
    1833 
    18341747/**
    18351748 * distributes found ecm-request to all clients with rc=99
     
    18961809  int32_t res=0;
    18971810  if( er->client && er->client->fd_m2c ) {
    1898     //Wie got an ECM (or nok). Now we should check for another clients waiting for it:
     1811    //We got an ECM (or nok). Now we should check for another clients waiting for it:
    18991812    res = write_ecm_request(er->client->fd_m2c, er);
    19001813  }
     
    27332646}
    27342647
    2735 void log_emm_request(struct s_reader *rdr)
    2736 {
    2737     cs_log("%s emm-request sent (reader=%s, caid=%04X, auprovid=%06lX)",
    2738             username(cur_client()), rdr->label, rdr->caid,
    2739             rdr->auprovid ? rdr->auprovid : b2i(4, rdr->prid[0]));
    2740 }
    2741 
    27422648void do_emm(struct s_client * client, EMM_PACKET *ep)
    27432649{
     
    28762782        write_to_pipe(aureader->fd, PIP_ID_EMM, (uchar *) ep, sizeof(EMM_PACKET));
    28772783    }
    2878 }
    2879 
    2880 int32_t comp_timeb(struct timeb *tpa, struct timeb *tpb)
    2881 {
    2882   if (tpa->time>tpb->time) return(1);
    2883   if (tpa->time<tpb->time) return(-1);
    2884   if (tpa->millitm>tpb->millitm) return(1);
    2885   if (tpa->millitm<tpb->millitm) return(-1);
    2886   return(0);
    28872784}
    28882785
     
    31183015}
    31193016
    3120 void cs_log_config()
    3121 {
    3122   uchar buf[20];
    3123 
    3124   if (cfg.nice!=99)
    3125     snprintf((char *)buf, sizeof(buf), ", nice=%d", cfg.nice);
    3126   else
    3127     buf[0]='\0';
    3128   cs_log("version=%s, build #%s, system=%s-%s-%s%s", CS_VERSION_X, CS_SVN_VERSION, CS_OS_CPU, CS_OS_HW, CS_OS_SYS, buf);
    3129   cs_log("client max. idle=%d sec, debug level=%d", cfg.cmaxidle, cs_dblevel);
    3130 
    3131   if( cfg.max_log_size )
    3132     snprintf((char *)buf, sizeof(buf), "%d Kb", cfg.max_log_size);
    3133   else
    3134     cs_strncpy((char *)buf, "unlimited", sizeof(buf));
    3135   cs_log("max. logsize=%s, loghistorysize=%d bytes", buf, cfg.loghistorysize);
    3136   cs_log("client timeout=%lu ms, fallback timeout=%lu ms, cache delay=%d ms",
    3137          cfg.ctimeout, cfg.ftimeout, cfg.delay);
    3138 }
    3139 
    31403017void cs_waitforcardinit()
    31413018{
     
    32703147    }
    32713148    return 0;
    3272 }
    3273 
    3274 /**
    3275  * get tmp dir
    3276   **/
    3277 char * get_tmp_dir()
    3278 {
    3279   if (cs_tmpdir[0])
    3280     return cs_tmpdir;
    3281 
    3282 #ifdef OS_CYGWIN32
    3283   char *d = getenv("TMPDIR");
    3284   if (!d || !d[0])
    3285     d = getenv("TMP");
    3286   if (!d || !d[0])
    3287     d = getenv("TEMP");
    3288   if (!d || !d[0])
    3289     getcwd(cs_tmpdir, sizeof(cs_tmpdir)-1);
    3290 
    3291   cs_strncpy(cs_tmpdir, d, sizeof(cs_tmpdir));
    3292   char *p = cs_tmpdir;
    3293   while(*p) p++;
    3294   p--;
    3295   if (*p != '/' && *p != '\\')
    3296     strcat(cs_tmpdir, "/");
    3297   strcat(cs_tmpdir, "_oscam");
    3298 #else
    3299   cs_strncpy(cs_tmpdir, "/tmp/.oscam", sizeof(cs_tmpdir));
    3300 #endif
    3301   mkdir(cs_tmpdir, S_IRWXU);
    3302   return cs_tmpdir;
    33033149}
    33043150
Note: See TracChangeset for help on using the changeset viewer.