aboutsummaryrefslogtreecommitdiff
path: root/jim-aio.c
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2014-09-01 08:03:40 +1000
committerSteve Bennett <steveb@workware.net.au>2014-09-01 08:07:06 +1000
commitc4d4bf8bc104733db1f5992a27d88fbfca9ba882 (patch)
treec5204de5e8fa064002561527490e9f38a1e46edb /jim-aio.c
parenta71a09f67ceb1bdadde86981da76d4f42dffab81 (diff)
downloadjimtcl-c4d4bf8bc104733db1f5992a27d88fbfca9ba882.zip
jimtcl-c4d4bf8bc104733db1f5992a27d88fbfca9ba882.tar.gz
jimtcl-c4d4bf8bc104733db1f5992a27d88fbfca9ba882.tar.bz2
aio: consolidate address formatting
Removes some duplicated code Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'jim-aio.c')
-rw-r--r--jim-aio.c77
1 files changed, 33 insertions, 44 deletions
diff --git a/jim-aio.c b/jim-aio.c
index 37e1ea7..62a6146 100644
--- a/jim-aio.c
+++ b/jim-aio.c
@@ -266,6 +266,37 @@ static int JimParseDomainAddress(Jim_Interp *interp, const char *path, struct so
return JIM_OK;
}
#endif
+
+/**
+ * Format that address in 'sa' as a string and store in variable 'varObjPtr'
+ */
+static int JimFormatIpAddress(Jim_Interp *interp, Jim_Obj *varObjPtr, const union sockaddr_any *sa)
+{
+ /* INET6_ADDRSTRLEN is 46. Add some for [] and port */
+ char addrbuf[60];
+
+#if IPV6
+ if (sa->sa.sa_family == PF_INET6) {
+ addrbuf[0] = '[';
+ /* Allow 9 for []:65535\0 */
+ inet_ntop(sa->sa.sa_family, &sa->sin6.sin6_addr, addrbuf + 1, sizeof(addrbuf) - 9);
+ snprintf(addrbuf + strlen(addrbuf), 8, "]:%d", ntohs(sa->sin.sin_port));
+ }
+ else
+#endif
+ if (sa->sa.sa_family == PF_INET) {
+ /* Allow 7 for :65535\0 */
+ inet_ntop(sa->sa.sa_family, &sa->sin.sin_addr, addrbuf, sizeof(addrbuf) - 7);
+ snprintf(addrbuf + strlen(addrbuf), 7, ":%d", ntohs(sa->sin.sin_port));
+ }
+ else {
+ /* recvfrom still works on unix domain sockets, etc */
+ addrbuf[0] = 0;
+ }
+
+ return Jim_SetVariable(interp, varObjPtr, Jim_NewStringObj(interp, addrbuf, -1));
+}
+
#endif /* JIM_BOOTSTRAP */
static void JimAioSetError(Jim_Interp *interp, Jim_Obj *name)
@@ -549,31 +580,7 @@ static int aio_cmd_recvfrom(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, buf, rlen));
if (argc > 1) {
- /* INET6_ADDRSTRLEN is 46. Add some for [] and port */
- char addrbuf[60];
-
-#if IPV6
- if (sa.sa.sa_family == PF_INET6) {
- addrbuf[0] = '[';
- /* Allow 9 for []:65535\0 */
- inet_ntop(sa.sa.sa_family, &sa.sin6.sin6_addr, addrbuf + 1, sizeof(addrbuf) - 9);
- snprintf(addrbuf + strlen(addrbuf), 8, "]:%d", ntohs(sa.sin.sin_port));
- }
- else
-#endif
- if (sa.sa.sa_family == PF_INET) {
- /* Allow 7 for :65535\0 */
- inet_ntop(sa.sa.sa_family, &sa.sin.sin_addr, addrbuf, sizeof(addrbuf) - 7);
- snprintf(addrbuf + strlen(addrbuf), 7, ":%d", ntohs(sa.sin.sin_port));
- }
- else {
- /* recvfrom still works on unix domain sockets, etc */
- addrbuf[0] = 0;
- }
-
- if (Jim_SetVariable(interp, argv[1], Jim_NewStringObj(interp, addrbuf, -1)) != JIM_OK) {
- return JIM_ERR;
- }
+ return JimFormatIpAddress(interp, argv[1], &sa);
}
return JIM_OK;
@@ -624,25 +631,7 @@ static int aio_cmd_accept(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
}
if (argc > 0) {
- /* INET6_ADDRSTRLEN is 46. Add some for [] and port */
- char addrbuf[60];
-
-#if IPV6
- if (sa.sa.sa_family == PF_INET6) {
- addrbuf[0] = '[';
- /* Allow 9 for []:65535\0 */
- inet_ntop(sa.sa.sa_family, &sa.sin6.sin6_addr, addrbuf + 1, sizeof(addrbuf) - 9);
- snprintf(addrbuf + strlen(addrbuf), 8, "]:%d", ntohs(sa.sin.sin_port));
- }
- else
-#endif
- if (sa.sa.sa_family == PF_INET) {
- /* Allow 7 for :65535\0 */
- inet_ntop(sa.sa.sa_family, &sa.sin.sin_addr, addrbuf, sizeof(addrbuf) - 7);
- snprintf(addrbuf + strlen(addrbuf), 7, ":%d", ntohs(sa.sin.sin_port));
- }
-
- if (Jim_SetVariable(interp, argv[0], Jim_NewStringObj(interp, addrbuf, -1)) != JIM_OK) {
+ if (JimFormatIpAddress(interp, argv[0], &sa) != JIM_OK) {
return JIM_ERR;
}
}