Annotation of OpenXM/src/kan96xx/plugin/mytcpio.c, Revision 1.8
1.8 ! takayama 1: /* $OpenXM: OpenXM/src/kan96xx/plugin/mytcpio.c,v 1.7 2002/10/20 07:58:18 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) {
116: errorMsg1s("Error in accept.");
117: return(-1);
118: }
119:
120: len = sizeof(struct sockaddr);
121: getpeername(news,&peer,&len);
122: printf("len= %d\n",len);
123: for (i=0; i<len; i++) {
124: printf(" %x ",peer.sa_data[i]);
125: }
126: printf("\n");
127: if (peer.sa_data[2] == 0x7f && peer.sa_data[3] == 0 &&
128: peer.sa_data[4] == 0 && peer.sa_data[5] == 1) {
1.4 takayama 129: fprintf(stderr,"Authentication: localhost is allowed to be accepted.\n");
1.1 maekawa 130: }else{
1.4 takayama 131: errorMsg1s("Authentication: The connection is not from the localhost.");
1.1 maekawa 132: close(s);
133: fprintf(stderr,"The connection is refused.");
134: return(-1);
135: }
136:
137: fprintf(TcpioError,"Accepted.\n"); fflush(TcpioError);
138: if (close(s) < 0) {
139: errorMsg1s("Error in closing the old socket.");
140: return(-1);
141: }
1.8 ! takayama 142: return(news);
! 143: }
! 144:
! 145: /* It does not close the socket for listening. */
! 146: socketAcceptLocal2(int snum) {
! 147: int s, news;
! 148: struct sockaddr peer;
! 149: int len;
! 150: int i;
! 151:
! 152: SET_TCPIOERROR;
! 153: s = snum;
! 154: fprintf(TcpioError,"Trying to accept from localhost... "); fflush(TcpioError);
! 155: len = sizeof(struct sockaddr);
! 156: if ((news = accept(s,&peer,&len)) < 0) {
! 157: errorMsg1s("Error in accept.");
! 158: return(-1);
! 159: }
! 160:
! 161: len = sizeof(struct sockaddr);
! 162: getpeername(news,&peer,&len);
! 163: printf("len= %d\n",len);
! 164: for (i=0; i<len; i++) {
! 165: printf(" %x ",peer.sa_data[i]);
! 166: }
! 167: printf("\n");
! 168: if (peer.sa_data[2] == 0x7f && peer.sa_data[3] == 0 &&
! 169: peer.sa_data[4] == 0 && peer.sa_data[5] == 1) {
! 170: fprintf(stderr,"Authentication: localhost is allowed to be accepted.\n");
! 171: }else{
! 172: errorMsg1s("Authentication: The connection is not from the localhost.");
! 173: fprintf(stderr,"The connection is refused.");
! 174: return(-1);
! 175: }
! 176:
! 177: fprintf(TcpioError,"Accepted.\n"); fflush(TcpioError);
1.1 maekawa 178: return(news);
179: }
180:
181: int oxSocketSelect0(int fd,int t) {
182: fd_set readfds;
183: struct timeval timeout;
184: int debug = 0;
185: extern int errno;
1.3 takayama 186: SET_TCPIOERROR;
1.1 maekawa 187: FD_ZERO(&readfds);
188: FD_SET(fd,&readfds);
189: timeout.tv_sec = 0;
190: timeout.tv_usec = (long) t;
191: if (t >= 0) {
192: if (debug) {printf("select t>=0 for fd = %d\n",fd); fflush(NULL);}
193: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
194: /* It must be fd+1 !, Not fd. */
195: fprintf(TcpioError,"select (non-block) error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
196: errorMsg1s("oxSocketSelect0() : select failed.");
197: return(0);
198: }
199: if(debug){printf("Return from select t>=0 for fd = %d\n",fd);fflush(NULL);}
200: }else{ /* block */
201: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
202: fprintf(TcpioError,"select (block) error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
203: errorMsg1s("socketSelect0() : select failed.");
204: return(0);
205: }
206: }
207: if (FD_ISSET(fd,&readfds)) return(1);
208: else return(0);
209: }
210:
211: oxSocketMultiSelect(int sid[],int size,int t,int result[])
212: {
213: int i,fd,p;
214: fd_set readfds;
215: struct timeval timeout;
216: int isdata = 0;
217: extern errno;
218:
1.3 takayama 219: SET_TCPIOERROR;
1.1 maekawa 220: FD_ZERO(&readfds);
221: timeout.tv_sec = 0;
222: timeout.tv_usec = (long)t;
223:
224: fd = 0;
225:
226: for (i=0; i<size; i++) {
227: if (sid[i] >= 0) {
228: p = sid[i];
229: if (p > fd) fd = p;
230: FD_SET(p,&readfds);
231: /* printf("p = %d, fd=%d",p,fd); */
232: }
233: }
234:
235: /* printf("MultiSelect..\n"); fflush(NULL); */
236: if (t >= 0) {
237: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
238: /* It must be fd+1 !, Not fd. */
239: fprintf(TcpioError,"Select error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
240: errorMsg1s("oxSocketMultiSelect() : select failed.");
241: return(0);
242: }
243: }else{ /* block */
244: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
245: fprintf(TcpioError,"Select error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
246: errorMsg1s("oxSocketMultiSelect() : (block) select failed.");
247: return(0);
248: }
249: }
250: /* printf("Done. MultiSelect.\n"); fflush(NULL); */
251: for (i=0; i<size; i++) {
252: p = sid[i];
253: if ((sid[i] >=0) && FD_ISSET(p,&readfds)) {
254: result[i] = 1; isdata = 1;
255: }else{
256: result[i] = 0;
257: }
258: }
259: return(isdata);
260: }
261:
262:
263: socketConnect(char *serverName,int portNumber) {
264: struct hostent *servhost;
265: struct sockaddr_in server;
266: int socketid;
267: int on;
268:
1.3 takayama 269: SET_TCPIOERROR;
1.1 maekawa 270: if ((servhost = gethostbyname(serverName)) == NULL) {
271: errorMsg1s("bad server name.\n");
272: return(-1);
273: }
274: bzero((char *)&server,sizeof(server));
275: server.sin_family = AF_INET;
276: server.sin_port = htons(portNumber);
277: bcopy(servhost->h_addr,
1.5 takayama 278: (char *)&server.sin_addr,servhost->h_length);
1.1 maekawa 279:
280: if ((socketid = socket(AF_INET,SOCK_STREAM,0)) <0) {
281: errorMsg1s("socket allocation is failed.\n");
282: return(-1);
283: }
284: /* on=1; setsockopt(socketid,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); */
285: if (!Quiet) {
286: fprintf(TcpioError,"Trying to connect port %d, ip=%x\n",ntohs(server.sin_port),server.sin_addr);
287: }
288: if (connect(socketid,(struct sockaddr *)&server,sizeof(server)) == -1) {
289: errorMsg1s("cannot connect");
290: return(-1);
291: }
292: if (!Quiet) fprintf(TcpioError,"connected.\n");
293: return(socketid);
294: }
295:
296: socketConnectWithPass(char *servername,int port,char *pass)
297: {
298: int fd;
299: int m;
1.3 takayama 300: SET_TCPIOERROR;
1.1 maekawa 301: fd = socketConnect(servername,port);
302: if (fd >= 0) {
1.2 takayama 303: m = write(fd,pass,strlen(pass)+1);
304: if (m != strlen(pass)+1) {
1.1 maekawa 305: fprintf(TcpioError,"Fail to send password to fd=%d.\n",fd);
306: return(-1);
307: }
308: return(fd);
309: }else{
310: return(-1);
311: }
312: }
313:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>