Ejemplo n.º 1
0
static int readlist(char *file_name, list addr[], int maxsize)
{
	int nlist = 0;
	xid data;
	FILE *in_file;

	// Auxiliary variables
	char tmp_data[41] = {0};
	int loop;

	if (!(in_file = fopen(file_name, "rb")))
	{
		perror(file_name);
		exit(-1);
	}

	while (fscanf(in_file, "%s", &tmp_data) != EOF)
	{
		if (nlist >= maxsize) return -1;
		addr[nlist] = (list) malloc(sizeof(struct xid));

		for (loop=0;loop<20;loop++)
		{
			data.w[loop] = hextochar(tmp_data[2*loop], tmp_data[2*loop + 1]);
		}
		*addr[nlist] = data;
		nlist++;
	}
	return nlist;
}
void ICACHE_FLASH_ATTR pn532_send_read_cmd_cb() {
	os_timer_disarm(&pn532readID);
	uint8 uid[4];
	uint8 *uidlen;
	bool a = readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid, &uidlen, 20);
	uint8 i;
	if (a) {
#ifdef MENJINDEBUG
		os_printf("\nUID:");
		for (i = 0; i < 4; i++) {
			os_printf("0x%x ", uid[i]);
		}
		os_printf("\n");
#endif
		char *id = "ID:";
		hextochar(uidstr, uid, 4);
		uidstr[8] = '\0';
		char *json_buf = NULL;
		json_buf = (char *) os_zalloc(jsonSize);
		json_ws_send((struct jsontree_value *) &cardid_info_tree, "cardID",
				json_buf);
		MQTT_Publish(&mqttClient, "/tc503/onCardRead", json_buf,
				strlen(json_buf), 0, 0);
		os_free(json_buf);
		json_buf = NULL;
	}
	os_timer_arm(&pn532readID, PN532_SEND_READ_CMD_DELAY, 1);
}
Ejemplo n.º 3
0
char *
urldecode(const char *str)
{
	char *ret, c;
	int i, reallen;

	if (str == NULL)
		return NULL;
	if ((ret = malloc(strlen(str)+1)) == NULL)
		err(1, "Can't allocate memory for URL decoding");
	for (i = 0, reallen = 0; str[i] != '\0'; i++, reallen++, ret++) {
		c = str[i];
		if (c == '+') {
			*ret = ' ';
			continue;
		}

		/* Cannot use strtol here because next char
		 * after %xx may be a digit.
		 */
		if (c == '%' && isxdigit(str[i+1]) && isxdigit(str[i+2])) {
			*ret = hextochar(&str[i+1]);
			i+=2;
			continue;
		}
		*ret = c;
	}
	*ret = '\0';

	return ret-reallen;
}
Ejemplo n.º 4
0
int SendBlyssCmd(int id,int value)
{
	char timestamp=0;
	static char key_index=0;
	char key[]= {0x98, 0xDA, 0x1E, 0xE6, 0x67};

	long            ms; // Milliseconds
	time_t          s;  // Seconds
	struct timespec spec;

	clock_gettime(CLOCK_REALTIME, &spec);

	s  = spec.tv_sec;
	ms = spec.tv_nsec / 326; // Convert nanoseconds to milliseconds

	timestamp=(311*ms)/1000;

	//0x98 -> 0xDA -> 0x1E -> 0xE6 -> 0x67


	char cmd[17]="FE6142280981C0 ";

	/*
	cmd[3]='0';
	cmd[4]='0';
	cmd[5]='0';
	 */
	cmd[6]=(id%10)+'0';


	cmd[8]= (value>0)? '0' : '1';

	cmd[9]=hextochar(key[key_index]>>4);
	cmd[10]=hextochar(key[key_index]&0xF);

	cmd[11]=hextochar(timestamp>>4);
	cmd[12]=hextochar(timestamp&0xF);

	info("BLYSS","Command Send: %s",cmd);

	key_index=(key_index+1)%sizeof(key);

	return write(fd_rf,cmd,strlen(cmd));
}
static int readentries(char *file_name, entry_t entry[], int maxsize)
{
	int nentries = 0;
	xid data, nexthop;
	int len;
	FILE *in_file;

	// Auxiliary variables
	char tmp_data[41] = {0};
	char tmp_nexthop[41] = {0};
	int loop;

	if (!(in_file = fopen(file_name, "rb")))
	{
		perror(file_name);
		exit(-1);
	}

	while (fscanf(in_file, "%s%i%s", &tmp_data, &len, &tmp_nexthop) != EOF)
	{
		if (nentries >= maxsize) return -1;
		entry[nentries] = (entry_t) malloc(sizeof(struct entryrec));

		for (loop=0;loop<20;loop++)
		{
			data.w[loop] = hextochar(tmp_data[2*loop], tmp_data[2*loop + 1]);
			nexthop.w[loop] = hextochar(tmp_nexthop[2*loop], tmp_nexthop[2*loop + 1]);
		}
		// extract the prefix from the bit pattern
		for (loop=0;loop<(len/8);loop++);
		data.w[loop] = data.w[loop] >> (8 - (len%8)) << (8 - (len%8));
		loop++;
		for (;loop<20;loop++)
			data.w[loop] = (char) 0;

		entry[nentries]->data = data;
		entry[nentries]->len = len;
		entry[nentries]->nexthop = nexthop;
		nentries++;
	}
	return nentries;
}
Ejemplo n.º 6
0
/**
 * This function initializes the tcp context global instance to 0 except for
 * file descriptor descriptor for stdin, stdout and stderr.
 *
 * @author    Mohd Naushad Ahmed
 * @version   1.0
 * @param     none
 * @return    none
 */
