Exemple #1
0
void threaded_resolve(char *list[]) {
  int i = 0, j, k;
  char dom[MAXSTRSIZE] = "", *foo = "", buf[1024];
  void *addr, *addr4;
  struct addrinfo hints, *res, *p;
  int status, found2;
  char ipv6str[INET6_ADDRSTRLEN], ipv4str[16];
  struct sockaddr_in6 *ipv6, *q;
  struct sockaddr_in *ipv4, *q4;

  memset(&hints, 0, sizeof hints);
  hints.ai_family = dof;        // AF_INET or AF_INET6 to force version

  if (type == NULL)
    type = foo;

  i = 0;
  while (list[i] != NULL && list[i][0] != 0) {
    snprintf(dom, sizeof(dom), "%s.%s", list[i], domain);
#if DEBUG
    printf("brute-forced domain: %s\n", dom);
#endif
    // ipv6 code modded from www.kame.net
    if ((status = getaddrinfo(dom, NULL, &hints, &res)) == 0) {
      q = NULL;
      q4 = NULL;
      found2 = 0;
      for (p = res; p != NULL; p = p->ai_next) {
        if (do6 && p->ai_family == AF_INET6) {  // IPv6
          ipv6 = (struct sockaddr_in6 *) p->ai_addr;
          addr = &(ipv6->sin6_addr);
          // convert the IP to a string and print it:
          if (q == NULL || memcmp(&ipv6->sin6_addr, &q->sin6_addr, 16) != 0) {
            q = ipv6;
            k = 1;
            if (wcard)
              for (j = 0; j < wcard; j++)
                if (memcmp(addr, wildcardIpStr[j], 16) == 0)
                  k = 0;
            if (k) {
              if (found2 == 0) {
                ++found;
                found2 = 1;
              }
              inet_ntop(p->ai_family, addr, ipv6str, sizeof ipv6str);
              sprintf(buf, "%s => %s\n", dom, ipv6str);
              printf("%s", buf);
              ++ipCount;
              if (ucount < MAX_UNIQUE) {
                if (ucount)
                  for (j = 0; j < ucount; j++)
                    if (memcmp(addr, unique[j], 16) == 0)
                      k = 0;
                if (k) {
                  memcpy(unique[ucount], addr, 16);
                  ucount++;
                }
              }
            }
          }
        } else if (do4 && p->ai_family == AF_INET) {
          ipv4 = (struct sockaddr_in *) p->ai_addr;
          addr4 = &(ipv4->sin_addr);
          if (q4 == NULL || memcmp(&ipv4->sin_addr, &q4->sin_addr, 4) != 0) {
            q4 = ipv4;
            k = 1;
            if (wcard4)
              for (j = 0; j < wcard4; j++)
                if (memcmp(wildcardIpStr4[j], addr4, 4) == 0)
                  k = 0;
            if (k) {
              if (found2 == 0) {
                ++found;
                found2 = 1;
              }
              inet_ntop(p->ai_family, addr4, ipv4str, sizeof ipv4str);
              sprintf(buf, "%s => %s\n", dom, ipv4str);
              printf("%s", buf);
              ++ipCount4;
              if (ucount4 < MAX_UNIQUE) {
                if (ucount4)
                  for (j = 0; j < ucount4; j++)
                    if (memcmp(addr4, unique4[j], 4) == 0)
                      k = 0;
                if (k) {
                  memcpy(unique4[ucount4], addr4, 4);
                  ucount4++;
                }
              }
            }
          }
        }

      }
      freeaddrinfo(res);        // free the linked list
    }                           // end of if conditional
    // user wants delay between DNS requests?
    if (do_a_delay)
      dodelay(milliseconds);
    i++;
  }
  return;
}
Exemple #2
0
void threaded_resolve_srv(char *list[]) {
  char dom[MAXSTRSIZE] = "", buf[1024];
  int len, cnt, i, j, k, found = 0, found1, found4 = 0, found2;
  unsigned short int *port, *prio, *weight;
  unsigned char vbuf[1500], *vptr;
  char dbuf[256];
  struct addrinfo hints, *res, *p;
  struct sockaddr_in6 *ipv6, *q;
  char ipv6str[INET6_ADDRSTRLEN], ipv4str[16];
  struct sockaddr_in *ipv4, *q4;
  void *addr, *addr4;

  if (type == NULL)
    return;
  memset((char *) &hints, 0, sizeof(hints));
  hints.ai_family = dof;

  i = 0;
  while (list != NULL && list[i] != NULL && list[i][0] != 0) {
    snprintf(dom, sizeof(dom), "%s.%s.%s", list[i], type, domain);
    memset(vbuf, 0, 4);
#if DEBUG
    printf("brute-forced domain: %s\n", dom);
#endif
    if ((len = res_query(dom, ns_c_in, ns_t_srv, vbuf, sizeof(vbuf))) > 0 && (vbuf[3] & 15) == 0 && (cnt = vbuf[7]) > 0) {
      vptr = vbuf + strlen(dom) + 17;
      ucountsrv++;
      for (j = 0; j < cnt && len - (vptr - vbuf) >= 20; j++) {
        vptr += 10;
        k = vptr[1] + vptr[0] * 256;
        vptr += 2;
        prio = (unsigned short int *) (vptr);
        weight = (unsigned short int *) (vptr + 2);
        port = (unsigned short int *) (vptr + 4);
        dbuf[0] = 0;
        dn_expand(vbuf, vbuf + len, vptr + 6, dbuf, sizeof(dbuf));
        if (getaddrinfo(dbuf, NULL, &hints, &res) == 0) {
          q = NULL;
          q4 = NULL;
          found1 = 0;
          found2 = 0;
          for (p = res; p != NULL; p = p->ai_next) {
            if (do6 && p->ai_family == AF_INET6) {      // IPv6
              ipv6 = (struct sockaddr_in6 *) p->ai_addr;
              addr = &(ipv6->sin6_addr);
              if (q == NULL || memcmp(&ipv6->sin6_addr, &q->sin6_addr, 16) != 0) {
                q = ipv6;
                // convert the IP to a string and print it:
                inet_ntop(p->ai_family, addr, ipv6str, sizeof ipv6str);
                snprintf(buf, sizeof(buf), "%s => %s is %s port %d %s (prio %d weight %d)\n", dom, dbuf, ipv6str, htons(*port), strcmp(type, "_tcp") == 0 ? "TCP" : "UDP",
                         htons(*prio), htons(*weight));
                printf("%s", buf);
                if (found1 == 0) {
                  ++found;
                  found1 = 1;
                }
              }
            } else if (do4 && p->ai_family == AF_INET) {        // IPv4
              ipv4 = (struct sockaddr_in *) p->ai_addr;
              addr4 = &(ipv4->sin_addr);
              if (q4 == NULL || memcmp(&ipv4->sin_addr, &q4->sin_addr, 4) != 0) {
                q4 = ipv4;
                // convert the IP to a string and print it:
                inet_ntop(p->ai_family, addr4, ipv4str, sizeof ipv4str);
                snprintf(buf, sizeof(buf), "%s => %s is %s port %d %s (prio %d weight %d)\n", dom, dbuf, ipv4str, htons(*port), strcmp(type, "_tcp") == 0 ? "TCP" : "UDP",
                         htons(*prio), htons(*weight));
                printf("%s", buf);
                if (found2 == 0) {
                  ++found4;
                  found2 = 1;
                }
              }
            }
          }
          freeaddrinfo(res);
        } else {
          snprintf(buf, sizeof(buf), "%s => %s port %d %s (prio %d weight %d)\n", dom, dbuf, htons(*port), strcmp(type, "_tcp") == 0 ? "TCP" : "UDP", htons(*prio),
                   htons(*weight));
          printf("%s", buf);
        }
        vptr += k;
        ucountsrvs++;
      }
    }
//else printf("srv: %s %d len %d code %d valid\n", dom, len, (vbuf[3] & 15), vbuf[2]);

    if ((vbuf[3] & 15) == 0 && vbuf[2] > 0) {
      memset(vbuf, 0, 4);
      if ((len = res_query(dom, ns_c_in, ns_t_any, vbuf, sizeof(vbuf))) > 0 && (vbuf[3] & 15) == 0 && (cnt = vbuf[7]) > 0) {
        if ((vbuf[2] & 2) == 2)
          fprintf(stderr, "Warning: truncated answer for entry \"%s\" type ANY\n", dom);
        vptr = vbuf + strlen(dom) + 17;
        ucountsrv++;
        for (j = 0; j < cnt && len - (vptr - vbuf) >= 20; j++) {
          k = vptr[11] + vptr[10] * 256;
          if (vptr[2] == 0 && vptr[3] == 0x0c) {
            dbuf[0] = 0;
            dn_expand(vbuf, vbuf + len, vptr + 12, dbuf, sizeof(dbuf));
            if (getaddrinfo(dbuf, NULL, &hints, &res) == 0) {
              q = NULL;
              q4 = NULL;
              found1 = 0;
              found2 = 0;
              for (p = res; p != NULL; p = p->ai_next) {
                if (do6 && p->ai_family == AF_INET6) {  // IPv6
                  ipv6 = (struct sockaddr_in6 *) p->ai_addr;
                  addr = &(ipv6->sin6_addr);
                  if (q == NULL || memcmp(&ipv6->sin6_addr, &q->sin6_addr, 16) != 0) {
                    q = ipv6;
                    // convert the IP to a string and print it:
                    inet_ntop(p->ai_family, addr, ipv6str, sizeof ipv6str);
                    snprintf(buf, sizeof(buf), "%s => %s is %s\n", dom, dbuf, ipv6str);
                    printf("%s", buf);
                    if (found1 == 0) {
                      ++found;
                      found1 = 1;
                    }
                  }
                } else if (do4 && p->ai_family == AF_INET) {    // IPv4
                  ipv4 = (struct sockaddr_in *) p->ai_addr;
                  addr4 = &(ipv4->sin_addr);
                  if (q4 == NULL || memcmp(&ipv4->sin_addr, &q4->sin_addr, 4) != 0) {
                    q4 = ipv4;
                    // convert the IP to a string and print it:
                    inet_ntop(p->ai_family, addr4, ipv4str, sizeof(ipv4str));
                    snprintf(buf, sizeof(buf), "%s => %s is %s\n", dom, dbuf, ipv4str);
                    printf("%s", buf);
                    if (found2 == 0) {
                      ++found4;
                      found2 = 1;
                    }
                  }
                }
              }
              freeaddrinfo(res);
            } else {
              snprintf(buf, sizeof(buf), "%s => %s\n", dom, dbuf);
              printf("%s", buf);
            }
          }
          vptr += k + 12;
          ucountsrvs++;
        }
      }
//else printf("ptr: %s %d len %d code %d valid\n", dom, len, (vbuf[3] & 15), vbuf[2]);
    }
    // user wants delay between DNS requests?
    if (do_a_delay)
      dodelay(milliseconds);
    i++;
  }
  return;
}
/**
 \brief Make sure the periodic communication with 2FOC board is on
 \details 
 \pre 
 \return 
   0: everything ok.
   ....: failed to initialize periodic communication */
