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