summaryrefslogtreecommitdiffstats
path: root/src/libbbsutil/net.c
blob: 60208a6544ef6473ca377aef4e69588b51f7de1c (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#include "libbbsutil.h"

unsigned int
ipstr2int(const char *ip)
{
    unsigned int i, val = 0;
    char buf[32];
    char *nil, *p;

    strlcpy(buf, ip, sizeof(buf));
    p = buf;
    for (i = 0; i < 4; i++) {
    nil = strchr(p, '.');
    if (nil != NULL)
        *nil = 0;
    val *= 256;
    val += atoi(p);
    if (nil != NULL)
        p = nil + 1;
    }
    return val;
}

int tobind(const char * host, int port)
{
    int     sockfd, val = 1;
    struct  sockaddr_in     servaddr;

    if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
    perror("socket()");
    exit(1);
    }
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
           (char *)&val, sizeof(val));
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    if (host == NULL || host[0] == 0)
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    else if (inet_aton(host, &servaddr.sin_addr) == 0) {
    perror("inet_aton()");
    exit(1);
    }
    servaddr.sin_port = htons(port);
    if( bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ) {
    perror("bind()");
    exit(1);
    }
    if( listen(sockfd, 5) < 0 ) {
    perror("listen()");
    exit(1);
    }

    return sockfd;
}

int toconnect(const char *host, int port)
{
    int    sock;
    struct sockaddr_in serv_name;
    if( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){
        perror("socket");
        return -1;
    }

    serv_name.sin_family = AF_INET;
    serv_name.sin_addr.s_addr = inet_addr(host);
    serv_name.sin_port = htons(port);
    if( connect(sock, (struct sockaddr*)&serv_name, sizeof(serv_name)) < 0 ){
        close(sock);
        return -1;
    }
    return sock;
}

/**
 * same as read(2), but read until exactly size len 
 */
int toread(int fd, void *buf, int len)
{
    int     l;
    for( l = 0 ; len > 0 ; )
    if( (l = read(fd, buf, len)) <= 0 )
        return -1;
    else{
        buf += l;
        len -= l;
    }
    return l;
}

/**
 * same as write(2), but write until exactly size len 
 */
int towrite(int fd, const void *buf, int len)
{
    int     l;
    for( l = 0 ; len > 0 ; )
    if( (l = write(fd, buf, len)) <= 0 )
        return -1;
    else{
        buf += l;
        len -= l;
    }
    return l;
}