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];
16 char *remoteinfo_get(ipr
,rp
,ipl
,lp
,timeout
)
17 struct ip_address
*ipr
;
19 struct ip_address
*ipl
;
25 struct sockaddr_in sin
;
32 s
= socket(AF_INET
,SOCK_STREAM
,0);
33 if (s
== -1) return 0;
35 byte_zero(&sin
,sizeof(sin
));
36 sin
.sin_family
= AF_INET
;
37 byte_copy(&sin
.sin_addr
,4,ipl
);
39 if (bind(s
,(struct sockaddr
*) &sin
,sizeof(sin
)) == -1) { close(s
); return 0; }
40 if (timeoutconn(s
,ipr
,113,timeout
) == -1) { close(s
); return 0; }
41 fcntl(s
,F_SETFL
,fcntl(s
,F_GETFL
,0) & ~O_NDELAY
);
44 len
+= fmt_ulong(line
+ len
,rp
);
45 len
+= fmt_str(line
+ len
," , ");
46 len
+= fmt_ulong(line
+ len
,lp
);
47 len
+= fmt_str(line
+ len
,"\r\n");
49 substdio_fdbuf(&ss
,timeoutwrite
,TIMEOUTWRITE(timeout
,s
),buf
,sizeof(buf
));
50 if (substdio_putflush(&ss
,line
,len
) == -1) { close(s
); return 0; }
52 substdio_fdbuf(&ss
,timeoutread
,TIMEOUTREAD(timeout
,s
),buf
,sizeof(buf
));
56 if (substdio_get(&ss
,&ch
,1) != 1) { close(s
); return 0; }
57 if ((ch
== ' ') || (ch
== '\t') || (ch
== '\r')) continue;
58 if (ch
== '\n') break;
59 if (numcolons
< 3) { if (ch
== ':') ++numcolons
; }
60 else { *x
++ = ch
; if (x
== line
+ sizeof(line
) - 1) break; }