Esempio n. 1
0
int
add_rdr(u_int32_t id, struct sockaddr *src, int s_rd, struct sockaddr *dst,
    u_int16_t d_port, struct sockaddr *rdr, u_int16_t rdr_port, int d_rd)
{
	if (!src || !dst || !d_port || !rdr || !rdr_port ||
	    (src->sa_family != rdr->sa_family)) {
		errno = EINVAL;
		return (-1);
	}

	if (prepare_rule(id, src, dst, d_port) == -1)
		return (-1);

	if (add_addr(rdr, &pfr.rule.rdr) == -1)
		return (-1);

	pfr.rule.direction = PF_IN;
	pfr.rule.onrdomain = s_rd;
	pfr.rule.rtableid = d_rd;
	pfr.rule.rdr.proxy_port[0] = rdr_port;
	if (ioctl(dev, DIOCADDRULE, &pfr) == -1)
		return (-1);

	return (0);
}
Esempio n. 2
0
int
add_nat(u_int32_t id, struct sockaddr *src, int s_rd, struct sockaddr *dst,
    u_int16_t d_port, struct sockaddr *nat, u_int16_t nat_range_low,
    u_int16_t nat_range_high)
{
	if (!src || !dst || !d_port || !nat || !nat_range_low ||
	    !nat_range_high || (src->sa_family != nat->sa_family)) {
		errno = EINVAL;
		return (-1);
	}

	if (prepare_rule(id, src, dst, d_port) == -1)
		return (-1);

	if (add_addr(nat, &pfr.rule.nat) == -1)
		return (-1);

	pfr.rule.direction = PF_OUT;
	pfr.rule.onrdomain = s_rd;
	pfr.rule.rtableid = -1;
	pfr.rule.nat.proxy_port[0] = nat_range_low;
	pfr.rule.nat.proxy_port[1] = nat_range_high;
	if (ioctl(dev, DIOCADDRULE, &pfr) == -1)
		return (-1);

	return (0);
}
void Mouse_Hexdump_Area::dec_cursor_pos()
{
  old_addr = addr_being_edited;
  cursor.toggle=1;
  if (!highnibble)
  {
    highnibble = 1;
  }
  else
  {
    if (res_x == 0)
    {
      res_x = 7;
      if (res_y == 0)
      {
        add_addr(-8);
      }
      else res_y--;
    }
    else
      res_x--;

    highnibble = 0;
  }
  
  update_editing_address();
}
void Mouse_Hexdump_Area::inc_cursor_pos()
{
  old_addr = addr_being_edited;
  cursor.toggle=1;
  if (highnibble)
  {
    highnibble = 0;
  }
  else
  {
    if (res_x == 7)
    {
      res_x = 0;
      if (res_y == 15)
      {
        add_addr(8);
      }
      else res_y++;


    }
    else
      res_x++;

    highnibble = 1;
  }
  update_editing_address();
}
Esempio n. 5
0
std::vector<sockaddrunion> dtls_dispatch::detect_local_addrs(uint32_t nbo_tun_addr)
{
	dout() << "detect_local_addrs()";
	std::vector<sockaddrunion> addrs;
#ifdef WINDOWS
	dbuf buf(15000); // per MSFT docs
	ULONG size = buf.size();
	IP_ADAPTER_ADDRESSES* adapter_addresses = (IP_ADAPTER_ADDRESSES*)(buf.data());
	// GAA_FLAG_INCLUDE_GATEWAYS would provide gateways, may be useful for NAT-PCP implementation
		// (but FirstGateway address was only added to structure in Vista, so may be an issue for XP/2003)
	if(GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME, nullptr, adapter_addresses, &size) != NO_ERROR) {
		dout() << "GetAdaptersAddresses returned error";
		return addrs;
	}
	for(IP_ADAPTER_ADDRESSES* adapter = adapter_addresses; adapter != nullptr; adapter = adapter->Next) {
		for(IP_ADAPTER_UNICAST_ADDRESS* addr = adapter->FirstUnicastAddress; addr != nullptr; addr = addr->Next) {
			add_addr(addrs, reinterpret_cast<const sockaddrunion*>(addr->Address.lpSockaddr), nbo_tun_addr);
		}
	}
