telnetproxy/network.c
2006-12-03 20:03:16 +01:00

95 lines
2.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include "logging.h"
int parse_saddr(const char *addr, struct sockaddr_in *sa)
{
char *addr_cpy = strdup(addr);
if (addr_cpy == NULL) {
log_print(LOG_WARN, "parse_saddr(): out of memory");
return -1;
}
char *tmp;
char *ip = strtok_r(addr_cpy, ":", &tmp);
if (ip == NULL) {
free(addr_cpy);
return -1;
}
char *port = strtok_r(NULL, ":", &tmp);
if (port == NULL) {
free(addr_cpy);
return -1;
}
sa->sin_family = AF_INET;
sa->sin_port = htons(atoi(port));
int ret = inet_aton(ip, &sa->sin_addr);
free(addr_cpy);
return (ret != 0) ? 0 : -1;
}
int get_sockaddr(struct sockaddr_in *sa, char *buf, int size)
{
return snprintf(buf, size, "%s:%d", inet_ntoa(sa->sin_addr), ntohs(sa->sin_port));
}
char * get_sockaddr_buf(struct sockaddr_in *sa)
{
static char buf[24];
get_sockaddr(sa, buf, sizeof(buf));
return buf;
}
int tcp_listen_socket(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_socket(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;
}
if (connect(sock, (struct sockaddr *)sa, sizeof(*sa))) {
log_print(LOG_ERROR, "tcp_connect_socket(): bind(%s)", get_sockaddr_buf(sa));
close(sock);
return -1;
}
return sock;
}