Rombobjörn

summaryrefslogtreecommitdiff
path: root/sockaddr_functions.c
diff options
context:
space:
mode:
authorBjörn Persson <bjorn@rombobjörn.se>2012-01-08 10:38:18 +0000
committerBjörn Persson <bjorn@rombobjörn.se>2012-01-08 10:38:18 +0000
commit30ab793c01eb86c885f837da6d14c6caa0a86625 (patch)
treecd91b7d97133c9b4e7fd5b3ad58e6c7e737964ec /sockaddr_functions.c
parentcc0fd29fa3881823c6ce1c0f98e02919d22feb75 (diff)
· Added new features corresponding to new versions of Libmilter.
· Added a multiple-line version of Set_Reply. · Added functions to access the client's IP address and port. · Clarified error messages. · Clarified some comments.
Diffstat (limited to 'sockaddr_functions.c')
-rw-r--r--sockaddr_functions.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/sockaddr_functions.c b/sockaddr_functions.c
new file mode 100644
index 0000000..6148414
--- /dev/null
+++ b/sockaddr_functions.c
@@ -0,0 +1,68 @@
+#include <stdint.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+
+extern const uint8_t milter_api_address_type_ipv4;
+extern const uint8_t milter_api_address_type_ipv6;
+extern const uint8_t milter_api_address_type_unknown;
+
+
+uint8_t milter_api_address_type(struct sockaddr const* const endpoint) {
+ if(endpoint->sa_family == AF_INET) {
+ return milter_api_address_type_ipv4;
+ } else if(endpoint->sa_family == AF_INET6) {
+ return milter_api_address_type_ipv6;
+ } else {
+ return milter_api_address_type_unknown;
+ }
+}
+
+
+void milter_api_ipv4_address(struct sockaddr_in const* const endpoint, // in
+ uint8_t* const buffer) // out
+{
+ memcpy(buffer, &endpoint->sin_addr, 4);
+}
+
+
+void milter_api_ipv6_address(struct sockaddr_in6 const* const endpoint, // in
+ uint8_t* const buffer) // out
+{
+ memcpy(buffer, &endpoint->sin6_addr, 16);
+}
+
+
+void milter_api_address_string(struct sockaddr const* const endpoint, // in
+ char* const buffer, // out
+ const uint8_t size) // in
+{
+ char const* result = NULL;
+
+ if(endpoint->sa_family == AF_INET) {
+ result = inet_ntop(endpoint->sa_family,
+ &((struct sockaddr_in const*)endpoint)->sin_addr,
+ buffer, size);
+ } else if(endpoint->sa_family == AF_INET6) {
+ result = inet_ntop(endpoint->sa_family,
+ &((struct sockaddr_in6 const*)endpoint)->sin6_addr,
+ buffer, size);
+ }
+ if(result == NULL) {
+ strncpy(buffer, "(error in address conversion)", size);
+ buffer[size - 1] = '\0';
+ }
+}
+
+
+uint16_t milter_api_port(struct sockaddr const* const endpoint) {
+ if(endpoint->sa_family == AF_INET) {
+ return ntohs(((struct sockaddr_in const*)endpoint)->sin_port);
+ } else if(endpoint->sa_family == AF_INET6) {
+ return ntohs(((struct sockaddr_in6 const*)endpoint)->sin6_port);
+ } else {
+ return 0;
+ }
+}