File: remopen.c

package info (click to toggle)
inn2 2.5.2-2~squeeze1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,072 kB
  • ctags: 8,521
  • sloc: ansic: 91,418; sh: 13,249; perl: 12,311; makefile: 2,928; yacc: 868; python: 342; lex: 266
file content (83 lines) | stat: -rw-r--r-- 1,796 bytes parent folder | download | duplicates (3)
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
/*  $Id: remopen.c 8921 2010-01-22 23:33:08Z iulius $
**
**  Open a connection to a remote NNTP server.
*/

#include "config.h"
#include "clibrary.h"
#include "portable/socket.h"
#include <errno.h>

#include "inn/innconf.h"
#include "inn/libinn.h"
#include "inn/network.h"
#include "inn/nntp.h"


/*
**  Open a connection to an NNTP server and create stdio FILE's for talking
**  to it.  Return -1 on error.
*/
int
NNTPconnect(const char *host, int port, FILE **FromServerp, FILE **ToServerp,
            char *errbuff, size_t len)
{
    char mybuff[NNTP_MAXLEN_COMMAND + 2];
    char *buff;
    int fd, code, oerrno;
    FILE *F = NULL;

    if (errbuff)
        buff = errbuff;
    else {
        buff = mybuff;
        len = sizeof(mybuff);
    }
    *buff = '\0';

    fd = network_connect_host(host, port, NULL);
    if (fd < 0)
        return -1;

    /* Connected -- now make sure we can post.  If we can't, use EPERM as a
       reasonable error code. */
    F = fdopen(fd, "r");
    if (F == NULL)
        goto fail;
    if (fgets(buff, len, F) == NULL)
        goto fail;
    code = atoi(buff);
    if (code != NNTP_OK_BANNER_POST && code != NNTP_OK_BANNER_NOPOST) {
        errno = EPERM;
        goto fail;
    }
    *FromServerp = F;
    *ToServerp = fdopen(dup(fd), "w");
    if (*ToServerp == NULL)
        goto fail;
    return 0;

 fail:
    oerrno = errno;
    if (F != NULL)
        fclose(F);
    else
        close(fd);
    errno = oerrno;
    return -1;
}


int
NNTPremoteopen(int port, FILE **FromServerp, FILE **ToServerp, char *errbuff,
               size_t len)
{
    char		*p;

    if ((p = innconf->server) == NULL) {
	if (errbuff)
	    strlcpy(errbuff, "What server?", len);
	return -1;
    }
    return NNTPconnect(p, port, FromServerp, ToServerp, errbuff, len);
}