int setup_2FOC_periodic()
{
    int  nbytes;
    int n;
    // \todo use the global variable for status/error 
    FOC_data_t *focrxdata = NULL;

    // zero contents of databuffers
    for(n = 0; n < num_chan ; n++) {
        *(FOC_data_array[n].setpoint) = 0;
        *(FOC_data_array[n].feedback) = 0;
        *(FOC_data_array[n].focstatus) = 0;
        *(FOC_data_array[n].focerror)  = 0;
        FOC_data_array[n].dbg       = 0;
    }

    // for each FOC axis send 'shutdown', 'switch on' and 'enable operation' commands
    for (n = 0; n < num_chan; n++) {

        // 0 bytes payload
        txframe[n].can_dlc = 0;

        // send 'shutdown' command
        // all FOCs have adx 3
        txframe[n].can_id = 0x83FFFF11;
        nbytes = write (sock[n], &txframe[n], sizeof (struct can_frame));
        if (sizeof (struct can_frame) != nbytes){
            rtapi_print_msg(RTAPI_MSG_ERR,"HAL_ZED_CAN: Unable to send 'shut down' command to axis %d",n);
            hal_exit(comp_id);
            exit(-1);
        }
        dodelay(20000);

        // send 'switch on' command
        // txframe[n].can_id = 0x80FFFF0E | ((n+1) << 24);
        // all FOC has adx 3
        txframe[n].can_id = 0x83FFFF0E;
        nbytes = write (sock[n], &txframe[n], sizeof (struct can_frame));
        if (sizeof (struct can_frame) != nbytes){
            rtapi_print_msg(RTAPI_MSG_ERR,"HAL_ZED_CAN: Unable to send 'switch on' command to axis %d",n);
            hal_exit(comp_id);
            exit(-1);
        }
        dodelay(20000);

        // send 'enable operation' command
        // txframe[n].can_id = 0x80FFFF0F | ((n+1) << 24);
        txframe[n].can_id = 0x83FFFF0F;
        nbytes = write (sock[n], &txframe[n], sizeof (struct can_frame));
        if (sizeof (struct can_frame) != nbytes){
            rtapi_print_msg(RTAPI_MSG_ERR,"HAL_ZED_CAN: Unable to send 'enable operation' command to axis %d",n);
            hal_exit(comp_id);
            exit(-1);
        }     
        dodelay(20000);
    }
    rtapi_print_msg(RTAPI_MSG_INFO, "HAL_ZED_CAN: ShutDown,SwitchOn, EnableOperation sent to all channels.");

    
    // \todo Check anything possible before waiting for alignment
    // - errors of any kind
    // - board switched on and operationenabled
    // - correct configuration (speed loop)

    // point to first axis data
    focrxdata = FOC_data_array;

    // wait for completion of the rotor alignment (flag in status message)
    for (n = 0; n < num_chan; n++) {
        int nbytes=0;
    
        do {
            // read CAN packet
            nbytes = read (sock[n], &rxframe[n], sizeof (struct can_frame));
            if (nbytes >= 0) {
                // parse packet type
                if ( 0 != ParseMessage(&(rxframe[n]), n, focrxdata) ){
                    //exit(-1);
                }
            }
        }
        // rotor alignment completed 
        while ( 0 == (*(focrxdata->focstatus) & 0x00020000 ) );
  
        // \todo Check anything possible before living control to machinekit:
        // - errors of any kind
        // - correct configuration (speed loop)
        // - 2FOC firmware version
        // - board switched on and operationenabled
        rtapi_print_msg(RTAPI_MSG_INFO, "HAL_ZED_CAN: axis %d aligment completed.",n);

        // enable periodic rx & tx
        FOCAxisIsOperationEnable[n] = true;

        // move to next axis
        focrxdata++;
    }

    // everityng is ok!
    return 0;
}