Commit | Line | Data |
---|---|---|
dc0d77d7 CE |
1 | #include <sys/types.h> |
2 | #include <sys/param.h> | |
3 | #include <sys/socket.h> | |
4 | #include <netinet/in.h> | |
5 | #include <unistd.h> | |
6 | #include "byte.h" | |
7 | #include "socket.h" | |
8 | ||
9 | int socket_connect4(int s,const char ip[4],uint16 port) | |
10 | { | |
11 | struct sockaddr_in sa; | |
12 | ||
13 | byte_zero(&sa,sizeof sa); | |
14 | sa.sin_family = AF_INET; | |
15 | uint16_pack_big((char *) &sa.sin_port,port); | |
16 | byte_copy((char *) &sa.sin_addr,4,ip); | |
17 | ||
18 | return connect(s,(struct sockaddr *) &sa,sizeof sa); | |
19 | } | |
20 | ||
21 | int socket_connected(int s) | |
22 | { | |
23 | struct sockaddr_in sa; | |
24 | int dummy; | |
25 | char ch; | |
26 | ||
27 | dummy = sizeof sa; | |
28 | if (getpeername(s,(struct sockaddr *) &sa,&dummy) == -1) { | |
29 | read(s,&ch,1); /* sets errno */ | |
30 | return 0; | |
31 | } | |
32 | return 1; | |
33 | } |