Esempio n. 1
0
LMD LinkC_lEth(PollMgr pm, char* ifname) {
	int mtu;
	
	struct sockaddr_ll addr = {0};
	addr.sll_family = AF_INET;
	addr.sll_protocol = htobe16(LinkC_eth_proto);
	if (!LinkC_getIfInfo(ifname, &(addr.sll_ifindex), &mtu)) return NULL;

	int bpf = CapsH_createBPF(ifname);
	if (bpf == -1) return NULL;
	
	NBS nbs = NBS_ctor(bpf, bpf, SockType_BPF);

	NBS_pollAttach(nbs, pm);
	
	/* get size of BPF buffer */
	if( ioctl( bpf, BIOCGBLEN, &nbs->bpf_len ) == -1 ) {
	    perror("error getting size of bpf device\n");
 	    return NULL;	
	}

	SockAddr lAddr = SockAddr_create(&addr, sizeof(struct sockaddr_ll));
	((struct sockaddr_ll*)(SockAddr_addr(lAddr)))->sll_family = AF_PACKET;
	LMD lmd = LMD_ctor(nbs, lAddr, mtu);
	return lmd;
}
Esempio n. 2
0
LMD LinkC_lUdp(PollMgr pm) {
	struct sockaddr_in6 addr = {0};
	addr.sin6_family = AF_INET6;
	addr.sin6_port = htobe16(LinkC_udp_port);

	int sock = socket(AF_INET6, SOCK_DGRAM, 0);
	if (sock == -1) return NULL;
	int flags = fcntl(sock, F_GETFL, 0);
	fcntl(sock, F_SETFL, flags | O_NONBLOCK);
	int res = bind(sock, (struct sockaddr*)(&addr), sizeof(struct sockaddr_in6));
	if (res != 0) {
		close(sock);
		return NULL;
	}

	NBS nbs = NBS_ctor(sock, sock, SockType_Dgram);
	NBS_pollAttach(nbs, pm);

	LMD lmd = LMD_ctor(nbs, NULL, LinkC_udp_mtu);
	return lmd;
}
Esempio n. 3
0
LMD LinkC_lEth(PollMgr pm, char* ifname) {
	int mtu;

	struct sockaddr_ll addr = {0};
	addr.sll_family = AF_PACKET;
	addr.sll_protocol = htobe16(LinkC_eth_proto);
	if (!LinkC_getIfInfo(ifname, &(addr.sll_ifindex), &mtu)) return NULL;

	int sock = CapsH_createPacketSock(SOCK_DGRAM | SOCK_NONBLOCK, LinkC_eth_proto);
	if (sock == -1) return NULL;
	int res = bind(sock, (struct sockaddr*)(&addr), sizeof(struct sockaddr_ll));
	if (res != 0) {
		close(sock);
		return NULL;
	}

	NBS nbs = NBS_ctor(sock, sock, SockType_Dgram);
	NBS_pollAttach(nbs, pm);

	SockAddr lAddr = SockAddr_create(&addr, sizeof(struct sockaddr_ll));
	LMD lmd = LMD_ctor(nbs, lAddr, mtu);
	return lmd;
}
Esempio n. 4
0
enum ccn_upcall_res CcnLAC_fetchFaceidCb(struct ccn_closure* selfp, enum ccn_upcall_kind kind, struct ccn_upcall_info* info) {
	CcnLAC self = (CcnLAC)selfp->data;
	switch (kind) {
		case CCN_UPCALL_FINAL: {
			free(selfp);
			return CCN_UPCALL_RESULT_OK;
		}
		case CCN_UPCALL_INTEREST_TIMED_OUT: {
			return CCN_UPCALL_RESULT_REEXPRESS;
		}
		case CCN_UPCALL_CONTENT_UNVERIFIED:
		case CCN_UPCALL_CONTENT_KEYMISSING:
		case CCN_UPCALL_CONTENT_RAW:
		case CCN_UPCALL_CONTENT: {
			struct ccn_forwarding_entry* fe = NULL;
			const unsigned char* fe_ccnb = NULL;
			size_t fe_ccnb_size = 0;
			int res = ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, &fe_ccnb, &fe_ccnb_size);
			if (res == 0) fe = ccn_forwarding_entry_parse(fe_ccnb, fe_ccnb_size);
			if (fe != NULL) {
				self->faceid = fe->faceid;
				ccn_forwarding_entry_destroy(&fe);
			} else {
				self->error = true;
			}
			PollMgr_detach(self->pm, ccn_get_connection_fd(self->ccnh), &CcnLAC_initPollCb, self);
			void* emptyPDU = malloc(CCN_EMPTY_PDU_LENGTH);
			memcpy(emptyPDU, CCN_EMPTY_PDU, CCN_EMPTY_PDU_LENGTH);
			NBS_write(self->nbs, emptyPDU, 0, CCN_EMPTY_PDU_LENGTH, NULL);
			NBS_pollAttach(self->nbs, self->pm);
			return CCN_UPCALL_RESULT_OK;
		}
		default: {
			return CCN_UPCALL_RESULT_ERR;
		}
	}
}
Esempio n. 5
0
int main(int argc, char* argv[]) {
  CapsH_drop();
  if (argc < 3) {
    usage(argv[0]);
    return 9;
  }

  int count;
  PollMgr pm = PollMgr_ctor(50);
  CcnCC cc = CcnCC_ctor();
  CcnCC_pollAttach(cc, pm);
  CcnLAC lac = CcnLAC_ctor();
  CcnLAC_initialize(lac, CcnCC_ccndid(cc), pm);

  count = 20;
  while (--count > 0 && !CcnLAC_ready(lac)) {
    PollMgr_poll(pm);
  }
  if (CcnCC_error(cc) || CcnLAC_error(lac) || !CcnLAC_ready(lac)) return 1;

  struct ccn_charbuf* prefix = ccn_charbuf_create();
  ccn_name_from_uri(prefix, argv[1]);
  CcnH_regPrefix(CcnPrefixOp_register, CcnCC_ccnh(cc), CcnCC_ccndid(cc), CcnLAC_faceid(lac), prefix);
  ccn_charbuf_destroy(&prefix);

  Link link;
  if (argc == 3 && strcmp(argv[2], "server") == 0) {
    if (D) printf("Server\n");
    // sdp_session_t* session = register_service();
    // sleep(10);
    // sdp_close(session);
    socklen_t opt = sizeof(struct sockaddr_rc);
    int sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
    struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
    loc_addr.rc_family = AF_BLUETOOTH;
    loc_addr.rc_channel = (uint8_t) RFCOMM_CHANNEL;
    loc_addr.rc_bdaddr = *BDADDR_ANY;
    int res = bind(sock, (struct sockaddr*)&loc_addr, sizeof(struct sockaddr_rc));
    if (res != 0) {
      close(sock);
      printf("Bind error\n");
      return 2;
    }
    if (D) printf("Bind done\n");

    listen(sock, 1);
    if (D) printf("Listening...\n");

    int new_sock = accept(sock, (struct sockaddr*)&rem_addr, &opt);
    if (D) printf("Accepted socket fd: %d\n", new_sock);

    char buf[18] = { 0 };
    ba2str( &rem_addr.rc_bdaddr, buf );
    printf("Remote address: %s\n", buf);

    int sock_flags = fcntl(new_sock, F_GETFL, 0);
    fcntl(new_sock, F_SETFL, sock_flags | O_NONBLOCK);

    NBS nbs = NBS_ctor(new_sock, new_sock, SockType_Stream);
    NBS_pollAttach(nbs, pm);
    link = Link_ctorStream(nbs);
    if (!link) {
      printf("Link error\n");
      return 3;
    }

  } else if (argc == 4 && strcmp(argv[2], "client") == 0) {
    if (D) printf("Client\n");
    int sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
    struct sockaddr_rc rem_addr = { 0 };

    str2ba( argv[3], &rem_addr.rc_bdaddr );
    rem_addr.rc_family = AF_BLUETOOTH;
    // rem_addr.rc_channel = discover_sdp( argv[3] );
    rem_addr.rc_channel = 13;

    int res = connect(sock, (struct sockaddr *) &rem_addr, sizeof(struct sockaddr_rc));
    if (res != 0) {
      printf("Connect error\n");
      close(sock);
      return 4;
    }
    printf("Connected\n");

    int flags = fcntl(sock, F_GETFL, 0);
    fcntl(sock, F_SETFL, flags | O_NONBLOCK);

    NBS nbs = NBS_ctor(sock, sock, SockType_Stream);
    NBS_pollAttach(nbs, pm);
    link = Link_ctorStream(nbs);
    if (!link) {
      printf("Link error\n");
      return 3;
    }
  }

  NdnlpSvc svc = NdnlpSvc_ctor(lac, link, 0, CMPConn_SentPktsCapacity_default, CMPConn_RetryCount_default, CMPConn_RetransmitTime_default, CMPConn_AcknowledgeTime_default);

  while(true) {
    PollMgr_poll(pm);
    NdnlpSvc_run(svc);
  }

  Link_dtor(link);
  CcnCC_pollDetach(cc, pm);
  CcnCC_dtor(cc);
  PollMgr_dtor(pm);

  return 0;
}