119 lines
2.5 KiB
C
119 lines
2.5 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <unistd.h>
|
||
|
#include <string.h>
|
||
|
#include <netinet/ip.h>
|
||
|
#include <arpa/inet.h>
|
||
|
#include <fcntl.h>
|
||
|
#include <errno.h>
|
||
|
|
||
|
#include "logging.h"
|
||
|
#include "sockaddr.h"
|
||
|
|
||
|
int tcp_listen(struct sockaddr_in *sa)
|
||
|
{
|
||
|
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||
|
if (sock < 0 ) {
|
||
|
log_print(LOG_ERROR, "tcp_listen_socket(): socket()");
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
int i = 1;
|
||
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i))) {
|
||
|
log_print(LOG_ERROR, "tcp_listen_socket(): setsockopt(SO_REUSEADDR)");
|
||
|
close(sock);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if (bind(sock, (struct sockaddr *)sa, sizeof(*sa))) {
|
||
|
log_print(LOG_ERROR, "tcp_listen_socket(): bind(%s)", get_sockaddr_buf(sa));
|
||
|
close(sock);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if (listen(sock, 8)) {
|
||
|
log_print(LOG_ERROR, "tcp_listen_socket(): listen()");
|
||
|
close(sock);
|
||
|
return -1;
|
||
|
}
|
||
|
return sock;
|
||
|
}
|
||
|
|
||
|
int tcp_connect(struct sockaddr_in *sa)
|
||
|
{
|
||
|
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||
|
if (sock < 0 ) {
|
||
|
log_print(LOG_ERROR, "tcp_connect_socket(): socket()");
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
int ret = connect(sock, (struct sockaddr *)sa, sizeof(*sa));
|
||
|
if (ret != 0) {
|
||
|
log_print(LOG_ERROR, "tcp_connect(): connect(%s)", get_sockaddr_buf(sa));
|
||
|
close(sock);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
return sock;
|
||
|
}
|
||
|
|
||
|
int tcp_connect_nonblock(struct sockaddr_in *sa)
|
||
|
{
|
||
|
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||
|
if (sock < 0 ) {
|
||
|
log_print(LOG_ERROR, "tcp_connect_nonblock(): socket()");
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
int flags = fcntl(sock, F_GETFL, 0);
|
||
|
if (flags < 0) {
|
||
|
log_print(LOG_ERROR, "tcp_connect_nonblock(): fcntl(F_GETFL)");
|
||
|
close(sock);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
/* non-blocking connect() */
|
||
|
if (fcntl(sock, F_SETFL, flags | O_NONBLOCK)) {
|
||
|
log_print(LOG_ERROR, "tcp_connect_nonblock(): fcntl(F_SETFL)");
|
||
|
close(sock);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
int ret = connect(sock, (struct sockaddr *)sa, sizeof(*sa));
|
||
|
if (ret && errno != EINPROGRESS) {
|
||
|
log_print(LOG_ERROR, "tcp_connect_nonblock(): connect(%s)", get_sockaddr_buf(sa));
|
||
|
close(sock);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
/* reset EINPROGRESS */
|
||
|
errno = 0;
|
||
|
|
||
|
/* all further actions are blocking */
|
||
|
if (fcntl(sock, F_SETFL, flags)) {
|
||
|
log_print(LOG_ERROR, "tcp_connect_nonblock(): fcntl(F_SETFL)");
|
||
|
close(sock);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
return sock;
|
||
|
}
|
||
|
|
||
|
int tcp_connect_error(int fd)
|
||
|
{
|
||
|
int err;
|
||
|
unsigned int err_size = sizeof(err);
|
||
|
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &err_size)) {
|
||
|
log_print(LOG_ERROR, "tcp_connect_error(): getsockopt(SO_ERROR)");
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if (err) {
|
||
|
errno = err;
|
||
|
log_print(LOG_ERROR, "tcp_connect_error()");
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|