OSDN Git Service

I was library can be compiled 20140223
[zither/ds-zither.git] / libms / socket_connect46select.c
1 // socket_connect46select.c
2 // masashi shimakura 20140223
3
4 #include<sys/types.h>
5 #include<sys/socket.h>
6 #include<netinet/in_systm.h>
7 #include<netinet/in.h>
8 #include<netinet/ip.h>
9 #include<netdb.h>
10 #include<stdio.h>
11 #include<errno.h>
12 #include<unistd.h>
13 #include<fcntl.h>
14 #include<string.h>
15 #include<stdlib.h>
16 #include<arpa/inet.h>
17 #include <errno.h>
18
19 #include"libms.h"
20
21 // int socket_connect46(struct addrinfo * hints, char * ip, char * port, int * sk);
22
23
24 int socket_connect46select(struct addrinfo * hints, char * ip, char * port, int * sk, int interval)
25 {
26
27 struct addrinfo *res, *res0;
28 int ret;
29 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
30 int error;
31 ret = -1;
32
33 hints->ai_socktype = SOCK_STREAM;
34 // hints->ai_socktype = SOCK_DGRAM;   /* Datagram socket */
35 hints->ai_flags = AI_PASSIVE;         /* For wildcard IP address */
36 hints->ai_family = AF_UNSPEC;         /* Allow IPv4 or IPv6 */
37 hints->ai_protocol = 0;               /* Any protocol */
38 hints->ai_canonname = NULL;
39 hints->ai_addr = NULL;
40 hints->ai_next = NULL;
41
42 error = getaddrinfo(ip, port, hints, &res0);
43 if(error){
44    fprintf(stderr, "ERR: %s %s: %s\n", ip, port, gai_strerror(error));
45    exit(1);
46    }
47
48 for(res = res0; res; res = res->ai_next){
49    error = getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
50    if(error){
51       fprintf(stderr,"ERR: %s %s: %s\n", ip, port, gai_strerror(error));
52       continue;
53       }
54
55 //  fprintf(stderr,"trying %s port %s\n", hbuf, sbuf);
56
57    * sk = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
58    if(* sk < 0){
59       fprintf(stderr,"ERR: %d\n", * sk);
60       continue;
61       }
62
63 // FreeBSDにて問題があり、現在は検証中です、
64 // if((ret = connect(* sk, res->ai_addr, res->ai_addrlen)) < 0){
65 //   if((ret = select_connect(sk, res->ai_addr, res->ai_addrlen, interval)) < 0){
66   if((ret = select_connect(sk, (struct sockaddr_in *)res->ai_addr, interval)) < 0){
67       close(* sk);
68       * sk = -1;
69       continue;
70       }
71    else{
72       return ret;
73       }
74    }
75
76 fprintf(stderr,"ERR: test: no destination to connect to\n");
77 exit(1);
78
79 }
80
81
82
83