Exemplo n.º 1
0
static int
plsetparam(Serialport *p)
{
	uint8_t buf[ParamReqSz];
	int res;
	Serial *ser;

	ser = p->s;

	PUT4(buf, p->baud);

	if(p->stop == 1)
		buf[4] = 0;
	else if(p->stop == 2)
		buf[4] = 2; 			/* see comment in getparam */
	buf[5] = p->parity;
	buf[6] = p->bits;

	dsprint(2, "serial: setparam: ");
	if(serialdebug)
		dumpbuf(buf, sizeof buf);
	res = usbcmd(ser->dev, Rh2d | Rclass | Riface, SetLineReq,
		0, 0, buf, sizeof buf);
	plmodemctl(p, p->mctl);
	plgetparam(p);		/* make sure our state corresponds */

	dsprint(2, "serial: setparam res: %d\n", res);
	return res;
}
Exemplo n.º 2
0
void read_from(enum eyefi_file __file)
{
	int tries = 0;
	int ret;
	int fd;
	char *file = eyefi_file(__file);
	int nr_fresh;

	init_card();

retry:
	fd = open(file, O_RDONLY);
	if (fd < 0)
		open_error(file, fd);
	fd_flush(fd);
	// fd_flush() does not appear to be working 100% of the
	// time.  It is not working on my Thinkpad, but works
	// fine on the same kernel on the Ideapad.  Bizarre.
	// This at least works around it by detecting when we
	// did and did not actually bring in pages from the
	// disk.
	nr_fresh = nr_fresh_pages(fd, EYEFI_BUF_SIZE);
	if (!nr_fresh) {
		tries++;
		debug_printf(2, "fd_flush(%d) was unsuccessful(%d), retrying (%d)...\n",
				fd, nr_fresh, tries);
		close(fd);
		goto retry;
	}
	ret = read(fd, eyefi_buf, EYEFI_BUF_SIZE);
	if ((eyefi_debug_level >= 3) ||
	    (eyefi_debug_level >= 2 && (__file == RSPM))) {
		printf("%s:", eyefi_file_name(__file));
		dumpbuf(eyefi_buf, 128);
	}
	if (ret < 0) {
		close(fd);
		perror("bad read, retrying...");
		goto retry;
		exit(1);
	}
	debug_printf(4, "read '%s': bytes: %d\n", file, ret);
	/*
	 * There was a time when I was carefully recording how each response
	 * looked, and I counted the zeros in each response.  I don't care
	 * any more.
	u8 c;
	int zeros = 0;
	int i;
	for (i=0; i < EYEFI_BUF_SIZE; i++) {
		c = ((char *)eyefi_buf)[i];
		if (c == '\0') {
			zeros++;
			continue;
		}
	}
	*/
	free(file);
	close(fd);
}
Exemplo n.º 3
0
NTSTATUS NewZwSetSystemInformation(
	IN SYSTEM_INFORMATION_CLASS SystemInformationClass,
	IN OUT PVOID SystemInformation,
	IN ULONG SystemInformationLength) {
  NTSTATUS status;
  PEPROCESS proc;
  HANDLE procid;
  
  proc = PsGetCurrentProcess();
  procid = PsGetCurrentProcessId();

  if (restrictEnabled() &&
      (SystemInformationClass==SystemLoadAndCallImage ||
       SystemInformationClass==SystemLoadImage)
      && (ULONG)procid != processid_smss
      //&& !isImageAllow(procid)) {
      && _strnicmp(((char *)proc)+EPROCESS_NAME_OFFSET,
		   "csrss.exe", 9)!=0) {
#ifdef DEBUG
    WCHAR buf[512];
    debugOutput(L"NewZwSetSystemInformation: ");
    swprintf(buf, L"proc 0x%lx ", procid);
    debugOutput(buf);
    dumpbuf(((char *)proc)+EPROCESS_NAME_OFFSET, 15);
    if (SystemInformationClass==SystemLoadAndCallImage)
      debugOutput(L": LoadAndCallImage\n");
    else
      debugOutput(L": LoadImage\n");
#endif
    return STATUS_ACCESS_DENIED;
  }

#ifndef NOTRY
  __try {
#endif
    status = (OldZwSetSystemInformation)(SystemInformationClass,
					 SystemInformation,
					 SystemInformationLength);
    return status;
#ifndef NOTRY
  }
  __except(EXCEPTION_EXECUTE_HANDLER) {
    debugOutput(L"ZwSetSystemInformation: EXCEPTION\n");
  }
  return(STATUS_UNSUCCESSFUL);
#endif
}
Exemplo n.º 4
0
void write_to(enum eyefi_file __file, void *stuff, int len)
{
	int ret;
	int wrote;
	int fd;
	char *file;

	if (fake_write)
		return;

	init_card();
	file = eyefi_file(__file);
	if (len == -1)
		len = strlen(stuff);

	memset(eyefi_buf, 0, EYEFI_BUF_SIZE);
	memcpy(eyefi_buf, stuff, len);
	fd = open(file, O_RDWR|O_CREAT, 0600);
	if (fd < 0 )
		open_error(file, fd);
	if ((eyefi_debug_level >= 3) ||
	    (eyefi_debug_level >= 2 && (__file == REQM))) {
		printf("%s:", eyefi_file_name(__file));
		dumpbuf(eyefi_buf, 128);
	}
	wrote = write(fd, eyefi_buf, EYEFI_BUF_SIZE);
	if (wrote < 0)
		open_error(file, wrote);
	ret = fd_flush(fd);
	if (ret < 0)
		open_error(file, ret);
	close(fd);
	debug_printf(3, "wrote %d bytes to '%s' (string was %d bytes)\n", wrote, file, len);
	if (ret < 0) {
		fprintf(stderr, "error writing to '%s': ", file);
		perror("");
		exit(ret);
	}
	free(file);
}
Exemplo n.º 5
0
static int
plgetparam(Serialport *p)
{
	uint8_t buf[ParamReqSz];
	int res;
	Serial *ser;

	ser = p->s;


	res = usbcmd(ser->dev, Rd2h | Rclass | Riface, GetLineReq,
		0, 0, buf, sizeof buf);
	p->baud = GET4(buf);

	/*
	 * with the Pl9 interface it is not possible to set `1.5' as stop bits
	 * for the prologic:
	 *	0 is 1 stop bit
	 *	1 is 1.5 stop bits
	 *	2 is 2 stop bits
	 */
	if(buf[4] == 1)
		fprint(2, "warning, stop bit set to 1.5 unsupported");
	else if(buf[4] == 0)
		p->stop = 1;
	else if(buf[4] == 2)
		p->stop = 2;
	p->parity = buf[5];
	p->bits = buf[6];

	dsprint(2, "serial: getparam: ");
	if(serialdebug)
		dumpbuf(buf, sizeof buf);
	dsprint(2, "serial: getparam res: %d\n", res);
	return res;
}
Exemplo n.º 6
0
int main(int argc, char **argv)
{
	sbdclnt_t *sbdclnt;
	int port;
	int clicked;
	clock_t timeout;
	aeh_t aeh;
	int ninst;
	char comments[sendcrsh_COMMENT_MAXLEN];
	int len, lensent;
	char buf[sbd_MAXLEN];
	int pos;
	unsigned long crc;
	char signature[10];
	int i = 0;

	logprint_setFile("sendcrsh.log");

	if (!checkEnvironment()) {
		DPRINT(("anet2.dll did not set our environment!\n"));
		return 1;
	}
	if (!checkUnique()) {
		DPRINT(("Another instance of sendcrsh was running!\n"));
		return 1;
	}

	if (argc <= 3) { /* Usage: sendcrsh <host> <port> <hexdata> */
		DPRINT(("sendcrsh invoked with fewer than 3 arguments\n"));
		return 1;
	}
	DPRINT(("%s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]));

	port = atoi(argv[2]);
	if ((port < 1024) || (port > 65536)) {
		DPRINT(("sendcrsh invoked with invalid port %d\n", port));
		return 1;
	}

	if (NULL == hex2buf(argv[3], buf, 2)) {
		DPRINT(("hex2buf(datalen) failed\n"));
		return 1;
	}
	len = dpMAKESHORT(buf[0], buf[1]);
	if (NULL == hex2buf(argv[3] + 4, buf, len)) {
		DPRINT(("hex2buf(data) failed\n"));
		return 1;
	}
	if (argv[3][4 + 2*len] != '\0') {
		DPRINT(("data does not end at datalen:%d\n", len));
		return 1;
	}
	if (len > sbd_MAXLEN) {
		DPRINT(("datalen:%d is greater than max:%d\n", len, sbd_MAXLEN));
		return 1;
	}
	pos = getLastRecord(buf, len, &aeh, &ninst);
	if (pos == -1) {
		DPRINT(("getLastRecord failed\n"));
		return 1;
	}
	if (aeh.nstk == 0) {
		DPRINT(("exception has 0 length stack trace\n"));
	}
	crc = aeh_getSignature(&aeh);
	if (crc == 0) {
		DPRINT(("aeh_getSignature returns 0 crc, error?\n"));
	}
	aeh_signature_toString(crc, signature);

	DPRINT(("%s to %s:%d len:%d data:%s\n", argv[0], argv[1], port, len,
		argv[3] + 4));
	sendbox_create(winCmdShow, signature, sendcrsh_COMMENT_MAXLEN, 120);
	while (1) {
		clock_t now = eclock();

		clicked = sendbox_poll(comments);
		if (clicked != sendbox_CLICKED_NONE) {
			DPRINT(("t:%d, clicked:%d, breaking\n", now, clicked));
			break;
		}
		Sleep(100);
		if (!((++i)%10)) {
			DPRINT(("%dth call to sendbox_poll at t:%d\n", i, now));
		}
	}
	sendbox_destroy();

	if (clicked != sendbox_CLICKED_OK) {
		DPRINT(("User cancelled send, clicked:%d\n", clicked));
		return 0;
	}

	timeout = eclock() + 30 * ECLOCKS_PER_SEC;
	/* add the comments to the buffer */
	if (comments[0]) {
		aeh_buf_t aehbuf;
		int commentlen = strlen(comments);
		int nwritten;

		if ((len + sizeof(unsigned int /* aeh_info_t.id */) + sizeof(commentlen) + commentlen) > sbd_MAXLEN) {
			DPRINT(("no room in buf for comment\n"));
			return 1;
		}
		/* stuff the comment into the info list */
		if (aeh_RES_OK != aeh_addComment(&aeh, comments)) {
			DPRINT(("aeh_addComment failed\n"));
			return 1;
		}
		/* convert it back to a buf */
		if (aeh_RES_OK != aeh_writeOutputStream(&aeh, &aehbuf)) {
			DPRINT(("can't convert aeh back to aehbuf\n"));
			return 1;
		}
		DPRINT(("writing new record at pos:%d buf:\n", pos));
		dumpbuf(aehbuf.buf, aehbuf.buflen);
		nwritten = aehlog_writetobuf(&aehbuf, ninst, buf+pos, sbd_MAXLEN-pos);
		if (-1 == nwritten) {
			DPRINT(("can't convert aehbuf back to buf\n"));
			return 1;
		}
		len = pos + nwritten;
	}
	aeh_Destroy(&aeh);

	/* send the buffer */
	sbdclnt = sbdclnt_create(buf, len, argv[1], (unsigned short)port);
	if (sbdclnt == NULL) {
		DPRINT(("sbdclnt_create failed\n"));
		return 1;
	}
	while ((long)(eclock() - timeout) < 0) {
		lensent = sbdclnt_poll(sbdclnt);
		if (lensent != 0)
			break;
		Sleep(100);
	}
	sbdclnt_destroy(sbdclnt);
	if (lensent != len) {
		DPRINT(("send only %d of %d bytes!\n", lensent, len));
		return 1;
	}
	DPRINT(("send completed successfully\n"));
	return 0;
}
Exemplo n.º 7
0
int main(int argc, char *argv[])
{
    struct sockaddr_ll srcethaddr;  /* man 7 packet    */
    struct sockaddr_ll bindethaddr; /* man 7 packet    */
    struct packet_mreq mr;          /* man 7 packet    */
    struct ifreq ifr;               /* man 7 netdevice */
    int nbytes;
    socklen_t addrlen;

    /* how big should the buffer be?
     *
     * This can be determined by MTU. MTU can be obtained by an ioctl call:
     *
     *         ioctl(socket, SIOCGIFMTU, &ifr)
     *
     * where socket is a socket descriptor, ifr is of type struct ifreq.
     * More information can be found at manual page netdevice(7). Note that
     * one needs to give ifr_name to invoke the ioctl call. If a program 
     * retrieves frames from more than one device, the maximum MTU among
     * the devices should be used. 
     *
     * In Linux, available network devices can be obtained in multiple
     * methods, commonly, 
     * (1) by walking through the /proc/net/dev file. 
     * (2) by calling ioctl(...) with request SIOCGIFCONF
     * (3) via rtnetlink socket
     *
     * MTU does not include header/trailer. For capturing Ethernet frames,
     * the buffer size should be set no less than 
     * 6 + 6 + 2 + MTU = ETHER_HDR_LEN + MTU.
     *
     * Question: how does IEEE 802.1Q affect the required buffer size?
     * */

    int bufsize;      /* how big should the buffer be? */
    int ifindex;      /* interface index               */

    setupsignal(SIGINT, cleanup);    /* capture CTRL-C */

    if (argc < 2) {
        usage(argv[0]);
        exit(1);
    }

    /* open a raw packet socket to capture all types of ethernet frames */
    sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    if (sockfd < 0) {
        perror("socket(AF_PACKET, SOCK_RAW, ETH_P_ALL) failed");
        exit(1);
    }

    /* obtain interface index from interface name */
    if ((ifindex = get_if_index(sockfd, argv[1])) == -1) {
        fprintf(stderr, 
                "failed to obtain interface index for interface %s\n", 
                argv[1]);
        exit(1);
    }

    /* put the interface into promiscuous mode. man 7 packet */
    memset(&mr, 0, sizeof(mr));
    mr.mr_ifindex = ifindex;
    mr.mr_type =  PACKET_MR_PROMISC;
    if (setsockopt(sockfd, SOL_PACKET, 
            PACKET_ADD_MEMBERSHIP, (char *)&mr, sizeof(mr)) != 0) {
        perror("setsockopt(sockfd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, ...)");
        exit(1);
    }
    

    /* bind the socket to the interface. see bind(2) and packet(7) */
    memset(&bindethaddr, 0, sizeof(bindethaddr));
    bindethaddr.sll_family = AF_PACKET;
    bindethaddr.sll_protocol = htons(ETH_P_ALL);
    bindethaddr.sll_ifindex = ifindex;
    if (bind(sockfd, 
		(struct sockaddr*)&bindethaddr, sizeof(bindethaddr)) != 0) {
        perror("bind(sockfd, &bindethaddr, sizeof(bindethaddr))");
        exit(1);
    }

    /* obtain MTU. see netdevice(7) */
    ifr.ifr_addr.sa_family = AF_PACKET;
    safe_strncpy(ifr.ifr_name, argv[1], IFNAMSIZ);
    if (ioctl(sockfd, SIOCGIFMTU, &ifr) != 0) {
        perror("ioctl(sockfd, SIOCGIFMTU, &ifr)");
        exit (1);
    }

    /* allocate buffer */
    bufsize = ifr.ifr_mtu + ETHER_HDR_LEN;
    if ((buf = malloc(bufsize)) == NULL) {
        fprintf(stderr, "insufficient memory\n");
        exit(1);
    }

    /* begin capturing */
    memset(&srcethaddr, 0, sizeof(srcethaddr));
    while (1) {

        addrlen = sizeof(srcethaddr);
        nbytes = recvfrom(sockfd, buf, bufsize, 
                0, (struct sockaddr*)&srcethaddr, &addrlen);

        if (nbytes < 0) {
            perror("recv(sockfd ...) failed");
            exit (1);
        }
    
        /* dump captured frame */
        printf("Captured at interface: %s frame from ", argv[1]);
        dump_physical_address(srcethaddr.sll_halen, srcethaddr.sll_addr);
        printf("\n");
        dumpbuf(buf, nbytes);
        /**
         * flush the buffer so that we don't have to rely on stdbuf, as in
         *     sudo stdbuf -o 0 ./ethercap eth0 | tee frame_captured.txt
         */
        fflush(stdout);
    }

    /* remove the interface's promiscuous mode */
    setsockopt(sockfd, SOL_PACKET, 
            PACKET_DROP_MEMBERSHIP, (char *)&mr, sizeof(mr));

    free(buf);
    close(sockfd);

    return 0;
}
Exemplo n.º 8
0
int ipmi_cmdraw_ms(uchar cmd, uchar netfn, uchar lun, uchar sa,
		uchar bus, uchar *pdata, int sdata, uchar *presp, int *sresp, 
		uchar *pcc, char fdebugcmd)
{
   int bRet;
   HRESULT hres;
   IWbemClassObject* pInParams = NULL; /*class definition*/
   IWbemClassObject* pInReq = NULL;    /*instance*/
   IWbemClassObject* pOutResp = NULL;
   VARIANT varCmd, varNetfn, varLun, varSa, varSize, varData;
   SAFEARRAY* psa = NULL;
   long i;
   uchar *p;

   fdebugms = fdebugcmd;
   if (!fmsopen) {
      bRet = ipmi_open_ms(fdebugcmd);
      if (bRet != 0) return(bRet);
   }
   bRet = -1;


   hres = pClass->GetMethod(L"RequestResponse",0,&pInParams,NULL);
   if (FAILED(hres)) {
        if (fdebugcmd) 
	     printf("ipmi_cmdraw_ms: Cannot get RequestResponse method\n");
        return (bRet);
   }

#ifdef WDM_FIXED
   /* see http://support.microsoft.com/kb/951242 for WDM bug info */
   hres = pInParams->SpawnInstance(0,&pInReq);
   if (FAILED(hres)) {
        if (fdebugcmd) 
	     printf("ipmi_cmdraw_ms: Cannot get RequestResponse instance\n");
        return (bRet);
   }
   // also substitute pInReq for pInParams below if this gets fixed.
#endif

   VariantInit(&varCmd);
   varCmd.vt = VT_UI1;
   varCmd.bVal = cmd;
   hres = pInParams->Put(_bstr_t(L"Command"), 0, &varCmd, 0);
   // VariantClear(&varCmd);
   if (FAILED(hres)) goto MSRET;

   VariantInit(&varNetfn);
   varNetfn.vt = VT_UI1;
   varNetfn.bVal = netfn;
   hres = pInParams->Put(_bstr_t(L"NetworkFunction"), 0, &varNetfn, 0);
   // VariantClear(&varNetfn);
   if (FAILED(hres)) goto MSRET;

   VariantInit(&varLun);
   varLun.vt = VT_UI1;
   varLun.bVal = lun;
   hres = pInParams->Put(_bstr_t(L"Lun"), 0, &varLun, 0);
   // VariantClear(&varLun);
   if (FAILED(hres)) goto MSRET;

   VariantInit(&varSa);
   varSa.vt = VT_UI1;
   varSa.bVal = sa;
   hres = pInParams->Put(_bstr_t(L"ResponderAddress"), 0, &varSa, 0);
   // VariantClear(&varSa);
   if (FAILED(hres)) goto MSRET;

   VariantInit(&varSize);
   varSize.vt = VT_I4;
   varSize.lVal = sdata;
   hres = pInParams->Put(_bstr_t(L"RequestDataSize"), 0, &varSize, 0);
   // VariantClear(&varSize);
   if (FAILED(hres)) goto MSRET;

   SAFEARRAYBOUND rgsabound[1];
   rgsabound[0].cElements = sdata;
   rgsabound[0].lLbound = 0;
   psa = SafeArrayCreate(VT_UI1,1,rgsabound);
   if(!psa) {
      printf("ipmi_cmdraw_ms: SafeArrayCreate failed\n");
      goto MSRET;
   }
#ifdef SHOULD_WORK_BUT_NO
   /* The SafeArrayPutElement does not put the data in the right 
    * place, so skip this and copy the raw data below. */
   VARIANT tvar;
   if (fdebugcmd && sdata > 0) 
	{ printf("psa1(%p):",psa); dumpbuf((uchar *)psa,42,1); }   

   for(i =0; i< sdata; i++)
   {
      VariantInit(&tvar);
      tvar.vt = VT_UI1;
      tvar.bVal = pdata[i];
      hres = SafeArrayPutElement(psa, &i, &tvar);
      // VariantClear(&tvar);
      if (FAILED(hres)) { 
         printf("ipmi_cmdraw_ms: SafeArrayPutElement(%d) failed\n",i);
         goto MSRET;
      }
   } /*end for*/
   if (fdebugcmd && sdata > 0) 
	{ printf("psa2(%p):",psa); dumpbuf((uchar *)psa,42,1); }  
#endif

   /* Copy the real RequestData into psa */
   memcpy(psa->pvData,pdata,sdata);

   VariantInit(&varData);
   varData.vt = VT_ARRAY | VT_UI1;
   varData.parray = psa;
   hres = pInParams->Put(_bstr_t(L"RequestData"), 0, &varData, 0);
   // VariantClear(&varData);
   if (FAILED(hres)) {
	printf("Put(RequestData) error %x\n",hres);
        goto MSRET;
   }

#ifdef TEST_METHODS
   IWbemClassObject* pOutSms = NULL;
   if (fdebugcmd) printf("ipmi_cmdraw_ms: calling SMS_Attention(%ls)\n",
			  V_BSTR(&varPath)); 
   hres = pSvc->ExecMethod( V_BSTR(&varPath), _bstr_t(L"SMS_Attention"), 
				0, NULL, NULL, &pOutSms, NULL);
   if (FAILED(hres)) {
	printf("ipmi_cmdraw_ms: SMS_Attention method error %x\n",hres);
        goto MSRET;
   }
   if (fdebugcmd) printf("ipmi_cmdraw_ms: SMS_Attention method ok\n"); 
   /* This does work, without input parameters */
   pOutSms->Release();
#endif

   hres = pSvc->ExecMethod( V_BSTR(&varPath), _bstr_t(L"RequestResponse"), 
				0, NULL, pInParams, &pOutResp, NULL);
   if (fdebugcmd) {
       printf("ipmi_cmdraw_ms(cmd=%x,netfn=%x,lun=%x,sa=%x,sdata=%d)"
	      " RequestResponse ret=%x\n", cmd,netfn,lun,sa,sdata,hres); 
       if (sdata > 0) {
	   printf("ipmi_cmdraw_ms: req data(%d):",sdata); 
	   dumpbuf(pdata,sdata,0); 
       }
   }
   if (FAILED(hres)) {
	printf("ipmi_cmdraw_ms: RequestResponse error %x %s\n",
		hres,res_str(hres));
#ifdef EXTRA_DESC
	/* This does not usually add any meaning for IPMI. */
	BSTR desc;
	IErrorInfo *pIErrorInfo;
	GetErrorInfo(0,&pIErrorInfo);
	pIErrorInfo->GetDescription(&desc);
	printf("ipmi_cmdraw_ms: ErrorInfoDescr: %ls\n",desc);
	SysFreeString(desc);
#endif
	bRet = -1; 
	/*fall through for cleanup and return*/
   }
   else {  /*successful, get ccode and response data */
	VARIANT varByte, varRSz, varRData;
        VariantInit(&varByte);
        VariantInit(&varRSz);
        VariantInit(&varRData);
	long rlen;

	hres = pOutResp->Get(_bstr_t(L"CompletionCode"),0, &varByte, NULL, 0);
	if (FAILED(hres)) goto MSRET;
	if (fdebugcmd) printf("ipmi_cmdraw_ms: CompletionCode %x returned\n",
				V_UI1(&varByte) );
	*pcc = V_UI1(&varByte);

	hres = pOutResp->Get(_bstr_t(L"ResponseDataSize"),0, &varRSz, NULL, 0);
	if (FAILED(hres)) goto MSRET;
        rlen = V_I4(&varRSz);
	if (rlen > 1) rlen--;   /*skip cc*/
	if (rlen > *sresp) {
	   if (fdebugcmd) printf("ResponseData truncated from %d to %d\n",
					rlen,*sresp);
	   rlen = *sresp; /*truncate*/
	}
	*sresp = (int)rlen;

	hres = pOutResp->Get(_bstr_t(L"ResponseData"),0, &varRData, NULL,0);
	if (FAILED(hres)) { /*ignore failure */ 
	   if (fdebugcmd) printf("Get ResponseData error %x\n",hres); 
	} else {  /* success */
#ifdef SHOULD_WORK_BUT_NO
	    uchar *pa;
	    p = (uchar*)varRData.parray->pvData;
	    pa = (uchar*)varRData.parray;
	    printf("pa=%p, pa+12=%p p=%p\n",pa,(pa+12),p);
	    if (fdebugcmd) {   
		 printf("Data.vt = %04x, Data.parray(%p):",
			varRData.vt, varRData.parray); 
	         // 0x2011 means VT_ARRAY | VT_UI1
		 dumpbuf((uchar *)varRData.parray,40,1);
	    }
	    /* The SafeArrayGetElement does not get the data from the right 
	     * place, so skip this and copy the raw data below. */
	    VARIANT rgvar[NVAR];
	    if (rlen > NVAR) *pcc = 0xEE; 
	    for (i = 0; i <= rlen; i++)
         	VariantInit(&rgvar[i]);
	    /* copy the response data from varRData to presp */
	    for( i = 0; i <= rlen; i++)
	    {
		hres = SafeArrayGetElement(varRData.parray, &i, &rgvar[i]);
		if (FAILED(hres)) { 
		   if (fdebugcmd)
		      printf("ipmi_cmdraw_ms: SafeArrayGetElement(%d) failed\n",i);
		   break;
		}
		if (fdebugcmd) {   
		     printf("Data[%d] vt=%02x val=%02x, rgvar(%p):",i,
				rgvar[i].vt, V_UI1(&rgvar[i]),&rgvar[i]);
		     dumpbuf((uchar *)&rgvar[i],12,0);
		}
	        /* skip the completion code */
	    	// if (i > 0) presp[i-1] = V_UI1(&rgvar[i]);
	    } /*end for*/
#endif
	    /* 
	     * parray from a GetDeviceId response:
	     * 0015CEE0: 01 00 80 00 01 00 00 00 00 00 00 00 00 cf 15 00
	     * 0015CEF0: 10 00 00 00 00 00 00 00 03 00 06 00 95 01 08 00
             *           ^- datalen=0x10
	     * 0015CF00: 00 20 01 00 19 02 9f 57 01 ...  
             *           ^- start of data (cc=00, ...)
	     */
	    /* Copy the real ResponseData into presp. */
	    p = (uchar*)varRData.parray->pvData;
	    for( i = 0; i <= rlen; i++) {
	        /* skip the completion code */
	    	if (i > 0) presp[i-1] = p[i];
	    }
	    if (fdebugcmd) {
		printf("ipmi_cmdraw_ms: resp data(%d):",rlen+1); 
		dumpbuf(p,rlen+1,0); 
	    }
	}
	bRet = 0;
   }

MSRET:
#define CLEAN_OK  1
#ifdef CLEAN_OK
   /* VariantClear(&var*) should be done by pInParams->Release() */
   if (psa != NULL) SafeArrayDestroy(psa);
   if (pInParams != NULL) pInParams->Release();
   if (pOutResp != NULL) pOutResp->Release();
#endif
   return(bRet);
}
Exemplo n.º 9
0
void
dolookup(int sockfd, uint64_t hash, uint32_t seqno,
	struct sockaddr_in *from_addr)
{
	tracker_lookup_resp_t	*resp;
	tracker_info_t		*respinfo;
	hash_info_t		*hashinfo;
	hash_info_t		*predhashinfo;
	size_t			len;
	int			j;
	int			flags;
	int			index, next_index;
	char			found;
	char			buf[64*1024];

	resp = (tracker_lookup_resp_t *)buf;
	resp->header.op = LOOKUP;
	resp->header.seqno = seqno;

	/*
	 * keep a running count for the length of the data
	 */
	len = sizeof(tracker_lookup_resp_t);

	/*
	 * look up info for this hash
	 */
	respinfo = (tracker_info_t *)resp->info;
	respinfo->hash = hash;

	len += sizeof(tracker_info_t);

	/*
	 * always send back at least one hash, even if it is empty (i.e.,
	 * it has no peers.
	 */
	resp->numhashes = 1;

	found = 0;

	if ((hashinfo = getpeers(hash, &index)) != NULL) {
		/*
		 * shuffle the peers
		 */
		prep_peers(hashinfo, respinfo, from_addr, &found);

		if (respinfo->numpeers > 0) {
			avalanche_log(respinfo->peers[0].ip, from_addr->sin_addr.s_addr, hash);
		}

#ifdef	DEBUG
		fprintf(stderr,
			"resp info numpeers (%d)\n", respinfo->numpeers);
#endif

		len += (sizeof(respinfo->peers[0]) * respinfo->numpeers);

		respinfo = (tracker_info_t *)
			(&(respinfo->peers[respinfo->numpeers]));

		/*
		 * now get hash info for the "predicted" next file downloads
		 */
		next_index = index;
		for (j = 0 ; j < PREDICTIONS ; ++j) {
			if ((predhashinfo = getnextpeers(hash, &next_index))
					!= NULL) {

				if (index == next_index) {
					/*
					 * there are less than 'PREDICTIONS'
					 * number of valid hash entries and
					 * we've examined them all. break out
					 * of this loop.
					 */
					break;
				}

				prep_peers(predhashinfo, respinfo, from_addr,
					NULL);

				if (respinfo->numpeers > 0) {
					avalanche_log(respinfo->peers[0].ip,
						      from_addr->sin_addr.s_addr,
						      predhashinfo->hash);
				}
			} else {
				/*
				 * no more valid hashes
				 */
				break;
			}

			len += sizeof(tracker_info_t) +
				(sizeof(respinfo->peers[0]) *
					respinfo->numpeers);

			respinfo = (tracker_info_t *)
				(&(respinfo->peers[respinfo->numpeers]));

			++resp->numhashes;
		}

#ifdef	DEBUG
	fprintf(stderr, "len (%d)\n", (int)len);
#endif

	} else {
		/*
		 * this hash is not in the table, so let's add it.
		 */
		if ((hashinfo = newentry()) == NULL) {
			fprintf(stderr, "dolookup:newentry:failed\n");
			abort();
		}

		hashinfo->hash = hash;
		hashinfo->numpeers = 0;

		respinfo->numpeers = 0;

		avalanche_log(ntohl(INADDR_LOOPBACK), from_addr->sin_addr.s_addr, hash);
	}

	if (!found) {
		peer_t	newpeer;

		/*
		 * the client has made a 'lookup' request which means the client
		 * will soon be downloading the file. let's add this peer to
		 * the hash and mark this peer as 'downloading'.
		 */
		newpeer.ip = from_addr->sin_addr.s_addr;
		newpeer.state = DOWNLOADING;
#ifdef	DEBUG
		fprintf(stderr, "dolookup:calling addpeer\n");
#endif

		addpeer(hashinfo, &newpeer);
	}

#ifdef	DEBUG
	fprintf(stderr, "dolookup:numhashes (%d)\n", resp->numhashes);
#endif

	resp->header.length = len;

#ifdef	DEBUG
	fprintf(stderr, "send buf: ");
	dumpbuf((char *)resp, len);
#endif

	flags = 0;
	sendto(sockfd, buf, len, flags, (struct sockaddr *)from_addr,
		sizeof(*from_addr));

#ifdef	DEBUG
	fprintf(stderr, "dolookup:exit:hash (0x%llx)\n", hash);
#endif

	return;
}
Exemplo n.º 10
0
int
main(int argc, char **argv)
{
	int newl, skip;
	struct msgbuf *bufp, cur;
	char *bp, *memf, *nlistf;
	kvm_t *kd;
	int ch;
	int clear = 0;
	int pri = 0;
	int tailmode = 0;
	unsigned int rindex;
	size_t buflen, bufpos;

	setlocale(LC_CTYPE, "");
	memf = nlistf = NULL;
	while ((ch = getopt(argc, argv, "acfM:N:")) != -1) {
		switch(ch) {
		case 'a':
			all_opt++;
			break;
		case 'c':
			clear = 1;
			break;
		case 'f':
			tailmode = 1;
			break;
		case 'M':
			memf = optarg;
			break;
		case 'N':
			nlistf = optarg;
			break;
		case '?':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	newl = 0;
	skip = 0;
	pri = 0;

	if (memf == NULL && nlistf == NULL && tailmode == 0) {
		/* Running kernel. Use sysctl. */
		if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
			err(1, "sysctl kern.msgbuf");
		buflen += 4096;		/* add some slop */
		if ((bp = malloc(buflen)) == NULL)
			errx(1, "malloc failed");
		if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
			err(1, "sysctl kern.msgbuf");
		/* We get a dewrapped buffer using sysctl. */
		bufpos = 0;
		dumpbuf(bp, bufpos, buflen, &newl, &skip, &pri);
	} else {
		/* Read in kernel message buffer, do sanity checks. */
		kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
		if (kd == NULL)
			exit (1);
		if (kvm_nlist(kd, nl) == -1)
			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
		if (nl[X_MSGBUF].n_type == 0)
			errx(1, "%s: msgbufp not found",
			    nlistf ? nlistf : "namelist");
		bp = malloc(INCRBUFSIZE);
		if (!bp)
			errx(1, "malloc failed");
		if (KREAD(nl[X_MSGBUF].n_value, bufp))
			errx(1, "kvm_read: %s", kvm_geterr(kd));
		if (KREAD((long)bufp, cur))
			errx(1, "kvm_read: %s", kvm_geterr(kd));
		if (cur.msg_magic != MSG_MAGIC)
			errx(1, "kernel message buffer has different magic "
			    "number");

		/*
		 * Start point.  Use rindex == bufx as our end-of-buffer
		 * indication but for the initial rindex from the kernel
		 * this means the buffer has cycled and is 100% full.
		 */
		rindex = cur.msg_bufr;
		if (rindex == cur.msg_bufx) {
			if (++rindex >= cur.msg_size)
				rindex = 0;
		}

		for (;;) {
			if (cur.msg_bufx >= rindex)
				buflen = cur.msg_bufx - rindex;
			else
				buflen = cur.msg_size - rindex;
			if (buflen > INCRBUFSIZE)
				buflen = INCRBUFSIZE;
			if (kvm_read(kd, (long)cur.msg_ptr + rindex,
				     bp, buflen) != (ssize_t)buflen) {
				errx(1, "kvm_read: %s", kvm_geterr(kd));
			}
			if (buflen)
				dumpbuf(bp, 0, buflen, &newl, &skip, &pri);
			rindex += buflen;
			if (rindex >= cur.msg_size)
				rindex = 0;
			if (rindex == cur.msg_bufx) {
				if (tailmode == 0)
					break;
				sleep(1);
			}
			if (KREAD((long)bufp, cur))
				errx(1, "kvm_read: %s", kvm_geterr(kd));
		}
		kvm_close(kd);
	}
	if (!newl)
		putchar('\n');
	if (clear) {
		if (sysctlbyname("kern.msgbuf_clear", NULL, NULL,
				 &clear, sizeof(int)) != 0) {
			err(1, "sysctl kern.msgbuf_clear");
		}
	}
	return(0);
}
Exemplo n.º 11
0
void testit0(void)
{
	char c;
	struct testbuf tb;
	int i;
	int fdin;
	int fdout;

	//start_direct();
	print_direct_status();
	//enable_direct_mode(60, 120);
	enable_direct_mode(DIRECT_WAIT_FOREVER, DIRECT_WAIT_FOREVER);
	print_direct_status();
	start_direct();
	exit(0);
	//char new_cmd[] = {'O', 0x06, 0x0d, 0x0a, 0x31, 0x30, 0x2e, 0x36, 0x2e, 0x30, 0x2e, 0x31, 0x33, 0x37};

	//printf("waiting...\n");
	//print_transfer_status();
	//exit(0);
	//int doagain = 1;
	//wlan_disable(0);
	//int to_test[] = {5, 8, 9, 11, 15, 16, 255, -1};
	int to_test[] = {0xFF, -1};

	zero_card_files();
	for (i = 0; i < 100; i++) {
		print_transfer_status();
	}
	exit(0);
	while (1) {
	//fprintf(stderr, "testing...\n");
	for (i = 0; i < 255; i++) {
		int cmd = to_test[i];
		if (cmd == -1)
			break;
		//zero_card_files();
		card_info_cmd(cmd);
		printf("UNKNOWN %3d result: ", cmd);
		int printed = dumpbuf(eyefi_buf, 256);
		if (!printed)
			printf("\n");
		print_transfer_status();
		print_connected_to();
	}
	}
	exit(0);
	scan_print_nets();
	printf("WLAN enabled: %d\n", wlan_enabled());
	//wlan_disable();
	printf("WLAN enabled: %d\n", wlan_enabled());
	for (i = 10; i <= 13; i++) {
		int printed;
		zero_card_files();
		card_info_cmd(i);
		printf("UNKNOWN %d result:\n", i);
		printed = dumpbuf(eyefi_buf, 64);
		printf("WLAN enabled: %d\n", wlan_enabled());
	}
	i = 0xff;
	card_info_cmd(i);
	printf("UNKNOWN %d result:", i);
	dumpbuf(eyefi_buf, 64);
	exit(3);

	card_info_cmd(3);
	printf("o3 result:\n");
	dumpbuf(eyefi_buf, 64);

	memset(&zbuf[0], 0, EYEFI_BUF_SIZE);
	zbuf[0] = 'o';
	zbuf[1] = 2;

	write_to(REQM, &zbuf[0], 16384);
	printf("o2 written\n");
	printf("seq: %x\n", (int)eyefi_seq.seq);
	inc_seq();

	for (i=0; i < 4; i++) {
		read_from(RSPC);
		printf("RSPC %d:\n", i);
		dumpbuf(eyefi_buf, 64);
		usleep(20000);
	}

	printf("RSPM1:\n");
	read_from(RSPM);
	dumpbuf(eyefi_buf, 64);

	memset(&zbuf[0], 0, EYEFI_BUF_SIZE);
	write_to(RSPM, zbuf, EYEFI_BUF_SIZE);
	write_to(REQM, zbuf, EYEFI_BUF_SIZE);

	fdin = open("/home/dave/projects/eyefi/EYEFIFWU.BIN.2.0001", O_RDONLY);
	perror("fdin");
	fdout = open("/media/EYE-FI/EYEFIFWU.BIN", O_WRONLY|O_CREAT);
	perror("fdout");
	if (fdin <= 0 || fdout <= 0)
		exit(1);
	fd_flush(fdin);
	i = read(fdin, &fwbuf[0], 524288);
	perror("read");
	if (i != 524288)
		exit(2);
	i = write(fdout, &fwbuf[0], 524288);
	fd_flush(fdout);
	perror("write");
	if (i != 524288)
		exit(3);

	printf("RSPM2:\n");
	read_from(RSPM);
	dumpbuf(eyefi_buf, 64);

	reboot_card();
	printf("after reboot:\n");
	dumpbuf(eyefi_buf, 64);

	printf("cic3:\n");
	card_info_cmd(3);
	dumpbuf(eyefi_buf, 64);

	printf("cic2:\n");
	card_info_cmd(2);
	dumpbuf(eyefi_buf, 64);

	memset(&zbuf[0], 0, EYEFI_BUF_SIZE);
	write_to(RSPM, zbuf, EYEFI_BUF_SIZE);
	write_to(REQM, zbuf, EYEFI_BUF_SIZE);

	printf("cic2v2:\n");
	card_info_cmd(2);
	dumpbuf(eyefi_buf, 64);

	exit(0);
	strcpy(tb.name, "www.sr71.net/");
	tb.l1 = strlen(tb.name);
	for (i = 0; i < 10; i++) {
		tb.cmd = 'O';
		tb.l1 = i;
		write_struct(RSPM, &z);
		write_struct(REQM, &tb);
		wait_for_response();
		printf("buffer after O %d:\n", i);
		dumpbuf(eyefi_buf, 64);
		printf("----------------\n");
		write_struct(REQM, &tb);
		card_info_cmd(i);
		printf("card info(%d):\n", i);
		dumpbuf(eyefi_buf, 64);
		printf("-----------\n");
	}
	return;

	strcpy(tb.name, "/public/eyefi/servname");
	strcpy(tb.name, "/config/networks.xml");
	//tb.len = strlen(tb.name);
	tb.l1 = 0;
	for (c = 'O'; c <= 'O'; c++) {
		tb.cmd = c;
		write_struct(REQM, &tb);
		wait_for_response();
		printf("dumping buffer:\n");
		dumpbuf(eyefi_buf, 64);
		printf("buffer dump done\n");
	}
}