]>
Commit | Line | Data |
---|---|---|
e5012450 | 1 | /* |
2 | * Misc network functions | |
3 | * | |
4 | */ | |
5 | ||
6 | #include <faim/aim.h> | |
7 | ||
8 | int Read(int fd, unsigned char *buf, int len) | |
9 | { | |
10 | int i = 0; | |
11 | int j = 0; | |
12 | int err_count=0; | |
13 | ||
14 | while ((i < len) && (!(i < 0))) | |
15 | { | |
16 | j = read(fd, &(buf[i]), len-i); | |
17 | if ( (j < 0) && (errno != EAGAIN)) | |
18 | return -errno; /* fail */ | |
19 | else if (j==0) | |
20 | { | |
21 | err_count++; | |
22 | if (err_count> 100) { | |
23 | /* | |
24 | * Reached maximum number of allowed read errors. | |
25 | * | |
26 | * Lets suppose the connection is lost and errno didn't | |
27 | * know it. | |
28 | * | |
29 | */ | |
30 | return (-1); | |
31 | } | |
32 | } | |
33 | else | |
34 | i += j; /* success, continue */ | |
35 | } | |
36 | return i; | |
37 | } | |
38 | ||
39 | /* | |
40 | * Create a listener socket. | |
41 | * | |
42 | * We go out of our way to make this interesting. (The Easy Way is not | |
43 | * thread-safe.) | |
44 | * | |
45 | */ | |
46 | int establish(u_short portnum) | |
47 | { | |
48 | int listenfd; | |
49 | const int on = 1; | |
50 | struct addrinfo hints, *res, *ressave; | |
51 | char serv[5]; | |
52 | ||
53 | sprintf(serv, "%d", portnum); | |
54 | ||
55 | memset(&hints, 0, sizeof(struct addrinfo)); | |
56 | hints.ai_flags = AI_PASSIVE; | |
57 | hints.ai_family = AF_UNSPEC; | |
58 | hints.ai_socktype = SOCK_STREAM; | |
59 | if (getaddrinfo(NULL/*any IP*/, serv, &hints, &res) != 0) { | |
60 | perror("getaddrinfo"); | |
61 | return -1; | |
62 | } | |
63 | ||
64 | ressave = res; | |
65 | do { | |
66 | listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); | |
67 | if (listenfd < 0) | |
68 | continue; | |
69 | setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); | |
70 | if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0) | |
71 | break; /* sucess */ | |
72 | close(listenfd); | |
73 | } while ( (res = res->ai_next) ); | |
74 | ||
75 | if (!res) | |
76 | return -1; | |
77 | ||
78 | if (listen(listenfd, 1024)!=0) { | |
79 | perror("listen"); | |
80 | return -1; | |
81 | } | |
82 | ||
83 | freeaddrinfo(ressave); | |
84 | ||
85 | return listenfd; | |
86 | } | |
87 | ||
88 | int get_connection(int s) | |
89 | { | |
90 | struct sockaddr isa; | |
91 | int addrlen = 0; | |
92 | int t; /* socket of connection */ | |
93 | ||
94 | memset(&isa, 0, sizeof(struct sockaddr)); | |
95 | if ((t = accept(s,&isa,&addrlen)) < 0) { | |
96 | perror("accept"); | |
97 | return -1; | |
98 | } | |
99 | ||
100 | return t; | |
101 | } | |
102 | ||
103 | int openconn(char *hostname, int port) | |
104 | { | |
105 | struct sockaddr_in sa; | |
106 | struct hostent *hp; | |
107 | int ret; | |
108 | ||
109 | if (!(hp = gethostbyname(hostname))) { | |
110 | perror("gethostbyname"); | |
111 | return -1; | |
112 | } | |
113 | ||
114 | memset(&sa.sin_zero, 0, 8); | |
115 | sa.sin_port = htons(port); | |
116 | memcpy(&sa.sin_addr, hp->h_addr, hp->h_length); | |
117 | sa.sin_family = hp->h_addrtype; | |
118 | ||
119 | ret = socket(hp->h_addrtype, SOCK_STREAM, 0); | |
120 | if (connect(ret, (struct sockaddr *)&sa, sizeof(struct sockaddr_in))<0) { | |
121 | perror("connect"); | |
122 | return -1; | |
123 | } | |
124 | ||
125 | return ret; | |
126 | } |