catalinux / Conn (public) (License: LGPLv2) (since 2016-03-01) (hash sha1)
Net library for easy building ipv4/ipv6 network daemons/clients
List of commits:
Subject Hash Author Date (UTC)
Removed bind_s example because blackhole_s will take place. b9b06d98f00413b5a5df9d7bc9b3bd05b5536f28 Catalin(ux) M. BOIE 2009-08-25 20:07:38
Added Conn_set_address helper. 1b6e7e8a294ac88aa966e4e43bb77a4fdff0ca5a Catalin(ux) M. BOIE 2009-08-25 19:21:00
Fixed storing of local address (do not overwrite remote address). d431edba6699d1363e4d616aa6f96588272188b0 Catalin(ux) M. BOIE 2009-08-25 18:37:55
Added Conn_stop to eleganly stop Conn system. 4ca191771c5c119e9755a5af3158fabe43ddf5f2 Catalin(ux) M. BOIE 2009-08-25 18:30:32
Replaced Conn_protocol with Conn_get_socket_protocol. a1cbc5c612d82e6d1341524c67fe249648c81806 Catalin(ux) M. BOIE 2009-08-25 18:16:45
Compressed a little the POLL flags. 47b5dce395694c18c973e3ce6ba9a71c690a41bc Catalin(ux) M. BOIE 2009-08-25 18:16:16
Store also local address for incoming connections. 0548ce6434185cdd36e110c61315b4cb21849666 Catalin(ux) M. BOIE 2009-08-25 18:15:46
Added possibility to shutdown blackhole_s server by typing 'quit'. 5fd60900809e1a2d7554aa7fa93ce83b29214de0 Catalin(ux) M. BOIE 2009-08-25 17:54:27
Cosmetic changes. 7099dd49a270b9f4ecc0cb2e55254bef671202e5 Catalin(ux) M. BOIE 2009-08-25 17:54:15
Improved status output. 1ff597059bbb208b56ad18bf8324fae5b63950a2 Catalin(ux) M. BOIE 2009-08-25 17:53:44
Conn_type_* becomes CONN_TYPE_*. b284a20cc5430042cc662edc4d121c7f18cb3569 Catalin(ux) M. BOIE 2009-08-25 17:52:51
Improved status logging. 54fea00d8f16127244fd15b209c24f606adb20b3 Catalin(ux) M. BOIE 2009-08-25 17:33:14
Added Conn_strcasestr for case insensitive search in buffer. ca14b37b2a1f23887ad12fed729a3dd0ff253443 Catalin(ux) M. BOIE 2009-08-24 17:18:24
Improved ntime example. b63de9b82ffab1c0d759571a40f1d72f9b8df927 Catalin(ux) M. BOIE 2009-08-24 17:09:21
Removed unused includes. 8af81a4c311caa7d738000a5208d01d7620d1040 Catalin(ux) M. BOIE 2009-08-21 18:57:33
Improved blackhole example. 2fd7ff181a412f4ba060b0e89b97acdf89a8fb88 Catalin(ux) M. BOIE 2009-08-21 18:03:22
Fixed connections to outside. Still, it is a little mess there. 417ccd4a48508a810377676658d51c5103168472 Catalin(ux) M. BOIE 2009-08-21 17:42:37
addr and bind_addr are not pointers! Corrected them. e1deeb765500ee079802c2cd6543f9c9496d8f9b Catalin(ux) M. BOIE 2009-08-21 16:49:24
Exported _alloc and _commit functions. 9a3159e89eff020cfb75b9eec49a45a637a6da57 Catalin(ux) M. BOIE 2009-08-21 16:39:27
Added blackhole example. 8a7ac49b018ad65ba386f8a50725745127b4cbd2 Catalin(ux) M. BOIE 2009-08-21 15:54:55
Commit b9b06d98f00413b5a5df9d7bc9b3bd05b5536f28 - Removed bind_s example because blackhole_s will take place.
Signed-off-by: Catalin(ux) M. BOIE <catab@embedromix.ro>
Author: Catalin(ux) M. BOIE
Author date (UTC): 2009-08-25 20:07
Committer name: Catalin(ux) M. BOIE
Committer date (UTC): 2009-09-02 15:42
Parent(s): 1b6e7e8a294ac88aa966e4e43bb77a4fdff0ca5a
Signing key:
Tree: e9eb95116a2978fd33a5d2070bbd69d9197b795d
File Lines added Lines deleted
examples/s_bind.c 0 167
File examples/s_bind.c deleted (index f3fede1..0000000)
1 /*
2 * Copyright: Catalin(ux) M. BOIE
3 * Shows how to bind to a specific address.
4 */
5
6 #define _GNU_SOURCE
7
8 #include <errno.h>
9 #include <string.h>
10 #include <strings.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <sys/poll.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <stdio.h>
17 #include <fcntl.h>
18 #include <netinet/in.h>
19 #include <sys/time.h>
20 #include <sys/ioctl.h>
21 #include <time.h>
22 #include <arpa/inet.h>
23 #include <stdarg.h>
24
25 #include <Conn.h>
26
27 /* Global variables */
28 static unsigned short debug = 20;
29
30 static FILE *Logf = NULL;
31 static char *log_file = "s_bind.log";
32 static int port = 9000;
33 static short ipv4 = 1, ipv6 = 1;
34 static int maxconn = 0;
35
36 static void s_accept_error(struct Conn *C)
37 {
38 Log(0, "%s: Cannot accept a new connection!\n");
39 }
40
41 static void s_accept(struct Conn *C)
42 {
43 char *s;
44
45 Log(4, "%s (A connection was accepted from [%s/%d]. Enqueue greeting...\n",
46 __FUNCTION__, C->addr, C->port);
47
48 s = "Hello!\r\n";
49 Conn_enqueue(C, s, strlen(s));
50 }
51
52 static void s_close(struct Conn *C)
53 {
54 Log(5, "%s (A connection will be closed [%s/%d] id=%lld)\n",
55 __FUNCTION__, C->addr, C->port, Conn_getid(C));
56 }
57
58 static int s_data_cb(struct Conn *C, char *line)
59 {
60 char *dump;
61 char buf[128];
62
63 if (debug >= 8) {
64 dump = Conn_dump(line, strlen(line));
65 Log(8, "%s: recv head=%d tail=%d bytes on slot %d: [%s]\n",
66 __FUNCTION__, C->ibuf_head, C->ibuf_tail,
67 C->slot, dump);
68 free(dump);
69 }
70
71 if (strcasecmp(line, "quit") == 0)
72 exit(0);
73
74 snprintf(buf, sizeof(buf), "%s\n", line);
75 Log(8, "Send back: %s", buf);
76 Conn_enqueue(C, buf, strlen(buf));
77
78 return 0;
79 }
80
81 static void s_data(struct Conn *C)
82 {
83 Conn_for_every_line(C, s_data_cb);
84 }
85
86 static void s_error(struct Conn *C)
87 {
88 Log(1, "%s id=%llu (%s)\n",
89 __FUNCTION__, Conn_getid(C), Conn_strerror());
90 }
91
92 int main(void)
93 {
94 struct Conn *I4 = NULL, *I6 = NULL;
95 int ret;
96 struct timeval start;
97
98 Logf = fopen(log_file, "w");
99 if (!Logf) {
100 fprintf(stderr, "Cannot open log file [%s] [%s]\n",
101 log_file, strerror(errno));
102 return 1;
103 }
104 if (debug > 0)
105 setlinebuf(Logf);
106
107 /*daemon(0, 0);*/
108
109 gettimeofday(&start, NULL);
110 Log(0, "\nStarting at %ld...\n", start.tv_sec);
111 Log(0, "\tPort=%d\n", port);
112 Log(0, "\tLogFile=%s Debug=%d\n", log_file, debug);
113 Log(0, "\tipv4=%s ipv6=%s\n", (ipv4 == 1) ? "on" : "off", (ipv6 == 1) ? "on" : "off");
114
115 /* set library output debug */
116 Conn_debug(Logf, debug);
117
118 ret = Conn_init(maxconn);
119 if (ret == -1) {
120 Log(0, "%s", Conn_strerror());
121 return 1;
122 }
123
124 if (ipv4 == 1) {
125 Log(9, "Try to register IPv4 socket...\n");
126 I4 = Conn_socket_addr(PF_INET, SOCK_STREAM, "127.0.0.1", port);
127 if (!I4)
128 Log(1, "Cannot bind on ipv4 socket [%s].\n", Conn_strerror());
129 }
130
131 if (ipv6 == 1) {
132 Log(9, "Try to register IPv6 socket...\n");
133 I6 = Conn_socket_addr(PF_INET6, SOCK_STREAM, "::1", port);
134 if (!I6)
135 Log(1, "Cannot bind on ipv6 socket [%s].\n", Conn_strerror());
136 }
137
138 if ((I6 == NULL) && (I4 == NULL)) {
139 Log(0, "Cannot bind!\n");
140 return 1;
141 }
142
143 Conn_accept_cb = s_accept;
144 Conn_data_cb = s_data;
145 Conn_close_cb = s_close;
146 Conn_error_cb = s_error;
147 Conn_accept_error_cb = s_accept_error;
148
149 while (1) {
150 ret = Conn_poll(10000);
151 if (ret == -1) {
152 Log(0, "Error calling Conn_poll [%s]!\n", Conn_strerror());
153 return 1;
154 }
155
156 /* Allow 30 seconds and exit */
157 /*
158 loops++;
159 if (loops > 30)
160 break;
161 */
162 }
163
164 Log(0, "Finish!\n\n");
165
166 return 0;
167 }
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/catalinux/Conn

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/catalinux/Conn

Clone this repository using git:
git clone git://git.rocketgit.com/user/catalinux/Conn

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main