| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| |
| 5 |
|
| #include "ppport.h" |
| 6 |
|
| |
| 7 |
|
| #include <sys/types.h> |
| 8 |
|
| #include <sys/socket.h> |
| 9 |
|
| |
| 10 |
|
| typedef PerlIO* InOutStream; |
| 11 |
|
| typedef int SysRet; |
| 12 |
|
| |
| 13 |
|
| #ifndef PerlIO |
| 14 |
|
| #define PerlIO_fileno(f) fileno(f) |
| 15 |
|
| #endif |
| 16 |
|
| |
| 17 |
|
| static Size_t aligned_cmsghdr_sz = 0; |
| 18 |
|
| |
| 19 |
|
| struct Socket__MsgHdr { |
| 20 |
|
| struct msghdr m; |
| 21 |
|
| struct iovec io; |
| 22 |
|
| }; |
| 23 |
|
| |
| 24 |
|
| static void |
| 25 |
17 |
| hv_2msghdr(struct Socket__MsgHdr *mh, SV *thing) |
| 26 |
|
| { |
| 27 |
|
| HV* hash; |
| 28 |
|
| SV ** svp; |
| 29 |
|
| STRLEN dlen; |
| 30 |
|
| |
| 31 |
17 |
▶ | if (!thing || !sv_isa(thing, "IO::EventMux::Socket::MsgHdr")) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!thing || !sv_isa(thing, "IO::EventMux::Socket::MsgHdr")) |
17 |
0 |
17 |
| if (!thing || !sv_isa(thing, "IO::EventMux::Socket::MsgHdr")) |
0 |
17 |
17 |
|
| 32 |
0 |
| croak("parameter not of type IO::EventMux::Socket::MsgHdr"); |
| 33 |
|
| |
| 34 |
17 |
| hash = (HV*) SvRV(thing); |
| 35 |
|
| |
| 36 |
17 |
| Zero(mh, 1, struct Socket__MsgHdr); |
| 37 |
|
| |
| 38 |
17 |
| mh->m.msg_iov = &mh->io; |
| 39 |
17 |
| mh->m.msg_iovlen = 1; |
| 40 |
|
| |
| 41 |
17 |
▶ | if (svp = hv_fetch(hash, "name", 4, FALSE)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (svp = hv_fetch(hash, "name", 4, FALSE)) { |
17 |
0 |
17 |
|
| 42 |
|
| // If undefined, define it |
| 43 |
17 |
▶ | if(!SvOK(*svp)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(!SvOK(*svp)) { |
8 |
9 |
17 |
|
| 44 |
8 |
| *svp = newSVpv("", 0); |
| 45 |
|
| } |
| 46 |
17 |
| mh->m.msg_name = SvPV_force(*svp, dlen); |
| 47 |
17 |
| mh->m.msg_namelen = dlen; |
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
17 |
▶ | if (svp = hv_fetch(hash, "buf", 3, FALSE)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (svp = hv_fetch(hash, "buf", 3, FALSE)) { |
17 |
0 |
17 |
|
| 51 |
|
| // If undefined, define it |
| 52 |
17 |
▶ | if(!SvOK(*svp)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(!SvOK(*svp)) { |
0 |
17 |
17 |
|
| 53 |
0 |
| *svp = newSVpv("", 0); |
| 54 |
|
| } |
| 55 |
17 |
| mh->io.iov_base = SvPV_force(*svp, dlen); |
| 56 |
17 |
| mh->io.iov_len = dlen; |
| 57 |
|
| } |
| 58 |
|
| |
| 59 |
17 |
▶ | if (svp = hv_fetch(hash, "control", 7, FALSE)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (svp = hv_fetch(hash, "control", 7, FALSE)) { |
17 |
0 |
17 |
|
| 60 |
|
| // If undefined, define it |
| 61 |
17 |
▶ | if(!SvOK(*svp)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(!SvOK(*svp)) { |
14 |
3 |
17 |
|
| 62 |
14 |
| *svp = newSVpv("", 0); |
| 63 |
|
| } |
| 64 |
17 |
| mh->m.msg_control = SvPV_force(*svp, dlen); |
| 65 |
17 |
| mh->m.msg_controllen = dlen; |
| 66 |
|
| } |
| 67 |
|
| |
| 68 |
17 |
▶ | if (svp = hv_fetch(hash, "flags", 5, FALSE)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (svp = hv_fetch(hash, "flags", 5, FALSE)) { |
17 |
0 |
17 |
|
| 69 |
17 |
▶ | if(!SvOK(*svp)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(!SvOK(*svp)) { |
0 |
17 |
17 |
|
| 70 |
0 |
| *svp = newSViv(0); |
| 71 |
|
| } |
| 72 |
17 |
| mh->m.msg_flags = SvIV(*svp); |
| 73 |
|
| } |
| 74 |
17 |
| } |
| 75 |
|
| |
| 76 |
|
| MODULE = IO::EventMux::Socket::MsgHdr PACKAGE = IO::EventMux::Socket::MsgHdr PREFIX = smh_ |
| 77 |
|
| |
| 78 |
|
| SV * |
| 79 |
|
| smh_pack_cmsghdr(...) |
| 80 |
|
| PROTOTYPE: $$$;@ |
| 81 |
|
| PREINIT: |
| 82 |
|
| STRLEN len; |
| 83 |
|
| STRLEN space; |
| 84 |
|
| I32 i; |
| 85 |
|
| struct cmsghdr *cm; |
| 86 |
|
| CODE: |
| 87 |
3 |
| space = 0; |
| 88 |
7 |
▶ | for (i=0; i<items; i+=3) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<items; i+=3) { |
4 |
3 |
7 |
|
| 89 |
4 |
| len = sv_len(ST(i+2)); |
| 90 |
4 |
| space += CMSG_SPACE(len); |
| 91 |
|
| } |
| 92 |
3 |
| RETVAL = newSV( space ); |
| 93 |
3 |
| SvPOK_on(RETVAL); |
| 94 |
3 |
| SvCUR_set(RETVAL, space); |
| 95 |
|
| |
| 96 |
3 |
| cm = (struct cmsghdr *)SvPVX(RETVAL); |
| 97 |
|
| |
| 98 |
7 |
▶ | for (i=0; i<items; i+=3) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<items; i+=3) { |
4 |
3 |
7 |
|
| 99 |
4 |
| len = sv_len(ST(i+2)); |
| 100 |
4 |
| cm->cmsg_len = CMSG_LEN(len); |
| 101 |
4 |
| cm->cmsg_level = SvIV( ST(i) ); |
| 102 |
4 |
| cm->cmsg_type = SvIV( ST(i+1) ); |
| 103 |
4 |
| Copy(SvPVX(ST(i+2)), CMSG_DATA(cm), len, U8); |
| 104 |
4 |
| cm = (struct cmsghdr *)((U8 *)cm + CMSG_SPACE( len )); |
| 105 |
|
| } |
| 106 |
|
| OUTPUT: |
| 107 |
|
| RETVAL |
| 108 |
|
| |
| 109 |
|
| void |
| 110 |
|
| smh_unpack_cmsghdr(cmsv) |
| 111 |
|
| SV* cmsv; |
| 112 |
|
| INIT: |
| 113 |
|
| struct msghdr dummy; |
| 114 |
|
| struct cmsghdr *cm; |
| 115 |
|
| STRLEN len; |
| 116 |
|
| PPCODE: |
| 117 |
2 |
| dummy.msg_control = (struct cmsghdr *) SvPV(cmsv, len); |
| 118 |
2 |
| dummy.msg_controllen = len; |
| 119 |
|
| |
| 120 |
2 |
▶ | if (!len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!len) |
0 |
2 |
2 |
|
| 121 |
0 |
| XSRETURN_EMPTY; |
| 122 |
|
| |
| 123 |
2 |
▶ | cm = CMSG_FIRSTHDR(&dummy); |
Branch: 1 branch
| Branch | True | False | Total |
| cm = CMSG_FIRSTHDR(&dummy); |
2 |
0 |
2 |
|
| 124 |
5 |
▶ | for (; cm; cm = CMSG_NXTHDR(&dummy, cm)) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; cm; cm = CMSG_NXTHDR(&dummy, cm)) { |
3 |
2 |
5 |
|
| 125 |
3 |
▶ | XPUSHs(sv_2mortal(newSViv(cm->cmsg_level))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(cm->cmsg_level))); |
0 |
3 |
3 |
|
| 126 |
3 |
▶ | XPUSHs(sv_2mortal(newSViv(cm->cmsg_type))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(cm->cmsg_type))); |
0 |
3 |
3 |
|
| 127 |
3 |
▶ | XPUSHs(sv_2mortal(newSVpvn(CMSG_DATA(cm), |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvn(CMSG_DATA(cm), |
0 |
3 |
3 |
|
| 128 |
|
| (cm->cmsg_len - aligned_cmsghdr_sz)))); |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
|
| SysRet |
| 132 |
|
| smh_sendmsg(s, msg_hdr, flags = 0) |
| 133 |
|
| InOutStream s; |
| 134 |
|
| SV * msg_hdr; |
| 135 |
|
| int flags; |
| 136 |
|
| |
| 137 |
|
| PROTOTYPE: $$;$ |
| 138 |
|
| PREINIT: |
| 139 |
|
| struct Socket__MsgHdr mh; |
| 140 |
|
| CODE: |
| 141 |
9 |
| hv_2msghdr(&mh, msg_hdr); |
| 142 |
9 |
| RETVAL = sendmsg(PerlIO_fileno(s), &mh.m, flags); |
| 143 |
|
| OUTPUT: |
| 144 |
|
| RETVAL |
| 145 |
|
| |
| 146 |
|
| SysRet |
| 147 |
|
| smh_recvmsg(s, msg_hdr, flags = 0) |
| 148 |
|
| InOutStream s; |
| 149 |
|
| SV * msg_hdr; |
| 150 |
|
| int flags; |
| 151 |
|
| |
| 152 |
|
| PROTOTYPE: $$;$ |
| 153 |
|
| PREINIT: |
| 154 |
|
| struct Socket__MsgHdr mh; |
| 155 |
|
| struct cmsghdr *cm; |
| 156 |
|
| |
| 157 |
|
| CODE: |
| 158 |
8 |
| hv_2msghdr(&mh, msg_hdr); |
| 159 |
8 |
▶ | if ((RETVAL = recvmsg(PerlIO_fileno(s), &mh.m, flags)) >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((RETVAL = recvmsg(PerlIO_fileno(s), &mh.m, flags)) >= 0) { |
7 |
1 |
8 |
|
| 160 |
|
| SV** svp; |
| 161 |
|
| HV* hsh; |
| 162 |
|
| |
| 163 |
7 |
| hsh = (HV*) SvRV(msg_hdr); |
| 164 |
|
| |
| 165 |
7 |
▶ | if (svp = hv_fetch(hsh, "name", 7, FALSE)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (svp = hv_fetch(hsh, "name", 7, FALSE)) |
0 |
7 |
7 |
|
| 166 |
0 |
| SvCUR_set(*svp, mh.m.msg_namelen); |
| 167 |
7 |
▶ | if (svp = hv_fetch(hsh, "buf", 3, FALSE)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (svp = hv_fetch(hsh, "buf", 3, FALSE)) |
7 |
0 |
7 |
|
| 168 |
7 |
| SvCUR_set(*svp, RETVAL); |
| 169 |
7 |
▶ | if (svp = hv_fetch(hsh, "control", 7, FALSE)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (svp = hv_fetch(hsh, "control", 7, FALSE)) |
7 |
0 |
7 |
|
| 170 |
7 |
| SvCUR_set(*svp, mh.m.msg_controllen); |
| 171 |
|
| } |
| 172 |
|
| OUTPUT: |
| 173 |
|
| RETVAL |
| 174 |
|
| |
| 175 |
|
| BOOT: |
| 176 |
3 |
| aligned_cmsghdr_sz = CMSG_LEN(0); |