Skip to content

Commit c490a15

Browse files
committed
Barely-functional code check-in
The main TCP listening code seems to work, according to the testing I've done. Checking this in now as it is needed for other projects.
1 parent 490d989 commit c490a15

File tree

18 files changed

+998
-14
lines changed

18 files changed

+998
-14
lines changed

LICENSE

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BSD 3-Clause License
22

3-
Copyright (c) 2024, Kalopa Robotics
3+
Copyright (c) 2024, Kalopa Robotics Limited.
44

55
Redistribution and use in source and binary forms, with or without
66
modification, are permitted provided that the following conditions are met:
@@ -12,17 +12,18 @@ modification, are permitted provided that the following conditions are met:
1212
this list of conditions and the following disclaimer in the documentation
1313
and/or other materials provided with the distribution.
1414

15-
3. Neither the name of the copyright holder nor the names of its
16-
contributors may be used to endorse or promote products derived from
17-
this software without specific prior written permission.
15+
3. Neither the Kalopa Robotics Limited nor the names of its contributors
16+
may be used to endorse or promote products derived from this software
17+
without specific prior written permission.
1818

19-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20-
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23-
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24-
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25-
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26-
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27-
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
19+
THIS SOFTWARE IS PROVIDED BY THE KALOPA ROBOTICS LIMITED AND CONTRIBUTORS
20+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22+
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE KALOPA
23+
ROBOTICS LIMITED OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26+
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2829
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Makefile

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#
2+
# Copyright (c) 2024, Kalopa Robotics Limited.
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions
6+
# are met:
7+
#
8+
# 1. Redistributions of source code must retain the above copyright
9+
# notice, this list of conditions and the following disclaimer.
10+
#
11+
# 2. Redistributions in binary form must reproduce the above
12+
# copyright notice, this list of conditions and the following
13+
# disclaimer in the documentation and/or other materials provided
14+
# with the distribution.
15+
#
16+
# 3. Neither Kalopa Robotics Limited nor the names of its contributors
17+
# may be used to endorse or promote products derived from this
18+
# software without specific prior written permission.
19+
#
20+
# THIS SOFTWARE IS PROVIDED BY KALOPA ROBOTICS LIMITED AND
21+
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
22+
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23+
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24+
# IN NO EVENT SHALL KALOPA ROBOTICS LIMITED OR CONTRIBUTORS BE
25+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26+
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27+
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28+
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29+
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31+
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32+
# SUCH DAMAGE.
33+
#
34+
DIRS= lib test
35+
PREFIX?=/usr/local
36+
37+
VERSION=0.1
38+
TARBALL=libkalnet_$(VERSION).orig.tar.gz
39+
40+
all:
41+
@for d in $(DIRS); do $(MAKE) -C $$d all; done
42+
43+
install:
44+
@for d in $(DIRS); do $(MAKE) -C $$d install; done
45+
46+
clean:
47+
@for d in $(DIRS); do $(MAKE) -C $$d clean; done
48+
rm -f $(TARBALL)
49+
50+
tarball: clean
51+
tar cvf $(TARBALL) --exclude .git --exclude $(TARBALL) .

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
11
# libkalnet
2-
A C library for managing and maintaining TCP/IP connections in a portable, efficient and non-blocking way.
2+
3+
A C library for managing and maintaining TCP/IP connections in a
4+
portable, efficient and non-blocking way.
5+
After writing the same C code for TCP/IP connectivity many, many
6+
times, it seemed right to just do it once more but this time as a
7+
library.
8+
9+
The intention here is to have a basic library for either a server
10+
or a client.
11+
In the server case, you can listen for UDP packets or TCP connections.
12+
The library will manage the new connections and (if required) handle
13+
reads and writes.
14+
You can specify callbacks for things like new connections or incoming
15+
data.
16+
Eventually, I'd like to build a pretty basic HTTP server with similar
17+
callbacks for specific URLs.
18+
The next progression after that would be to build in a Prometheus
19+
client so you can report metrics to Prometheus without needing a
20+
whole pile of additional code, just this library.
21+
For now though, it's just about managing and maintaining channels
22+
and connections.

debian/changelog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
libkalnet (0.1-1) UNRELEASED; urgency=medium
2+
3+
* Initial release.
4+
5+
-- Dermot Tynan <[email protected]> Mon, 12 Feb 2024 18:41:15 +0000

debian/control

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Source: libkalnet
2+
Maintainer: Dermot Tynan <[email protected]>
3+
Section: misc
4+
Priority: optional
5+
Standards-Version: 4.6.2
6+
Homepage: https://github.com/kalopa/libkalnet
7+
Build-Depends: debhelper-compat (= 13)
8+
9+
Package: libkalnet
10+
Architecture: any
11+
Depends: ${shlibs:Depends}, ${misc:Depends}
12+
Description: A C library for TCP and UDP connections.
13+
Based on the select(2) system call, libkalnet provides
14+
a framework for building and maintaining multiple TCP
15+
and UDP connections.

