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