#else
	ifaddrs* ifad;
	if(getifaddrs(&ifad) == -1) {
		wout_perr() << "getifaddrs could not get local IP addrs";
		return addrs;
	}
	// TODO: link local addrs: what to do?
		// should bind them just to keep anything else from doing so
		// not sure whether to keep them; in theory they're addresses and could be used 
		// and sometimes you may even want to: suppose you can't find any DHT peers (e.g. LAN party with no internet)
		// so you send out a broadcast msg to the snow port asking for some
		// a local peer responds and supplies its link local addr and hashkey and you're up and running completely zeroconf
		// that would give you the ability to just plug machines into a switch or create an ad hoc 802.11 network and be done
		// and sending them to DHT peers is probably harmless (minor overhead): they'll just try to connect and fail, unless they succeed
		// on the other hand, something that can be expected to fail 99% of the time may not be worth doing
	for(ifaddrs* ifa = ifad; ifa != nullptr; ifa = ifa->ifa_next) {
		add_addr(addrs, reinterpret_cast<sockaddrunion*>(ifa->ifa_addr), nbo_tun_addr);
	}
	freeifaddrs(ifad);
#endif
	iout out;
	out << "Detected the following local addrs: ";
	for(auto& addr : addrs)
		out << addr.get_ip_union() << " ";
	return std::move(addrs);
}
void Mouse_Hexdump_Area::dec_cursor_row()
{
  old_addr = addr_being_edited;
  cursor.toggle=1;
  
  if (res_y == 0)
  {
    add_addr(-8);
  }
  else res_y--;    
  update_editing_address();
}
void Mouse_Hexdump_Area::inc_cursor_row()
{
  old_addr = addr_being_edited;
  cursor.toggle=1;
  
  if (res_y == 15)
  {
    add_addr(+8); //address += 8;
  }
  else res_y++;    
  update_editing_address();
}
Esempio n. 8
0
static void add_rel(unsigned size) {
    uint64_t offs = 0;
    uint64_t sign = (uint64_t)1 << (size * 8 - 1);
    uint64_t mask = sign - 1;
    unsigned i = 0;

    while (i < size) {
        offs |= (uint64_t)get_code() << (i * 8);
        i++;
    }

    if (offs & sign) {
        offs = (offs ^ (sign | mask)) + 1;
        add_str("-0x");
        add_hex_uint64(offs);
        add_addr(instr_addr + code_pos - offs);
    }
    else {
        add_str("+0x");
        add_hex_uint64(offs);
        add_addr(instr_addr + code_pos + offs);
    }
}
Esempio n. 9
0
static int dusb_write(int dir, uint8_t data)
{
	static uint8_t array[20];
  	static int i = 0;
	static unsigned long state = 1;
	static uint16_t src_addr, src_id;
	static uint16_t dst_addr, dst_id;
	static uint8_t data_size, ack, sq;
	static int cnt;
	static uint8_t ascii[HEXDUMP_SIZE+1];

  	if (logfile == NULL)
    		return -1;

	array[i++ % 16] = data;

	switch(state)	// Finite State Machine
	{
	case 1:			// unused
	case 2:
		break;

	case 3:			// source address
		break;
	case 4: 
		src_addr = (((uint16_t)(array[2])) << 8) | ((uint16_t)(array[3]));
		fprintf(logfile, "%04x:", src_addr);
		add_addr(addr_found, src_addr, &af);
		break;

	case 5:			// source service id
		break;
	case 6: 
		src_id = (((uint16_t)(array[4])) << 8) | ((uint16_t)(array[5]));
		fprintf(logfile, "%04x->", src_id);
		add_sid(sid_found, src_id, &sif);
		break;

	case 7:			// destination address
		break;
	case 8: 
		dst_addr = (((uint16_t)(array[6])) << 8) | ((uint16_t)(array[7]));
		fprintf(logfile, "%04x:", dst_addr);
		add_addr(addr_found, dst_addr, &af);
		break;

	case 9:			// destination service id
		break;
	case 10: 
		dst_id = (((uint16_t)(array[8])) << 8) | ((uint16_t)(array[9]));
		fprintf(logfile, "%04x ", dst_id);
		add_sid(sid_found, src_id, &sif);
		break;
		
	case 11: break;	// data checksum
	case 12: break;
		
	case 13:		// data size
		data_size = array[12];
		break;

	case 14:		// acknowledgment
		ack = array[13];
		fprintf(logfile, "AK=%02x ", ack);
		break;

	case 15:		// sequence number
		sq = array[14];
		fprintf(logfile, "SQ=%02x ", sq);
		break;

	case 16:		// header checksum
		fprintf(logfile, "(%3i bytes) ", data_size);
		cnt = 0;

		fprintf(logfile, "\t\t\t\t\t%s (%s) ==> %s (%s)\n", 
			name_of_addr(src_addr), name_of_sid(src_id), 
			name_of_addr(dst_addr), name_of_sid(dst_id));

		if(data_size == 0)
			state = 0;
		break;

	default:
		if(!cnt)
			fprintf(logfile, "\t\t");

		fprintf(logfile, "%02X ", data);
		ascii[cnt % HEXDUMP_SIZE] = data;

		if(!(++cnt % HEXDUMP_SIZE))
		{
			fprintf(logfile, " | ");
			for(i = 0; i < HEXDUMP_SIZE; i++)
				fprintf(logfile, "%c", isalnum(ascii[i]) ? ascii[i] : '.');

			fprintf(logfile, "\n\t\t");
		}
		
		if(--data_size == 0)
		{
			for(i = 0; i < HEXDUMP_SIZE - (cnt%HEXDUMP_SIZE); i++)
				fprintf(logfile, "   ");
			fprintf(logfile, " | ");
			for(i = 0; i < (cnt%HEXDUMP_SIZE); i++)
				fprintf(logfile, "%c", isalnum(ascii[i]) ? ascii[i] : '.');

			fprintf(logfile, "\n");
			state = 0;
		}
		break;
	}

	if(state == 0)
	{
		fprintf(logfile, "\n");
		i = 0;
	}
	state++;

	return 0;
}
Esempio n. 10
0
LDAP *
cldap_open( char *host, int port )
{
    int 		s;
    in_addr_t		address;
    struct sockaddr_in 	sock;
    struct hostent	*hp;
    LDAP		*ld;
    char		*p;
    int		i;
#ifdef SUN
    struct hostent      hpret;
    char                hpbuf[NSS_BUFLEN_HOSTS];
    int                 hperrno;
#endif
    in_addr_t inet_addr(const char *);
    int close(int);

    Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 113, "ldap_open\n"), 0, 0, 0 );

    if ( port == 0 ) {
	    port = LDAP_PORT;
    }

    if ( (s = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 ) {
	return( NULL );
    }

    sock.sin_addr.s_addr = 0;
    sock.sin_family = AF_INET;
    sock.sin_port = 0;
    if ( bind(s, (struct sockaddr *) &sock, sizeof(sock)) < 0)  {
	close( s );
	return( NULL );
    }

    if (( ld = ldap_init( host, port )) == NULL ) {
	close( s );
	return( NULL );
    }
    if ( (ld->ld_sb.sb_fromaddr = (void *) calloc( 1,
	    sizeof( struct sockaddr ))) == NULL ) {
	free( ld );
	close( s );
	return( NULL );
    }	
    ld->ld_sb.sb_sd = s;
    ld->ld_sb.sb_naddr = 0;
    ld->ld_version = LDAP_VERSION;

    sock.sin_family = AF_INET;
    sock.sin_port = htons( port );

    /*
     * 'host' may be a space-separated list.
     */
    if ( host != NULL ) {
	for ( ; host != NULL; host = p ) {
	    if (( p = strchr( host, ' ' )) != NULL ) {
		for (*p++ = '\0'; *p == ' '; p++) {
		    ;
		}
	    }

	    if ( (address = inet_addr( host )) == -1 ) {
#ifdef SUN
		if ( (hp = gethostbyname_r( host, &hpret, hpbuf, NSS_BUFLEN_HOSTS, &hperrno)) == NULL ) {
		    errno = EHOSTUNREACH;
		    continue;
		}
#else
		if ( (hp = gethostbyname( host )) == NULL ) {
		    errno = EHOSTUNREACH;
		    continue;
		}
#endif

		for ( i = 0; hp->h_addr_list[ i ] != 0; ++i ) {
		    SAFEMEMCPY( (char *)&sock.sin_addr.s_addr,
			    (char *)hp->h_addr_list[ i ],
			    sizeof(sock.sin_addr.s_addr));
		    if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
			close( s );
			free( ld );
			return( NULL );
		    }
		}

	    } else {
		sock.sin_addr.s_addr = address;
		if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
		    close( s );
		    free( ld );
		    return( NULL );
		}
	    }

	    if ( ld->ld_host == NULL ) {
		    ld->ld_host = strdup( host );
	    }
	}

    } else {
	address = INADDR_LOOPBACK;
	sock.sin_addr.s_addr = htonl( address );
	if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
	    close( s );
	    free( ld );
	    return( NULL );
	}
    }

    if ( ld->ld_sb.sb_addrs == NULL
#ifdef LDAP_REFERRALS
	    || ( ld->ld_defconn = new_connection( ld, NULL, 1,0,0 )) == NULL
#endif /* LDAP_REFERRALS */
	    ) {
	free( ld );
	return( NULL );
    }

    ld->ld_sb.sb_useaddr = ld->ld_sb.sb_addrs[ 0 ];
    cldap_setretryinfo( ld, 0, 0 );

