2 #include <sys/socket.h>
3 #include <netinet/in.h>
9 #include "timeoutconn.h"
10 #include "timeoutread.h"
11 #include "timeoutwrite.h"
12 #include "remoteinfo.h"
14 static char line
[999];
17 static int mywrite(fd
,buf
,len
) int fd
; char *buf
; int len
;
19 return timeoutwrite(t
,fd
,buf
,len
);
21 static int myread(fd
,buf
,len
) int fd
; char *buf
; int len
;
23 return timeoutread(t
,fd
,buf
,len
);
26 char *remoteinfo_get(ipr
,rp
,ipl
,lp
,timeout
)
27 struct ip_address
*ipr
;
29 struct ip_address
*ipl
;
35 struct sockaddr_in sin
;
44 s
= socket(AF_INET
,SOCK_STREAM
,0);
45 if (s
== -1) return 0;
47 byte_zero(&sin
,sizeof(sin
));
48 sin
.sin_family
= AF_INET
;
49 byte_copy(&sin
.sin_addr
,4,ipl
);
51 if (bind(s
,(struct sockaddr
*) &sin
,sizeof(sin
)) == -1) { close(s
); return 0; }
52 if (timeoutconn(s
,ipr
,113,timeout
) == -1) { close(s
); return 0; }
53 fcntl(s
,F_SETFL
,fcntl(s
,F_GETFL
,0) & ~O_NDELAY
);
56 len
+= fmt_ulong(line
+ len
,rp
);
57 len
+= fmt_str(line
+ len
," , ");
58 len
+= fmt_ulong(line
+ len
,lp
);
59 len
+= fmt_str(line
+ len
,"\r\n");
61 substdio_fdbuf(&ss
,mywrite
,s
,buf
,sizeof buf
);
62 if (substdio_putflush(&ss
,line
,len
) == -1) { close(s
); return 0; }
64 substdio_fdbuf(&ss
,myread
,s
,buf
,sizeof buf
);
68 if (substdio_get(&ss
,&ch
,1) != 1) { close(s
); return 0; }
69 if ((ch
== ' ') || (ch
== '\t') || (ch
== '\r')) continue;
70 if (ch
== '\n') break;
71 if (numcolons
< 3) { if (ch
== ':') ++numcolons
; }
72 else { *x
++ = ch
; if (x
== line
+ sizeof(line
) - 1) break; }