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