#ifdef LDAP_DEBUG
    putchar( '\n' );
    for ( i = 0; i < ld->ld_sb.sb_naddr; ++i ) {
	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 114, "end of cldap_open address %1$d is %2$s\n"),
		i, inet_ntoa( ((struct sockaddr_in *)
		ld->ld_sb.sb_addrs[ i ])->sin_addr ), 0 );
    }
#endif

    return( ld );
}
Esempio n. 11
0
int main(int argc, char **argv) {
  setvbuf(stdout, NULL, _IONBF, 0);

  url = NULL;
  keyfile = NULL;
  certfile = NULL;

  int opt;
  while ((opt = getopt(argc, argv, "p:c:u:s:")) != -1) {
    switch (opt) {
      case 'p':
        keyfile = strdup(optarg);
        break;
      case 'c':
        certfile = strdup(optarg);
        break;
      case 'u':
        url = strdup(optarg);
        break;
      case 's':
        fprintf(stderr, "ERROR: -s not yet implemented!\n");
        exit(1);
        break;
      default:
        fprintf(stderr, "ERROR: unknown option \"-%c\"!\n", opt);
        exit(1);
    }
  }

  if (url == NULL) {
    fprintf(stderr, "ERROR: No URL (-u) given!\n");
    exit(1);
  }

  if (access(keyfile, R_OK) == -1) {
    fprintf(stderr, "ERROR: Can not open private key for reading: %s\n", strerror(errno));
    exit(1);
  }

  if (access(certfile, R_OK) == -1) {
    fprintf(stderr, "ERROR: Can not open certificate for reading: %s\n", strerror(errno));
    exit(1);
  }

  // parse options
  // -p
  // -c
  // -u
  // [-s]


  curl_global_init(CURL_GLOBAL_DEFAULT);

  struct {
    struct nlmsghdr n;
    struct ifaddrmsg r;
  } req;

  struct rtattr *rta;
  struct sockaddr_in6 *sin6p;
  struct sockaddr_in *sinp;
  int status;
  char buf[16384];
  struct nlmsghdr *nlmp;
  struct ifaddrmsg *rtmp;
  struct rtattr *rtatp;
  int rtattrlen;
  struct in_addr *inp;
  struct in6_addr *in6p;
  struct ifa_cacheinfo *cache_info;

  int fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);

  struct sockaddr_nl addr;

  memset (&addr,0,sizeof(addr));
  addr.nl_family = AF_NETLINK;
  addr.nl_groups = RTMGRP_IPV6_IFADDR;

  if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
    perror ("bind failure\n");
    return 1;
  }

  memset(&req, 0, sizeof(req));
  req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
  req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
  req.n.nlmsg_type = RTM_GETADDR;
  req.r.ifa_family = AF_INET6;

  rta = (struct rtattr *)(((char *)&req) + NLMSG_ALIGN(req.n.nlmsg_len));
  rta->rta_len = RTA_LENGTH(16);

  status = send(fd, &req, req.n.nlmsg_len, 0);

  if (status < 0) {
    perror("send");
    return 1;
  }

  struct timeval tv = {
    .tv_sec = 60
  };

  setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));

  while (1) {
    status = recv(fd, buf, sizeof(buf), 0);

    if (status < 0) {
      if (errno == EAGAIN) {
        if (!sent)
          send_request();

        continue;
      }
      perror("recv");
      return 1;
    }

    if(status == 0){
      printf("EOF\n");
      return 1;
    }

    /* Typically the message is stored in buf, so we need to parse the message to *
     * get the required data for our display. */

    for(nlmp = (struct nlmsghdr *)buf; status > sizeof(*nlmp);){
      int len = nlmp->nlmsg_len;
      int req_len = len - sizeof(*nlmp);

      if (req_len<0 || len>status) {
        printf("error\n");
        return -1;
      }

      if (!NLMSG_OK(nlmp, status)) {
        printf("NLMSG not OK\n");
        return 1;
      }

      rtmp = (struct ifaddrmsg *)NLMSG_DATA(nlmp);
      rtatp = (struct rtattr *)IFA_RTA(rtmp);

      struct address addr = {};

      addr.ifa_flags = rtmp->ifa_flags;

      rtattrlen = IFA_PAYLOAD(nlmp);

      clock_gettime(CLOCK_MONOTONIC, &addr.ifa_prefered);

      for (; RTA_OK(rtatp, rtattrlen); rtatp = RTA_NEXT(rtatp, rtattrlen)) {
        if(rtatp->rta_type == IFA_CACHEINFO){
          cache_info = (struct ifa_cacheinfo *)RTA_DATA(rtatp);
          addr.ifa_prefered.tv_sec += cache_info->ifa_prefered == 0xFFFFFFFFU ? 100 * 86400 : cache_info->ifa_prefered;
        }

        if(rtatp->rta_type == IFA_ADDRESS){
          in6p = (struct in6_addr *)RTA_DATA(rtatp);
          addr.ip = *in6p;

        }
      }

      if ((ntohs((addr.ip).s6_addr16[0]) & 0xE000) == 0x2000) {
        if (nlmp->nlmsg_type == RTM_DELADDR) {
          del_addr(&addr);
        } else if (nlmp->nlmsg_type == RTM_NEWADDR) {
          if (addr.ifa_flags & (IFA_F_TEMPORARY | IFA_F_TENTATIVE | IFA_F_DADFAILED)) {
            del_addr(&addr);
          } else {
            add_addr(&addr);
          }
        }
      }

      status -= NLMSG_ALIGN(len);
      nlmp = (struct nlmsghdr*)((char*)nlmp + NLMSG_ALIGN(len));
    }
  }
}