init using 2016.11.9
git-svn-id: http://192.168.1.226/svn/proxy@7 454eff88-639b-444f-9e54-f578c98de674
New file |
| | |
| | | #include <stdio.h>
|
| | | #include <time.h>
|
| | | #include <string.h>
|
| | |
|
| | | #define MD 5
|
| | |
|
| | | #ifndef PROTOTYPES
|
| | | #define PROTOTYPES 0
|
| | | #endif
|
| | |
|
| | | typedef unsigned char *POINTER; /* POINTER defines a generic pointer type */
|
| | | typedef unsigned short int UINT2; /* UINT2 defines a two byte word */
|
| | | typedef unsigned long int UINT4; /* UINT4 defines a four byte word */
|
| | |
|
| | | /*
|
| | | PROTO_LIST is defined depending on how PROTOTYPES is defined above.
|
| | | If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
|
| | | returns an empty list.
|
| | | */
|
| | | //#if PROTOTYPES
|
| | | //#define PROTO_LIST(list) list
|
| | | //#else
|
| | | //#define PROTO_LIST(list) ()
|
| | | //#endif
|
| | |
|
| | | /* Length of test block, number of test blocks. */
|
| | | #define TEST_BLOCK_LEN 1000
|
| | | #define TEST_BLOCK_COUNT 1000
|
| | |
|
| | | /* Constants for MD5Transform routine. */
|
| | | #define S11 7 /* 各轮左移的位数 */
|
| | | #define S12 12
|
| | | #define S13 17
|
| | | #define S14 22
|
| | | #define S21 5
|
| | | #define S22 9
|
| | | #define S23 14
|
| | | #define S24 20
|
| | | #define S31 4
|
| | | #define S32 11
|
| | | #define S33 16
|
| | | #define S34 23
|
| | | #define S41 6
|
| | | #define S42 10
|
| | | #define S43 15
|
| | | #define S44 21
|
| | |
|
| | | typedef struct |
| | | {
|
| | | UINT4 state[4]; /* state (ABCD) */
|
| | | UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
|
| | | unsigned char buffer[64]; /* input buffer */
|
| | | }MD5_CTX;
|
| | |
|
| | | static unsigned char PADDING[64] = |
| | | {
|
| | | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
| | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
| | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
| | | };
|
| | |
|
| | | /* |
| | | F, G, H and I are basic MD5 functions.
|
| | | */
|
| | | #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
|
| | | #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
|
| | | #define H(x, y, z) ((x) ^ (y) ^ (z))
|
| | | #define I(x, y, z) ((y) ^ ((x) | (~z)))
|
| | |
|
| | | /* |
| | | ROTATE_LEFT rotates x left n bits.
|
| | | */
|
| | | #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
|
| | |
|
| | | /*
|
| | | FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
|
| | | Rotation is separate from addition to prevent recomputation.
|
| | | */
|
| | | #define FF(a, b, c, d, x, s, ac) { \
|
| | | (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
| | | (a) = ROTATE_LEFT ((a), (s)); \
|
| | | (a) += (b); \
|
| | | }
|
| | | #define GG(a, b, c, d, x, s, ac) { \
|
| | | (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
| | | (a) = ROTATE_LEFT ((a), (s)); \
|
| | | (a) += (b); \
|
| | | }
|
| | | #define HH(a, b, c, d, x, s, ac) { \
|
| | | (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
| | | (a) = ROTATE_LEFT ((a), (s)); \
|
| | | (a) += (b); \
|
| | | }
|
| | | #define II(a, b, c, d, x, s, ac) { \
|
| | | (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
| | | (a) = ROTATE_LEFT ((a), (s)); \
|
| | | (a) += (b); \
|
| | | }
|
| | |
|
| | | char* MDString (char *string);
|
| | |
|
| | |
|
| | | void MD5Init (MD5_CTX *context);
|
| | | void MD5Update(MD5_CTX *context, unsigned char *input,unsigned int inputLen);
|
| | | void MD5Final (unsigned char digest[16], MD5_CTX *context);
|
| | | void MD5Transform (UINT4 [4], unsigned char [64]) ;
|
| | | void Encode(unsigned char *, UINT4 *, unsigned int);
|
| | | void Decode (UINT4 *, unsigned char *, unsigned int);
|
| | | void MD5_memcpy(POINTER, POINTER, unsigned int);
|
| | | void MD5_memset(POINTER, int, unsigned int);
|
| | |
|
| | | void MD5Init (MD5_CTX *context) /* context */
|
| | | {
|
| | | context->count[0] = context->count[1] = 0;
|
| | | context->state[0] = 0x67452301; /* Load magic initialization constants.*/
|
| | | context->state[1] = 0xefcdab89;
|
| | | context->state[2] = 0x98badcfe;
|
| | | context->state[3] = 0x10325476;
|
| | | }
|
| | |
|
| | | /* -----------------------------------------------------------------------------------
|
| | | MD5 block update operation. Continues an MD5 message-digest
|
| | | operation, processing another message block, and updating the context.
|
| | | -------------------------------------------------------------------------------------*/
|
| | | /* context */ /* input block */ /* length of input block */
|
| | | void MD5Update (MD5_CTX *context, unsigned char *input,unsigned int inputLen )
|
| | | {
|
| | | unsigned int i, index, partLen;
|
| | | index = (unsigned int)((context->count[0] >> 3) & 0x3F); /* Compute number of bytes mod 64 */
|
| | |
|
| | | if ( (context->count[0] += ( (UINT4)inputLen << 3) ) < ( (UINT4)inputLen << 3 ) )
|
| | | context->count[1]++;
|
| | | |
| | | context->count[1] += ((UINT4)inputLen >> 29);
|
| | | partLen = 64 - index;
|
| | |
|
| | | /* Transform as many times as possible.*/
|
| | | if (inputLen >= partLen) |
| | | {
|
| | | MD5_memcpy( (POINTER)&context->buffer[index], (POINTER)input, partLen);
|
| | | MD5Transform (context->state, context->buffer);
|
| | |
|
| | | for (i = partLen; i + 63 < inputLen; i += 64)
|
| | | MD5Transform (context->state, &input[i]);
|
| | |
|
| | | index = 0;
|
| | | }
|
| | | else
|
| | | i = 0;
|
| | | /* Buffer remaining input */
|
| | | MD5_memcpy( (POINTER)&context->buffer[index], (POINTER)&input[i],inputLen-i );
|
| | | }
|
| | |
|
| | | /* |
| | | MD5 finalization. Ends an MD5 message-digest operation, writing the
|
| | | the message digest and zeroizing the context.
|
| | | */
|
| | | void MD5Final (unsigned char digest[16], MD5_CTX *context)
|
| | | /* message digest */ /* context */
|
| | | {
|
| | | unsigned char bits[8];
|
| | | unsigned int index, padLen;
|
| | | |
| | | Encode (bits, context->count, 8); /* Save number of bits */
|
| | |
|
| | | /* Pad out to 56 mod 64.*/
|
| | | index = (unsigned int)((context->count[0] >> 3) & 0x3f);
|
| | | padLen = (index < 56) ? (56 - index) : (120 - index);
|
| | | MD5Update (context,(unsigned char*) PADDING, padLen);
|
| | |
|
| | | MD5Update (context, bits, 8); /* Append length (before padding) */
|
| | | Encode (digest, context->state, 16); /* Store state in digest */
|
| | |
|
| | | /* Zeroize sensitive information.*/
|
| | | MD5_memset ((POINTER)context, 0, sizeof (*context));
|
| | | }
|
| | |
|
| | | /* MD5 basic transformation. Transforms state based on block.
|
| | | */
|
| | | void MD5Transform (UINT4 state[4],unsigned char block[64])
|
| | | {
|
| | | |
| | | UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
|
| | |
|
| | | Decode (x, block, 64);
|
| | | /* Round 1 */
|
| | | FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
|
| | | FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
|
| | | FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
|
| | | FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
|
| | | FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
|
| | | FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
|
| | | FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
|
| | | FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
|
| | | FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
|
| | | FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
|
| | | FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
|
| | | FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
|
| | | FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
|
| | | FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
|
| | | FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
|
| | | FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
|
| | | /* Round 2 */
|
| | | GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
|
| | | GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
|
| | | GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
|
| | | GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
|
| | | GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
|
| | | GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
|
| | | GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
|
| | | GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
|
| | | GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
|
| | | GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
|
| | | GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
|
| | | GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
|
| | | GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
|
| | | GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
|
| | | GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
|
| | | GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
|
| | | /* Round 3 */
|
| | | HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
|
| | | HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
|
| | | HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
|
| | | HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
|
| | | HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
|
| | | HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
|
| | | HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
|
| | | HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
|
| | | HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
|
| | | HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
|
| | | HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
|
| | | HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
|
| | | HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
|
| | | HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
|
| | | HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
|
| | | HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
|
| | | /* Round 4 */
|
| | | II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
|
| | | II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
|
| | | II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
|
| | | II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
|
| | | II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
|
| | | II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
|
| | | II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
|
| | | II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
|
| | | II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
|
| | | II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
|
| | | II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
|
| | | II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
|
| | | II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
|
| | | II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
|
| | | II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
|
| | | II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
|
| | |
|
| | | state[0] += a;
|
| | | state[1] += b;
|
| | | state[2] += c;
|
| | | state[3] += d;
|
| | |
|
| | | /* Zeroize sensitive information.*/
|
| | | MD5_memset ((POINTER)x, 0, sizeof (x));
|
| | | }
|
| | |
|
| | | /* Encodes input (UINT4) into output (unsigned char). Assumes len is
|
| | | a multiple of 4.
|
| | | */
|
| | | void Encode (unsigned char *output,UINT4 *input,unsigned int len)
|
| | | {
|
| | | unsigned int i, j;
|
| | |
|
| | | for (i = 0, j = 0; j < len; i++, j += 4) |
| | | {
|
| | | output[j] = (unsigned char)(input[i] & 0xff);
|
| | | output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
|
| | | output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
|
| | | output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
|
| | | }
|
| | | }
|
| | |
|
| | | /* Decodes input (unsigned char) into output (UINT4). Assumes len is
|
| | | a multiple of 4.
|
| | | */
|
| | | void Decode (UINT4 *output,unsigned char *input,unsigned int len)
|
| | | {
|
| | | unsigned int i, j;
|
| | |
|
| | | for (i = 0, j = 0; j < len; i++, j += 4)
|
| | | output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
|
| | | }
|
| | |
|
| | | /* Note: Replace "for loop" with standard memcpy if possible. */
|
| | | void MD5_memcpy (POINTER output,POINTER input,unsigned int len)
|
| | | {
|
| | | unsigned int i;
|
| | |
|
| | | for (i = 0; i < len; i++)
|
| | | output[i] = input[i];
|
| | | }
|
| | |
|
| | | /* Note: Replace "for loop" with standard memset if possible. */
|
| | | void MD5_memset (POINTER output,int value,unsigned int len)
|
| | | {
|
| | | unsigned int i;
|
| | |
|
| | | for (i = 0; i < len; i++)
|
| | | ((char *)output)[i] = (char)value;
|
| | | }
|
| | |
|
| | | /* Digests a string and prints the result. */
|
| | | char* MDString (char *string)
|
| | | {
|
| | | MD5_CTX context;
|
| | | unsigned char digest[16];
|
| | | char output1[33];
|
| | | static char output[33]={"\0"};
|
| | | unsigned int len = strlen (string);
|
| | | int i;
|
| | | MD5Init (&context);
|
| | | MD5Update (&context, (unsigned char*)string, len);
|
| | | MD5Final (digest, &context);
|
| | | for (i = 0; i < 16; i++)
|
| | | {
|
| | | sprintf(&(output1[2*i]),"%02x",(unsigned char)digest[i]);
|
| | | //sprintf(&(output1[2*i+1]),"%02x",(unsigned char)(digest[i]<<4));
|
| | | }
|
| | | for(i=0;i<32;i++)
|
| | | output[i]=output1[i];
|
| | | return output;
|
| | | }
|
| | |
|
| | |
|
| | |
|
New file |
| | |
| | | /*
|
| | | * proxyserver.c
|
| | | *
|
| | | * Created on: Oct 9, 2016
|
| | | * Author: JKM
|
| | | */
|
| | | #include <sys/socket.h>
|
| | | #include <sys/types.h>
|
| | | #include <netinet/in.h>
|
| | | #include <netdb.h>
|
| | | #include <arpa/inet.h>
|
| | | #include <pthread.h>
|
| | | #include <sys/time.h>
|
| | | #include <fcntl.h>
|
| | | #include "/usr/include/mysql/mysql.h"
|
| | | #include <stdio.h>
|
| | | #include <stdlib.h>
|
| | | #include <unistd.h>
|
| | | #include <sys/io.h>
|
| | | #include <errno.h>
|
| | | #include <sys/stat.h>
|
| | | #include <signal.h>
|
| | |
|
| | | #include "MD5Linux.h"
|
| | |
|
| | | #define MAX_NUM 16
|
| | | #define MAX 1024
|
| | | #define fileread_buf 128
|
| | | typedef struct{
|
| | | int m_id;
|
| | | char serverIP[32];
|
| | | char serverport[10];
|
| | | char port[10];
|
| | | char ip[32];
|
| | | char user[64];
|
| | | char pass[64];
|
| | | int reboot;
|
| | | }sockinfo,*lpsockinfo;
|
| | | pthread_t thread_no[MAX_NUM];
|
| | | pthread_t thread_noEQ[MAX_NUM];
|
| | | pthread_t thread_Real[MAX_NUM];
|
| | | sockinfo equipments[16];
|
| | | int sock_upper[16],sock_down[16];
|
| | | int rsock_down[16];
|
| | | char byte_array[2600];
|
| | | int bbbbb=0;
|
| | | int CommandEquipIdx=0;
|
| | | int CommandKind[16];
|
| | | char sessionIDs[16][5];
|
| | |
|
| | | char linkequip_ip[32];
|
| | | char linkequip_port[10];
|
| | | int focusServer=-1;
|
| | | int focusEquip=-1;
|
| | |
|
| | | int NUM=0,equip_num=0;
|
| | |
|
| | | int SupperConnectedflag[16]={0,},EquipConnectedflag[16]={0,},serverflag=0,GPIO_flag=0;
|
| | | int EquipRealAlarmConnectedflag[16];
|
| | | timer_t* timerID;
|
| | |
|
| | | int Read_MysqlData();
|
| | | int UpdateMysqData(int key);
|
| | | void* createServerthread(void* arg);
|
| | | void* createEquipmentthread(void* arg);
|
| | | void* MyEquipmentthread(void* arg);
|
| | |
|
| | |
|
| | | int net_test(char* serverIP,char* port);
|
| | | void EquipCheckGPIOOut(void);
|
| | | void GPIOinit(void);
|
| | | int GPIOoutput(int no);
|
| | | int time_lock(int year,int month,int day,int hour,int min,int sec,int during);
|
| | |
|
| | | int GPIO_OPEN(int PortNum);
|
| | | void GPIO_CLOSE(int PortNum);
|
| | | int GPIO_READ(int PortNum);
|
| | | void GPIO_WRITE(int PortNum,unsigned char DATA);
|
| | | int RealEventStart(int Idx);
|
| | | int gCapturing[16];
|
| | | char sessions[16][10];
|
| | |
|
| | | int n=0,m=0,k=0;
|
| | | unsigned char DownBuffer[16][614000];
|
| | | int DownCount[16];
|
| | | int gCount=0;
|
| | | int gCount30s=0;
|
| | | int gCount2s=0;
|
| | | void zclock_sleep (int msecs);
|
| | |
|
| | | void zclock_sleep (int msecs)
|
| | | {
|
| | | struct timespec t;
|
| | | t.tv_sec = msecs / 1000;
|
| | | t.tv_nsec = (msecs % 1000) * 1000000;
|
| | | nanosleep (&t, NULL);
|
| | | }
|
| | | void u_alarm_handler()
|
| | | {
|
| | | //printf("connect fail==============");
|
| | |
|
| | | }
|
| | | void timer_handler(int signum)
|
| | | {
|
| | | int i,j;
|
| | |
|
| | | ////////////// added on 2016.11.8 //////////////////
|
| | | gCount2s++;
|
| | | if(gCount2s>=8){
|
| | | gCount2s=0;
|
| | | for(;CommandEquipIdx<16;CommandEquipIdx++){//connected
|
| | | if(EquipConnectedflag[CommandEquipIdx]>0) break;
|
| | | }
|
| | | if(CommandEquipIdx>=16){
|
| | | CommandEquipIdx = 0;
|
| | | }
|
| | | }
|
| | | /////////////////////////////////////////////////////
|
| | | gCount30s++;
|
| | | if(gCount30s>=120){//4*30=30s
|
| | | gCount30s=0;
|
| | | if(NUM>0){ |
| | | EquipCheckGPIOOut();//GPIO Watchdog_Timer
|
| | | }
|
| | | }
|
| | | gCount++;
|
| | | if(gCount>=4){//1s
|
| | | gCount = 0;
|
| | | Read_MysqlData();
|
| | | equip_num=NUM;
|
| | | //time_lock(2016,10,28,10,10,10,30);
|
| | | }
|
| | | //serverthread
|
| | | for(i=0;i<NUM;i++){ |
| | | for(j=0;j<i;j++) if(strcmp(equipments[i].serverIP,equipments[j].serverIP)==0 && strcmp(equipments[i].serverport,equipments[j].serverport)==0) break;
|
| | | if(j<i)continue;
|
| | | |
| | | if(SupperConnectedflag[i]==0){
|
| | | struct sockaddr_in servaddr;
|
| | | int strlen1=sizeof(servaddr);
|
| | | if(equipments[i].serverIP[0]!=0 && equipments[i].serverport[0]!=0) {
|
| | | if((sock_upper[i]=socket(PF_INET,SOCK_STREAM,0))<0){
|
| | | perror("socket fail1");
|
| | | continue;
|
| | | }
|
| | | memset(&servaddr,0,strlen1);
|
| | | servaddr.sin_family=AF_INET;
|
| | | inet_pton(AF_INET,equipments[i].serverIP,&servaddr.sin_addr);
|
| | | servaddr.sin_port=htons(atoi(equipments[i].serverport));
|
| | |
|
| | | sigset(SIGALRM, u_alarm_handler);
|
| | | alarm(2);
|
| | | int rt = connect(sock_upper[i],(struct sockaddr*)&servaddr,strlen1);
|
| | | alarm(0);
|
| | | sigrelse(SIGALRM);
|
| | | if(rt<0){
|
| | | printf("server connect fail-%s:%s\n",equipments[i].serverIP,equipments[i].serverport);
|
| | | close(sock_upper[i]);
|
| | | continue;
|
| | | }
|
| | | SupperConnectedflag[i]=1;
|
| | | printf("server connect Success=:%s:%s\n",equipments[i].serverIP,equipments[i].serverport);
|
| | | pthread_create(&thread_no[i],NULL,&createServerthread,(void*)i);//super
|
| | | }
|
| | | }
|
| | | }
|
| | | //equipment thread
|
| | | for(i=0;i<NUM;i++){ |
| | | for(j=0;j<i;j++) if(strcmp(equipments[i].ip,equipments[j].ip)==0 && strcmp(equipments[i].port,equipments[j].port)==0) break;
|
| | | if(j<i)continue;
|
| | | if(EquipConnectedflag[i]==0){
|
| | | if(equipments[i].ip[0]!=0 && equipments[i].port[0]!=0) {
|
| | | struct sockaddr_in servaddr;
|
| | | int strlen1=sizeof(servaddr);
|
| | | if((sock_down[i]=socket(PF_INET,SOCK_STREAM,0))<0){
|
| | | perror("socket fail2");
|
| | | continue;
|
| | | }
|
| | | // printf("==============================3333\n");
|
| | | memset(&servaddr,0,strlen1);
|
| | | servaddr.sin_family=AF_INET;
|
| | | inet_pton(AF_INET,equipments[i].ip,&servaddr.sin_addr);
|
| | | servaddr.sin_port=htons(atoi(equipments[i].port));
|
| | | |
| | | sigset(SIGALRM, u_alarm_handler);//add 2016.11.22
|
| | | alarm(2);
|
| | | int rt = connect(sock_down[i],(struct sockaddr*)&servaddr,strlen1);
|
| | | alarm(0);
|
| | | sigrelse(SIGALRM); |
| | | if(rt<0){
|
| | | printf("Equipment connect fail-%s:%s\n",equipments[i].ip,equipments[i].port);
|
| | | close(sock_down[i]);
|
| | | continue;
|
| | | }
|
| | | EquipConnectedflag[i]=1;
|
| | | pthread_create(&thread_noEQ[i],NULL,&createEquipmentthread,(void*)i);//super(void*)(&equipments[0])
|
| | | printf("equipment connect Success=:%s:%s\n",equipments[i].ip,equipments[i].port);
|
| | | }
|
| | | }
|
| | | if(EquipRealAlarmConnectedflag[i]==0)
|
| | | {////// added on 2017.11.21
|
| | | //printf("Myequipment connect ==================:");
|
| | | if(RealEventStart(i)==0) EquipRealAlarmConnectedflag[i]=1; |
| | | }
|
| | |
|
| | | }
|
| | | }
|
| | |
|
| | | int main(int argc,char **argv)
|
| | | {
|
| | | struct sigaction sa;
|
| | | struct itimerval timer;
|
| | | linkequip_port[0]=0;
|
| | | linkequip_ip[0]=0; |
| | | GPIOinit();
|
| | | int i;
|
| | | Read_MysqlData();
|
| | | chmod("/etc/network/interfaces",0777);
|
| | | chmod("/tmp/interfaces",0777);
|
| | |
|
| | | for(i=0;i<NUM;i++) {
|
| | | SupperConnectedflag[i]=0;
|
| | | EquipConnectedflag[i]=0;
|
| | | EquipRealAlarmConnectedflag[i]=0;
|
| | | sock_upper[i]= -1;
|
| | | sock_down[i] = -1;
|
| | | rsock_down[i] = -1;
|
| | | }
|
| | | |
| | | /*Install timer_handler as th siganl handler for SIGVTALRM */
|
| | | memset(&sa,0,sizeof(sa));
|
| | | sa.sa_handler=&timer_handler;
|
| | | sigaction(SIGVTALRM,&sa,NULL);
|
| | | /* Configure the timer to expire after 250msec..*/
|
| | | timer.it_value.tv_sec=0;
|
| | | timer.it_value.tv_usec=250000;
|
| | | /*...and every 250 msec after that.*/
|
| | | timer.it_interval.tv_sec=0;
|
| | | timer.it_interval.tv_usec=250000;
|
| | | /*Start a virtual timer.It counts down whenever this process is executing.*/
|
| | |
|
| | | setitimer(ITIMER_VIRTUAL,&timer,NULL);
|
| | | while(1);
|
| | |
|
| | | }
|
| | |
|
| | | //=================================================================
|
| | | void* createServerthread(void* arg)
|
| | | {
|
| | | int sidx = (int)arg; |
| | | int length=0;
|
| | | char buff[102400];
|
| | | |
| | | puts("Step.1 ========>Server Thread is strarted......");
|
| | | unsigned char data[4096]; |
| | | //=======================================equipment data================================
|
| | | int idx=0; |
| | | int i;
|
| | | data[idx++]=0x68;
|
| | | data[idx++]=0x05;
|
| | | for(i=0;i<NUM;i++){
|
| | | data[idx++]=strlen(equipments[i].ip);
|
| | | memcpy(&data[idx],equipments[i].ip,strlen(equipments[i].ip));idx+=strlen(equipments[i].ip);
|
| | | data[idx++]=atoi(equipments[i].port)/256;
|
| | | data[idx++]=atoi(equipments[i].port)%256;
|
| | | data[idx++]=strlen(equipments[i].user);
|
| | | memcpy(&data[idx],equipments[i].user,strlen(equipments[i].user));idx+=strlen(equipments[i].user); |
| | | data[idx++]=strlen(equipments[i].pass);
|
| | | memcpy(&data[idx],equipments[i].pass,strlen(equipments[i].pass));idx+=strlen(equipments[i].pass);
|
| | | }
|
| | | data[idx++]=0x16;
|
| | | write(sock_upper[sidx],&data,idx);//send to equipment;순환문
|
| | | printf("Send equipment's info to Server OK! len=:%d\n",idx);
|
| | | //=======================================data================================================
|
| | | while(1)
|
| | | {
|
| | | int j;
|
| | | length=read(sock_upper[sidx],&buff,sizeof(buff)-1);
|
| | | printf("from Server received data===============:%d\n",length);
|
| | | if(length<=0)
|
| | | {
|
| | | printf(" Upper Socket closed \n");
|
| | | break;
|
| | | }
|
| | | else { |
| | | //====analysis
|
| | | // if(buff[0]==0x68 && buff[1]==0x07) {
|
| | | // focusServer =-1;
|
| | | // printf("from Server received data===============:%d%d\n",buff[0],buff[1]);
|
| | | // memcpy(buff,&buff[3],length-3);
|
| | | // length-=3;
|
| | | // if(length<=0) continue;
|
| | | //}
|
| | | |
| | | if(buff[0]==0x68 && buff[1]==0x03 )
|
| | | {
|
| | |
|
| | | printf("=========================buffer=====%s\n",buff);
|
| | | |
| | | int index=buff[2];
|
| | | char DownIP[32];
|
| | | char Downport[10];
|
| | | int result=-1;
|
| | | memcpy(&DownIP[0],&buff[3],index);
|
| | | int port=buff[index+3]*256+(unsigned char)buff[index+4];
|
| | | sprintf(Downport,"%d",port);
|
| | | if(buff[index+5]==0x16)
|
| | | {
|
| | | result=net_test(DownIP,Downport);
|
| | | printf("Result=%s:%d:%d \n",DownIP,port,result);
|
| | | if(result>0)
|
| | | {
|
| | | //response[0]=0x68;
|
| | | //response[1]=0x03;
|
| | | //response[2]=0x00;
|
| | | //write(sock_upper[sidx],response,3); |
| | | focusServer = sidx;
|
| | | for(j=0;j<NUM;j++) {
|
| | | if(strcmp(DownIP,equipments[j].ip)==0 && strcmp(Downport,equipments[j].port)==0) |
| | | {
|
| | | focusEquip = j;
|
| | | break;
|
| | | }
|
| | | }
|
| | | continue;
|
| | | }
|
| | | else{
|
| | | //response[0]=0x68;
|
| | | //response[1]=0x03;
|
| | | //response[2]=0x01;
|
| | | //write(sock_upper[sidx],response,3);
|
| | | continue;
|
| | | }
|
| | | // memcpy(buff,&buff[index+6],length-(index+6));
|
| | | // length-=index+6;
|
| | | // if(length<=0) continue;
|
| | | }
|
| | | }
|
| | | if(focusServer>=0)
|
| | | {
|
| | | if(focusEquip>=0 && sock_down[focusEquip]>0 ){
|
| | | write(sock_down[focusEquip],buff,length);
|
| | | printf("Directly down to Equipment data===============:%d\n",length);
|
| | | }
|
| | | else{
|
| | | printf("Saved to Equipment data=========================:%d\n",length);
|
| | | memcpy(DownBuffer[focusEquip],buff,length);//send to equipment;
|
| | | DownCount[focusEquip] = length;
|
| | | }
|
| | | }
|
| | | continue;
|
| | | }
|
| | | }
|
| | | puts("Server Thread is stoped..");
|
| | | close(sock_upper[sidx]);
|
| | | sock_upper[sidx] = -1;
|
| | | SupperConnectedflag[sidx]=0;//?
|
| | | pthread_exit((void*)thread_no[sidx]);
|
| | | }
|
| | |
|
| | | void* createEquipmentthread(void* arg)
|
| | | {
|
| | | int eidx = (int)arg; |
| | | printf("Step.2 ===%d=====>Equipment Thread is strarted......\n",eidx);
|
| | |
|
| | | int length=0;
|
| | | char buff[102400];
|
| | | while(1)
|
| | | {
|
| | | if(DownCount[eidx]>0) {
|
| | | write(sock_down[eidx],DownBuffer[eidx],DownCount[eidx]);//?
|
| | | printf("down to Equipment data=============:%d\n",DownCount[eidx]);
|
| | | DownCount[eidx]=0;
|
| | | }
|
| | | memset(buff,0,sizeof(buff));
|
| | | length=read(sock_down[eidx],&buff,sizeof(buff)-1); |
| | | if(length<=0)
|
| | | {
|
| | | printf(" Down Socket closed \n");
|
| | | break;
|
| | | }
|
| | | else if(length>0){
|
| | | if( focusServer>=0){
|
| | | printf("Received data from Equipment length==========:%d\n",length);
|
| | | if(sock_upper[focusServer] >0) |
| | | {
|
| | | write(sock_upper[focusServer],buff,length);//send to equipment;
|
| | | printf("send data to length Sever ===============:%d\n",length);
|
| | | }
|
| | | }
|
| | | |
| | | }
|
| | | }
|
| | | close(sock_down[eidx]);
|
| | | sock_down[eidx]=-1;
|
| | | puts("Equipment Thread is stoped..");
|
| | | EquipConnectedflag[eidx]=0;
|
| | | CommandKind[CommandEquipIdx]=0;
|
| | | pthread_exit((void*)thread_noEQ[eidx]);
|
| | | }
|
| | | //=====================Reatime thread realstart=========================
|
| | | void* MyEquipmentthread(void* arg)
|
| | | {
|
| | | int eidx = (int)arg; |
| | | printf("Step.3 ===%d=====>RealTimeEvent Thread is strarted......\n",eidx);
|
| | |
|
| | | int length=0;
|
| | | char buff[102400];
|
| | | while(1)
|
| | | {
|
| | | memset(buff,0,sizeof(buff));
|
| | | length=read(rsock_down[eidx],&buff,sizeof(buff)-1); |
| | | if(length<=0)
|
| | | {
|
| | | printf(" Down Socket closed \n");
|
| | | break;
|
| | | }
|
| | | else if(length>0){
|
| | | if(gCapturing[eidx]==1){
|
| | | int k;
|
| | | for(k=0;k<16;k++){
|
| | | if(SupperConnectedflag[k]==1){
|
| | | write(sock_upper[k],buff,strlen(buff));
|
| | | printf("======>>>>>>>upper send length====%d======\n",strlen(buff));
|
| | | printf("=============================================================\n");
|
| | | //printf("%s",buff);
|
| | | }
|
| | | }
|
| | | continue; |
| | | }
|
| | | }
|
| | | }
|
| | | close(rsock_down[eidx]);
|
| | | rsock_down[eidx]=-1;
|
| | | EquipRealAlarmConnectedflag[eidx]=0;
|
| | | puts("RealTimeEvent Thread is stoped..");
|
| | | pthread_exit((void*)thread_Real[eidx]);
|
| | | }
|
| | |
|
| | | int Read_MysqlData(){
|
| | | int ret=0;
|
| | | MYSQL *conn;
|
| | | MYSQL_RES *res;
|
| | | MYSQL_ROW row;
|
| | | char *server="localhost";
|
| | | char *user="root";
|
| | | char *password="123";
|
| | | char *database="opentutorials";
|
| | | if(!(conn=mysql_init((MYSQL*)NULL))){
|
| | | printf("init fail\n");
|
| | | exit(1);
|
| | | }
|
| | | if(!mysql_real_connect(conn,server,user,password,NULL,3306,NULL,0)){
|
| | | printf("connect error.\n");
|
| | | exit(1);
|
| | | }
|
| | | if(mysql_select_db(conn,database)!=0){
|
| | | mysql_close(conn);
|
| | | printf("select_db_fail.\n");
|
| | | exit(1);
|
| | | }
|
| | | if(mysql_query(conn,"select * from tbl_deviceinfo")){
|
| | | printf("query fail\n");
|
| | | exit(1);
|
| | | }
|
| | | res=mysql_store_result(conn);
|
| | | NUM=0; |
| | | while((row=mysql_fetch_row(res))!=NULL){
|
| | | equipments[NUM].m_id=atoi(row[0]);//id
|
| | | // printf("====%s:%s",row[0],row[11]); //sprintf(equipments[NUM].m_id,"%d",row[0]);
|
| | | if(strcmp(equipments[NUM].ip,row[1])!=0) ret = 1; |
| | | if(strcmp(equipments[NUM].port,row[2])!=0) ret = 1; |
| | | if(strcmp(equipments[NUM].serverIP,row[3])!=0) ret = 1; |
| | | if(strcmp(equipments[NUM].serverport,row[4])!=0) ret = 1; |
| | | if(strcmp(equipments[NUM].pass,row[5])!=0) ret = 1; |
| | | if(strcmp(equipments[NUM].user,row[6])!=0) ret = 1; |
| | | |
| | | |
| | | memset(equipments[NUM].ip,0,32);
|
| | | strcpy(equipments[NUM].ip,row[1]);//equipIP
|
| | | memset(equipments[NUM].port,0,10);
|
| | | strcpy(equipments[NUM].port,row[2]);//equipPort
|
| | | |
| | | memset(equipments[NUM].serverIP,0,32);
|
| | | strcpy(equipments[NUM].serverIP,row[3]);//ServerIP
|
| | | memset(equipments[NUM].serverport,0,10);
|
| | | strcpy(equipments[NUM].serverport,row[4]);//ServerPort
|
| | |
|
| | | memset(equipments[NUM].user,0,64);
|
| | | strcpy(equipments[NUM].user,row[6]);//equipUser
|
| | | memset(equipments[NUM].pass,0,64);
|
| | | strcpy(equipments[NUM].pass,row[5]);//equipPass
|
| | | |
| | | equipments[NUM].reboot=atoi(row[11]);//reboot option. |
| | | NUM++;
|
| | | }
|
| | | mysql_close(conn); |
| | | puts("Step.0 ========>MySQL is getting ok"); |
| | | return ret;
|
| | | |
| | | }
|
| | | int UpdateMysqData(int key){
|
| | | int ret=0;
|
| | | MYSQL *conn; |
| | | char *server="localhost";
|
| | | char *user="root";
|
| | | char *password="123";
|
| | | char *database="opentutorials";
|
| | | char query[255];
|
| | | puts("Step.0 ========>MySQL is getting......");
|
| | | if(!(conn=mysql_init((MYSQL*)NULL))){
|
| | | printf("init fail\n");
|
| | | exit(1);
|
| | | }
|
| | | if(!mysql_real_connect(conn,server,user,password,NULL,3306,NULL,0)){
|
| | | printf("connect error.\n");
|
| | | exit(1);
|
| | | }
|
| | | if(mysql_select_db(conn,database)!=0){
|
| | | mysql_close(conn);
|
| | | printf("select_db_fail.\n");
|
| | | exit(1);
|
| | | }
|
| | | sprintf(query,"UPDATE tbl_deviceinfo SET reboot=0 where id=%d",key); |
| | | if(mysql_query(conn,query)){
|
| | | printf("query fail\n");
|
| | | exit(1);
|
| | | } |
| | | mysql_close(conn);
|
| | | return ret;
|
| | | |
| | | }
|
| | | int net_test(char* serverIP,char* port){
|
| | | struct sockaddr_in servaddr;
|
| | | int strlen=sizeof(servaddr);
|
| | | int sockfdd;
|
| | | if((sockfdd=socket(PF_INET,SOCK_STREAM,0))<0){
|
| | | perror("socket fail");
|
| | | close(sockfdd);
|
| | | return -1;
|
| | | }
|
| | | memset(&servaddr,0,strlen);
|
| | | servaddr.sin_family=AF_INET;
|
| | | inet_pton(AF_INET,serverIP,&servaddr.sin_addr);
|
| | | servaddr.sin_port=htons(atoi(port));
|
| | | if(connect(sockfdd,(struct sockaddr*)&servaddr,strlen)<0){
|
| | | // perror("connect fail-3");
|
| | | close(sockfdd);
|
| | | return -1;
|
| | | }
|
| | | close(sockfdd);
|
| | | return 1;
|
| | | }
|
| | | void GPIOinit(void)
|
| | | {
|
| | | int retVal = -1;
|
| | | int PortNum = 0xA06;
|
| | | unsigned char wVal = 0x15;
|
| | | retVal = GPIO_OPEN(PortNum);
|
| | | printf("GPIO OPEN RES = %d \n",retVal);
|
| | | GPIO_WRITE(PortNum,wVal);
|
| | | printf("GPIO WRITE VALUE = 0x%X \n", wVal);
|
| | | |
| | | retVal = GPIO_READ(PortNum);
|
| | | printf("GPIO READ VALUE 02 = 0x%X \n",retVal);
|
| | |
|
| | | GPIO_CLOSE(PortNum);
|
| | | }
|
| | | int GPIOoutput(int no)
|
| | | {
|
| | | int PortNum = 0xA06;
|
| | | switch(no){
|
| | | case 0: |
| | | GPIO_WRITE(PortNum,0x15);
|
| | | return 0;
|
| | | case 1: |
| | | GPIO_WRITE(PortNum,0x57);
|
| | | return 0;
|
| | | case 2: |
| | | GPIO_WRITE(PortNum,0x5D);
|
| | | return 0;
|
| | | case 3: |
| | | GPIO_WRITE(PortNum,0x5F);
|
| | | return 0;
|
| | | case 4: |
| | | GPIO_WRITE(PortNum,0x75);
|
| | | return 0;
|
| | | case 5: |
| | | GPIO_WRITE(PortNum,0x77);
|
| | | return 0;
|
| | | case 6: |
| | | GPIO_WRITE(PortNum,0x7D);
|
| | | return 0;
|
| | | case 7: |
| | | GPIO_WRITE(PortNum,0x7F);
|
| | | return 0;
|
| | | case 8: |
| | | GPIO_WRITE(PortNum,0xD5);
|
| | | return 0; |
| | | case 9: |
| | | GPIO_WRITE(PortNum,0xD7);
|
| | | return 0;
|
| | | case 10: |
| | | GPIO_WRITE(PortNum,0xDD);
|
| | | return 0;
|
| | | case 11: |
| | | GPIO_WRITE(PortNum,0xDF);
|
| | | return 0;
|
| | | case 12: |
| | | GPIO_WRITE(PortNum,0xF5);
|
| | | return 0;
|
| | | case 13: |
| | | GPIO_WRITE(PortNum,0xF7);
|
| | | return 0;
|
| | | case 14: |
| | | GPIO_WRITE(PortNum,0xFD);
|
| | | return 0;
|
| | | case 15: |
| | | GPIO_WRITE(PortNum,0xFF);
|
| | | return 0;
|
| | | }
|
| | | return -1;
|
| | | }
|
| | | int GPIO_OPEN(int PortNum)
|
| | | {
|
| | | return ioperm(PortNum, 0x01, 0x01);
|
| | | }
|
| | |
|
| | | void GPIO_CLOSE(int PortNum)
|
| | | {
|
| | | ioperm(PortNum, 0x01, 0x00);
|
| | | }
|
| | |
|
| | | int GPIO_READ(int PortNum)
|
| | | {
|
| | | return inb(PortNum);
|
| | | }
|
| | |
|
| | | void GPIO_WRITE(int PortNum,unsigned char DATA)
|
| | | {
|
| | | outb(DATA,PortNum);
|
| | | }
|
| | |
|
| | | void EquipCheckGPIOOut(void)
|
| | | {
|
| | |
|
| | | int PortNum = 0xA06; |
| | | int i;
|
| | | int retVal;
|
| | | int result;
|
| | | printf("Step.3 ========>GPIO manupulation===> AnalyEquipmentNum=%d \n",equip_num);
|
| | | for(i=0;i<equip_num;i++)
|
| | | {
|
| | | retVal = -1;
|
| | | result=-1;
|
| | | printf("=============================:%d \n",equipments[i].reboot);
|
| | | result=net_test(equipments[i].ip,equipments[i].port);
|
| | | if(result<0)
|
| | | {
|
| | | printf("Equipment(%d) check=%s:%s\n",i+1,equipments[i].ip,equipments[i].port);
|
| | | retVal = GPIO_OPEN(PortNum); |
| | | if(retVal == 0)
|
| | | {
|
| | | retVal = GPIOoutput(i+1);
|
| | | if(retVal==0) printf("GPIO output OK! \n");
|
| | | GPIO_CLOSE(PortNum);
|
| | | }
|
| | | else printf("GPIO OPEN FAIL! \n");
|
| | | }
|
| | | else printf("Equipment(%d) Check Success!=%s:%s\n",i+1,equipments[i].ip,equipments[i].port);
|
| | | } |
| | | }
|
| | | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
| | | /////////////////////////// added on 2016.11.21
|
| | | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
| | | int memorycmp(unsigned char* src ,int slen, char* token, int len)
|
| | | {
|
| | | int start=0,k,i,klen=strlen(token);
|
| | | lp1:
|
| | | for(i=start;i<slen;i++) if(src[i]==token[0]) break;
|
| | | if (i>=slen) return -1;
|
| | | k=i;
|
| | | for(i=0;i<klen;i++) |
| | | {
|
| | | if(src[k+i]!=token[i])
|
| | | {
|
| | | start=k+i;
|
| | | goto lp1;
|
| | | }
|
| | | }
|
| | | if(i>=klen) return k;
|
| | | return -1;
|
| | | }
|
| | | int stringcmp(char* src ,char* token, int len)
|
| | | {
|
| | | int start=0,k,i,slen=strlen(src);
|
| | | lp:
|
| | | for(i=start;i<slen;i++) if(src[i]==token[0]) break;
|
| | | if (i>=slen) return -1;
|
| | | k=i;
|
| | | for(i=0;i<len;i++) |
| | | {
|
| | | if(src[k+i]!=token[i])
|
| | | {
|
| | | start=k+i;
|
| | | goto lp;
|
| | | }
|
| | | }
|
| | | if(i>=len) return k;
|
| | | return -1;
|
| | | }
|
| | |
|
| | | void AurhorizationResponseBytesReal(char* byte_array_out,int * byte_array_length_out,char *Myuri, char *nonce,char *cnonce,char *response)
|
| | | {
|
| | | // Authorization: Digest username="supervisor", realm="VideoIQ", nonce="n99u1VYBAABAf0HT+h92EO4hT2CzmfxG", uri="/PSIA/VIQ/Event/metadata/0/files", response="7d8d929e39655ad9f3e6e94e9004792e", qop=auth, nc=00000001, cnonce="0d2175d8cdb63f84f18894200e129ace", algorithm="MD5"
|
| | | int idx=0,len;
|
| | | idx = sprintf(byte_array_out,"%s","Authorization: Digest username="); |
| | | byte_array_out[idx++]=0x22;//'"'
|
| | | len = sprintf(&byte_array_out[idx],"%s","supervisor");idx+=len;
|
| | | byte_array_out[idx++]=0x22;//'"'
|
| | | len = sprintf(&byte_array_out[idx],"%s",",realm=");idx+=len;
|
| | | byte_array_out[idx++]=0x22;//'"'
|
| | | len = sprintf(&byte_array_out[idx],"%s","VideoIQ");idx+=len;
|
| | | byte_array_out[idx++]=0x22;//'"'
|
| | | len = sprintf(&byte_array_out[idx],"%s",",nonce=");idx+=len;
|
| | | byte_array_out[idx++]=0x22;//'"'
|
| | | len = sprintf(&byte_array_out[idx], "%s",nonce);idx+=len;//"n99u1VYBAABAf0HT+h92EO4hT2CzmfxG"
|
| | | byte_array_out[idx++]=0x22;//'"'
|
| | | len = sprintf(&byte_array_out[idx],"%s",",uri=");idx+=len;
|
| | | byte_array_out[idx++]=0x22;//'"'
|
| | | len = sprintf(&byte_array_out[idx],"%s",Myuri);idx+=len;
|
| | | byte_array_out[idx++]=0x22;//'"'
|
| | | len = sprintf(&byte_array_out[idx],"%s",",algorithm=");idx+=len;
|
| | | byte_array_out[idx++]=0x22;//'"'
|
| | | len = sprintf(&byte_array_out[idx],"%s","MD5");idx+=len;
|
| | | byte_array_out[idx++]=0x22;//'"'
|
| | | len = sprintf(&byte_array_out[idx],"%s",",cnonce=");idx+=len;
|
| | | byte_array_out[idx++]=0x22;//'"'
|
| | | len = sprintf(&byte_array_out[idx],"%s",cnonce);idx+=len;
|
| | | byte_array_out[idx++]=0x22;//'"'
|
| | | len = sprintf(&byte_array_out[idx],"%s",",nc=00000001,qop=");idx+=len;
|
| | | byte_array_out[idx++]=0x22;//'"'
|
| | | len = sprintf(&byte_array_out[idx],"%s","auth");idx+=len;//cnonce
|
| | | byte_array_out[idx++]=0x22;//'"'
|
| | |
|
| | | len = sprintf(&byte_array_out[idx],"%s",",response=");idx+=len;
|
| | | byte_array_out[idx++]=0x22;//'"'
|
| | | len = sprintf(&byte_array_out[idx],"%s",response);idx+=len;
|
| | | byte_array_out[idx++]=0x22;//'"'
|
| | | *byte_array_length_out = idx;
|
| | | }
|
| | | int CharToByte(char* chars, char* bytes, unsigned int count,char* ptr1,char* ptr2,char* ptr3,char* ptr4,unsigned char* number){ |
| | | int p=0;
|
| | | int n=0;
|
| | | int i=0;
|
| | | unsigned char temp;
|
| | | for(i = 0; i < count; i++)
|
| | | {
|
| | | temp = (unsigned char)chars[i];
|
| | | if (p==1)
|
| | | {
|
| | | if (temp!=0x22)
|
| | | {
|
| | | bytes[n++]=(unsigned char)chars[i];
|
| | | } |
| | | }
|
| | | if (temp==0x22)
|
| | | {
|
| | | p=p+1;
|
| | | }
|
| | | if (p==2)
|
| | | {
|
| | | bytes[n++]=0x2C;
|
| | | p=0; |
| | | }
|
| | |
|
| | | }
|
| | | char* token=NULL;
|
| | | char s[]=",";
|
| | | token=strtok(bytes,s); |
| | | number[0]=strlen(token); |
| | | memcpy(ptr1,token,strlen(token));
|
| | | int nn=0;
|
| | | while(token!=NULL)
|
| | | {
|
| | | token=strtok(NULL,s); |
| | | nn=nn+1; |
| | | if (nn==1)
|
| | | {
|
| | | number[1]=strlen(token);
|
| | | memcpy(ptr2,token,strlen(token));
|
| | | }
|
| | | if (nn==2)
|
| | | {
|
| | | //ptr3=new char[strlen(token)];
|
| | | number[2]=strlen(token);
|
| | | memcpy(ptr3,token,strlen(token));
|
| | | }
|
| | | if (nn==3)
|
| | | {
|
| | | if(token!=NULL)
|
| | | {
|
| | | number[3]=strlen(token);
|
| | | memcpy(ptr4,token,strlen(token));
|
| | | }//ptr3=new char[strlen(token)];
|
| | |
|
| | | }
|
| | | }
|
| | | return n;
|
| | | }
|
| | | //for real event
|
| | | void MakeAuthorizedPacketReal(char* buff,int length, char* Method,char* uri,int Idx)
|
| | | {
|
| | | char Device[150] ;
|
| | | char Realm_buf1[512] ;
|
| | | char nonce_buf2[512] ;
|
| | | char auth_buf3[512] ;
|
| | | char domain_buf4[512] ;
|
| | | unsigned char len[4];
|
| | |
|
| | | memset(Device,0,150);
|
| | | memset(domain_buf4,0,512);
|
| | |
|
| | | CharToByte((char*)buff, Device, length,Realm_buf1,nonce_buf2,auth_buf3,domain_buf4, len);
|
| | | char* cnonce_buf="163acd0e58793a190f29ee879d4b224f";//"08a9d86c1b5e45963cf82f43dca59c51";
|
| | | Realm_buf1[len[0]]=0;
|
| | | nonce_buf2[len[1]]=0;
|
| | | auth_buf3[len[2]]=0;
|
| | | char* nc="00000001";
|
| | | //CString qop;qop=auth_buf3;/////////////////////////////
|
| | | char User[200];
|
| | | memset(User,0,200);
|
| | | sprintf(User,"%s:%s:%s",equipments[Idx].user,Realm_buf1,equipments[Idx].pass);
|
| | |
|
| | | char urii[200];
|
| | | int kk=sprintf(urii,"%s:%s",Method,uri);//PUT:/PSIA/System/reboot";
|
| | | urii[kk]=0;
|
| | | char* A1 = MDString(User);
|
| | | char AA1[33];
|
| | | memset(AA1,0,33);
|
| | | memcpy(AA1,A1,strlen(A1));
|
| | | char* A2 = MDString(urii);
|
| | | char AA2[33];
|
| | | memset(AA2,0,33);
|
| | | memcpy(AA2,A2,strlen(A2));
|
| | | char rrr[200];
|
| | | memset(rrr,0,200);
|
| | | sprintf(rrr,"%s:%s:%s:%s:auth:%s",AA1,nonce_buf2,nc,cnonce_buf,AA2);
|
| | | // printf("%s:%s:%s:%s:auth:%s\n",AA1,nonce_buf2,nc,cnonce_buf,AA2);
|
| | | char* response = MDString(rrr);
|
| | | char res[33];
|
| | | memset(res,0,33);
|
| | | memcpy(res,response,strlen(response));
|
| | |
|
| | | //// 第二次 发送
|
| | | char byte_arrayxxx[2600];
|
| | | int lengthxxx = 0;
|
| | | int iidx = 0;
|
| | | AurhorizationResponseBytesReal(byte_arrayxxx,&lengthxxx,uri,nonce_buf2,cnonce_buf,res);
|
| | |
|
| | | int llen = sprintf(byte_array,"%s %s HTTP/1.1\r\n",Method,uri); iidx+=llen;
|
| | | llen = sprintf(&byte_array[iidx],"Content-Type: text/xml\r\n"); iidx+=llen;
|
| | | strncpy(&byte_array[iidx],byte_arrayxxx,lengthxxx); iidx+=lengthxxx;
|
| | | byte_array[iidx]=0x0d; iidx+=1;
|
| | | byte_array[iidx]=0x0a; iidx+=1;
|
| | | llen = sprintf(&byte_array[iidx],"Host: %s\r\n",equipments[Idx].ip); iidx+=llen;
|
| | | llen = sprintf(&byte_array[iidx],"Content-Length: 0\r\n"); iidx+=llen;
|
| | | // llen = sprintf(&byte_array[iidx],"%s","Connection: Keep_Alive\r\n"); iidx+=llen;
|
| | | byte_array[iidx]=0x0d; iidx+=1;
|
| | | byte_array[iidx]=0x0a; iidx+=1; |
| | | byte_array[iidx]=0;
|
| | |
|
| | | }
|
| | | |
| | | //// 功能:开始查看实时监控
|
| | | //// 输入:分析器IP,用户,密码
|
| | | //// 输出:0(成功), -1(初始化失败),-2(连接失败)
|
| | | int RealEventStart(int Idx)
|
| | | {
|
| | | |
| | | char buff[10240];
|
| | | int bbbb=0;
|
| | | if(equipments[Idx].ip==NULL || equipments[Idx].ip[0]==0) return -1;
|
| | |
|
| | | struct sockaddr_in servaddr;
|
| | | int strlen1=sizeof(servaddr);
|
| | | if((rsock_down[Idx]=socket(PF_INET,SOCK_STREAM,0))<0){
|
| | | perror("socket fail2");
|
| | | return -1;
|
| | | } |
| | | memset(&servaddr,0,strlen1);
|
| | | servaddr.sin_family=AF_INET;
|
| | | inet_pton(AF_INET,equipments[Idx].ip,&servaddr.sin_addr);
|
| | | servaddr.sin_port=htons(atoi(equipments[Idx].port));
|
| | | |
| | | sigset(SIGALRM, u_alarm_handler);//add 2016.11.22
|
| | | alarm(2); |
| | | int rt = connect(rsock_down[Idx],(struct sockaddr*)&servaddr,strlen1); |
| | | alarm(0);
|
| | | sigrelse(SIGALRM); |
| | | if(rt<0){
|
| | | printf("Equipment connect fail-%s:%s\n",equipments[Idx].ip,equipments[Idx].port);
|
| | | close(rsock_down[Idx]);
|
| | | return -1;
|
| | | }
|
| | | |
| | | int length = 0;
|
| | | length = sprintf(byte_array,"POST /PSIA/VIQ/System/sessions/permanent HTTP/1.1\r\nContent-Type: text/xml\r\nHost: %s\r\nContent-Length: 0\r\nConnection: Keep-Alive\r\n\r\n",equipments[Idx].ip); |
| | | write(rsock_down[Idx],byte_array,length);
|
| | | printf("%s",byte_array);
|
| | | while(1){
|
| | | memset(buff,0,sizeof(buff));
|
| | | sigset(SIGALRM, u_alarm_handler);//add 2016.11.22
|
| | | alarm(2);
|
| | | length=read(rsock_down[Idx],&buff,sizeof(buff));
|
| | | alarm(0);
|
| | | sigrelse(SIGALRM); |
| | | printf("Myequipment connect ==================length =%d:\n",length); |
| | | if(length<=0)
|
| | | {
|
| | | close(rsock_down[Idx]);
|
| | | return -1;
|
| | | }
|
| | | if(memorycmp((unsigned char*)buff,length, "WWW-Authenticate:",17)>0 && bbbb==0)
|
| | | {
|
| | | // analyse reception
|
| | | MakeAuthorizedPacketReal(buff,length,"POST","/PSIA/VIQ/System/sessions/permanent",Idx);
|
| | | zclock_sleep(100);
|
| | | write(rsock_down[Idx],byte_array,strlen(byte_array));
|
| | | // printf("%s\n",(char*)byte_array); |
| | | bbbb=1;
|
| | | continue;
|
| | | }
|
| | | if(bbbb==1){
|
| | | int sidx = stringcmp((char*)buff,"<urn:id>",8);
|
| | | if(sidx >=0){//<urn:ViqDateTimeInterval urn:version="1.0" xmlns:urn="urn:videoiq-com"><urn:startTime urn:version="1.0"><urn:date>2016-08-18T01:07:20.749</urn:date><urn:offset>0</urn:offset></urn:startTime><urn:endTime urn:version="1.0"><urn:date>2016-08-29T08:33:48.231</urn:date><urn:offset>0</urn:offset></urn:endTime></urn:ViqDateTimeInterval>
|
| | | int eidx = stringcmp((char*)buff,"</urn:id>",9);
|
| | | if(eidx>=0 && sidx+8<eidx){
|
| | | memcpy(sessions[Idx],&buff[sidx+8],eidx-sidx-8);
|
| | | bbbb=2;
|
| | | printf("SessionID =======> %s\n",(char*)sessions[Idx]); |
| | | break;
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | close(rsock_down[Idx]);
|
| | |
|
| | | if((rsock_down[Idx]=socket(PF_INET,SOCK_STREAM,0))<0){
|
| | | perror("socket fail2");
|
| | | return -1;
|
| | | }
|
| | | memset(&servaddr,0,strlen1);
|
| | | servaddr.sin_family=AF_INET;
|
| | | inet_pton(AF_INET,equipments[Idx].ip,&servaddr.sin_addr);
|
| | | servaddr.sin_port=htons(80);
|
| | | sigset(SIGALRM, u_alarm_handler);//add 2016.11.22
|
| | | alarm(2); |
| | | rt = connect(rsock_down[Idx],(struct sockaddr*)&servaddr,strlen1);
|
| | | alarm(0);
|
| | | sigrelse(SIGALRM); |
| | | if(rt<0){
|
| | | printf("Equipment connect fail-%s:%d\n",equipments[Idx].ip,80);
|
| | | close(rsock_down[Idx]);
|
| | | return -2;
|
| | | }
|
| | | char uri[200];
|
| | | sprintf(uri,"/PSIA/Custom/Event/notification/notificationStream?sessionId=%s",sessions[Idx]);
|
| | | length = sprintf(byte_array,"GET %s HTTP/1.1\r\nContent-Type: text/xml\r\nHost: %s\r\nContent-Length: 0\r\nConnection: Keep-Alive\r\n\r\n",uri,equipments[Idx].ip); |
| | | write(rsock_down[Idx],byte_array,strlen(byte_array));
|
| | | while(1){
|
| | | memset(buff,0,sizeof(buff));
|
| | | sigset(SIGALRM, u_alarm_handler);//add 2016.11.22
|
| | | alarm(2);
|
| | | length=read(rsock_down[Idx],&buff,sizeof(buff)); |
| | | alarm(0);
|
| | | sigrelse(SIGALRM); |
| | | if(length<=0)
|
| | | {
|
| | | close(rsock_down[Idx]);
|
| | | break;
|
| | | }
|
| | | if(memorycmp((unsigned char*)buff,length, "WWW-Authenticate:",17)>0 && bbbb==2)
|
| | | {
|
| | | char uri[200];
|
| | | sprintf(uri,"/PSIA/Custom/Event/notification/notificationStream?sessionId=%s",sessions[Idx]);
|
| | | MakeAuthorizedPacketReal(buff,length,"GET",uri,Idx);
|
| | | write(rsock_down[Idx],byte_array,strlen(byte_array));
|
| | | // printf("%s\n",(char*)byte_array); |
| | | bbbb=3; |
| | | continue;
|
| | | }
|
| | | if(stringcmp(buff,"multipart/text; boundary=VIQeventEVENTeventVIQ",46)>0 && bbbb==3) |
| | | {
|
| | | pthread_create(&thread_Real[Idx],NULL,&MyEquipmentthread,(void*)Idx);//super(void*)(&equipments[0])
|
| | | gCapturing[Idx]=1;
|
| | | printf("%s\n","QQQQQQQQQQQQQQQQQQQQ Capturing...");
|
| | | //printf("%s\n",buff);
|
| | | return 0;
|
| | | }
|
| | | }
|
| | | return -2;
|
| | | }
|
| | | |
| | | |
| | |
|
| | |
|
| | |
|