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