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