]> code.citadel.org Git - citadel.git/blob - webcit/tcp_sockets.c
* tcp_sockets.c: removed some dead code
[citadel.git] / webcit / tcp_sockets.c
1 /*
2  * tcp_sockets.c
3  * 
4  * TCP socket module for WebCit
5  *
6  * $Id$
7  */
8
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <netdb.h>
18 #include <string.h>
19 #include <stdarg.h>
20 #include "webcit.h"
21
22 char server_is_local = 0;
23
24 #ifndef INADDR_NONE
25 #define INADDR_NONE 0xffffffff
26 #endif
27
28 extern int errno;
29
30 int serv_sock;
31
32
33 void timeout(int signum) {
34         printf("\rConnection timed out.\n");
35         exit(3);
36         }
37
38 int connectsock(char *host, char *service, char *protocol)
39 {
40         struct hostent *phe;
41         struct servent *pse;
42         struct protoent *ppe;
43         struct sockaddr_in sin;
44         int s,type;
45
46         bzero((char *)&sin,sizeof(sin));
47         sin.sin_family = AF_INET;
48
49         pse=getservbyname(service,protocol);
50         if (pse) {
51                 sin.sin_port = pse->s_port;
52                 }
53         else if ((sin.sin_port = htons((u_short)atoi(service))) == 0) {
54                 fprintf(stderr,"can't get %s service entry\n", service);
55                 return(-1);
56                 }
57         
58         phe=gethostbyname(host);
59         if (phe) {
60                 bcopy(phe->h_addr,(char *)&sin.sin_addr,phe->h_length);
61                 }
62         else if ((sin.sin_addr.s_addr = inet_addr(host))==INADDR_NONE) {
63                 fprintf(stderr,"can't get %s host entry: %s\n",
64                         host,strerror(errno));
65                 return(-1);
66                 }
67
68         if ((ppe=getprotobyname(protocol))==0) {
69                 fprintf(stderr,"can't get %s protocol entry: %s\n",
70                         protocol,strerror(errno));
71                 return(-1);
72                 }
73
74         if (!strcmp(protocol,"udp"))
75                 type = SOCK_DGRAM;
76         else
77                 type = SOCK_STREAM;
78
79         s = socket(PF_INET,type,ppe->p_proto);
80         if (s<0) {
81                 fprintf(stderr,"can't create socket: %s\n",strerror(errno));
82                 return(-1);
83                 }
84
85
86         signal(SIGALRM,timeout);
87         alarm(30);
88
89         if (connect(s,(struct sockaddr *)&sin,sizeof(sin))<0) {
90                 fprintf(stderr,"can't connect to %s.%s: %s\n",
91                         host,service,strerror(errno));
92                 return(-1);
93                 }
94
95         alarm(0);
96         signal(SIGALRM,SIG_IGN);
97
98         return(s);
99         }
100
101
102
103
104 /*
105  * Input binary data from socket
106  */
107 void serv_read(char *buf, int bytes)
108 {
109         int len,rlen;
110
111         len = 0;
112         while(len<bytes) {
113                 rlen = read(serv_sock,&buf[len],bytes-len);
114                 if (rlen<1) {
115                         fprintf(stderr, "Server connection broken: %s\n",
116                                 strerror(errno));
117                         connected = 0;
118                         logged_in = 0;
119                         return;
120                         }
121                 len = len + rlen;
122                 }
123         }
124
125
126 /*
127  * input string from pipe
128  */
129 void serv_gets(char *strbuf)
130 {
131         int ch,len;
132         char buf[2];
133
134         len = 0;
135         strcpy(strbuf,"");
136         do {
137                 serv_read(&buf[0], 1);
138                 ch = buf[0];
139                 strbuf[len++] = ch;
140                 } while((ch!=10)&&(ch!=13)&&(ch!=0)&&(len<255));
141         strbuf[len-1] = 0;
142         /* fprintf(stderr, ">%s\n", strbuf); */
143         }
144
145
146
147 /*
148  * send binary to server
149  */
150 void serv_write(char *buf, int nbytes)
151 {
152         int bytes_written = 0;
153         int retval;
154         while (bytes_written < nbytes) {
155                 retval = write(serv_sock, &buf[bytes_written],
156                         nbytes - bytes_written);
157                 if (retval < 1) {
158                         fprintf(stderr, "Server connection broken: %s\n",
159                                 strerror(errno));
160                         connected = 0;
161                         logged_in = 0;
162                         return;
163                         }
164                 bytes_written = bytes_written + retval;
165                 }
166         }
167
168
169 /*
170  * send line to server
171  */
172 void serv_puts(char *string)
173 {
174         char buf[256];
175
176         sprintf(buf,"%s\n",string);
177         serv_write(buf, strlen(buf));
178         }
179
180
181 /*
182  * convenience function to send stuff to the server
183  */
184 void serv_printf(const char *format, ...) {   
185         va_list arg_ptr;   
186         char buf[256];
187
188         va_start(arg_ptr, format);   
189         vsprintf(buf, format, arg_ptr);   
190         va_end(arg_ptr);   
191
192         strcat(buf, "\n");
193         serv_write(buf, strlen(buf));
194         /* fprintf(stderr, "<%s", buf); */
195         }
196
197