char *http_encode_pap_password (char *password,
                                char *uam_secret,
                                char *challenge)
{
  int            n,m;
  unsigned char  user_password[RADIUS_PWSIZE + 1];
  unsigned char  p[RADIUS_PWSIZE + 1];
  int            plen = -1; 
  char *buffer;
  MD5_CTX context;
  
  /* challenge  */
  buffer = (char*) malloc(512);
  memset (buffer, 0, sizeof(buffer));
  strncpy ((char *)buffer, (const char *)challenge, strlen(challenge));
  
  /* converting from hex to character string */
  hextochar (buffer, challenge, MD5LEN);
  
  /* uamsecret */
  MD5Init(&context);
  MD5Update(&context, challenge, MD5LEN);
  MD5Update(&context, (unsigned char*)uam_secret, strlen(uam_secret));
  MD5Final(challenge, &context);
  
  /* password */
  
  memset(p, 0, sizeof(p));
  
  strncpy((char *)p, (const char *)password, (size_t)strlen(password));
  plen = strlen(password);

  for (m=0; m < plen;)
  {
    for (n=0; n < REDIR_MD5LEN; m++, n++)
    {
      user_password[m] = p[m] ^ challenge[n];
    }
  }
  
  chartohex(user_password, buffer, plen);
  return (buffer);
}/* http_encode_pap_password */
static int readlist(char *file_name, entry_t entry[], int maxsize)
{
	int nentry = 0;
	xid data, nextid;
	FILE *in_file;

	// Auxiliary variables
	char tmp_xid[41] = {0};
	int i, len;

	if (!(in_file = fopen(file_name, "rb")))
	{
		perror(file_name);
		exit(-1);
	}
	memset(&nextid, 0, 20);

	while (fscanf(in_file, "%s%d", &tmp_xid, &len) != EOF)
	{
		if (nentry >= maxsize) return -1;
		entry[nentry] = (entry_t) malloc(sizeof(struct entryrec));
		
		for (i=0;i<20;i++)
			data.w[i] = hextochar(tmp_xid[2*i], tmp_xid[2*i + 1]);
			
		for (i=0;i<(len/8);i++);
		data.w[i] = data.w[i] >> (8 - (len%8)) << (8 - (len%8));
		i++;
		for (;i<20;i++)
			data.w[i] = (char) 0;
		entry[nentry]->data = data;
		entry[nentry]->len = len;
		entry[nentry]->nexthop = nextid;
		entry[nentry]->pre = NOPRE;
		nentry++;
	}
	return nentry;
}
Ejemplo n.º 8
0
Archivo: util.c Proyecto: SICOM/rlib
void rlib_parsecolor(struct rlib_rgb *color, const gchar *strx) {
	const gchar *str = colornames(strx);
	if(str != NULL && r_strlen(str) == 8) {
		guchar r;
		guchar g;
		guchar b;
		r = (hextochar(str[2]) << 4) | hextochar(str[3]);
		g = (hextochar(str[4]) << 4) | hextochar(str[5]);
		b = (hextochar(str[6]) << 4) | hextochar(str[7]);
		color->r = (gfloat)r/(gfloat)0xFF;
		color->g = (gfloat)g/(gfloat)0xFF;
		color->b = (gfloat)b/(gfloat)0xFF;
	} else {
		color->r = -1;
		color->g = -1;
		color->b = -1;
	}
}
Ejemplo n.º 9
0
int mresponse_main(int argc, char **argv) {
  uint8_t chap_ident = 0;
  uint8_t challenge[32];
  char buffer[512];
  MD5_CTX context;

  int idx = 0;
  int usent = 0;
  int usepap = 0;

  if (argc < 2)
    return usage(argv[0]);

  if (!strcmp(argv[1],"-nt")) {
    usent = 1;
    argc--;
    idx++;
  }

  if (!strcmp(argv[1],"-pap")) {
    usepap = 1;
    argc--;
    idx++;
  }

  if (usent && usepap)
    return usage(argv[0]);

  if (argc < 4)
    return usage(argv[0]);

  if (argc == 5) 
    chap_ident = atoi(argv[idx+4]);

  /* challenge - argv 1 */
  memset(buffer, 0, sizeof(buffer));
  strcpy(buffer, argv[idx+1]);
  hextochar(buffer, challenge, MD5LEN);

  /* uamsecret - argv 2 */
  MD5Init(&context);
  MD5Update(&context, challenge, MD5LEN);
  MD5Update(&context, (uint8_t*)argv[idx+2], strlen(argv[idx+2]));
  MD5Final(challenge, &context);

  if (usepap) {
    uint8_t user_password[RADIUS_PWSIZE + 1];
    uint8_t p[RADIUS_PWSIZE + 1];
    int m, n, plen = strlen(argv[idx+3]);
    
    memset(p, 0, sizeof(p));
    safe_strncpy((char *)p, argv[idx+3], RADIUS_PWSIZE);
    
    for (m=0; m < plen;) {
      for (n=0; n < REDIR_MD5LEN; m++, n++) {
	user_password[m] = p[m] ^ challenge[n];
      }
    }
    
    chartohex(user_password, buffer, plen);
    printf("%s\n", buffer);
    
  } else if (usent) {
    
#ifdef HAVE_OPENSSL
    uint8_t ntresponse[24];

    if (argc < 5)
      return usage(argv[0]);

    GenerateNTResponse(challenge, challenge,
		       (uint8_t*)argv[idx+3], strlen(argv[idx+3]),
		       (uint8_t*)argv[idx+4], strlen(argv[idx+4]),
		       ntresponse);
    chartohex(ntresponse, buffer, 24);
    printf("%s\n", buffer);

#else

    printf("Requires OpenSSL Support\n");

#endif

  } else {
    uint8_t response[32];

    /* password - argv 3 */
    MD5Init(&context);
    MD5Update(&context, (uint8_t*)&chap_ident, 1);	  
    MD5Update(&context, (uint8_t*)argv[idx+3], strlen(argv[idx+3]));
    MD5Update(&context, challenge, MD5LEN);
    MD5Final(response, &context);
    
    chartohex(response, buffer, MD5LEN);
    printf("%s\n", buffer);
  }

  return 0;
}
Ejemplo n.º 10
0
char readgps()
   {
   while (lib_serial_numcharsavailable(GPS_SERIAL_PORT))
      {
      char c=lib_serial_getchar(GPS_SERIAL_PORT);

      if (c=='$') // start of a new message
         {
         gpsparameternumber=0;
         gpsdataindex=0;
         gpschecksum=0;
         gpsframetype=0;
         }
      else
         {
         if (c==',' || c=='*')
            { // we just finished loading a parameter, interpret it
            gpsdata[gpsdataindex]='\0';

            if (gpsparameternumber == 0) 
               { //frame identification
               if (gpsdata[0] == 'G' && gpsdata[1] == 'P' && gpsdata[2] == 'G' && gpsdata[3] == 'G' && gpsdata[4] == 'A') gpsframetype=FRAME_GGA;
               else if (gpsdata[0] == 'G' && gpsdata[1] == 'P' && gpsdata[2] == 'R' && gpsdata[3] == 'M' && gpsdata[4] == 'C') gpsframetype = FRAME_RMC;
               } 
            else if (gpsframetype == FRAME_GGA) 
               {
               if (gpsparameternumber == 2) 
                  {
                  gpslatitudedegrees = gpsstringtoangle(gpsdata);
                  }
               else if (gpsparameternumber == 3)
                  {
                  if ( gpsdata[0] == 'S') gpslatitudedegrees=-gpslatitudedegrees;
                  }
               else if (gpsparameternumber == 4)
                  {
                  gpslongitudedegrees = gpsstringtoangle(gpsdata);
                  }
               else if (gpsparameternumber == 5)
                  {
                  if ( gpsdata[0] == 'W') gpslongitudedegrees=-gpslongitudedegrees;
                  }
               else if (gpsparameternumber == 6)
                  {
                  gpsfix = gpsdata[0]-'0';
                  }
               else if (gpsparameternumber == 7)
                  {
                  global.gps_num_satelites = lib_fp_stringtolong(gpsdata);
                  }
               else if (gpsparameternumber == 9)
                  {
                  global.gps_current_altitude = lib_fp_stringtofixedpointnum(gpsdata);
                  }
               } 
            else if (gpsframetype == FRAME_RMC) 
               {
               if (gpsparameternumber == 7)
                  {
                  // 1 knot = 0.514444444 m / s
                  global.gps_current_speed=lib_fp_multiply(lib_fp_stringtofixedpointnum(gpsdata),33715L); // 33715L is .514444444 * 2^16
                  }
               else if (gpsparameternumber == 8) 
                  {
//                  GPS_ground_course = grab_fields(gpsdata,1); 
                  }                 //ground course deg*10 
               }
            
            ++gpsparameternumber;
            gpsdataindex = 0; // get read for the next parameter
            if (c == '*') gpsfinalchecksum=gpschecksum;
            }
         else if (c == '\r' || c == '\n') 
            { // end of the message
            unsigned char checksum = hextochar(gpsdata[0]);
            checksum <<= 4;
            checksum += hextochar(gpsdata[1]);

            if (checksum != gpsfinalchecksum || gpsframetype!=FRAME_GGA || !gpsfix) return(0);
            gpsframetype=0; // so we don't check again

            global.gps_current_latitude=gpslatitudedegrees;
            global.gps_current_longitude=gpslongitudedegrees;
            
            return(1); // we got a good frame
            }
         else if (gpsdataindex<GPSDATASIZE)
            {
            gpsdata[gpsdataindex++]=c;
            }
         gpschecksum^=c;
         }
      }
   return(0); // no complete data yet
   }