File Conn.c changed (mode: 100644) (index be20c99..2932696) |
... |
... |
static struct Conn *Conn_alloc(void) |
389 |
389 |
return C; |
return C; |
390 |
390 |
} |
} |
391 |
391 |
|
|
392 |
|
struct Conn *Conn_socket(const int domain, const int type, const int port) |
|
|
392 |
|
struct Conn *Conn_socket_addr(const int domain, const int type, |
|
393 |
|
const char *addr, const int port) |
393 |
394 |
{ |
{ |
394 |
395 |
struct Conn *C; |
struct Conn *C; |
395 |
396 |
int i, ret; |
int i, ret; |
|
... |
... |
struct Conn *Conn_socket(const int domain, const int type, const int port) |
397 |
398 |
struct sockaddr_in sa; |
struct sockaddr_in sa; |
398 |
399 |
struct sockaddr_in6 sa6; |
struct sockaddr_in6 sa6; |
399 |
400 |
int sock_len = 0; |
int sock_len = 0; |
400 |
|
char *addr = "?"; |
|
401 |
401 |
int do_bind = 1, do_listen = 1; |
int do_bind = 1, do_listen = 1; |
402 |
402 |
int protocol = 0; |
int protocol = 0; |
403 |
403 |
int first_state; |
int first_state; |
|
... |
... |
struct Conn *Conn_socket(const int domain, const int type, const int port) |
406 |
406 |
case PF_INET: |
case PF_INET: |
407 |
407 |
memset(&sa, 0, sizeof(sa)); |
memset(&sa, 0, sizeof(sa)); |
408 |
408 |
sa.sin_family = AF_INET; |
sa.sin_family = AF_INET; |
409 |
|
sa.sin_addr.s_addr = htonl(INADDR_ANY); |
|
|
409 |
|
ret = inet_pton(AF_INET, addr, &sa.sin_addr); |
|
410 |
|
if (ret < 0) { |
|
411 |
|
snprintf(Conn_error, sizeof(Conn_error), |
|
412 |
|
"inet_pton(%s) failed", addr); |
|
413 |
|
return NULL; |
|
414 |
|
} |
410 |
415 |
sa.sin_port = htons(port); |
sa.sin_port = htons(port); |
411 |
416 |
psa = (struct sockaddr *) &sa; |
psa = (struct sockaddr *) &sa; |
412 |
417 |
sock_len = sizeof(sa); |
sock_len = sizeof(sa); |
413 |
|
addr = "0.0.0.0"; |
|
414 |
418 |
if (type == SOCK_STREAM) { |
if (type == SOCK_STREAM) { |
415 |
419 |
first_state = CONN_STATE_LISTEN; |
first_state = CONN_STATE_LISTEN; |
416 |
420 |
} else if (type == SOCK_DGRAM) { |
} else if (type == SOCK_DGRAM) { |
|
... |
... |
struct Conn *Conn_socket(const int domain, const int type, const int port) |
422 |
426 |
case PF_INET6: |
case PF_INET6: |
423 |
427 |
memset(&sa6, 0, sizeof(sa6)); |
memset(&sa6, 0, sizeof(sa6)); |
424 |
428 |
sa6.sin6_family = AF_INET6; |
sa6.sin6_family = AF_INET6; |
425 |
|
ret = inet_pton(AF_INET6, "::", &sa6.sin6_addr); |
|
|
429 |
|
ret = inet_pton(AF_INET6, addr, &sa6.sin6_addr); |
426 |
430 |
if (ret < 0) { |
if (ret < 0) { |
427 |
431 |
snprintf(Conn_error, sizeof(Conn_error), |
snprintf(Conn_error, sizeof(Conn_error), |
428 |
|
"inet_pton(::) failed"); |
|
|
432 |
|
"inet_pton(%s) failed", addr); |
429 |
433 |
return NULL; |
return NULL; |
430 |
434 |
} |
} |
431 |
435 |
sa6.sin6_port = htons(port); |
sa6.sin6_port = htons(port); |
432 |
436 |
psa = (struct sockaddr *) &sa6; |
psa = (struct sockaddr *) &sa6; |
433 |
437 |
sock_len = sizeof(sa6); |
sock_len = sizeof(sa6); |
434 |
|
addr = "::"; |
|
435 |
438 |
if (type == SOCK_STREAM) { |
if (type == SOCK_STREAM) { |
436 |
439 |
first_state = CONN_STATE_LISTEN; |
first_state = CONN_STATE_LISTEN; |
437 |
440 |
} else if (type == SOCK_DGRAM) { |
} else if (type == SOCK_DGRAM) { |
|
... |
... |
struct Conn *Conn_socket(const int domain, const int type, const int port) |
530 |
533 |
return NULL; |
return NULL; |
531 |
534 |
} |
} |
532 |
535 |
|
|
|
536 |
|
struct Conn *Conn_socket(const int domain, const int type, const int port) |
|
537 |
|
{ |
|
538 |
|
char *addr; |
|
539 |
|
|
|
540 |
|
switch (domain) { |
|
541 |
|
case PF_INET: addr = "0.0.0.0"; break; |
|
542 |
|
case PF_INET6: addr = "::"; break; |
|
543 |
|
default: addr = NULL; |
|
544 |
|
} |
|
545 |
|
|
|
546 |
|
return Conn_socket_addr(domain, type, addr, port); |
|
547 |
|
} |
|
548 |
|
|
533 |
549 |
struct Conn *Conn_connect(const int domain, const int type, const char *addr, |
struct Conn *Conn_connect(const int domain, const int type, const char *addr, |
534 |
550 |
const int port) |
const int port) |
535 |
551 |
{ |
{ |
|
... |
... |
int Conn_poll(const int timeout) |
1135 |
1151 |
if (ret < 0) |
if (ret < 0) |
1136 |
1152 |
return -1; |
return -1; |
1137 |
1153 |
|
|
1138 |
|
Log(9, "Compacting cells, do expiration and band stuff...\n"); |
|
|
1154 |
|
Log(9, "Do compacting, expiration and band stuff...\n"); |
1139 |
1155 |
i = 0; |
i = 0; |
1140 |
1156 |
while (i < Conn_no) { |
while (i < Conn_no) { |
1141 |
1157 |
C = &Conns[i]; |
C = &Conns[i]; |
File Conn.h changed (mode: 100644) (index 677f5d8..77f3d32) |
... |
... |
extern int Conn_init(const unsigned int max); |
7 |
7 |
extern int Conn_shutdown(void); |
extern int Conn_shutdown(void); |
8 |
8 |
extern ssize_t Conn_send(struct Conn *C, void *buf, const size_t count); |
extern ssize_t Conn_send(struct Conn *C, void *buf, const size_t count); |
9 |
9 |
extern ssize_t Conn_recv(struct Conn *C, void *buf, const size_t count); |
extern ssize_t Conn_recv(struct Conn *C, void *buf, const size_t count); |
|
10 |
|
extern struct Conn *Conn_socket_addr(const int domain, const int type, |
|
11 |
|
const char *addr, const int port); |
10 |
12 |
extern struct Conn *Conn_socket(const int domain, const int type, |
extern struct Conn *Conn_socket(const int domain, const int type, |
11 |
13 |
const int port); |
const int port); |
12 |
14 |
extern int Conn_poll(const int timeout); |
extern int Conn_poll(const int timeout); |
File examples/s_bind.c copied from file examples/s.c (similarity 92%) (mode: 100644) (index ac15060..f3fede1) |
|
1 |
|
/* |
|
2 |
|
* Copyright: Catalin(ux) M. BOIE |
|
3 |
|
* Shows how to bind to a specific address. |
|
4 |
|
*/ |
|
5 |
|
|
1 |
6 |
#define _GNU_SOURCE |
#define _GNU_SOURCE |
2 |
7 |
|
|
3 |
8 |
#include <errno.h> |
#include <errno.h> |
|
23 |
28 |
static unsigned short debug = 20; |
static unsigned short debug = 20; |
24 |
29 |
|
|
25 |
30 |
static FILE *Logf = NULL; |
static FILE *Logf = NULL; |
26 |
|
static char *log_file = "s.log"; |
|
|
31 |
|
static char *log_file = "s_bind.log"; |
27 |
32 |
static int port = 9000; |
static int port = 9000; |
28 |
33 |
static short ipv4 = 1, ipv6 = 1; |
static short ipv4 = 1, ipv6 = 1; |
29 |
34 |
static int maxconn = 0; |
static int maxconn = 0; |
|
... |
... |
int main(void) |
118 |
123 |
|
|
119 |
124 |
if (ipv4 == 1) { |
if (ipv4 == 1) { |
120 |
125 |
Log(9, "Try to register IPv4 socket...\n"); |
Log(9, "Try to register IPv4 socket...\n"); |
121 |
|
I4 = Conn_socket(PF_INET, SOCK_STREAM, port); |
|
|
126 |
|
I4 = Conn_socket_addr(PF_INET, SOCK_STREAM, "127.0.0.1", port); |
122 |
127 |
if (!I4) |
if (!I4) |
123 |
128 |
Log(1, "Cannot bind on ipv4 socket [%s].\n", Conn_strerror()); |
Log(1, "Cannot bind on ipv4 socket [%s].\n", Conn_strerror()); |
124 |
129 |
} |
} |
125 |
130 |
|
|
126 |
131 |
if (ipv6 == 1) { |
if (ipv6 == 1) { |
127 |
132 |
Log(9, "Try to register IPv6 socket...\n"); |
Log(9, "Try to register IPv6 socket...\n"); |
128 |
|
I6 = Conn_socket(PF_INET6, SOCK_STREAM, port); |
|
|
133 |
|
I6 = Conn_socket_addr(PF_INET6, SOCK_STREAM, "::1", port); |
129 |
134 |
if (!I6) |
if (!I6) |
130 |
135 |
Log(1, "Cannot bind on ipv6 socket [%s].\n", Conn_strerror()); |
Log(1, "Cannot bind on ipv6 socket [%s].\n", Conn_strerror()); |
131 |
136 |
} |
} |