1 //#include <clib/debug_protos.h>
3 //#include <emul/emulregs.h>
4 #include <exec/libraries.h>
5 #include <exec/lists.h>
6 #include <exec/resident.h>
7 #include <exec/semaphores.h>
9 #include <libraries/miami.h>
10 #include <proto/exec.h>
11 #include <proto/utility.h>
15 #define USE_INLINE_STDARG
16 //#include <proto/usergroup.h>
19 #include <sys/malloc.h>
20 #include <sys/socket.h>
21 #include <kern/amiga_log.h>
22 #include <net/route.h>
24 #include <net/if_protos.h>
32 #include "amiga_api.h"
33 #include "gethtbynamadr.h"
34 #include "miami_api.h"
36 #include <proto/bsdsocket.h>
39 #define SocketBase MiamiBase->_SocketBase
40 #define UserGroupBase MiamiBase->_UserGroupBase
42 typedef VOID (* f_void
)(APTR args
, ...);
44 extern f_void Miami_UserFuncTable
[];
45 extern struct ifnet
*ifnet
;
47 void __MiamiLIB_Cleanup(struct MiamiBase
*MiamiBase
)
53 D(bug("[AROSTCP.MIAMI] miami_api.c: __MiamiLIB_Cleanup()\n"));
57 CloseLibrary(&SocketBase
->libNode
);
59 freestart
= (void *)((IPTR
)MiamiBase
- (IPTR
)MiamiBase
->Lib
.lib_NegSize
);
60 size
= MiamiBase
->Lib
.lib_NegSize
+ MiamiBase
->Lib
.lib_PosSize
;
61 FreeMem(freestart
, size
);
64 AROS_LH1 (struct Library
*, Open
,
65 AROS_LHA(ULONG
, version
, D0
),
66 struct Library
*, MasterBase
, 1, Miami
)
70 struct MiamiBase
*MiamiBase
;
74 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiLIB_Open()\n"));
77 D(kprintf("MiamiLIB_Open: 0x%p <%s> OpenCount %ld\n",
79 MasterBase
->lib_Node
.ln_Name
,
80 MasterBase
->lib_OpenCnt
));
82 MiamiBase
= (struct MiamiBase
*)MakeLibrary(Miami_UserFuncTable
,
83 #if !defined(__AROS__)
84 (UWORD
*)&Miami_initTable
,
89 sizeof(struct MiamiBase
),
92 ((struct Library
*)MiamiBase
)->lib_Node
.ln_Type
= NT_LIBRARY
;
93 ((struct Library
*)MiamiBase
)->lib_Node
.ln_Name
= (APTR
)MIAMILIBNAME
;
94 ((struct Library
*)MiamiBase
)->lib_Flags
= (LIBF_SUMUSED
|LIBF_CHANGED
);
95 ((struct Library
*)MiamiBase
)->lib_Version
= MIAMI_VERSION
;
96 ((struct Library
*)MiamiBase
)->lib_Revision
= MIAMI_REVISION
;
97 ((struct Library
*)MiamiBase
)->lib_IdString
= (APTR
)RELEASESTRING MIAMI_VSTRING
;
99 D(bug("[AROSTCP](miami_api.c) MiamiLIB_Open: Created MIAMI user library base: 0x%p\n", MiamiBase
));
101 D(kprintf("Created user miami.library base: 0x%p\n", MiamiBase
);)
103 for (i
= (WORD
*)((struct Library
*)MiamiBase
+ 1); i
< (WORD
*)(MiamiBase
+ 1); i
++)
105 MiamiBase
->Lib
.lib_OpenCnt
= 1;
106 SocketBase
= (APTR
)OpenLibrary("bsdsocket.library", VERSION
);
108 D(__log(LOG_DEBUG
,"miami.library opened: SocketBase = 0x%p, MiamiBase = 0x%p", (IPTR
)SocketBase
, (IPTR
)MiamiBase
);)
109 MasterBase
->lib_OpenCnt
++;
110 return((struct Library
*)MiamiBase
);
112 D(else kprintf("Unable to open bsdsocket.library\n");)
113 __MiamiLIB_Cleanup(MiamiBase
);
120 AROS_LH0(ULONG
*, Close
, struct MiamiBase
*, MiamiBase
, 2, Miami
)
124 #if defined(__AROS__)
125 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiLIB_Close()\n"));
128 D(kprintf("MiamiLIB_Close: 0x%p <%s> OpenCount %ld\n",
130 MasterMiamiBase
->lib_Node
.ln_Name
,
131 MasterMiamiBase
->lib_OpenCnt
));
133 if (MiamiBase
->_UserGroupBase
)
134 CloseLibrary(MiamiBase
->_UserGroupBase
);
136 __MiamiLIB_Cleanup(MiamiBase
);
137 MasterMiamiBase
->lib_OpenCnt
--;
138 D(kprintf("MiamiLIB_Close: done\n"));
145 AROS_LH7(int, MiamiSysCtl
,
146 AROS_LHA(LONG
*, name
, A0
),
147 AROS_LHA(ULONG
, namelen
, D0
),
148 AROS_LHA(void *, oldp
, A1
),
149 AROS_LHA(LONG
*, oldlenp
, A2
),
150 AROS_LHA(void *, newp
, A3
),
151 AROS_LHA(LONG
, newlen
, D1
),
152 AROS_LHA(int, len
, D2
),
153 struct MiamiBase
*, MiamiBase
, 5, Miami
157 #if defined(__AROS__)
158 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiSysCtl()\n"));
161 __log(LOG_CRIT
,"MiamiSysCtl() is not implemented");
166 AROS_LH1(void, MiamiDisallowDNS
,
167 AROS_LHA(LONG
, value
, D0
),
168 struct MiamiBase
*, MiamiBase
, 11, Miami
173 #if defined(__AROS__)
174 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiDisallowDNS()\n"));
177 DSYSCALLS(__log(LOG_DEBUG
,"MiamiDisallowDNS(%ld) called",value
);)
179 SocketBase
->res_state
.options
|= AROSTCP_RES_DISABLED
;
181 SocketBase
->res_state
.options
&= ~AROSTCP_RES_DISABLED
;
186 AROS_LH0(void *, MiamiGetPid
,
187 struct MiamiBase
*, MiamiBase
, 13, Miami
192 #if defined(__AROS__)
193 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiGetPid()\n"));
196 return FindTask(NULL
);
201 AROS_LH3(APTR
, MiamiPFAddHook
,
202 AROS_LHA(struct Hook
*, hook
, A0
),
203 AROS_LHA(UBYTE
*, interface
, A1
),
204 AROS_LHA(struct TagItem
*, taglist
, A2
),
205 struct MiamiBase
*, MiamiBase
, 16, Miami
210 struct packet_filter_hook
*pf
= NULL
;
213 #if defined(__AROS__)
214 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiPFAddHook()\n"));
217 #ifdef ENABLE_PACKET_FILTER
218 DPF(__log(LOG_DEBUG
,"MiamiPFAddHook(0x%p, %s) called", hook
, interface
);)
220 ifp
= ifunit(interface
);
221 DPF(__log(LOG_DEBUG
,"ifp = 0x%pn");)
223 pf
= bsd_malloc(sizeof(struct packet_filter_hook
), NULL
, NULL
);
224 DPF(syslog(LOG_DEBUG
,"Handle = 0x%p", pf
);)
227 pf
->pfil_hook
= hook
;
228 pf
->pfil_hooktype
= GetTagData(MIAMITAG_HOOKTYPE
, MIAMICPU_M68KREG
, taglist
);
229 ObtainSemaphore(&pfil_list_lock
);
230 AddTail((struct List
*)&pfil_list
, (struct Node
*)pf
);
231 ReleaseSemaphore(&pfil_list_lock
);
232 DPF(__log(LOG_DEBUG
,"Added packet filter hook:");)
233 DPF(__log(LOG_DEBUG
,"Function: 0x%p", hook
->h_Entry
);)
234 DPF(__log(LOG_DEBUG
,"CPU type: %lu", pf
->pfil_hooktype
);)
238 __log(LOG_CRIT
,"Miami packet filter disabled", NULL
);
245 AROS_LH1(void, MiamiPFRemoveHook
,
246 AROS_LHA(APTR
, handle
, A0
),
247 struct MiamiBase
*, MiamiBase
, 17, Miami
252 #if defined(__AROS__)
253 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiPFRemoveHook()\n"));
256 DPF(log("MiamiPFRemoveHook(0x%p) called", handle
);)
258 ObtainSemaphore(&pfil_list_lock
);
260 ReleaseSemaphore(&pfil_list_lock
);
261 bsd_free(handle
, NULL
);
267 AROS_LH1(int, MiamiGetHardwareLen
,
268 AROS_LHA(char *, name
, A0
),
269 struct MiamiBase
*, MiamiBase
, 18, Miami
274 #if defined(__AROS__)
275 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiGetHardwareLen()\n"));
278 __log(LOG_CRIT
,"MiamiGetHardwareLen() is not implemented");
284 AROS_LH1(struct Library
*, MiamiOpenSSL
,
285 AROS_LHA(struct TagItem
*, taglist
, A0
),
286 struct MiamiBase
*, MiamiBase
, 25, Miami
291 #if defined(__AROS__)
292 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiOpenSSL()\n"));
299 AROS_LH0(void, MiamiCloseSSL
,
300 struct MiamiBase
*, MiamiBase
, 26, Miami
305 #if defined(__AROS__)
306 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiCloseSSL()\n"));
312 AROS_LH2(int, MiamiSetSocksConn
,
313 AROS_LHA(struct sockaddr
*, sa
, A0
),
314 AROS_LHA(int, len
, D0
),
315 struct MiamiBase
*, MiamiBase
, 33, Miami
320 #if defined(__AROS__)
321 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiSetSocksConn()\n"));
323 __log(LOG_CRIT
,"MiamiSetSocksConn() is not implemented");
329 AROS_LH1(int, MiamiIsOnline
,
330 AROS_LHA(char *, name
, A0
),
331 struct MiamiBase
*, MiamiBase
, 35, Miami
339 #if defined(__AROS__)
340 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiIsOnline()\n"));
343 DSYSCALLS(__log(LOG_DEBUG
,"MiamiIsOnline(%s) called", (ULONG
)(name
? name
: "<NULL>"));)
344 if (name
&& strcmp(name
,"mi0")) {
347 online
= (ifp
->if_flags
& IFF_UP
) ? TRUE
: FALSE
;
349 for (ifp
= ifnet
; ifp
; ifp
= ifp
->if_next
) {
350 DSYSCALLS(__log(LOG_DEBUG
,"Checking interface %s%u",ifp
->if_name
, ifp
->if_unit
);)
351 if ((ifp
->if_flags
& (IFF_UP
| IFF_LOOPBACK
)) == IFF_UP
) {
357 DSYSCALLS(__log(LOG_DEBUG
,"MiamiIsOnline() result: %ld", online
);)
363 AROS_LH2(void, MiamiOnOffline
,
364 AROS_LHA(char *, name
, A0
),
365 AROS_LHA(int, online
, D0
),
366 struct MiamiBase
*, MiamiBase
, 36, Miami
373 #if defined(__AROS__)
374 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiOnOffline()\n"));
379 ifupdown(ifp
, online
);
384 AROS_LH4(STRPTR
, inet_ntop
,
385 AROS_LHA(LONG
, family
, D0
),
386 AROS_LHA(char *, addrptr
, A0
),
387 AROS_LHA(char *, strptr
, A1
),
388 AROS_LHA(LONG
, len
, D1
),
389 struct MiamiBase
*, MiamiBase
, 38, Miami
394 #if defined(__AROS__)
395 D(bug("[AROSTCP.MIAMI] miami_api.c: inet_ntop()\n"));
398 DSYSCALLS(__log(LOG_DEBUG
,"inet_ntop(%ld) called",family
);)
399 if (family
== AF_INET
)
401 sprintf(strptr
, "%u.%u.%u.%u", addrptr
[0], addrptr
[1], addrptr
[2], addrptr
[3]);
404 __log(LOG_CRIT
,"inet_ntop(): address family %ld is not implemented", family
);
411 AROS_LH2(int, Miami_inet_aton
,
412 AROS_LHA(char *, strptr
, A0
),
413 AROS_LHA(void *, addrptr
, A2
),
414 struct MiamiBase
*, MiamiBase
, 39, Miami
419 #if defined(__AROS__)
420 D(bug("[AROSTCP.MIAMI] miami_api.c: Miami_inet_aton()\n"));
423 DSYSCALLS(syslog(LOG_DEBUG
,"inet_aton(%s) called", (ULONG
)strptr
);)
424 return __inet_aton(strptr
, addrptr
);
429 AROS_LH3(int, inet_pton
,
430 AROS_LHA(LONG
, family
, D0
),
431 AROS_LHA(char *, strptr
, A0
),
432 AROS_LHA(void *, addrptr
, A1
),
433 struct MiamiBase
*, MiamiBase
, 40, Miami
438 #if defined(__AROS__)
439 D(bug("[AROSTCP.MIAMI] miami_api.c: inet_pton()\n"));
442 DSYSCALLS(__log(LOG_DEBUG
,"inet_pton(%ld, %s) called", family
, (ULONG
)strptr
);)
443 if (family
== AF_INET
)
444 return __inet_aton(strptr
, addrptr
);
446 __log(LOG_CRIT
,"inet_pton(): address family %ld is not implemented", family
);
453 AROS_LH2(struct hostent
*, gethostbyname2
,
454 AROS_LHA(const char *, name
, A0
),
455 AROS_LHA(LONG
, family
, D0
),
456 struct MiamiBase
*, MiamiBase
, 41, Miami
461 #if defined(__AROS__)
462 D(bug("[AROSTCP.MIAMI] miami_api.c: gethostbyname2()\n"));
465 DSYSCALLS(__log(LOG_DEBUG
,"gethostbyname2(%s, %ld) called", (ULONG
)name
, family
);)
466 if (family
== AF_INET
)
467 return __gethostbyname(name
, SocketBase
);
469 syslog(LOG_CRIT
,"gethostbyname2(): address family %ld is not implemented", family
);
476 char *ai_errors
[] = {
478 "Address family for host not supported",
479 "Temporary failure in name resolution",
480 "Invalid flags value",
481 "Non-recoverable failure in name resolution",
482 "Address family not supported",
483 "Memory allocation failure",
484 "No address associated with host",
485 "Host nor service provided, or not known",
486 "Service not supported for socket type",
487 "Socket type not supported",
492 AROS_LH1(char *, gai_strerror
,
493 AROS_LHA(LONG
, error
, D0
),
494 struct MiamiBase
*, MiamiBase
, 42, Miami
499 #if defined(__AROS__)
500 D(bug("[AROSTCP.MIAMI] miami_api.c: gai_strerror()\n"));
503 DSYSCALLS(__log(LOG_DEBUG
,"gai_strerror(%ld) called", error
);)
506 return ai_errors
[error
];
511 AROS_LH1(void, freeaddrinfo
,
512 AROS_LHA(struct addrinfo
*, addrinfo
, A0
),
513 struct MiamiBase
*, MiamiBase
, 43, Miami
518 #if defined(__AROS__)
519 D(bug("[AROSTCP.MIAMI] miami_api.c: freeaddrinfo()\n"));
525 AROS_LH4(LONG
, getaddrinfo
,
526 AROS_LHA(char *, hostname
, A0
),
527 AROS_LHA(char *, servicename
, A1
),
528 AROS_LHA(struct addrinfo
*, hintsp
, A2
),
529 AROS_LHA(struct addrinfo
**, result
, A3
),
530 struct MiamiBase
*, MiamiBase
, 44, Miami
535 #if defined(__AROS__)
536 D(bug("[AROSTCP.MIAMI] miami_api.c: getaddrinfo()\n"));
539 __log(LOG_CRIT
,"getaddrinfo() is not implemented");
540 writeErrnoValue(SocketBase
, ENOSYS
);
546 AROS_LH7(LONG
, getnameinfo
,
547 AROS_LHA(struct sockaddr
*, sockaddr
, A0
),
548 AROS_LHA(LONG
, addrlen
, D0
),
549 AROS_LHA(char *, hostname
, A1
),
550 AROS_LHA(LONG
, hostlen
, D1
),
551 AROS_LHA(char *, servicename
, A2
),
552 AROS_LHA(LONG
, servicelen
, D2
),
553 AROS_LHA(LONG
, flags
, D3
),
554 struct MiamiBase
*, MiamiBase
, 45, Miami
559 #if defined(__AROS__)
560 D(bug("[AROSTCP.MIAMI] miami_api.c: getnameinfo()\n"));
563 __log(LOG_CRIT
,"getnameinfo() is not implemented");
569 AROS_LH0(LONG
, MiamiSupportsIPV6
,
570 struct MiamiBase
*, MiamiBase
, 50, Miami
575 #if defined(__AROS__)
576 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiSupportsIPV6()\n"));
579 DSYSCALLS(__log(LOG_DEBUG
,"MiamiSupportsIPV6() called");)
585 AROS_LH0(LONG
, MiamiGetResOptions
,
586 struct MiamiBase
*, MiamiBase
, 51, Miami
591 #if defined(__AROS__)
592 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiResGetOptions()\n"));
595 DSYSCALLS(__log(LOG_DEBUG
,"MiamiResGetOptions() called, result: 0x%p", SocketBase
.res_state
.options
);)
596 return SocketBase
->res_state
.options
;
601 AROS_LH1(void, MiamiSetResOptions
,
602 AROS_LHA(LONG
, options
, D0
),
603 struct MiamiBase
*, MiamiBase
, 52, Miami
608 #if defined(__AROS__)
609 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiResSetOptions()\n"));
612 DSYSCALLS(__log(LOG_DEBUG
,"MiamiResSetOptions(0x%p) called", options
);)
613 SocketBase
->res_state
.options
= options
;
618 AROS_LH1(LONG
, sockatmark
,
619 AROS_LHA(LONG
, sockfd
, D0
),
620 struct MiamiBase
*, MiamiBase
, 53, Miami
625 #if defined(__AROS__)
626 D(bug("[AROSTCP.MIAMI] miami_api.c: sockatmark()\n"));
629 __log(LOG_CRIT
,"sockatmark() is not implemented");
635 AROS_LH3(void, MiamiSupportedCPUs
,
636 AROS_LHA(ULONG
*, apis
, A0
),
637 AROS_LHA(ULONG
*, callbacks
, A1
),
638 AROS_LHA(ULONG
*, kernel
, A2
),
639 struct MiamiBase
*, MiamiBase
, 54, Miami
644 #if defined(__AROS__)
645 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiSupportedCPUs()\n"));
648 DSYSCALLS(__log(LOG_DEBUG
,"MiamiSupportedCPUs() called");)
649 *apis
= MIAMICPU_M68KREG
;
650 *callbacks
= MIAMICPU_M68KREG
;
651 *kernel
= MIAMICPU_PPCV4
;
656 AROS_LH1(LONG
, MiamiGetFdCallback
,
657 AROS_LHA(fdCallback_t
*, cbptr
, A0
),
658 struct MiamiBase
*, MiamiBase
, 55, Miami
663 #if defined(__AROS__)
664 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiGetFdCallback()\n"));
667 *cbptr
= SocketBase
->fdCallback
;
668 DSYSCALLS(__log(LOG_DEBUG
,"MiamiGetFdCallback() called, *cbptr = 0x%p", (ULONG
)*cbptr
);)
669 return *cbptr
? MIAMICPU_M68KREG
: 0;
674 AROS_LH2(LONG
, MiamiSetFdCallback
,
675 AROS_LHA(fdCallback_t
, cbptr
, A0
),
676 AROS_LHA(LONG
, cputype
, D0
),
677 struct MiamiBase
*, MiamiBase
, 56, Miami
682 #if defined(__AROS__)
683 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiSetFdCallback()\n"));
686 DSYSCALLS(__log(LOG_DEBUG
,"MiamiSetFdCallback(0x%p, %ld) called", (ULONG
)cbptr
, cputype
);)
687 if (cputype
== MIAMICPU_M68KREG
) {
688 SocketBase
->fdCallback
= cbptr
;
696 AROS_LH0(void, SetSysLogPort
,
697 struct MiamiBase
*, MiamiBase
, 6, Miami
702 #if defined(__AROS__)
703 D(bug("[AROSTCP.MIAMI] miami_api.c: SetSysLogPort()\n"));
706 DSYSCALLS(__log(LOG_DEBUG
,"SetSysLogPort() called");)
707 ExtLogPort
= FindPort("SysLog");
712 AROS_LH2(int, Miami_sethostname
,
713 AROS_LHA(const char *, name
, A0
),
714 AROS_LHA(size_t, namelen
, D0
),
715 struct MiamiBase
*, MiamiBase
, 23, Miami
720 #if defined(__AROS__)
721 D(bug("[AROSTCP.MIAMI] miami_api.c: Miami_sethostname()\n"));
724 return sethostname(name
, namelen
);
729 AROS_LH2(void, Miami_QueryInterfaceTagList
,
730 AROS_LHA(STRPTR
, interface_name
, A0
),
731 AROS_LHA(struct TagItem
*, tags
, A1
),
732 struct MiamiBase
*, MiamiBase
, 7, Miami
737 #if defined(__AROS__)
738 D(bug("[AROSTCP.MIAMI] miami_api.c: Miami_QueryInterfaceTagList()\n"));
740 // "return" removed, because we are a void function
741 __QueryInterfaceTagList(interface_name
, tags
, SocketBase
);
746 AROS_LH0(struct UserGroupCredentials
*, MiamiGetCredentials
,
747 struct MiamiBase
*, MiamiBase
, 58, Miami
)
751 #if defined(__AROS__)
752 D(bug("[AROSTCP.MIAMI] miami_api.c: MiamiGetCredentials()\n"));
755 /* We don't want to have this library at all, so we open it only if we really need it */
757 UserGroupBase
= OpenLibrary("usergroup.library",4);
759 DSYSCALLS(__log(LOG_DEBUG
,"MiamiGetCredentials(): UserGroupBase = 0x%p", (IPTR
)UserGroupBase
);)
760 /* TODO: uncomment the following lines once we have a working usergroups.library implemenetation */
761 // if (UserGroupBase)
762 // return getcredentials(NULL);