Annotation of OpenXM/src/kan96xx/plugin/mytcpio.c, Revision 1.1
1.1 ! maekawa 1: #include <stdio.h>
! 2: #include <sys/types.h>
! 3: #include <sys/socket.h>
! 4: #include <sys/time.h>
! 5: #include <netinet/in.h>
! 6: #include <netdb.h>
! 7: #include <setjmp.h>
! 8: /* -lnsl -lsocket /usr/ucblib/libucb.a */
! 9: #include "ox_kan.h"
! 10: /*
! 11: MyEnv_oxmisc is used in oxmisc.c.
! 12: It cannot be put in oxmisc.c for the case of sun.
! 13: */
! 14: #if defined(sun)
! 15: int MyEnv_oxmisc[2000];
! 16: #else
! 17: jmp_buf MyEnv_oxmisc; /* may cause a trouble in Solaris. */
! 18: #endif
! 19:
! 20: #define READBUFSIZE 10000
! 21: static void errorMsg1s(char *s) {
! 22: fprintf(stderr,"%s\n",s);
! 23: }
! 24:
! 25: FILE *TcpioError = stdout;
! 26: int OpenedSocket = 0;
! 27: extern int Quiet;
! 28:
! 29: socketOpen(char *serverName,int portNumber) {
! 30: static struct hostent *myhost;
! 31: static struct sockaddr_in me;
! 32: static int s_waiting;
! 33: static int on;
! 34: extern int errno;
! 35: int tt;
! 36:
! 37:
! 38: fprintf(TcpioError,"Hello from open. serverName is %s and portnumber is %d\n",
! 39: serverName,portNumber);
! 40: if ((myhost = gethostbyname(serverName)) == NULL) {
! 41: errorMsg1s("Bad server name.");
! 42: return(-1);
! 43: }
! 44: bzero((char *)&me,sizeof(me));
! 45: me.sin_family = AF_INET;
! 46: me.sin_port = htons(portNumber);
! 47: bcopy(myhost->h_addr,
! 48: &me.sin_addr,myhost->h_length);
! 49:
! 50: if ((s_waiting = socket(AF_INET,SOCK_STREAM,0)) < 0) {
! 51: errorMsg1s("Socket allocation is failed.");
! 52: return(-1);
! 53: }
! 54:
! 55: on=1; setsockopt(s_waiting,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
! 56: /* important */
! 57: if ((tt = bind(s_waiting,(struct sockaddr *) &me,sizeof(me))) == -1) {
! 58: fprintf(TcpioError,"Bind error. Error no is %d. See /usr/include/sys/errno.h. (asm/errno.h)\n",errno);
! 59: errorMsg1s("cannot bind");
! 60: return(-1);
! 61: }
! 62: /* printf("bind returns %d\n",tt); */
! 63: tt = sizeof(me);
! 64: if (getsockname(s_waiting,(struct sockaddr *)&me,&tt) < 0) {
! 65: fprintf(TcpioError,"getsockname error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
! 66: errorMsg1s("cannot getsockname");
! 67: return(-1);
! 68: }
! 69:
! 70: if (listen(s_waiting,1) < 0) {
! 71: errorMsg1s("Listen failed");
! 72: return(-1);
! 73: }
! 74: fprintf(TcpioError,"Done the initialization. port =%d\n",ntohs(me.sin_port));
! 75: OpenedSocket = ntohs(me.sin_port);
! 76: return(s_waiting);
! 77: }
! 78:
! 79:
! 80: socketAccept(int snum) {
! 81: int s, news;
! 82:
! 83: s = snum;
! 84: fprintf(TcpioError,"Trying to accept... "); fflush(TcpioError);
! 85: if ((news = accept(s,NULL,NULL)) < 0) {
! 86: errorMsg1s("Error in accept.");
! 87: return(-1);
! 88: }
! 89: fprintf(TcpioError,"Accepted.\n"); fflush(TcpioError);
! 90: if (close(s) < 0) {
! 91: errorMsg1s("Error in closing the old socket.");
! 92: return(-1);
! 93: }
! 94: return(news);
! 95: }
! 96:
! 97: socketAcceptLocal(int snum) {
! 98: int s, news;
! 99: struct sockaddr peer;
! 100: int len;
! 101: int i;
! 102:
! 103: s = snum;
! 104: fprintf(TcpioError,"Trying to accept from localhost... "); fflush(TcpioError);
! 105: len = sizeof(struct sockaddr);
! 106: if ((news = accept(s,&peer,&len)) < 0) {
! 107: errorMsg1s("Error in accept.");
! 108: return(-1);
! 109: }
! 110:
! 111: len = sizeof(struct sockaddr);
! 112: getpeername(news,&peer,&len);
! 113: printf("len= %d\n",len);
! 114: for (i=0; i<len; i++) {
! 115: printf(" %x ",peer.sa_data[i]);
! 116: }
! 117: printf("\n");
! 118: if (peer.sa_data[2] == 0x7f && peer.sa_data[3] == 0 &&
! 119: peer.sa_data[4] == 0 && peer.sa_data[5] == 1) {
! 120: fprintf(stderr,"Authentification: localhost is allowed to be accepted.\n");
! 121: }else{
! 122: errorMsg1s("Authentification: The connection is not from the localhost.");
! 123: close(s);
! 124: fprintf(stderr,"The connection is refused.");
! 125: return(-1);
! 126: }
! 127:
! 128: fprintf(TcpioError,"Accepted.\n"); fflush(TcpioError);
! 129: if (close(s) < 0) {
! 130: errorMsg1s("Error in closing the old socket.");
! 131: return(-1);
! 132: }
! 133: return(news);
! 134: }
! 135:
! 136: int oxSocketSelect0(int fd,int t) {
! 137: fd_set readfds;
! 138: struct timeval timeout;
! 139: int debug = 0;
! 140: extern int errno;
! 141: FD_ZERO(&readfds);
! 142: FD_SET(fd,&readfds);
! 143: timeout.tv_sec = 0;
! 144: timeout.tv_usec = (long) t;
! 145: if (t >= 0) {
! 146: if (debug) {printf("select t>=0 for fd = %d\n",fd); fflush(NULL);}
! 147: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
! 148: /* It must be fd+1 !, Not fd. */
! 149: fprintf(TcpioError,"select (non-block) error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
! 150: errorMsg1s("oxSocketSelect0() : select failed.");
! 151: return(0);
! 152: }
! 153: if(debug){printf("Return from select t>=0 for fd = %d\n",fd);fflush(NULL);}
! 154: }else{ /* block */
! 155: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
! 156: fprintf(TcpioError,"select (block) error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
! 157: errorMsg1s("socketSelect0() : select failed.");
! 158: return(0);
! 159: }
! 160: }
! 161: if (FD_ISSET(fd,&readfds)) return(1);
! 162: else return(0);
! 163: }
! 164:
! 165: oxSocketMultiSelect(int sid[],int size,int t,int result[])
! 166: {
! 167: int i,fd,p;
! 168: fd_set readfds;
! 169: struct timeval timeout;
! 170: int isdata = 0;
! 171: extern errno;
! 172:
! 173: FD_ZERO(&readfds);
! 174: timeout.tv_sec = 0;
! 175: timeout.tv_usec = (long)t;
! 176:
! 177: fd = 0;
! 178:
! 179: for (i=0; i<size; i++) {
! 180: if (sid[i] >= 0) {
! 181: p = sid[i];
! 182: if (p > fd) fd = p;
! 183: FD_SET(p,&readfds);
! 184: /* printf("p = %d, fd=%d",p,fd); */
! 185: }
! 186: }
! 187:
! 188: /* printf("MultiSelect..\n"); fflush(NULL); */
! 189: if (t >= 0) {
! 190: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
! 191: /* It must be fd+1 !, Not fd. */
! 192: fprintf(TcpioError,"Select error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
! 193: errorMsg1s("oxSocketMultiSelect() : select failed.");
! 194: return(0);
! 195: }
! 196: }else{ /* block */
! 197: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
! 198: fprintf(TcpioError,"Select error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
! 199: errorMsg1s("oxSocketMultiSelect() : (block) select failed.");
! 200: return(0);
! 201: }
! 202: }
! 203: /* printf("Done. MultiSelect.\n"); fflush(NULL); */
! 204: for (i=0; i<size; i++) {
! 205: p = sid[i];
! 206: if ((sid[i] >=0) && FD_ISSET(p,&readfds)) {
! 207: result[i] = 1; isdata = 1;
! 208: }else{
! 209: result[i] = 0;
! 210: }
! 211: }
! 212: return(isdata);
! 213: }
! 214:
! 215:
! 216: socketConnect(char *serverName,int portNumber) {
! 217: struct hostent *servhost;
! 218: struct sockaddr_in server;
! 219: int socketid;
! 220: int on;
! 221:
! 222: if ((servhost = gethostbyname(serverName)) == NULL) {
! 223: errorMsg1s("bad server name.\n");
! 224: return(-1);
! 225: }
! 226: bzero((char *)&server,sizeof(server));
! 227: server.sin_family = AF_INET;
! 228: server.sin_port = htons(portNumber);
! 229: bcopy(servhost->h_addr,
! 230: (char *)&server.sin_addr,servhost->h_length);
! 231:
! 232: if ((socketid = socket(AF_INET,SOCK_STREAM,0)) <0) {
! 233: errorMsg1s("socket allocation is failed.\n");
! 234: return(-1);
! 235: }
! 236: /* on=1; setsockopt(socketid,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); */
! 237: if (!Quiet) {
! 238: fprintf(TcpioError,"Trying to connect port %d, ip=%x\n",ntohs(server.sin_port),server.sin_addr);
! 239: }
! 240: if (connect(socketid,(struct sockaddr *)&server,sizeof(server)) == -1) {
! 241: errorMsg1s("cannot connect");
! 242: return(-1);
! 243: }
! 244: if (!Quiet) fprintf(TcpioError,"connected.\n");
! 245: return(socketid);
! 246: }
! 247:
! 248: socketConnectWithPass(char *servername,int port,char *pass)
! 249: {
! 250: int fd;
! 251: int m;
! 252: fd = socketConnect(servername,port);
! 253: if (fd >= 0) {
! 254: m = write(fd,pass,strlen(pass));
! 255: if (m != strlen(pass)) {
! 256: fprintf(TcpioError,"Fail to send password to fd=%d.\n",fd);
! 257: return(-1);
! 258: }
! 259: return(fd);
! 260: }else{
! 261: return(-1);
! 262: }
! 263: }
! 264:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>