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