Removed the SA define.
Made the sock_mutex static since it's not referenced outside the file. Changed the data types for some of the variables. Cleaned up the readline function (changed data types and removed variables.)
This commit is contained in:
parent
981adafb5d
commit
42b09e5441
49
src/sock.c
49
src/sock.c
@ -1,4 +1,4 @@
|
|||||||
/* $Id: sock.c,v 1.4 2001-05-23 18:01:23 rjkaes Exp $
|
/* $Id: sock.c,v 1.5 2001-05-27 02:31:20 rjkaes Exp $
|
||||||
*
|
*
|
||||||
* Sockets are created and destroyed here. When a new connection comes in from
|
* Sockets are created and destroyed here. When a new connection comes in from
|
||||||
* a client, we need to copy the socket and the create a second socket to the
|
* a client, we need to copy the socket and the create a second socket to the
|
||||||
@ -28,15 +28,13 @@
|
|||||||
#include "sock.h"
|
#include "sock.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
#define SA struct sockaddr
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The mutex is used for locking around the calls to the dnscache since I
|
* The mutex is used for locking around the calls to the dnscache since I
|
||||||
* don't want multiple threads accessing the linked list at the same time.
|
* don't want multiple threads accessing the linked list at the same time.
|
||||||
* This should be more fine grained, but it will do for now.
|
* This should be more fine grained, but it will do for now.
|
||||||
* - rjkaes
|
* - rjkaes
|
||||||
*/
|
*/
|
||||||
pthread_mutex_t sock_mutex = PTHREAD_MUTEX_INITIALIZER;
|
static pthread_mutex_t sock_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
#define SOCK_LOCK() pthread_mutex_lock(&sock_mutex);
|
#define SOCK_LOCK() pthread_mutex_lock(&sock_mutex);
|
||||||
#define SOCK_UNLOCK() pthread_mutex_unlock(&sock_mutex);
|
#define SOCK_UNLOCK() pthread_mutex_unlock(&sock_mutex);
|
||||||
@ -53,7 +51,7 @@ pthread_mutex_t sock_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|||||||
* dotted-decimal form before it does a name lookup.
|
* dotted-decimal form before it does a name lookup.
|
||||||
* - rjkaes
|
* - rjkaes
|
||||||
*/
|
*/
|
||||||
int opensock(char *ip_addr, int port)
|
int opensock(char *ip_addr, uint16_t port)
|
||||||
{
|
{
|
||||||
int sock_fd;
|
int sock_fd;
|
||||||
struct sockaddr_in port_info;
|
struct sockaddr_in port_info;
|
||||||
@ -62,7 +60,7 @@ int opensock(char *ip_addr, int port)
|
|||||||
assert(ip_addr != NULL);
|
assert(ip_addr != NULL);
|
||||||
assert(port > 0);
|
assert(port > 0);
|
||||||
|
|
||||||
memset((SA *) &port_info, 0, sizeof(port_info));
|
memset((struct sockaddr*)&port_info, 0, sizeof(port_info));
|
||||||
|
|
||||||
port_info.sin_family = AF_INET;
|
port_info.sin_family = AF_INET;
|
||||||
|
|
||||||
@ -75,19 +73,19 @@ int opensock(char *ip_addr, int port)
|
|||||||
SOCK_UNLOCK();
|
SOCK_UNLOCK();
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
log(LOG_ERR, "opensock: Could not lookup address: %s", ip_addr);
|
log_message(LOG_ERR, "opensock: Could not lookup address: %s", ip_addr);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
port_info.sin_port = htons(port);
|
port_info.sin_port = htons(port);
|
||||||
|
|
||||||
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
|
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
|
||||||
log(LOG_ERR, "opensock: socket (%s)", strerror(errno));
|
log_message(LOG_ERR, "opensock: socket (%s)", strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (connect(sock_fd, (SA *) &port_info, sizeof(port_info)) < 0) {
|
if (connect(sock_fd, (struct sockaddr*)&port_info, sizeof(port_info)) < 0) {
|
||||||
log(LOG_ERR, "connecting socket");
|
log_message(LOG_ERR, "connecting socket");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,7 +124,7 @@ int socket_blocking(int sock)
|
|||||||
* the pointer, while the socket is returned as a default return.
|
* the pointer, while the socket is returned as a default return.
|
||||||
* - rjkaes
|
* - rjkaes
|
||||||
*/
|
*/
|
||||||
int listen_sock(unsigned int port, socklen_t *addrlen)
|
int listen_sock(uint16_t port, socklen_t *addrlen)
|
||||||
{
|
{
|
||||||
int listenfd;
|
int listenfd;
|
||||||
const int on = 1;
|
const int on = 1;
|
||||||
@ -164,13 +162,13 @@ int listen_sock(unsigned int port, socklen_t *addrlen)
|
|||||||
char *getpeer_ip(int fd, char *ipaddr)
|
char *getpeer_ip(int fd, char *ipaddr)
|
||||||
{
|
{
|
||||||
struct sockaddr_in name;
|
struct sockaddr_in name;
|
||||||
int namelen = sizeof(name);
|
size_t namelen = sizeof(name);
|
||||||
|
|
||||||
assert(fd >= 0);
|
assert(fd >= 0);
|
||||||
assert(ipaddr != NULL);
|
assert(ipaddr != NULL);
|
||||||
|
|
||||||
if (getpeername(fd, (struct sockaddr*)&name, &namelen) != 0) {
|
if (getpeername(fd, (struct sockaddr*)&name, &namelen) != 0) {
|
||||||
log(LOG_ERR, "Connect: 'could not get peer name'");
|
log_message(LOG_ERR, "Connect: 'could not get peer name'");
|
||||||
} else {
|
} else {
|
||||||
strlcpy(ipaddr,
|
strlcpy(ipaddr,
|
||||||
inet_ntoa(*(struct in_addr*)&name.sin_addr.s_addr),
|
inet_ntoa(*(struct in_addr*)&name.sin_addr.s_addr),
|
||||||
@ -187,14 +185,14 @@ char *getpeer_ip(int fd, char *ipaddr)
|
|||||||
char *getpeer_string(int fd, char *string)
|
char *getpeer_string(int fd, char *string)
|
||||||
{
|
{
|
||||||
struct sockaddr_in name;
|
struct sockaddr_in name;
|
||||||
int namelen = sizeof(name);
|
size_t namelen = sizeof(name);
|
||||||
struct hostent *peername;
|
struct hostent *peername;
|
||||||
|
|
||||||
assert(fd >= 0);
|
assert(fd >= 0);
|
||||||
assert(string != NULL);
|
assert(string != NULL);
|
||||||
|
|
||||||
if (getpeername(fd, (struct sockaddr *)&name, &namelen) != 0) {
|
if (getpeername(fd, (struct sockaddr *)&name, &namelen) != 0) {
|
||||||
log(LOG_ERR, "Connect: 'could not get peer name'");
|
log_message(LOG_ERR, "Connect: 'could not get peer name'");
|
||||||
} else {
|
} else {
|
||||||
SOCK_LOCK();
|
SOCK_LOCK();
|
||||||
peername = gethostbyaddr((char *)&name.sin_addr.s_addr,
|
peername = gethostbyaddr((char *)&name.sin_addr.s_addr,
|
||||||
@ -209,15 +207,21 @@ char *getpeer_string(int fd, char *string)
|
|||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t readline(int fd, void *vptr, size_t maxlen)
|
/*
|
||||||
|
* Reads in a line of text one character at a time. Finishes when either a
|
||||||
|
* newline is detected, or maxlen characters have been read. The function
|
||||||
|
* will actually copy one less than maxlen into the buffer. In other words,
|
||||||
|
* the returned string will _always_ be '\0' terminated.
|
||||||
|
*/
|
||||||
|
ssize_t readline(int fd, char *ptr, size_t maxlen)
|
||||||
{
|
{
|
||||||
ssize_t n, rc;
|
size_t n;
|
||||||
char c, *ptr;
|
ssize_t rc;
|
||||||
|
char c;
|
||||||
|
|
||||||
assert(fd >= 0);
|
assert(fd >= 0);
|
||||||
assert(vptr != NULL);
|
assert(ptr != NULL);
|
||||||
|
|
||||||
ptr = vptr;
|
|
||||||
for (n = 1; n < maxlen; n++) {
|
for (n = 1; n < maxlen; n++) {
|
||||||
again:
|
again:
|
||||||
if ((rc = read(fd, &c, 1)) == 1) {
|
if ((rc = read(fd, &c, 1)) == 1) {
|
||||||
@ -236,6 +240,7 @@ ssize_t readline(int fd, void *vptr, size_t maxlen)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*ptr = 0;
|
/* Tack a NIL to the end to make is a standard "C" string */
|
||||||
return n;
|
*ptr = '\0';
|
||||||
|
return (ssize_t)n;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
/* $Id: sock.h,v 1.2 2000-09-11 23:56:32 rjkaes Exp $
|
/* $Id: sock.h,v 1.3 2001-05-27 02:31:20 rjkaes Exp $
|
||||||
*
|
*
|
||||||
* See 'sock.c' for a detailed description.
|
* See 'sock.c' for a detailed description.
|
||||||
*
|
*
|
||||||
@ -24,8 +24,8 @@
|
|||||||
|
|
||||||
#define MAXLINE (1024 * 4)
|
#define MAXLINE (1024 * 4)
|
||||||
|
|
||||||
extern int opensock(char *ip_addr, int port);
|
extern int opensock(char *ip_addr, uint16_t port);
|
||||||
extern int listen_sock(unsigned int port, socklen_t *addrlen);
|
extern int listen_sock(uint16_t port, socklen_t *addrlen);
|
||||||
|
|
||||||
extern int socket_nonblocking(int sock);
|
extern int socket_nonblocking(int sock);
|
||||||
extern int socket_blocking(int sock);
|
extern int socket_blocking(int sock);
|
||||||
@ -33,6 +33,6 @@ extern int socket_blocking(int sock);
|
|||||||
extern char *getpeer_ip(int fd, char *ipaddr);
|
extern char *getpeer_ip(int fd, char *ipaddr);
|
||||||
extern char *getpeer_string(int fd, char *string);
|
extern char *getpeer_string(int fd, char *string);
|
||||||
|
|
||||||
extern ssize_t readline(int fd, void *vptr, size_t maxlen);
|
extern ssize_t readline(int fd, char *ptr, size_t maxlen);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user