-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomms_remote.c
141 lines (111 loc) · 2.7 KB
/
comms_remote.c
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* $Id: comms_remote.c 103 2004-11-06 16:33:16Z bmoore $
*
* Copyright (c) 2004 Branden J. Moore.
*
* This file is part of MacBiff, and is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* MacBiff is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with MacBiff; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
*/
/*
* Communications Wrapper for Remote Connections
*/
#include <errno.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
#if 0
#define EBUG 1
#endif
#include "debug.h"
#include "comms_remote.h"
static int sock;
static struct sockaddr_in sa;
extern int comms_conn;
int comms_remote_setup( const char *server, int port )
{
struct hostent *hp;
dprintf("%s: Talking to %s\n", __FUNCTION__, server);
if ( !server || !server[0] ) {
alert("No server defined\n");
return EINVAL;
}
if ( !( hp = gethostbyname( server ) ) ) {
herror("gethostbyname");
return EINVAL;
}
sa.sin_family = AF_INET;
sa.sin_port = htons(port ? port : 143); /* IMAP Port */
memcpy(&sa.sin_addr, hp->h_addr_list[0], hp->h_length);
comms_conn = 0;
return 0;
}
int comms_remote_connect( void )
{
if ( comms_conn || sock != -1 ) {
alert("Connecting, but already comms_conn.\n");
comms_remote_close();
}
sock = socket( AF_INET, SOCK_STREAM, 0 );
if ( sock < 0 ) {
return errno;
}
if ( connect(sock, (struct sockaddr*)&sa, sizeof(sa) ) < 0 ) {
if ( close( sock ) ) {
perror("close");
}
sock = -1;
return errno;
}
comms_conn = 1;
return 0;
}
size_t comms_remote_read( void *buf, size_t count )
{
dprintf( "In %s\n", __FUNCTION__ );
if ( !comms_conn ) {
error("System not comms_conn\n");
}
return read(sock, buf, count);
}
size_t comms_remote_write( void *buf, size_t count )
{
if ( !comms_conn ) {
error("System not comms_conn\n");
}
return write(sock, buf, count);
}
int comms_remote_close( void )
{
if ( !comms_conn ) {
return 0;
}
close(sock); sock = -1;
comms_conn = 0;
return 0;
}
void comms_remote_destroy( void )
{
if ( comms_conn ) {
comms_remote_close();
}
dprintf("%s:\n", __FUNCTION__);
}