summaryrefslogtreecommitdiffstats
path: root/socket_bind.c
blob: b31f6732bab9ff5e1f689e3c42f3417a7d01b91e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "socket.h"
#include "uint16.h"

int socket_bind4(int s,char ip[4],u16 port)
{
	struct sockaddr_in sa;

	memset(&sa,0,sizeof sa);
	sa.sin_family = AF_INET;
	u16_pack_big((char *) &sa.sin_port,port);
	memcpy((char *) &sa.sin_addr,ip,4);

	return bind(s,(struct sockaddr *) &sa,sizeof sa);
}

int socket_bind4_reuse(int s,char ip[4],u16 port)
{
	int opt = 1;
	setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof opt);
	return socket_bind4(s,ip,port);
}

void socket_tryreservein(int s,int size)
{
	while (size >= 1024) {
		if (setsockopt(s,SOL_SOCKET,SO_RCVBUF,&size,sizeof size) == 0) return;
		size -= (size >> 5);
	}
}