annotate mcabber/socket.c @ 0:b3b2332715fb

Tailorization of /trunk Import of the upstream sources from Repository: file:///tmp/svn-mcabber Module: /trunk Revision: 15
author tailor@frmp8452
date Thu, 30 Jun 2005 21:39:31 +0000
parents
children 0c2d2b79c4b5
rev   line source
0
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
1 #include <stdio.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
2 #include <stdlib.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
3 #include <string.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
4 #include <sys/socket.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
5 #include <unistd.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
6 #include "utils.h"
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
7
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
8 #include "socket.h"
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
9 #include <signal.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
10
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
11 /* Desc: create socket connection
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
12 *
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
13 * In : servername, port
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
14 * Out : socket (or -1 on error)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
15 *
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
16 * Note: -
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
17 */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
18 int sk_conn(struct sockaddr *name)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
19 {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
20 int sock;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
21
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
22 if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
23 perror("socket (socket.c:23)");
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
24 return -1;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
25 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
26
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
27 if (connect(sock, (struct sockaddr *) name, sizeof(struct sockaddr)) < 0) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
28 perror("connect (socket.c:29)");
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
29 return -1;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
30 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
31
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
32 return sock;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
33 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
34
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
35
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
36 /* Desc: send data through socket
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
37 *
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
38 * In : socket, buffer to send
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
39 * Out : 0 = fail, 1 = pass
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
40 *
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
41 * Note: -
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
42 */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
43 int sk_send(int sock, char *buffer)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
44 {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
45 if ((send(sock, buffer, strlen(buffer), 0)) == -1)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
46 return 0;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
47 else
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
48 return 1;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
49 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
50
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
51 /* Desc: receive data through socket
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
52 *
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
53 * In : socket
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
54 * Out : received buffer
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
55 *
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
56 * Note: it is up to the caller to free the returned string
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
57 */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
58 char *sk_recv(int sock)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
59 {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
60 int i = 1;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
61 int tambuffer = 128;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
62 char mtag[16];
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
63
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
64 char *buffer = malloc(tambuffer);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
65 char *retval = malloc(tambuffer + 1);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
66
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
67 memset(retval, 0, tambuffer);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
68 memset(buffer, 0, tambuffer + 1);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
69
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
70 while (1) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
71 char *p1;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
72 recv(sock, buffer, tambuffer, 0);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
73
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
74 if (i == 1) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
75 char *p2;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
76 strcpy(retval, buffer);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
77 p1 = retval+1;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
78 p2 = mtag;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
79 while (('a' <= *p1) && (*p1 <= 'z') && (p2-mtag < 14))
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
80 *p2++ = *p1++;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
81 *p2++ = '>'; *p2++ = 0;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
82 //fprintf(stderr, "TAG=\"%s\"\n", mtag);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
83 } else {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
84 retval = realloc(retval, (tambuffer * i) + 1);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
85 strncat(retval, buffer, tambuffer + 1);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
86 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
87 i++;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
88 p1 = retval + strlen(retval) - strlen(mtag);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
89 //fprintf(stderr, "buffer:[%s]\n", buffer);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
90 //fprintf(stderr, "End RET=[%s]\n", p1);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
91 if (!strcmp(p1, mtag))
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
92 break;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
93 for (p1 = retval; *p1 && (*p1 != '>'); p1++);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
94 if ((*p1 == '>') && (*(p1-1) == '/'))
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
95 break;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
96 memset(buffer, 0, tambuffer);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
97 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
98 free(buffer);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
99 ut_WriteLog("Received:%s\n", retval);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
100 return retval;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
101 }