Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dbfb662

Browse files
committed
fix a couple last-minute bugs in the raw socket support
1 parent 96da8b6 commit dbfb662

1 file changed

Lines changed: 24 additions & 24 deletions

File tree

Modules/socketmodule.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ Module interface:
3737
- an AF_PACKET socket address is a tuple containing a string
3838
specifying the ethernet interface and an integer specifying
3939
the Ethernet protocol number to be received. For example:
40-
("eth0",0x1234). Optional 3rd and 4th elements in the tuple
41-
specify packet-type and ha-type -- these are ignored by
40+
("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple
41+
specify packet-type and ha-type/addr -- these are ignored by
4242
networking code, but accepted since they are returned by the
4343
getsockname() method.
4444
@@ -534,7 +534,7 @@ makeipaddr(struct sockaddr_in *addr)
534534

535535
/*ARGSUSED*/
536536
static PyObject *
537-
makesockaddr(struct sockaddr *addr, int addrlen)
537+
makesockaddr(int sockfd, struct sockaddr *addr, int addrlen)
538538
{
539539
if (addrlen == 0) {
540540
/* No address -- may be recvfrom() from known socket */
@@ -575,20 +575,15 @@ makesockaddr(struct sockaddr *addr, int addrlen)
575575
struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
576576
char *ifname = "";
577577
struct ifreq ifr;
578-
int s;
579-
/* need a socket on which we can do an ioctl to look
580-
* up interface name from index, but only if index is
581-
* non-zero.
582-
*/
583-
if (a->sll_ifindex
584-
&& ((s = socket(AF_PACKET, SOCK_RAW, 0)) >= 0)) {
578+
/* need to look up interface name give index */
579+
if (a->sll_ifindex) {
585580
ifr.ifr_ifindex = a->sll_ifindex;
586-
if (ioctl(s, SIOCGIFNAME, &ifr) == 0)
581+
if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
587582
ifname = ifr.ifr_name;
588-
close(s);
589583
}
590-
return Py_BuildValue("shbh", ifname, ntohs(a->sll_protocol),
591-
a->sll_pkttype, a->sll_hatype);
584+
return Py_BuildValue("shbhs#", ifname, ntohs(a->sll_protocol),
585+
a->sll_pkttype, a->sll_hatype,
586+
a->sll_addr, a->sll_halen);
592587
}
593588
#endif
594589

@@ -612,7 +607,8 @@ makesockaddr(struct sockaddr *addr, int addrlen)
612607
through len_ret. */
613608

614609
static int
615-
getsockaddrarg(PySocketSockObject *s, PyObject *args, struct sockaddr **addr_ret, int *len_ret)
610+
getsockaddrarg(PySocketSockObject *s, PyObject *args,
611+
struct sockaddr **addr_ret, int *len_ret)
616612
{
617613
switch (s->sock_family) {
618614

@@ -671,14 +667,17 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, struct sockaddr **addr_ret
671667
int protoNumber;
672668
int hatype = 0;
673669
int pkttype = 0;
670+
char *haddr;
674671

675-
if (!PyArg_ParseTuple(args, "si|ii", &interfaceName,
676-
&protoNumber, &pkttype, &hatype))
672+
if (!PyArg_ParseTuple(args, "si|iis", &interfaceName,
673+
&protoNumber, &pkttype, &hatype, &haddr))
677674
return 0;
678675
strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
679676
ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
680-
if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr))
677+
if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
678+
PyErr_SetFromErrno(PySocket_Error);
681679
return 0;
680+
}
682681
addr = &(s->sock_addr.ll);
683682
addr->sll_family = AF_PACKET;
684683
addr->sll_protocol = htons((short)protoNumber);
@@ -779,11 +778,12 @@ PySocketSock_accept(PySocketSockObject *s, PyObject *args)
779778
SOCKETCLOSE(newfd);
780779
goto finally;
781780
}
782-
if (!(addr = makesockaddr((struct sockaddr *) addrbuf, addrlen)))
781+
addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
782+
addrlen);
783+
if (addr == NULL)
783784
goto finally;
784785

785-
if (!(res = Py_BuildValue("OO", sock, addr)))
786-
goto finally;
786+
res = Py_BuildValue("OO", sock, addr);
787787

788788
finally:
789789
Py_XDECREF(sock);
@@ -1128,7 +1128,7 @@ PySocketSock_getsockname(PySocketSockObject *s, PyObject *args)
11281128
Py_END_ALLOW_THREADS
11291129
if (res < 0)
11301130
return PySocket_Err();
1131-
return makesockaddr((struct sockaddr *) addrbuf, addrlen);
1131+
return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
11321132
}
11331133

11341134
static char getsockname_doc[] =
@@ -1157,7 +1157,7 @@ PySocketSock_getpeername(PySocketSockObject *s, PyObject *args)
11571157
Py_END_ALLOW_THREADS
11581158
if (res < 0)
11591159
return PySocket_Err();
1160-
return makesockaddr((struct sockaddr *) addrbuf, addrlen);
1160+
return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
11611161
}
11621162

11631163
static char getpeername_doc[] =
@@ -1319,7 +1319,7 @@ PySocketSock_recvfrom(PySocketSockObject *s, PyObject *args)
13191319
if (n != len && _PyString_Resize(&buf, n) < 0)
13201320
return NULL;
13211321

1322-
if (!(addr = makesockaddr((struct sockaddr *)addrbuf, addrlen)))
1322+
if (!(addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf, addrlen)))
13231323
goto finally;
13241324

13251325
ret = Py_BuildValue("OO", buf, addr);

0 commit comments

Comments
 (0)