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 "byte.h" | |
6 | #include "socket.h" | |
7 | ||
8 | int socket_bind4(int s,char ip[4],uint16 port) | |
9 | { | |
10 | struct sockaddr_in sa; | |
11 | ||
12 | byte_zero(&sa,sizeof sa); | |
13 | sa.sin_family = AF_INET; | |
14 | uint16_pack_big((char *) &sa.sin_port,port); | |
15 | byte_copy((char *) &sa.sin_addr,4,ip); | |
16 | ||
17 | return bind(s,(struct sockaddr *) &sa,sizeof sa); | |
18 | } | |
19 | ||
20 | int socket_bind4_reuse(int s,char ip[4],uint16 port) | |
21 | { | |
22 | int opt = 1; | |
23 | setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof opt); | |
24 | return socket_bind4(s,ip,port); | |
25 | } | |
26 | ||
27 | void socket_tryreservein(int s,int size) | |
28 | { | |
29 | while (size >= 1024) { | |
30 | if (setsockopt(s,SOL_SOCKET,SO_RCVBUF,&size,sizeof size) == 0) return; | |
31 | size -= (size >> 5); | |
32 | } | |
33 | } |