Annotation of OpenXM/src/kan96xx/plugin/mytcpio.c, Revision 1.10
1.10 ! takayama 1: /* $OpenXM: OpenXM/src/kan96xx/plugin/mytcpio.c,v 1.9 2002/10/23 08:42:22 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
1.7 takayama 26: #define MAX_LISTEN_QUEUE 3
1.1 maekawa 27: static void errorMsg1s(char *s) {
28: fprintf(stderr,"%s\n",s);
29: }
30:
1.3 takayama 31: #define SET_TCPIOERROR { if (TcpioError == NULL) TcpioError = stdout; }
32: FILE *TcpioError = NULL;
1.1 maekawa 33: int OpenedSocket = 0;
34: extern int Quiet;
35:
36: socketOpen(char *serverName,int portNumber) {
37: static struct hostent *myhost;
38: static struct sockaddr_in me;
39: static int s_waiting;
40: static int on;
41: extern int errno;
42: int tt;
43:
1.3 takayama 44: SET_TCPIOERROR;
1.1 maekawa 45: fprintf(TcpioError,"Hello from open. serverName is %s and portnumber is %d\n",
1.5 takayama 46: serverName,portNumber);
1.1 maekawa 47: if ((myhost = gethostbyname(serverName)) == NULL) {
48: errorMsg1s("Bad server name.");
49: return(-1);
50: }
51: bzero((char *)&me,sizeof(me));
52: me.sin_family = AF_INET;
53: me.sin_port = htons(portNumber);
54: bcopy(myhost->h_addr,
1.5 takayama 55: &me.sin_addr,myhost->h_length);
1.1 maekawa 56:
57: if ((s_waiting = socket(AF_INET,SOCK_STREAM,0)) < 0) {
58: errorMsg1s("Socket allocation is failed.");
59: return(-1);
60: }
61:
62: on=1; setsockopt(s_waiting,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
63: /* important */
64: if ((tt = bind(s_waiting,(struct sockaddr *) &me,sizeof(me))) == -1) {
65: fprintf(TcpioError,"Bind error. Error no is %d. See /usr/include/sys/errno.h. (asm/errno.h)\n",errno);
66: errorMsg1s("cannot bind");
67: return(-1);
68: }
69: /* printf("bind returns %d\n",tt); */
70: tt = sizeof(me);
71: if (getsockname(s_waiting,(struct sockaddr *)&me,&tt) < 0) {
72: fprintf(TcpioError,"getsockname error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
73: errorMsg1s("cannot getsockname");
74: return(-1);
75: }
76:
1.7 takayama 77: if (listen(s_waiting,MAX_LISTEN_QUEUE) < 0) {
1.1 maekawa 78: errorMsg1s("Listen failed");
79: return(-1);
80: }
81: fprintf(TcpioError,"Done the initialization. port =%d\n",ntohs(me.sin_port));
82: OpenedSocket = ntohs(me.sin_port);
83: return(s_waiting);
84: }
85:
86:
87: socketAccept(int snum) {
88: int s, news;
89:
1.3 takayama 90: SET_TCPIOERROR;
1.1 maekawa 91: s = snum;
92: fprintf(TcpioError,"Trying to accept... "); fflush(TcpioError);
93: if ((news = accept(s,NULL,NULL)) < 0) {
94: errorMsg1s("Error in accept.");
95: return(-1);
96: }
97: fprintf(TcpioError,"Accepted.\n"); fflush(TcpioError);
98: if (close(s) < 0) {
99: errorMsg1s("Error in closing the old socket.");
100: return(-1);
101: }
102: return(news);
103: }
104:
105: socketAcceptLocal(int snum) {
106: int s, news;
107: struct sockaddr peer;
108: int len;
109: int i;
1.3 takayama 110:
111: SET_TCPIOERROR;
1.1 maekawa 112: s = snum;
113: fprintf(TcpioError,"Trying to accept from localhost... "); fflush(TcpioError);
114: len = sizeof(struct sockaddr);
115: if ((news = accept(s,&peer,&len)) < 0) {
1.10 ! takayama 116: errorMsg1s("Error in accept. Retrying");
! 117: { /* Code added for strange behavior on cygwin. */
! 118: int i;
! 119: for (i=0; i<5; i++) {
! 120: if ((news = accept(s,&peer,&len)) < 0) {
! 121: fprintf(stderr,"%d : ",i);
! 122: errorMsg1s("Error in accept. Retrying");
! 123: }
! 124: }
! 125: if (news < 0) {
! 126: errorMsg1s("Error in accept. Retrying");
! 127: return(-1);
! 128: }
! 129: }
1.1 maekawa 130: }
131:
132: len = sizeof(struct sockaddr);
133: getpeername(news,&peer,&len);
134: printf("len= %d\n",len);
135: for (i=0; i<len; i++) {
136: printf(" %x ",peer.sa_data[i]);
137: }
138: printf("\n");
139: if (peer.sa_data[2] == 0x7f && peer.sa_data[3] == 0 &&
140: peer.sa_data[4] == 0 && peer.sa_data[5] == 1) {
1.4 takayama 141: fprintf(stderr,"Authentication: localhost is allowed to be accepted.\n");
1.1 maekawa 142: }else{
1.4 takayama 143: errorMsg1s("Authentication: The connection is not from the localhost.");
1.1 maekawa 144: close(s);
145: fprintf(stderr,"The connection is refused.");
146: return(-1);
147: }
148:
149: fprintf(TcpioError,"Accepted.\n"); fflush(TcpioError);
150: if (close(s) < 0) {
151: errorMsg1s("Error in closing the old socket.");
152: return(-1);
153: }
1.8 takayama 154: return(news);
155: }
156:
157: /* It does not close the socket for listening. */
158: socketAcceptLocal2(int snum) {
159: int s, news;
160: struct sockaddr peer;
161: int len;
162: int i;
163:
164: SET_TCPIOERROR;
165: s = snum;
166: fprintf(TcpioError,"Trying to accept from localhost... "); fflush(TcpioError);
167: len = sizeof(struct sockaddr);
168: if ((news = accept(s,&peer,&len)) < 0) {
169: errorMsg1s("Error in accept.");
170: return(-1);
171: }
172:
173: len = sizeof(struct sockaddr);
174: getpeername(news,&peer,&len);
175: printf("len= %d\n",len);
176: for (i=0; i<len; i++) {
177: printf(" %x ",peer.sa_data[i]);
178: }
179: printf("\n");
180: if (peer.sa_data[2] == 0x7f && peer.sa_data[3] == 0 &&
181: peer.sa_data[4] == 0 && peer.sa_data[5] == 1) {
182: fprintf(stderr,"Authentication: localhost is allowed to be accepted.\n");
183: }else{
184: errorMsg1s("Authentication: The connection is not from the localhost.");
185: fprintf(stderr,"The connection is refused.");
186: return(-1);
187: }
188:
189: fprintf(TcpioError,"Accepted.\n"); fflush(TcpioError);
1.1 maekawa 190: return(news);
191: }
192:
193: int oxSocketSelect0(int fd,int t) {
194: fd_set readfds;
195: struct timeval timeout;
196: int debug = 0;
197: extern int errno;
1.3 takayama 198: SET_TCPIOERROR;
1.1 maekawa 199: FD_ZERO(&readfds);
200: FD_SET(fd,&readfds);
201: timeout.tv_sec = 0;
202: timeout.tv_usec = (long) t;
203: if (t >= 0) {
204: if (debug) {printf("select t>=0 for fd = %d\n",fd); fflush(NULL);}
205: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
206: /* It must be fd+1 !, Not fd. */
207: fprintf(TcpioError,"select (non-block) error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
208: errorMsg1s("oxSocketSelect0() : select failed.");
209: return(0);
210: }
211: if(debug){printf("Return from select t>=0 for fd = %d\n",fd);fflush(NULL);}
212: }else{ /* block */
213: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
214: fprintf(TcpioError,"select (block) error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
215: errorMsg1s("socketSelect0() : select failed.");
216: return(0);
217: }
218: }
219: if (FD_ISSET(fd,&readfds)) return(1);
220: else return(0);
221: }
222:
223: oxSocketMultiSelect(int sid[],int size,int t,int result[])
224: {
225: int i,fd,p;
226: fd_set readfds;
227: struct timeval timeout;
228: int isdata = 0;
229: extern errno;
230:
1.3 takayama 231: SET_TCPIOERROR;
1.1 maekawa 232: FD_ZERO(&readfds);
233: timeout.tv_sec = 0;
234: timeout.tv_usec = (long)t;
235:
236: fd = 0;
237:
238: for (i=0; i<size; i++) {
239: if (sid[i] >= 0) {
240: p = sid[i];
241: if (p > fd) fd = p;
242: FD_SET(p,&readfds);
243: /* printf("p = %d, fd=%d",p,fd); */
244: }
245: }
246:
247: /* printf("MultiSelect..\n"); fflush(NULL); */
248: if (t >= 0) {
249: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
250: /* It must be fd+1 !, Not fd. */
251: fprintf(TcpioError,"Select error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
252: errorMsg1s("oxSocketMultiSelect() : select failed.");
253: return(0);
254: }
255: }else{ /* block */
256: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
257: fprintf(TcpioError,"Select error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
258: errorMsg1s("oxSocketMultiSelect() : (block) select failed.");
259: return(0);
260: }
261: }
262: /* printf("Done. MultiSelect.\n"); fflush(NULL); */
263: for (i=0; i<size; i++) {
264: p = sid[i];
265: if ((sid[i] >=0) && FD_ISSET(p,&readfds)) {
266: result[i] = 1; isdata = 1;
267: }else{
268: result[i] = 0;
269: }
270: }
271: return(isdata);
272: }
273:
274:
275: socketConnect(char *serverName,int portNumber) {
276: struct hostent *servhost;
277: struct sockaddr_in server;
278: int socketid;
279: int on;
280:
1.3 takayama 281: SET_TCPIOERROR;
1.1 maekawa 282: if ((servhost = gethostbyname(serverName)) == NULL) {
283: errorMsg1s("bad server name.\n");
284: return(-1);
285: }
286: bzero((char *)&server,sizeof(server));
287: server.sin_family = AF_INET;
288: server.sin_port = htons(portNumber);
289: bcopy(servhost->h_addr,
1.5 takayama 290: (char *)&server.sin_addr,servhost->h_length);
1.1 maekawa 291:
292: if ((socketid = socket(AF_INET,SOCK_STREAM,0)) <0) {
293: errorMsg1s("socket allocation is failed.\n");
294: return(-1);
295: }
296: /* on=1; setsockopt(socketid,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); */
297: if (!Quiet) {
298: fprintf(TcpioError,"Trying to connect port %d, ip=%x\n",ntohs(server.sin_port),server.sin_addr);
299: }
300: if (connect(socketid,(struct sockaddr *)&server,sizeof(server)) == -1) {
301: errorMsg1s("cannot connect");
302: return(-1);
303: }
304: if (!Quiet) fprintf(TcpioError,"connected.\n");
305: return(socketid);
306: }
307:
308: socketConnectWithPass(char *servername,int port,char *pass)
309: {
310: int fd;
311: int m;
1.3 takayama 312: SET_TCPIOERROR;
1.1 maekawa 313: fd = socketConnect(servername,port);
1.9 takayama 314: if ((pass == NULL) && (fd >= 0)) return fd;
315: if ((pass == NULL) && (fd < 0)) return -1;
1.1 maekawa 316: if (fd >= 0) {
1.2 takayama 317: m = write(fd,pass,strlen(pass)+1);
318: if (m != strlen(pass)+1) {
1.1 maekawa 319: fprintf(TcpioError,"Fail to send password to fd=%d.\n",fd);
320: return(-1);
321: }
322: return(fd);
323: }else{
324: return(-1);
325: }
326: }
327:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>