-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.c
More file actions
67 lines (57 loc) · 1.02 KB
/
system.c
File metadata and controls
67 lines (57 loc) · 1.02 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <assert.h>
#include "system.h"
int
create_tcp_socket()
{
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
printf("socket() : %s\n", strerror(errno));
return -1;
}
return sock;
}
void
socket_set_block(int sock, int block)
{
block = !block;
ioctl(sock, FIONBIO, &block);
}
void
socket_set_linger(int sock)
{
/* make this socket reusable */
struct linger li;
li.l_onoff = 1;
li.l_linger = 0;
setsockopt(sock, SOL_SOCKET, SO_LINGER,
&li, sizeof(struct linger));
}
void netaddr_init_ipv4(struct netaddr *addr, const char *ip, int port)
{
addr->type = ADDR_IPV4;
*((uint32_t*)addr->addr_data) = inet_addr(ip);
addr->port = port;
}
int min(int x, int y)
{
if (x > y)
return x;
return y;
}
int max(int x, int y)
{
if (x > y)
return x;
return y;
}