[ 永远的UNIX::UNIX技术资料的宝库 ]

首页 > 编程技术 > 源码天堂 > 正文

C源码:雷卡(RACAL)加密机与SCO-UNIX、TANDEM、HP9000三种平台的连接测试程序

来源:本文出自:http://mly363.363.net 作者: (2001-08-22 16:35:57)

/*
你试过在UNIX系统上连接通讯设备吗?UNIX系统的版本很多,在具体处理上都有不少差别。这里向你介绍我的一个程序:雷卡(RACAL)加密机与SCO-UNIX、TANDEM、HP9000三种平台的连接测试程序
   hsmtest.c --- Test RACAL HSM

   Host & port & device name:
1) Tandem: NonStop-UX
  Asyn port 2: /dev/ttya2

2) PC Server: SCO UNIX 5.0.5
  COM1 port: /dev/tty1a

3) HP 9000 Model K360: HP-UX B.10.20
  dtc port 1: /dev/ttydtc1

   Compile:
1)Tandem:
 cc hsmtest.c -ohsmtest
2)PC SCO-UNIX:
 cc hsmtest.c -ohsmtest
3)HP9000:
 cc -Aa +DAportable -D_HPUX_SOURCE hsmtest.c -ohsmtest
*/

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <termio.h>
#include <sys/ioctl.h>
#include <sys/termio.h>

typedef unsigned char  USGC;

#if __nonstopux                             /* Tandem */
char logical_port_id[] = "/dev/ttya2";
#endif
#if unix & i386                             /* PC Server, SCO-UNIX 5.0.5 */
char logical_port_id[] = "/dev/tty1a";
#endif
#if __hpux                                  /* HP9000 K360, HP-UX 10.20 */
char logical_port_id[] = "/dev/ttydtc1";
#endif

int  port_fd;
int  msg_head_no = 0, cmd_no = 1;

void disp_string(USGC *str, int n)
{
  int  i;

  for(i = 0; i < n; i++)
  {
    if(str[i] < 0x20 || str[i] >= 0x7F)
      printf("(%02X)", str[i]);
    else
      printf("%c", str[i]);
  }
}

int  open_port(char *dev_name)
{
  struct termio termo, termn;      /* New and old termio specs */

  port_fd = open(dev_name, O_RDWR);
  if(port_fd < 0) return -1;

  if(!isatty(port_fd)) return -1;

  if(ioctl(port_fd, TCGETA, &termo) < 0) return -1;
  memcpy(&termn, &termo, sizeof(struct termio));
  termn.c_cflag &= ~CBAUD;
  termn.c_cflag |= B9600;
  termn.c_cflag &= ~CSIZE;
  termn.c_cflag |= CS8;
  termn.c_cflag &= ~PARENB;
  termn.c_cflag &= ~CSTOPB;  /* use 1 stop bit  */
  termn.c_lflag &= ~ICANON;
  termn.c_iflag &= ~ISTRIP;  /* process all 8 bits */
  termn.c_oflag &= ~OPOST;   /* characters are transmitted without change */
  termn.c_cc[VMIN] = 0;      /* minimum # of chars */
  termn.c_cc[VTIME] = 1;     /* 10'ths of seconds */
  if(ioctl(port_fd, TCSETA, &termn) < 0) return -1;
  return 0;
}

int  write_port(char *buf, int n)
{
  int  rc;

  printf("Send %d bytes: ", n);
  disp_string((USGC *)buf, n);
  printf("\n");
  rc = write(port_fd, buf, n);
  if(rc != n)
  {
    printf("write() = %d\n", rc);
    return -1;
  }
  return 0;
}

int  read_port(USGC *buf)

  USGC tmpbuf[1024];
  int  n;

  memset(tmpbuf, 0x00, 1024);
  n = read(port_fd, tmpbuf, 1024);
  if(n > 0)
  {
    printf("Recv %d bytes: ", n);
    disp_string(tmpbuf, n);
    printf("\n");
    memcpy(buf, tmpbuf, n);
    return n;
  }
  printf("read() = %d\n", n);
  return -1;
}

int  close_port(void)
{
  int  rc;

  rc = close(port_fd);
  if(rc == 0) return 0;
  printf("close() = %d\n", rc);
  return -1;


int  hsm(char *req)
{
  USGC resp[1024];
  int  rc, reqmsglen;
  char msg_head[5];

  if(msg_head_no > 0)
  {
    sprintf(msg_head, "%04.4d", msg_head_no++);
    memmove(req+1, msg_head, 4);
  }
  memset(resp, 0x00, 1024);
  reqmsglen = strlen(req);

  printf("--- %d -> open port: ", cmd_no++);
  if(open_port(logical_port_id) != 0)
  {
    printf("open_port() error!\n");
    return 0;
  }
  printf("\nWrite: ");
  if((rc = write_port(req, reqmsglen)) != 0)
  {
    printf("write_port() error!\n");
    goto hsm_end;
  }
  printf("Read: ");
  read_port(resp);
  read_port(resp);
  printf("\n");
hsm_end:
  close_port();
  return 0;
}

int  main(int argc, char **argv)
{
/*  Usage: hsmtest [msg_head_no [port_name]]
*/
  if(argc > 1) msg_head_no  = atoi(argv[1]);
  if(argc > 2) strcpy(logical_port_id, argv[2]);

  hsm("\0021234BA888888F123456789012\003");
  hsm("\0024567JG12345DF98765432F012345678901234567890\003");
  hsm("\0027890NG1234567890123456789\003");
  return 0;
}

(http://www.fanqiang.com)



 
 相关文章

★  感谢所有的作者为我们学习技术知识提供了一条捷径  ★
www.fanqiang.com