Пример #1
0
/*
  socket = MG_NET_CREATEPORT(portnum [, /TCP] [, /UDP])

  Creates a socket listening on the specified port for a new connection. Set
  the TCP keyword to create a TCP/IP port, or set the UDP keyword to create a
  UDP/IP port. By default a TCP port is created.

  For TCP sockets, MG_NET_SELECT returns true for this socket if there is an
  attempt to connect to it (which should be serviced by MG_NET_ACCEPT).

  For UDP sockets, you can both send and receive from this socket.
*/
static IDL_VPTR IDL_CDECL mg_net_createport(int argc, IDL_VPTR argv[], char *argk) {
  SOCKET s;
  struct sockaddr_in sin;
  short	port;
  int err;
  IDL_LONG i;
  
  static IDL_LONG	iUDP,iTCP;
  static IDL_KW_PAR kw_pars[] = { IDL_KW_FAST_SCAN,
				  { "TCP", IDL_TYP_LONG, 1, IDL_KW_ZERO, 0, IDL_CHARA(iTCP) },
				  { "UDP", IDL_TYP_LONG, 1, IDL_KW_ZERO, 0, IDL_CHARA(iUDP) },
				  { NULL }
  };

  IDL_KWCleanup(IDL_KW_MARK);
  IDL_KWGetParams(argc, argv, argk, kw_pars, argv, 1);
  port = (short) IDL_LongScalar(argv[0]);
  IDL_KWCleanup(IDL_KW_CLEAN);

  for(i = 0; i < MAX_SOCKETS; i++) {
    if (net_list[i].iState == NET_UNUSED) break;
  }
  if (i == MAX_SOCKETS) return (IDL_GettmpLong(-2));

  if (iUDP) {
    s = socket(AF_INET, SOCK_DGRAM, 0);
    net_list[i].iType = NET_UDP;
  } else {
    s = socket(AF_INET, SOCK_STREAM, 0);
    net_list[i].iType = NET_TCP;
  }
  if (s == -1) return (IDL_GettmpLong(-1));

  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = htonl(INADDR_ANY);
  sin.sin_port = htons(port);
  err = bind(s,(struct sockaddr *) &sin, sizeof(sin));
  if (err == -1) {
    CLOSE(s);
    return(IDL_GettmpLong(-1));
  }
  if (!iUDP) {
    err = listen(s, 5);
    if (err == -1) {
      CLOSE(s);
      return (IDL_GettmpLong(-1));
    }
    net_list[i].iState = NET_LISTEN;
  } else {
    net_list[i].iState = NET_IO;
  }

  net_list[i].socket = s;

  return(IDL_GettmpLong(i));
}
Пример #2
0
static IDL_VPTR IDLOldCnvMapOpen(int argc,IDL_VPTR *argv,char *argk) {
  
  IDL_VARIABLE unit;
  IDL_VPTR fargv[2]; 

  
  IDL_VPTR outargv[1];
  static IDL_LONG iread;
  static IDL_LONG iwrite;
  static IDL_LONG iupdate;
  int access=0;

  static IDL_KW_PAR kw_pars[]={IDL_KW_FAST_SCAN,
			       {"READ",IDL_TYP_LONG,1,
                                IDL_KW_ZERO,0,
                                IDL_CHARA(iread)},
			       {"WRITE",IDL_TYP_LONG,1,
                                IDL_KW_ZERO,0,
                                IDL_CHARA(iwrite)},
         	               {"UPDATE",IDL_TYP_LONG,1,
                                IDL_KW_ZERO,0,
                                IDL_CHARA(iwrite)},
				 {NULL}};

  IDL_KWCleanup(IDL_KW_MARK);
  IDL_KWGetParams(argc,argv,argk,kw_pars,outargv,1);

  IDL_ENSURE_STRING(outargv[0]);

  unit.type=IDL_TYP_LONG;
  unit.flags=0;

  fargv[0]=&unit;
  fargv[1]=outargv[0];
  
  IDL_FileGetUnit(1,fargv);  

  if (iread !=0) access=access | IDL_OPEN_R;
  if (iwrite !=0) access=access | IDL_OPEN_W;
  if (iupdate !=0) access=access | IDL_OPEN_APND;

  if (access==0) access=IDL_OPEN_R;

  IDL_FileOpen(2,fargv,NULL,access,IDL_F_STDIO,1,0);

  IDL_KWCleanup(IDL_KW_CLEAN);
  
  return IDL_GettmpLong(IDL_LongScalar(&unit));
}
Пример #3
0
/*
  socket = MG_NET_ACCEPT(socket [, BUFFER=size] [, /NODELAY])

  Accepts a requested TCP/IP connection and returns a socket on which I/O can
  be performed. The BUFFER keyword can be used to set the socket buffer size.
  For high-performance TCP/IP networks (e.g. gigE), higher bandwidth can be
  achived by setting the buffer size to several megabytes. Setting the NODELAY
  keyword disables the Nagle algorithm for the socket (appropriate for
  applications with large numbers of small packets).

	Only valid for TCP based sockets.  Will return -1 if called for a UDP
	socket.
*/
static IDL_VPTR IDL_CDECL mg_net_accept(int argc, IDL_VPTR inargv[], char *argk) {
  IDL_LONG i, j;
  struct sockaddr_in peer_addr;
  int	addr_len;
  SOCKET s;
  IDL_VPTR argv[1];

  static IDL_LONG	iBuffer, iNoDelay;
  static IDL_KW_PAR kw_pars[] = { IDL_KW_FAST_SCAN,
				  { "BUFFER", IDL_TYP_LONG, 1, IDL_KW_ZERO, 0, IDL_CHARA(iBuffer) },
				  { "NODELAY", IDL_TYP_LONG, 1, IDL_KW_ZERO, 0, IDL_CHARA(iNoDelay) },
				  { NULL }
  };

  IDL_KWCleanup(IDL_KW_MARK);
  IDL_KWGetParams(argc, inargv, argk, kw_pars, argv, 1);
  j = IDL_LongScalar(argv[0]);
  IDL_KWCleanup(IDL_KW_CLEAN);
  
  if ((j < 0) || (j >= MAX_SOCKETS)) return (IDL_GettmpLong(-1));
  if (net_list[j].iState != NET_LISTEN) return (IDL_GettmpLong(-1));
  
  for(i = 0; i < MAX_SOCKETS; i++) {
    if (net_list[i].iState == NET_UNUSED) break;
  }
  if (i == MAX_SOCKETS) return(IDL_GettmpLong(-2));

  addr_len = sizeof(struct sockaddr_in);
  s = accept(net_list[j].socket, (struct sockaddr *)&peer_addr, &addr_len);
  if (s == -1) return (IDL_GettmpLong(-1));

  if (iBuffer) rebuffer_socket(s, iBuffer);
  if (iNoDelay) nodelay_socket(s, 1);
  net_list[i].iState = NET_IO;
  net_list[i].socket = s;

  return(IDL_GettmpLong(i));
}
Пример #4
0
/*
  nbytes = MG_NET_RECV(socket, variable [, MAXIMUM_BYTES=b])

  Reads the raw data available on the socket and returns a BYTE array in
  variable. The maximum number of bytes to read can be specified by the
  MAXIMUM_BYTES keyword. The default is to read all the data available on the
  socket. Note: no byteswapping is performed.
*/
static IDL_VPTR IDL_CDECL mg_net_recv(int argc, IDL_VPTR argv[], char *argk) {
  IDL_LONG i, iRet, err;
  int len;
  IDL_VPTR vpPlainArgs[2], vpTmp;
  char *pbuffer;

  static IDL_LONG	iMax;
  static IDL_KW_PAR kw_pars[] = { IDL_KW_FAST_SCAN,
				  { "MAXIMUM_BYTES", IDL_TYP_LONG, 1, IDL_KW_ZERO, 0, IDL_CHARA(iMax) },
				  { NULL }
  };

  IDL_KWCleanup(IDL_KW_MARK);
  IDL_KWGetParams(argc, argv, argk, kw_pars, vpPlainArgs, 1);

  i = IDL_LongScalar(vpPlainArgs[0]);
  if ((i < 0) || (i >= MAX_SOCKETS)) return (IDL_GettmpLong(-1));
  if (net_list[i].iState != NET_IO) return (IDL_GettmpLong(-1));
  IDL_EXCLUDE_EXPR(vpPlainArgs[1]);

  err = IOCTL(net_list[i].socket, FIONREAD, &len);
  if (err != 0) {
    iRet = -1;
    goto err;
  }
  if (iMax) len = IDL_MIN(iMax, len);

  pbuffer = (char *) IDL_MakeTempVector(IDL_TYP_BYTE, len, IDL_ARR_INI_NOP, &vpTmp);
  IDL_VarCopy(vpTmp, vpPlainArgs[1]);

  iRet = recv(net_list[i].socket, pbuffer, len, 0);

 err:
  IDL_KWCleanup(IDL_KW_CLEAN);

  return(IDL_GettmpLong(iRet));
}
Пример #5
0
/*
  err = MG_NET_QUERY(socket [, AVAILABLE_BYTES=a] [, IS_LISTENER=l]
                     [, LOCAL_HOST=lh] [, LOCAL_PORT=lp]
                     [, REMOTE_HOST=rh] [, REMOTE_PORT=rp])

  Returns various information about the socket in question.

  AVAILABLE_BYTES: number of bytes available for reading.
  REMOTE_HOST: host number of the remote host the socket is connected to.
  IS_LISTENER: true if the socket was created using MG_NET_CREATEPORT()
*/
static IDL_VPTR IDL_CDECL mg_net_query(int argc, IDL_VPTR argv[], char *argk) {
  IDL_LONG i;
  IDL_VPTR vpPlainArgs[1],vpTmp;
  struct sockaddr_in peer_addr;
  int addr_len, err;
  IDL_LONG iRet = 0;

  static IDL_VPTR	vpRHost, vpAvail, vpListen, vpLPort, vpRPort, vpLHost;
  static IDL_KW_PAR kw_pars[] = { IDL_KW_FAST_SCAN,
				  { "AVAILABLE_BYTES", IDL_TYP_UNDEF, 1, IDL_KW_OUT | IDL_KW_ZERO, 0, IDL_CHARA(vpAvail) },
				  { "IS_LISTENER", IDL_TYP_UNDEF, 1, IDL_KW_OUT | IDL_KW_ZERO, 0, IDL_CHARA(vpListen) },
				  { "LOCAL_HOST", IDL_TYP_UNDEF, 1, IDL_KW_OUT | IDL_KW_ZERO, 0, IDL_CHARA(vpLHost) },
				  { "LOCAL_PORT", IDL_TYP_UNDEF, 1, IDL_KW_OUT | IDL_KW_ZERO, 0, IDL_CHARA(vpLPort) },
				  { "REMOTE_HOST", IDL_TYP_UNDEF, 1, IDL_KW_OUT | IDL_KW_ZERO, 0, IDL_CHARA(vpRHost) },
				  { "REMOTE_PORT", IDL_TYP_UNDEF, 1, IDL_KW_OUT | IDL_KW_ZERO, 0, IDL_CHARA(vpRPort) },
				  { NULL}
  };

  IDL_KWCleanup(IDL_KW_MARK);
  IDL_KWGetParams(argc, argv, argk, kw_pars, vpPlainArgs, 1);

  i = IDL_LongScalar(vpPlainArgs[0]);
  if ((i < 0) || (i >= MAX_SOCKETS)) {
    IDL_KWCleanup(IDL_KW_CLEAN);
    return(IDL_GettmpLong(-1));
  }

  if (vpRHost || vpRPort) {
    addr_len = sizeof(struct sockaddr_in);
    err = getpeername(net_list[i].socket,
		      (struct sockaddr *) &peer_addr, &addr_len);
    if (err != 0) {
      iRet = -1;
    } else {
      if (vpRHost) {
	vpTmp = IDL_GettmpULong(peer_addr.sin_addr.s_addr);
	IDL_VarCopy(vpTmp, vpRHost);
      }
      if (vpRPort) {
	vpTmp = IDL_GettmpLong((long) ntohs(peer_addr.sin_port));
	IDL_VarCopy(vpTmp, vpRPort);
      }
    }
  }
  if (vpAvail) {
    int len;
    err = IOCTL(net_list[i].socket, FIONREAD, &len);
    if (err != 0) {
      iRet = -1;
    } else {
      vpTmp = IDL_GettmpULong(len);
      IDL_VarCopy(vpTmp, vpAvail);
    }
  }
  if (vpListen) {
    vpTmp = IDL_GettmpLong(net_list[i].iState == NET_LISTEN);
    IDL_VarCopy(vpTmp, vpListen);
  }
  if (vpLPort || vpLHost) {
    addr_len = sizeof(struct  sockaddr_in);
    err = getsockname(net_list[i].socket,
		      (struct sockaddr *) &peer_addr, &addr_len);
    if (err != 0) {
      iRet = -1;
    } else {
      if (vpLHost) {
        vpTmp = IDL_GettmpULong(peer_addr.sin_addr.s_addr);
        IDL_VarCopy(vpTmp, vpLHost);
      }
      if (vpLPort) {
        vpTmp = IDL_GettmpLong((long) ntohs(peer_addr.sin_port));
        IDL_VarCopy(vpTmp, vpLPort);
      }
    }
  }

  IDL_KWCleanup(IDL_KW_CLEAN);

  return(IDL_GettmpLong(iRet));
}
Пример #6
0
/*
  socket = MG_NET_CONNECT(host, port [, BUFFER=size] [, LOCAL_PORT=lp]
                          [, /NODELAY] [, /TCP] [, /UDP])

  Connect to a TCP socket listener on some specified host and port. The
  returned socket can be used for I/O after the server "accepts" the
  connection.

	The BUFFER keyword can be used to set the socket buffer size. For
	high-performance TCP/IP networks (e.g. gigE), higher bandwidth can be
	achieved by setting the buffer size to several megabytes. Setting the
	NODELAY keyword disables the Nagle algorithm for the socket (appropriate for
	applications with large numbers of small packets).

  Set the UDP keyword to create a UDP port with the peer address set to the
  provided host and port value. Data can then be sent using the MG_NET_SEND
  function which is somehwhat more efficient than the MG_NET_SENDTO function.
  This is useful if you will be sending data to primarily one host/port.

  MG_NET_CONNECT only creates TCP based sockets.
*/
static IDL_VPTR IDL_CDECL mg_net_connect(int argc, IDL_VPTR inargv[], char *argk) {
  SOCKET s;
  struct sockaddr_in sin;
  int	addr_len,err;
  short	port;
  int	host;
  IDL_LONG i;
  IDL_VPTR argv[2];

  static IDL_LONG	iBuffer,iNoDelay,iUDP,iTCP, iLocPort;
  static IDL_KW_PAR kw_pars[] = { IDL_KW_FAST_SCAN,
				  { "BUFFER", IDL_TYP_LONG, 1, IDL_KW_ZERO, 0, IDL_CHARA(iBuffer) },
				  { "LOCAL_PORT", IDL_TYP_LONG, 1, IDL_KW_ZERO, 0, IDL_CHARA(iLocPort) },
				  { "NODELAY", IDL_TYP_LONG, 1, IDL_KW_ZERO, 0, IDL_CHARA(iNoDelay) },
				  { "TCP", IDL_TYP_LONG, 1, IDL_KW_ZERO, 0, IDL_CHARA(iTCP) },
				  { "UDP", IDL_TYP_LONG, 1, IDL_KW_ZERO, 0, IDL_CHARA(iUDP) },
				  { NULL }
  };

  IDL_KWCleanup(IDL_KW_MARK);
  IDL_KWGetParams(argc,inargv,argk,kw_pars,argv,1);
  host = IDL_ULongScalar(argv[0]);
  port = (short) IDL_LongScalar(argv[1]);
  IDL_KWCleanup(IDL_KW_CLEAN);

  for (i = 0; i < MAX_SOCKETS; i++) {
    if (net_list[i].iState == NET_UNUSED) break;
  }
  if (i == MAX_SOCKETS) return (IDL_GettmpLong(-2));

  if (iUDP) {
    s = socket(AF_INET,SOCK_DGRAM, 0);
    net_list[i].iType = NET_UDP_PEER;
  } else {
    s = socket(AF_INET, SOCK_STREAM, 0);
    if (iBuffer) rebuffer_socket(s, iBuffer);
    if (iNoDelay) nodelay_socket(s, 1);
    net_list[i].iType = NET_TCP;
  }
  if (s == -1) return (IDL_GettmpLong(-2));

  if (iLocPort) {
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = htonl(INADDR_ANY);
    sin.sin_port = htons((short)iLocPort);
    err=bind(s,(struct sockaddr *)&sin, sizeof(sin));
    if (err == -1) {
      CLOSE(s);
      return(IDL_GettmpLong(-2));
    }
  }

  sin.sin_addr.s_addr = host;
  sin.sin_family = AF_INET;
  sin.sin_port = htons(port);
  addr_len = sizeof(struct sockaddr_in);
  err = connect(s, (struct sockaddr *)&sin, addr_len);
  if (err == -1) {
    CLOSE(s);
    return (IDL_GettmpLong(-1));
  }

  net_list[i].iState = NET_IO;
  net_list[i].socket = s;

  return (IDL_GettmpLong(i));
}
Пример #7
0
static IDL_VPTR IDLOldCnvMapSeek(int argc,IDL_VPTR *argv,char *argk) {

  struct OldCnvMapIDLInx *ifptr;

  int s=0,n;
  struct RfileIndex *finx=NULL;
  static IDL_VPTR vatme;
  double atme=0;

  int outargc=0;
  IDL_VPTR outargv[8];
  static IDL_KW_PAR kw_pars[]={IDL_KW_FAST_SCAN,
			       {"ATME",IDL_TYP_UNDEF,1,
                                IDL_KW_OUT | IDL_KW_ZERO,0,
                                IDL_CHARA(vatme)},
				 {NULL}};


  IDL_LONG unit=0,yr=0,mo=0,dy=0,hr=0,mt=0,sc=0;
  IDL_FILE_STAT stat;

  FILE *fp=NULL;

  IDL_KWCleanup(IDL_KW_MARK);
  outargc=IDL_KWGetParams(argc,argv,argk,kw_pars,outargv,1);


  IDL_ENSURE_SCALAR(outargv[0]);
  IDL_ENSURE_SCALAR(outargv[1]);
  IDL_ENSURE_SCALAR(outargv[2]);
  IDL_ENSURE_SCALAR(outargv[3]);
  IDL_ENSURE_SCALAR(outargv[4]);
  IDL_ENSURE_SCALAR(outargv[5]);
  IDL_ENSURE_SCALAR(outargv[6]);

  if (outargc>7) IDL_ENSURE_ARRAY(outargv[7]);

  unit=IDL_LongScalar(outargv[0]);

  s=IDL_FileEnsureStatus(IDL_MSG_RET,unit,IDL_EFS_USER);

  if (s==FALSE) {
    s=-1;
    return (IDL_GettmpLong(s));
  }

  /* Get information about the file */

  IDL_FileFlushUnit(unit);
  IDL_FileStat(unit,&stat);
 
  /* Find the file pointer */

  fp=stat.fptr;
  
  if (fp==NULL) {
    s=-1;
    return (IDL_GettmpLong(s));
  }

  yr=IDL_LongScalar(outargv[1]);
  mo=IDL_LongScalar(outargv[2]);
  dy=IDL_LongScalar(outargv[3]);
  hr=IDL_LongScalar(outargv[4]);
  mt=IDL_LongScalar(outargv[5]);
  sc=IDL_LongScalar(outargv[6]);

  /* test for existence of index */

  if (outargc>7) {
    /* decode index here */
    finx=malloc(sizeof(struct RfileIndex));
    if (finx==NULL) {
      s=-1;
      return (IDL_GettmpLong(s));
    }
    finx->num=outargv[7]->value.s.arr->n_elts;
    finx->time.start=malloc(sizeof(double)*finx->num);
    if (finx->time.start==NULL) {
      s=-1;
      free(finx);
      return (IDL_GettmpLong(s));
    } 
    finx->time.end=malloc(sizeof(double)*finx->num);
    if (finx->time.end==NULL) {
      s=-1;
      free(finx->time.start);
      free(finx);
      return (IDL_GettmpLong(s));
    } 

    finx->offset=malloc(sizeof(int)*finx->num);
    if (finx->offset==NULL) {
      s=-1;
      free(finx->time.start);
      free(finx->time.end);
      free(finx);
      return (IDL_GettmpLong(s));
    } 
    for (n=0;n<finx->num;n++) {
      ifptr=(struct OldCnvMapIDLInx *) (outargv[7]->value.s.arr->data+
                                    n*outargv[7]->value.s.arr->elt_len);
      finx->time.start[n]=ifptr->st_time;
      finx->time.end[n]=ifptr->ed_time;
      finx->offset[n]=ifptr->offset;
    }
    finx->st_time=finx->time.start[0];
    finx->ed_time=finx->time.end[finx->num-1];

  }


  s=OldCnvMapFseek(fp,yr,mo,dy,hr,mt,sc,finx,&atme);

  if (vatme) IDL_StoreScalar(vatme,IDL_TYP_DOUBLE,(IDL_ALLTYPES *) &atme);
 
  if (finx !=NULL) {
    free(finx->time.start);
    free(finx->time.end);
    free(finx->offset);
    free(finx);
  }
   
  IDL_KWCleanup(IDL_KW_CLEAN);
  return (IDL_GettmpLong(s));


}
Пример #8
0
static IDL_VPTR IDLOldRawSeek(int argc,IDL_VPTR *argv,char *argk) {

  IDL_FILE_STAT stat;
  struct OldRawIDLFp *irawfp;
  struct OldRawFp rawfp;
  int s=0;
  static IDL_VPTR vatme;
  double atme=0;

  IDL_VPTR outargv[8];
  static IDL_KW_PAR kw_pars[]={IDL_KW_FAST_SCAN,
			       {"ATME",IDL_TYP_UNDEF,1,
                                IDL_KW_OUT | IDL_KW_ZERO,0,
                                IDL_CHARA(vatme)},
				 {NULL}};


  IDL_LONG yr=0,mo=0,dy=0,hr=0,mt=0,sc=0;
  FILE *ffp=NULL,*ifp=NULL;

  IDL_KWCleanup(IDL_KW_MARK);
  IDL_KWGetParams(argc,argv,argk,kw_pars,outargv,1);

  IDL_ENSURE_STRUCTURE(outargv[0]);
  IDL_ENSURE_SCALAR(outargv[1]);
  IDL_ENSURE_SCALAR(outargv[2]);
  IDL_ENSURE_SCALAR(outargv[3]);
  IDL_ENSURE_SCALAR(outargv[4]);
  IDL_ENSURE_SCALAR(outargv[5]);
  IDL_ENSURE_SCALAR(outargv[6]);

  yr=IDL_LongScalar(outargv[1]);
  mo=IDL_LongScalar(outargv[2]);
  dy=IDL_LongScalar(outargv[3]);
  hr=IDL_LongScalar(outargv[4]);
  mt=IDL_LongScalar(outargv[5]);
  sc=IDL_LongScalar(outargv[6]);

  irawfp=(struct OldRawIDLFp *) outargv[0]->value.s.arr->data;
  
  s=IDL_FileEnsureStatus(IDL_MSG_RET,irawfp->rawunit,IDL_EFS_USER);

  if (s==FALSE) {
    s=-1;
    return (IDL_GettmpLong(s));
  }

  IDL_FileFlushUnit(irawfp->rawunit);
  IDL_FileStat(irawfp->rawunit,&stat);
 

  ffp=stat.fptr;
  
  if (ffp==NULL) {
    s=-1;
    return (IDL_GettmpLong(s));
  }
 
  if (irawfp->inxunit !=-1) {
    IDL_FileFlushUnit(irawfp->inxunit);
    IDL_FileStat(irawfp->inxunit,&stat);
    ifp=stat.fptr;
  }

  if (ifp !=NULL) fflush(ifp);

  rawfp.rawfp=fileno(ffp);
  if (ifp !=NULL) rawfp.inxfp=fileno(ifp);
  else rawfp.inxfp=-1;

  OldRawIDLToRawFp(irawfp,&rawfp);

  s=OldRawSeek(&rawfp,yr,mo,dy,hr,mt,sc,&atme);

  OldRawRawFpToIDL(&rawfp,irawfp);


  if (vatme) IDL_StoreScalar(vatme,IDL_TYP_DOUBLE,(IDL_ALLTYPES *) &atme);
 
  IDL_KWCleanup(IDL_KW_CLEAN);
  return (IDL_GettmpLong(s));
}