debian/copyright

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2024, Kalopa Robotics Limited.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
1. Redistributions of source code must retain the above copyright notice, this
9+
list of conditions and the following disclaimer.
10+
11+
2. Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
15+
3. Neither the Kalopa Robotics Limited nor the names of its contributors
16+
may be used to endorse or promote products derived from this software
17+
without specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE KALOPA ROBOTICS LIMITED AND CONTRIBUTORS
20+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22+
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE KALOPA
23+
ROBOTICS LIMITED OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26+
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

debian/rules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/make -f
2+
%:
3+
dh $@

debian/source/format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
12.0 (bookworm)

lib/Makefile

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#
2+
# Copyright (c) 2024, Kalopa Robotics Limited.
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions
6+
# are met:
7+
#
8+
# 1. Redistributions of source code must retain the above copyright
9+
# notice, this list of conditions and the following disclaimer.
10+
#
11+
# 2. Redistributions in binary form must reproduce the above
12+
# copyright notice, this list of conditions and the following
13+
# disclaimer in the documentation and/or other materials provided
14+
# with the distribution.
15+
#
16+
# 3. Neither Kalopa Robotics Limited nor the names of its contributors
17+
# may be used to endorse or promote products derived from this
18+
# software without specific prior written permission.
19+
#
20+
# THIS SOFTWARE IS PROVIDED BY KALOPA ROBOTICS LIMITED AND
21+
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
22+
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23+
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24+
# IN NO EVENT SHALL KALOPA ROBOTICS LIMITED OR CONTRIBUTORS BE
25+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26+
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27+
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28+
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29+
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31+
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32+
# SUCH DAMAGE.
33+
#
34+
PREFIX?=/usr/local
35+
CFLAGS= -Wall -O -I..
36+
37+
SRCS= init.c channel.c loop.c listen.c queue.c parse.c
38+
OBJS= $(SRCS:.c=.o)
39+
LIB= libkalnet.a
40+
41+
all: $(LIB)
42+
43+
clean:
44+
rm -f $(LIB) $(OBJS)
45+
46+
install: $(LIB)
47+
install -m 0644 $(LIB) $(PREFIX)/lib/$(LIB)
48+
49+
$(LIB): $(OBJS)
50+
ar ru $@ $?
51+
52+
$(OBJS): ../libkalnet.h

lib/channel.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2024, Kalopa Robotics Limited.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above
12+
* copyright notice, this list of conditions and the following
13+
* disclaimer in the documentation and/or other materials provided
14+
* with the distribution.
15+
*
16+
* 3. Neither Kalopa Robotics Limited nor the names of its contributors
17+
* may be used to endorse or promote products derived from this
18+
* software without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY KALOPA ROBOTICS LIMITED AND
21+
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
22+
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23+
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24+
* IN NO EVENT SHALL KALOPA ROBOTICS LIMITED OR CONTRIBUTORS BE
25+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26+
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27+
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28+
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31+
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32+
* SUCH DAMAGE.
33+
*/
34+
#include <stdio.h>
35+
#include <stdlib.h>
36+
#include <unistd.h>
37+
#include <string.h>
38+
39+
#include "libkalnet.h"
40+
41+
/*
42+
* Allocate a kn_channel structure and return a pointer to it.
43+
*/
44+
struct kn_channel *
45+
kn_channel_alloc(void)
46+
{
47+
static int next_channo = 0;
48+
struct kn_channel *kcp;
49+
50+
if ((kcp = kn_get_queue_head(KN_FREE_Q)) == NULL) {
51+
if ((kcp = malloc(sizeof(struct kn_channel))) == NULL) {
52+
perror("kn_channel_alloc: malloc");
53+
exit(1);
54+
}
55+
memset(kcp, 0, sizeof(struct kn_channel));
56+
kn_enqueue(kcp, KN_BUSY_Q);
57+
} else
58+
kn_qmove(kcp, KN_BUSY_Q);
59+
kcp->fd = -1;
60+
kcp->state = KN_CHANNEL_STATE_ACTIVE;
61+
kcp->type = KN_CHANNEL_TYPE_UNKNOWN;
62+
kcp->channo = ++next_channo;
63+
kcp->rdfunc = kcp->wrfunc = NULL;
64+
return(kcp);
65+
}
66+
67+
/*
68+
* Channel read or write error. It's dead. Kill it.
69+
*/
70+
void
71+
kn_channel_close(struct kn_channel *kcp)
72+
{
73+
if (kcp->fd >= 0)
74+
close(kcp->fd);
75+
kcp->state = KN_CHANNEL_STATE_DEAD;
76+
kn_disable_channel_fd(kcp, KN_IO_READ | KN_IO_WRITE);
77+
kn_qmove(kcp, KN_FREE_Q);
78+
}
79+
80+
/*
81+
* Is this channel still active?
82+
*/
83+
int
84+
kn_is_active(struct kn_channel *kcp)
85+
{
86+
return(kcp->state == KN_CHANNEL_STATE_ACTIVE);
87+
}

0 commit comments

Comments
 (0)