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